metadata:
  id: 7bb02abe-d669-5058-a2d6-6ce5ee2dc2be
  name: Ensure that the Expiration Date is set for all Keys in Non-RBAC Key Vaults.
  profile_applicability: '* Level 1'
  description: |-
    Ensure that all Keys in Non Role Based Access Control (RBAC) Azure Key Vaults have an expiration date set.
  rationale: |-
    Azure Key Vault enables users to store and use cryptographic keys within the Microsoft Azure environment.
    The `exp` (expiration date) attribute identifies the expiration date on or after which the key MUST NOT be used for a cryptographic operation.
    By default, keys never expire.
    It is thus recommended that keys be rotated in the key vault and set an explicit expiration date for all keys.
    This ensures that the keys cannot be used beyond their assigned lifetimes.
  audit: |-
    **From Azure Portal:**

    1. Go to `Key vaults`.
    2. For each Key vault, click on `Keys`.
    3. In the main pane, ensure that the status of the key is `Enabled`.
    4. For each enabled key, ensure that an appropriate `Expiration date` is set.

    **From Azure CLI:**

    Get a list of all the key vaults in your Azure environment by running the following command:

    ```
    az keyvault list
    ```

    For each key vault, ensure that the output of the below command contains Key ID (kid), enabled status as `true` and Expiration date (expires) is not empty or null:

    ```
    az keyvault key list --vault-name <KEYVAULTNAME> --query '[*].{"kid":kid,"enabled":attributes.enabled,"expires":attributes.expires}' 
    ```

    **From PowerShell:**

    Retrieve a list of Azure Key vaults:

    ```
    Get-AzKeyVault
    ```

    For each Key vault, run the following command to determine which vaults are configured to not use RBAC:

    ```
    Get-AzKeyVault -VaultName <Vault Name>
    ```

    For each Key vault with the `EnableRbacAuthorizatoin` setting set to `False` or empty, run the following command.

    ```
    Get-AzKeyVaultKey -VaultName <Vault Name>
    ```

    Make sure the `Expires` setting is configured with a value as appropriate wherever the `Enabled` setting is set to `True`.
  remediation: |-
    **From Azure Portal:**

    1. Go to `Key vaults`.
    2. For each Key vault, click on `Keys`.
    3. In the main pane, ensure that the status of the key is `Enabled`.
    4. For each enabled key, ensure that an appropriate `Expiration date` is set.

    **From Azure CLI:**

    Update the `Expiration date` for the key using the below command:

    ```
    az keyvault key set-attributes --name <keyName> --vault-name <vaultName> --expires Y-m-d'T'H:M:S'Z'
    ```

    **Note:**
    To view the expiration date on all keys in a Key Vault using Microsoft API, the "List" Key permission is required.

    To update the expiration date for the keys:
    5. Go to Key vault, click on `Access policies`.
    6. Click on `Create` and add an access policy with the `Update` permission (in the Key Permissions - Key Management Operations section).

    **From PowerShell**

    ```
    Set-AzKeyVaultKeyAttribute -VaultName <Vault Name> -Name <Key Name> -Expires <DateTime>
    ```
  impact: |-
    Keys cannot be used beyond their assigned expiration dates respectively. Keys need to be rotated periodically wherever they are used.
  default_value: ''
  references: |-
    1. https://docs.microsoft.com/en-us/azure/key-vault/key-vault-whatis
    2. https://docs.microsoft.com/en-us/rest/api/keyvault/about-keys--secrets-and-certificates#key-vault-keys
    3. https://docs.microsoft.com/en-us/security/benchmark/azure/security-controls-v3-data-protection#dp-6-use-a-secure-key-management-process
    4. https://docs.microsoft.com/en-us/powershell/module/az.keyvault/set-azkeyvaultkeyattribute?view=azps-0.10.0
  section: Key Vault
  version: '1.0'
  tags:
  - CIS
  - AZURE
  - CIS 8.2
  - Key Vault
  benchmark:
    name: CIS Microsoft Azure Foundations
    version: v2.0.0
    id: cis_azure
    rule_number: '8.2'
    posture_type: cspm
