Skip to content

Commit

Permalink
feat(config): add support for custom CA certificate when calling Key …
Browse files Browse the repository at this point in the history
…Vault
  • Loading branch information
ttyS0e committed Feb 8, 2024
1 parent a44d34f commit ac95673
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { SecretClient } = require("@azure/keyvault-secrets");
const { DefaultAzureCredential } = require("@azure/identity");
const fs = require("fs")

const logAppName = '[azure-keyvault-secrets]';

Expand All @@ -13,7 +14,7 @@ const secretsCache = {
},
};

const getKeyVaultSecret = async function (keyVaultName, secretName) {
const getKeyVaultSecret = async function (keyVaultName, secretName, keyVaultCACert) {
const cacheSecretName = `${keyVaultName}${secretName}`;
const cachedSecretValue = secretsCache.getSecret(cacheSecretName);

Expand All @@ -24,7 +25,16 @@ const getKeyVaultSecret = async function (keyVaultName, secretName) {

const credential = new DefaultAzureCredential();
const url = `https://${keyVaultName}.vault.azure.net`;
const client = new SecretClient(url, credential);

const opts = {}

if (keyVaultCACert) {
console.log(logAppName, `using custom CA certificate at ${keyVaultCACert} for Azure KeyVault`)
opts.tlsOptions = {
ca: [fs.readFileSync(keyVaultCACert)]
}
}
const client = new SecretClient(url, credential, opts);

try {
const secret = await client.getSecret(secretName);
Expand Down Expand Up @@ -56,13 +66,14 @@ const secretTag = {
async run(context, secretName) {

const keyVaultName = await context.context.AZURE_KEYVAULT;
const keyVaultCACert = await context.context.AZURE_KEYVAULT_CA_CERT;

if (typeof keyVaultName === 'undefined') {
console.error(logAppName, 'missing AZURE_KEYVAULT environment variable');
return '';
}

const secretValue = getKeyVaultSecret(keyVaultName, secretName);
const secretValue = getKeyVaultSecret(keyVaultName, secretName, keyVaultCACert);

return secretValue;
}
Expand Down

0 comments on commit ac95673

Please sign in to comment.