Skip to content

Commit

Permalink
refactor: tracer wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
KishenKumarrrrr committed Nov 22, 2024
1 parent 2fd482d commit f36beed
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
40 changes: 16 additions & 24 deletions backend/src/email/middlewares/email-callback.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
import { Request, Response, NextFunction } from 'express'
import { EmailCallbackService } from '@email/services'
import { loggerWithLabel } from '@core/logger'
import { tracer } from 'dd-trace'

const logger = loggerWithLabel(module)

const isAuthenticated = (
req: Request,
res: Response,
next: NextFunction
): Response | void => {
tracer.wrap('isAuthenticated', () => {
const authHeader = req.get('authorization')
if (!authHeader) {
// SNS will send 2 request:
// - first one without the basic authorization first and require the callback
// server to respond with 401 WWW-Authenticate Basic realm="Email"
// - second one with the basic authorization
// The above mechanism is based on RFC-2671 https://www.rfc-editor.org/rfc/rfc2617.html#page-8
// Of course, this middleare is to reject all requests without the
// Authorization header as well
res.set('WWW-Authenticate', 'Basic realm="Email"')
return res.sendStatus(401)
}
if (EmailCallbackService.isAuthenticated(authHeader)) {
return next()
}
return res.sendStatus(403)
})
const authHeader = req.get('authorization')
if (!authHeader) {
// SNS will send 2 request:
// - first one without the basic authorization first and require the callback
// server to respond with 401 WWW-Authenticate Basic realm="Email"
// - second one with the basic authorization
// The above mechanism is based on RFC-2671 https://www.rfc-editor.org/rfc/rfc2617.html#page-8
// Of course, this middleare is to reject all requests without the
// Authorization header as well
res.set('WWW-Authenticate', 'Basic realm="Email"')
return res.sendStatus(401)
}
if (EmailCallbackService.isAuthenticated(authHeader)) {
return next()
}
return res.sendStatus(403)
}

const parseEvent = async (
Expand All @@ -48,9 +44,6 @@ const printConfirmSubscription = (
res: Response,
next: NextFunction
): Response | void => {
const printConfirmSubscriptionSpan = tracer.startSpan(
'printConfirmSubscription'
)
const { Type: type, SubscribeURL: subscribeUrl } = JSON.parse(req.body)
if (type === 'SubscriptionConfirmation') {
const parsed = new URL(subscribeUrl)
Expand All @@ -68,7 +61,6 @@ const printConfirmSubscription = (
return res.sendStatus(202)
}
}
printConfirmSubscriptionSpan.finish()
return next()
}
export const EmailCallbackMiddleware = {
Expand Down
10 changes: 7 additions & 3 deletions backend/src/email/routes/email-callback.routes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Router } from 'express'
import { EmailCallbackMiddleware } from '@email/middlewares'
import tracer from 'dd-trace'
const router = Router()
/**
* paths:
Expand All @@ -16,9 +17,12 @@ const router = Router()
*/
router.post(
'/',
EmailCallbackMiddleware.printConfirmSubscription,
EmailCallbackMiddleware.isAuthenticated,
EmailCallbackMiddleware.parseEvent
tracer.wrap(
'printConfirmSubscription',
() => EmailCallbackMiddleware.printConfirmSubscription
),
tracer.wrap('isAuthenticated', () => EmailCallbackMiddleware.isAuthenticated),
tracer.wrap('parseEvent', () => EmailCallbackMiddleware.parseEvent)
)

export default router

0 comments on commit f36beed

Please sign in to comment.