@asymmetrik/fhir-secrets
Advanced tools
Comparing version 0.9.3 to 0.9.5
'use strict'; | ||
let KMS_INSTANCE = { | ||
__params: {}, | ||
__error: undefined, | ||
__results: undefined, | ||
__params: {}, | ||
__error: undefined, | ||
__results: undefined, | ||
decrypt: (params, callback) => { | ||
KMS_INSTANCE.__params = params; | ||
callback(KMS_INSTANCE.__error, KMS_INSTANCE.__results); | ||
}, | ||
decrypt: (params, callback) => { | ||
KMS_INSTANCE.__params = params; | ||
callback(KMS_INSTANCE.__error, KMS_INSTANCE.__results); | ||
}, | ||
__setError: function(err) { | ||
KMS_INSTANCE.__error = err; | ||
}, | ||
__setError: function (err) { | ||
KMS_INSTANCE.__error = err; | ||
}, | ||
__setResults: function(data) { | ||
KMS_INSTANCE.__results = data; | ||
}, | ||
__setResults: function (data) { | ||
KMS_INSTANCE.__results = data; | ||
}, | ||
__reset: function() { | ||
KMS_INSTANCE.__params = {}; | ||
KMS_INSTANCE.__error = undefined; | ||
KMS_INSTANCE.__results = undefined; | ||
}, | ||
__reset: function () { | ||
KMS_INSTANCE.__params = {}; | ||
KMS_INSTANCE.__error = undefined; | ||
KMS_INSTANCE.__results = undefined; | ||
}, | ||
}; | ||
let sdk = { | ||
__config: {}, | ||
KMS: function(options) { | ||
sdk.__config = options; | ||
return KMS_INSTANCE; | ||
}, | ||
__config: {}, | ||
KMS: function (options) { | ||
sdk.__config = options; | ||
return KMS_INSTANCE; | ||
}, | ||
}; | ||
module.exports = sdk; |
70
index.js
@@ -7,42 +7,42 @@ const AWS = require('aws-sdk'); | ||
let secrets = { | ||
/** | ||
* @function configure | ||
* @description Update AWS config for KMS service | ||
* @param {Object} options | ||
* @return {Object} this | ||
*/ | ||
configure: function configure(options) { | ||
kms = new AWS.KMS(options); | ||
return this; | ||
}, | ||
/** | ||
* @function configure | ||
* @description Update AWS config for KMS service | ||
* @param {Object} options | ||
* @return {Object} this | ||
*/ | ||
configure: function configure(options) { | ||
kms = new AWS.KMS(options); | ||
return this; | ||
}, | ||
/** | ||
* @function decrypt | ||
* @description Takes a CiphertextBlob, returns a promise that resolves | ||
* to the plain text version of the secret | ||
* @param {Object} options - Options for kms.decrypt. | ||
* See https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/KMS.html#decrypt-property | ||
* @return {Promise.<String, Error>} | ||
*/ | ||
decrypt: function decrypt(options = {}) { | ||
return new Promise((resolve, reject) => { | ||
if (!options.CiphertextBlob) { | ||
reject(new Error('Missing required argument property: CiphertextBlob')); | ||
} | ||
/** | ||
* @function decrypt | ||
* @description Takes a CiphertextBlob, returns a promise that resolves | ||
* to the plain text version of the secret | ||
* @param {Object} options - Options for kms.decrypt. | ||
* See https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/KMS.html#decrypt-property | ||
* @return {Promise.<String, Error>} | ||
*/ | ||
decrypt: function decrypt(options = {}) { | ||
return new Promise((resolve, reject) => { | ||
if (!options.CiphertextBlob) { | ||
reject(new Error('Missing required argument property: CiphertextBlob')); | ||
} | ||
let params = Object.assign({}, options, { | ||
CiphertextBlob: Buffer.from(options.CiphertextBlob, 'base64'), | ||
}); | ||
let params = Object.assign({}, options, { | ||
CiphertextBlob: Buffer.from(options.CiphertextBlob, 'base64'), | ||
}); | ||
kms.decrypt(params, (err, data) => { | ||
if (err) { | ||
return reject(err); | ||
} else { | ||
return resolve(data.Plaintext.toString('utf-8')); | ||
} | ||
}); | ||
}); | ||
}, | ||
kms.decrypt(params, (err, data) => { | ||
if (err) { | ||
return reject(err); | ||
} else { | ||
return resolve(data.Plaintext.toString('utf-8')); | ||
} | ||
}); | ||
}); | ||
}, | ||
}; | ||
module.exports = secrets; |
@@ -7,56 +7,52 @@ const secrets = require('./index'); | ||
describe('FHIR Secrets Test', () => { | ||
describe('Method: configure', () => { | ||
test('should pass options to AWS.KMS', () => { | ||
let options = { region: 'us-east-2' }; | ||
expect(AWS.__config).not.toBe(options); | ||
describe('Method: configure', () => { | ||
test('should pass options to AWS.KMS', () => { | ||
let options = { region: 'us-east-2' }; | ||
expect(AWS.__config).not.toBe(options); | ||
secrets.configure(options); | ||
expect(AWS.__config).toBe(options); | ||
}); | ||
secrets.configure(options); | ||
expect(AWS.__config).toBe(options); | ||
}); | ||
test('should return self for chaining subsequent operations', () => { | ||
let options = { region: 'us-east-2' }; | ||
let self = secrets.configure(options); | ||
test('should return self for chaining subsequent operations', () => { | ||
let options = { region: 'us-east-2' }; | ||
let self = secrets.configure(options); | ||
expect(self.configure).toBeDefined(); | ||
expect(self.decrypt).toBeDefined(); | ||
}); | ||
}); | ||
expect(self.configure).toBeDefined(); | ||
expect(self.decrypt).toBeDefined(); | ||
}); | ||
}); | ||
describe('Method: decrypt', () => { | ||
beforeEach(() => { | ||
kms_instance.__reset(); | ||
}); | ||
describe('Method: decrypt', () => { | ||
beforeEach(() => { | ||
kms_instance.__reset(); | ||
}); | ||
test('should resolve plain text secrets when successfully called', () => { | ||
// Setup mock | ||
kms_instance.__setResults({ Plaintext: 'foobar' }); | ||
test('should resolve plain text secrets when successfully called', () => { | ||
// Setup mock | ||
kms_instance.__setResults({ Plaintext: 'foobar' }); | ||
expect.assertions(1); | ||
return secrets | ||
.decrypt({ CiphertextBlob: 'SomeFakeBlob=' }) | ||
.then(secret => { | ||
expect(secret).toBe('foobar'); | ||
}); | ||
}); | ||
expect.assertions(1); | ||
return secrets.decrypt({ CiphertextBlob: 'SomeFakeBlob=' }).then((secret) => { | ||
expect(secret).toBe('foobar'); | ||
}); | ||
}); | ||
test('should pass any errors back in reject', () => { | ||
// Setup mock | ||
kms_instance.__setError('foobar'); | ||
test('should pass any errors back in reject', () => { | ||
// Setup mock | ||
kms_instance.__setError('foobar'); | ||
expect.assertions(1); | ||
return secrets.decrypt({ CiphertextBlob: 'SomeFakeBlob=' }).catch(err => { | ||
expect(err).toBe('foobar'); | ||
}); | ||
}); | ||
expect.assertions(1); | ||
return secrets.decrypt({ CiphertextBlob: 'SomeFakeBlob=' }).catch((err) => { | ||
expect(err).toBe('foobar'); | ||
}); | ||
}); | ||
test('should return expected error back for missing blob', () => { | ||
expect.assertions(1); | ||
return secrets.decrypt().catch(err => { | ||
expect(err.message).toBe( | ||
'Missing required argument property: CiphertextBlob', | ||
); | ||
}); | ||
}); | ||
}); | ||
test('should return expected error back for missing blob', () => { | ||
expect.assertions(1); | ||
return secrets.decrypt().catch((err) => { | ||
expect(err.message).toBe('Missing required argument property: CiphertextBlob'); | ||
}); | ||
}); | ||
}); | ||
}); |
{ | ||
"name": "@asymmetrik/fhir-secrets", | ||
"version": "0.9.3", | ||
"version": "0.9.5", | ||
"description": "AWS KMS Secrets retrieval promisified", | ||
"main": "index.js", | ||
"homepage": "https://github.com/Asymmetrik/phx-tools", | ||
"author": "Robert-W <rwinterbottom@asymmetrik.com>", | ||
"license": "MIT", | ||
"repository": { | ||
@@ -13,4 +10,7 @@ "type": "git", | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
"license": "MIT", | ||
"author": "Robert-W <rwinterbottom@asymmetrik.com>", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jest" | ||
}, | ||
@@ -20,3 +20,6 @@ "dependencies": { | ||
}, | ||
"gitHead": "be01510edc8b9f666375ccb7624c29b0a45d2508" | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"gitHead": "4ccd64a6c9dc3c7131fd66e69b8c2046a3e67922" | ||
} |
@@ -13,7 +13,8 @@ # `FHIR-Secrets` | ||
* You will need an AWS Account. | ||
* You should have the CiphertextBlob you need to decrypt. | ||
* You can [authenticate with AWS](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html). | ||
- You will need an AWS Account. | ||
- You should have the CiphertextBlob you need to decrypt. | ||
- You can [authenticate with AWS](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html). | ||
## Usage | ||
Depending on where you are running this code, the setup portion may change. If you have a default AWS profile with region and everything else set, you can use the decrypt function as follows: | ||
@@ -29,4 +30,5 @@ | ||
// Lets assume this blob contains metadata for a plaintext secret with value foobar | ||
secrets.decrypt({ CiphertextBlob }) | ||
.then(secret => { | ||
secrets | ||
.decrypt({ CiphertextBlob }) | ||
.then((secret) => { | ||
console.log(secret); | ||
@@ -43,5 +45,6 @@ // logs foobar | ||
secrets.configure({ region: 'us-east-2' }) | ||
secrets | ||
.configure({ region: 'us-east-2' }) | ||
.decrypt({ CiphertextBlob: 'somefakeblobcontent=' }) | ||
.then(secret => doThingsWithSecret()) | ||
.then((secret) => doThingsWithSecret()) | ||
.catch(console.error); | ||
@@ -55,2 +58,3 @@ ``` | ||
### configure | ||
> Configures the service class with any AWS or service class specific configurations. | ||
@@ -60,6 +64,6 @@ | ||
### decrypt | ||
### decrypt | ||
> Wrapper on kms.decrypt but returns a promise which resolves the plain text of the secret. | ||
This takes a single options object. See https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/KMS.html#decrypt-property. The only necessary property from this is `CiphertextBlob`. |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
7679
65
0
116