Skip to content

Commit a4aa521

Browse files
authored
Update release dates and key rotation code/samples (Azure#23688)
1 parent ab4c52c commit a4aa521

File tree

12 files changed

+286
-153
lines changed

12 files changed

+286
-153
lines changed

sdk/keyvault/azure-keyvault-administration/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Release History
22

3-
## 4.1.0 (2022-03-25)
3+
## 4.1.0 (2022-03-28)
44

55
### Features Added
66
- Key Vault API version 7.3 is now the default

sdk/keyvault/azure-keyvault-certificates/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Release History
22

3-
## 4.4.0 (2022-03-25)
3+
## 4.4.0 (2022-03-28)
44

55
### Features Added
66
- Key Vault API version 7.3 is now the default

sdk/keyvault/azure-keyvault-keys/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Release History
22

3-
## 4.5.0 (2022-03-25)
3+
## 4.5.0 (2022-03-28)
44

55
### Features Added
66
- Key Vault API version 7.3 is now the default

sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ def update_key_rotation_policy(self, key_name, policy, **kwargs):
815815
]
816816

817817
attributes = self._models.KeyRotationPolicyAttributes(expiry_time=kwargs.pop("expires_in", policy.expires_in))
818-
new_policy = self._models.KeyRotationPolicy(lifetime_actions=lifetime_actions, attributes=attributes)
818+
new_policy = self._models.KeyRotationPolicy(lifetime_actions=lifetime_actions or [], attributes=attributes)
819819
result = self._client.update_key_rotation_policy(
820820
vault_base_url=self._vault_url, key_name=key_name, key_rotation_policy=new_policy
821821
)

sdk/keyvault/azure-keyvault-keys/azure/keyvault/keys/aio/_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ async def update_key_rotation_policy(
798798
]
799799

800800
attributes = self._models.KeyRotationPolicyAttributes(expiry_time=kwargs.pop("expires_in", policy.expires_in))
801-
new_policy = self._models.KeyRotationPolicy(lifetime_actions=lifetime_actions, attributes=attributes)
801+
new_policy = self._models.KeyRotationPolicy(lifetime_actions=lifetime_actions or [], attributes=attributes)
802802
result = await self._client.update_key_rotation_policy(
803803
vault_base_url=self._vault_url, key_name=key_name, key_rotation_policy=new_policy
804804
)

sdk/keyvault/azure-keyvault-keys/samples/key_rotation.py

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# ------------------------------------
55
import os
66
from azure.identity import DefaultAzureCredential
7-
from azure.keyvault.keys import KeyClient, KeyRotationLifetimeAction, KeyRotationPolicyAction
7+
from azure.keyvault.keys import KeyClient, KeyRotationLifetimeAction, KeyRotationPolicy, KeyRotationPolicyAction
88

99
# ----------------------------------------------------------------------------------------------------------
1010
# Prerequisites:
@@ -45,30 +45,51 @@
4545
key = client.create_rsa_key(key_name)
4646
print("\nCreated a key; new version is {}".format(key.properties.version))
4747

48-
# Set the key's automated rotation policy to rotate the key two months after the key was created
49-
actions = [KeyRotationLifetimeAction(KeyRotationPolicyAction.ROTATE, time_after_create="P2M")]
50-
updated_policy = client.update_key_rotation_policy(key_name, lifetime_actions=actions)
48+
# Set the key's automated rotation policy to rotate the key two months after the key was created.
49+
# If you pass an empty KeyRotationPolicy() as the `policy` parameter, the rotation policy will be set to the
50+
# default policy. Any keyword arguments will update specified properties of the policy.
51+
actions = [KeyRotationLifetimeAction(KeyRotationPolicyAction.rotate, time_after_create="P2M")]
52+
updated_policy = client.update_key_rotation_policy(
53+
key_name, KeyRotationPolicy(), expires_in="P90D", lifetime_actions=actions
54+
)
55+
assert updated_policy.expires_in == "P90D"
5156

52-
# The created policy should only have one action
53-
assert len(updated_policy.lifetime_actions) == 1, "There should be exactly one rotation policy action"
54-
policy_action = updated_policy.lifetime_actions[0]
57+
# The updated policy should have the specified lifetime action
58+
policy_action = None
59+
for i in range(len(updated_policy.lifetime_actions)):
60+
if updated_policy.lifetime_actions[i].action == KeyRotationPolicyAction.rotate:
61+
policy_action = updated_policy.lifetime_actions[i]
62+
assert policy_action, "The specified action should exist in the key rotation policy"
63+
assert policy_action.time_after_create == "P2M", "The action should have the specified time_after_create"
64+
assert policy_action.time_before_expiry is None, "The action shouldn't have a time_before_expiry"
5565
print("\nCreated a new key rotation policy: {} after {}".format(policy_action.action, policy_action.time_after_create))
5666

