Skip to content

Commit a2571bb

Browse files
authored
Chore: [AEA-5803] - move secret creation into psu repo (#2406)
## Summary - 🤖 Operational or Infrastructure Change ### Details Rename secrets and bring them back from account resources One benefit of the secrets being in the same repo as the consuming code is that we don't need to feed the secret names into the SAM templates but can instead use outputs of the secret template.
1 parent 9ac3092 commit a2571bb

File tree

2 files changed

+146
-6
lines changed

2 files changed

+146
-6
lines changed

SAMtemplates/functions/main.yaml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,12 @@ Resources:
431431
LOG_LEVEL: !Ref LogLevel
432432
NHS_NOTIFY_PRESCRIPTIONS_SQS_QUEUE_URL: !Ref NHSNotifyPrescriptionsSQSQueueUrl
433433
TABLE_NAME: !Ref PrescriptionNotificationStatesTableName
434-
API_KEY_SECRET: secrets-PSU-Notify-API-Key
435-
PRIVATE_KEY_SECRET: secrets-PSU-Notify-PrivateKey
436-
KID_SECRET: secrets-PSU-Notify-Application-KID
434+
API_KEY_SECRET:
435+
Fn::ImportValue: !Sub ${StackName}-PSU-Notify-API-Key
436+
PRIVATE_KEY_SECRET:
437+
Fn::ImportValue: !Sub ${StackName}-PSU-Notify-PrivateKey
438+
KID_SECRET:
439+
Fn::ImportValue: !Sub ${StackName}-PSU-Notify-KID
437440
NHS_NOTIFY_ROUTING_ID_PARAM: !Ref NotifyRoutingPlanIDParam
438441
NOTIFY_API_BASE_URL_PARAM: !Ref NotifyAPIBaseURLParam
439442
MAKE_REAL_NOTIFY_REQUESTS_PARAM: !Ref EnableNotificationsExternalParam
@@ -483,6 +486,8 @@ Resources:
483486
- Fn::ImportValue: !Sub ${StackName}:tables:${PrescriptionNotificationStatesTableName}:TableWritePolicyArn
484487
- Fn::ImportValue: !Sub ${StackName}:tables:UsePrescriptionNotificationStatesKMSKeyPolicyArn
485488
- Fn::ImportValue: !Sub ${StackName}-GetNotificationsParameterPolicy
489+
- Fn::ImportValue: !Sub ${StackName}-GetPSUSecretPolicy
490+
- Fn::ImportValue: !Sub ${StackName}-UsePSUSecretsKMSKeyPolicyArn
486491

487492
NHSNotifyUpdateCallback:
488493
Type: AWS::Serverless::Function
@@ -495,8 +500,10 @@ Resources:
495500
Variables:
496501
LOG_LEVEL: !Ref LogLevel
497502
TABLE_NAME: !Ref PrescriptionNotificationStatesTableName
498-
APP_ID_SECRET: secrets-PSU-Notify-Application-ID
499-
API_KEY_SECRET: secrets-PSU-Notify-API-Key
503+
APP_ID_SECRET:
504+
Fn::ImportValue: !Sub ${StackName}-PSU-Notify-Application-ID
505+
API_KEY_SECRET:
506+
Fn::ImportValue: !Sub ${StackName}-PSU-Notify-API-Key
500507
Metadata:
501508
BuildMethod: esbuild
502509
guard:
@@ -527,6 +534,8 @@ Resources:
527534
- - Fn::ImportValue: !Sub ${StackName}:tables:${PrescriptionNotificationStatesTableName}:TableReadPolicyArn
528535
- Fn::ImportValue: !Sub ${StackName}:tables:${PrescriptionNotificationStatesTableName}:TableWritePolicyArn
529536
- Fn::ImportValue: !Sub ${StackName}:tables:UsePrescriptionNotificationStatesKMSKeyPolicyArn
537+
- Fn::ImportValue: !Sub ${StackName}-GetPSUSecretPolicy
538+
- Fn::ImportValue: !Sub ${StackName}-UsePSUSecretsKMSKeyPolicyArn
530539
LogRetentionInDays: !Ref LogRetentionInDays
531540
CloudWatchKMSKeyId: !ImportValue account-resources:CloudwatchLogsKmsKeyArn
532541
EnableSplunk: !Ref EnableSplunk

SAMtemplates/secrets/main.yaml

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,55 @@ Parameters:
66
Default: none
77

88
Resources:
9+
PSUSecretsKMSKey:
10+
Type: AWS::KMS::Key
11+
Properties:
12+
EnableKeyRotation: true
13+
KeyPolicy:
14+
Version: 2012-10-17
15+
Id: PSUSecretsKeyPolicy
16+
Statement:
17+
- Sid: EnableIAMUserPermissions
18+
Effect: Allow
19+
Principal:
20+
AWS: !Sub "arn:aws:iam::${AWS::AccountId}:root"
21+
Action: kms:*
22+
Resource: "*"
23+
- Sid: Enable read only decrypt
24+
Effect: Allow
25+
Principal:
26+
AWS: "*"
27+
Action:
28+
- kms:DescribeKey
29+
- kms:Decrypt
30+
Resource: "*"
31+
Condition:
32+
ArnLike:
33+
aws:PrincipalArn: !Sub "arn:aws:iam::${AWS::AccountId}:role/aws-reserved/sso.amazonaws.com/${AWS::Region}/AWSReservedSSO_ReadOnly*"
34+
35+
PSUSecretsKMSKeyAlias:
36+
Type: AWS::KMS::Alias
37+
Properties:
38+
AliasName: !Sub alias/${StackName}-PSUSecretsKMSKey
39+
TargetKeyId: !Ref PSUSecretsKMSKey
40+
41+
UsePSUSecretsKMSKeyPolicy:
42+
Type: AWS::IAM::ManagedPolicy
43+
Properties:
44+
ManagedPolicyName: !Sub ${StackName}-UsePSUSecretsKMSKey
45+
PolicyDocument:
46+
Version: "2012-10-17"
47+
Statement:
48+
- Sid: AllowKmsForSecretsEncryption
49+
Effect: Allow
50+
Action:
51+
- kms:DescribeKey
52+
- kms:GenerateDataKey*
53+
- kms:Encrypt
54+
- kms:ReEncrypt*
55+
- kms:Decrypt
56+
Resource: !GetAtt PSUSecretsKMSKey.Arn
57+
958
SQSSaltSecret:
1059
Type: AWS::SecretsManager::Secret
1160
Properties:
@@ -17,6 +66,34 @@ Resources:
1766
PasswordLength: 32
1867
ExcludePunctuation: true
1968

69+
PSUNotifyKIDSecret:
70+
Type: AWS::SecretsManager::Secret
71+
Properties:
72+
Name: !Sub ${StackName}-PSU-Notify-KID
73+
Description: The id of the key (KID) used to sign JWT to NHS Notify and sent in the header
74+
KmsKeyId: !Ref PSUSecretsKMSKey
75+
76+
PSUNotifyPrivateKeySecret:
77+
Type: AWS::SecretsManager::Secret
78+
Properties:
79+
Name: !Sub ${StackName}-PSU-Notify-PrivateKey
80+
Description: RSA private key (PEM) for signing JWT to NHS Notify
81+
KmsKeyId: !Ref PSUSecretsKMSKey
82+
83+
PSUNotifyApplicationIDSecret:
84+
Type: AWS::SecretsManager::Secret
85+
Properties:
86+
Name: !Sub ${StackName}-PSU-Notify-Application-ID
87+
Description: The application ID for the DoS application to use when sending notifications to NHS Notify (e.g. bd492cde-b67e-487b-97fd-44b7414c8e95)
88+
KmsKeyId: !Ref PSUSecretsKMSKey
89+
90+
PSUNotifyAPIKeySecret:
91+
Type: AWS::SecretsManager::Secret
92+
Properties:
93+
Name: !Sub ${StackName}-PSU-Notify-API-Key
94+
Description: API Key for NHS Notify
95+
KmsKeyId: !Ref PSUSecretsKMSKey
96+
2097
GetSQSSaltSecretPolicy:
2198
Type: AWS::IAM::ManagedPolicy
2299
Properties:
@@ -28,7 +105,25 @@ Resources:
28105
Action:
29106
- secretsmanager:GetSecretValue
30107
- secretsmanager:DescribeSecret
31-
Resource: !Ref SQSSaltSecret
108+
Resource:
109+
- !Ref SQSSaltSecret
110+
111+
GetPSUSecretPolicy:
112+
Type: AWS::IAM::ManagedPolicy
113+
Properties:
114+
Description: "Allows reading PSU secret parameters"
115+
PolicyDocument:
116+
Version: 2012-10-17
117+
Statement:
118+
- Effect: Allow
119+
Action:
120+
- secretsmanager:GetSecretValue
121+
- secretsmanager:DescribeSecret
122+
Resource:
123+
- !Ref PSUNotifyKIDSecret
124+
- !Ref PSUNotifyPrivateKeySecret
125+
- !Ref PSUNotifyApplicationIDSecret
126+
- !Ref PSUNotifyAPIKeySecret
32127

33128
Outputs:
34129
SQSSaltSecret:
@@ -37,8 +132,44 @@ Outputs:
37132
Export:
38133
Name: !Sub ${StackName}-SQSSaltSecret
39134

135+
PSUNotifyKIDSecret:
136+
Description: The name of the PSU Notify KID secret
137+
Value: !Ref PSUNotifyKIDSecret
138+
Export:
139+
Name: !Sub ${StackName}-PSU-Notify-KID
140+
141+
PSUNotifyPrivateKeySecret:
142+
Description: The name of the PSU Notify Private Key secret
143+
Value: !Ref PSUNotifyPrivateKeySecret
144+
Export:
145+
Name: !Sub ${StackName}-PSU-Notify-PrivateKey
146+
147+
PSUNotifyApplicationIDSecret:
148+
Description: The name of the PSU Notify Application ID secret
149+
Value: !Ref PSUNotifyApplicationIDSecret
150+
Export:
151+
Name: !Sub ${StackName}-PSU-Notify-Application-ID
152+
153+
PSUNotifyAPIKeySecret:
154+
Description: The name of the PSU Notify API Key secret
155+
Value: !Ref PSUNotifyAPIKeySecret
156+
Export:
157+
Name: !Sub ${StackName}-PSU-Notify-API-Key
158+
159+
GetPSUSecretPolicy:
160+
Description: ARN of policy granting permission to read secrets
161+
Value: !Ref GetPSUSecretPolicy
162+
Export:
163+
Name: !Sub ${StackName}-GetPSUSecretPolicy
164+
40165
GetSQSSaltSecretPolicy:
41166
Description: ARN of policy granting permission to read the SQS salt secret
42167
Value: !Ref GetSQSSaltSecretPolicy
43168
Export:
44169
Name: !Sub ${StackName}-GetSQSSaltSecretPolicy
170+
171+
UsePSUSecretsKMSKeyPolicyArn:
172+
Description: ARN of managed policy granting PSU secrets KMS usage
173+
Value: !Ref UsePSUSecretsKMSKeyPolicy
174+
Export:
175+
Name: !Sub ${StackName}-UsePSUSecretsKMSKeyPolicyArn

0 commit comments

Comments
 (0)