-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
53 lines (44 loc) · 2.02 KB
/
app.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
const dotenv = require('dotenv')
dotenv.config()
const { awsCredentialsPath, useAccountId} = require('./params')
const ConfigParser = require('configparser')
const {
getAccountRoleCredentials,
getAccountRoles,
getAccounts,
getAccessToken,
authorizeDevice,
registerClient} = require("./aws")
const {error} = require("./util")
const config = new ConfigParser()
const updateCredentials = async () => {
// search for credentials file first, default: ~/.aws/credentials
try {
await config.readAsync(awsCredentialsPath)
} catch (e){
error('cannot open file: ' + awsCredentialsPath)
}
// start authentication flow
const { clientId, clientSecret } = await registerClient()
// needs to the user to be fully logged in
const { deviceCode, userCode } = await authorizeDevice(clientId, clientSecret)
const { accessToken } = await getAccessToken(clientId, clientSecret, deviceCode, userCode)
const { accountList } = await getAccounts(accessToken)
for (const { accountId, accountName } of accountList) {
const { roleList } = await getAccountRoles(accessToken, accountId)
for (const { roleName } of roleList) {
const { accessKeyId, secretAccessKey, sessionToken } = await getAccountRoleCredentials(accessToken, accountId, roleName)
// default format is [account-name_AWSRoleName]
const account_section_name = useAccountId ? accountId : accountName + '_' + roleName
!config.sections().includes(account_section_name) ? config.addSection(account_section_name) : ''
config.set(account_section_name, 'aws_access_key_id', accessKeyId)
config.set(account_section_name, 'aws_secret_access_key', secretAccessKey)
config.set(account_section_name, 'aws_session_token', sessionToken)
console.log(config.items(account_section_name))
}
}
// saves changes into credentials file
config.write(awsCredentialsPath)
console.log('credentials updated')
}
updateCredentials()