-
Notifications
You must be signed in to change notification settings - Fork 1
Fix/df 469 forbidden error logs #974
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
408366f
Remove old style duplicate error logging
davidjamesstone 3ad00c6
Remove duplicate crumb from context
davidjamesstone 2ad3b3f
Re-send hapi logs to pino so we don't miss internal or plugin logs
davidjamesstone 7e0da02
Add forward logs plugin
davidjamesstone 0b2d5b7
Fix tests that rely on enforceCsrf being false
davidjamesstone 4626071
Default enforceCsrf: true for all environments
davidjamesstone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { type RequestEvent } from '@hapi/hapi' | ||
| import type pino from 'pino' | ||
|
|
||
| import { forwardLogs } from '~/src/server/common/helpers/logging/forward-logs.js' | ||
|
|
||
| describe('forwardLogs', () => { | ||
| let logger: pino.Logger | ||
|
|
||
| beforeEach(() => { | ||
| const info = jest.fn() | ||
| const error = jest.fn() | ||
|
|
||
| logger = { error, info } as unknown as pino.Logger | ||
| }) | ||
|
|
||
| it('logs info with string data', () => { | ||
| forwardLogs( | ||
| logger, | ||
| { | ||
| channel: 'app', | ||
| timestamp: Date.now().toString(), | ||
| tags: ['a', 'b', 'c'], | ||
| data: 'My log msg' | ||
| } as RequestEvent, | ||
| { a: true, b: true, c: true } | ||
| ) | ||
|
|
||
| expect(logger.info).toHaveBeenCalledExactlyOnceWith( | ||
| 'Channel: app, Tags: [a,b,c], Data: My log msg' | ||
| ) | ||
| }) | ||
|
|
||
| it('logs info with undefined data', () => { | ||
| forwardLogs( | ||
| logger, | ||
| { | ||
| channel: 'app', | ||
| timestamp: Date.now().toString(), | ||
| tags: ['a', 'b', 'c'] | ||
| } as RequestEvent, | ||
| { a: true, b: true, c: true } | ||
| ) | ||
|
|
||
| expect(logger.info).toHaveBeenCalledExactlyOnceWith( | ||
| 'Channel: app, Tags: [a,b,c], Data: type - undefined' | ||
| ) | ||
| }) | ||
|
|
||
| it('logs info with object data', () => { | ||
| forwardLogs( | ||
| logger, | ||
| { | ||
| channel: 'app', | ||
| timestamp: Date.now().toString(), | ||
| tags: ['a', 'b', 'c'], | ||
| data: { some: 'data' } | ||
| } as RequestEvent, | ||
| { a: true, b: true, c: true } | ||
| ) | ||
|
|
||
| expect(logger.info).toHaveBeenCalledExactlyOnceWith( | ||
| 'Channel: app, Tags: [a,b,c], Data: type - object' | ||
| ) | ||
| }) | ||
|
|
||
| it('logs info with function data', () => { | ||
| forwardLogs( | ||
| logger, | ||
| { | ||
| channel: 'app', | ||
| timestamp: Date.now().toString(), | ||
| tags: ['a', 'b', 'c'], | ||
| data: () => { | ||
| '' | ||
| } | ||
| } as RequestEvent, | ||
| { a: true, b: true, c: true } | ||
| ) | ||
|
|
||
| expect(logger.info).toHaveBeenCalledExactlyOnceWith( | ||
| 'Channel: app, Tags: [a,b,c], Data: type - function' | ||
| ) | ||
| }) | ||
|
|
||
| it('logs error with string data', () => { | ||
| const error = new Error('Some error') | ||
|
|
||
| forwardLogs( | ||
| logger, | ||
| { | ||
| channel: 'internal', | ||
| timestamp: Date.now().toString(), | ||
| tags: ['a', 'b', 'c', 'error'], | ||
| error | ||
| } as RequestEvent, | ||
| { a: true, b: true, c: true, error: true } | ||
| ) | ||
|
|
||
| expect(logger.error).toHaveBeenCalledWith( | ||
| error, | ||
| 'Channel: internal, Tags: [a,b,c,error], Error: Some error' | ||
| ) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { getErrorMessage } from '@defra/forms-model' | ||
| import { | ||
| type LogEvent, | ||
| type RequestEvent, | ||
| type ServerRegisterPluginObject | ||
| } from '@hapi/hapi' | ||
| import type pino from 'pino' | ||
|
|
||
| export const forwardLogs = ( | ||
| logger: pino.Logger, | ||
| event: LogEvent | RequestEvent, | ||
| tags: Record<string, true> | ||
| ) => { | ||
| const tagstr = event.tags.join(',') | ||
| const message = `Channel: ${event.channel}, Tags: [${tagstr}]` | ||
|
|
||
| if ('error' in tags) { | ||
| logger.error( | ||
| event.error, | ||
| `${message}, Error: ${getErrorMessage(event.error)}` | ||
| ) | ||
| } else { | ||
| const data = | ||
| typeof event.data === 'string' | ||
| ? event.data | ||
| : `type - ${typeof event.data}` | ||
|
|
||
| logger.info(`${message}, Data: ${data}`) | ||
| } | ||
| } | ||
|
|
||
| export default { | ||
| plugin: { | ||
| name: 'forward-logs', | ||
| register: (server) => { | ||
| server.events.on('log', (event, tags) => | ||
| forwardLogs(server.logger, event, tags) | ||
| ) | ||
|
|
||
| server.events.on('request', (request, event, tags) => | ||
| forwardLogs(request.logger, event, tags) | ||
| ) | ||
| } | ||
| } | ||
| } satisfies ServerRegisterPluginObject<void> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice, I like the plugin approach