Skip to content

Commit

Permalink
Merge pull request #98 from brettneese/master
Browse files Browse the repository at this point in the history
refactor, simplify
  • Loading branch information
brettneese committed Jun 22, 2021
2 parents baaaeef + 32deb4b commit 554b38f
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 205 deletions.
17 changes: 9 additions & 8 deletions example/config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
"use strict";

const convict = require("@hbkapps/convict");
convict.configureProvider(require("../index"));
const convict = require("convict");
const getParams = require("../index");

module.exports = convict({
const config = {
ip: {
default: "127.0.0.1",
format: "ipaddress",
providerPath: "/test/foo/ip" // can use multiple basePaths, and we'll only need to contact AWS once per path
},
port: {
default: 0,
format: "port",
providerPath: "/test/PORT"
}
}).validate().getProperties();
},
};

const ssmParams = getParams("/example-app/");

module.exports = convict(config).load(ssmParams).validate().getProperties();
4 changes: 1 addition & 3 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"use strict";

const path = require("path");
const http = require("http");
const config = require("./config");

console.log(config);
console.log(config);
42 changes: 8 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use strict";
const _each = require("lodash.foreach");
const awsParamStore = require("aws-param-store");
const AWS = require("aws-sdk");
const AWSConfig = new AWS.Config();
Expand All @@ -13,7 +12,7 @@ function getBasePath(path) {
return a.join("/");
}

module.exports = function(path) {
module.exports = function (rootPath) {
if (AWSConfig.credentials == null) {
console.log(
"WARNING: No AWS credentials, not connecting to SSM for secrets..."
Expand All @@ -27,39 +26,14 @@ module.exports = function(path) {
"INFO: AWS_SSM is set to false, not connecting to SSM for secrets..."
);
} else {
// get the last part of the path
let key = path.split("/").pop();
let output = {};
let parameters = awsParamStore.getParametersByPathSync(rootPath);

// get the basePath (everything except the key) from the SSM path
// ie /test/staging/FOO's basePath is just /test/staging
// this way we can have multiple path patterns in our config.js.
// although idk why you'd actually want to do that.
parameters.forEach((element) => {
const name = element.Name.split(rootPath)[1];
output[name] = element.Value;
});

let basePath = getBasePath(path);

// declare a local results object
let r = {};

try {
// if we don't already have results globally from this particular path, go ahead and query AWS
if (!results[basePath]) {
// do this sync because we need variables before app bootup anyway
let parameters = awsParamStore.newQuery(basePath).executeSync();

// filter through the results and give them a nice key/value structure
_each(parameters, function(requestResult) {
let k = requestResult.Name.split("/").pop();
r[k] = requestResult.Value;
});

// add the results from the query to the global results object keyed under their basepath
results[basePath] = r;
}

// return the results for this particular basePath/key pair
return results[basePath][key];
} catch (err) {
throw err;
}
return output;
}
};
Loading

0 comments on commit 554b38f

Please sign in to comment.