Skip to content

Commit

Permalink
Merge pull request #3 from jr1000000/bugfix
Browse files Browse the repository at this point in the history
Keyvault samples unique key names
  • Loading branch information
jr1000000 authored Oct 12, 2023
2 parents b3369f5 + 7da127f commit 2fe1304
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
.credential(clientSecretCredential)
.buildClient();

String certificateName = "certificateName" + System.currentTimeMillis();

// Let's create a self-signed certificate valid for 1 year. If the certificate already exists in the key vault,
// then a new version of the certificate is created.
CertificatePolicy policy = new CertificatePolicy("Self", "CN=SelfSignedJavaPkcs12")
Expand All @@ -64,23 +66,23 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
tags.put("foo", "bar");

SyncPoller<CertificateOperation, KeyVaultCertificateWithPolicy> certificatePoller =
certificateClient.beginCreateCertificate("certificateName", policy, true, tags);
certificateClient.beginCreateCertificate(certificateName, policy, true, tags);
certificatePoller.waitUntil(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED);

KeyVaultCertificate cert = certificatePoller.getFinalResult();

// Backups are good to have, if in case certificates get accidentally deleted by you.
// For long term storage, it is ideal to write the backup to a file.
String backupFilePath = "YOUR_BACKUP_FILE_PATH";
byte[] certificateBackup = certificateClient.backupCertificate("certificateName");
byte[] certificateBackup = certificateClient.backupCertificate(certificateName);

System.out.printf("Backed up certificate with back up blob length %d", certificateBackup.length);

writeBackupToFile(certificateBackup, backupFilePath);

// The certificate is no longer in use, so you delete it.
SyncPoller<DeletedCertificate, Void> deletedCertificatePoller =
certificateClient.beginDeleteCertificate("certificateName");
certificateClient.beginDeleteCertificate(certificateName);
// Deleted Certificate is accessible as soon as polling beings.
PollResponse<DeletedCertificate> pollResponse = deletedCertificatePoller.poll();

Expand All @@ -93,7 +95,7 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
Thread.sleep(30000);

// If the vault is soft-delete enabled, then you need to purge the certificate as well for permanent deletion.
certificateClient.purgeDeletedCertificateWithResponse("certificateName", new Context("key1", "value1"));
certificateClient.purgeDeletedCertificateWithResponse(certificateName, new Context("key1", "value1"));

