-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoteJSONEnvs.js
190 lines (176 loc) · 6.1 KB
/
RemoteJSONEnvs.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"use strict";
const ssm = require("serverless/lib/configuration/variables/sources/instance-dependent/get-ssm");
const s3 = require("serverless/lib/configuration/variables/sources/instance-dependent/get-s3");
const ServerlessError = require("serverless/lib/serverless-error");
class RemoteJSONEnvs {
constructor(serverless) {
this.serverless = serverless;
this.hooks = {
"package:compileEvents": () => this.packageCompile(),
};
this.ssm = ssm;
}
async packageCompile() {
this.storeConfig = this.getRemoteJSONEnvsConfig(
this.serverless.configurationInput
);
const envValues = await this.resolveEnvs();
const valuesWithMetadata = this.extractSecrets(envValues);
const mergedSecrets = this.mergeSecrets(valuesWithMetadata);
this.mergeVariables(mergedSecrets);
// console.log(
// this.serverless.service.provider.compiledCloudFormationTemplate.Resources
// .HelloDashworldLambdaFunction.Properties.Environment
// );
}
async resolveEnvs() {
let envs = {};
if (this.storeConfig.provider == "SSMParameterStore") {
envs = await this.resolveSSM();
} else if (this.storeConfig.provider == "S3") {
envs = await this.resolveS3();
}
return envs;
}
resolveS3(resolver) {
let S3Resolver = s3(this.serverless);
if (resolver) S3Resolver = resolver;
const toResolve = this.storeConfig.keys.map((key) => {
return S3Resolver.resolve({ address: key.key });
});
return Promise.all(toResolve)
.then((results) => {
results[0].value = JSON.parse(results[0].value);
return results;
})
.catch((e) => {
throw new ServerlessError(e);
});
}
mergeVariables(mergedSecrets) {
let currentVariables;
Object.keys(
this.serverless.service.provider.compiledCloudFormationTemplate.Resources
).forEach((lambda) => {
if (
this.serverless.service.provider.compiledCloudFormationTemplate
.Resources[lambda]["Type"] === "AWS::Lambda::Function"
) {
currentVariables = {};
if (
this.serverless.service.provider.compiledCloudFormationTemplate
.Resources[lambda].Properties.Environment
) {
currentVariables =
this.serverless.service.provider.compiledCloudFormationTemplate
.Resources[lambda].Properties.Environment.Variables;
} else {
this.serverless.service.provider.compiledCloudFormationTemplate.Resources[
lambda
].Properties.Environment = {};
}
this.serverless.service.provider.compiledCloudFormationTemplate.Resources[
lambda
].Properties.Environment.Variables = {
...currentVariables,
...mergedSecrets,
};
}
});
}
mergeSecrets(valuesWithMetadata) {
let merge = {};
valuesWithMetadata.forEach((value) => {
merge = { ...merge, ...value.value };
});
return merge;
}
extractSecrets(values) {
const valuesWithMetadata = this.storeConfig.keys.map((key, index) => {
if (values[index].value == null) {
this.serverless.cli.consoleLog(
`RemoteJSONEnvsPlugin Warning: "${key.key}" key not found`
);
return {};
}
key.value = values[index].value[`${key.secretJSONKey}`];
if (!values[index].value[`${key.secretJSONKey}`])
this.serverless.cli.consoleLog(
`RemoteJSONEnvsPlugin Warning: "${key.secretJSONKey}" its not a valid secretJSONKey for ${key.key} value`
);
return key;
});
return valuesWithMetadata;
}
resolveSSM(resolver) {
let SSMResolver = ssm(this.serverless);
if (resolver) SSMResolver = resolver;
const toResolve = this.storeConfig.keys.map((key) => {
return SSMResolver.resolve({ address: key.key });
});
return Promise.all(toResolve)
.then((results) => results)
.catch((e) => {
throw new ServerlessError(e);
});
}
getRemoteJSONEnvsConfig(configurationInput) {
if (!configurationInput.custom || !configurationInput.custom.RemoteJSONEnvs)
return;
if (!configurationInput.custom.RemoteJSONEnvs.provider) {
throw new ServerlessError(`RemoteJSONEnvs Provider its not defined`);
}
const RemoteJSONEnvsProvider =
this.getRemoteJSONEnvsProvider(configurationInput);
return RemoteJSONEnvsProvider;
}
getRemoteJSONEnvsProvider(configurationInput) {
switch (Object.keys(configurationInput.custom.RemoteJSONEnvs.provider)[0]) {
case "aws":
const AWSConfig = configurationInput.custom.RemoteJSONEnvs.provider.aws;
if (
Object.keys(AWSConfig)[0] != "SSMParameterStore" &&
Object.keys(AWSConfig)[0] != "S3"
)
throw new ServerlessError(
`RemoteJSONEnvsPlugin AWS Provider its not correctly configured, SSMParameterStore or S3 are supported`
);
const AWSSSMConfig =
configurationInput.custom.RemoteJSONEnvs.provider.aws[
`${Object.keys(AWSConfig)[0]}`
];
if (!AWSSSMConfig)
throw new ServerlessError(
`RemoteJSONEnvs Provider aws its not configured`
);
if (typeof AWSSSMConfig != "object" || !Array.isArray(AWSSSMConfig)) {
throw new ServerlessError(
`RemoteJSONEnvs Provider AWS ${
Object.keys(AWSConfig)[0]
} its not an array object`
);
}
AWSSSMConfig.forEach((element) => {
if (!element.key)
throw new ServerlessError(
`RemoteJSONEnvs Provider aws-ssm array must contain a key`
);
if (typeof element.key != "string")
throw new ServerlessError(
`RemoteJSONEnvs Provider aws-ssm keys are not strings: ${element.key}`
);
});
return {
provider: Object.keys(AWSConfig)[0],
keys: AWSSSMConfig,
};
default:
throw new ServerlessError(
`Provider ${
Object.keys(configurationInput.custom.RemoteJSONEnvs.provider)[0]
} its not defined`
);
}
}
}
module.exports = RemoteJSONEnvs;