Skip to content

Commit cbf3204

Browse files
enforce cache expiration for versioned secret
1 parent 027faa2 commit cbf3204

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/IKeyValueAdapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ export interface IKeyValueAdapter {
1717
/**
1818
* This method is called when a change is detected in the configuration setting.
1919
*/
20-
onChangeDetected(setting?: ConfigurationSetting): Promise<void>;
20+
onChangeDetected(): Promise<void>;
2121
}

src/keyvault/AzureKeyVaultSecretProvider.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { SecretClient, KeyVaultSecretIdentifier } from "@azure/keyvault-secrets"
88

99
export class AzureKeyVaultSecretProvider {
1010
#keyVaultOptions: KeyVaultOptions | undefined;
11-
#refreshTimer: RefreshTimer | undefined;
11+
#secretRefreshTimer: RefreshTimer | undefined;
12+
#cacheRefreshTimer: RefreshTimer = new RefreshTimer(24*60*60*1000); // Enforce cache expiration every 24 hours
1213
#secretClients: Map<string, SecretClient>; // map key vault hostname to corresponding secret client
1314
#cachedSecretValue: Map<string, any> = new Map<string, any>(); // map secret identifier to secret value
1415

@@ -22,7 +23,7 @@ export class AzureKeyVaultSecretProvider {
2223
}
2324
}
2425
this.#keyVaultOptions = keyVaultOptions;
25-
this.#refreshTimer = refreshTimer;
26+
this.#secretRefreshTimer = refreshTimer;
2627
this.#secretClients = new Map();
2728
for (const client of this.#keyVaultOptions?.secretClients ?? []) {
2829
const clientUrl = new URL(client.vaultUrl);
@@ -39,7 +40,7 @@ export class AzureKeyVaultSecretProvider {
3940
return this.#cachedSecretValue.get(identifierKey);
4041
}
4142

42-
if (this.#refreshTimer && !this.#refreshTimer.canRefresh()) {
43+
if (this.#secretRefreshTimer && !this.#secretRefreshTimer.canRefresh()) {
4344
// If the refresh interval is not expired, return the cached value if available.
4445
if (this.#cachedSecretValue.has(identifierKey)) {
4546
return this.#cachedSecretValue.get(identifierKey);
@@ -53,6 +54,12 @@ export class AzureKeyVaultSecretProvider {
5354
}
5455

5556
clearCache(): void {
57+
if (this.#cacheRefreshTimer.canRefresh()) {
58+
// Clear the cache if the cache expiration timer has expired.
59+
this.#cachedSecretValue.clear();
60+
this.#cacheRefreshTimer.reset();
61+
return;
62+
}
5663
// If the secret identifier has specified a version, it is not removed from the cache.
5764
// If the secret identifier has not specified a version, it means that the latest version should be used. Remove the cached value to force a reload.
5865
for (const key of this.#cachedSecretValue.keys()) {

src/keyvault/KeyVaultOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface KeyVaultOptions {
4444
parallelSecretResolutionEnabled?: boolean;
4545

4646
/**
47-
* Specifies the refresh interval in milliseconds for periodically reloading secret from Key Vault.
47+
* Specifies the refresh interval in milliseconds for periodically reloading all secrets from Key Vault.
4848
*
4949
* @remarks
5050
* If specified, the value must be greater than 60 seconds.

0 commit comments

Comments
 (0)