Skip to content

Commit

Permalink
Redact Authorization from logs (#59)
Browse files Browse the repository at this point in the history
Headers are often represented with this capitalisation.

At some point we may have to bite the bullet and ratify aggressive
redaction in a package that wraps Pino (e.g. `logger-js`).
  • Loading branch information
72636c authored Jun 16, 2020
1 parent 5753b38 commit 93cdf6c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-ghosts-poke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'skuba': patch
---

**template:** Redact `Authorization` headers in logs
3 changes: 3 additions & 0 deletions template/koa-rest-api/src/framework/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ export const rootLogger = pino({
redact: {
censor: '🤿 REDACTED 🚩',
paths: [
'err.config.headers.Authorization',
'err.config.headers.authorization',
'err.request.headers.authorization',
'err.request.config.headers.Authorization',
'err.request.config.headers.authorization',
'err.response.config.headers.Authorization',
'err.response.config.headers.authorization',
'err.response.headers.authorization',
'err.response.request.headers.authorization',
Expand Down
34 changes: 21 additions & 13 deletions template/lambda-sqs-worker/src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,45 +63,53 @@ describe('handler', () => {
it('throws on invalid input', () => {
const event = createSqsEvent(['}']);

return expect(app.handler(event, ctx)).rejects.toThrow('');
return expect(app.handler(event, ctx)).rejects.toThrow('invoke error');
});

it('bubbles up scoring service error', () => {
it('bubbles up scoring service error', async () => {
const err = Error(chance.sentence());

scoringService.request.mockRejectedValue(err);

const event = createSqsEvent([JSON.stringify(jobPublished)]);

return expect(app.handler(event, ctx)).rejects.toThrow(err);
await expect(app.handler(event, ctx)).rejects.toThrow('invoke error');

expect(contextLogger.error).toBeCalledWith({ err }, 'request');
});

it('bubbles up SNS error', () => {
it('bubbles up SNS error', async () => {
const err = Error(chance.sentence());

sns.publish.mockPromise(Promise.reject(err));

const event = createSqsEvent([JSON.stringify(jobPublished)]);

return expect(app.handler(event, ctx)).rejects.toThrow(err);
await expect(app.handler(event, ctx)).rejects.toThrow('invoke error');

expect(contextLogger.error).toBeCalledWith({ err }, 'request');
});

it('throws on zero records', () => {
it('throws on zero records', async () => {
const err = new Error('received 0 records');

const event = createSqsEvent([]);

return expect(app.handler(event, ctx)).rejects.toThrow(
'received 0 records',
);
await expect(app.handler(event, ctx)).rejects.toThrow('invoke error');

expect(contextLogger.error).toBeCalledWith({ err }, 'request');
});

it('throws on multiple records', () => {
it('throws on multiple records', async () => {
const err = new Error('received 2 records');

const event = createSqsEvent([
JSON.stringify(jobPublished),
JSON.stringify(jobPublished),
]);

return expect(app.handler(event, ctx)).rejects.toThrow(
'received 2 records',
);
await expect(app.handler(event, ctx)).rejects.toThrow('invoke error');

expect(contextLogger.error).toBeCalledWith({ err }, 'request');
});
});
4 changes: 2 additions & 2 deletions template/lambda-sqs-worker/src/framework/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('createHandler', () => {

const handler = createHandler(() => Promise.reject(err));

await expect(handler(input, ctx)).rejects.toThrow(err);
await expect(handler(input, ctx)).rejects.toThrow('invoke error');

expect(contextLogger.error.mock.calls).toEqual([[{ err }, 'request']]);

Expand All @@ -52,7 +52,7 @@ describe('createHandler', () => {
throw err;
});

await expect(handler(input, ctx)).rejects.toThrow(err);
await expect(handler(input, ctx)).rejects.toThrow('invoke error');

expect(contextLogger.error.mock.calls).toEqual([[{ err }, 'request']]);

Expand Down
2 changes: 1 addition & 1 deletion template/lambda-sqs-worker/src/framework/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export const createHandler = <Event, Output = unknown>(

logger.error({ err }, 'request');

throw err;
throw new Error('invoke error');
}
};
3 changes: 3 additions & 0 deletions template/lambda-sqs-worker/src/framework/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ export const rootLogger = pino({
redact: {
censor: '🤿 REDACTED 🚩',
paths: [
'err.config.headers.Authorization',
'err.config.headers.authorization',
'err.request.headers.authorization',
'err.request.config.headers.Authorization',
'err.request.config.headers.authorization',
'err.response.config.headers.Authorization',
'err.response.config.headers.authorization',
'err.response.headers.authorization',
'err.response.request.headers.authorization',
Expand Down

0 comments on commit 93cdf6c

Please sign in to comment.