Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Creare SecretClient con le credenziali di autenticazione a livello di codice appropriate, quindi usare il client per trovare un segreto da Azure Key Vault.
Tutti i metodi di elenco restituiscono un'iterabile. È possibile ottenere tutti gli elementi nell'elenco o concatenare il metodo byPage per scorrere una pagina di elementi alla volta.
Dopo aver ottenuto le proprietà di un segreto, è possibile usare il metodo getSecret per ottenere il valore del segreto.
Elencare tutti i segreti
Per elencare tutti i segreti in Azure Key Vault, usare il metodo listPropertiesOfSecrets per ottenere le proprietà di un segreto corrente.
for await (const secretProperties of client.listPropertiesOfSecrets()){
// do something with properties
console.log(`Secret name: ${secretProperties.name}`);
}
Questo metodo restituisce l'oggetto SecretProperties .
Elencare tutti i segreti per pagina
Per elencare tutti i segreti in Azure Key Vault, usare il metodo listPropertiesOfSecrets per ottenere proprietà segrete una pagina alla volta impostando l'oggetto 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}`);
Questo metodo restituisce l'oggetto SecretProperties .
Elencare tutte le versioni di un segreto
Per elencare tutte le versioni di un segreto in Azure Key Vault, usare il metodo listPropertiesOfSecretVersions .
for await (const secretProperties of client.listPropertiesOfSecretVersions(secretName)) {
// do something with version's properties
console.log(`Version created on: ${secretProperties.createdOn.toString()}`);
}
Questo metodo restituisce l'oggetto SecretProperties .
Elenco dei segreti eliminati
Per elencare tutti i segreti eliminati in Azure Key Vault, usare il metodo 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}`);
L'oggetto secretProperties è un oggetto DeletedSecret .
Trova segreto per proprietà
Per trovare la versione corrente (più recente) di un segreto, che corrisponde a un nome/valore di proprietà, eseguire un ciclo su tutti i segreti e confrontare le proprietà. Il codice JavaScript seguente trova tutti i segreti abilitati.
Questo codice usa il metodo seguente in un ciclo di tutti i segreti:
- listPropertiesOfSecrets(): restituisce l'oggetto proprietà della versione più recente per segreto
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'
]
*/
Trovare versioni per proprietà
Per trovare tutte le versioni che corrispondono a un nome/valore di proprietà, eseguire un ciclo su tutte le versioni segrete e confrontare le proprietà.
Questo codice usa i metodi seguenti in un ciclo annidato:
- listPropertiesOfSecrets(): restituisce l'oggetto proprietà dell'ultima versione per ciascun segreto
- listPropertiesOfSecretVersions(): restituisce tutte le versioni per 1 segreto
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'
}
]
*/