Skip to content

Commit

Permalink
PLATFORM-3053 Get secrets from SM
Browse files Browse the repository at this point in the history
  • Loading branch information
michizubi-SRF committed Sep 22, 2023
1 parent e0581ce commit 92f0915
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ if (process.env.AWS_SAM_LOCAL !== undefined) {
console.log(process.env);
}

const headers = {"X-Aws-Parameters-Secrets-Token": process.env.AWS_SESSION_TOKEN}
const scaniiAPISecrets = process.env.SCANII_API_SECRETS_NAME;

async function getScaniiAPISecrets () {
let response = await fetch(`http://localhost:2773/secretsmanager/get?secretId=${scaniiAPISecrets}`, {
method: 'GET',
headers: headers
});
let data = await response.json();

return data.SecretString;
function getScaniiAPISecrets() {
const headers = {"X-Aws-Parameters-Secrets-Token": process.env.AWS_SESSION_TOKEN}
const scaniiAPISecrets = process.env.SCANII_API_SECRETS_NAME;
return fetch(`http://localhost:2773/secretsmanager/get?secretId=${scaniiAPISecrets}`, {
method: 'GET',
headers: headers,
})
.then(function (response) {
if (!response.ok) {
throw new Error('API request failed with status: ' + response.status);
}
return response.json();
})
.then(function (data) {
return data.SecretString;
})
}

function defaults() {
Expand All @@ -28,18 +33,7 @@ function defaults() {
CONFIG.MAX_ATTEMPTS = 10;
CONFIG.MAX_ATTEMPT_DELAY_MSEC = 30_000;

const SCANII_API_SECRETS = getScaniiAPISecrets();
CONFIG.KEY = SCANII_API_SECRETS['API_KEY'];
CONFIG.SECRET = SCANII_API_SECRETS['API_SECRET'];

// extracting config overwrites from the environment:
// if (process.env.API_KEY) {
// CONFIG.KEY = process.env.API_KEY;
// }
// if (process.env.API_SECRET) {
// CONFIG.SECRET = process.env.API_SECRET;
// }

if (process.env.API_ENDPOINT) {
CONFIG.API_ENDPOINT = process.env.API_ENDPOINT;
}
Expand Down Expand Up @@ -67,5 +61,13 @@ function defaults() {
}

defaults();

var secrets = getScaniiAPISecrets()
.then(function (secretString) {
secrets = JSON.parse(secretString)
CONFIG.KEY = secrets['API_KEY'];
CONFIG.SECRET = secrets['API_SECRET'];
});

exports.defaults = defaults;
exports.CONFIG = CONFIG;

0 comments on commit 92f0915

Please sign in to comment.