Skip to content

Commit a6884cd

Browse files
added permissons and given token id
1 parent 24050f2 commit a6884cd

4 files changed

Lines changed: 38 additions & 17 deletions

File tree

.github/workflows/cicd-4b-preprod-seed-users.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@ on:
44
push:
55
branches:
66
- ELI-417-preprod-db-seeding
7+
workflow_dispatch:
8+
inputs:
9+
environment:
10+
description: Target environment
11+
required: true
12+
type: choice
13+
options:
14+
- dev
715

816
jobs:
9-
seed:
17+
seed-dynamodb:
1018
runs-on: ubuntu-latest
1119
environment: "dev"
1220
permissions:
@@ -37,7 +45,7 @@ jobs:
3745

3846
- name: Run seed script
3947
run: |
40-
python .github/scripts/seed_dynamodb.py \
48+
python scripts/seed_users/seed_dynamodb.py \
4149
--table-name "${{ env.DYNAMODB_TABLE }}" \
4250
--region "${{ env.AWS_REGION }}" \
4351
--data-folder "${{ env.DATA_FOLDER }}"

infrastructure/stacks/iams-developer-roles/github_actions_policies.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,18 @@ resource "aws_iam_policy" "dynamodb_management" {
109109
}
110110
],
111111
# to create test users in preprod
112-
var.environment == "preprod" ? [
112+
var.environment == "dev" ? [
113113
{
114114
Effect = "Allow",
115115
Action = [
116116
"dynamodb:GetItem",
117117
"dynamodb:PutItem",
118118
"dynamodb:DeleteItem",
119+
"dynamodb:Scan",
120+
"dynamodb:BatchWriteItem"
119121
],
120122
Resource = [
121-
"arn:aws:dynamodb:*:${data.aws_caller_identity.current.account_id}:table/*eligibility-signposting-api-preprod-eligibility_datastore"
123+
"arn:aws:dynamodb:*:${data.aws_caller_identity.current.account_id}:table/*eligibility-signposting-api-${var.environment}-eligibility_datastore"
122124
]
123125
}
124126
] : []

infrastructure/stacks/iams-developer-roles/iams_permissions_boundary.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ data "aws_iam_policy_document" "permissions_boundary" {
4040
"dynamodb:GetItem",
4141
"dynamodb:PutItem",
4242
"dynamodb:DeleteItem",
43+
"dynamodb:Scan",
44+
"dynamodb:BatchWriteItem",
4345

4446
# EC2 - networking infrastructure
4547
"ec2:Describe*",

scripts/seed_users/seed_dynamodb.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,33 @@ def parse_args():
1414
return parser.parse_args()
1515

1616

17-
def clear_table(table):
18-
scan = table.scan(
19-
ProjectionExpression="#nhs, #type", ExpressionAttributeNames={"#nhs": "NHS_NUMBER", "#type": "ATTRIBUTE_TYPE"}
20-
)
17+
def get_keys_from_folder(data_folder):
18+
keys_to_delete = []
19+
json_files = glob.glob(os.path.join(data_folder, "*.json"))
20+
for file_path in json_files:
21+
with open(file_path) as f:
22+
payload = json.load(f)
23+
items = payload.get("data", [])
24+
for item in items:
25+
nhs_number = item.get("NHS_NUMBER")
26+
attr_type = item.get("ATTRIBUTE_TYPE")
27+
if nhs_number and attr_type:
28+
keys_to_delete.append({"NHS_NUMBER": nhs_number, "ATTRIBUTE_TYPE": attr_type})
29+
return keys_to_delete
30+
31+
32+
def delete_specific_items(table, keys):
2133
with table.batch_writer() as batch:
22-
for item in scan["Items"]:
23-
batch.delete_item(Key={"NHS_NUMBER": item["NHS_NUMBER"], "ATTRIBUTE_TYPE": item["ATTRIBUTE_TYPE"]})
34+
for key in keys:
35+
batch.delete_item(Key=key)
2436

2537

2638
def insert_data_from_folder(table, data_folder):
2739
json_files = glob.glob(os.path.join(data_folder, "*.json"))
2840
for file_path in json_files:
2941
with open(file_path) as f:
30-
try:
31-
payload = json.load(f)
32-
items = payload.get("data", [])
33-
except Exception as e:
34-
print(f"Skipping {file_path}: {e}")
35-
continue
42+
payload = json.load(f)
43+
items = payload.get("data", [])
3644

3745
with table.batch_writer() as batch:
3846
for item in items:
@@ -48,7 +56,8 @@ def main():
4856
dynamodb = boto3.resource("dynamodb", region_name=args.region)
4957
table = dynamodb.Table(args.table_name)
5058

51-
clear_table(table)
59+
keys_to_delete = get_keys_from_folder(args.data_folder)
60+
delete_specific_items(table, keys_to_delete)
5261
insert_data_from_folder(table, args.data_folder)
5362

5463

0 commit comments

Comments
 (0)