-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
55 lines (49 loc) · 1.77 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
const configsClass = require('./libs/configs');
const awsSignedConfigs = require('./libs/awsSignedConfigs');
const request = require('got');
const { defaultProvider } = require('@aws-sdk/credential-provider-node');
class VaultAwsAuth {
constructor (args) {
let configs = new configsClass(args);
let validConfigs = configs.validateConfigs();
if(!validConfigs.valid) throw validConfigs.details;
this.configs = configs.getConfigs();
}
getOptions (creds) {
let awsLoginConfigs = new awsSignedConfigs({host:this.configs.host,vaultAppName:this.configs.vaultAppName});
let options = {
url: this.configs.uri,
followAllRedirects: this.configs.followAllRedirects,
body: JSON.stringify(awsLoginConfigs.getSignedConfigs(creds))
};
if(this.configs.sslCertificate) {
let https = options['https'] || {};
https.certificate = this.configs.sslCertificate;
options['https'] = https;
}
if(!this.configs.sslRejectUnAuthorized) {
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
}
return options;
}
async authenticate () {
const providerChain = defaultProvider();
const creds = await providerChain();
const options = this.getOptions(creds);
try {
const response = await request.post(options);
const result = JSON.parse(response.body);
if(result.errors) throw result;
else return result;
}
catch (error) {
if (error.response) {
const ex = new Error(error.message);
ex.body = JSON.parse(error.response.body);
throw ex;
}
else throw error;
}
}
}
module.exports = VaultAwsAuth;