// To ensure the certificate is purged server-side.
Thread.sleep(15000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
.credential(clientSecretCredential)
.buildClient();

String certificateName = "certificateName" + System.currentTimeMillis();
String myCertificate = "myCertificate" + System.currentTimeMillis();

// Let's create a self-signed certificate valid for 1 year. If the certificate already exists in the key vault,
// then a new version of the certificate is created.
CertificatePolicy policy = new CertificatePolicy("Self", "CN=SelfSignedJavaPkcs12")
Expand All @@ -59,13 +62,13 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
tags.put("foo", "bar");

SyncPoller<CertificateOperation, KeyVaultCertificateWithPolicy> certificatePoller =
certificateClient.beginCreateCertificate("certificateName", policy, true, tags);
certificateClient.beginCreateCertificate(certificateName, policy, true, tags);
certificatePoller.waitUntil(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED);

KeyVaultCertificate cert = certificatePoller.getFinalResult();

// Let's get the latest version of the certificate from the key vault.
KeyVaultCertificate certificate = certificateClient.getCertificate("certificateName");
KeyVaultCertificate certificate = certificateClient.getCertificate(certificateName);

System.out.printf("Certificate is returned with name %s and secret id %s \n",
certificate.getProperties().getName(), certificate.getSecretId());
Expand All @@ -90,19 +93,19 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
System.out.printf("Issuer retrieved with name %s and provider %s", myIssuer.getName(), myIssuer.getProvider());

// Let's create a certificate signed by our issuer.
certificateClient.beginCreateCertificate("myCertificate",
certificateClient.beginCreateCertificate(myCertificate,
new CertificatePolicy("myIssuer", "CN=SelfSignedJavaPkcs12"), true, tags)
.waitUntil(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED);

// Let's get the latest version of our certificate from the key vault.
KeyVaultCertificate myCert = certificateClient.getCertificate("myCertificate");
KeyVaultCertificate myCert = certificateClient.getCertificate(myCertificate);

System.out.printf("Certificate is returned with name %s and secret id %s \n", myCert.getProperties().getName(),
myCert.getSecretId());

// The certificates and issuers are no longer needed, need to delete it from the key vault.
SyncPoller<DeletedCertificate, Void> deletedCertificatePoller =
certificateClient.beginDeleteCertificate("certificateName");
certificateClient.beginDeleteCertificate(certificateName);
// Deleted certificate is accessible as soon as polling beings.
PollResponse<DeletedCertificate> pollResponse = deletedCertificatePoller.poll();

Expand All @@ -112,7 +115,7 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
deletedCertificatePoller.waitForCompletion();

SyncPoller<DeletedCertificate, Void> deletedCertPoller =
certificateClient.beginDeleteCertificate("myCertificate");
certificateClient.beginDeleteCertificate(myCertificate);
// Deleted certificate is accessible as soon as polling beings.
PollResponse<DeletedCertificate> deletePollResponse = deletedCertPoller.poll();

Expand All @@ -130,7 +133,7 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
Thread.sleep(30000);

// If the key vault is soft-delete enabled, then deleted certificates need to be purged for permanent deletion.
certificateClient.purgeDeletedCertificate("certificateName");
certificateClient.purgeDeletedCertificate("myCertificate");
certificateClient.purgeDeletedCertificate(certificateName);
certificateClient.purgeDeletedCertificate(myCertificate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
.credential(clientSecretCredential)
.buildClient();

String certName = "certName" + System.currentTimeMillis();
String myCertificate = "myCertificate" + System.currentTimeMillis();

// Let's create a self-signed certificate valid for 1 year. If the certificate already exists in the key vault,
// then a new version of the certificate is created.
CertificatePolicy policy = new CertificatePolicy("Self", "CN=SelfSignedJavaPkcs12");
Map<String, String> tags = new HashMap<>();
tags.put("foo", "bar");

SyncPoller<CertificateOperation, KeyVaultCertificateWithPolicy> certificatePoller =
certificateClient.beginCreateCertificate("certName", policy, true, tags);
certificateClient.beginCreateCertificate(certName, policy, true, tags);
certificatePoller.waitUntil(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED);

KeyVaultCertificate cert = certificatePoller.getFinalResult();
Expand All @@ -63,7 +66,7 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
System.out.printf("Issuer created with name %s and provider %s", myIssuer.getName(), myIssuer.getProvider());

// Let's create a certificate signed by our issuer.
certificateClient.beginCreateCertificate("myCertificate",
certificateClient.beginCreateCertificate(myCertificate,
new CertificatePolicy("myIssuer", "CN=SignedJavaPkcs12"), true, tags)
.waitUntil(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED);

Expand All @@ -78,7 +81,7 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
}

// Let's list all certificate versions of the certificate.
for (CertificateProperties certificate : certificateClient.listPropertiesOfCertificateVersions("myCertificate")) {
for (CertificateProperties certificate : certificateClient.listPropertiesOfCertificateVersions(myCertificate)) {
KeyVaultCertificate certificateWithAllProperties =
certificateClient.getCertificateVersion(certificate.getName(), certificate.getVersion());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ deleted certificates to be retained for a given retention period (90 days). Duri
.credential(clientSecretCredential)
.buildAsyncClient();

String certificateName = "certificateName" + System.currentTimeMillis();

// Let's create a self-signed certificate valid for 1 year. If the certificate already exists in the key vault,
// then a new version of the certificate is created.
CertificatePolicy policy = new CertificatePolicy("Self", "CN=SelfSignedJavaPkcs12")
Expand All @@ -54,7 +56,7 @@ deleted certificates to be retained for a given retention period (90 days). Duri
Map<String, String> tags = new HashMap<>();
tags.put("foo", "bar");

certificateAsyncClient.beginCreateCertificate("certificateName", policy, true, tags)
certificateAsyncClient.beginCreateCertificate(certificateName, policy, true, tags)
.subscribe(pollResponse -> {
System.out.println("---------------------------------------------------------------------------------");
System.out.println(pollResponse.getStatus());
Expand All @@ -65,7 +67,7 @@ deleted certificates to be retained for a given retention period (90 days). Duri
Thread.sleep(22000);

// The certificate is no longer needed, need to delete it from the key vault.
certificateAsyncClient.beginDeleteCertificate("certificateName")
certificateAsyncClient.beginDeleteCertificate(certificateName)
.subscribe(pollResponse -> {
System.out.println("Delete Status: " + pollResponse.getStatus().toString());
System.out.println("Delete Certificate Name: " + pollResponse.getValue().getName());
Expand All @@ -77,7 +79,7 @@ deleted certificates to be retained for a given retention period (90 days). Duri

// We accidentally deleted the certificate. Let's recover it.
// A deleted certificate can only be recovered if the key vault is soft-delete enabled.
certificateAsyncClient.beginRecoverDeletedCertificate("certificateName")
certificateAsyncClient.beginRecoverDeletedCertificate(certificateName)
.subscribe(pollResponse -> {
System.out.println("Recovery Status: " + pollResponse.getStatus().toString());
System.out.println("Recover Certificate Name: " + pollResponse.getValue().getName());
Expand All @@ -88,7 +90,7 @@ deleted certificates to be retained for a given retention period (90 days). Duri
Thread.sleep(10000);

// The certificate is no longer needed, need to delete it from the key vault.
certificateAsyncClient.beginDeleteCertificate("certificateName")
certificateAsyncClient.beginDeleteCertificate(certificateName)
.subscribe(pollResponse -> {
System.out.println("Delete Status: " + pollResponse.getStatus().toString());
System.out.println("Delete Certificate Name: " + pollResponse.getValue().getName());
Expand All @@ -106,7 +108,7 @@ deleted certificates to be retained for a given retention period (90 days). Duri
Thread.sleep(15000);

// If the keyvault is soft-delete enabled, then deleted certificates need to be purged for permanent deletion.
certificateAsyncClient.purgeDeletedCertificateWithResponse("certificateName")
certificateAsyncClient.purgeDeletedCertificateWithResponse(certificateName)
.subscribe(purgeResponse ->
System.out.printf("Purge Status response %d %n", purgeResponse.getStatusCode()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,23 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
.credential(clientSecretCredential)
.buildClient();

String backupCloudRsaKey = "BackupCloudRsaKey" + System.currentTimeMillis();

// Let's create an RSA key valid for 1 year. If the key already exists in the key vault, then a new version of
// the key is created.
keyClient.createRsaKey(new CreateRsaKeyOptions("BackupCloudRsaKey")
keyClient.createRsaKey(new CreateRsaKeyOptions(backupCloudRsaKey)
.setExpiresOn(OffsetDateTime.now().plusYears(1))
.setKeySize(2048));

// Backups are good to have, if in case keys get accidentally deleted by you.
// For long term storage, it is ideal to write the backup to a file.
String backupFilePath = "java/com/azuresamples/keyvault";
byte[] keyBackup = keyClient.backupKey("BackupCloudRsaKey");
byte[] keyBackup = keyClient.backupKey(backupCloudRsaKey);

writeBackupToFile(keyBackup, backupFilePath);

// The RSA key is no longer in use, so you delete it.
SyncPoller<DeletedKey, Void> rsaDeletedKeyPoller = keyClient.beginDeleteKey("BackupCloudRsaKey");
SyncPoller<DeletedKey, Void> rsaDeletedKeyPoller = keyClient.beginDeleteKey(backupCloudRsaKey);
PollResponse<DeletedKey> pollResponse = rsaDeletedKeyPoller.poll();
DeletedKey rsaDeletedKey = pollResponse.getValue();

Expand All @@ -80,7 +82,7 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
Thread.sleep(30000);

// If the vault is soft-delete enabled, then you need to purge the key as well for permanent deletion.
keyClient.purgeDeletedKey("BackupCloudRsaKey");
keyClient.purgeDeletedKey(backupCloudRsaKey);

// To ensure the key is purged server-side.
Thread.sleep(15000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,20 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
.credential(clientSecretCredential)
.buildClient();

String helloCloudRsaKey = "HelloCloudRsaKey" + System.currentTimeMillis();

// Let's create an RSA key valid for 1 year. If the key already exists in the key vault, then a new version of
// the key is created.
Response<KeyVaultKey> createKeyResponse =
keyClient.createRsaKeyWithResponse(new CreateRsaKeyOptions("HelloCloudRsaKey")
keyClient.createRsaKeyWithResponse(new CreateRsaKeyOptions(helloCloudRsaKey)
.setExpiresOn(OffsetDateTime.now().plusYears(1))
.setKeySize(2048), new Context("key1", "value1"));

// Let's validate the create key operation succeeded using the status code information in the response.
Log.i(TAG, String.format("Create Key operation succeeded with status code %s \n", createKeyResponse.getStatusCode()));

// Let's get the RSA key from the key vault.
KeyVaultKey cloudRsaKey = keyClient.getKey("HelloCloudRsaKey");
KeyVaultKey cloudRsaKey = keyClient.getKey(helloCloudRsaKey);

Log.i(TAG, String.format("Key is returned with name %s and type %s \n", cloudRsaKey.getName(),
cloudRsaKey.getKeyType()));
Expand All @@ -75,12 +77,12 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
// We need the RSA key with bigger key size, so you want to update the key in key vault to ensure it has the
// required size. Calling createRsaKey() on an existing key creates a new version of the key in the key vault
// with the new specified size.
keyClient.createRsaKey(new CreateRsaKeyOptions("HelloCloudRsaKey")
keyClient.createRsaKey(new CreateRsaKeyOptions(helloCloudRsaKey)
.setExpiresOn(OffsetDateTime.now().plusYears(1))
.setKeySize(4096));

// The RSA key is no longer needed, need to delete it from the key vault.
SyncPoller<DeletedKey, Void> rsaDeletedKeyPoller = keyClient.beginDeleteKey("HelloCloudRsaKey");
SyncPoller<DeletedKey, Void> rsaDeletedKeyPoller = keyClient.beginDeleteKey(helloCloudRsaKey);
PollResponse<DeletedKey> pollResponse = rsaDeletedKeyPoller.poll();
DeletedKey rsaDeletedKey = pollResponse.getValue();

Expand All @@ -94,7 +96,7 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
Thread.sleep(30000);

// If the keyvault is soft-delete enabled, then deleted keys need to be purged for permanent deletion.
keyClient.purgeDeletedKey("HelloCloudRsaKey");
keyClient.purgeDeletedKey(helloCloudRsaKey);
Log.i(TAG, "HelloCloudRsaKey purged from vault");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
.buildAsyncClient();

// Let's create an RSA key.
String keyName = "MyKey";
String keyName = "MyKey" + System.currentTimeMillis();
keyAsyncClient.createRsaKey(new CreateRsaKeyOptions(keyName).setKeySize(2048))
.subscribe(originalKey ->
Log.i(TAG, String.format("Key created with name: %s, and type: %s%n", originalKey.getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,24 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
.credential(clientSecretCredential)
.buildClient();

String storageAccountPassword = "StorageAccountPassword" + System.currentTimeMillis();

// Let's create secrets holding storage account credentials valid for 1 year. If the secret already exists in
// the key vault, then a new version of the secret is created.
client.setSecret(new KeyVaultSecret("StorageAccountPassword", "f4G34fMh8v-fdsgjsk2323=-asdsdfsdf")
client.setSecret(new KeyVaultSecret(storageAccountPassword, "f4G34fMh8v-fdsgjsk2323=-asdsdfsdf")
.setProperties(new SecretProperties()
.setExpiresOn(OffsetDateTime.now().plusYears(1))));

// Backups are good to have, if in case secrets get accidentally deleted by you.
// For long term storage, it is ideal to write the backup to a file.
String backupFilePath = "YOUR_BACKUP_FILE_PATH";
byte[] secretBackup = client.backupSecret("StorageAccountPassword");
byte[] secretBackup = client.backupSecret(storageAccountPassword);

writeBackupToFile(secretBackup, backupFilePath);

// The storage account secret is no longer in use, so you delete it.
SyncPoller<DeletedSecret, Void> deletedStorageSecretPoller =
client.beginDeleteSecret("StorageAccountPassword");
client.beginDeleteSecret(storageAccountPassword);
PollResponse<DeletedSecret> pollResponse = deletedStorageSecretPoller.poll();
DeletedSecret deletedStorageSecret = pollResponse.getValue();

Expand All @@ -80,7 +82,7 @@ public static void main(String endpoint, ClientSecretCredential clientSecretCred
Thread.sleep(30000);

// If the vault is soft-delete enabled, then you need to purge the secret as well for permanent deletion.
client.purgeDeletedSecret("StorageAccountPassword");
client.purgeDeletedSecret(storageAccountPassword);

// To ensure the secret is purged server-side.
Thread.sleep(15000);
Expand Down
Loading

0 comments on commit 2fe1304

Please sign in to comment.