Skip to content

Commit 321e636

Browse files
committed
update the anotations
1 parent 0c7bb54 commit 321e636

File tree

4 files changed

+246
-84
lines changed

4 files changed

+246
-84
lines changed

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

Lines changed: 43 additions & 24 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 { isKmsConfig, KmsConfig, RegionalKmsConfig } from './kms_config'
4+
import { KmsConfig, KmsKeyConfig } from './kms_config'
55
import { KMSClient } from '@aws-sdk/client-kms'
66
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
77
import {
@@ -62,12 +62,12 @@ export interface KeyStoreInfoOutput {
6262
}
6363

6464
export class BranchKeyStoreNode implements IBranchKeyStoreNode {
65-
public declare logicalKeyStoreName: string
66-
public declare kmsConfiguration: Readonly<KmsConfig>
67-
public declare kmsClient: KMSClient
68-
public declare keyStoreId: string
69-
public declare grantTokens?: ReadonlyArray<string>
70-
public declare storage: IBranchKeyStorage
65+
public declare readonly logicalKeyStoreName: string
66+
public declare readonly kmsConfiguration: Readonly<KmsKeyConfig>
67+
public declare readonly kmsClient: KMSClient
68+
public declare readonly keyStoreId: string
69+
public declare readonly grantTokens?: ReadonlyArray<string>
70+
public declare readonly storage: IBranchKeyStorage
7171

7272
constructor({
7373
logicalKeyStoreName,
@@ -82,8 +82,12 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
8282
'Logical keystore name must be a string'
8383
)
8484

85-
/* Precondition: KMS Configuration must be SRK */
86-
needs(isKmsConfig(kmsConfiguration), 'KMS Configuration must be SRK')
85+
needs(kmsConfiguration, 'AWS KMS Configuration required')
86+
readOnlyProperty(
87+
this,
88+
'kmsConfiguration',
89+
new KmsKeyConfig(kmsConfiguration)
90+
)
8791

8892
/* Precondition: KMS client must be a KMSClient */
8993
if (keyManagement?.kmsClient) {
@@ -131,7 +135,16 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
131135
storage.ddbClient instanceof DynamoDBClient
132136
? storage.ddbClient
133137
: new DynamoDBClient({
134-
region: (kmsConfiguration as RegionalKmsConfig).getRegion(),
138+
//= aws-encryption-sdk-specification/framework/branch-key-store.md#initialization
139+
//# If a DDB client needs to be constructed and the AWS KMS Configuration is KMS Key ARN or KMS MRKey ARN,
140+
//# a new DynamoDb client MUST be created with the region of the supplied KMS ARN.
141+
//#
142+
//# If a DDB client needs to be constructed and the AWS KMS Configuration is Discovery,
143+
//# a new DynamoDb client MUST be created with the default configuration.
144+
//#
145+
//# If a DDB client needs to be constructed and the AWS KMS Configuration is MRDiscovery,
146+
//# a new DynamoDb client MUST be created with the region configured in the MRDiscovery.
147+
region: this.kmsConfiguration.getRegion(),
135148
}),
136149
})
137150
}
@@ -176,9 +189,6 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
176189
)
177190
/* Postcondition: If unprovided, the grant tokens are undefined */
178191

