使用 JavaScript 列出或查找 Azure Key Vault 中的机密

使用相应的编程身份验证凭据创建 SecretClient,然后使用客户端从 Azure Key Vault 查找机密。

所有列表方法都返回一个可迭代对象。 你可以获取列表中的所有项,或链接 byPage 方法以一次迭代一页项。

获得机密的属性后,可以使用 getSecret 方法获取机密的值。

列出所有机密

若要列出 Azure Key Vault 中的所有机密,请使用 listPropertiesOfSecrets 方法获取当前机密的属性。

for await (const secretProperties of client.listPropertiesOfSecrets()){

  // do something with properties
  console.log(`Secret name: ${secretProperties.name}`);

}

此方法返回 SecretProperties 对象。

按页列出所有机密

若要列出 Azure Key Vault 中的所有机密,请使用 listPropertiesOfSecrets 方法通过设置 PageSettings 对象一次获取一个页面的机密属性。

// 5 secrets per page
const maxResults = 5;
let pageCount = 1;
let itemCount=1;

// loop through all secrets
for await (const page of client.listPropertiesOfSecrets().byPage({ maxPageSize: maxResults })) {

  let itemOnPageCount = 1;

  // loop through each secret on page
  for (const secretProperties of page) {

    console.log(`Page:${pageCount++}, item:${itemOnPageCount++}:${secretProperties.name}`);

    itemCount++;
  }
}
console.log(`Total # of secrets:${itemCount}`);

此方法返回 SecretProperties 对象。

列出某个机密的所有版本

若要列出 Azure Key Vault 中某个机密的所有版本,请使用 listPropertiesOfSecretVersions 方法。

for await (const secretProperties of client.listPropertiesOfSecretVersions(secretName)) {

  // do something with version's properties
  console.log(`Version created on: ${secretProperties.createdOn.toString()}`);
}

此方法返回 SecretProperties 对象。

列出已删除的机密

若要列出 Azure Key Vault 中所有已删除的机密,请使用 listDeletedSecrets 方法。

// 5 secrets per page
const maxResults = 5;
let pageCount = 1;
let itemCount=1;

// loop through all secrets
for await (const page of client.listDeletedSecrets().byPage({ maxPageSize: maxResults })) {

  let itemOnPageCount = 1;

  // loop through each secret on page
  for (const secretProperties of page) {

    console.log(`Page:${pageCount++}, item:${itemOnPageCount++}:${secretProperties.name}`);

    itemCount++;
  }
}
console.log(`Total # of secrets:${itemCount}`);

secretProperties 对象是一个 DeletedSecret 对象。

按属性查找机密

若要查找与属性名称/值匹配的当前(最新)机密版本,请遍历所有机密并比较属性。 以下 JavaScript 代码查找所有已启用的机密。

此代码在遍历所有机密时使用以下方法:


const secretsFound = [];

const propertyName = "enabled"
const propertyValue = false;

for await (const secretProperties of client.listPropertiesOfSecrets()){

  if(propertyName === 'tags'){
    if(JSON.stringify(secretProperties.tags) === JSON.stringify(propertyValue)){
      secretsFound.push( secretProperties.name )
    }
  } else {
    if(secretProperties[propertyName].toString() === propertyValue.toString()){
      secretsFound.push( secretProperties.name )
    }
  }
}

console.log(secretsFound)
/*
[
  'my-secret-1683734823721',
  'my-secret-1683735278751',
  'my-secret-1683735523489',
  'my-secret-1684172237551'
]
*/

按属性查找版本

若要查找与属性名称/值匹配的所有版本,请遍历所有机密版本并比较属性。

此代码在嵌套循环中使用以下方法:

const secretsFound = [];

const propertyName = 'createdOn';
const propertyValue = 'Mon May 15 2023 20:52:37 GMT+0000 (Coordinated Universal Time)';

for await (const { name } of client.listPropertiesOfSecrets()){

  console.log(`Secret name: ${name}`);

  for await (const secretProperties of client.listPropertiesOfSecretVersions(name)) {

    console.log(`Secret version ${secretProperties.version}`);

    if(propertyName === 'tags'){
      if(JSON.stringify(secretProperties.tags) === JSON.stringify(propertyValue)){
        console.log(`Tags match`);
        secretsFound.push({ name: secretProperties.name, version: secretProperties.version });
      }
    } else {
      if(secretProperties[propertyName].toString() === propertyValue.toString()){
        console.log(`${propertyName} matches`);
        secretsFound.push({ name: secretProperties.name, version: secretProperties.version });
      }
    }
  }
}

console.log(secretsFound);
/*
[
  {
    name: 'my-secret-1684183956189',
    version: '93beaec3ff614be9a67cd2f4ef4d90c5'
  }
]
*/

后续步骤