Skip to content

Commit 78396d7

Browse files
committed
all tests pass and all features “work”
1 parent ff6bdf2 commit 78396d7

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

modules/kms-keyring-node/src/kms_hkeyring_node.ts

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,16 @@ export interface KmsHierarchicalKeyRingNodeInput {
5555
branchKeyIdSupplier?: BranchKeyIdSupplier
5656
keyStore: BranchKeyStoreNode
5757
cacheLimitTtl: number
58+
cache?: CryptographicMaterialsCache<NodeAlgorithmSuite>
5859
maxCacheSize?: number
60+
partitionId?: string
5961
}
6062

6163
export interface IKmsHierarchicalKeyRingNode extends KeyringNode {
6264
branchKeyId?: string
6365
branchKeyIdSupplier?: Readonly<BranchKeyIdSupplier>
6466
keyStore: Readonly<BranchKeyStoreNode>
6567
cacheLimitTtl: number
66-
maxCacheSize: number
6768
_onEncrypt(material: NodeEncryptionMaterial): Promise<NodeEncryptionMaterial>
6869
_onDecrypt(
6970
material: NodeDecryptionMaterial,
@@ -78,23 +79,25 @@ export class KmsHierarchicalKeyRingNode
7879
public declare branchKeyId?: string
7980
public declare branchKeyIdSupplier?: Readonly<BranchKeyIdSupplier>
8081
public declare keyStore: Readonly<BranchKeyStoreNode>
81-
public declare _logicalKeyStoreName: Buffer
82+
public declare _logicalKeyStoreName: Buffer
8283
public declare cacheLimitTtl: number
83-
public declare maxCacheSize: number
84-
private _cmc: CryptographicMaterialsCache<NodeAlgorithmSuite>
84+
public declare maxCacheSize?: number
85+
public declare _cmc: CryptographicMaterialsCache<NodeAlgorithmSuite>
8586
declare readonly _partition: Buffer
8687

8788
constructor({
8889
branchKeyId,
8990
branchKeyIdSupplier,
9091
keyStore,
9192
cacheLimitTtl,
93+
cache,
9294
maxCacheSize,
95+
partitionId,
9396
}: KmsHierarchicalKeyRingNodeInput) {
9497
super()
9598

96-
const partition = randomBytes(64)
97-
readOnlyProperty(this, '_partition', partition)
99+
needs(!partitionId || typeof partitionId === 'string', 'Partition id must be a string.')
100+
readOnlyProperty(this, '_partition', partitionId ? stringToUtf8Bytes(partitionId) : randomBytes(64))
98101

99102
/* Precondition: The branch key id must be a string */
100103
if (branchKeyId) {
@@ -122,24 +125,18 @@ export class KmsHierarchicalKeyRingNode
122125
'The keystore must be a BranchKeyStore'
123126
)
124127

125-
readOnlyProperty(this, '_logicalKeyStoreName', stringToUtf8Bytes(keyStore.getKeyStoreInfo().logicalKeyStoreName))
128+
readOnlyProperty(
129+
this,
130+
'_logicalKeyStoreName',
131+
stringToUtf8Bytes(keyStore.getKeyStoreInfo().logicalKeyStoreName)
132+
)
126133

127134
/* Precondition: The cache limit TTL must be a number */
128135
needs(
129136
typeof cacheLimitTtl === 'number',
130137
'The cache limit TTL must be a number'
131138
)
132139

133-
/* Precondition: The max cache size must be a number */
134-
if (maxCacheSize || maxCacheSize === 0) {
135-
needs(
136-
typeof maxCacheSize === 'number',
137-
'The max cache size must be a number'
138-
)
139-
} else {
140-
maxCacheSize = undefined
141-
}
142-
143140
//= aws-encryption-sdk-specification/framework/aws-kms/aws-kms-hierarchical-keyring.md#cache-limit-ttl
144141
//# The maximum amount of time in seconds that an entry within the cache may be used before it MUST be evicted.
145142
//# The client MUST set a time-to-live (TTL) for [branch key materials](../structures.md#branch-key-materials) in the underlying cache.
@@ -162,14 +159,6 @@ export class KmsHierarchicalKeyRingNode
162159
'Must provide a branch key identifier or supplier'
163160
)
164161

165-
/* Precondition: Max cache size must be non-negative and less than or equal Number.MAX_SAFE_INTEGER */
166-
if (maxCacheSize) {
167-
needs(
168-
0 <= maxCacheSize && maxCacheSize <= Number.MAX_SAFE_INTEGER,
169-
'Max cache size must be non-negative and less than or equal Number.MAX_SAFE_INTEGER'
170-
)
171-
}
172-
173162
readOnlyProperty(this, 'keyStore', Object.freeze(keyStore))
174163
/* Postcondition: The keystore object is frozen */
175164

@@ -187,17 +176,33 @@ export class KmsHierarchicalKeyRingNode
187176
)
188177
/* Postcondition: Provided branch key supplier must be frozen */
189178

190-
//= aws-encryption-sdk-specification/framework/aws-kms/aws-kms-hierarchical-keyring.md#initialization
191-
//# If no max cache size is provided, the cryptographic materials cache MUST be configured to a
192-
//# max cache size of 1000.
193-
readOnlyProperty(
194-
this,
195-
'maxCacheSize',
196-
maxCacheSize || maxCacheSize === 0 ? maxCacheSize : 1000
197-
)
198-
/* Postcondition: The max cache size is initialized */
179+
if (cache) {
180+
needs(!maxCacheSize, 'Max cache size not supported when passing a cache.')
181+
} else {
199182

200-
this._cmc = getLocalCryptographicMaterialsCache(this.maxCacheSize)
183+
console.log(maxCacheSize)
184+
185+
/* Precondition: The max cache size must be a number */
186+
needs(
187+
// Order is important, 0 is a number but also false.
188+
typeof maxCacheSize === 'number' || !maxCacheSize,
189+
'The max cache size must be a number'
190+
)
191+
192+
//= aws-encryption-sdk-specification/framework/aws-kms/aws-kms-hierarchical-keyring.md#initialization
193+
//# If no max cache size is provided, the cryptographic materials cache MUST be configured to a
194+
//# max cache size of 1000.
195+
maxCacheSize = maxCacheSize === 0 || maxCacheSize ? maxCacheSize : 1000
196+
/* Precondition: Max cache size must be non-negative and less than or equal Number.MAX_SAFE_INTEGER */
197+
needs(
198+
0 <= maxCacheSize && maxCacheSize <= Number.MAX_SAFE_INTEGER,
199+
'Max cache size must be non-negative and less than or equal Number.MAX_SAFE_INTEGER'
200+
)
201+
202+
cache = getLocalCryptographicMaterialsCache(maxCacheSize)
203+
}
204+
readOnlyProperty(this, 'maxCacheSize', maxCacheSize)
205+
readOnlyProperty(this, '_cmc', cache)
201206

202207
Object.freeze(this)
203208
/* Postcondition: The HKR object must be frozen */

0 commit comments

Comments
 (0)