5767
# Get the key's current rotation policy
5868
current_policy = client.get_key_rotation_policy(key_name)
59-
policy_action = current_policy.lifetime_actions[0]
69+
policy_action = None
70+
for i in range(len(current_policy.lifetime_actions)):
71+
if current_policy.lifetime_actions[i].action == KeyRotationPolicyAction.rotate:
72+
policy_action = current_policy.lifetime_actions[i]
6073
print("\nCurrent rotation policy: {} after {}".format(policy_action.action, policy_action.time_after_create))
6174

62-
# Update the key's automated rotation policy to notify 30 days before the key expires
63-
new_actions = [KeyRotationLifetimeAction(KeyRotationPolicyAction.NOTIFY, time_before_expiry="P30D")]
64-
# You may also specify the duration after which the newly rotated key will expire
65-
# In this example, any new key versions will expire after 90 days
66-
new_policy = client.update_key_rotation_policy(key_name, expires_in="P90D", lifetime_actions=new_actions)
75+
# Update the key's automated rotation policy to notify 10 days before the key expires
76+
new_actions = [KeyRotationLifetimeAction(KeyRotationPolicyAction.notify, time_before_expiry="P10D")]
77+
# To preserve an existing rotation policy, pass in the existing policy as the `policy` parameter.
78+
# Any property specified as a keyword argument will be overridden completely by the provided value.
79+
# In this case, the rotate action we created earlier will be removed from the policy.
80+
new_policy = client.update_key_rotation_policy(key_name, current_policy, lifetime_actions=new_actions)
81+
assert new_policy.expires_in == "P90D", "The key's expiry time should have been preserved"
6782

68-
# The updated policy should only have one action
69-
assert len(new_policy.lifetime_actions) == 1, "There should be exactly one rotation policy action"
70-
policy_action = new_policy.lifetime_actions[0]
71-
print("\nUpdated rotation policy: {} {} before expiry".format(policy_action.action, policy_action.time_before_expiry))
83+
# The updated policy should include the new notify action
84+
notify_action = None
85+
for i in range(len(new_policy.lifetime_actions)):
86+
if new_policy.lifetime_actions[i].action == KeyRotationPolicyAction.notify:
87+
notify_action = new_policy.lifetime_actions[i]
88+
89+
assert notify_action, "The specified action should exist in the key rotation policy"
90+
assert notify_action.time_after_create is None, "The action shouldn't have a time_after_create"
91+
assert notify_action.time_before_expiry == "P10D", "The action should have the specified time_before_expiry"
92+
print("\nNew policy action: {} {} before expiry".format(notify_action.action, notify_action.time_before_expiry))
7293

7394
# Finally, you can rotate a key on-demand by creating a new version of the key
7495
rotated_key = client.rotate_key(key_name)

sdk/keyvault/azure-keyvault-keys/samples/key_rotation_async.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import asyncio
66
import os
77
from azure.identity.aio import DefaultAzureCredential
8-
from azure.keyvault.keys import KeyRotationLifetimeAction, KeyRotationPolicyAction
8+
from azure.keyvault.keys import KeyRotationLifetimeAction, KeyRotationPolicy, KeyRotationPolicyAction
99
from azure.keyvault.keys.aio import KeyClient
1010

1111
# ----------------------------------------------------------------------------------------------------------
@@ -48,34 +48,53 @@ async def run_sample():
4848
key = await client.create_rsa_key(key_name)
4949
print("\nCreated a key; new version is {}".format(key.properties.version))
5050

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"
5459

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"
5868
print(
5969
"\nCreated a new key rotation policy: {} after {}".format(policy_action.action, policy_action.time_after_create)
6070
)
6171

6272
# Get the key's current rotation policy
6373
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]
6578
print("\nCurrent rotation policy: {} after {}".format(policy_action.action, policy_action.time_after_create))
6679

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"
7287

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))
7998

8099
# Finally, you can rotate a key on-demand by creating a new version of the key
81100
rotated_key = await client.rotate_key(key_name)

0 commit comments

Comments
 (0)