-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (75 loc) · 3.19 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const parser = require('./lib/parser');
const s3 = require('./lib/stores/s3');
const ssm = require('./lib/stores/ssm');
const secretsmanager = require('./lib/stores/secretsmanager');
const fileSystem = require('./lib/file-system');
const provide = {};
const stores = {
'aws-s3': s3.getConfigs,
'aws-parameter': ssm.getConfigs
}
/**
* Get configs via cStore given the provided yml path and env tag. Returns as key/value object.
* @param {String} tag Name of the desired env tag
* @param {String} ymlPath Absolute path to cstore.yml file
* @param {Boolean} injectSecrets If `true`, env vars referencing secrets manager
* will be fetched and returned in decrypted form.
*/
const getConfigs = async (tag, ymlPath, injectSecrets) => {
console.info(`Loading configuration for ${tag}`);
const doc = parser.parseYaml(ymlPath);
const context = doc.context;
const fileinfos = parser.locateTag(doc, tag);
let envVarsMerged = {};
for (let fileinfo of fileinfos) {
if (!stores[fileinfo.store]) {
throw new Error(`cstore-js - unsupported store type ${fileinfo.store}`);
}
const envVars = await stores[fileinfo.store](context, fileinfo);
envVarsMerged = Object.assign(envVarsMerged, envVars);
}
if (injectSecrets) {
await secretsmanager.findAllAndInject(context, envVarsMerged);
}
console.info(`Loaded configuration for ${tag}`);
return envVarsMerged;
}
/**
* Pull configs via cStore given the provided cstore.yml path and env tag. Returns as object.
* Will also inject straight into `process.env`.
* @param {String} tag Name of the desired env tag
* @param {String} ymlPath Absolute path to cstore.yml file. Defaults to process.cwd()/cstore.yml
* @param {Boolean} injectIntoProcess If `true`, env vars will be automatically
* injected into process.env
* @param {Boolean} injectSecrets If `true`, env vars referencing secrets manager
* will be fetched and returned in decrypted form.
*/
provide.pull = async (tag, ymlPath = `${process.cwd()}/cstore.yml`, injectIntoProcess = true, injectSecrets = true) => {
const envVarsMerged = await getConfigs(tag, ymlPath, injectSecrets);
if (injectIntoProcess) {
for (let envKey in envVarsMerged) {
process.env[envKey] = envVarsMerged[envKey];
}
console.info('Injected configuration into process.env');
}
return envVarsMerged;
}
/**
* Pull configs via cStore given the provided cstore.yml path and env tag. Stores the results
* in a random.env file located at `process.cwd()`. Returns the absolute filepath for the random
* env file.
* @param {String} tag Name of the desired env tag
* @param {String} ymlPath Absolute path to cstore.yml file. Defaults to process.cwd()/cstore.yml
* @param {Boolean} injectSecrets If `true`, env vars referencing secrets manager
* will be fetched and returned in decrypted form.
*/
provide.download = async (tag, ymlPath = `${process.cwd()}/cstore.yml`, injectSecrets = true) => {
const envVarsMerged = await getConfigs(tag, ymlPath, injectSecrets);
let dataToWrite = '';
for (let envKey in envVarsMerged) {
dataToWrite += `${envKey}=${envVarsMerged[envKey]}\n`;
}
const filepath = await fileSystem.saveToRandomLocation(dataToWrite);
return filepath;
}
module.exports = provide;