@@ -55,15 +55,16 @@ export interface KmsHierarchicalKeyRingNodeInput {
55
55
branchKeyIdSupplier ?: BranchKeyIdSupplier
56
56
keyStore : BranchKeyStoreNode
57
57
cacheLimitTtl : number
58
+ cache ?: CryptographicMaterialsCache < NodeAlgorithmSuite >
58
59
maxCacheSize ?: number
60
+ partitionId ?: string
59
61
}
60
62
61
63
export interface IKmsHierarchicalKeyRingNode extends KeyringNode {
62
64
branchKeyId ?: string
63
65
branchKeyIdSupplier ?: Readonly < BranchKeyIdSupplier >
64
66
keyStore : Readonly < BranchKeyStoreNode >
65
67
cacheLimitTtl : number
66
- maxCacheSize : number
67
68
_onEncrypt ( material : NodeEncryptionMaterial ) : Promise < NodeEncryptionMaterial >
68
69
_onDecrypt (
69
70
material : NodeDecryptionMaterial ,
@@ -78,23 +79,25 @@ export class KmsHierarchicalKeyRingNode
78
79
public declare branchKeyId ?: string
79
80
public declare branchKeyIdSupplier ?: Readonly < BranchKeyIdSupplier >
80
81
public declare keyStore : Readonly < BranchKeyStoreNode >
81
- public declare _logicalKeyStoreName : Buffer
82
+ public declare _logicalKeyStoreName : Buffer
82
83
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 >
85
86
declare readonly _partition : Buffer
86
87
87
88
constructor ( {
88
89
branchKeyId,
89
90
branchKeyIdSupplier,
90
91
keyStore,
91
92
cacheLimitTtl,
93
+ cache,
92
94
maxCacheSize,
95
+ partitionId,
93
96
} : KmsHierarchicalKeyRingNodeInput ) {
94
97
super ( )
95
98
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 ) )
98
101
99
102
/* Precondition: The branch key id must be a string */
100
103
if ( branchKeyId ) {
@@ -122,24 +125,18 @@ export class KmsHierarchicalKeyRingNode
122
125
'The keystore must be a BranchKeyStore'
123
126
)
124
127
125
- readOnlyProperty ( this , '_logicalKeyStoreName' , stringToUtf8Bytes ( keyStore . getKeyStoreInfo ( ) . logicalKeyStoreName ) )
128
+ readOnlyProperty (
129
+ this ,
130
+ '_logicalKeyStoreName' ,
131
+ stringToUtf8Bytes ( keyStore . getKeyStoreInfo ( ) . logicalKeyStoreName )
132
+ )
126
133
127
134
/* Precondition: The cache limit TTL must be a number */
128
135
needs (
129
136
typeof cacheLimitTtl === 'number' ,
130
137
'The cache limit TTL must be a number'
131
138
)
132
139
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
-
143
140
//= aws-encryption-sdk-specification/framework/aws-kms/aws-kms-hierarchical-keyring.md#cache-limit-ttl
144
141
//# The maximum amount of time in seconds that an entry within the cache may be used before it MUST be evicted.
145
142
//# 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
162
159
'Must provide a branch key identifier or supplier'
163
160
)
164
161
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
-
173
162
readOnlyProperty ( this , 'keyStore' , Object . freeze ( keyStore ) )
174
163
/* Postcondition: The keystore object is frozen */
175
164
@@ -187,17 +176,33 @@ export class KmsHierarchicalKeyRingNode
187
176
)
188
177
/* Postcondition: Provided branch key supplier must be frozen */
189
178
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 {
199
182
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 )
201
206
202
207
Object . freeze ( this )
203
208
/* Postcondition: The HKR object must be frozen */
0 commit comments