Compartir a través de


Habilitación y deshabilitación de un secreto en Azure Key Vault con JavaScript

Cree SecretClient con las credenciales de autenticación mediante programación adecuadas y, a continuación, use el cliente para habilitar y deshabilitar un secreto de Azure Key Vault.

Habilitación de un secreto

Para habilitar un secreto en Azure Key Vault, use el método updateSecretProperties de la clase SecretClient .

const name = 'mySecret';
const version= 'd9f2f96f120d4537ba7d82fecd913043'

const properties = await client.updateSecretProperties(
    secretName,
    version,
    { enabled: true }
);

// get secret value
const { value } = await client.getSecret(secretName, version);

Este método devuelve el objeto SecretProperties .

Deshabilitar un nuevo secreto

Para deshabilitar un secreto cuando se crea, use el método setSecret con la opción para habilitado establecida en falso.

const mySecretName = 'mySecret';
const mySecretValue = 'mySecretValue';

// Success
const { name, value, properties } = await client.setSecret(
    mySecretName, 
    mySecretValue, 
    { enabled: false }
);

// Can't read value of disabled secret
try{
    const secret = await client.getSecret(
        mySecretName, 
        properties.version
    );
} catch(err){
    // `Operation get is not allowed on a disabled secret.`
    console.log(err.message);
}

Deshabilitar un secreto existente

Para deshabilitar un secreto existente en Azure Key Vault, use el método updateSecretProperties de la clase SecretClient .

const name = 'mySecret';
const version= 'd9f2f96f120d4537ba7d82fecd913043';

// Success
const properties = await client.updateSecretProperties(
    secretName,
    version,
    { enabled: false }
);

// Can't read value of disabled secret
try{
    const { value } = await client.getSecret(secretName, version);
} catch(err){
    // `Operation get is not allowed on a disabled secret.`
    console.log(err.message);
}

Este método devuelve el objeto SecretProperties .

Pasos siguientes