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

Chore: [AEA-0000] - Timeout diagnosing #360

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/cognito/src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ const lambdaHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayPro
logger.appendKeys({
"apigw-request-id": event.requestContext?.requestId
})
let start = Date.now()
const axiosInstance = axios.create()
logger.info(`Creating the axios instance took ${Date.now() - start}ms`)

const body = event.body
if (body === undefined) {
Expand All @@ -80,7 +82,9 @@ const lambdaHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayPro
const objectBodyParameters = parse(body as string)
let rewrittenObjectBodyParameters: ParsedUrlQuery

start = Date.now()
const jwtPrivateKey = await getSecret(jwtPrivateKeyArn)
logger.info(`Getting the secret took ${Date.now() - start}ms`)
rewrittenObjectBodyParameters = rewriteBodyToAddSignedJWT(
logger, objectBodyParameters, idpTokenPath, jwtPrivateKey as PrivateKey, jwtKid)

Expand Down
3 changes: 2 additions & 1 deletion packages/common/authFunctions/src/cis2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,15 @@ export const verifyCIS2Token = async (

// Fetch the signing key from the JWKS endpoint
let signingKey
let start = Date.now()
try {
logger.info("Fetching signing key", {kid})
signingKey = await getSigningKey(oidcConfig.jwksClient, kid)
} catch (err) {
logger.error("Error getting signing key", {err})
throw new Error("Error getting signing key")
}
logger.info("Signing key fetched successfully")
logger.info(`Signing key fetched successfully. Took ${Date.now() - start}ms`)

// Verify the token signature
const verifyOptions: jwt.VerifyOptions = {
Expand Down
7 changes: 6 additions & 1 deletion packages/trackerUserInfoLambda/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const CPT_ACCESS_ACTIVITY_CODES = ["B0570", "B0278"]

const lambdaHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
logger.appendKeys({"apigw-request-id": event.requestContext?.requestId})
logger.info("Lambda handler invoked", {event})

// Mock usernames start with "Mock_", and real requests use usernames starting with "Primary_"
const username = getUsernameFromEvent(event)
Expand All @@ -66,23 +65,29 @@ const lambdaHandler = async (event: APIGatewayProxyEvent): Promise<APIGatewayPro

logger.info("Is this a mock request?", {isMockRequest})

let start = Date.now()
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const {cis2AccessToken, cis2IdToken} = await fetchAndVerifyCIS2Tokens(
event,
documentClient,
logger,
isMockRequest ? mockOidcConfig : cis2OidcConfig
)
logger.info(`Fetch and verify CIS2 tokens took: ${Date.now() - start}`)

start = Date.now()
const userInfoResponse = await fetchUserInfo(
cis2AccessToken,
CPT_ACCESS_ACTIVITY_CODES,
undefined,
logger,
isMockRequest ? mockOidcConfig : cis2OidcConfig
)
logger.info(`fetch user info took: ${Date.now() - start}`)

start = Date.now()
updateDynamoTable(username, userInfoResponse, documentClient, logger, tokenMappingTableName)
logger.info(`Update dynamo table took: ${Date.now() - start}`)

return {
statusCode: 200,
Expand Down
14 changes: 3 additions & 11 deletions packages/trackerUserInfoLambda/src/userInfoHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const fetchUserInfo = async (
Authorization: `Bearer ${cis2AccessToken}`
}
})
logger.info("User info fetched successfully", {data: response.data})
logger.info("User info fetched successfully")

// Extract the roles from the user info response
const data: UserInfoResponse = response.data
Expand All @@ -55,17 +55,14 @@ export const fetchUserInfo = async (
const roles = data.nhsid_nrbac_roles || []

roles.forEach((role) => {
logger.debug("Processing role", {role})
const activityCodes = role.activity_codes || []

const hasAccess = activityCodes.some((code: string) => accepted_access_codes.includes(code))
logger.debug("Role CPT access?", {hasAccess})

const roleInfo: RoleDetails = {
role_name: removeRoleCategories(role.role_name),
role_id: role.person_roleid,
org_code: role.org_code,
org_name: getOrgNameFromOrgCode(data, role.org_code, logger)
org_name: getOrgNameFromOrgCode(data, role.org_code)
}

// Ensure the role has at least one of the required fields
Expand All @@ -77,14 +74,11 @@ export const fetchUserInfo = async (

if (hasAccess) {
rolesWithAccess.push(roleInfo)
logger.debug("Role has access; adding to rolesWithAccess", {roleInfo})
} else {
rolesWithoutAccess.push(roleInfo)
logger.debug("Role does not have access; adding to rolesWithoutAccess", {roleInfo})
}

// Determine the currently selected role
logger.debug("Checking if role is currently selected", {selectedRoleId, role_id: role.person_roleid, roleInfo})
if (selectedRoleId && role.person_roleid === selectedRoleId) {
logger.debug("Role is currently selected", {role_id: role.person_roleid, roleInfo})
if (hasAccess) {
Expand Down Expand Up @@ -112,11 +106,9 @@ export const fetchUserInfo = async (
}

// Helper function to get organization name from org_code
function getOrgNameFromOrgCode(data: UserInfoResponse, org_code: string, logger: Logger): string | undefined {
logger.info("Getting org name from org code", {org_code, data})
function getOrgNameFromOrgCode(data: UserInfoResponse, org_code: string): string | undefined {
const orgs = data.nhsid_user_orgs || []
const org = orgs.find((o) => o.org_code === org_code)
logger.info("Found org", {org})
return org ? org.org_name : undefined
}

Expand Down
Loading