-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
AzureSecretStore.impl.ts
63 lines (53 loc) · 2.22 KB
/
AzureSecretStore.impl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { DefaultAzureCredential } from '@azure/identity'
import { SecretClient } from '@azure/keyvault-secrets'
import {
type ObjectWithKeysFromStringArray,
SecretStoreBaseClass,
StatusCode,
type StoreBaseConfig,
UnhandledError,
} from '@purista/core'
import type { AzureSecretStoreConfig } from './types.js'
/**
* The secret store adapter for Azure Secrets Manager.
* It will store, retrive, update or remove secrets in Azure Secrets Manager.
*
* For performance reasons, and to reduce costs, the secret values are cached in memory after first fetch.
*
* You can disable the whole caching via config by setting enableCache to false.
* If the cache is enabled, you can set the ttl for cached secret values via config cacheTtl (in ms).
*
* This will return the cached secret if available and if ttl is not exceeded.
* If a secret value exceeds the ttl, it does not automatically get removed from cache.
* It will be removed/overwritten on next get request.
*/
export class AzureSecretStore extends SecretStoreBaseClass<AzureSecretStoreConfig> {
client: SecretClient
constructor(config: StoreBaseConfig<AzureSecretStoreConfig>) {
super('AzureSecretStore', { enableCache: true, ...config })
const credential = new DefaultAzureCredential()
this.client = new SecretClient(this.config.vaultUrl, credential, this.config.options)
}
protected async getSecretImpl<SecretNames extends string[]>(
...secretNames: SecretNames
): Promise<ObjectWithKeysFromStringArray<SecretNames, string | undefined>> {
const result: Record<string, string | undefined> = {}
for (const name of secretNames) {
try {
const response = await this.client.getSecret(name)
result[name] = response?.value
} catch (err) {
result[name] = undefined
this.logger.error({ err })
throw UnhandledError.fromError(err, StatusCode.InternalServerError)
}
}
return result as ObjectWithKeysFromStringArray<SecretNames, string | undefined>
}
protected async removeSecretImpl(secretName: string) {
await this.client.beginDeleteSecret(secretName)
}
protected async setSecretImpl(secretName: string, secretValue: string) {
await this.client.setSecret(secretName, secretValue)
}
}