-
Notifications
You must be signed in to change notification settings - Fork 5
/
auth.js
68 lines (63 loc) · 1.92 KB
/
auth.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
const core = require('@actions/core');
const akeylessApi = require('./akeyless_api');
const akeyless = require('akeyless')
const akeylessCloudId = require('akeyless-cloud-id')
function action_fail(message) {
core.debug(message);
core.setFailed(message);
throw new Error(message);
}
async function jwtLogin(apiUrl, accessId) {
api = akeylessApi.api(apiUrl);
core.debug(apiUrl);
let githubToken = undefined;
let akeylessResponse = undefined;
try {
core.debug('Fetching JWT from Github');
githubToken = await core.getIDToken();
} catch (error) {
action_fail(`Failed to fetch Github JWT: ${error.message}`);
}
try {
core.debug('Fetching token from AKeyless');
return api.auth(akeyless.Auth.constructFromObject({
'access-type': 'jwt',
'access-id': accessId,
'jwt': githubToken,
}));
} catch (error) {
action_fail(`Failed to login to AKeyless: ${error.message}`);
}
}
async function awsIamLogin(apiUrl, accessId) {
api = akeylessApi.api(apiUrl);
try {
const cloudId = await akeylessCloudId();
} catch (error) {
action_fail(`Failed to fetch cloud id: ${error.message}`);
}
try {
return api.auth(akeyless.Auth.constructFromObject({
'access-type': 'aws_iam',
'access-id': accessId,
'cloud-id': cloudId,
}));
} catch (error) {
action_fail(`Failed to login to AKeyless: ${error.message}`);
}
}
const login = {
'jwt': jwtLogin,
'aws_iam': awsIamLogin,
}
const allowedAccessTypes = Object.keys(login);
async function akeylessLogin(accessId, accessType, apiUrl) {
try {
core.debug('fetch token');
return login[accessType](apiUrl, accessId);
} catch (error) {
action_fail(error.message);
}
};
exports.akeylessLogin = akeylessLogin;
exports.allowedAccessTypes = allowedAccessTypes;