Skip to content

Commit

Permalink
feat: remove even more serverless v2 logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Jun 2, 2022
1 parent 6b101aa commit 677d1b1
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 284 deletions.
14 changes: 2 additions & 12 deletions src/events/authFunctionNameExtractor.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import serverlessLog from '../serverlessLog.js'

// FIXME "slessLog" param is only remaining for tests, should be removed
export default function authFunctionNameExtractor(endpoint, slessLog, v3Utils) {
export default function authFunctionNameExtractor(endpoint, v3Utils) {
const buildFailureResult = (warningMessage) => {
const log = v3Utils && v3Utils.log
const _serverlessLog = slessLog || serverlessLog // FIXME remove

if (log) {
log.warning(warningMessage)
} else {
_serverlessLog(`WARNING: ${warningMessage}`)
}
v3Utils.log.warning(warningMessage)

return { unsupportedAuth: true }
}
Expand Down
5 changes: 2 additions & 3 deletions src/events/authValidateContext.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Boom from '@hapi/boom'
import serverlessLog from '../serverlessLog.js'

const { keys, values } = Object

Expand Down Expand Up @@ -29,7 +28,7 @@ function transform(context) {
return context
}

export default function authValidateContext(context, authFunName) {
export default function authValidateContext(context, authFunName, { log }) {
if (typeof context !== 'object') {
return internalServerError('Authorizer response context must be an object')
}
Expand All @@ -38,7 +37,7 @@ export default function authValidateContext(context, authFunName) {
const error =
'Authorizer response context values must be of type string, number, or boolean'

serverlessLog(
log.notice(
`Detected invalid value types returned in authorizer context: (λ: ${authFunName}). ${error}. ` +
'More info: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-output.html',
)
Expand Down
24 changes: 7 additions & 17 deletions src/events/http/HttpServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import LambdaProxyIntegrationEventV2 from './lambda-events/LambdaProxyIntegrationEventV2.js'
import parseResources from './parseResources.js'
import payloadSchemaValidator from './payloadSchemaValidator.js'
import serverlessLog, { logRoutes } from '../../serverlessLog.js'
import logRoutes from '../../utils/logRoutes.js'
import {
detectEncoding,
generateHapiPath,
Expand Down Expand Up @@ -222,11 +222,7 @@ export default class HttpServer {
try {
await this.#server.register([h2o2])
} catch (err) {
if (this.log) {
this.log.error(err)
} else {
serverlessLog(err)
}
this.log.error(err)
}
}

Expand Down Expand Up @@ -299,7 +295,7 @@ export default class HttpServer {
}

#extractAuthFunctionName(endpoint) {
const result = authFunctionNameExtractor(endpoint, null, this)
const result = authFunctionNameExtractor(endpoint, this.v3Utils)

return result.unsupportedAuth ? null : result.authorizerName
}
Expand Down Expand Up @@ -1090,7 +1086,7 @@ export default class HttpServer {
}

#replyError(statusCode, response, message, error) {
serverlessLog(message)
this.log.notice(message)

this.log.error(error)

Expand Down Expand Up @@ -1214,15 +1210,9 @@ export default class HttpServer {
resultUri += request.url.search // search is empty string by default
}

if (log) {
log.notice(
`PROXY ${request.method} ${request.url.pathname} -> ${resultUri}`,
)
} else {
serverlessLog(
`PROXY ${request.method} ${request.url.pathname} -> ${resultUri}`,
)
}
log.notice(
`PROXY ${request.method} ${request.url.pathname} -> ${resultUri}`,
)

return h.proxy({
passThrough: true,
Expand Down
8 changes: 1 addition & 7 deletions src/events/http/authJWTSettingsExtractor.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import serverlessLog from '../../serverlessLog.js'

export default function authJWTSettingsExtractor(
endpoint,
provider,
ignoreJWTSignature,
{ log },
) {
const buildFailureResult = (warningMessage) => {
if (log) {
log.warning(warningMessage)
} else {
serverlessLog(`WARNING: ${warningMessage}`)
}
log.warning(warningMessage)

return {
unsupportedAuth: true,
Expand Down
65 changes: 17 additions & 48 deletions src/events/http/createAuthScheme.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Boom from '@hapi/boom'
import authCanExecuteResource from '../authCanExecuteResource.js'
import authValidateContext from '../authValidateContext.js'
import serverlessLog from '../../serverlessLog.js'
import {
nullIfEmpty,
parseHeaders,
Expand Down Expand Up @@ -34,17 +33,10 @@ export default function createAuthScheme(
// Create Auth Scheme
return () => ({
async authenticate(request, h) {
if (log) {
log.notice()
log.notice(
`Running Authorization function for ${request.method} ${request.path} (λ: ${authFunName})`,
)
} else {
console.log('') // Just to make things a little pretty
serverlessLog(
`Running Authorization function for ${request.method} ${request.path} (λ: ${authFunName})`,
)
}
log.notice()
log.notice(
`Running Authorization function for ${request.method} ${request.path} (λ: ${authFunName})`,
)

// Get Authorization header
const { req } = request.raw
Expand Down Expand Up @@ -123,29 +115,17 @@ export default function createAuthScheme(

// Validate that the policy document has the principalId set
if (!result.principalId) {
if (log) {
log.notice(
`Authorization response did not include a principalId: (λ: ${authFunName})`,
)
} else {
serverlessLog(
`Authorization response did not include a principalId: (λ: ${authFunName})`,
)
}
log.notice(
`Authorization response did not include a principalId: (λ: ${authFunName})`,
)

return Boom.forbidden('No principalId set on the Response')
}

if (!authCanExecuteResource(result.policyDocument, event.methodArn)) {
if (log) {
log.notice(
`Authorization response didn't authorize user to access resource: (λ: ${authFunName})`,
)
} else {
serverlessLog(
`Authorization response didn't authorize user to access resource: (λ: ${authFunName})`,
)
}
log.notice(
`Authorization response didn't authorize user to access resource: (λ: ${authFunName})`,
)

return Boom.forbidden(
'User is not authorized to access this resource',
Expand All @@ -158,6 +138,7 @@ export default function createAuthScheme(
const validationResult = authValidateContext(
result.context,
authFunName,
{ log },
)

if (validationResult instanceof Error) {
Expand All @@ -167,15 +148,9 @@ export default function createAuthScheme(
result.context = validationResult
}

if (log) {
log.notice(
`Authorization function returned a successful response: (λ: ${authFunName})`,
)
} else {
serverlessLog(
`Authorization function returned a successful response: (λ: ${authFunName})`,
)
}
log.notice(
`Authorization function returned a successful response: (λ: ${authFunName})`,
)

const authorizer = {
integrationLatency: '42',
Expand All @@ -193,15 +168,9 @@ export default function createAuthScheme(
},
})
} catch {
if (log) {
log.notice(
`Authorization function returned an error response: (λ: ${authFunName})`,
)
} else {
serverlessLog(
`Authorization function returned an error response: (λ: ${authFunName})`,
)
}
log.notice(
`Authorization function returned an error response: (λ: ${authFunName})`,
)

return Boom.unauthorized('Unauthorized')
}
Expand Down
64 changes: 15 additions & 49 deletions src/events/http/createJWTAuthScheme.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Boom from '@hapi/boom'
import { decode } from 'jsonwebtoken'
import serverlessLog from '../../serverlessLog.js'

const { isArray } = Array

Expand All @@ -20,22 +19,10 @@ export default function createAuthScheme(jwtOptions, { log }) {
// Create Auth Scheme
return () => ({
async authenticate(request, h) {
if (log) {
log.notice()
log.notice(
`Running JWT Authorization function for ${request.method} ${request.path} (${authorizerName})`,
)
} else {
console.log('') // Just to make things a little pretty

// TODO: this only validates specific properties of the JWT
// it does not verify the JWT is correctly signed. That would
// be a great feature to add under an optional flag :)

serverlessLog(
`Running JWT Authorization function for ${request.method} ${request.path} (${authorizerName})`,
)
}
log.notice()
log.notice(
`Running JWT Authorization function for ${request.method} ${request.path} (${authorizerName})`,
)

// Get Authorization header
const { req } = request.raw
Expand All @@ -58,11 +45,8 @@ export default function createAuthScheme(jwtOptions, { log }) {
const { iss, aud, scope } = decoded.payload
const clientId = decoded.payload.client_id
if (iss !== jwtOptions.issuerUrl) {
if (log) {
log.notice(`JWT Token not from correct issuer url`)
} else {
serverlessLog(`JWT Token not from correct issuer url`)
}
log.notice(`JWT Token not from correct issuer url`)

return Boom.unauthorized('JWT Token not from correct issuer url')
}

Expand All @@ -75,11 +59,8 @@ export default function createAuthScheme(jwtOptions, { log }) {
)

if (!validAudienceProvided && !validAudiences.includes(clientId)) {
if (log) {
log.notice(`JWT Token does not contain correct audience`)
} else {
serverlessLog(`JWT Token does not contain correct audience`)
}
log.notice(`JWT Token does not contain correct audience`)

return Boom.unauthorized(
'JWT Token does not contain correct audience',
)
Expand All @@ -88,11 +69,8 @@ export default function createAuthScheme(jwtOptions, { log }) {
let scopes = null
if (jwtOptions.scopes && jwtOptions.scopes.length) {
if (!scope) {
if (log) {
log.notice(`JWT Token missing valid scope`)
} else {
serverlessLog(`JWT Token missing valid scope`)
}
log.notice(`JWT Token missing valid scope`)

return Boom.forbidden('JWT Token missing valid scope')
}

Expand All @@ -102,20 +80,13 @@ export default function createAuthScheme(jwtOptions, { log }) {
return !jwtOptions.scopes.includes(s)
})
) {
if (log) {
log.notice(`JWT Token missing valid scope`)
} else {
serverlessLog(`JWT Token missing valid scope`)
}
log.notice(`JWT Token missing valid scope`)

return Boom.forbidden('JWT Token missing valid scope')
}
}

if (log) {
log.notice(`JWT Token validated`)
} else {
serverlessLog(`JWT Token validated`)
}
log.notice(`JWT Token validated`)

// Set the credentials for the rest of the pipeline
// return resolve(
Expand All @@ -126,13 +97,8 @@ export default function createAuthScheme(jwtOptions, { log }) {
},
})
} catch (err) {
if (log) {
log.notice(`JWT could not be decoded`)
log.error(err)
} else {
serverlessLog(`JWT could not be decoded`)
serverlessLog(err)
}
log.notice(`JWT could not be decoded`)
log.error(err)

return Boom.unauthorized('Unauthorized')
}
Expand Down
14 changes: 5 additions & 9 deletions src/events/http/lambda-events/LambdaIntegrationEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default class LambdaIntegrationEvent {
this.#request = request
this.#requestTemplate = requestTemplate
this.#stage = stage

this.log = v3Utils.log
this.v3Utils = v3Utils
}

Expand All @@ -31,15 +33,9 @@ export default class LambdaIntegrationEvent {
}
}
} catch {
if (this.log) {
this.log.error(
'Could not parse process.env.AUTHORIZER, make sure it is correct JSON',
)
} else {
console.error(
'Serverless-offline: Could not parse process.env.AUTHORIZER, make sure it is correct JSON.',
)
}
this.log.error(
'Could not parse process.env.AUTHORIZER, make sure it is correct JSON',
)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/events/websocket/WebSocketClients.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export default class WebSocketClients {
const validatedContext = authValidateContext(
policy.context,
authorizerFunction,
{ log: this.log },
)
if (validatedContext instanceof Error) throw validatedContext

Expand Down Expand Up @@ -374,7 +375,7 @@ export default class WebSocketClients {
return null
}

const result = authFunctionNameExtractor(endpoint, null, this)
const result = authFunctionNameExtractor(endpoint, this.v3Utils)

return result.unsupportedAuth ? null : result.authorizerName
}
Expand Down
Loading

0 comments on commit 677d1b1

Please sign in to comment.