Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sqs): encryptionType is incorrect when encryptionMasterKey is provided #26886

Merged
merged 6 commits into from
Aug 25, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions packages/aws-cdk-lib/aws-sqs/lib/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CfnQueue } from './sqs.generated';
import { validateProps } from './validate-props';
import * as iam from '../../aws-iam';
import * as kms from '../../aws-kms';
import { Duration, RemovalPolicy, Stack, Token, ArnFormat } from '../../core';
import { Duration, RemovalPolicy, Stack, Token, ArnFormat, Annotations } from '../../core';

/**
* Properties for creating a new Queue
Expand Down Expand Up @@ -336,7 +336,7 @@ export class Queue extends QueueBase {
}
: undefined;

const { encryptionMasterKey, encryptionProps } = _determineEncryptionProps.call(this);
const { encryptionMasterKey, encryptionProps, encryptionType } = _determineEncryptionProps.call(this);

const fifoProps = this.determineFifoProps(props);
this.fifo = fifoProps.fifoQueue || false;
Expand All @@ -362,25 +362,37 @@ export class Queue extends QueueBase {
this.encryptionMasterKey = encryptionMasterKey;
this.queueUrl = queue.ref;
this.deadLetterQueue = props.deadLetterQueue;
this.encryptionType = props.encryption;
this.encryptionType = encryptionType;

function _determineEncryptionProps(this: Queue): { encryptionProps: EncryptionProps, encryptionMasterKey?: kms.IKey } {
function _determineEncryptionProps(this: Queue): {
encryptionProps: EncryptionProps,
encryptionMasterKey?: kms.IKey,
encryptionType: QueueEncryption | undefined
} {
let encryption = props.encryption;

if (encryption === QueueEncryption.SQS_MANAGED && props.encryptionMasterKey) {
throw new Error("'encryptionMasterKey' is not supported if encryption type 'SQS_MANAGED' is used");
}

if (encryption !== QueueEncryption.KMS && props.encryptionMasterKey) {
if (encryption !== undefined) {
Annotations.of(this).addWarningV2('@aws-cdk/aws-sqs:queueEncryptionChangedToKMS', [
`encryption: Automatically changed to QueueEncryption.KMS, was: QueueEncryption.${Object.keys(QueueEncryption)[Object.values(QueueEncryption).indexOf(encryption)]}`,
'When encryptionMasterKey is provided, always set `encryption: QueueEncryption.KMS`',
].join('\n'));
}

encryption = QueueEncryption.KMS; // KMS is implied by specifying an encryption key
}

if (!encryption) {
return { encryptionProps: {} };
return { encryptionProps: {}, encryptionType: encryption };
}

if (encryption === QueueEncryption.UNENCRYPTED) {
return {
encryptionType: encryption,
encryptionProps: {
sqsManagedSseEnabled: false,
},
Expand All @@ -389,6 +401,7 @@ export class Queue extends QueueBase {

if (encryption === QueueEncryption.KMS_MANAGED) {
return {
encryptionType: encryption,
encryptionProps: {
kmsMasterKeyId: 'alias/aws/sqs',
kmsDataKeyReusePeriodSeconds: props.dataKeyReuse && props.dataKeyReuse.toSeconds(),
Expand All @@ -402,6 +415,7 @@ export class Queue extends QueueBase {
});

return {
encryptionType: encryption,
encryptionMasterKey: masterKey,
encryptionProps: {
kmsMasterKeyId: masterKey.keyArn,
Expand All @@ -412,6 +426,7 @@ export class Queue extends QueueBase {

if (encryption === QueueEncryption.SQS_MANAGED) {
return {
encryptionType: encryption,
encryptionProps: {
sqsManagedSseEnabled: true,
},
Expand Down
Loading