Skip to content

Commit

Permalink
Merge pull request #2 from mmz-srf/fix/PLATFORm-3053-fix-async-issues
Browse files Browse the repository at this point in the history
PLATFORM-3053 Fix function async issues
  • Loading branch information
michizubi-SRF authored Sep 25, 2023
2 parents 643d87e + db643ef commit 1ab9c05
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
8 changes: 8 additions & 0 deletions lib/ag-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const assert = require('assert');
const actions = require('./actions');
const utils = require('./utils');
const pkg = require('../package.json');
const CONFIG = require('./config').CONFIG;

/**
* Handles HTTP result callback
Expand All @@ -15,6 +16,13 @@ exports.handler = async (event, context, callback) => {
try {
assert.ok(event.body !== undefined, "event had no body");

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

const result = JSON.parse(event.body);

// callback sanity checks
Expand Down
26 changes: 0 additions & 26 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,6 @@ if (process.env.AWS_SAM_LOCAL !== undefined) {
console.log(process.env);
}

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() {
CONFIG.KEY = null;
CONFIG.SECRET = null;
Expand Down Expand Up @@ -61,13 +43,5 @@ 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;
7 changes: 7 additions & 0 deletions lib/s3-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ exports.handler = async (event, context, callback) => {
try {
console.log(`handling s3 event using ${pkg.name}/v${pkg.version}`);

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

// pre-flight checks:
assert.ok(CONFIG.CALLBACK_URL !== null, "api callback url cannot be null");
assert.ok(CONFIG.KEY !== null, "api key cannot be null");
Expand Down
19 changes: 19 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,26 @@ exports.formatTagValue = (value) => {
return formattedValue;
};

const getScaniiAPISecrets = function() {
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;
})
};

exports.internalId = internalId;
exports.generateSignature = generateSignature;
exports.getScaniiAPISecrets = getScaniiAPISecrets;


0 comments on commit 1ab9c05

Please sign in to comment.