-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
137 lines (117 loc) · 2.98 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const { getToken, getSecretPayload, getSecretIdByPackage } = require('./lib')
const load = async ({
secrets,
saKeyFile,
iamToken,
logger,
iamEndpoint,
lockboxEndpoint,
} = { }) => {
const secretList = (typeof secrets === 'string' || secrets instanceof String) ? [secrets] : secrets
if (logger == null) {
const pino = require('pino')
logger = pino({
level: 'info',
formatters: {
// eslint-disable-next-line no-unused-vars
level(label, number) {
return { level: label }
},
},
})
}
const apiToken = await getToken({ iamToken, saKeyFile, iamEndpoint })
const result = {}
for (let i = 0; i < secretList.length; i += 1) {
const entries = await getSecretPayload(secretList[i], apiToken, { logger, lockboxEndpoint })
entries.forEach((entry) => {
if (entry.textValue) {
result[entry.key] = entry.textValue
}
})
}
return result
}
const config = async ({
secrets,
saKeyFile,
iamToken,
envPrefix = false,
oneTimeRead = false,
logger,
iamEndpoint,
lockboxEndpoint,
folderId,
} = { }) => {
if (process.env.LOCKBOX_SECRET_ID) {
secrets = process.env.LOCKBOX_SECRET_ID
}
if (process.env.LOCKBOX_FOLDER_ID) {
folderId = process.env.LOCKBOX_FOLDER_ID
}
let secretList = (typeof secrets === 'string' || secrets instanceof String) ? [secrets] : secrets
if (logger == null) {
const pino = require('pino')
logger = pino({
level: 'info',
formatters: {
// eslint-disable-next-line no-unused-vars
level(label, number) {
return { level: label }
},
},
})
}
const apiToken = await getToken({ iamToken, saKeyFile, iamEndpoint })
if (folderId != null && (secretList == null || secretList.length < 1 || secretList[0] == null)) {
secretList = await getSecretIdByPackage(folderId, apiToken, {
logger, lockboxEndpoint,
})
}
for (let i = 0; i < secretList.length; i += 1) {
const entries = await getSecretPayload(secretList[i], apiToken, { logger, lockboxEndpoint })
entries.forEach((entry) => {
if (entry.textValue) {
let textValue = entry.textValue
Object.defineProperty(process.env, `${envPrefix || ''}${envPrefix ? '_' : ''}${entry.key}`, {
get() {
const v = textValue
if (oneTimeRead) {
textValue = undefined
if (!v) logger.warn({ msg: 'lockbox-env value cleared after first time read' })
}
return v
},
})
}
})
}
}
const configSync = ({
secrets,
saKeyFile,
iamToken,
envPrefix = false,
oneTimeRead = false,
logger,
iamEndpoint,
lockboxEndpoint,
folderId,
} = { }) => {
const sp = require('synchronized-promise')
const sync = sp(config)
return sync({
secrets,
saKeyFile,
iamToken,
envPrefix,
oneTimeRead,
logger,
iamEndpoint,
lockboxEndpoint,
folderId,
})
}
module.exports = {
config, configSync, load,
}