-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
78 lines (60 loc) · 2.35 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
const { uploadParam, deleteParam } = require('./src/paramsStore');
const {
getTags,
getParamName,
getDescription,
getApiKey,
getKmsKeyId,
getTier,
getEnabled
} = require('./src/configuration');
const getSsm = (serverless) => {
const provider = serverless.getProvider('aws');
const awsCredentials = provider.getCredentials();
awsCredentials.region = provider.getRegion();
return new provider.sdk.SSM(awsCredentials);
};
const makePutParameter = (ssm) => async (params) => ssm.putParameter(params).promise();
const makeGetParameter = (ssm) => async (params) => ssm.getParameter(params).promise();
const makeAddTagsToResource = (ssm) => async (params) => ssm.addTagsToResource(params).promise();
const makeDeleteParameter = (ssm) => async (params) => ssm.deleteParameter(params).promise();
const upload = (serverless) => {
if (!getEnabled(serverless.service)) return;
const ssm = getSsm(serverless);
const putParameter = makePutParameter(ssm);
const addTagsToResource = makeAddTagsToResource(ssm);
const getParameter = makeGetParameter(ssm);
return uploadParam(putParameter, addTagsToResource, getParameter, serverless.cli)(
getApiKey(serverless),
getParamName(serverless.service),
getDescription(serverless.service),
getTier(serverless.service),
getTags(serverless.service),
getKmsKeyId(serverless.service)
);
};
const remove = (serverless) => {
if (!getEnabled(serverless.service)) return;
const ssm = getSsm(serverless);
const deleteParameter = makeDeleteParameter(ssm);
const paramName = getParamName(serverless.service);
return deleteParam(deleteParameter)(paramName);
};
class ServerlessApiKeyUploadToParameterStore {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
if (serverless.service.provider.apiKeys && serverless.service.provider.apiKeys.length === 1) {
this.hooks = this.setupHooks();
} else {
this.serverless.cli.log('serverless-api-key-upload works only for one api key');
}
}
setupHooks() {
return {
'after:deploy:deploy': () => upload(this.serverless),
'before:remove:remove': () => remove(this.serverless)
};
}
}
module.exports = ServerlessApiKeyUploadToParameterStore;