使用相应的编程身份验证凭据创建 KeyClient,然后使用客户端在 Azure Key Vault 中设置、更新和轮换密钥。
轮换密钥意味着创建密钥的新版本并将该版本设置为最新版本。 不会删除之前的版本,但它不再是有效版本。
使用轮换策略创建密钥
若要在 Azure Key Vault 中创建密钥,请使用 KeyClient 类的 createKey 方法。 使用可选的 createKeyOptions 对象设置任何属性。 创建密钥后,使用轮换策略更新密钥。
这会返回一个 KeyVaultKey。 使用 updateKeyRotationPolicy 和包含通知的策略来更新密钥。
可对以下密钥类型使用便利创建方法,这些方法会设置与密钥类型关联的属性:
// Microsoft client libraries
import { DefaultAzureCredential } from '@azure/identity';
import {
  CreateKeyOptions,
  KeyClient,
  KeyRotationPolicyProperties,
  KnownKeyOperations,
  KnownKeyTypes
} from '@azure/keyvault-keys';
// Day/time manipulation
import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration';
dayjs.extend(duration);
// Authenticate to Azure Key Vault
const credential = new DefaultAzureCredential();
const client = new KeyClient(
    `https://${process.env.AZURE_KEYVAULT_NAME}.vault.azure.cn`,
    credential
);
// Name of key
const keyName = `mykey-${Date.now().toString()}`;
// Set key options
const keyOptions: CreateKeyOptions = {
enabled: true,
expiresOn: dayjs().add(1, 'year').toDate(),
exportable: false,
tags: {
    project: 'test-project'
},
keySize: 2048,
keyOps: [
    KnownKeyOperations.Encrypt,
    KnownKeyOperations.Decrypt
    // KnownKeyOperations.Verify,
    // KnownKeyOperations.Sign,
    // KnownKeyOperations.Import,
    // KnownKeyOperations.WrapKey,
    // KnownKeyOperations.UnwrapKey
]
};
// Set key type
const keyType = KnownKeyTypes.RSA; //  'EC', 'EC-HSM', 'RSA', 'RSA-HSM', 'oct', 'oct-HSM'
// Create key
const key = await client.createKey(keyName, keyType, keyOptions);
if (key) {
    // Set rotation policy properties: KeyRotationPolicyProperties
    const rotationPolicyProperties: KeyRotationPolicyProperties = {
        expiresIn: 'P90D',
        lifetimeActions: [
        {
            action: 'Rotate',
            timeAfterCreate: 'P30D'
        },
        {
            action: 'Notify',
            timeBeforeExpiry: dayjs.duration({ days: 7 }).toISOString()
        }
    ]};
    // Set rotation policy: KeyRotationPolicy
    const keyRotationPolicy = await client.updateKeyRotationPolicy(
        key.name,
        rotationPolicyProperties
    );
    console.log(keyRotationPolicy);
}
手动轮换密钥
如果需要轮换密钥,请使用 rotateKey 方法。 这会创建密钥的新版本,并将该版本设置为有效版本。
// Microsoft client libraries
import { DefaultAzureCredential } from '@azure/identity';
import {
  KeyClient
} from '@azure/keyvault-keys';
// Authenticate to Azure Key Vault
const credential = new DefaultAzureCredential();
const client = new KeyClient(
    `https://${process.env.AZURE_KEYVAULT_NAME}.vault.azure.cn`,
    credential
);
// Get existing key
let key = await client.getKey(`MyKey`);
console.log(key);
if(key?.name){
    // rotate key
    key = await client.rotateKey(key.name);
    console.log(key);
}
更新密钥属性
使用 updateKeyProperties 更新最新版本的密钥的属性,或者使用 updateKeyProperties 更新特定版本的密钥。 未指定的 UpdateKeyPropertiesOptions 属性都保持不变。 这不会更改密钥值。
// Microsoft client libraries
import { DefaultAzureCredential } from '@azure/identity';
import {
  KeyClient
} from '@azure/keyvault-keys';
// Authenticate to Azure Key Vault
const credential = new DefaultAzureCredential();
const client = new KeyClient(
    `https://${process.env.AZURE_KEYVAULT_NAME}.vault.azure.cn`,
    credential
);
// Get existing key
const key = await client.getKey('MyKey');
if (key) {
    // 
    const updateKeyPropertiesOptions = {
        enabled: false,
        // expiresOn,
        // keyOps,
        // notBefore, 
        // releasePolicy, 
        tags: { 
            ...key.properties.tags, subproject: 'Health and wellness' 
        }
    }
    // update properties of latest version
    await client.updateKeyProperties(
        key.name,
        updateKeyPropertiesOptions
    );
    // update properties of specific version
    await client.updateKeyProperties(
        key.name,
        key?.properties?.version,
        {
            enabled: true
        }
    );
}
更新密钥值
若要更新密钥值,请使用 rotateKey 方法。 请确保传递的新值包含当前版本的密钥中要保留的所有属性。 在对 rotateKey 的其他调用中未设置的当前属性都将丢失。
这会生成新版本的密钥。 返回的 KeyVaultKey 对象包括新版本 ID。