次の方法で共有


JavaScript を使用して Azure Key Vault でキーのプロパティを作成、ローテーション、更新する

適切なプログラムの認証資格情報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 オブジェクトには、新しいバージョン ID が含まれています。

次のステップ