179-
needs(kmsConfiguration, 'AWS KMS Configuration required')
180-
readOnlyProperty(this, 'kmsConfiguration', Object.freeze(kmsConfiguration))
181-
182192
// TODO: when other KMS configuration types/classes are supported for the keystore,
183193
// verify the configuration object type to determine how we instantiate the
184194
// KMS client. This will ensure safe type casting.
@@ -189,7 +199,16 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
189199
//# If no AWS KMS client is provided one MUST be constructed.
190200
keyManagement?.kmsClient ||
191201
new KMSClient({
192-
region: (this.kmsConfiguration as RegionalKmsConfig).getRegion(),
202+
//= aws-encryption-sdk-specification/framework/branch-key-store.md#initialization
203+
//# If AWS KMS client needs to be constructed and the AWS KMS Configuration is KMS Key ARN or KMS MRKey ARN,
204+
//# a new AWS KMS client MUST be created with the region of the supplied KMS ARN.
205+
//#
206+
//# If AWS KMS client needs to be constructed and the AWS KMS Configuration is Discovery,
207+
//# a new AWS KMS client MUST be created with the default configuration.
208+
//#
209+
//# If AWS KMS client needs to be constructed and the AWS KMS Configuration is MRDiscovery,
210+
//# a new AWS KMS client MUST be created with the region configured in the MRDiscovery.
211+
region: this.kmsConfiguration.getRegion(),
193212
//= aws-encryption-sdk-specification/framework/branch-key-store.md#initialization
194213
//# On initialization the KeyStore SHOULD
195214
//# append a user agent string to the AWS KMS SDK Client with
@@ -375,7 +394,7 @@ export class BranchKeyStoreNode implements IBranchKeyStoreNode {
375394
keystoreTableName: this.storage.getKeyStorageInfo().name,
376395
logicalKeyStoreName: this.logicalKeyStoreName,
377396
grantTokens: !!this.grantTokens ? this.grantTokens.slice() : [],
378-
kmsConfiguration: this.kmsConfiguration,
397+
kmsConfiguration: this.kmsConfiguration._config,
379398
}
380399
}
381400
}
@@ -650,25 +669,25 @@ export function isIBranchKeyStoreNode(
650669
//= aws-encryption-sdk-specification/framework/branch-key-store.md#getbeaconkey
651670
//= type=exception
652671
//# On invocation, the caller:
653-
//#
672+
//#
654673
//# - MUST supply a `branch-key-id`
655-
//#
674+
//#
656675
//# GetBeaconKey MUST get the requested beacon key from the keystore
657676
//# by calling the configured [KeyStorage interface's](./key-store/key-storage.md#interface)
658677
//# [GetEncryptedBeaconKey](./key-store/key-storage.md#getencryptedbeaconkey)
659678
//# using the supplied `branch-key-id`.
660-
//#
679+
//#
661680
//# Because the storage interface can be a custom implementation the key store needs to verify correctness.
662-
//#
681+
//#
663682
//# GetBeaconKey MUST verify that the returned EncryptedHierarchicalKey MUST have the requested `branch-key-id`.
664683
//# GetBeaconKey MUST verify that the returned EncryptedHierarchicalKey is an ActiveHierarchicalSymmetricBeacon.
665684
//# GetBeaconKey MUST verify that the returned EncryptedHierarchicalKey MUST have a logical table name equal to the configured logical table name.
666-
//#
685+
//#
667686
//# The operation MUST decrypt the beacon key according to the [AWS KMS Branch Key Decryption](#aws-kms-branch-key-decryption) section.
668-
//#
687+
//#
669688
//# If the beacon key fails to decrypt, this operation MUST fail.
670-
//#
689+
//#
671690
//# This GetBeaconKey MUST construct [beacon key materials](./structures.md#beacon-key-materials) from the decrypted branch key material
672691
//# and the `branchKeyId` from the returned `branch-key-id` field.
673-
//#
674-
//# This operation MUST return the constructed [beacon key materials](./structures.md#beacon-key-materials).
692+
//#
693+
//# This operation MUST return the constructed [beacon key materials](./structures.md#beacon-key-materials).

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { BranchKeyItem, BranchKeyRecord } from './branch_keystore_structures'
1313
import { EncryptedHierarchicalKey, BranchKeyEncryptionContext } from './types'
1414
// import { IBranchKeyStoreNode } from './branch_keystore'
1515
import { DecryptCommand } from '@aws-sdk/client-kms'
16-
import { KmsKeyArnConfig, KmsConfig } from './kms_config'
16+
import { KmsKeyConfig } from './kms_config'
1717
import {
1818
PARTITION_KEY,
1919
SORT_KEY,
@@ -31,6 +31,7 @@ import {
3131
BEACON_KEY_TYPE_VALUE,
3232
POTENTIAL_BRANCH_KEY_RECORD_FIELDS,
3333
} from './constants'
34+
import { parseAwsKmsKeyArn } from '@aws-crypto/kms-keyring'
3435

3536
/**
3637
* This utility function uses a partition and sort key to query for a single branch
@@ -254,32 +255,33 @@ export async function decryptBranchKey(
254255
kmsClient,
255256
}: {
256257
kmsClient: KMSClient
257-
kmsConfiguration: Readonly<KmsConfig>
258+
kmsConfiguration: Readonly<KmsKeyConfig>
258259
grantTokens?: ReadonlyArray<string>
259260
},
260261
encryptedHierarchicalKey: EncryptedHierarchicalKey
261262
): Promise<Buffer> {
262-
//= aws-encryption-sdk-specification/framework/branch-key-store.md#aws-kms-branch-key-decryption
263-
//# If the Keystore's [AWS KMS Configuration](#aws-kms-configuration) is `KMS Key ARN` or `KMS MRKey ARN`,
264-
//# the `kms-arn` field of the DDB response item MUST be
265-
//# [compatible with](#aws-key-arn-compatibility) the configured KMS Key in
266-
//# the [AWS KMS Configuration](#aws-kms-configuration) for this keystore,
267-
//# or the operation MUST fail.
263+
//= aws-encryption-sdk-specification/framework/branch-key-store.md#discovery
264+
//# The Keystore MAY use the KMS Key ARNs already
265+
//# persisted to the backing DynamoDB table,
266+
//# provided they are in records created
267+
//# with an identical Logical Keystore Name.
268268

269-
//# If the Keystore's [AWS KMS Configuration](#aws-kms-configuration) is `Discovery` or `MRDiscovery`,
270-
//# the `kms-arn` field of DDB response item MUST NOT be an Alias
271-
//# or the operation MUST fail.
272-
needs(
273-
kmsConfiguration.isCompatibleWithArn(encryptedHierarchicalKey.kmsArn),
274-
'KMS ARN from DDB response item MUST be compatible with the configured KMS Key in the AWS KMS Configuration for this keystore'
269+
//= aws-encryption-sdk-specification/framework/branch-key-store.md#mrdiscovery
270+
//# The Keystore MAY use the KMS Key ARNs already
271+
//# persisted to the backing DynamoDB table,
272+
//# provided they are in records created
273+
//# with an identical Logical Keystore Name.
274+
275+
const KeyId = kmsConfiguration.getCompatibleArnArn(
276+
encryptedHierarchicalKey.kmsArn
275277
)
276278

277279
//= aws-encryption-sdk-specification/framework/branch-key-store.md#aws-kms-branch-key-decryption
278280
//# When calling [AWS KMS Decrypt](https://docs.aws.amazon.com/kms/latest/APIReference/API_Decrypt.html),
279281
//# the keystore operation MUST call with a request constructed as follows:
280282
const response = await kmsClient.send(
281283
new DecryptCommand({
282-
KeyId: (kmsConfiguration as KmsKeyArnConfig).getArn(), // make this type casting assumption since only SRK Compatibility is supported currently
284+
KeyId,
283285
//= aws-encryption-sdk-specification/framework/branch-key-store.md#aws-kms-branch-key-decryption
284286
//# - `CiphertextBlob` MUST be the `CiphertextBlob` attribute value on the provided EncryptedHierarchicalKey
285287
CiphertextBlob: encryptedHierarchicalKey.ciphertextBlob,

0 commit comments

Comments
 (0)