JavaScript を使用してAzure Key Vaultでシークレットを一覧表示または検索する

適切なプログラミング認証資格情報>/c0>>を使用してSecretClient<を作成し、クライアントを使用してAzure Key Vaultからシークレットを検索します。

すべてのリスト メソッドは、iterable を返します。 リスト内のすべての項目を取得したり、 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 コードは、有効になっているすべてのシークレットを検索します。

このコードでは、すべてのシークレットのループで次のメソッドを使用します。

  • listPropertiesOfSecrets() - シークレットごとに最新バージョンのプロパティ オブジェクトを返します

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

次のステップ