-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAwsCredentials.js
61 lines (54 loc) · 1.57 KB
/
AwsCredentials.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
require('rubico/global')
const fs = require('fs')
const nodePath = require('path')
const pathResolve = require('./internal/pathResolve')
/**
* @name AwsCredentials
*
* @synopsis
* ```coffeescript [specscript]
* AwsCredentials(options {
* profile?: string,
* credentialsFileDir?: string,
* }) -> awsCreds {
* accessKeyId: string,
* secretAccessKey: string,
* }
*
* AwsCredentials(profile string) -> awsCreds {
* accessKeyId: string,
* secretAccessKey: string,
* }
* ```
*/
const AwsCredentials = async function (options = {}) {
const profile = (
typeof options == 'string' ? options : options.profile
) ?? 'default'
let credentialsFileDir =
options.credentialsFileDir ?? pathResolve(process.cwd())
let lines = ''
while (credentialsFileDir != '/') {
const credentialsFilePath =
pathResolve(credentialsFileDir, '.aws/credentials')
if (fs.existsSync(credentialsFilePath)) {
lines = await (
fs.promises.readFile(credentialsFilePath)
.then(buffer => `${buffer}`.split('\n'))
)
break
}
credentialsFileDir = pathResolve(credentialsFileDir, '..')
}
const startingLineNumber = lines.findIndex(line => line == `[${profile}]`)
const accessKeyId = lines.find(
(line, index) => index > startingLineNumber
&& line.startsWith('aws_access_key_id')
).split(' = ')[1]
const secretAccessKey = lines.find(
(line, index) => index > startingLineNumber
&& line.startsWith('aws_secret_access_key')
).split(' = ')[1]
return { accessKeyId, secretAccessKey }
}
module.exports = AwsCredentials