-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
504b3b0
commit 5a31c7e
Showing
13 changed files
with
223 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright The Linux Foundation and each contributor to CommunityBridge. | ||
// SPDX-License-Identifier: MIT | ||
|
||
import * as env from './cla-env-config.json'; | ||
export const EnvConfig = env as any; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright The Linux Foundation and each contributor to CommunityBridge. | ||
// SPDX-License-Identifier: MIT | ||
|
||
const fs = require('fs'); | ||
const RetrieveSSMValues = require('./read-ssm'); | ||
const configVarArray = ['auth0-clientId', 'auth0-domain']; | ||
const region = 'us-east-1'; | ||
const profile = process.env.AWS_PROFILE; | ||
const stageEnv = process.env.STAGE_ENV; | ||
const AWS_SSM_JSON_PATH = './src/app/config/cla-env-config.json'; | ||
|
||
async function prefetchSSM() { | ||
let result = {}; | ||
console.log(`Start to fetch SSM values at ${stageEnv}...`); | ||
result = await RetrieveSSMValues(configVarArray, stageEnv, region, profile); | ||
|
||
//test for local | ||
// result['cla-api-url'] = 'http://localhost:5000'; | ||
fs.writeFile(AWS_SSM_JSON_PATH, JSON.stringify(result), function (err) { | ||
if (err) throw new Error(`Couldn't save SSM paramters to disk with error ${err}`); | ||
console.log('Fetching completed...'); | ||
}); | ||
} | ||
|
||
prefetchSSM(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright The Linux Foundation and each contributor to CommunityBridge. | ||
// SPDX-License-Identifier: MIT | ||
|
||
/** | ||
* @param {string[]} variables | ||
* @returns {{ [key:string]: string }} | ||
*/ | ||
async function retrieveLocalConfigValues(variables, fileName) { | ||
const localConfig = require(`../${fileName}`); | ||
const parameterMap = {}; | ||
variables.forEach((variable) => { | ||
value = localConfig[variable]; | ||
if (value === undefined) { | ||
throw new Error(`Couldn't retrieve value from local config for ${variable}`); | ||
} | ||
parameterMap[variable] = localConfig[variable]; | ||
}); | ||
return parameterMap; | ||
} | ||
|
||
module.exports = retrieveLocalConfigValues; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// @ts-check | ||
|
||
// Copyright The Linux Foundation and each contributor to CommunityBridge. | ||
// SPDX-License-Identifier: MIT | ||
const AWS = require('aws-sdk'); | ||
|
||
/** | ||
* @param {string[]} variables | ||
* @param {string} stage | ||
* @param {string} region | ||
* @param {string} profile | ||
* @returns {Promise<{ [key:string]: string}>} | ||
*/ | ||
async function retrieveSSMValues(variables, stage, region, profile) { | ||
const scopedVariables = variables.map((param) => { | ||
return `cla-${param}-${stage}`; | ||
}); | ||
const result = await requestSSMParameters(scopedVariables, stage, region, profile); | ||
const parameters = result.Parameters; | ||
const error = result.$response.error; | ||
if (error !== null) { | ||
throw new Error( | ||
`Couldn't retrieve SSM parameters for stage ${stage} in region ${region} using profile ${profile} - error ${error}` | ||
); | ||
} | ||
const scopedParams = createParameterMap(parameters, stage); | ||
let params; | ||
Object.keys(scopedParams).forEach((key) => { | ||
const param = scopedParams[key]; | ||
key = key.replace('cla-', ''); | ||
key = key.replace(`-${stage}`, ''); | ||
params[key] = param; | ||
}); | ||
|
||
variables.forEach((variable) => { | ||
if (params[variable] === undefined) { | ||
throw new Error( | ||
`Missing SSM parameter with name ${variable} for stage ${stage} in region ${region} using profile ${profile}`, | ||
); | ||
} | ||
}); | ||
return params; | ||
} | ||
|
||
/** | ||
* @param {string[]} variables | ||
* @param {string} stage | ||
* @param {string} region | ||
*/ | ||
function requestSSMParameters(variables, stage, region, profile) { | ||
AWS.config.credentials = new AWS.SharedIniFileCredentials({ profile }); | ||
const ssm = new AWS.SSM({ region: region }); | ||
const ps = { | ||
Names: variables, | ||
WithDecryption: true | ||
}; | ||
console.log(AWS.config.credentials); | ||
return ssm.getParameters(ps).promise(); | ||
} | ||
|
||
/** | ||
* @param {AWS.SSM.Parameter[]} parameters | ||
* @param {string} stage | ||
*/ | ||
function createParameterMap(parameters, stage) { | ||
return parameters.filter((param) => param.Name.endsWith(`-${stage}`)) | ||
.map((param) => { | ||
const output = {}; | ||
output[param.Name] = param.Value; | ||
return output; | ||
}) | ||
.reduce((prev, current) => { | ||
return { ...prev, ...current }; | ||
}, {}); | ||
} | ||
|
||
module.exports = retrieveSSMValues; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
declare module '*.json' { | ||
const value: any; | ||
export default value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters