Control: 8.8 Ensure Automatic Key Rotation is Enabled Within Azure Key Vault for the Supported Services
Description
Automatic Key Rotation is available in Public Preview. The currently supported applications are Key Vault, Managed Disks, and Storage accounts accessing keys within Key Vault. The number of supported applications will incrementally increased.
Once set up, Automatic Private Key Rotation removes the need for manual administration when keys expire at intervals determined by your organization's policy. The recommended key lifetime is 2 years. Your organization should determine its own key expiration policy.
Remediation
Note: Azure CLI and Powershell use ISO8601 flags to input timespans. Every timespan input will be in the format P<timespanInISO8601Format>(Y,M,D). The leading P is required with it denoting period. The (Y,M,D) are for the duration of Year, Month,and Day respectively. A time frame of 2 years, 2 months, 2 days would be (P2Y2M2D).
From Azure Portal
- From Azure Portal select the Portal Menu in the top left.
 - Select Key Vaults.
 - Select the Key Vault you wish t oaudit.
 - Under the 
Settingsheading selectKeys. - Select the key you wish to audit within this vault and repeat the next steps for each.
 - In the top row select 
Rotation policy. - Next to 
Set key rotation policyselect the linkNot Configured. - Select the 
Expiry Timein for your need. - In this menu select 
Enablein the rowEnable auto rotation - Set the 
Rotation OptiontoAutomatically renew.. To automatically rotate keys, set the rotation time to or near theExpiry Time. - If desired set the 
Notification durationto a duration before the expiration. 
From Azure CLI
Run the following command for each key to update its policy to be auto-rotated:
az keyvault key rotation-policy update -n <keyName> --vault-name <vaultName> --value <path/to/policy.json>
Note: It is easiest to supply the policy flags in a .json file. An examplejson file would be:
{ "lifetimeActions": [  {    "trigger": {    "timeAfterCreate": "<timespanInISO8601Format>",    "timeBeforeExpiry" : null    },    "action": {    "type": "Rotate"    }  },  {    "trigger": {    "timeBeforeExpiry" : "<timespanInISO8601Format>"    },    "action": {    "type": "Notify"    }  } ],  "attributes": {  "expiryTime": "<timespanInISO8601Format>"  }}
From Powershell
Run the following command for each key to update its policy:
Set-AzKeyVaultKeyRotationPolicy -VaultName test-kv -Name test-key -PolicyPathrotation_policy.json
Note: It is easiest to supply the policy flags in a .json file. An example json file would be:
<#rotation_policy.json{ "lifetimeActions": [  {    "trigger": {    "timeAfterCreate": "P<timespanInISO8601Format>M",    "timeBeforeExpiry": null    },    "action": {    "type": "Rotate"    }  },  {    "trigger": {    "timeBeforeExpiry": "P<timespanInISO8601Format>D"    },    "action": {    "type": "Notify"    }  } ],  "attributes": {  "expiryTime": "P<timespanInISO8601Format>Y"  }}#>
Default Value:
By default, Automatic Key Rotation is not enabled.
Usage
Run the control in your terminal:
powerpipe control run azure_compliance.control.cis_v150_8_8Snapshot and share results via Turbot Pipes:
powerpipe loginpowerpipe control run azure_compliance.control.cis_v150_8_8 --shareSQL
This control uses a named query:
with key_rotation_policy as (  select    id  from    azure_key_vault_key,    jsonb_array_elements(rotation_policy -> 'lifetimeActions') as lifetimeActions  where    lifetimeActions -> 'action' ->> 'type' = 'Rotate'    and lifetimeActions -> 'trigger' -> 'timeAfterCreate' is not null)select  kvk.id as resource,  case    when p.id is not null then 'ok'    else 'alarm'  end as status,  case    when p.id is not null then vault_name || ' key ' || kvk.name || ' automatic rotation enabled.'    else vault_name || ' key ' || kvk.name || ' automatic rotation disabled.'  end as reason    , kvk.resource_group as resource_group  , sub.display_name as subscriptionfrom  azure_key_vault_key kvk  left join key_rotation_policy as p on p.id = kvk.id  left join azure_subscription sub on sub.subscription_id = kvk.subscription_id;