|
5 | 5 | import asyncio |
6 | 6 | import os |
7 | 7 | from azure.identity.aio import DefaultAzureCredential |
8 | | -from azure.keyvault.keys import KeyRotationLifetimeAction, KeyRotationPolicyAction |
| 8 | +from azure.keyvault.keys import KeyRotationLifetimeAction, KeyRotationPolicy, KeyRotationPolicyAction |
9 | 9 | from azure.keyvault.keys.aio import KeyClient |
10 | 10 |
|
11 | 11 | # ---------------------------------------------------------------------------------------------------------- |
@@ -48,34 +48,53 @@ async def run_sample(): |
48 | 48 | key = await client.create_rsa_key(key_name) |
49 | 49 | print("\nCreated a key; new version is {}".format(key.properties.version)) |
50 | 50 |
|
51 | | - # Set the key's automated rotation policy to rotate the key two months after the key was created |
52 | | - actions = [KeyRotationLifetimeAction(KeyRotationPolicyAction.ROTATE, time_after_create="P2M")] |
53 | | - updated_policy = await client.update_key_rotation_policy(key_name, lifetime_actions=actions) |
| 51 | + # Set the key's automated rotation policy to rotate the key two months after the key was created. |
| 52 | + # If you pass an empty KeyRotationPolicy() as the `policy` parameter, the rotation policy will be set to the |
| 53 | + # default policy. Any keyword arguments will update specified properties of the policy. |
| 54 | + actions = [KeyRotationLifetimeAction(KeyRotationPolicyAction.rotate, time_after_create="P2M")] |
| 55 | + updated_policy = await client.update_key_rotation_policy( |
| 56 | + key_name, KeyRotationPolicy(), expires_in="P90D", lifetime_actions=actions |
| 57 | + ) |
| 58 | + assert updated_policy.expires_in == "P90D" |
54 | 59 |
|
55 | | - # The created policy should only have one action |
56 | | - assert len(updated_policy.lifetime_actions) == 1, "There should be exactly one rotation policy action" |
57 | | - policy_action = updated_policy.lifetime_actions[0] |
| 60 | + # The updated policy should have the specified lifetime action |
| 61 | + policy_action = None |
| 62 | + for i in range(len(updated_policy.lifetime_actions)): |
| 63 | + if updated_policy.lifetime_actions[i].action == KeyRotationPolicyAction.rotate: |
| 64 | + policy_action = updated_policy.lifetime_actions[i] |
| 65 | + assert policy_action, "The specified action should exist in the key rotation policy" |
| 66 | + assert policy_action.time_after_create == "P2M", "The action should have the specified time_after_create" |
| 67 | + assert policy_action.time_before_expiry is None, "The action shouldn't have a time_before_expiry" |
58 | 68 | print( |
59 | 69 | "\nCreated a new key rotation policy: {} after {}".format(policy_action.action, policy_action.time_after_create) |
60 | 70 | ) |
61 | 71 |
|
62 | 72 | # Get the key's current rotation policy |
63 | 73 | current_policy = await client.get_key_rotation_policy(key_name) |
64 | | - policy_action = current_policy.lifetime_actions[0] |
| 74 | + policy_action = None |
| 75 | + for i in range(len(current_policy.lifetime_actions)): |
| 76 | + if current_policy.lifetime_actions[i].action == KeyRotationPolicyAction.rotate: |
| 77 | + policy_action = current_policy.lifetime_actions[i] |
65 | 78 | print("\nCurrent rotation policy: {} after {}".format(policy_action.action, policy_action.time_after_create)) |
66 | 79 |
|
67 | | - # Update the key's automated rotation policy to notify 30 days before the key expires |
68 | | - new_actions = [KeyRotationLifetimeAction(KeyRotationPolicyAction.NOTIFY, time_before_expiry="P30D")] |
69 | | - # You may also specify the duration after which the newly rotated key will expire |
70 | | - # In this example, any new key versions will expire after 90 days |
71 | | - new_policy = await client.update_key_rotation_policy(key_name, expires_in="P90D", lifetime_actions=new_actions) |
| 80 | + # Update the key's automated rotation policy to notify 10 days before the key expires |
| 81 | + new_actions = [KeyRotationLifetimeAction(KeyRotationPolicyAction.notify, time_before_expiry="P10D")] |
| 82 | + # To preserve an existing rotation policy, pass in the existing policy as the `policy` parameter. |
| 83 | + # Any property specified as a keyword argument will be overridden completely by the provided value. |
| 84 | + # In this case, the rotate action we created earlier will be removed from the policy. |
| 85 | + new_policy = await client.update_key_rotation_policy(key_name, current_policy, lifetime_actions=new_actions) |
| 86 | + assert new_policy.expires_in == "P90D", "The key's expiry time should have been preserved" |
72 | 87 |
|
73 | | - # The updated policy should only have one action |
74 | | - assert len(new_policy.lifetime_actions) == 1, "There should be exactly one rotation policy action" |
75 | | - policy_action = new_policy.lifetime_actions[0] |
76 | | - print( |
77 | | - "\nUpdated rotation policy: {} {} before expiry".format(policy_action.action, policy_action.time_before_expiry) |
78 | | - ) |
| 88 | + # The updated policy should include the new notify action |
| 89 | + notify_action = None |
| 90 | + for i in range(len(new_policy.lifetime_actions)): |
| 91 | + if new_policy.lifetime_actions[i].action == KeyRotationPolicyAction.notify: |
| 92 | + notify_action = new_policy.lifetime_actions[i] |
| 93 | + |
| 94 | + assert notify_action, "The specified action should exist in the key rotation policy" |
| 95 | + assert notify_action.time_after_create is None, "The action shouldn't have a time_after_create" |
| 96 | + assert notify_action.time_before_expiry == "P10D", "The action should have the specified time_before_expiry" |
| 97 | + print("\nNew policy action: {} {} before expiry".format(notify_action.action, notify_action.time_before_expiry)) |
79 | 98 |
|
80 | 99 | # Finally, you can rotate a key on-demand by creating a new version of the key |
81 | 100 | rotated_key = await client.rotate_key(key_name) |
|
0 commit comments