This repository has been archived by the owner on Dec 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (112 loc) · 3.8 KB
/
index.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
138
139
140
141
const AWS = require('aws-sdk');
const _ = require('lodash');
const fs = require('fs');
class Setec {
constructor(configOrConfigfile) {
this.config = Setec.getConfig(configOrConfigfile);
this.awsConfig = this.config.aws || {};
AWS.config.update(_.omit(this.awsConfig, 'role'));
this.setecConfig = this.config.setec || {};
/* eslint-disable no-console */
this.logger = this.config.logger || console.log;
/* eslint-enable no-console */
}
async loadSecret(secretName) {
const ssm = new AWS.SSM(this.awsConfig);
const prefix = _.get(this.setecConfig, 'prefix', '');
const fullSecretName = `${prefix}${secretName}`;
try {
const secretObject = await new Promise(
(resolve, reject) => ssm.getParameter(
{ Name: fullSecretName, WithDecryption: true },
(err, result) => {
if (err) reject(err);
else resolve(result);
},
),
);
const secretValue = secretObject.Parameter.Value;
return secretValue;
} catch (error) {
throw new Error(`error resolving secret "${fullSecretName}": ${error.message}`);
}
}
async loadSecrets(config) {
if (_.isArray(config)) {
return Promise.all(config.map(value => this.loadSecrets(value)));
}
if (_.isObject(config)) {
if (_.has(config, 'secret') && Object.keys(config).length === 1) {
return this.loadSecret(config.secret);
}
const keyValPromises = Object.keys(config).map(async key => [
key,
await this.loadSecrets(config[key]),
]);
return Promise
.all(keyValPromises)
.then(keyVals => keyVals.reduce(
(acc, [key, val]) => Object.assign(acc, { [key]: val }),
{},
));
}
return config;
}
static getConfig(configOrConfigfile) {
if (_.isString(configOrConfigfile)) {
const configFile = configOrConfigfile;
const ext = configFile.split('.').pop();
const hasExt = /\/[^/]+\.[^.]+$/.test(configFile);
if (!hasExt) throw new Error(`config file "${configFile}" has no extension`);
else if (ext === 'json') return JSON.parse(fs.readFileSync(configFile));
// eslint-disable-next-line global-require, import/no-dynamic-require
else if (ext === 'js') return require(configFile);
else throw new Error(`config file "${configFile}" has unknown extension "${ext}"`);
}
if (_.isObject(configOrConfigfile)) return configOrConfigfile;
throw new Error('Invalid config, must be either a string filename or object');
}
async assumeRole({ role }) {
this.logger(`assuming role ${role}`);
const sts = new AWS.STS();
const result = await new Promise(
(resolve, reject) => sts.assumeRole(
{
RoleArn: role,
RoleSessionName: 'local-developer',
DurationSeconds: 3600,
},
(err, assumeResult) => {
if (err) reject(new Error(`unable to assume role "${role}": ${err.message}`));
else resolve(assumeResult);
},
),
);
AWS.config.update({
accessKeyId: result.Credentials.AccessKeyId,
secretAccessKey: result.Credentials.SecretAccessKey,
sessionToken: result.Credentials.SessionToken,
});
this.logger(`assumed role ${role}`);
}
async load() {
if (this.loaded) return this.config;
if (this.awsConfig.role) {
if (this.config.production) {
throw new Error(
'will not assume a role in a production config',
);
}
await this.assumeRole(this.awsConfig);
}
const resolvedConfig = await this.loadSecrets(this.config);
Object.assign(this.config, resolvedConfig);
this.loaded = true;
return this.config;
}
exportable() {
this.config.load = this.load.bind(this);
return this.config;
}
}
module.exports = Setec;