Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.

Commit de75be5

Browse files
GWPGeorge Price
andauthored
Encrypt sqs sns (#90)
* removed generated files from tracking, updated gitignore * Revert "removed generated files from tracking, updated gitignore" This reverts commit a4620a4. * added encryption to SNS topic and JobResults Queue Co-authored-by: George Price <gwprice@amazon.com>
1 parent a3e2c4b commit de75be5

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

deployment/document-understanding-solution.template

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,10 @@ Resources:
686686
"iam:Get*",
687687
"iam:AttachRolePolicy",
688688
"iam:PutRolePolicy",
689-
"iam:CreateServiceLinkedRole"
689+
"iam:DeleteRolePolicy",
690+
"iam:DetachRolePolicy",
691+
"iam:CreateServiceLinkedRole",
692+
"iam:DeleteServiceLinkedRole"
690693
]
691694
},
692695
{

source/lib/cdk-textract-stack.ts

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import { QueueEncryption } from "@aws-cdk/aws-sqs";
4646
import { LogGroup } from "@aws-cdk/aws-logs";
4747
import { LogGroupLogDestination } from "@aws-cdk/aws-apigateway";
4848

49-
const API_CONCURRENT_REQUESTS = 20; //approximate number of 1-2 page documents to be processed parallelly
49+
const API_CONCURRENT_REQUESTS = 20; //approximate number of 1-2 page documents to be processed in parallell
5050

5151
export interface TextractStackProps {
5252
email: string;
@@ -171,12 +171,14 @@ export class CdkTextractStack extends cdk.Stack {
171171
behaviors: [{ isDefaultBehavior: true }],
172172
},
173173
],
174-
errorConfigurations: [{
175-
errorCode: 404,
176-
responseCode: 200,
177-
errorCachingMinTtl: 5,
178-
responsePagePath: '/index.html'
179-
}],
174+
errorConfigurations: [
175+
{
176+
errorCode: 404,
177+
responseCode: 200,
178+
errorCachingMinTtl: 5,
179+
responsePagePath: "/index.html",
180+
},
181+
],
180182
priceClass: PriceClass.PRICE_CLASS_100,
181183
httpVersion: HttpVersion.HTTP2,
182184
enableIpV6: true,
@@ -324,12 +326,39 @@ export class CdkTextractStack extends cdk.Stack {
324326
elasticSearch.node.addDependency(serviceLinkedRole);
325327
}
326328

329+
const jobResultsKey = new kms.Key(
330+
this,
331+
this.resourceName("JobResultsKey"),
332+
{
333+
enableKeyRotation: true,
334+
enabled: true,
335+
trustAccountIdentities: true,
336+
policy: new iam.PolicyDocument({
337+
assignSids: true,
338+
statements: [
339+
new iam.PolicyStatement({
340+
actions: ["kms:GenerateDataKey*", "kms:Decrypt"],
341+
resources: ["*"], // Resource level permissions are not necessary in this policy statement, as it is automatically restricted to this key
342+
effect: iam.Effect.ALLOW,
343+
principals: [
344+
new iam.ServicePrincipal("sns.amazonaws.com"),
345+
new iam.ServicePrincipal("lambda.amazonaws.com"),
346+
new iam.ServicePrincipal("textract.amazonaws.com"),
347+
new iam.ServicePrincipal("sqs.amazonaws.com"),
348+
],
349+
}),
350+
],
351+
}),
352+
}
353+
);
354+
327355
// SNS Topic
328356
const jobCompletionTopic = new sns.Topic(
329357
this,
330-
this.resourceName("JobCompletion"),
358+
this.resourceName("JobCompletionTopic"),
331359
{
332360
displayName: "Job completion topic",
361+
masterKey: jobResultsKey,
333362
}
334363
);
335364

@@ -349,6 +378,13 @@ export class CdkTextractStack extends cdk.Stack {
349378
resources: [jobCompletionTopic.topicArn],
350379
})
351380
);
381+
textractServiceRole.addToPolicy(
382+
new iam.PolicyStatement({
383+
effect: iam.Effect.ALLOW,
384+
actions: ["kms:Decrypt", "kms:GenerateDataKey*"],
385+
resources: [jobResultsKey.keyArn],
386+
})
387+
);
352388

353389
// DynamoDB tables
354390
const outputTable = new ddb.Table(this, this.resourceName("OutputTable"), {
@@ -440,6 +476,7 @@ export class CdkTextractStack extends cdk.Stack {
440476
{
441477
visibilityTimeout: cdk.Duration.seconds(900),
442478
retentionPeriod: cdk.Duration.seconds(1209600),
479+
encryption: QueueEncryption.KMS_MANAGED,
443480
}
444481
);
445482

@@ -449,12 +486,16 @@ export class CdkTextractStack extends cdk.Stack {
449486
{
450487
visibilityTimeout: cdk.Duration.seconds(900),
451488
retentionPeriod: cdk.Duration.seconds(1209600),
489+
encryption: QueueEncryption.KMS,
490+
encryptionMasterKey: jobResultsKey,
491+
dataKeyReuse: cdk.Duration.seconds(86400),
452492
deadLetterQueue: {
453493
maxReceiveCount: 3,
454494
queue: jobResultsDLQueue,
455495
},
456496
}
457497
);
498+
458499
// trigger
459500
jobCompletionTopic.addSubscription(
460501
new snsSubscriptions.SqsSubscription(jobResultsQueue)
@@ -874,6 +915,7 @@ export class CdkTextractStack extends cdk.Stack {
874915
jobResultProcessor.addLayers(textractorLayer);
875916
jobResultProcessor.addLayers(boto3Layer);
876917
jobResultProcessor.addLayers(elasticSearchLayer);
918+
jobResultsKey.grantEncryptDecrypt(jobResultProcessor);
877919

878920
// Triggers
879921
jobResultProcessor.addEventSource(

0 commit comments

Comments
 (0)