Skip to content

Commit 92d47eb

Browse files
authored
Added sync and async quickstart guides for key rotation. (Azure#24836)
* Added sync nd async quickstart guides for key rotation. * Applied PR comments.
1 parent 8e20c17 commit 92d47eb

File tree

5 files changed

+143
-12
lines changed

5 files changed

+143
-12
lines changed

sdk/core/azure-core-serializer-avro-apache/src/test/java/com/azure/core/serializer/avro/apache/generatedtestsources/HandOfCards.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static HandOfCards fromByteBuffer(
7171
return DECODER.decode(b);
7272
}
7373

74-
@Deprecated public java.util.List<com.azure.core.serializer.avro.apache.generatedtestsources.PlayingCard> cards;
74+
private java.util.List<com.azure.core.serializer.avro.apache.generatedtestsources.PlayingCard> cards;
7575

7676
/**
7777
* Default constructor. Note that this does not initialize fields
@@ -94,7 +94,7 @@ public HandOfCards(java.util.List<com.azure.core.serializer.avro.apache.generate
9494
public java.lang.Object get(int field$) {
9595
switch (field$) {
9696
case 0: return cards;
97-
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
97+
default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
9898
}
9999
}
100100

@@ -103,7 +103,7 @@ public java.lang.Object get(int field$) {
103103
public void put(int field$, java.lang.Object value$) {
104104
switch (field$) {
105105
case 0: cards = (java.util.List<com.azure.core.serializer.avro.apache.generatedtestsources.PlayingCard>)value$; break;
106-
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
106+
default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
107107
}
108108
}
109109

sdk/core/azure-core-serializer-avro-apache/src/test/java/com/azure/core/serializer/avro/apache/generatedtestsources/LongLinkedList.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public static LongLinkedList fromByteBuffer(
7171
return DECODER.decode(b);
7272
}
7373

74-
@Deprecated public long value;
75-
@Deprecated public com.azure.core.serializer.avro.apache.generatedtestsources.LongLinkedList next;
74+
private long value;
75+
private com.azure.core.serializer.avro.apache.generatedtestsources.LongLinkedList next;
7676

7777
/**
7878
* Default constructor. Note that this does not initialize fields
@@ -98,7 +98,7 @@ public java.lang.Object get(int field$) {
9898
switch (field$) {
9999
case 0: return value;
100100
case 1: return next;
101-
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
101+
default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
102102
}
103103
}
104104

@@ -108,7 +108,7 @@ public void put(int field$, java.lang.Object value$) {
108108
switch (field$) {
109109
case 0: value = (java.lang.Long)value$; break;
110110
case 1: next = (com.azure.core.serializer.avro.apache.generatedtestsources.LongLinkedList)value$; break;
111-
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
111+
default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
112112
}
113113
}
114114

@@ -321,6 +321,7 @@ public com.azure.core.serializer.avro.apache.generatedtestsources.LongLinkedList
321321
* @param value The builder instance that must be set.
322322
* @return This builder.
323323
*/
324+
324325
public com.azure.core.serializer.avro.apache.generatedtestsources.LongLinkedList.Builder setNextBuilder(com.azure.core.serializer.avro.apache.generatedtestsources.LongLinkedList.Builder value) {
325326
clearNext();
326327
nextBuilder = value;

sdk/core/azure-core-serializer-avro-apache/src/test/java/com/azure/core/serializer/avro/apache/generatedtestsources/PlayingCard.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public static PlayingCard fromByteBuffer(
7171
return DECODER.decode(b);
7272
}
7373

74-
@Deprecated public boolean isFaceCard;
75-
@Deprecated public int cardValue;
76-
@Deprecated public com.azure.core.serializer.avro.apache.generatedtestsources.PlayingCardSuit playingCardSuit;
74+
private boolean isFaceCard;
75+
private int cardValue;
76+
private com.azure.core.serializer.avro.apache.generatedtestsources.PlayingCardSuit playingCardSuit;
7777

7878
/**
7979
* Default constructor. Note that this does not initialize fields
@@ -102,7 +102,7 @@ public java.lang.Object get(int field$) {
102102
case 0: return isFaceCard;
103103
case 1: return cardValue;
104104
case 2: return playingCardSuit;
105-
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
105+
default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
106106
}
107107
}
108108

@@ -113,7 +113,7 @@ public void put(int field$, java.lang.Object value$) {
113113
case 0: isFaceCard = (java.lang.Boolean)value$; break;
114114
case 1: cardValue = (java.lang.Integer)value$; break;
115115
case 2: playingCardSuit = (com.azure.core.serializer.avro.apache.generatedtestsources.PlayingCardSuit)value$; break;
116-
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
116+
default: throw new IndexOutOfBoundsException("Invalid index: " + field$);
117117
}
118118
}
119119

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.security.keyvault.keys;
5+
6+
import com.azure.identity.DefaultAzureCredentialBuilder;
7+
import com.azure.security.keyvault.keys.models.CreateRsaKeyOptions;
8+
import com.azure.security.keyvault.keys.models.KeyRotationLifetimeAction;
9+
import com.azure.security.keyvault.keys.models.KeyRotationPolicy;
10+
import com.azure.security.keyvault.keys.models.KeyRotationPolicyAction;
11+
import com.azure.security.keyvault.keys.models.KeyRotationPolicyProperties;
12+
import com.azure.security.keyvault.keys.models.KeyVaultKey;
13+
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
17+
/*
18+
* This sample demonstrates how to set key rotation policies and manually rotate keys in Key Vault to create a new key
19+
* version.
20+
*/
21+
public class KeyRotation {
22+
/**
23+
* Authenticates with the key vault and shows set key rotation policies and manually rotate keys in Key Vault to
24+
* create a new key version.
25+
*
26+
* @param args Unused. Arguments to the program.
27+
* @throws IllegalArgumentException when an invalid key vault endpoint is passed.
28+
*/
29+
public static void main(String[] args) {
30+
// Instantiate a KeyClient that will be used to call the service. Notice that the KeyClient is using default Azure
31+
// credentials. To make default credentials work, ensure that environment variables 'AZURE_CLIENT_ID',
32+
// 'AZURE_CLIENT_KEY' and 'AZURE_TENANT_ID' are set with the service principal credentials.
33+
KeyClient keyClient = new KeyClientBuilder()
34+
.vaultUrl("https://{YOUR_VAULT_NAME}.vault.azure.net")
35+
.credential(new DefaultAzureCredentialBuilder().build())
36+
.buildClient();
37+
38+
// Let's create an RSA key.
39+
String keyName = "MyKey";
40+
KeyVaultKey originalKey = keyClient.createRsaKey(new CreateRsaKeyOptions(keyName).setKeySize(2048));
41+
42+
System.out.printf("Key created with name %s and type %s%n", originalKey.getName(), originalKey.getKeyType());
43+
44+
// You can configure its key rotation policy to allow Azure Key Vault to do it automatically under certain
45+
// conditions.
46+
List<KeyRotationLifetimeAction> keyRotationLifetimeActionList = new ArrayList<>();
47+
KeyRotationLifetimeAction rotateLifetimeAction = new KeyRotationLifetimeAction(KeyRotationPolicyAction.ROTATE)
48+
.setTimeAfterCreate("P90D"); // Rotate the key after 90 days of its creation.
49+
keyRotationLifetimeActionList.add(rotateLifetimeAction);
50+
KeyRotationPolicyProperties keyRotationPolicyProperties = new KeyRotationPolicyProperties()
51+
.setLifetimeActions(keyRotationLifetimeActionList)
52+
.setExpiryTime("P6M"); // Make any new versions of the key expire 6 months after creation.
53+
54+
// An object containing the details of the recently updated key rotation policy will be returned by the update
55+
// method.
56+
KeyRotationPolicy keyRotationPolicy =
57+
keyClient.updateKeyRotationPolicy(keyName, keyRotationPolicyProperties);
58+
59+
System.out.printf("Updated key rotation policy with id: %s%n", keyRotationPolicy.getId());
60+
61+
// You can also manually rotate a key by calling the following method.
62+
KeyVaultKey manuallyRotatedKey = keyClient.rotateKey(keyName);
63+
64+
System.out.printf("Rotated key with name %s%n", manuallyRotatedKey.getName());
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.security.keyvault.keys;
5+
6+
import com.azure.identity.DefaultAzureCredentialBuilder;
7+
import com.azure.security.keyvault.keys.models.CreateRsaKeyOptions;
8+
import com.azure.security.keyvault.keys.models.KeyRotationLifetimeAction;
9+
import com.azure.security.keyvault.keys.models.KeyRotationPolicyAction;
10+
import com.azure.security.keyvault.keys.models.KeyRotationPolicyProperties;
11+
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
/*
16+
* This sample demonstrates how to set key rotation policies and manually rotate keys in Key Vault to create a new key
17+
* version.
18+
*/
19+
public class KeyRotationAsync {
20+
/**
21+
* Authenticates with the key vault and shows set key rotation policies and manually rotate keys in Key Vault to
22+
* create a new key version.
23+
*
24+
* @param args Unused. Arguments to the program.
25+
* @throws IllegalArgumentException when an invalid key vault endpoint is passed.
26+
*/
27+
public static void main(String[] args) {
28+
// Instantiate a KeyAsyncClient that will be used to call the service. Notice that the KeyAsyncClient is using
29+
// default Azure credentials. To make default credentials work, ensure that environment variables
30+
// 'AZURE_CLIENT_ID', 'AZURE_CLIENT_KEY' and 'AZURE_TENANT_ID' are set with the service principal credentials.
31+
KeyAsyncClient keyAsyncClient = new KeyClientBuilder()
32+
.vaultUrl("https://{YOUR_VAULT_NAME}.vault.azure.net")
33+
.credential(new DefaultAzureCredentialBuilder().build())
34+
.buildAsyncClient();
35+
36+
// Let's create an RSA key.
37+
String keyName = "MyKey";
38+
keyAsyncClient.createRsaKey(new CreateRsaKeyOptions(keyName).setKeySize(2048))
39+
.subscribe(originalKey ->
40+
System.out.printf("Key created with name: %s, and type: %s%n", originalKey.getName(),
41+
originalKey.getKeyType()));
42+
43+
// You can configure its key rotation policy to allow Azure Key Vault to do it automatically under certain
44+
// conditions.
45+
List<KeyRotationLifetimeAction> keyRotationLifetimeActionList = new ArrayList<>();
46+
KeyRotationLifetimeAction rotateLifetimeAction = new KeyRotationLifetimeAction(KeyRotationPolicyAction.ROTATE)
47+
.setTimeAfterCreate("P90D"); // Rotate the key after 90 days of its creation.
48+
keyRotationLifetimeActionList.add(rotateLifetimeAction);
49+
KeyRotationPolicyProperties keyRotationPolicyProperties = new KeyRotationPolicyProperties()
50+
.setLifetimeActions(keyRotationLifetimeActionList)
51+
.setExpiryTime("P6M"); // Make any new versions of the key expire 6 months after creation.
52+
53+
// An object containing the details of the recently updated key rotation policy will be returned by the update
54+
// method.
55+
keyAsyncClient.updateKeyRotationPolicy(keyName, keyRotationPolicyProperties)
56+
.subscribe(keyRotationPolicy ->
57+
System.out.printf("Updated key rotation policy with id: %s%n", keyRotationPolicy.getId()));
58+
59+
// You can also manually rotate a key by calling the following method.
60+
keyAsyncClient.rotateKey(keyName)
61+
.subscribe(manuallyRotatedKey ->
62+
System.out.printf("Rotated key with name: %s%n", manuallyRotatedKey.getName()));
63+
}
64+
}

0 commit comments

Comments
 (0)