Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't throw error if context is invalid #757

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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