共用方式為


使用 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'
  }
]
*/

後續步驟