Skip to content

Commit 6d8c41f

Browse files
always cached secret with version
1 parent 4389642 commit 6d8c41f

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/keyvault/AzureKeyVaultSecretProvider.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,34 @@ export class AzureKeyVaultSecretProvider {
3131
}
3232

3333
async getSecretValue(secretIdentifier: KeyVaultSecretIdentifier): Promise<unknown> {
34+
// The map key is a combination of sourceId and version: "{sourceId}\n{version}"
35+
const identifierKey = `${secretIdentifier.sourceId}\n${secretIdentifier.version ?? ""}`;
3436
if (this.#refreshTimer && !this.#refreshTimer.canRefresh()) {
3537
// return the cached secret value if it exists
36-
if (this.#cachedSecretValue.has(secretIdentifier.sourceId)) {
37-
const cachedValue = this.#cachedSecretValue.get(secretIdentifier.sourceId);
38+
if (this.#cachedSecretValue.has(identifierKey)) {
39+
const cachedValue = this.#cachedSecretValue.get(identifierKey);
3840
return cachedValue;
3941
}
4042
// not found in cache, get the secret value from key vault
4143
const secretValue = await this.#getSecretValueFromKeyVault(secretIdentifier);
42-
this.#cachedSecretValue.set(secretIdentifier.sourceId, secretValue);
44+
this.#cachedSecretValue.set(identifierKey, secretValue);
4345
return secretValue;
4446
}
4547

4648
// Always reload the secret value from key vault when the refresh timer expires.
4749
const secretValue = await this.#getSecretValueFromKeyVault(secretIdentifier);
48-
this.#cachedSecretValue.set(secretIdentifier.sourceId, secretValue);
50+
this.#cachedSecretValue.set(identifierKey, secretValue);
4951
return secretValue;
5052
}
5153

5254
clearCache(): void {
53-
this.#cachedSecretValue.clear();
55+
// If the secret identifier has specified a version, it is not removed from the cache.
56+
// 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.
57+
for (const key of this.#cachedSecretValue.keys()) {
58+
if (key.endsWith("\n")) {
59+
this.#cachedSecretValue.delete(key);
60+
}
61+
}
5462
}
5563

5664
async #getSecretValueFromKeyVault(secretIdentifier: KeyVaultSecretIdentifier): Promise<unknown> {

0 commit comments

Comments
 (0)