forked from serverless/serverless-secrets-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (111 loc) · 4.16 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
'use strict';
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const BbPromise = require('bluebird');
const algorithm = 'aes-256-cbc';
class ServerlessSecretsPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.secretsFile = serverless.service.custom.secretsFile; // The file configured in the app's yml serverless config
if (this.serverless.service.custom && this.serverless.service.custom.secretsFilePathPrefix) {
this.customPath = this.serverless.service.custom.secretsFilePathPrefix;
} else {
this.customPath = '';
}
const commandOptions = {
stage: {
usage: 'Stage of the file to encrypt',
shortcut: 's',
required: true,
},
password: {
usage: 'Password to encrypt the file.',
shortcut: 'p',
required: true,
},
secretsFile: {
usage: 'The file to encrypt',
required: false,
},
};
this.commands = {
encrypt: {
usage: 'Encrypt a secrets file for a specific stage.',
lifecycleEvents: [
'encrypt',
],
options: commandOptions,
},
decrypt: {
usage: 'Decrypt a secrets file for a specific stage.',
lifecycleEvents: [
'decrypt',
],
options: commandOptions,
},
};
this.hooks = {
'encrypt:encrypt': this.encrypt.bind(this),
'decrypt:decrypt': this.decrypt.bind(this),
'package:cleanup': this.checkFileExists.bind(this),
};
}
encrypt() {
return new BbPromise((resolve, reject) => {
const servicePath = this.serverless.config.servicePath;
const customPath = this.customPath;
const credentialFileName = this.options.secretsFile || this.secretsFile || `secrets.${this.options.stage}.yml`;
const encryptedCredentialFileName = `${credentialFileName}.encrypted`;
const secretsPath = path.join(servicePath, customPath, credentialFileName);
const encryptedCredentialsPath = path.join(servicePath, customPath, encryptedCredentialFileName);
fs.createReadStream(secretsPath)
.on('error', reject)
.pipe(crypto.createCipher(algorithm, this.options.password))
.on('error', reject)
.pipe(fs.createWriteStream(encryptedCredentialsPath))
.on('error', reject)
.on('close', () => {
this.serverless.cli.log(`Successfully encrypted '${credentialFileName}' to '${encryptedCredentialFileName}'`);
resolve();
});
});
}
decrypt() {
return new BbPromise((resolve, reject) => {
const servicePath = this.serverless.config.servicePath;
const customPath = this.customPath;
const credentialFileName = this.options.secretsFile || this.secretsFile || `secrets.${this.options.stage}.yml`;
const encryptedCredentialFileName = `${credentialFileName}.encrypted`;
const secretsPath = path.join(servicePath, customPath, credentialFileName);
const encryptedCredentialsPath = path.join(servicePath, customPath, encryptedCredentialFileName);
fs.createReadStream(encryptedCredentialsPath)
.on('error', reject)
.pipe(crypto.createDecipher(algorithm, this.options.password))
.on('error', reject)
.pipe(fs.createWriteStream(secretsPath))
.on('error', reject)
.on('close', () => {
this.serverless.cli.log(`Successfully decrypted '${encryptedCredentialFileName}' to '${credentialFileName}'`);
resolve();
});
});
}
checkFileExists() {
return new BbPromise((resolve, reject) => {
const servicePath = this.serverless.config.servicePath;
const customPath = this.customPath;
const credentialFileName = this.options.secretsFile || this.secretsFile || `secrets.${this.options.stage}.yml`;
const secretsPath = path.join(servicePath, customPath, credentialFileName);
fs.access(secretsPath, fs.F_OK, (err) => {
if (err) {
reject(`Couldn't find the secrets file for this stage: ${credentialFileName}`);
} else {
resolve();
}
});
});
}
}
module.exports = ServerlessSecretsPlugin;