Skip to content

Commit 406dee7

Browse files
nvobilisseebees
authored andcommitted
type checks to class constructors and methods (#637)
* type checks to class constructors and methods * modify grant token initialization
1 parent 6fc741d commit 406dee7

File tree

10 files changed

+459
-138
lines changed

10 files changed

+459
-138
lines changed

modules/branch-keystore-node/src/branch_keystore.ts

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { KmsConfig, RegionalKmsConfig } from './kms_config'
4+
import { isKmsConfig, KmsConfig, RegionalKmsConfig } from './kms_config'
55
import { KMSClient } from '@aws-sdk/client-kms'
66
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
77
import {
@@ -54,7 +54,7 @@ export interface IBranchKeyStoreNode {
5454
kmsClient: KMSClient
5555
ddbClient: DynamoDBClient
5656
keyStoreId: string
57-
grantTokens: ReadonlyArray<string>
57+
grantTokens?: ReadonlyArray<string>
5858

5959
getActiveBranchKey(branchKeyId: string): Promise<NodeBranchKeyMaterial>
6060
getBranchKeyVersion(
@@ -70,7 +70,7 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
7070
public declare kmsClient: KMSClient
7171
public declare ddbClient: DynamoDBClient
7272
public declare keyStoreId: string
73-
public declare grantTokens: ReadonlyArray<string>
73+
public declare grantTokens?: ReadonlyArray<string>
7474

7575
constructor({
7676
ddbTableName,
@@ -81,18 +81,67 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
8181
keyStoreId,
8282
grantTokens,
8383
}: 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+
84135
//= aws-encryption-sdk-specification/framework/branch-key-store.md#keystore-id
85136
//# The Identifier for this KeyStore.
86137
//# If one is not supplied, then a [version 4 UUID](https://www.ietf.org/rfc/rfc4122.txt) MUST be used.
87138
readOnlyProperty(this, 'keyStoreId', keyStoreId ? keyStoreId : v4())
139+
/* Postcondition: If unprovided, the keystore id is a generated valid uuidv4 */
88140

89141
//= aws-encryption-sdk-specification/framework/branch-key-store.md#aws-kms-grant-tokens
90142
//# 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 */
96145

97146
needs(kmsConfiguration, 'AWS KMS Configuration required')
98147
readOnlyProperty(this, 'kmsConfiguration', Object.freeze(kmsConfiguration))
@@ -125,6 +174,7 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
125174
region: (this.kmsConfiguration as RegionalKmsConfig).getRegion(),
126175
})
127176
)
177+
/* Postcondition: If unprovided, the DDB client is configured */
128178

129179
//= aws-encryption-sdk-specification/framework/branch-key-store.md#kms-client
130180
//# The KMS Client used when wrapping and unwrapping keys.
@@ -159,6 +209,7 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
159209
customUserAgent: KMS_CLIENT_USER_AGENT,
160210
})
161211
)
212+
/* Postcondition: If unprovided, the KMS client is configured */
162213

163214
//= aws-encryption-sdk-specification/framework/branch-key-store.md#table-name
164215
//# The table name of the DynamoDb table that backs this Keystore.
@@ -223,7 +274,10 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
223274
//# On invocation, the caller:
224275

225276
//# - 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+
)
227281

228282
//= aws-encryption-sdk-specification/framework/branch-key-store.md#getactivebranchkey
229283
//# To get the active version for the branch key id from the keystore
@@ -244,9 +298,13 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
244298

245299
//# - MUST supply a `branch-key-id`
246300
//# - MUST supply a `branchKeyVersion`
301+
needs(
302+
branchKeyId && typeof branchKeyId === 'string',
303+
'MUST supply a string branch key id'
304+
)
247305
needs(
248306
branchKeyId && branchKeyVersion,
249-
'MUST supply a branch key id and branch key version'
307+
'MUST supply a string branch key version'
250308
)
251309

252310
//= aws-encryption-sdk-specification/framework/branch-key-store.md#getbranchkeyversion
@@ -260,3 +318,10 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
260318
}
261319

262320
immutableClass(BranchKeyStoreNode)
321+
322+
// type guard
323+
export function isIBranchKeyStoreNode(
324+
keyStore: any
325+
): keyStore is BranchKeyStoreNode {
326+
return keyStore instanceof BranchKeyStoreNode
327+
}

modules/branch-keystore-node/src/branch_keystore_helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ export async function decryptBranchKey(
269269
KeyId: (kmsConfiguration as KmsKeyArnConfig).getArn(), // make this type casting assumption since only SRK Compatibility is supported currently
270270
CiphertextBlob: branchKeyRecord[BRANCH_KEY_FIELD],
271271
EncryptionContext: authenticatedEncryptionContext,
272-
GrantTokens: grantTokens.slice(),
272+
GrantTokens: grantTokens ? grantTokens.slice() : grantTokens,
273273
})
274274
)
275275

modules/branch-keystore-node/src/kms_config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ export abstract class KmsKeyArnConfig implements RegionalKmsConfig {
3737
//# `KMS Key ARN` and `KMS MRKey ARN` MUST take an additional argument
3838
//# that is a KMS ARN.
3939
constructor(arn: string) {
40+
/* Precondition: ARN must be a string */
41+
needs(typeof arn === 'string', 'ARN must be a string')
42+
4043
//= aws-encryption-sdk-specification/framework/branch-key-store.md#aws-kms-configuration
4144
//# This ARN MUST NOT be an Alias.
4245
//# This ARN MUST be a valid
@@ -76,3 +79,7 @@ export class SrkCompatibilityKmsConfig extends KmsKeyArnConfig {
7679
return this.getArn() === otherArn
7780
}
7881
}
82+
83+
export function isKmsConfig(config: any): config is SrkCompatibilityKmsConfig {
84+
return config instanceof SrkCompatibilityKmsConfig
85+
}

0 commit comments

Comments
 (0)