使用適當的程式設計驗證認證來建立 KeyClient,然後使用用戶端在 Azure Key Vault 中設定、更新及輪替金鑰。
輪替金鑰表示建立新版的金鑰,並將該版本設定為最新版本。 舊版不會遭到刪除,但不再是作用中版本。
使用輪替原則建立金鑰
若要在 Azure Key Vault 中建立金鑰,請使用 KeyClient 類別的 createKey 方法。 使用選擇性 createKeyOptions 物件設定任何屬性。 建立金鑰之後,請使用輪替原則更新金鑰。
KeyVaultKey 便會傳回。 使用 updateKeyRotationPolicy 搭配原則來更新金鑰,其中包含通知。
便利 create 方法適用於下列金鑰類型,而這些類型會設定與該金鑰類型相關聯的屬性:
// Azure 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.net`,
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 方法。 這會建立新版的金鑰,並將該版本設定為作用中版本。
// Azure 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.net`,
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 屬性都維持不變。 這不會變更金鑰值。
// Azure 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.net`,
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 物件包含新版本識別碼。