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: Make sqs.Queue.encryptionMasterKey readonly #650

Merged
merged 1 commit into from
Sep 2, 2018
Merged
Changes from all commits
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
85 changes: 47 additions & 38 deletions packages/@aws-cdk/aws-sqs/lib/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export class Queue extends QueueRef {
/**
* If this queue is encrypted, this is the KMS key.
*/
public encryptionMasterKey?: kms.EncryptionKeyRef;
public readonly encryptionMasterKey?: kms.EncryptionKeyRef;

protected readonly autoCreatePolicy = true;

Expand All @@ -214,20 +214,65 @@ export class Queue extends QueueRef {
}
: undefined;

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

const queue = new cloudformation.QueueResource(this, 'Resource', {
queueName: props.queueName,
...this.determineFifoProps(props),
...this.determineEncryptionProps(props),
...encryptionProps,
redrivePolicy,
delaySeconds: props.deliveryDelaySec,
maximumMessageSize: props.maxMessageSizeBytes,
messageRetentionPeriod: props.retentionPeriodSec,
receiveMessageWaitTimeSeconds: props.receiveMessageWaitTimeSec,
visibilityTimeout: props.visibilityTimeoutSec,
});
this.encryptionMasterKey = encryptionMasterKey;
this.queueArn = queue.queueArn;
this.queueName = queue.queueName;
this.queueUrl = queue.ref;

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

if (encryption !== QueueEncryption.Kms && props.encryptionMasterKey) {
encryption = QueueEncryption.Kms; // KMS is implied by specifying an encryption key
}

if (encryption === QueueEncryption.Unencrypted) {
return { encryptionProps: {} };
}

if (encryption === QueueEncryption.KmsManaged) {
const masterKey = kms.EncryptionKey.import(this, 'Key', {
keyArn: new kms.KeyArn('alias/aws/sqs')
});

return {
encryptionMasterKey: masterKey,
encryptionProps: {
kmsMasterKeyId: 'alias/aws/sqs',
kmsDataKeyReusePeriodSeconds: props.dataKeyReuseSec
}
};
}

if (encryption === QueueEncryption.Kms) {
const masterKey = props.encryptionMasterKey || new kms.EncryptionKey(this, 'Key', {
description: `Created by ${this.path}`
});

return {
encryptionMasterKey: masterKey,
encryptionProps: {
kmsMasterKeyId: masterKey.keyArn,
kmsDataKeyReusePeriodSeconds: props.dataKeyReuseSec
}
};
}

throw new Error(`Unexpected 'encryptionType': ${encryption}`);
}
}

/**
Expand Down Expand Up @@ -258,42 +303,6 @@ export class Queue extends QueueRef {
fifoQueue,
};
}

private determineEncryptionProps(props: QueueProps): EncryptionProps {
let encryption = props.encryption || QueueEncryption.Unencrypted;

if (encryption !== QueueEncryption.Kms && props.encryptionMasterKey) {
encryption = QueueEncryption.Kms; // KMS is implied by specifying an encryption key
}

if (encryption === QueueEncryption.Unencrypted) {
return {};
}

if (encryption === QueueEncryption.KmsManaged) {
this.encryptionMasterKey = kms.EncryptionKey.import(this, 'Key', {
keyArn: new kms.KeyArn('alias/aws/sqs')
});

return {
kmsMasterKeyId: 'alias/aws/sqs',
kmsDataKeyReusePeriodSeconds: props.dataKeyReuseSec
};
}

if (encryption === QueueEncryption.Kms) {
this.encryptionMasterKey = props.encryptionMasterKey || new kms.EncryptionKey(this, 'Key', {
description: `Created by ${this.path}`
});

return {
kmsMasterKeyId: this.encryptionMasterKey.keyArn,
kmsDataKeyReusePeriodSeconds: props.dataKeyReuseSec
};
}

throw new Error(`Unexpected 'encryptionType': ${encryption}`);
}
}

interface FifoProps {
Expand Down