Skip to content

Commit

Permalink
fix: don't throw error if context is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
tripodsan committed Feb 2, 2023
1 parent 68a747d commit 8fcfcf6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/helix-shared-secrets/src/secrets-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,21 @@ export function reset() {
* @returns {Promise<object>} the secrets or {@code null}.
*/
async function loadSecrets(ctx, opts) {
if (!ctx.runtime) {
// eslint-disable-next-line no-console
console.warn('local secrets not loaded. no ctx.runtime');
return {};
}
if (ctx.runtime.name !== 'aws-lambda') {
const error = Error(`unsupported runtime: ${ctx.runtime.name}`);
error.statusCode = 500;
throw error;
}
if (!ctx.func) {
// eslint-disable-next-line no-console
console.warn('local secrets not loaded. no ctx.func');
return {};
}

const {
expiration = CACHE_EXPIRATION,
Expand Down
25 changes: 25 additions & 0 deletions packages/helix-shared-secrets/test/secrets-wrapper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ describe('Secrets Wrapper Unit Tests', () => {
assert.strictEqual(resp.headers.get('x-error'), 'error fetching secrets.');
});

it('Responds with {} for invalid context (no runtime)', async () => {
const main = wrap((req, ctx) => {
assert.deepStrictEqual(ctx.env, { });
return new Response(200);
}).with(secrets);
const resp = await main(new Request('http://localhost'), {
env: {},
});
assert.strictEqual(resp.status, 200);
});

it('Responds with {} for invalid context (no func)', async () => {
const main = wrap((req, ctx) => {
assert.deepStrictEqual(ctx.env, { });
return new Response(200);
}).with(secrets);
const resp = await main(new Request('http://localhost'), {
env: {},
runtime: {
name: 'aws-lambda',
},
});
assert.strictEqual(resp.status, 200);
});

it('fetches secrets with default name', async () => {
nock('https://secretsmanager.us-east-1.amazonaws.com/')
.post('/')
Expand Down

0 comments on commit 8fcfcf6

Please sign in to comment.