1
1
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
- import { KmsConfig , RegionalKmsConfig } from './kms_config'
4
+ import { isKmsConfig , KmsConfig , RegionalKmsConfig } from './kms_config'
5
5
import { KMSClient } from '@aws-sdk/client-kms'
6
6
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
7
7
import {
@@ -54,7 +54,7 @@ export interface IBranchKeyStoreNode {
54
54
kmsClient : KMSClient
55
55
ddbClient : DynamoDBClient
56
56
keyStoreId : string
57
- grantTokens : ReadonlyArray < string >
57
+ grantTokens ? : ReadonlyArray < string >
58
58
59
59
getActiveBranchKey ( branchKeyId : string ) : Promise < NodeBranchKeyMaterial >
60
60
getBranchKeyVersion (
@@ -70,7 +70,7 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
70
70
public declare kmsClient : KMSClient
71
71
public declare ddbClient : DynamoDBClient
72
72
public declare keyStoreId : string
73
- public declare grantTokens : ReadonlyArray < string >
73
+ public declare grantTokens ? : ReadonlyArray < string >
74
74
75
75
constructor ( {
76
76
ddbTableName,
@@ -81,18 +81,67 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
81
81
keyStoreId,
82
82
grantTokens,
83
83
} : BranchKeyStoreNodeInput ) {
84
+ /* Precondition: DDB table name must be a string */
85
+ needs ( typeof ddbTableName === 'string' , 'DDB table name must be a string' )
86
+
87
+ /* Precondition: Logical keystore name must be a string */
88
+ needs (
89
+ typeof logicalKeyStoreName === 'string' ,
90
+ 'Logical keystore name must be a string'
91
+ )
92
+
93
+ /* Precondition: KMS Configuration must be SRK */
94
+ needs ( isKmsConfig ( kmsConfiguration ) , 'KMS Configuration must be SRK' )
95
+
96
+ /* Precondition: KMS client must be a KMSClient */
97
+ if ( kmsClient ) {
98
+ needs ( kmsClient instanceof KMSClient , 'KMS client must be a KMSClient' )
99
+ } else {
100
+ // ensure it's strictly undefined and not some other falsey value
101
+ kmsClient = undefined
102
+ }
103
+
104
+ /* Precondition: DDB client must be a DynamoDBClient */
105
+ if ( ddbClient ) {
106
+ needs (
107
+ ddbClient instanceof DynamoDBClient ,
108
+ 'DDB client must be a DynamoDBClient'
109
+ )
110
+ } else {
111
+ // ensure it's strictly undefined and not some other falsey value
112
+ ddbClient = undefined
113
+ }
114
+
115
+ /* Precondition: Keystore id must be a string */
116
+ if ( keyStoreId ) {
117
+ needs ( typeof keyStoreId === 'string' , 'Keystore id must be a string' )
118
+ } else {
119
+ // ensure it's strictly undefined and not some other falsey value
120
+ keyStoreId = undefined
121
+ }
122
+
123
+ /* Precondition: Grant tokens must be a string array */
124
+ if ( grantTokens ) {
125
+ needs (
126
+ Array . isArray ( grantTokens ) &&
127
+ grantTokens . every ( ( grantToken ) => typeof grantToken === 'string' ) ,
128
+ 'Grant tokens must be a string array'
129
+ )
130
+ } else {
131
+ // ensure it's strictly undefined and not some other falsey value
132
+ grantTokens = undefined
133
+ }
134
+
84
135
//= aws-encryption-sdk-specification/framework/branch-key-store.md#keystore-id
85
136
//# The Identifier for this KeyStore.
86
137
//# If one is not supplied, then a [version 4 UUID](https://www.ietf.org/rfc/rfc4122.txt) MUST be used.
87
138
readOnlyProperty ( this , 'keyStoreId' , keyStoreId ? keyStoreId : v4 ( ) )
139
+ /* Postcondition: If unprovided, the keystore id is a generated valid uuidv4 */
88
140
89
141
//= aws-encryption-sdk-specification/framework/branch-key-store.md#aws-kms-grant-tokens
90
142
//# A list of AWS KMS [grant tokens](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#grant_token).
91
- readOnlyProperty (
92
- this ,
93
- 'grantTokens' ,
94
- Object . freeze ( grantTokens ? grantTokens : [ ] )
95
- )
143
+ readOnlyProperty ( this , 'grantTokens' , grantTokens )
144
+ /* Postcondition: If unprovided, the grant tokens are undefined */
96
145
97
146
needs ( kmsConfiguration , 'AWS KMS Configuration required' )
98
147
readOnlyProperty ( this , 'kmsConfiguration' , Object . freeze ( kmsConfiguration ) )
@@ -125,6 +174,7 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
125
174
region : ( this . kmsConfiguration as RegionalKmsConfig ) . getRegion ( ) ,
126
175
} )
127
176
)
177
+ /* Postcondition: If unprovided, the DDB client is configured */
128
178
129
179
//= aws-encryption-sdk-specification/framework/branch-key-store.md#kms-client
130
180
//# The KMS Client used when wrapping and unwrapping keys.
@@ -159,6 +209,7 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
159
209
customUserAgent : KMS_CLIENT_USER_AGENT ,
160
210
} )
161
211
)
212
+ /* Postcondition: If unprovided, the KMS client is configured */
162
213
163
214
//= aws-encryption-sdk-specification/framework/branch-key-store.md#table-name
164
215
//# The table name of the DynamoDb table that backs this Keystore.
@@ -223,7 +274,10 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
223
274
//# On invocation, the caller:
224
275
225
276
//# - MUST supply a `branch-key-id`
226
- needs ( branchKeyId , 'MUST supply a branch key id' )
277
+ needs (
278
+ branchKeyId && typeof branchKeyId === 'string' ,
279
+ 'MUST supply a string branch key id'
280
+ )
227
281
228
282
//= aws-encryption-sdk-specification/framework/branch-key-store.md#getactivebranchkey
229
283
//# To get the active version for the branch key id from the keystore
@@ -244,9 +298,13 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
244
298
245
299
//# - MUST supply a `branch-key-id`
246
300
//# - MUST supply a `branchKeyVersion`
301
+ needs (
302
+ branchKeyId && typeof branchKeyId === 'string' ,
303
+ 'MUST supply a string branch key id'
304
+ )
247
305
needs (
248
306
branchKeyId && branchKeyVersion ,
249
- 'MUST supply a branch key id and branch key version'
307
+ 'MUST supply a string branch key version'
250
308
)
251
309
252
310
//= aws-encryption-sdk-specification/framework/branch-key-store.md#getbranchkeyversion
@@ -260,3 +318,10 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
260
318
}
261
319
262
320
immutableClass ( BranchKeyStoreNode )
321
+
322
+ // type guard
323
+ export function isIBranchKeyStoreNode (
324
+ keyStore : any
325
+ ) : keyStore is BranchKeyStoreNode {
326
+ return keyStore instanceof BranchKeyStoreNode
327
+ }
0 commit comments