Skip to content

Commit 2cfd52d

Browse files
authored
Remove GetQueueAttributes from error queue policy (#11)
* Remove unnecessary GetQueueAttrs from err queue policy; allow custom stmts * Terraform string escape rule warning - quotes in IAM/resource policies
1 parent dc24761 commit 2cfd52d

File tree

4 files changed

+81
-42
lines changed

4 files changed

+81
-42
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ reliable process, free of
1111
[race conditions](https://github.com/sqlxpert/stay-stopped-aws-rds-aurora#perspective)
1212
that might leave databases running without warning.
1313

14+
Step-Stay-Stopped resolves Cloud Efficiency Hub reports
15+
[CER-0293: Automatic Restart of Stopped Aurora Clusters Causing Unintended Compute Charges](https://hub.pointfive.co/inefficiencies/automatic-restart-of-stopped-aurora-clusters-causing-unintended-compute-charges)
16+
and
17+
[CER-0097: No Lifecycle Management for Temporarily Stopped RDS Instances](https://hub.pointfive.co/inefficiencies/no-lifecycle-management-for-temporarily-stopped-rds-instances).
18+
1419
Jump to:
1520
[Get Started](#get-started)
1621
•
@@ -78,7 +83,7 @@ or an
7883

7984
```terraform
8085
module "stay_stopped_rds" {
81-
source = "git::https://github.com/sqlxpert/step-stay-stopped-aws-rds-aurora.git//terraform?ref=v2.2.0"
86+
source = "git::https://github.com/sqlxpert/step-stay-stopped-aws-rds-aurora.git//terraform?ref=v2.3.0"
8287
# Reference a specific version from github.com/sqlxpert/step-stay-stopped-aws-rds-aurora/releases
8388
}
8489
```
@@ -144,7 +149,7 @@ account) pair. To deploy in multiple regions and/or multiple AWS accounts,
144149
145150
```terraform
146151
module "stay_stopped_rds_stackset" {
147-
source = "git::https://github.com/sqlxpert/step-stay-stopped-aws-rds-aurora.git//terraform-multi?ref=v2.2.0"
152+
source = "git::https://github.com/sqlxpert/step-stay-stopped-aws-rds-aurora.git//terraform-multi?ref=v2.3.0"
148153
# Reference a specific version from github.com/sqlxpert/step-stay-stopped-aws-rds-aurora/releases
149154
150155
stay_stopped_rds_stackset_regions = ["us-east-1", "us-west-2", ]
@@ -181,7 +186,7 @@ resemble:
181186
182187
```terraform
183188
module "stay_stopped_rds" {
184-
source = "git::https://github.com/sqlxpert/step-stay-stopped-aws-rds-aurora.git//terraform?ref=v2.2.0"
189+
source = "git::https://github.com/sqlxpert/step-stay-stopped-aws-rds-aurora.git//terraform?ref=v2.3.0"
185190
# Reference a specific version from github.com/sqlxpert/step-stay-stopped-aws-rds-aurora/releases
186191
187192
for_each = toset(["us-east-1", "us-west-2", ])
@@ -320,7 +325,7 @@ entirely at your own risk. You are encouraged to review the source code.
320325
stale events, if any.
321326

322327
- Readable Identity and Access Management policies, formatted as CloudFormation
323-
YAML rather than JSON (where permitted), and broken down into discrete
328+
YAML rather than JSON (where possible), and broken down into discrete
324329
statements by service, resource or principal.
325330

326331
### Your Security Steps

cloudformation/step_stay_stopped_aws_rds_aurora.yaml

Lines changed: 66 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ Parameters:
131131
primary key itself) must exist in every target region.
132132
Default: ""
133133

134+
ErrorQueueAdditionalPolicyStatements:
135+
Type: String
136+
Description: >-
137+
Additional statements for the error queue policy. The least-privilege
138+
default is the empty string. For each custom statement, including the
139+
first or only one, specify a comma and a JSON object. You could allow
140+
access from a central monitoring account, for example. See
141+
https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-creating-custom-policies.html
142+
Default: '' # Values might contain double quotation marks!
143+
134144
StepFnTaskTimeoutSeconds:
135145
Type: Number
136146
Description: >-
@@ -267,6 +277,7 @@ Metadata:
267277
- MessageRetentionPeriodSeconds
268278
- MaximumMessageSizeBytes
269279
- SqsKmsKey
280+
- ErrorQueueAdditionalPolicyStatements
270281
- Label:
271282
default: AWS Step Function to stop databases
272283
Parameters:
@@ -307,6 +318,8 @@ Metadata:
307318
default: Maximum bytes in a message
308319
SqsKmsKey:
309320
default: KMS encryption key
321+
ErrorQueueAdditionalPolicyStatements:
322+
default: Additional error queue policy statements
310323
StepFnTaskTimeoutSeconds:
311324
default: Maximum seconds for an AWS request
312325
StepFnWaitSeconds:
@@ -602,42 +615,59 @@ Resources:
602615
Type: AWS::SQS::QueuePolicy
603616
Properties:
604617
Queues: [ !Ref ErrorQueue ]
605-
PolicyDocument:
606-
Version: "2012-10-17"
607-
Statement:
608-
- Sid: RequireTls
609-
Effect: Deny
610-
Principal: "*"
611-
Action: sqs:*
612-
Resource: "*"
613-
Condition:
614-
Bool: { aws:SecureTransport: "false" }
615-
- Effect: Allow
616-
Principal: "*"
617-
Action: sqs:GetQueueAttributes
618-
Resource: "*"
619-
- Sid:
620-
Fn::If:
621-
- SqsKmsKeyCustom
622-
- SourceEventRulesNoteKeyPolicyNeedsEventBridgeSqsKmsEncrypt
623-
- SourceEventRules
624-
Effect: Allow
625-
Principal: { Service: events.amazonaws.com }
626-
Action: sqs:SendMessage
627-
Resource: "*"
628-
Condition:
629-
ArnEquals:
630-
"aws:SourceArn":
631-
- !GetAtt DbForcedStartToStepFnRule.Arn
632-
- Sid: ExclusiveSources
633-
Effect: Deny
634-
Principal: "*"
635-
Action: sqs:SendMessage
636-
Resource: "*"
637-
Condition:
638-
ArnNotEquals:
639-
"aws:SourceArn":
640-
- !GetAtt DbForcedStartToStepFnRule.Arn
618+
# Using YAML makes a policy easier to read, but we need to support
619+
# insertion of custom JSON from ErrorQueueAdditionalPolicyStatements .
620+
PolicyDocument: !Sub
621+
- >-
622+
{
623+
"Version": "2012-10-17",
624+
"Statement": [
625+
{
626+
"Sid": "RequireTls",
627+
"Effect": "Deny",
628+
"Principal": "*",
629+
"Action": "sqs:*",
630+
"Resource": "*",
631+
"Condition": {
632+
"Bool": {
633+
"aws:SecureTransport": "false"
634+
}
635+
}
636+
},
637+
{
638+
"Sid": "${SourceEventRulesSid}",
639+
"Effect": "Allow",
640+
"Principal": {
641+
"Service": "events.amazonaws.com"
642+
},
643+
"Action": "sqs:SendMessage",
644+
"Resource": "*",
645+
"Condition": {
646+
"ArnEquals": {
647+
"aws:SourceArn": "${DbForcedStartToStepFnRule.Arn}"
648+
}
649+
}
650+
},
651+
{
652+
"Sid": "ExclusiveSources",
653+
"Effect": "Deny",
654+
"Principal": "*",
655+
"Action": "sqs:SendMessage",
656+
"Resource": "*",
657+
"Condition": {
658+
"ArnNotEquals": {
659+
"aws:SourceArn": "${DbForcedStartToStepFnRule.Arn}"
660+
}
661+
}
662+
}
663+
${ErrorQueueAdditionalPolicyStatements}
664+
]
665+
}
666+
-
667+
SourceEventRulesSid: !If
668+
- SqsKmsKeyCustom
669+
- SourceEventRulesNoteKeyPolicyNeedsEventBridgeSqsKmsEncrypt
670+
- SourceEventRules
641671

642672
# https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/USER_Events.Messages.html#USER_Events.Messages.cluster
643673
# - RDS-EVENT-0153 "DB cluster is being started due to it exceeding the

terraform-multi/variables.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ variable "stay_stopped_rds_stackset_params" {
5858
SqsKmsKey = optional(string, "")
5959
CloudWatchLogsKmsKey = optional(string, "")
6060

61+
ErrorQueueAdditionalPolicyStatements = optional(string, "")
62+
6163
StepFnTaskTimeoutSeconds = optional(number, 30)
6264
StepFnWaitSeconds = optional(number, 540)
6365
StepFnTimeoutSeconds = optional(number, 86400)
@@ -79,7 +81,7 @@ variable "stay_stopped_rds_stackset_params" {
7981
# aws_cloudformation_stack_set.lifecycle.ignore_changes
8082
})
8183

82-
description = "Step Stay-Stopped CloudFormation StackSet parameter map. Keys, all optional, are parameter names from cloudformation/step_stay_stopped_aws_rds_aurora.yaml ; parameters are described there. CloudFormation and Terraform data types match, except for Boolean parameters. Terraform converts bool values to CloudFormation String values automatically. In the StackSet, Test is always ignored and set to false , to prevent unintended use in production."
84+
description = "Step Stay-Stopped CloudFormation StackSet parameter map. Keys, all optional, are parameter names from cloudformation/step_stay_stopped_aws_rds_aurora.yaml ; parameters are described there. CloudFormation and Terraform data types match, except for Boolean parameters. Terraform converts bool values to CloudFormation String values automatically. In the StackSet, Test is always ignored and set to false , to prevent unintended use in production. Follow Terraform string escape rules for double quotation marks, etc. inside ErrorQueueAdditionalPolicyStatements ."
8385

8486
default = {}
8587
}

terraform/variables.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ variable "stay_stopped_rds_params" {
3030
SqsKmsKey = optional(string, "")
3131
CloudWatchLogsKmsKey = optional(string, "")
3232

33+
ErrorQueueAdditionalPolicyStatements = optional(string, "")
34+
3335
StepFnTaskTimeoutSeconds = optional(number, 30)
3436
StepFnWaitSeconds = optional(number, 540)
3537
StepFnTimeoutSeconds = optional(number, 86400)
@@ -43,7 +45,7 @@ variable "stay_stopped_rds_params" {
4345
# Repeat defaults from cloudformation/step_stay_stopped_aws_rds_aurora.yaml
4446
})
4547

46-
description = "Step Stay-Stopped CloudFormation stack parameter map. Keys, all optional, are parameter names from cloudformation/step_stay_stopped_aws_rds_aurora.yaml ; parameters are described there. CloudFormation and Terraform data types match, except for Boolean parameters. Terraform converts bool values to CloudFormation String values automatically. Specifying a value other than the empty string for StepFnRoleAttachLocalPolicyName , StepFnKmsKey , SqsKmsKey or CloudWatchLogsKmsKey causes Terraform to look up the resource, which must exist."
48+
description = "Step Stay-Stopped CloudFormation stack parameter map. Keys, all optional, are parameter names from cloudformation/step_stay_stopped_aws_rds_aurora.yaml ; parameters are described there. CloudFormation and Terraform data types match, except for Boolean parameters. Terraform converts bool values to CloudFormation String values automatically. Specifying a value other than the empty string for StepFnRoleAttachLocalPolicyName , StepFnKmsKey , SqsKmsKey or CloudWatchLogsKmsKey causes Terraform to look up the resource, which must exist. Follow Terraform string escape rules for double quotation marks, etc. inside ErrorQueueAdditionalPolicyStatements ."
4749

4850
default = {}
4951
}

0 commit comments

Comments
 (0)