| 1 | import { PlatformSecretStore, SecretStore } from "../interfaces/index.js"; |
| 2 | import { |
| 3 | SecretLocation, |
| 4 | encodeSecretLocation, |
| 5 | } from "../interfaces/SecretResult.js"; |
| 6 | |
| 7 | export async function resolveSecretLocationInProxy( |
| 8 | secretLocaton: SecretLocation, |
| 9 | platformSecretStore: PlatformSecretStore, |
| 10 | environmentSecretStore?: SecretStore, |
| 11 | ): Promise<string> { |
| 12 | // 1. Check environment variables (if supported) |
| 13 | if (environmentSecretStore) { |
| 14 | const envSecretValue = await environmentSecretStore.get( |
| 15 | secretLocaton.secretName, |
| 16 | ); |
| 17 | if (envSecretValue) { |
| 18 | return envSecretValue; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // 2. Get from secret location |
| 23 | const platformSecret = |
| 24 | await platformSecretStore.getSecretFromSecretLocation(secretLocaton); |
| 25 | if (platformSecret) { |
| 26 | return platformSecret; |
| 27 | } |
| 28 | |
| 29 | throw new Error( |
| 30 | `Could not resolve secret with location ${encodeSecretLocation(secretLocaton)}`, |
| 31 | ); |
| 32 | } |