-
Notifications
You must be signed in to change notification settings - Fork 2
CCM-12649 Get Status healthcheck endpoint #206
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
17 commits
Select commit
Hold shift + click to select a range
77c3202
CCM-12649 Get status healthcheck endpoint
stevebux 16f802c
Address review comments
stevebux f1a5ffa
Merge branch 'main' into feature/CCM-12649
stevebux aa26b42
Fix following merge with main
stevebux 0ad5632
Avoid leaking system details in 500 error message
stevebux dcc4fc9
Tidy up @internal paths
stevebux 77ddbdd
Update .gitleaksignore
timireland 2dffb63
Fix logging in error mapper
stevebux 9bc9ad3
Address review comments
stevebux 77bf03c
Terraform fix
stevebux 54da309
Allow for no headers
stevebux fc5ac32
Merge branch 'main' into feature/CCM-12649
stevebux 3b562ce
Merge branch 'main' into feature/CCM-12649
masl2 e345ec6
Change response shape
francisco-videira-nhs db23668
Merge remote-tracking branch 'origin/main' into feature/CCM-12649
stevebux 7257ae0
Merge remote-tracking branch 'origin/main' into feature/CCM-12649
stevebux 3c02151
Merge remote-tracking branch 'origin/main' into feature/CCM-12649
stevebux 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
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
76 changes: 76 additions & 0 deletions
76
infrastructure/terraform/components/api/module_lambda_get_status.tf
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,76 @@ | ||
| module "get_status" { | ||
| source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.24/terraform-lambda.zip" | ||
|
|
||
| function_name = "get_status" | ||
| description = "Healthcheck for service" | ||
|
|
||
| aws_account_id = var.aws_account_id | ||
| component = var.component | ||
| environment = var.environment | ||
| project = var.project | ||
| region = var.region | ||
| group = var.group | ||
|
|
||
| log_retention_in_days = var.log_retention_in_days | ||
| kms_key_arn = module.kms.key_arn | ||
|
|
||
| iam_policy_document = { | ||
| body = data.aws_iam_policy_document.get_status_lambda.json | ||
| } | ||
|
|
||
| function_s3_bucket = local.acct.s3_buckets["lambda_function_artefacts"]["id"] | ||
| function_code_base_path = local.aws_lambda_functions_dir_path | ||
| function_code_dir = "api-handler/dist" | ||
| function_include_common = true | ||
| handler_function_name = "getStatus" | ||
| runtime = "nodejs22.x" | ||
| memory = 128 | ||
| timeout = 5 | ||
| log_level = var.log_level | ||
|
|
||
| force_lambda_code_deploy = var.force_lambda_code_deploy | ||
| enable_lambda_insights = false | ||
|
|
||
| send_to_firehose = true | ||
| log_destination_arn = local.destination_arn | ||
| log_subscription_role_arn = local.acct.log_subscription_role_arn | ||
|
|
||
| lambda_env_vars = merge(local.common_lambda_env_vars, {}) | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "get_status_lambda" { | ||
| statement { | ||
| sid = "KMSPermissions" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "kms:Decrypt", | ||
| "kms:GenerateDataKey", | ||
| ] | ||
|
|
||
| resources = [ | ||
| module.kms.key_arn, ## Requires shared kms module | ||
| ] | ||
| } | ||
|
|
||
| statement { | ||
| sid = "AllowDynamoDBAccess" | ||
| effect = "Allow" | ||
|
|
||
| actions = [ | ||
| "dynamodb:DescribeTable" | ||
| ] | ||
|
|
||
| resources = [ | ||
| aws_dynamodb_table.letters.arn, | ||
| "${aws_dynamodb_table.letters.arn}/index/supplierStatus-index" | ||
| ] | ||
| } | ||
|
|
||
|
|
||
| statement { | ||
| sid = "S3ListAllMyBuckets" | ||
| actions = ["s3:ListAllMyBuckets"] | ||
| resources = ["arn:aws:s3:::*"] | ||
| } | ||
| } |
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,38 @@ | ||
| import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; | ||
| import { DBHealthcheck } from "../healthcheck"; | ||
| import { createTables, DBContext, deleteTables, setupDynamoDBContainer } from "./db"; | ||
|
|
||
| // Database tests can take longer, especially with setup and teardown | ||
| jest.setTimeout(30000); | ||
|
|
||
| describe('DBHealthcheck', () => { | ||
|
|
||
| let db: DBContext; | ||
|
|
||
| beforeAll(async () => { | ||
| db = await setupDynamoDBContainer(); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| await createTables(db); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await deleteTables(db); | ||
| }); | ||
|
|
||
| it('passes when the database is available', async () => { | ||
| const dbHealthCheck = new DBHealthcheck(db.docClient, db.config); | ||
| await dbHealthCheck.check(); | ||
| }); | ||
|
|
||
| it('fails when the database is unavailable', async () => { | ||
| const realFunction = db.docClient.send; | ||
| db.docClient.send = jest.fn().mockImplementation(() => { throw new Error('Failed to send')}); | ||
|
|
||
| const dbHealthCheck = new DBHealthcheck(db.docClient, db.config); | ||
| await expect(dbHealthCheck.check()).rejects.toThrow(); | ||
|
|
||
| db.docClient.send = realFunction; | ||
| }); | ||
| }); |
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,13 @@ | ||
| import { DescribeTableCommand } from "@aws-sdk/client-dynamodb"; | ||
| import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; | ||
| import { LetterRepositoryConfig } from "./letter-repository"; | ||
|
|
||
| export class DBHealthcheck { | ||
| constructor(readonly ddbClient: DynamoDBDocumentClient, | ||
| readonly config: LetterRepositoryConfig) {} | ||
|
|
||
| async check(): Promise<void> { | ||
| await this.ddbClient.send(new DescribeTableCommand({ | ||
| TableName: this.config.lettersTableName})); | ||
| } | ||
| } |
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
73 changes: 73 additions & 0 deletions
73
lambdas/api-handler/src/handlers/__tests__/get_status.test.ts
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,73 @@ | ||
| import { S3Client } from "@aws-sdk/client-s3"; | ||
| import { DBHealthcheck } from "@internal/datastore/src"; | ||
| import pino from "pino"; | ||
| import { Deps } from "../../config/deps"; | ||
| import { makeApiGwEvent } from "./utils/test-utils"; | ||
| import { mockDeep } from "jest-mock-extended"; | ||
| import { Context } from "aws-lambda"; | ||
| import { createGetStatusHandler } from "../get-status"; | ||
|
|
||
| describe('API Lambda handler', () => { | ||
| it('passes if S3 and DynamoDB are available', async() => { | ||
|
|
||
| const event = makeApiGwEvent({path: '/_status', | ||
| headers: undefined | ||
| }); | ||
|
|
||
| const getLetterDataHandler = createGetStatusHandler(getMockedDeps()); | ||
| const result = await getLetterDataHandler(event, mockDeep<Context>(), jest.fn()); | ||
|
|
||
| expect(result).toEqual({ | ||
| statusCode: 200, | ||
| body: JSON.stringify({ code: 200 }, null, 2) | ||
| }); | ||
| }); | ||
|
|
||
| it('fails if S3 is unavailable', async() => { | ||
| const mockedDeps = getMockedDeps(); | ||
| mockedDeps.s3Client.send = jest.fn().mockRejectedValue(new Error('unexpected error')); | ||
|
|
||
| const event = makeApiGwEvent({path: '/_status', | ||
| headers: undefined | ||
| }); | ||
|
|
||
| const getLetterDataHandler = createGetStatusHandler(mockedDeps); | ||
| const result = await getLetterDataHandler(event, mockDeep<Context>(), jest.fn()); | ||
|
|
||
| expect(result).toEqual({ | ||
| statusCode: 500, | ||
| body: JSON.stringify({ code: 500 }, null, 2) | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
| it('fails if DynamoDB is unavailable', async() => { | ||
| const mockedDeps = getMockedDeps(); | ||
| mockedDeps.dbHealthcheck.check = jest.fn().mockRejectedValue(new Error('unexpected error')); | ||
|
|
||
| const event = makeApiGwEvent({path: '/_status', | ||
| headers: {'Nhsd-Correlation-Id': 'correlationId'} | ||
| }); | ||
|
|
||
| const getLetterDataHandler = createGetStatusHandler(mockedDeps); | ||
| const result = await getLetterDataHandler(event, mockDeep<Context>(), jest.fn()); | ||
|
|
||
| expect(result).toEqual({ | ||
| statusCode: 500, | ||
| body: JSON.stringify({ code: 500 }, null, 2) | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
| function getMockedDeps(): jest.Mocked<Deps> { | ||
| return { | ||
| s3Client: { send: jest.fn()} as unknown as S3Client, | ||
| dbHealthcheck: {check: jest.fn()} as unknown as DBHealthcheck, | ||
| logger: { info: jest.fn(), error: jest.fn() } as unknown as pino.Logger, | ||
| env: { | ||
| SUPPLIER_ID_HEADER: 'nhsd-supplier-id', | ||
| APIM_CORRELATION_HEADER: 'nhsd-correlation-id' | ||
| } | ||
| } as Deps; | ||
| } | ||
| }); |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.