-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
238 lines (224 loc) · 6.69 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
const exec = require('@actions/exec');
const os = require('os');
const core = require('@actions/core');
const tc = require('@actions/tool-cache');
const { Octokit } = require("@octokit/rest");
let octokit;
const token = core.getInput('github-token');
const userAgent = 'slv-action';
if (token) {
const { createTokenAuth } = "@octokit/auth-token";
octokit = new Octokit({
authStrategy: createTokenAuth,
auth: token,
userAgent: userAgent,
});
} else {
octokit = new Octokit({
userAgent: userAgent,
});
}
const owner = 'amagioss';
const repo = 'slv';
const assetName = 'slv';
async function findInstalledVersion() {
const options = {
silent: true,
failOnStdErr: true
};
try {
const execOutput = await exec.getExecOutput('slv', ['--version'], options);
let installedVersion = '';
const lines = execOutput.stdout.split("\n");
lines.forEach(line => {
if (line.toLowerCase().includes("slv version")) {
installedVersion = line.split(':')[1];
installedVersion = installedVersion.trim();
if (installedVersion.startsWith('v')) {
installedVersion = installedVersion.slice(1);
}
}
});
core.saveState('SLV_VERSION_INSTALLED', installedVersion);
return installedVersion;
} catch (error) {
return '';
}
}
async function getInstalledVersion() {
let installedVersion = core.getState('SLV_VERSION_INSTALLED');
if (installedVersion === undefined || installedVersion === '') {
installedVersion = await findInstalledVersion();
}
if (installedVersion) {
core.info(`Installed version of SLV: ${ installedVersion }`);
}
return installedVersion;
}
async function getLatestVersion() {
try {
let latestVersion = core.getState('SLV_VERSION_LATEST');
if (latestVersion === undefined || latestVersion === '') {
core.info('Fetching latest release version from GitHub...');
const latestRelease = await octokit.rest.repos.getLatestRelease({
owner: owner,
repo: repo
});
latestVersion = latestRelease.data.tag_name;
if (latestVersion.startsWith('v')) {
latestVersion = latestVersion.slice(1);
}
core.saveState('SLV_VERSION_LATEST', latestVersion);
}
return latestVersion;
} catch (error) {
core.setFailed('Error retrieving latest release version:' + error.message);
}
}
function mapArch(arch) {
const mappings = {
x32: '386',
x64: 'amd64'
};
return mappings[arch] || arch;
}
function mapOS(os) {
const mappings = {
win32: 'windows'
};
return mappings[os] || os;
}
async function getDownloadUrlForVersion(version) {
const platform = os.platform();
try {
const release = await octokit.rest.repos.getReleaseByTag({
owner: owner,
repo: repo,
tag: `v${ version }`
});
const assets = release.data.assets;
if (assets.length === 0) {
core.setFailed('No assets found in the release version ' + version);
}
for (let i = 0; i < assets.length; i++) {
if (assets[i].name.includes(assetName + '_') &&
assets[i].name.includes(mapOS(platform)) &&
assets[i].name.includes(mapArch(os.arch())) &&
assets[i].name.includes('.zip')) {
return assets[i].browser_download_url;
}
}
} catch (error) {
core.setFailed('Error retrieving download URL for version ' + version + ': ' + error.message);
return '';
}
core.setFailed('No assets found for current the platform (' + mapOS(platform) +
'-' + mapArch(os.arch()) + ') in the release version ' + version);
return '';
}
async function installVersion(version) {
try {
const downloadUrl = await getDownloadUrlForVersion(version);
if (!downloadUrl) {
return;
}
const pathToZip = await tc.downloadTool(downloadUrl);
const pathToCLI = await tc.extractZip(pathToZip);
core.addPath(pathToCLI);
const installed_version = await findInstalledVersion();
if (installed_version === version) {
core.info(`Successfully installed SLV version ${ version }`);
} else {
core.setFailed(`Failed to install SLV version ${ version }`);
}
} catch (e) {
core.setFailed(e);
}
}
async function setup() {
try {
const installedVersion = await getInstalledVersion();
let requiredVersion = core.getInput('version');
if (!requiredVersion || requiredVersion === 'latest') {
requiredVersion = await getLatestVersion();
} else if (requiredVersion.startsWith('v')) {
requiredVersion = requiredVersion.slice(1);
}
if (installedVersion && installedVersion === requiredVersion) {
core.info(`Required version SLV ${ installedVersion } is already installed`);
return;
}
await installVersion(requiredVersion);
} catch (e) {
core.setFailed(e);
}
}
async function getSecrets(vaultFile, slvEnvSecretKey) {
const options = {
env: {
['SLV_ENV_SECRET_KEY']: slvEnvSecretKey
},
silent: true,
ignoreReturnCode: true
};
let withMetadata = true;
let execOutput = await exec.getExecOutput('slv', ['vault', 'export', '-v', vaultFile, '--format', 'json', '--with-metadata'], options);
if (execOutput.exitCode !== 0) {
execOutput = await exec.getExecOutput('slv', ['vault', 'export', '-v', vaultFile, '--format', 'json'], options);
withMetadata = false;
}
if (execOutput.exitCode === 0) {
let vaultData = JSON.parse(execOutput.stdout);
if (!withMetadata) {
for (const key in vaultData) {
vaultData[key] = {
value: vaultData[key],
secret: true
};
}
}
return vaultData;
}
core.setFailed('Failed to get secrets: ' + execOutput.stderr);
}
async function injectSecrets() {
const slvEnvSecretKey = core.getInput('env-secret-key');
const vaultFile = core.getInput('vault');
const selectiveStr = core.getInput('selective');
let selectiveSet = new Set(
selectiveStr
? selectiveStr.split(',').map(item => item.trim())
: []
);
if (vaultFile) {
if (!slvEnvSecretKey) {
core.setFailed('SLV environment secret key is required');
}
const vaultData = await getSecrets(vaultFile, slvEnvSecretKey);
let prefix = core.getInput('prefix');
if (!prefix) {
prefix = '';
}
for (const key in vaultData) {
const prefixedKey = prefix + key
if (selectiveSet.size === 0 || selectiveSet.has(key) || selectiveSet.has(prefixedKey)) {
if (vaultData[key].secret) {
core.setSecret(vaultData[key].value);
}
core.exportVariable(prefixedKey, vaultData[key].value);
}
}
}
}
async function run() {
try {
await setup();
await injectSecrets();
} catch (error) {
core.setFailed(error.message);
}
}
module.exports = run
if (require.main === module) {
run();
}