Skip to content

Commit a70ebb8

Browse files
authored
CCM-10333: save clientId on templates (#529)
1 parent 8f1c6ee commit a70ebb8

File tree

85 files changed

+1708
-492
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+1708
-492
lines changed

infrastructure/terraform/components/sandbox/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ No requirements.
2525
| Name | Source | Version |
2626
|------|--------|---------|
2727
| <a name="module_backend_api"></a> [backend\_api](#module\_backend\_api) | ../../modules/backend-api | n/a |
28+
| <a name="module_cognito_triggers"></a> [cognito\_triggers](#module\_cognito\_triggers) | ../../modules/cognito-triggers | n/a |
2829
## Outputs
2930

3031
| Name | Description |
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
resource "aws_cognito_user_pool" "sandbox" {
22
name = local.csi
3+
4+
lambda_config {
5+
pre_token_generation_config {
6+
lambda_arn = module.cognito_triggers.pre_token_generation_lambda_function_arn
7+
lambda_version = "V2_0"
8+
}
9+
}
10+
11+
schema {
12+
name = "sbx_client_id"
13+
attribute_data_type = "String"
14+
mutable = true
15+
required = false
16+
string_attribute_constraints {}
17+
}
318
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module "cognito_triggers" {
2+
source = "../../modules/cognito-triggers"
3+
4+
aws_account_id = var.aws_account_id
5+
component = var.component
6+
environment = var.environment
7+
project = var.project
8+
region = var.region
9+
group = var.group
10+
11+
function_s3_bucket = local.acct.s3_buckets["artefacts"]["id"]
12+
kms_key_arn = data.aws_kms_key.sandbox.arn
13+
log_retention_in_days = var.log_retention_in_days
14+
user_pool_id = aws_cognito_user_pool.sandbox.id
15+
}

infrastructure/terraform/modules/backend-api/spec.tmpl.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@
88
},
99
{
1010
"properties": {
11+
"clientId": {
12+
"type": "string"
13+
},
1114
"createdAt": {
1215
"format": "date-time",
1316
"type": "string"
1417
},
18+
"createdBy": {
19+
"type": "string"
20+
},
1521
"id": {
1622
"type": "string"
1723
},
@@ -21,12 +27,15 @@
2127
"updatedAt": {
2228
"format": "date-time",
2329
"type": "string"
30+
},
31+
"updatedBy": {
32+
"type": "string"
2433
}
2534
},
2635
"required": [
36+
"createdAt",
2737
"id",
2838
"templateStatus",
29-
"createdAt",
3039
"updatedAt"
3140
],
3241
"type": "object"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!-- BEGIN_TF_DOCS -->
2+
<!-- markdownlint-disable -->
3+
<!-- vale off -->
4+
5+
## Requirements
6+
7+
No requirements.
8+
## Inputs
9+
10+
| Name | Description | Type | Default | Required |
11+
|------|-------------|------|---------|:--------:|
12+
| <a name="input_aws_account_id"></a> [aws\_account\_id](#input\_aws\_account\_id) | The AWS Account ID (numeric) | `string` | n/a | yes |
13+
| <a name="input_component"></a> [component](#input\_component) | The variable encapsulating the name of this component | `string` | `"cog"` | no |
14+
| <a name="input_environment"></a> [environment](#input\_environment) | The name of the tfscaffold environment | `string` | n/a | yes |
15+
| <a name="input_function_s3_bucket"></a> [function\_s3\_bucket](#input\_function\_s3\_bucket) | Name of S3 bucket to upload lambda artefacts to | `string` | n/a | yes |
16+
| <a name="input_group"></a> [group](#input\_group) | The group variables are being inherited from (often synonmous with account short-name) | `string` | n/a | yes |
17+
| <a name="input_kms_key_arn"></a> [kms\_key\_arn](#input\_kms\_key\_arn) | KMS key ARN | `string` | n/a | yes |
18+
| <a name="input_log_retention_in_days"></a> [log\_retention\_in\_days](#input\_log\_retention\_in\_days) | The retention period in days for the Cloudwatch Logs events to be retained, default of 0 is indefinite | `number` | `0` | no |
19+
| <a name="input_project"></a> [project](#input\_project) | The name of the tfscaffold project | `string` | n/a | yes |
20+
| <a name="input_region"></a> [region](#input\_region) | The AWS Region | `string` | n/a | yes |
21+
| <a name="input_user_pool_id"></a> [user\_pool\_id](#input\_user\_pool\_id) | ID of the Cognito user pool the triggers should be applied to | `string` | n/a | yes |
22+
## Modules
23+
24+
| Name | Source | Version |
25+
|------|--------|---------|
26+
| <a name="module_pre_token_generation_lambda"></a> [pre\_token\_generation\_lambda](#module\_pre\_token\_generation\_lambda) | git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/lambda | v2.0.11 |
27+
## Outputs
28+
29+
| Name | Description |
30+
|------|-------------|
31+
| <a name="output_pre_token_generation_lambda_function_arn"></a> [pre\_token\_generation\_lambda\_function\_arn](#output\_pre\_token\_generation\_lambda\_function\_arn) | n/a |
32+
<!-- vale on -->
33+
<!-- markdownlint-enable -->
34+
<!-- END_TF_DOCS -->
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
locals {
2+
lambdas_dir = "../../../../lambdas"
3+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module "pre_token_generation_lambda" {
2+
source = "git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/lambda?ref=v2.0.11"
3+
4+
project = var.project
5+
environment = var.environment
6+
component = var.component
7+
aws_account_id = var.aws_account_id
8+
region = var.region
9+
10+
function_name = "pre-token-generation"
11+
description = "Pre token generation handler for Cognito user pool"
12+
13+
function_s3_bucket = var.function_s3_bucket
14+
function_code_base_path = local.lambdas_dir
15+
function_code_dir = "cognito-triggers/dist"
16+
function_module_name = "pre-token-generation"
17+
handler_function_name = "handler"
18+
19+
memory = 512
20+
timeout = 3
21+
runtime = "nodejs20.x"
22+
23+
kms_key_arn = var.kms_key_arn
24+
log_retention_in_days = var.log_retention_in_days
25+
26+
permission_statements = [{
27+
statement_id = "AllowCognitoInvoke"
28+
principal = "cognito-idp.amazonaws.com"
29+
action = "lambda:InvokeFunction"
30+
source_arn = "arn:aws:cognito-idp:${var.region}:${var.aws_account_id}:userpool/${var.user_pool_id}"
31+
source_account = var.aws_account_id
32+
}]
33+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "pre_token_generation_lambda_function_arn" {
2+
value = module.pre_token_generation_lambda.function_arn
3+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
##
2+
# Basic Required Variables for tfscaffold Components
3+
##
4+
5+
variable "project" {
6+
type = string
7+
description = "The name of the tfscaffold project"
8+
}
9+
10+
variable "environment" {
11+
type = string
12+
description = "The name of the tfscaffold environment"
13+
}
14+
15+
variable "aws_account_id" {
16+
type = string
17+
description = "The AWS Account ID (numeric)"
18+
}
19+
20+
variable "region" {
21+
type = string
22+
description = "The AWS Region"
23+
}
24+
25+
variable "group" {
26+
type = string
27+
description = "The group variables are being inherited from (often synonmous with account short-name)"
28+
}
29+
30+
31+
##
32+
# tfscaffold variables specific to this component
33+
##
34+
35+
variable "component" {
36+
type = string
37+
description = "The variable encapsulating the name of this component"
38+
default = "cog"
39+
}
40+
41+
##
42+
# Variables specific to this component
43+
##
44+
45+
variable "log_retention_in_days" {
46+
type = number
47+
description = "The retention period in days for the Cloudwatch Logs events to be retained, default of 0 is indefinite"
48+
default = 0
49+
}
50+
51+
variable "kms_key_arn" {
52+
type = string
53+
description = "KMS key ARN"
54+
}
55+
56+
variable "function_s3_bucket" {
57+
type = string
58+
description = "Name of S3 bucket to upload lambda artefacts to"
59+
}
60+
61+
variable "user_pool_id" {
62+
type = string
63+
description = "ID of the Cognito user pool the triggers should be applied to"
64+
}

lambdas/authorizer/src/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const generateMethodArn = (
2020
const generatePolicy = (
2121
Resource: string,
2222
Effect: 'Allow' | 'Deny',
23-
context?: { user: string }
23+
context?: { user: string; clientId?: string }
2424
) => ({
2525
principalId: 'api-caller',
2626
policyDocument: {
@@ -36,10 +36,8 @@ const generatePolicy = (
3636
context,
3737
});
3838

39-
export const handler: APIGatewayRequestAuthorizerHandler = async ({
40-
headers,
41-
requestContext,
42-
}) => {
39+
export const handler: APIGatewayRequestAuthorizerHandler = async (event) => {
40+
const { headers, requestContext } = event;
4341
const methodArn = generateMethodArn(requestContext);
4442

4543
if (!headers?.Authorization) {
@@ -69,6 +67,7 @@ export const handler: APIGatewayRequestAuthorizerHandler = async ({
6967
if (authResult.success) {
7068
return generatePolicy(methodArn, 'Allow', {
7169
user: authResult.subject,
70+
clientId: authResult.clientId,
7271
});
7372
}
7473

0 commit comments

Comments
 (0)