| | | 1 | | import type { SecretClient } from '@azure/keyvault-secrets'; |
| | | 2 | | import type { SecretExistence } from '../../domain/contract-validation-result.js'; |
| | | 3 | | import type { ISecretExistenceChecker } from '../../domain/ports/secret-existence-checker.js'; |
| | | 4 | | import type { ISecretProvider } from '../../domain/ports/secret-provider.js'; |
| | | 5 | | import { describeError } from '../describe-error.js'; |
| | | 6 | | import { toAzureSecretName } from './azure-secret-name.js'; |
| | | 7 | | |
| | | 8 | | /** |
| | | 9 | | * {@link ISecretProvider} backed by Azure Key Vault. |
| | | 10 | | * |
| | | 11 | | * Secrets are fetched in parallel. Secrets that return HTTP 404 |
| | | 12 | | * are treated as missing and silently omitted from the result. |
| | | 13 | | * Each reference is mapped to its Key Vault name with |
| | | 14 | | * {@link toAzureSecretName}, the same rule the CLI uses. |
| | | 15 | | */ |
| | | 16 | | export class AzureKeyVaultSecretProvider |
| | | 17 | | implements ISecretProvider, ISecretExistenceChecker |
| | | 18 | | { |
| | | 19 | | private readonly secretClient: SecretClient; |
| | | 20 | | |
| | | 21 | | constructor(secretClient: SecretClient) { |
| | 16 | 22 | | if (!secretClient) { |
| | 1 | 23 | | throw new Error('secretClient cannot be null'); |
| | | 24 | | } |
| | 15 | 25 | | this.secretClient = secretClient; |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | async getSecrets(names: string[]): Promise<Map<string, string>> { |
| | 9 | 29 | | const result = new Map<string, string>(); |
| | 9 | 30 | | if (names.length === 0) { |
| | 1 | 31 | | return result; |
| | | 32 | | } |
| | | 33 | | |
| | 8 | 34 | | for (const name of names) { |
| | 14 | 35 | | if (!name?.trim()) { |
| | 1 | 36 | | throw new Error('Secret name cannot be null or empty'); |
| | | 37 | | } |
| | | 38 | | } |
| | 7 | 39 | | const secretNames = names.map(toAzureSecretName); |
| | | 40 | | |
| | 7 | 41 | | const entries = await Promise.all( |
| | | 42 | | names.map(async (name, index) => { |
| | 12 | 43 | | const value = await this.fetchSecret(secretNames[index]); |
| | 11 | 44 | | return [name, value] as const; |
| | | 45 | | }), |
| | | 46 | | ); |
| | | 47 | | |
| | 6 | 48 | | for (const [name, value] of entries) { |
| | 11 | 49 | | if (value !== null) { |
| | 9 | 50 | | result.set(name, value); |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | |
| | 6 | 54 | | return result; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /** |
| | | 58 | | * Lists the vault's secret names once, which reads metadata only, and |
| | | 59 | | * matches every name against it the way Key Vault does, ignoring case. A |
| | | 60 | | * disabled secret cannot be read, so it counts as missing. Needs the `List` |
| | | 61 | | * secret permission (access policies) or |
| | | 62 | | * `Microsoft.KeyVault/vaults/secrets/readMetadata/action` (Azure RBAC, |
| | | 63 | | * included in Key Vault Secrets User). |
| | | 64 | | */ |
| | | 65 | | async checkSecretsExist( |
| | | 66 | | names: readonly string[], |
| | | 67 | | ): Promise<ReadonlyMap<string, SecretExistence>> { |
| | 9 | 68 | | const results = new Map<string, SecretExistence>(); |
| | 9 | 69 | | const distinct = [...new Set(names)]; |
| | 9 | 70 | | if (distinct.length === 0) { |
| | 1 | 71 | | return results; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | let enabled: Set<string>; |
| | 8 | 75 | | try { |
| | 8 | 76 | | enabled = await this.listEnabledSecretNames(); |
| | | 77 | | } catch (error) { |
| | 4 | 78 | | const existence = toExistence(error); |
| | 4 | 79 | | for (const name of distinct) { |
| | 8 | 80 | | results.set(name, existence); |
| | | 81 | | } |
| | 4 | 82 | | return results; |
| | | 83 | | } |
| | 4 | 84 | | for (const name of distinct) { |
| | 7 | 85 | | const secretName = toAzureSecretName(name).toLowerCase(); |
| | 7 | 86 | | results.set(name, { |
| | | 87 | | status: enabled.has(secretName) ? 'exists' : 'missing', |
| | | 88 | | }); |
| | | 89 | | } |
| | 4 | 90 | | return results; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | private async listEnabledSecretNames(): Promise<Set<string>> { |
| | 8 | 94 | | const enabled = new Set<string>(); |
| | 8 | 95 | | for await (const properties of this.secretClient.listPropertiesOfSecrets()) { |
| | 7 | 96 | | if (properties.enabled !== false) { |
| | 6 | 97 | | enabled.add(properties.name.toLowerCase()); |
| | | 98 | | } |
| | | 99 | | } |
| | 4 | 100 | | return enabled; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | private async fetchSecret(name: string): Promise<string | null> { |
| | 12 | 104 | | try { |
| | 12 | 105 | | const response = await this.secretClient.getSecret(name); |
| | 9 | 106 | | return response.value ?? null; |
| | | 107 | | } catch (error: unknown) { |
| | 3 | 108 | | if (isNotFound(error)) { |
| | 2 | 109 | | return null; |
| | | 110 | | } |
| | 1 | 111 | | throw error; |
| | | 112 | | } |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | function isNotFound(error: unknown): boolean { |
| | 3 | 117 | | return ( |
| | | 118 | | typeof error === 'object' && |
| | | 119 | | error !== null && |
| | | 120 | | 'statusCode' in error && |
| | | 121 | | (error as { statusCode: number }).statusCode === 404 |
| | | 122 | | ); |
| | | 123 | | } |
| | | 124 | | |
| | 4 | 125 | | const AUTHENTICATION_ERROR_NAMES = new Set([ |
| | | 126 | | 'CredentialUnavailableError', |
| | | 127 | | 'AuthenticationError', |
| | | 128 | | 'AuthenticationRequiredError', |
| | | 129 | | 'AggregateAuthenticationError', |
| | | 130 | | ]); |
| | | 131 | | |
| | | 132 | | function toExistence(error: unknown): SecretExistence { |
| | 4 | 133 | | const reason = describeError(error); |
| | | 134 | | const statusCode = |
| | 4 | 135 | | typeof error === 'object' && error !== null && 'statusCode' in error |
| | | 136 | | ? error.statusCode |
| | | 137 | | : undefined; |
| | | 138 | | const name = |
| | 4 | 139 | | typeof error === 'object' && error !== null && 'name' in error |
| | | 140 | | ? String(error.name) |
| | | 141 | | : ''; |
| | 4 | 142 | | if (statusCode === 403) { |
| | 1 | 143 | | return { status: 'access_denied', reason }; |
| | | 144 | | } |
| | 3 | 145 | | if (statusCode === 401 || AUTHENTICATION_ERROR_NAMES.has(name)) { |
| | 2 | 146 | | return { status: 'authentication_failed', reason }; |
| | | 147 | | } |
| | 1 | 148 | | return { status: 'provider_error', reason }; |
| | | 149 | | } |