|
1 | 1 | import { EOL } from 'os';
|
2 | 2 | import * as events from '@aws-cdk/aws-events';
|
3 | 3 | import * as iam from '@aws-cdk/aws-iam';
|
| 4 | +import * as kms from '@aws-cdk/aws-kms'; |
4 | 5 | import { ArnFormat, IResource, Lazy, RemovalPolicy, Resource, Stack, Token } from '@aws-cdk/core';
|
5 | 6 | import { IConstruct, Construct } from 'constructs';
|
6 | 7 | import { CfnRepository } from './ecr.generated';
|
@@ -327,6 +328,27 @@ export interface RepositoryProps {
|
327 | 328 | */
|
328 | 329 | readonly repositoryName?: string;
|
329 | 330 |
|
| 331 | + /** |
| 332 | + * The kind of server-side encryption to apply to this repository. |
| 333 | + * |
| 334 | + * If you choose KMS, you can specify a KMS key via `encryptionKey`. If |
| 335 | + * encryptionKey is not specified, an AWS managed KMS key is used. |
| 336 | + * |
| 337 | + * @default - `KMS` if `encryptionKey` is specified, or `AES256` otherwise. |
| 338 | + */ |
| 339 | + readonly encryption?: RepositoryEncryption; |
| 340 | + |
| 341 | + /** |
| 342 | + * External KMS key to use for repository encryption. |
| 343 | + * |
| 344 | + * The 'encryption' property must be either not specified or set to "KMS". |
| 345 | + * An error will be emitted if encryption is set to "AES256". |
| 346 | + * |
| 347 | + * @default - If encryption is set to `KMS` and this property is undefined, |
| 348 | + * an AWS managed KMS key is used. |
| 349 | + */ |
| 350 | + readonly encryptionKey?: kms.IKey; |
| 351 | + |
330 | 352 | /**
|
331 | 353 | * Life cycle rules to apply to this registry
|
332 | 354 | *
|
@@ -490,6 +512,7 @@ export class Repository extends RepositoryBase {
|
490 | 512 | scanOnPush: true,
|
491 | 513 | },
|
492 | 514 | imageTagMutability: props.imageTagMutability || undefined,
|
| 515 | + encryptionConfiguration: this.parseEncryption(props), |
493 | 516 | });
|
494 | 517 |
|
495 | 518 | resource.applyRemovalPolicy(props.removalPolicy);
|
@@ -602,6 +625,34 @@ export class Repository extends RepositoryBase {
|
602 | 625 | validateAnyRuleLast(ret);
|
603 | 626 | return ret;
|
604 | 627 | }
|
| 628 | + |
| 629 | + /** |
| 630 | + * Set up key properties and return the Repository encryption property from the |
| 631 | + * user's configuration. |
| 632 | + */ |
| 633 | + private parseEncryption(props: RepositoryProps): CfnRepository.EncryptionConfigurationProperty | undefined { |
| 634 | + |
| 635 | + // default based on whether encryptionKey is specified |
| 636 | + const encryptionType = props.encryption ?? (props.encryptionKey ? RepositoryEncryption.KMS : RepositoryEncryption.AES_256); |
| 637 | + |
| 638 | + // if encryption key is set, encryption must be set to KMS. |
| 639 | + if (encryptionType !== RepositoryEncryption.KMS && props.encryptionKey) { |
| 640 | + throw new Error(`encryptionKey is specified, so 'encryption' must be set to KMS (value: ${encryptionType.value})`); |
| 641 | + } |
| 642 | + |
| 643 | + if (encryptionType === RepositoryEncryption.AES_256) { |
| 644 | + return undefined; |
| 645 | + } |
| 646 | + |
| 647 | + if (encryptionType === RepositoryEncryption.KMS) { |
| 648 | + return { |
| 649 | + encryptionType: 'KMS', |
| 650 | + kmsKey: props.encryptionKey?.keyArn, |
| 651 | + }; |
| 652 | + } |
| 653 | + |
| 654 | + throw new Error(`Unexpected 'encryptionType': ${encryptionType}`); |
| 655 | + } |
605 | 656 | }
|
606 | 657 |
|
607 | 658 | function validateAnyRuleLast(rules: LifecycleRule[]) {
|
@@ -664,3 +715,24 @@ export enum TagMutability {
|
664 | 715 | IMMUTABLE = 'IMMUTABLE',
|
665 | 716 |
|
666 | 717 | }
|
| 718 | + |
| 719 | +/** |
| 720 | + * Indicates whether server-side encryption is enabled for the object, and whether that encryption is |
| 721 | + * from the AWS Key Management Service (AWS KMS) or from Amazon S3 managed encryption (SSE-S3). |
| 722 | + * @see https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html#SysMetadata |
| 723 | + */ |
| 724 | +export class RepositoryEncryption { |
| 725 | + /** |
| 726 | + * 'AES256' |
| 727 | + */ |
| 728 | + public static readonly AES_256 = new RepositoryEncryption('AES256'); |
| 729 | + /** |
| 730 | + * 'KMS' |
| 731 | + */ |
| 732 | + public static readonly KMS = new RepositoryEncryption('KMS'); |
| 733 | + |
| 734 | + /** |
| 735 | + * @param value the string value of the encryption |
| 736 | + */ |
| 737 | + protected constructor(public readonly value: string) { } |
| 738 | +} |
0 commit comments