1
- import { Construct } from '@aws-cdk/cdk' ;
1
+ import { Construct , IResource , Resource , Token } from '@aws-cdk/cdk' ;
2
2
import { IKey } from './key' ;
3
3
import { CfnAlias } from './kms.generated' ;
4
4
5
5
const REQUIRED_ALIAS_PREFIX = 'alias/' ;
6
- const DISALLOWED_PREFIX = REQUIRED_ALIAS_PREFIX + 'AWS ' ;
6
+ const DISALLOWED_PREFIX = REQUIRED_ALIAS_PREFIX + 'aws/ ' ;
7
7
8
- export interface EncryptionKeyAliasProps {
8
+ /**
9
+ * A KMS Key alias.
10
+ */
11
+ export interface IAlias extends IResource {
12
+ /**
13
+ * The name of the alias.
14
+ *
15
+ * @attribute AliasName
16
+ */
17
+ readonly aliasName : string ;
18
+
19
+ /**
20
+ * The Key to which the Alias refers.
21
+ *
22
+ * @attribute TargetKeyId
23
+ */
24
+ readonly aliasTargetKey : IKey ;
25
+ }
26
+
27
+ /**
28
+ * Construction properties for a KMS Key Alias object.
29
+ */
30
+ export interface AliasProps {
9
31
/**
10
32
* The name of the alias. The name must start with alias followed by a
11
33
* forward slash, such as alias/. You can't specify aliases that begin with
12
34
* alias/AWS. These aliases are reserved.
13
35
*/
14
- readonly alias : string ;
36
+ readonly name : string ;
15
37
16
38
/**
17
39
* The ID of the key for which you are creating the alias. Specify the key's
18
40
* globally unique identifier or Amazon Resource Name (ARN). You can't
19
41
* specify another alias.
20
42
*/
21
- readonly key : IKey ;
43
+ readonly targetKey : IKey ;
44
+ }
45
+
46
+ abstract class AliasBase extends Resource implements IAlias {
47
+ public abstract readonly aliasName : string ;
48
+
49
+ public abstract readonly aliasTargetKey : IKey ;
50
+ }
51
+
52
+ export interface AliasAttributes {
53
+ readonly aliasName : string ;
54
+ readonly aliasTargetKey : IKey ;
22
55
}
23
56
24
57
/**
@@ -29,31 +62,46 @@ export interface EncryptionKeyAliasProps {
29
62
* Working with Aliases in the AWS Key Management Service Developer Guide.
30
63
*
31
64
* You can also add an alias for a key by calling `key.addAlias(alias)`.
65
+ *
66
+ * @resource AWS::KMS::Alias
32
67
*/
33
- export class EncryptionKeyAlias extends Construct {
34
- /**
35
- * The name of the alias.
36
- */
37
- public aliasName : string ;
68
+ export class Alias extends AliasBase {
69
+ public static fromAliasAttributes ( scope : Construct , id : string , attrs : AliasAttributes ) : IAlias {
70
+ // tslint:disable-next-line: class-name
71
+ class _Alias extends AliasBase {
72
+ public get aliasName ( ) { return attrs . aliasName ; }
73
+ public get aliasTargetKey ( ) { return attrs . aliasTargetKey ; }
74
+ }
75
+ return new _Alias ( scope , id ) ;
76
+ }
77
+
78
+ public readonly aliasName : string ;
79
+ public readonly aliasTargetKey : IKey ;
38
80
39
- constructor ( scope : Construct , id : string , props : EncryptionKeyAliasProps ) {
81
+ constructor ( scope : Construct , id : string , props : AliasProps ) {
40
82
super ( scope , id ) ;
41
83
42
- if ( ! props . alias . startsWith ( REQUIRED_ALIAS_PREFIX ) ) {
43
- throw new Error ( `Alias must start with the prefix "${ REQUIRED_ALIAS_PREFIX } ": ${ props . alias } ` ) ;
44
- }
84
+ if ( ! Token . unresolved ( props . name ) ) {
85
+ if ( ! props . name . startsWith ( REQUIRED_ALIAS_PREFIX ) ) {
86
+ throw new Error ( `Alias must start with the prefix "${ REQUIRED_ALIAS_PREFIX } ": ${ props . name } ` ) ;
87
+ }
45
88
46
- if ( props . alias === REQUIRED_ALIAS_PREFIX ) {
47
- throw new Error ( `Alias must include a value after "${ REQUIRED_ALIAS_PREFIX } ": ${ props . alias } ` ) ;
48
- }
89
+ if ( props . name === REQUIRED_ALIAS_PREFIX ) {
90
+ throw new Error ( `Alias must include a value after "${ REQUIRED_ALIAS_PREFIX } ": ${ props . name } ` ) ;
91
+ }
92
+
93
+ if ( props . name . startsWith ( DISALLOWED_PREFIX ) ) {
94
+ throw new Error ( `Alias cannot start with ${ DISALLOWED_PREFIX } : ${ props . name } ` ) ;
95
+ }
49
96
50
- if ( props . alias . startsWith ( DISALLOWED_PREFIX ) ) {
51
- throw new Error ( `Alias cannot start with ${ DISALLOWED_PREFIX } : ${ props . alias } ` ) ;
97
+ if ( ! props . name . match ( / ^ [ a - z A - Z 0 - 9 : / _ - ] { 1 , 256 } $ / ) ) {
98
+ throw new Error ( `Alias name must be between 1 and 256 characters in a-zA-Z0-9:/_-` ) ;
99
+ }
52
100
}
53
101
54
102
const resource = new CfnAlias ( this , 'Resource' , {
55
- aliasName : props . alias ,
56
- targetKeyId : props . key . keyArn
103
+ aliasName : props . name ,
104
+ targetKeyId : props . targetKey . keyArn
57
105
} ) ;
58
106
59
107
this . aliasName = resource . aliasName ;
0 commit comments