Skip to content

Commit f81f393

Browse files
authored
Merge pull request #72 from ONS-Innovation/credential-refactor-and-key-rotation
KEH 1706 & 1708: Credential Refactor and Key Rotation
2 parents 15a05eb + 6181679 commit f81f393

File tree

8 files changed

+274
-128
lines changed

8 files changed

+274
-128
lines changed

.checkov.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,12 @@ skip-check:
2525
# It is not needed here since our Lambda functions use container
2626
# images over uploading .zip files for layers.
2727
- CKV_AWS_272
28+
29+
# AWS-managed key encryption is sufficient and CMK not required for this service
30+
- CKV_AWS_149
31+
32+
# IAM user required for local development
33+
- CKV_AWS_273
34+
35+
# Key rotation is already provisioned by external module
36+
- CKV2_AWS_57

.trivyignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,11 @@ AVD-AWS-0017
99
AVD-AWS-0104
1010

1111
# Ignore wildcarded resource in IAM policy
12-
AVD-AWS-0057
12+
AVD-AWS-0057
13+
14+
# MFA enforcement on IAM group is not applicable as group is used for
15+
# programmatic access via access keys, not console login
16+
AVD-AWS-0123
17+
18+
# AWS-managed key encryption is sufficient and CMK not required
19+
AVD-AWS-0098

docs/technical_documentation/configuration.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The `config.json` file contains the following:
1111
"features": {
1212
"show_log_locally": false,
1313
"write_data_locally": false
14-
},
14+
}
1515
}
1616
```
1717

@@ -40,7 +40,7 @@ When testing locally, you might set the `config.json` file as follows:
4040
"features": {
4141
"show_log_locally": true,
4242
"write_data_locally": true
43-
},
43+
}
4444
}
4545
```
4646

@@ -53,7 +53,7 @@ When deploying to AWS, the `config.json` file should be set as follows:
5353
"features": {
5454
"show_log_locally": false,
5555
"write_data_locally": false
56-
},
56+
}
5757
}
5858
```
5959

kics.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ exclude-queries:
1010

1111
# Sensitive Port Exposed to Private Network (This is fine due to VPC)
1212
- 92fe237e-074c-4262-81a4-2077acb928c1
13+
14+
# AWS-managed key encryption is sufficient and CMK not required for this service
15+
- a2f548f2-188c-4fff-b172-e9a6acb216bd
16+
17+
# Prevent user group no membership false positive
18+
- fc101ca7-c9dd-4198-a1eb-0fbe92e80044

poetry.lock

Lines changed: 176 additions & 122 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ github-api-toolkit = {git = "https://github.com/ONS-Innovation/github-api-packag
1313
requests = "^2.32.3"
1414
botocore = "^1.40.2"
1515
certifi = "^2025.8.3"
16-
cffi = "^1.17.1"
16+
cffi = ">=2.0.0"
1717
charset-normalizer = "^3.4.2"
18-
cryptography = "^45.0.5"
18+
cryptography = "^46.0.5"
1919
idna = "^3.10"
2020
jmespath = "^1.0.1"
2121
pycparser = "^2.22"

terraform/main.tf

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,60 @@ resource "aws_iam_role_policy_attachment" "eventbridge_policy" {
137137
resource "aws_cloudwatch_log_group" "loggroup" {
138138
name = "/aws/lambda/${aws_lambda_function.lambda_function.function_name}"
139139
retention_in_days = var.log_retention_days
140+
}
141+
142+
# IAM User Group
143+
resource "aws_iam_group" "group" {
144+
name = "${var.env_name}-${var.lambda_name}-user-group"
145+
path = "/"
146+
}
147+
148+
resource "aws_iam_group_policy_attachment" "group_vpc_permissions_attachment" {
149+
group = aws_iam_group.group.name
150+
policy_arn = aws_iam_policy.vpc_permissions.arn
151+
}
152+
153+
resource "aws_iam_group_policy_attachment" "group_lambda_logging_attachment" {
154+
group = aws_iam_group.group.name
155+
policy_arn = aws_iam_policy.lambda_logging.arn
156+
}
157+
158+
resource "aws_iam_group_policy_attachment" "group_lambda_s3_policy_attachment" {
159+
group = aws_iam_group.group.name
160+
policy_arn = aws_iam_policy.lambda_s3_policy.arn
161+
}
162+
163+
resource "aws_iam_group_policy_attachment" "group_lambda_secret_manager_policy_attachment" {
164+
group = aws_iam_group.group.name
165+
policy_arn = aws_iam_policy.lambda_secret_manager_policy.arn
166+
}
167+
168+
resource "aws_iam_group_policy_attachment" "group_lambda_eventbridge_policy_attachment" {
169+
group = aws_iam_group.group.name
170+
policy_arn = aws_iam_policy.lambda_eventbridge_policy.arn
171+
}
172+
173+
# IAM User
174+
resource "aws_iam_user" "user" {
175+
name = "${var.env_name}-${var.lambda_name}"
176+
path = "/"
177+
}
178+
179+
# Assign IAM User to group
180+
resource "aws_iam_user_group_membership" "user_group_attach" {
181+
user = aws_iam_user.user.name
182+
183+
groups = [
184+
aws_iam_group.group.name
185+
]
186+
}
187+
188+
# IAM Key Rotation Module
189+
module "iam_key_rotation" {
190+
source = "git::https://github.com/ONS-Innovation/keh-aws-iam-key-rotation.git?ref=v0.1.0"
191+
192+
iam_username = aws_iam_user.user.name
193+
access_key_secret_arn = aws_secretsmanager_secret.access_key.arn
194+
secret_key_secret_arn = aws_secretsmanager_secret.secret_key.arn
195+
rotation_in_days = 90
140196
}

terraform/secrets.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Secrets for rotated IAM user access keys
2+
resource "aws_secretsmanager_secret" "access_key" {
3+
name = "${var.env_name}-${var.lambda_name}-access-key"
4+
description = "Access Key ID for copilot usage lambda IAM user"
5+
recovery_window_in_days = 0 // Secret will be deleted immediately
6+
force_overwrite_replica_secret = true // Allow overwriting the secret in case of changes
7+
}
8+
9+
resource "aws_secretsmanager_secret" "secret_key" {
10+
name = "${var.env_name}-${var.lambda_name}-secret-key"
11+
description = "Secret Access Key for copilot usage lambda IAM user"
12+
recovery_window_in_days = 0 // Secret will be deleted immediately
13+
force_overwrite_replica_secret = true // Allow overwriting the secret in case of changes
14+
}

0 commit comments

Comments
 (0)