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

feat(auth): add logs to routes #2697

Merged
merged 8 commits into from May 21, 2024
Merged

feat(auth): add logs to routes #2697

merged 8 commits into from May 21, 2024

Conversation

njlie
Copy link
Contributor

@njlie njlie commented May 2, 2024

Changes proposed in this pull request

  • Adds logging to route handlers on the auth server
    • Includes request method, path, and a unique request id in each log
    • Includes a breakdown of the request body, params, and headers (that are applicable) in each log
    • If applicable, any other relevant objects that were created/retrieved in the route business logic (e.g. a grant or an interaction)

Context

Closes #2577.

Checklist

  • Related issues linked using fixes #number
  • Tests added/updated
  • Documentation added
  • Make sure that all checks pass
  • Bruno collection updated

@github-actions github-actions bot added type: source Changes business logic pkg: auth Changes in the GNAP auth package. labels May 2, 2024
Copy link

netlify bot commented May 2, 2024

Deploy Preview for brilliant-pasca-3e80ec canceled.

Name Link
🔨 Latest commit b7d2663
🔍 Latest deploy log https://app.netlify.com/sites/brilliant-pasca-3e80ec/deploys/664caf937930dc00089deddf

Copy link
Contributor

@mkurapov mkurapov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we create an openPaymentsServerErrorMiddleware like we do in backend to catch all of the GNAPErrors to have a single place for a standardized log for each error? (Since most of the logs are added right before the throw).

requestBody: typeof ctx.request.body
} {
return {
requestId: v4(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would show a different uuid for each log, right?
I was looking into a way to have a common request/traceId as a property on a child pino logger, but it is a bit difficult to do that with how we've set up dependency injection with adonis fold library. I'll take another attempt at addressing this in my upcoming changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured it would be workable for now as the routes log once per request, but I admit it's wishful thinking

deps.logger.debug(
{
...generateRouteLogs(ctx),
accessToken
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we be logging out tokens such as accessToken?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make any difference that its a debug log? I guess we can say these can but shouldnt be logged in production. Had the same thought about grant.continueToken elsewhere.

Comment on lines +12 to +26
export function generateRouteLogs(ctx: AppContext): {
route: typeof ctx.path
method: typeof ctx.method
params: typeof ctx.params
headers: typeof ctx.headers
requestBody: typeof ctx.request.body
} {
return {
method: ctx.method,
route: ctx.path,
headers: ctx.headers,
params: ctx.params,
requestBody: ctx.request.body
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine there is probably some sensitive info in here somewhere.

for example from a POST continue/{{continueId}}:

  • requestBody.continue.access_token.value
  • requestBody.access_token.value

And is everything in headers OK to log or should any of it be redacted?

  "headers": {
    "accept": "application/json",
    "content-type": "application/json",
    "content-digest": "sha-512=:wJHRy2GvqCYWZhLTIAdtBaDT9MRO69ldMWgdld0GO8cYVu8uop7pmilYh1xmlVDiiT8d2P1CSdyYgnbCsJIrNA==:",
    "content-length": "174",
    "signature": "sig1=:M5wb//IFQa43pZ6DfUrxcx7RCf3aQWXApk28TmhqgiUrzBbhlu0hs2/zVzXuiB7kEa1LZuq3taQT1XT5wuBDDg==:",
    "signature-input": "sig1=(\"@method\" \"@target-uri\" \"content-digest\" \"content-length\" \"content-type\");keyid=\"keyid-97a3a431-8ee1-48fc-ac85-70e2f5eba8e5\";created=1715013373",
    "user-agent": "axios/1.6.8",
    "accept-encoding": "gzip, compress, deflate, br",
    "host": "happy-life-bank-test-auth:4106",
    "connection": "keep-alive"
  },

If we need to configure pino to redact I imagine it might make sense to do it where we initialize the logger for some things, like the headers (if required).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a good pattern btw though. Also wondering if you think it would make sense to destructure this where we init the pino logger so we dont have to do ...generateRouteLogs(ctx) everywhere? Or if we dont want to always log context do a new loggerWithCtx or something?

@mkurapov
Copy link
Contributor

mkurapov commented May 8, 2024

What do you think about original PR review comment?

Should we create an openPaymentsServerErrorMiddleware like we do in backend to catch all of the GNAPErrors to have a single place for a standardized log for each error? (Since most of the logs are added right before the throw).

Might be nice to match the behaviour in backend

@njlie
Copy link
Contributor Author

njlie commented May 14, 2024

What do you think about original PR review comment?

Should we create an openPaymentsServerErrorMiddleware like we do in backend to catch all of the GNAPErrors to have a single place for a standardized log for each error? (Since most of the logs are added right before the throw).

Might be nice to match the behaviour in backend

I think it's possible to have this PR also match that pattern. It'll be slightly different since it's supposed to also return GNAP error codes, but will be mostly similar.

@github-actions github-actions bot added the type: tests Testing related label May 14, 2024
Copy link
Contributor

@mkurapov mkurapov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, just a few minor comments

Comment on lines 159 to 161
'invalid access token'
)
}

if (!accessToken.grant) {
const logger = await ctx.container.use('logger')
logger.error(
`access token with management id ${ctx.params['id']} has no grant associated with it.`
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this log worth keeping still?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably...in hindsight that's a pretty severe case that shouldn't happen - we should know about if it does for some reason

@@ -3,7 +3,14 @@ import createLogger, { Logger as PinoLogger } from 'pino'
import { Config } from '../config/app'

function initLogger(): PinoLogger {
const logger = createLogger()
const logger = createLogger({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to use this when instantiating the logger singleton in index.ts?

}
}

export async function gnapServerErrorMiddleware(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also check for OpenAPIValidatorMiddlewareError here as well, similar to backend, so we get a nicely formatted error when getting validation errors as well. This means we need to update @interledger/openapi to 2.0.1

BlairCurrey
BlairCurrey previously approved these changes May 20, 2024
Copy link
Contributor

@mkurapov mkurapov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just one comment

ctx.container = deps
})

test('handles GNAPServerRouteError error', async (): Promise<void> => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add a test for the validator middleware error

@njlie njlie force-pushed the nl/2577/auth-routes-logs branch from c7af5bf to b7d2663 Compare May 21, 2024 14:28
@njlie njlie merged commit 8aaf505 into main May 21, 2024
42 checks passed
@njlie njlie deleted the nl/2577/auth-routes-logs branch May 21, 2024 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: auth Changes in the GNAP auth package. type: source Changes business logic type: tests Testing related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add logs in Auth routes
3 participants