diff --git a/lib/providers/aws.js b/lib/providers/aws.js index 35de9ec..353e324 100644 --- a/lib/providers/aws.js +++ b/lib/providers/aws.js @@ -1,6 +1,8 @@ 'use strict' const AWS = require('aws-sdk') +const url = require('url') +const HttpsProxyAgent = require('https-proxy-agent') const defaultOptions = { apiVersion: '2014-11-06', @@ -8,9 +10,23 @@ const defaultOptions = { } module.exports = function (options) { + + // Use HTTPS Proxy (Optional) + const proxy = process.env.proxy || + process.env.HTTP_PROXY || + process.env.http_proxy || + process.env.HTTPS_PROXY || + process.env.https_proxy; + + if (proxy) { + const proxyOptions = url.parse(proxy); + proxyOptions.secureEndpoint = true; + AWS.config.httpOptions.agent = new HttpsProxyAgent(proxyOptions); + } + const ssm = new AWS.SSM(Object.assign({}, defaultOptions, options)) - function getSecret (parameterNames) { + function getSecret(parameterNames) { const names = Array.isArray(parameterNames) ? parameterNames : [parameterNames] const params = { Names: names, @@ -25,7 +41,7 @@ module.exports = function (options) { }) } - function setSecret (name, value, description = 'Created with Serverless Secrets', isEncrypted = true, keyId) { + function setSecret(name, value, description = 'Created with Serverless Secrets', isEncrypted = true, keyId) { const params = { Name: name, Value: value, @@ -39,13 +55,13 @@ module.exports = function (options) { return ssm.putParameter(params).promise() } - function deleteSecret (name) { + function deleteSecret(name) { return ssm.deleteParameter({ Name: name }).promise() } - function listSecrets () { + function listSecrets() { return new Promise((resolve, reject) => { const secretKeys = [] ssm.describeParameters({}).eachPage((err, data, done) => { @@ -76,4 +92,4 @@ module.exports = function (options) { deleteSecret, listSecrets } -} +} \ No newline at end of file diff --git a/lib/providers/aws.tests.js b/lib/providers/aws.tests.js index a1a2818..7ff131b 100644 --- a/lib/providers/aws.tests.js +++ b/lib/providers/aws.tests.js @@ -5,8 +5,26 @@ const td = require('testdouble') let AWS, awsProvider +test.cb('constructor: Http or Https proxy is set', t => { + let proxyArray = ['HTTP_PROXY', 'http_proxy', 'HTTPS_PROXY', 'https_proxy'] + proxyArray.forEach(param => { + delete process.env[param] + }) + + AWS = require('aws-sdk'); + proxyArray.forEach((proxyParam) => { + process.env[proxyParam] = "http://localhost:8080" + awsProvider = require('./aws')() + t.is(AWS.config.httpOptions.agent.options.host, 'localhost:8080') + t.is(AWS.config.httpOptions.agent.options.secureEndpoint, true) + AWS.config.httpOptions.agent = null; + delete process.env[proxyParam] + }) + t.end() +}) + test.beforeEach('create provider', t => { - function SSM () {} + function SSM() {} SSM.prototype.getParameters = () => {} SSM.prototype.putParameter = () => {} @@ -21,15 +39,15 @@ test.afterEach.always('cleanup', t => { test.cb('getSecret: happy path', t => { td.when(AWS.SSM.prototype.getParameters(td.matchers.anything())) - .thenReturn({ promise: () => Promise.resolve({ - Parameters: [ - { + .thenReturn({ + promise: () => Promise.resolve({ + Parameters: [{ Name: 'test_parameter', Type: 'String', Value: 'test_secret' - } - ] - }) }) + }] + }) + }) awsProvider.getSecret('test_parameter').then(data => { t.is(data.test_parameter, 'test_secret') @@ -39,20 +57,21 @@ test.cb('getSecret: happy path', t => { test.cb('getSecret: happy path array', t => { td.when(AWS.SSM.prototype.getParameters(td.matchers.anything())) - .thenReturn({ promise: () => Promise.resolve({ - Parameters: [ - { - Name: 'test_parameter', - Type: 'String', - Value: 'test_secret' - }, - { - Name: 'test_parameter2', - Type: 'String', - Value: 'test_secret2' - } - ] - }) }) + .thenReturn({ + promise: () => Promise.resolve({ + Parameters: [{ + Name: 'test_parameter', + Type: 'String', + Value: 'test_secret' + }, + { + Name: 'test_parameter2', + Type: 'String', + Value: 'test_secret2' + } + ] + }) + }) awsProvider.getSecret(['test_parameter', 'test_parameter2']).then(data => { t.is(data.test_parameter, 'test_secret') @@ -62,8 +81,14 @@ test.cb('getSecret: happy path array', t => { }) test.cb('getSecret: requests decryption', t => { - td.when(AWS.SSM.prototype.getParameters(td.matchers.contains({ WithDecryption: true }))) - .thenReturn({ promise: () => Promise.resolve({ Parameters: [] }) }) + td.when(AWS.SSM.prototype.getParameters(td.matchers.contains({ + WithDecryption: true + }))) + .thenReturn({ + promise: () => Promise.resolve({ + Parameters: [] + }) + }) awsProvider.getSecret([]).then(() => { t.pass() @@ -74,7 +99,9 @@ test.cb('getSecret: requests decryption', t => { test.cb('getSecret: error bubbles up', t => { const error = {} td.when(AWS.SSM.prototype.getParameters(td.matchers.anything())) - .thenReturn({ promise: () => Promise.reject(error) }) + .thenReturn({ + promise: () => Promise.reject(error) + }) awsProvider.getSecret().catch(() => { t.pass() @@ -90,7 +117,9 @@ test('setSecret: happy path', t => { Type: 'String', KeyId: 'myKmsKey', Overwrite: true - })).thenReturn({ promise: () => Promise.resolve() }) + })).thenReturn({ + promise: () => Promise.resolve() + }) awsProvider.setSecret('name', 'value', 'description', false, 'myKmsKey') @@ -100,10 +129,12 @@ test('setSecret: happy path', t => { test.cb('setSecret: error bubbles up', t => { const error = {} td.when(AWS.SSM.prototype.putParameter(td.matchers.anything())) - .thenReturn({ promise: () => Promise.reject(error) }) + .thenReturn({ + promise: () => Promise.reject(error) + }) awsProvider.setSecret([]).catch(() => { t.pass() t.end() }) -}) +}) \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 97ee0d5..2a2e4e4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -334,6 +334,14 @@ "private": "0.1.7", "slash": "1.0.0", "source-map": "0.5.7" + }, + "dependencies": { + "lodash": { + "version": "4.17.4", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", + "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=", + "dev": true + } } }, "babel-generator": { @@ -357,6 +365,12 @@ "resolved": "https://registry.npmjs.org/jsesc/-/jsesc-1.3.0.tgz", "integrity": "sha1-RsP+yMGJKxKwgz25vHYiF226s0s=", "dev": true + }, + "lodash": { + "version": "4.17.4", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", + "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=", + "dev": true } } }, @@ -436,6 +450,14 @@ "babel-runtime": "6.26.0", "babel-types": "6.26.0", "lodash": "4.17.4" + }, + "dependencies": { + "lodash": { + "version": "4.17.4", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", + "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=", + "dev": true + } } }, "babel-helper-remap-async-to-generator": { @@ -634,6 +656,14 @@ "lodash": "4.17.4", "mkdirp": "0.5.1", "source-map-support": "0.4.16" + }, + "dependencies": { + "lodash": { + "version": "4.17.4", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", + "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=", + "dev": true + } } }, "babel-runtime": { @@ -657,6 +687,14 @@ "babel-types": "6.26.0", "babylon": "6.18.0", "lodash": "4.17.4" + }, + "dependencies": { + "lodash": { + "version": "4.17.4", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", + "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=", + "dev": true + } } }, "babel-traverse": { @@ -674,6 +712,14 @@ "globals": "9.18.0", "invariant": "2.2.2", "lodash": "4.17.4" + }, + "dependencies": { + "lodash": { + "version": "4.17.4", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", + "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=", + "dev": true + } } }, "babel-types": { @@ -686,6 +732,14 @@ "esutils": "2.0.2", "lodash": "4.17.4", "to-fast-properties": "1.0.3" + }, + "dependencies": { + "lodash": { + "version": "4.17.4", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", + "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=", + "dev": true + } } }, "babylon": { @@ -1422,7 +1476,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.1.1", @@ -1473,7 +1528,8 @@ "balanced-match": { "version": "0.4.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "bcrypt-pbkdf": { "version": "1.0.1", @@ -1488,6 +1544,7 @@ "version": "0.0.9", "bundled": true, "dev": true, + "optional": true, "requires": { "inherits": "2.0.3" } @@ -1496,6 +1553,7 @@ "version": "2.10.1", "bundled": true, "dev": true, + "optional": true, "requires": { "hoek": "2.16.3" } @@ -1504,6 +1562,7 @@ "version": "1.1.7", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "0.4.2", "concat-map": "0.0.1" @@ -1512,7 +1571,8 @@ "buffer-shims": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "caseless": { "version": "0.12.0", @@ -1529,12 +1589,14 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "combined-stream": { "version": "1.0.5", "bundled": true, "dev": true, + "optional": true, "requires": { "delayed-stream": "1.0.0" } @@ -1542,17 +1604,20 @@ "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "cryptiles": { "version": "2.0.5", @@ -1598,7 +1663,8 @@ "delayed-stream": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "delegates": { "version": "1.0.0", @@ -1624,7 +1690,8 @@ "extsprintf": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "forever-agent": { "version": "0.6.1", @@ -1646,12 +1713,14 @@ "fs.realpath": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "fstream": { "version": "1.0.11", "bundled": true, "dev": true, + "optional": true, "requires": { "graceful-fs": "4.1.11", "inherits": "2.0.3", @@ -1707,6 +1776,7 @@ "version": "7.1.2", "bundled": true, "dev": true, + "optional": true, "requires": { "fs.realpath": "1.0.0", "inflight": "1.0.6", @@ -1719,7 +1789,8 @@ "graceful-fs": { "version": "4.1.11", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "har-schema": { "version": "1.0.5", @@ -1758,7 +1829,8 @@ "hoek": { "version": "2.16.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "http-signature": { "version": "1.1.1", @@ -1775,6 +1847,7 @@ "version": "1.0.6", "bundled": true, "dev": true, + "optional": true, "requires": { "once": "1.4.0", "wrappy": "1.0.2" @@ -1783,7 +1856,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.4", @@ -1795,6 +1869,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "1.0.1" } @@ -1808,7 +1883,8 @@ "isarray": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "isstream": { "version": "0.1.2", @@ -1881,12 +1957,14 @@ "mime-db": { "version": "1.27.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "mime-types": { "version": "2.1.15", "bundled": true, "dev": true, + "optional": true, "requires": { "mime-db": "1.27.0" } @@ -1895,6 +1973,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "1.1.7" } @@ -1902,12 +1981,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "mkdirp": { "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -1960,7 +2041,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "oauth-sign": { "version": "0.8.2", @@ -1978,6 +2060,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1.0.2" } @@ -2007,7 +2090,8 @@ "path-is-absolute": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "performance-now": { "version": "0.2.0", @@ -2018,7 +2102,8 @@ "process-nextick-args": { "version": "1.0.7", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "punycode": { "version": "1.4.1", @@ -2056,6 +2141,7 @@ "version": "2.2.9", "bundled": true, "dev": true, + "optional": true, "requires": { "buffer-shims": "1.0.0", "core-util-is": "1.0.2", @@ -2100,6 +2186,7 @@ "version": "2.6.1", "bundled": true, "dev": true, + "optional": true, "requires": { "glob": "7.1.2" } @@ -2107,7 +2194,8 @@ "safe-buffer": { "version": "5.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "semver": { "version": "5.3.0", @@ -2161,24 +2249,26 @@ } } }, - "string_decoder": { - "version": "1.0.1", - "bundled": true, - "dev": true, - "requires": { - "safe-buffer": "5.0.1" - } - }, "string-width": { "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "1.1.0", "is-fullwidth-code-point": "1.0.0", "strip-ansi": "3.0.1" } }, + "string_decoder": { + "version": "1.0.1", + "bundled": true, + "dev": true, + "optional": true, + "requires": { + "safe-buffer": "5.0.1" + } + }, "stringstream": { "version": "0.0.5", "bundled": true, @@ -2189,6 +2279,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "2.1.1" } @@ -2203,6 +2294,7 @@ "version": "2.2.1", "bundled": true, "dev": true, + "optional": true, "requires": { "block-stream": "0.0.9", "fstream": "1.0.11", @@ -2258,7 +2350,8 @@ "util-deprecate": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "uuid": { "version": "3.0.1", @@ -2287,7 +2380,8 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, @@ -2933,9 +3027,9 @@ } }, "lodash": { - "version": "4.17.4", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", - "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=" + "version": "4.17.12", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.12.tgz", + "integrity": "sha512-+CiwtLnsJhX03p20mwXuvhoebatoh5B3tt+VvYlrPgZC1g36y+RRbkufX95Xa+X4I59aWEacDFYwnJZiyBh9gA==" }, "lodash.clonedeep": { "version": "4.5.0", @@ -3640,6 +3734,14 @@ "dev": true, "requires": { "lodash": "4.17.4" + }, + "dependencies": { + "lodash": { + "version": "4.17.4", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", + "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=", + "dev": true + } } }, "randomatic": { @@ -4054,15 +4156,6 @@ "integrity": "sha1-1PM6tU6OOHeLDKXP07OvsS22hiA=", "dev": true }, - "string_decoder": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz", - "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==", - "dev": true, - "requires": { - "safe-buffer": "5.1.1" - } - }, "string-width": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", @@ -4090,6 +4183,15 @@ } } }, + "string_decoder": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz", + "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==", + "dev": true, + "requires": { + "safe-buffer": "5.1.1" + } + }, "stringify-object-es5": { "version": "2.5.0", "resolved": "https://registry.npmjs.org/stringify-object-es5/-/stringify-object-es5-2.5.0.tgz", @@ -4187,6 +4289,14 @@ "quibble": "0.5.1", "resolve": "1.4.0", "stringify-object-es5": "2.5.0" + }, + "dependencies": { + "lodash": { + "version": "4.17.4", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", + "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=", + "dev": true + } } }, "text-table": { @@ -4487,6 +4597,13 @@ "integrity": "sha1-qlijBBoGb5DqoWwvU4n/GfP0YaU=", "requires": { "lodash": "4.17.4" + }, + "dependencies": { + "lodash": { + "version": "4.17.4", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", + "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=" + } } }, "xtend": { diff --git a/package.json b/package.json index 5b967bc..4dbe4b8 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,9 @@ "homepage": "https://github.com/trek10inc/serverless-secrets", "dependencies": { "aws-sdk": "^2.102.0", - "lodash": "^4.17.4" + "https-proxy-agent": "^2.1.0", + "lodash": "^4.17.12", + "url": "^0.11.0" }, "devDependencies": { "ava": "^0.19.1",