-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
221 lines (196 loc) · 5 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const fs = require("fs"),
yaml = require("yaml"),
ini = require("ini"),
cla = require("command-line-args"),
github = require("./lib/github"),
packageReader = require("./lib/package-reader"),
util = require('util'),
debuglog = util.debuglog('app'),
path = require('path');
const optionDefinitions = [
{
name: "token",
alias: "t",
type: String,
description: "GitHub OAuth2 token",
},
{
name: "repo",
alias: "r",
type: String,
description: 'GitHub repo path as "owner/repo"',
},
{
name: "stage",
alias: "s",
type: String,
description: "Deployment stage, e.g. dev, staging, prod",
},
{
name: "configPath",
alias: "c",
type: String,
description: "Path to serverless config file"
},
{
name: "configFilePattern",
alias: "f",
type: String,
description: "Match pattern for serverless config file containing values to write to GitHub Actions Secrets. Replace tokens supported: {stage}"
},
{
name: "noConfigFile",
type: Boolean,
description: "If true, config file is ignored"
},
{
name: "prefixWithStage",
alias: "w",
type: Boolean,
description: "If true, secret names will be prefixed by {stage}{separator}"
},
{
name: "prefixSeparator",
alias: "e",
type: String,
description: "Separator between prefix and secret name"
},
{
name: "operation",
alias: "o",
type: String,
description: "One of: write, writeOne, list, get, cred",
default: "write",
},
{
name: "secretName",
alias: "n",
type: String,
description: "GitHub secret name (for writeOne)",
},
{
name: "secretValue",
type: String,
description: "GitHub secret value (for writeOne)",
},
{
name: "awsProfileName",
alias: "p",
type: String,
description: "AWS profile name from credentials file",
},
];
var configFile = '';
const options = cla(optionDefinitions);
const pkg = new packageReader();
if (!options.token) {
if (!process.env.GITHUB_ACCESS_TOKEN) {
throw new Error("Token must be specified");
}
options.token = process.env.GITHUB_ACCESS_TOKEN;
}
if (!options.repo) {
options.repo = pkg.getRepo();
}
if (!options.stage) {
try {
options.stage = pkg.getStage();
} catch (e) {
console.log(e);
options.stage = null;
console.log("Stage not available - exiting...");
process.exit();
}
}
if (!options.noConfigFile && !options.configPath) {
options.configPath = pkg.getConfigPath();
}
if (!options.noConfigFile && !options.configFilePattern) {
options.configFilePattern = pkg.getConfigFilePattern();
}
if (!options.prefixWithStage) {
options.prefixWithStage = pkg.getPrefixWithStage();
}
if (!options.prefixSeparator) {
options.prefixSeparator = pkg.getPrefixSeparator();
}
var prefix = '';
if (options.prefixWithStage && options.prefixSeparator) {
prefix = `${options.stage}${options.prefixSeparator}`;
}
// Resolve config file if options allow
if (!options.noConfigFile) {
configFile = path.resolve(
options.configPath,
options.configFilePattern.replace('{stage}', options.stage)
);
}
var tokenRedacted = options.token.replace(/(\w{5})\w+/, '$1xxxxxxxxxx');
debuglog(`Running with options: ${tokenRedacted} ${options.repo} ${options.stage} ${options.noConfigFile} ${configFile}`);
const gitHubAPI = new github({
token: options.token,
repoPath: options.repo,
prefix: prefix
});
switch (options.operation) {
case "write": {
console.log("Reading " + configFile);
const configDoc = yaml.parse(fs.readFileSync(configFile, "utf8"));
Object.keys(configDoc).forEach((configKey) => {
gitHubAPI.writeToGitHub(configKey, configDoc[configKey]);
});
break;
}
case "writeOne": {
gitHubAPI.writeToGitHub(options.secretName, options.secretValue);
break;
}
case "cred": {
if (!options.awsProfileName) {
console.error(
'AWS profile name must be specified for operation type "cred"'
);
process.exit();
}
const configFile = `${process.env.HOME}/.aws/credentials`;
console.log("Reading " + configFile);
const configDoc = ini.parse(fs.readFileSync(configFile, "utf8"));
gitHubAPI.writeToGitHub(
"AWS_ACCESS_KEY_ID",
configDoc[options.awsProfileName].aws_access_key_id
);
gitHubAPI.writeToGitHub(
"AWS_SECRET_ACCESS_KEY",
configDoc[options.awsProfileName].aws_secret_access_key
);
break;
}
case "list": {
gitHubAPI.listSecrets();
break;
}
case "get": {
if (!options.secretName) {
console.error('Secret name must be specified for operation type "get"');
process.exit();
}
gitHubAPI.getSecret(options.secretName);
break;
}
case "delete": {
if (!options.secretName) {
console.error('Secret name must be specified for operation type "delete"');
process.exit();
}
gitHubAPI.deleteSecret(options.secretName);
break;
}
case "connect": {
gitHubAPI.connect();
break;
}
default: {
console.log("No operation specified");
break;
}
}