|
1 | 1 | import { Template } from '@aws-cdk/assertions';
|
2 | 2 | import * as events from '@aws-cdk/aws-events';
|
| 3 | +import * as kms from '@aws-cdk/aws-kms'; |
3 | 4 | import * as sqs from '@aws-cdk/aws-sqs';
|
4 | 5 | import { Duration, Stack } from '@aws-cdk/core';
|
| 6 | +import * as cxapi from '@aws-cdk/cx-api'; |
5 | 7 | import * as targets from '../../lib';
|
6 | 8 |
|
7 | 9 | test('sqs queue as an event rule target', () => {
|
@@ -141,6 +143,124 @@ test('multiple uses of a queue as a target results in multi policy statement bec
|
141 | 143 | });
|
142 | 144 | });
|
143 | 145 |
|
| 146 | +test('Encrypted queues result in a policy statement with aws:sourceAccount condition when the feature flag is on', () => { |
| 147 | + // GIVEN |
| 148 | + const stack = new Stack(); |
| 149 | + stack.node.setContext(cxapi.EVENTS_TARGET_QUEUE_SAME_ACCOUNT, true); |
| 150 | + const queue = new sqs.Queue(stack, 'MyQueue', { |
| 151 | + encryptionMasterKey: kms.Key.fromKeyArn(stack, 'key', 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'), |
| 152 | + }); |
| 153 | + |
| 154 | + const rule = new events.Rule(stack, 'MyRule', { |
| 155 | + schedule: events.Schedule.rate(Duration.hours(1)), |
| 156 | + }); |
| 157 | + |
| 158 | + // WHEN |
| 159 | + rule.addTarget(new targets.SqsQueue(queue)); |
| 160 | + |
| 161 | + // THEN |
| 162 | + Template.fromStack(stack).hasResourceProperties('AWS::SQS::QueuePolicy', { |
| 163 | + PolicyDocument: { |
| 164 | + Statement: [ |
| 165 | + { |
| 166 | + Action: [ |
| 167 | + 'sqs:SendMessage', |
| 168 | + 'sqs:GetQueueAttributes', |
| 169 | + 'sqs:GetQueueUrl', |
| 170 | + ], |
| 171 | + Condition: { |
| 172 | + StringEquals: { |
| 173 | + 'aws:SourceAccount': { Ref: 'AWS::AccountId' }, |
| 174 | + }, |
| 175 | + }, |
| 176 | + Effect: 'Allow', |
| 177 | + Principal: { Service: 'events.amazonaws.com' }, |
| 178 | + Resource: { |
| 179 | + 'Fn::GetAtt': [ |
| 180 | + 'MyQueueE6CA6235', |
| 181 | + 'Arn', |
| 182 | + ], |
| 183 | + }, |
| 184 | + }, |
| 185 | + ], |
| 186 | + Version: '2012-10-17', |
| 187 | + }, |
| 188 | + Queues: [{ Ref: 'MyQueueE6CA6235' }], |
| 189 | + }); |
| 190 | + |
| 191 | + Template.fromStack(stack).hasResourceProperties('AWS::Events::Rule', { |
| 192 | + ScheduleExpression: 'rate(1 hour)', |
| 193 | + State: 'ENABLED', |
| 194 | + Targets: [ |
| 195 | + { |
| 196 | + Arn: { |
| 197 | + 'Fn::GetAtt': [ |
| 198 | + 'MyQueueE6CA6235', |
| 199 | + 'Arn', |
| 200 | + ], |
| 201 | + }, |
| 202 | + Id: 'Target0', |
| 203 | + }, |
| 204 | + ], |
| 205 | + }); |
| 206 | +}); |
| 207 | + |
| 208 | +test('Encrypted queues result in a permissive policy statement when the feature flag is off', () => { |
| 209 | + // GIVEN |
| 210 | + const stack = new Stack(); |
| 211 | + const queue = new sqs.Queue(stack, 'MyQueue', { |
| 212 | + encryptionMasterKey: kms.Key.fromKeyArn(stack, 'key', 'arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab'), |
| 213 | + }); |
| 214 | + |
| 215 | + const rule = new events.Rule(stack, 'MyRule', { |
| 216 | + schedule: events.Schedule.rate(Duration.hours(1)), |
| 217 | + }); |
| 218 | + |
| 219 | + // WHEN |
| 220 | + rule.addTarget(new targets.SqsQueue(queue)); |
| 221 | + |
| 222 | + // THEN |
| 223 | + Template.fromStack(stack).hasResourceProperties('AWS::SQS::QueuePolicy', { |
| 224 | + PolicyDocument: { |
| 225 | + Statement: [ |
| 226 | + { |
| 227 | + Action: [ |
| 228 | + 'sqs:SendMessage', |
| 229 | + 'sqs:GetQueueAttributes', |
| 230 | + 'sqs:GetQueueUrl', |
| 231 | + ], |
| 232 | + Effect: 'Allow', |
| 233 | + Principal: { Service: 'events.amazonaws.com' }, |
| 234 | + Resource: { |
| 235 | + 'Fn::GetAtt': [ |
| 236 | + 'MyQueueE6CA6235', |
| 237 | + 'Arn', |
| 238 | + ], |
| 239 | + }, |
| 240 | + }, |
| 241 | + ], |
| 242 | + Version: '2012-10-17', |
| 243 | + }, |
| 244 | + Queues: [{ Ref: 'MyQueueE6CA6235' }], |
| 245 | + }); |
| 246 | + |
| 247 | + Template.fromStack(stack).hasResourceProperties('AWS::Events::Rule', { |
| 248 | + ScheduleExpression: 'rate(1 hour)', |
| 249 | + State: 'ENABLED', |
| 250 | + Targets: [ |
| 251 | + { |
| 252 | + Arn: { |
| 253 | + 'Fn::GetAtt': [ |
| 254 | + 'MyQueueE6CA6235', |
| 255 | + 'Arn', |
| 256 | + ], |
| 257 | + }, |
| 258 | + Id: 'Target0', |
| 259 | + }, |
| 260 | + ], |
| 261 | + }); |
| 262 | +}); |
| 263 | + |
144 | 264 | test('fail if messageGroupId is specified on non-fifo queues', () => {
|
145 | 265 | const stack = new Stack();
|
146 | 266 | const queue = new sqs.Queue(stack, 'MyQueue');
|
|
0 commit comments