node-microsvc-lib
Advanced tools
Comparing version
@@ -8,4 +8,6 @@ import { IConfigsProvider } from "../interfaces"; | ||
private _aws_creds; | ||
private _aws_role_name; | ||
private _aws_runtime_credentials; | ||
readonly solution_name: string; | ||
constructor(solution_name: string, secret_name: string, region: string, aws_creds: IAWSCredentials); | ||
constructor(solution_name: string, secret_name: string, region: string, aws_creds: IAWSCredentials, aws_role_name?: string | null); | ||
init(keys: string[], callback: (err?: Error) => void): void; | ||
@@ -12,0 +14,0 @@ get_value(key_name: string): string | null; |
@@ -13,6 +13,4 @@ "use strict"; | ||
class AWSSecretsManagerProvider { | ||
get solution_name() { | ||
return this._solution_name; | ||
} | ||
constructor(solution_name, secret_name, region, aws_creds) { | ||
constructor(solution_name, secret_name, region, aws_creds, aws_role_name = null) { | ||
this._aws_role_name = null; | ||
this._kvs = new Map(); | ||
@@ -23,2 +21,3 @@ this._solution_name = solution_name; | ||
this._aws_creds = aws_creds; | ||
this._aws_role_name = aws_role_name; | ||
assert.ok(this._region); | ||
@@ -30,4 +29,29 @@ assert.ok(this._secret_name); | ||
} | ||
get solution_name() { | ||
return this._solution_name; | ||
} | ||
init(keys, callback) { | ||
this._fetch_all_from_aws_secrets_manager(keys, callback); | ||
if (this._aws_role_name) { | ||
// assume role | ||
const sts = new AWS.STS({}); | ||
const role_params = { | ||
RoleArn: this._aws_role_name, | ||
RoleSessionName: `${this._solution_name}_assume_role_session` | ||
}; | ||
sts.assumeRole(role_params, (err, data) => { | ||
if (err) | ||
return callback(err); | ||
if (!data) | ||
return new Error(`AWSSecretsManagerProvider - Could not assume role and get credentials from it`); | ||
// successful response | ||
// @ts-ignore | ||
this._aws_runtime_credentials = new AWS.Credentials(data.Credentials.AccessKeyId, data.Credentials.SecretAccessKey, data.Credentials.SessionToken); | ||
this._fetch_all_from_aws_secrets_manager(keys, callback); | ||
}); | ||
} | ||
else { | ||
// fetch all | ||
this._aws_runtime_credentials = new AWS.Credentials(this._aws_creds.accessKeyId, this._aws_creds.secretAccessKey, this._aws_creds.sessionToken); | ||
this._fetch_all_from_aws_secrets_manager(keys, callback); | ||
} | ||
} | ||
@@ -38,4 +62,3 @@ get_value(key_name) { | ||
_fetch_all_from_aws_secrets_manager(keys, callback) { | ||
const creds = new AWS.Credentials(this._aws_creds.accessKeyId, this._aws_creds.secretAccessKey, this._aws_creds.sessionToken); | ||
const secrets_manager = new AWS.SecretsManager({ region: this._region, credentials: creds }); | ||
const secrets_manager = new AWS.SecretsManager({ region: this._region, credentials: this._aws_runtime_credentials }); | ||
secrets_manager.getSecretValue({ SecretId: this._secret_name }, (err, secret_data) => { | ||
@@ -42,0 +65,0 @@ if (err) |
{ | ||
"name": "node-microsvc-lib", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "NodeJS microservice framework library", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is not supported yet
101407
1.96%1458
1.74%