Membuat, memutar, dan memperbarui properti kunci di Azure Key Vault dengan JavaScript

Buat KeyClient dengan kredensial autentikasi terprogram yang sesuai, lalu gunakan klien untuk mengatur, memperbarui, dan memutar kunci di Azure Key Vault.

Untuk memutar kunci berarti membuat versi baru kunci dan mengatur versi tersebut sebagai versi terbaru. Versi sebelumnya tidak dihapus, tetapi tidak lagi menjadi versi aktif.

Membuat kunci dengan kebijakan rotasi

Untuk membuat kunci di Azure Key Vault, gunakan metode createKey dari kelas KeyClient . Atur properti apa pun dengan objek createKeyOptions opsional. Setelah kunci dibuat, perbarui kunci dengan kebijakan rotasi.

KeyVaultKey dikembalikan. Perbarui kunci menggunakan updateKeyRotationPolicy dengan kebijakan, yang mencakup pemberitahuan.

Metode pembuatan kenyamanan tersedia untuk jenis kunci berikut, yang mengatur properti yang terkait dengan jenis kunci tersebut:

// 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);
}

Memutar kunci secara manual

Saat Anda perlu memutar kunci, gunakan metode rotateKey . Ini membuat versi baru kunci dan mengatur versi tersebut sebagai versi aktif.

// 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);
}

Memperbarui properti kunci

Perbarui properti kunci versi terbaru dengan updateKeyProperties atau perbarui versi kunci tertentu dengan updateKeyProperties. Properti UpdateKeyPropertiesOptions apa pun yang tidak ditentukan tidak berubah. Ini tidak mengubah nilai kunci.

// 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
        }
    );
}

Memperbarui nilai kunci

Untuk memperbarui nilai kunci, gunakan metode rotateKey . Pastikan untuk meneruskan nilai baru dengan semua properti yang ingin Anda simpan dari versi kunci saat ini. Properti saat ini yang tidak diatur dalam panggilan tambahan untuk rotateKey akan hilang.

Ini menghasilkan versi baru kunci. Objek KeyVaultKey yang dikembalikan menyertakan ID versi baru.

Langkah berikutnya