Skip to content

Commit c895e92

Browse files
committed
Add expiration
1 parent 174b3bd commit c895e92

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

modules/cache-material/src/cryptographic_materials_cache.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,5 @@ export interface DecryptionMaterialEntry<S extends SupportedAlgorithmSuites>
6363

6464
export interface BranchKeyMaterialEntry {
6565
readonly response: BranchKeyMaterial
66+
readonly now: number
6667
}

modules/cache-material/src/get_local_cryptographic_materials_cache.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export function getLocalCryptographicMaterialsCache<
121121

122122
const entry = Object.seal({
123123
response: material,
124+
now: Date.now(),
124125
})
125126

126127
cache.set(key, entry, maxAge)

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
isDecryptionMaterial,
2424
} from '@aws-crypto/material-management'
2525
import {
26+
BranchKeyMaterialEntry,
2627
CryptographicMaterialsCache,
2728
getLocalCryptographicMaterialsCache,
2829
} from '@aws-crypto/cache-material'
@@ -85,6 +86,7 @@ export interface IKmsHierarchicalKeyRingNode extends KeyringNode {
8586
material: NodeDecryptionMaterial,
8687
encryptedDataKeys: EncryptedDataKey[]
8788
): Promise<NodeDecryptionMaterial>
89+
cacheEntryHasExceededLimits(entry: BranchKeyMaterialEntry): boolean
8890
}
8991

9092
export class KmsHierarchicalKeyRingNode
@@ -337,6 +339,21 @@ export class KmsHierarchicalKeyRingNode
337339
return material
338340
}
339341

342+
cacheEntryHasExceededLimits({ now }: BranchKeyMaterialEntry): boolean {
343+
//= aws-encryption-sdk-specification/framework/aws-kms/aws-kms-hierarchical-keyring.md#onencrypt
344+
//# There MUST be a check (cacheEntryWithinLimits) to make sure that for the cache entry found, who's TTL has NOT expired,
345+
//# `time.now() - cacheEntryCreationTime <= ttlSeconds` is true and
346+
//# valid for TTL of the Hierarchical Keyring getting the cache entry.
347+
348+
//= aws-encryption-sdk-specification/framework/aws-kms/aws-kms-hierarchical-keyring.md#ondecrypt
349+
//# There MUST be a check (cacheEntryWithinLimits) to make sure that for the cache entry found, who's TTL has NOT expired,
350+
//# `time.now() - cacheEntryCreationTime <= ttlSeconds` is true and
351+
//# valid for TTL of the Hierarchical Keyring getting the cache entry.
352+
353+
const age = Date.now() - now
354+
return age > this.cacheLimitTtl
355+
}
356+
340357
async _onDecrypt(
341358
decryptionMaterial: NodeDecryptionMaterial,
342359
encryptedDataKeyObjs: EncryptedDataKey[]

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,19 +219,27 @@ export function getCacheEntryId(
219219
}
220220

221221
export async function getBranchKeyMaterials(
222-
{ keyStore, cacheLimitTtl }: IKmsHierarchicalKeyRingNode,
222+
hKeyring: IKmsHierarchicalKeyRingNode,
223223
cmc: CryptographicMaterialsCache<NodeAlgorithmSuite>,
224224
branchKeyId: string,
225225
cacheEntryId: string,
226226
branchKeyVersion?: string
227227
): Promise<NodeBranchKeyMaterial> {
228+
const { keyStore, cacheLimitTtl } = hKeyring
229+
228230
//= aws-encryption-sdk-specification/framework/aws-kms/aws-kms-hierarchical-keyring.md#onencrypt
229231
//# The hierarchical keyring MUST attempt to find [branch key materials](../structures.md#branch-key-materials)
230232
//# from the underlying [cryptographic materials cache](../local-cryptographic-materials-cache.md).
231233
const cacheEntry = cmc.getBranchKeyMaterial(cacheEntryId)
232234
let branchKeyMaterials: NodeBranchKeyMaterial
233235
// if the cache entry is false, branch key materials were not found
234-
if (!cacheEntry) {
236+
if (!cacheEntry || hKeyring.cacheEntryHasExceededLimits(cacheEntry)) {
237+
//= aws-encryption-sdk-specification/framework/aws-kms/aws-kms-hierarchical-keyring.md#onencrypt
238+
//# If this is NOT true, then we MUST treat the cache entry as expired.
239+
240+
//= aws-encryption-sdk-specification/framework/aws-kms/aws-kms-hierarchical-keyring.md#ondecrypt
241+
//# If this is NOT true, then we MUST treat the cache entry as expired.
242+
235243
//= aws-encryption-sdk-specification/framework/aws-kms/aws-kms-hierarchical-keyring.md#onencrypt
236244
//# If a cache entry is not found or the cache entry is expired, the hierarchical keyring MUST attempt to obtain the branch key materials
237245
//# by querying the backing branch keystore specified in the [retrieve OnEncrypt branch key materials](#query-branch-keystore-onencrypt) section.
@@ -245,8 +253,8 @@ export async function getBranchKeyMaterials(
245253
//= aws-encryption-sdk-specification/framework/aws-kms/aws-kms-hierarchical-keyring.md#getitem-branch-keystore-ondecrypt
246254
//# Otherwise, OnDecrypt MUST fail.
247255

248-
// get them from the keystore, whether we need the active or versioned
249-
// material
256+
//= aws-encryption-sdk-specification/framework/aws-kms/aws-kms-hierarchical-keyring.md#query-branch-keystore-onencrypt
257+
//# OnEncrypt MUST call the Keystore's [GetActiveBranchKey](../branch-key-store.md#getactivebranchkey) operation with the following inputs:
250258

251259
//= aws-encryption-sdk-specification/framework/aws-kms/aws-kms-hierarchical-keyring.md#getitem-branch-keystore-ondecrypt
252260
//# OnDecrypt MUST call the Keystore's [GetBranchKeyVersion](../branch-key-store.md#getbranchkeyversion) operation with the following inputs:

0 commit comments

Comments
 (0)