Skip to content

Commit b142808

Browse files
CCM-13787: create supplier mock for testing (#535)
* add supplier mock dummy lambda function * correct the name of the supplier_mock lambda * remove comment from tf file * get dummy test to pass * disable vale spelling from README * correct lambda handler name * restore changes * call patch letter and add variables to parameter store * resolve final conflicts * refresh package-lock file * correct lint and typecheck errors * resolve final conflicts * re-add @pact-foundation/pact library in run-pact-tests.sh script * install amd64 version of @pact-foundation if missing * add ssm:GetParameter permission for supplierId parameter in supplier mock * get package-lock in line with main * don't deploy the scheduler by default * only deploy scheduler resources when flag is set to true * deploy the schedule to test it * add README with instructions on the supplier-mock lambda * add supplier-mock config in parameter store whith specificationId mapping * fix typecheck * remove comment * correct patch letter request body * separate the ssm_parameter to its own file * npm instlal * update README for supplier-mock lambda * address Sid comments * move terraform resources into separate folders * align README structure with the rest * fix zod typecheck error * don't deploy the scheduler by default * conditionally deploy the aws_iam_policy_document.supplier_mock_lambda data block
1 parent cd6e693 commit b142808

28 files changed

Lines changed: 1267 additions & 4 deletions

.env.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Your github Personal Access Token (PAT)
2+
PR_NUMBER=prxx # remove if needs to run against main
23
GITHUB_TOKEN=
34

45
# Apigee proxy name to be used for test execution

.github/workflows/stage-3-build.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ jobs:
189189
--terraformAction "apply" \
190190
--overrideProjectName "nhs" \
191191
--overrideRoleName "nhs-main-acct-supplier-api-github-deploy"
192-
193192
populate-config:
194193
name: "Populate Supplier Config"
195194
runs-on: ubuntu-latest

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ version.json
1313
# Please, add your custom content below!
1414
.idea
1515
.env
16+
.devcontainer/devcontainer-lock.json
1617

1718
# dependencies
1819
node_modules

infrastructure/terraform/components/api/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ No requirements.
1818
| <a name="input_csoc_destination_account"></a> [csoc\_destination\_account](#input\_csoc\_destination\_account) | value of the CSOC destination account, if applicable. If null, CSOC destination account will not be added as a resource in the logging policy | `string` | `"000000000000"` | no |
1919
| <a name="input_csoc_log_forwarding"></a> [csoc\_log\_forwarding](#input\_csoc\_log\_forwarding) | Enable forwarding of API Gateway logs to CSOC | `bool` | `true` | no |
2020
| <a name="input_default_tags"></a> [default\_tags](#input\_default\_tags) | A map of default tags to apply to all taggable resources within the component | `map(string)` | `{}` | no |
21+
| <a name="input_deploy_supplier_mock_scheduler"></a> [deploy\_supplier\_mock\_scheduler](#input\_deploy\_supplier\_mock\_scheduler) | Deploy EventBridge Scheduler trigger for supplier mock lambda | `bool` | `false` | no |
2122
| <a name="input_disable_gateway_execute_endpoint"></a> [disable\_gateway\_execute\_endpoint](#input\_disable\_gateway\_execute\_endpoint) | Disable the execution endpoint for the API Gateway | `bool` | `true` | no |
2223
| <a name="input_download_url_ttl_seconds"></a> [download\_url\_ttl\_seconds](#input\_download\_url\_ttl\_seconds) | TTL in seconds for generated download URLs | `number` | `60` | no |
2324
| <a name="input_enable_alarms"></a> [enable\_alarms](#input\_enable\_alarms) | Enable CloudWatch alarms for this deployed environment | `bool` | `true` | no |
@@ -26,6 +27,7 @@ No requirements.
2627
| <a name="input_enable_event_anomaly_detection"></a> [enable\_event\_anomaly\_detection](#input\_enable\_event\_anomaly\_detection) | Enable CloudWatch anomaly detection alarm for SNS message Detects abnormal drops or spikes in event publishing volume. | `bool` | `true` | no |
2728
| <a name="input_enable_event_cache"></a> [enable\_event\_cache](#input\_enable\_event\_cache) | Enable caching of events to an S3 bucket | `bool` | `true` | no |
2829
| <a name="input_enable_sns_delivery_logging"></a> [enable\_sns\_delivery\_logging](#input\_enable\_sns\_delivery\_logging) | Enable SNS Delivery Failure Notifications | `bool` | `true` | no |
30+
| <a name="input_enable_supplier_mock_scheduler"></a> [enable\_supplier\_mock\_scheduler](#input\_enable\_supplier\_mock\_scheduler) | Enable EventBridge Scheduler trigger for supplier mock lambda | `bool` | `false` | no |
2931
| <a name="input_environment"></a> [environment](#input\_environment) | The name of the tfscaffold environment | `string` | n/a | yes |
3032
| <a name="input_event_anomaly_band_width"></a> [event\_anomaly\_band\_width](#input\_event\_anomaly\_band\_width) | The width of the anomaly detection band. Higher values (e.g. 4-6) reduce sensitivity and noise, lower values (e.g. 2-3) increase sensitivity. Recommended: 2-4. | `number` | `4` | no |
3133
| <a name="input_event_anomaly_evaluation_periods"></a> [event\_anomaly\_evaluation\_periods](#input\_event\_anomaly\_evaluation\_periods) | Number of evaluation periods for the anomaly alarm. Each period is defined by event\_anomaly\_period. | `number` | `3` | no |
@@ -51,6 +53,7 @@ No requirements.
5153
| <a name="input_region"></a> [region](#input\_region) | The AWS Region | `string` | n/a | yes |
5254
| <a name="input_shared_infra_account_id"></a> [shared\_infra\_account\_id](#input\_shared\_infra\_account\_id) | The AWS Account ID of the shared infrastructure account | `string` | `"000000000000"` | no |
5355
| <a name="input_sns_success_logging_sample_percent"></a> [sns\_success\_logging\_sample\_percent](#input\_sns\_success\_logging\_sample\_percent) | Enable SNS Delivery Successful Sample Percentage | `number` | `0` | no |
56+
| <a name="input_supplier_mock_schedule_expression"></a> [supplier\_mock\_schedule\_expression](#input\_supplier\_mock\_schedule\_expression) | Schedule expression for supplier mock scheduler | `string` | `"rate(1 minute)"` | no |
5457
## Modules
5558

5659
| Name | Source | Version |
@@ -85,6 +88,7 @@ No requirements.
8588
| <a name="module_sqs_supplier_config"></a> [sqs\_supplier\_config](#module\_sqs\_supplier\_config) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/3.1.5/terraform-sqs.zip | n/a |
8689
| <a name="module_supplier_allocator"></a> [supplier\_allocator](#module\_supplier\_allocator) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/4.0.2/terraform-lambda.zip | n/a |
8790
| <a name="module_supplier_config_ingress"></a> [supplier\_config\_ingress](#module\_supplier\_config\_ingress) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.29/terraform-lambda.zip | n/a |
91+
| <a name="module_supplier_mock"></a> [supplier\_mock](#module\_supplier\_mock) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/4.0.5/terraform-lambda.zip | n/a |
8892
| <a name="module_supplier_ssl"></a> [supplier\_ssl](#module\_supplier\_ssl) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/v2.0.26/terraform-ssl.zip | n/a |
8993
| <a name="module_update_letter_queue"></a> [update\_letter\_queue](#module\_update\_letter\_queue) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/4.0.2/terraform-lambda.zip | n/a |
9094
| <a name="module_upsert_letter"></a> [upsert\_letter](#module\_upsert\_letter) | https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/4.0.2/terraform-lambda.zip | n/a |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
resource "aws_iam_policy" "supplier_mock_scheduler_invoke_policy" {
2+
count = var.deploy_supplier_mock_scheduler ? 1 : 0
3+
name = "${local.csi}-supplier-mock-scheduler-invoke"
4+
policy = data.aws_iam_policy_document.supplier_mock_scheduler_invoke_policy[0].json
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
resource "aws_iam_role_policy_attachment" "supplier_mock_scheduler_invoke_policy" {
2+
count = var.deploy_supplier_mock_scheduler ? 1 : 0
3+
role = aws_iam_role.supplier_mock_scheduler[0].name
4+
policy_arn = aws_iam_policy.supplier_mock_scheduler_invoke_policy[0].arn
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
resource "aws_iam_role" "supplier_mock_scheduler" {
2+
name = "${local.csi}-supplier-mock-scheduler"
3+
description = "Allows EventBridge Scheduler to invoke supplier mock lambda"
4+
assume_role_policy = data.aws_iam_policy_document.supplier_mock_scheduler_trust_policy.json
5+
count = var.deploy_supplier_mock_scheduler ? 1 : 0
6+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
module "supplier_mock" {
2+
source = "https://github.com/NHSDigital/nhs-notify-shared-modules/releases/download/4.0.5/terraform-lambda.zip"
3+
count = var.deploy_supplier_mock_scheduler ? 1 : 0
4+
5+
function_name = "supplier_mock"
6+
description = "Mock the behaviour of a supplier"
7+
8+
aws_account_id = var.aws_account_id
9+
component = var.component
10+
environment = var.environment
11+
project = var.project
12+
region = var.region
13+
group = var.group
14+
15+
log_retention_in_days = var.log_retention_in_days
16+
kms_key_arn = module.kms.key_arn
17+
18+
iam_policy_document = {
19+
body = data.aws_iam_policy_document.supplier_mock_lambda[0].json
20+
}
21+
22+
function_s3_bucket = local.acct.s3_buckets["lambda_function_artefacts"]["id"]
23+
function_code_base_path = local.aws_lambda_functions_dir_path
24+
function_code_dir = "supplier-mock/dist"
25+
function_include_common = true
26+
handler_function_name = "supplierMockHandler"
27+
runtime = "nodejs22.x"
28+
memory = 512
29+
timeout = 29
30+
log_level = var.log_level
31+
32+
force_lambda_code_deploy = var.force_lambda_code_deploy
33+
enable_lambda_insights = false
34+
35+
log_destination_arn = local.destination_arn
36+
log_subscription_role_arn = local.acct.log_subscription_role_arn
37+
38+
lambda_env_vars = merge(local.common_lambda_env_vars, {
39+
ENVIRONMENT = var.environment
40+
GET_LETTERS_FUNCTION_NAME = module.get_letters.function_name
41+
PATCH_LETTER_FUNCTION_NAME = module.patch_letter.function_name
42+
SUPPLIER_MOCK_CONFIG_PARAM_NAME = aws_ssm_parameter.supplier_mock_config[0].name
43+
})
44+
}
45+
46+
data "aws_iam_policy_document" "supplier_mock_lambda" {
47+
count = var.deploy_supplier_mock_scheduler ? 1 : 0
48+
49+
statement {
50+
sid = "KMSPermissions"
51+
effect = "Allow"
52+
53+
actions = [
54+
"kms:Decrypt",
55+
"kms:GenerateDataKey",
56+
]
57+
58+
resources = [
59+
module.kms.key_arn, ## Requires shared kms module
60+
]
61+
}
62+
63+
statement {
64+
sid = "AllowInvokeLambda"
65+
effect = "Allow"
66+
67+
actions = [
68+
"lambda:InvokeFunction",
69+
]
70+
71+
resources = [
72+
module.get_letters.function_arn,
73+
module.patch_letter.function_arn
74+
]
75+
}
76+
77+
statement {
78+
sid = "AllowReadSupplierMockConfigParameter"
79+
effect = "Allow"
80+
81+
actions = [
82+
"ssm:GetParameter",
83+
]
84+
85+
resources = [
86+
aws_ssm_parameter.supplier_mock_config[0].arn
87+
]
88+
}
89+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
resource "aws_scheduler_schedule" "supplier_mock" {
2+
count = var.deploy_supplier_mock_scheduler ? 1 : 0
3+
name = "${local.csi}-supplier-mock"
4+
description = "Scheduled trigger for supplier mock lambda"
5+
state = var.enable_supplier_mock_scheduler ? "ENABLED" : "DISABLED"
6+
7+
flexible_time_window {
8+
mode = "OFF"
9+
}
10+
11+
schedule_expression = var.supplier_mock_schedule_expression
12+
13+
target {
14+
arn = module.supplier_mock[0].function_arn
15+
role_arn = aws_iam_role.supplier_mock_scheduler[0].arn
16+
input = jsonencode({
17+
source = "eventbridge-scheduler"
18+
})
19+
}
20+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
data "aws_iam_policy_document" "supplier_mock_scheduler_trust_policy" {
2+
statement {
3+
sid = "AllowSchedulerAssumeRole"
4+
effect = "Allow"
5+
6+
actions = [
7+
"sts:AssumeRole",
8+
]
9+
10+
principals {
11+
type = "Service"
12+
13+
identifiers = [
14+
"scheduler.amazonaws.com",
15+
]
16+
}
17+
}
18+
}
19+
20+
21+
data "aws_iam_policy_document" "supplier_mock_scheduler_invoke_policy" {
22+
count = var.deploy_supplier_mock_scheduler ? 1 : 0
23+
24+
statement {
25+
sid = "AllowInvokeSupplierMockLambda"
26+
effect = "Allow"
27+
28+
actions = [
29+
"lambda:InvokeFunction",
30+
]
31+
32+
resources = [
33+
module.supplier_mock[0].function_arn,
34+
]
35+
}
36+
}

0 commit comments

Comments
 (0)