getVaultSecrets.ts 1.1 KB

123456789101112131415161718192021
  1. import { writeFile } from "fs/promises";
  2. import { join } from "path";
  3. import { ConfigFileAuthenticationDetailsProvider, InstancePrincipalsAuthenticationDetailsProviderBuilder } from "oci-common";
  4. import { SecretsClient } from "oci-secrets";
  5. const configPath = process.env.OCI_CONFIG_PATH;
  6. const configProfile = process.env.OCI_CONFIG_PROFILE;
  7. const mountPath = process.env.MOUNT_PATH || "";
  8. const secretName = process.env.SECRET_NAME || "secret";
  9. (async () => {
  10. const authProvider = configPath || configProfile
  11. ? new ConfigFileAuthenticationDetailsProvider(configPath, configProfile)
  12. : await new InstancePrincipalsAuthenticationDetailsProviderBuilder().build();
  13. const vault = new SecretsClient({ authenticationDetailsProvider: authProvider });
  14. const secretResponse = await vault.getSecretBundleByName({ vaultId: "ocid1.vault.oc1.iad.b5rus2i4aadd4.abuwcljrbitufodxbxctfhkdg3g54svlhxszh6kr5odha2pmtzcjc45waw6q", secretName })
  15. const secret = Buffer.from(secretResponse.secretBundle.secretBundleContent?.content ?? "", "base64").toString();
  16. await writeFile(join(mountPath, secretName), secret);
  17. })();