This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathsubscriptions.ts
157 lines (143 loc) · 5.61 KB
/
subscriptions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { Duration } from 'aws-cdk-lib';
import {
AccountRootPrincipal,
Effect,
PolicyDocument,
PolicyStatement,
Role,
ServicePrincipal,
StarPrincipal,
} from 'aws-cdk-lib/aws-iam';
import { Key } from 'aws-cdk-lib/aws-kms';
import { CfnSubscription, CfnTopic } from 'aws-cdk-lib/aws-sns';
import { Queue } from 'aws-cdk-lib/aws-sqs';
import { NagSuppressions } from 'cdk-nag';
import { Construct } from 'constructs';
export default class SubscriptionsResources {
subscriptionsKey: Key;
restHookDLQ: Queue;
restHookQueue: Queue;
subscriptionsTopic: CfnTopic;
restHookSubscription: CfnSubscription;
restHookLambdaRole: Role;
constructor(scope: Construct, region: string, partition: string, stage: string) {
this.subscriptionsKey = new Key(scope, 'subscriptionsKey', {
description: 'Encryption key for rest hook queue that can be used by SNS',
enableKeyRotation: true,
policy: new PolicyDocument({
statements: [
new PolicyStatement({
effect: Effect.ALLOW,
principals: [new ServicePrincipal('sns.amazonaws.com')],
actions: ['kms:Decrypt', 'kms:GenerateDataKey*'],
resources: ['*'],
}),
new PolicyStatement({
sid: 'Allow administration of the key',
effect: Effect.ALLOW,
principals: [new AccountRootPrincipal()],
actions: ['kms:*'],
resources: ['*'],
}),
],
}),
});
this.restHookDLQ = new Queue(scope, 'restHookDLQ', {
encryptionMasterKey: this.subscriptionsKey,
retentionPeriod: Duration.days(14), // 14 days in seconds
});
NagSuppressions.addResourceSuppressions(this.restHookDLQ, [
{
id: 'AwsSolutions-SQS3',
reason: 'This is a DLQ.',
},
]);
this.restHookQueue = new Queue(scope, 'restHookQueue', {
encryptionMasterKey: this.subscriptionsKey,
deadLetterQueue: {
queue: this.restHookDLQ,
maxReceiveCount: 2,
},
});
this.subscriptionsTopic = new CfnTopic(scope, 'subscriptionsTopic', {
topicName: `SubscriptionsTopic-${stage}`,
kmsMasterKeyId: this.subscriptionsKey.keyId,
});
this.restHookDLQ.addToResourcePolicy(
new PolicyStatement({
effect: Effect.DENY,
actions: ['sqs:*'],
resources: [this.restHookDLQ.queueArn],
principals: [new StarPrincipal()],
conditions: {
Bool: {
'aws:SecureTransport': 'false',
},
},
}),
);
this.restHookQueue.addToResourcePolicy(
new PolicyStatement({
effect: Effect.DENY,
actions: ['sqs:*'],
resources: [this.restHookQueue.queueArn],
principals: [new StarPrincipal()],
conditions: {
Bool: {
'aws:SecureTransport': false,
},
},
}),
);
this.restHookQueue.addToResourcePolicy(
new PolicyStatement({
effect: Effect.ALLOW,
actions: ['sqs:SendMessage'],
resources: [this.restHookQueue.queueArn],
principals: [new ServicePrincipal('sns.amazonaws.com')],
conditions: {
ArnEquals: {
'aws:SourceArn': this.subscriptionsTopic.ref,
},
},
}),
);
this.restHookSubscription = new CfnSubscription(scope, 'restHookSubscription', {
topicArn: this.subscriptionsTopic.ref,
endpoint: this.restHookQueue.queueArn,
protocol: 'sqs',
filterPolicy: {
channelType: ['rest-hook'],
},
});
this.restHookLambdaRole = new Role(scope, 'restHookLambdaRole', {
assumedBy: new ServicePrincipal('lambda.amazonaws.com'),
inlinePolicies: {
restHookLambdaPolicy: new PolicyDocument({
statements: [
new PolicyStatement({
effect: Effect.ALLOW,
actions: ['logs:CreateLogStream', 'logs:CreateLogGroup', 'logs:PutLogEvents'],
resources: [`arn:${partition}:logs:${region}:*:*`],
}),
new PolicyStatement({
effect: Effect.ALLOW,
actions: ['xray:PutTraceSegments', 'xray:PutTelemetryRecords'],
resources: ['*'],
}),
new PolicyStatement({
effect: Effect.ALLOW,
actions: ['kms:Decrypt'],
resources: [this.subscriptionsKey.keyArn],
}),
new PolicyStatement({
effect: Effect.ALLOW,
actions: ['sqs:DeleteMessage', 'sqs:ReceiveMessage', 'sqs:GetQueueAttributes'],
resources: [this.restHookQueue.queueArn],
}),
],
}),
},
});
}
}