forked from abdullahshahin/vault-auth-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (49 loc) · 1.76 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
const AWS = require('aws-sdk');
const configsClass = require('./libs/configs');
const awsSignedCongifs = require('./libs/awsSignedConfigs');
const request = require('request');
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 awsSignedCongifs({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) {
options['cert'] = this.configs.sslCertificate;
}
if(!this.configs.sslRejectUnAuthorized) {
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
}
return options;
}
authenticate () {
const providerChain = new AWS.CredentialProviderChain();
return providerChain.resolvePromise().then(creds => {
return new Promise((resolve, reject) => {
let options = this.getOptions(creds);
request.post(options, function (err, res, body) {
if(err)
reject(err);
else {
let result = JSON.parse(body);
if(result.errors)
reject(result);
else
resolve(result);
}
});
});
});
}
}
module.exports = vaultAwsAuth;