Search in sources :

Example 11 with KeyVaultErrorException

use of com.microsoft.azure.keyvault.models.KeyVaultErrorException in project azure-sdk-for-java by Azure.

the class AsyncOperationsTest method certificateAsync.

@Test
public void certificateAsync() throws Exception {
    String vault = getVaultUri();
    String certificateName = "myCertificate";
    CreateCertificateRequest createCertificateRequest = new CreateCertificateRequest.Builder(vault, certificateName).withPolicy(new CertificatePolicy().withSecretProperties(new SecretProperties().withContentType("application/x-pkcs12")).withIssuerParameters(new IssuerParameters().withName("Self")).withX509CertificateProperties(new X509CertificateProperties().withSubject("CN=SelfSignedJavaPkcs12").withValidityInMonths(12))).build();
    CertificateOperation certificateOperation = keyVaultClient.createCertificateAsync(createCertificateRequest, null).get();
    Assert.assertNotNull(certificateOperation);
    UpdateCertificateOperationRequest updateCertificateOperationRequest = new UpdateCertificateOperationRequest.Builder(vault, certificateName, false).build();
    certificateOperation = keyVaultClient.updateCertificateOperationAsync(updateCertificateOperationRequest, null).get();
    Assert.assertNotNull(certificateOperation);
    Map<String, String> tags = new HashMap<String, String>();
    tags.put("tag1", "foo");
    UpdateCertificateRequest updateCertificateRequest = new UpdateCertificateRequest.Builder(vault, certificateName).withTags(tags).build();
    CertificateBundle certificateBundle = keyVaultClient.updateCertificateAsync(updateCertificateRequest, null).get();
    Assert.assertNotNull(certificateBundle);
    UpdateCertificatePolicyRequest updateCertificatePolicyRequest = new UpdateCertificatePolicyRequest.Builder(vault, certificateName).build();
    CertificatePolicy certificatePolicy = keyVaultClient.updateCertificatePolicyAsync(updateCertificatePolicyRequest, null).get();
    Assert.assertNotNull(certificatePolicy);
    certificatePolicy = keyVaultClient.getCertificatePolicyAsync(vault, certificateName, null).get();
    Assert.assertNotNull(certificatePolicy);
    certificateOperation = keyVaultClient.getCertificateOperationAsync(vault, certificateName, null).get();
    Assert.assertNotNull(certificateOperation);
    certificateBundle = keyVaultClient.getCertificateAsync(vault, certificateName, null).get();
    Assert.assertNotNull(certificateBundle);
    String cert = keyVaultClient.getPendingCertificateSigningRequestAsync(vault, certificateName, null).get();
    Assert.assertTrue(!cert.isEmpty());
    List<CertificateItem> certificateItem = keyVaultClient.listCertificatesAsync(vault, null).get();
    Assert.assertNotNull(certificateItem);
    List<CertificateItem> certificateVersionItem = keyVaultClient.listCertificateVersionsAsync(vault, certificateName, null).get();
    Assert.assertNotNull(certificateVersionItem);
    keyVaultClient.deleteCertificateOperationAsync(vault, certificateName, null).get();
    keyVaultClient.deleteCertificateAsync(vault, certificateName, null).get();
    try {
        keyVaultClient.deleteCertificateAsync(vault, certificateName, null).get();
    } catch (ExecutionException ex) {
        Throwable t = ex.getCause();
        if (t instanceof KeyVaultErrorException) {
            Assert.assertEquals("CertificateNotFound", ((KeyVaultErrorException) t).body().error().code());
        } else
            throw ex;
    }
}
Also used : HashMap(java.util.HashMap) CertificateBundle(com.microsoft.azure.keyvault.models.CertificateBundle) KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) CertificatePolicy(com.microsoft.azure.keyvault.models.CertificatePolicy) IssuerParameters(com.microsoft.azure.keyvault.models.IssuerParameters) UpdateCertificateOperationRequest(com.microsoft.azure.keyvault.requests.UpdateCertificateOperationRequest) X509CertificateProperties(com.microsoft.azure.keyvault.models.X509CertificateProperties) CertificateOperation(com.microsoft.azure.keyvault.models.CertificateOperation) UpdateCertificatePolicyRequest(com.microsoft.azure.keyvault.requests.UpdateCertificatePolicyRequest) CreateCertificateRequest(com.microsoft.azure.keyvault.requests.CreateCertificateRequest) SecretProperties(com.microsoft.azure.keyvault.models.SecretProperties) CertificateItem(com.microsoft.azure.keyvault.models.CertificateItem) UpdateCertificateRequest(com.microsoft.azure.keyvault.requests.UpdateCertificateRequest) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 12 with KeyVaultErrorException

use of com.microsoft.azure.keyvault.models.KeyVaultErrorException in project azure-sdk-for-java by Azure.

the class CertificateOperationsTest method createCsr.

/**
     * Create a certificate signing request with key in Key Vault.
     * @throws ExecutionException 
     * @throws InterruptedException 
     * @throws IOException 
     * @throws IllegalArgumentException 
     * @throws KeyVaultErrorException 
     * 
     * @throws Exception
     */
@Test
public void createCsr() throws InterruptedException, ExecutionException, KeyVaultErrorException, IllegalArgumentException, IOException {
    SecretProperties secretProperties = new SecretProperties();
    secretProperties.withContentType(MIME_PKCS12);
    X509CertificateProperties x509Properties = new X509CertificateProperties();
    String subjectName = "CN=ManualEnrollmentJava";
    x509Properties.withSubject(subjectName);
    x509Properties.withValidityInMonths(12);
    // Set issuer to "Unknown"
    IssuerParameters issuerParameters = new IssuerParameters();
    issuerParameters.withName(ISSUER_UNKNOWN);
    CertificatePolicy certificatePolicy = new CertificatePolicy().withSecretProperties(secretProperties).withIssuerParameters(issuerParameters).withX509CertificateProperties(x509Properties);
    String vaultUri = getVaultUri();
    String certificateName = "createManualEnrollmentJava";
    CertificateOperation certificateOperation = keyVaultClient.createCertificate(new CreateCertificateRequest.Builder(vaultUri, certificateName).withPolicy(certificatePolicy).build());
    Assert.assertNotNull(certificateOperation);
    Assert.assertTrue(certificateOperation.status().equalsIgnoreCase(STATUS_IN_PROGRESS));
    Assert.assertNotNull(certificateOperation.csr());
    String csr = keyVaultClient.getPendingCertificateSigningRequest(vaultUri, certificateName);
    Assert.assertNotNull(csr);
    CertificateBundle deletedCertificateBundle = keyVaultClient.deleteCertificate(getVaultUri(), certificateName);
    Assert.assertNotNull(deletedCertificateBundle);
    try {
        keyVaultClient.getCertificate(deletedCertificateBundle.certificateIdentifier().baseIdentifier());
    } catch (KeyVaultErrorException e) {
        Assert.assertNotNull(e.body().error());
        Assert.assertEquals("CertificateNotFound", e.body().error().code());
    }
}
Also used : CreateCertificateRequest(com.microsoft.azure.keyvault.requests.CreateCertificateRequest) SecretProperties(com.microsoft.azure.keyvault.models.SecretProperties) CertificateBundle(com.microsoft.azure.keyvault.models.CertificateBundle) KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) IssuerParameters(com.microsoft.azure.keyvault.models.IssuerParameters) CertificatePolicy(com.microsoft.azure.keyvault.models.CertificatePolicy) X509CertificateProperties(com.microsoft.azure.keyvault.models.X509CertificateProperties) CertificateOperation(com.microsoft.azure.keyvault.models.CertificateOperation) Test(org.junit.Test)

Example 13 with KeyVaultErrorException

use of com.microsoft.azure.keyvault.models.KeyVaultErrorException in project azure-sdk-for-java by Azure.

the class CertificateOperationsTest method createSelfSignedCertificatePem.

/**
     * Create a self-signed certificate in PEM format (which includes the
     * private key) certificate.
     * 
     * @throws Exception
     */
@Test
public void createSelfSignedCertificatePem() throws Exception {
    // Set content type to indicate the certificate is PKCS12 format.
    SecretProperties secretProperties = new SecretProperties().withContentType(MIME_PEM);
    String subjectName = "CN=SelfSignedJavaPem";
    X509CertificateProperties x509Properties = new X509CertificateProperties().withSubject(subjectName).withValidityInMonths(12);
    // Set issuer to "Self"
    IssuerParameters issuerParameters = new IssuerParameters().withName(ISSUER_SELF);
    CertificatePolicy certificatePolicy = new CertificatePolicy().withSecretProperties(secretProperties).withIssuerParameters(issuerParameters).withX509CertificateProperties(x509Properties);
    String vaultUri = getVaultUri();
    String certificateName = "SelfSignedJavaPem";
    CertificateOperation certificateOperation = keyVaultClient.createCertificate(new CreateCertificateRequest.Builder(vaultUri, certificateName).withPolicy(certificatePolicy).build());
    Assert.assertNotNull(certificateOperation);
    Assert.assertTrue(certificateOperation.status().equalsIgnoreCase(STATUS_IN_PROGRESS));
    CertificateBundle certificateBundle = pollOnCertificateOperation(certificateOperation);
    validateCertificateBundle(certificateBundle, certificatePolicy);
    validatePem(certificateBundle, subjectName);
    CertificateBundle deletedCertificateBundle = keyVaultClient.deleteCertificate(getVaultUri(), certificateName);
    Assert.assertNotNull(deletedCertificateBundle);
    try {
        keyVaultClient.getCertificate(deletedCertificateBundle.certificateIdentifier().baseIdentifier());
    } catch (KeyVaultErrorException e) {
        Assert.assertNotNull(e.body().error());
        Assert.assertEquals("CertificateNotFound", e.body().error().code());
    }
}
Also used : CreateCertificateRequest(com.microsoft.azure.keyvault.requests.CreateCertificateRequest) SecretProperties(com.microsoft.azure.keyvault.models.SecretProperties) CertificateBundle(com.microsoft.azure.keyvault.models.CertificateBundle) KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) IssuerParameters(com.microsoft.azure.keyvault.models.IssuerParameters) CertificatePolicy(com.microsoft.azure.keyvault.models.CertificatePolicy) X509CertificateProperties(com.microsoft.azure.keyvault.models.X509CertificateProperties) CertificateOperation(com.microsoft.azure.keyvault.models.CertificateOperation) Test(org.junit.Test)

Example 14 with KeyVaultErrorException

use of com.microsoft.azure.keyvault.models.KeyVaultErrorException in project azure-sdk-for-java by Azure.

the class CertificateOperationsTest method contactsCrudOperations.

/**
     * CRUD for Certificate contacts
     * @throws Exception 
     */
@Test
public void contactsCrudOperations() throws Exception {
    // Create
    Contact contact1 = new Contact();
    contact1.withName("James");
    contact1.withEmailAddress("james@contoso.com");
    contact1.withPhone("7777777777");
    Contact contact2 = new Contact();
    contact2.withName("Ethan");
    contact2.withEmailAddress("ethan@contoso.com");
    contact2.withPhone("8888888888");
    List<Contact> contacts = new ArrayList<Contact>();
    contacts.add(contact1);
    contacts.add(contact2);
    Contacts certificateContacts = new Contacts();
    certificateContacts.withContactList(contacts);
    Contacts createdCertificateContacts = keyVaultClient.setCertificateContacts(getVaultUri(), certificateContacts);
    Assert.assertNotNull(createdCertificateContacts);
    Assert.assertNotNull(createdCertificateContacts.contactList());
    Assert.assertTrue(createdCertificateContacts.contactList().size() == 2);
    Contact[] createContacts = createdCertificateContacts.contactList().toArray(new Contact[createdCertificateContacts.contactList().size()]);
    Assert.assertTrue(createContacts[0].name().equalsIgnoreCase("James"));
    Assert.assertTrue(createContacts[0].emailAddress().equalsIgnoreCase("james@contoso.com"));
    Assert.assertTrue(createContacts[0].phone().equalsIgnoreCase("7777777777"));
    Assert.assertTrue(createContacts[1].name().equalsIgnoreCase("Ethan"));
    Assert.assertTrue(createContacts[1].emailAddress().equalsIgnoreCase("ethan@contoso.com"));
    Assert.assertTrue(createContacts[1].phone().equalsIgnoreCase("8888888888"));
    // Get
    Contacts retrievedCertificateContacts = keyVaultClient.getCertificateContacts(getVaultUri());
    Assert.assertNotNull(retrievedCertificateContacts);
    Assert.assertNotNull(retrievedCertificateContacts.contactList());
    Assert.assertTrue(retrievedCertificateContacts.contactList().size() == 2);
    // Delete
    Contacts deletedCertificateContacts = keyVaultClient.deleteCertificateContacts(getVaultUri());
    Assert.assertNotNull(deletedCertificateContacts);
    Assert.assertNotNull(deletedCertificateContacts.contactList());
    Assert.assertTrue(deletedCertificateContacts.contactList().size() == 2);
    // Get after delete        
    try {
        keyVaultClient.getCertificateContacts(getVaultUri());
    } catch (KeyVaultErrorException e) {
        Assert.assertNotNull(e.body().error());
        Assert.assertEquals("ContactsNotFound", e.body().error().code());
    }
}
Also used : Contacts(com.microsoft.azure.keyvault.models.Contacts) KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) ArrayList(java.util.ArrayList) Contact(com.microsoft.azure.keyvault.models.Contact) Test(org.junit.Test)

Example 15 with KeyVaultErrorException

use of com.microsoft.azure.keyvault.models.KeyVaultErrorException in project azure-sdk-for-java by Azure.

the class AsyncOperationsTest method secretAsync.

@Test
public void secretAsync() throws Exception {
    String vault = getVaultUri();
    String secretname = "mySecret";
    String password = "password";
    SetSecretRequest setSecretRequest = new SetSecretRequest.Builder(vault, secretname, password).build();
    SecretBundle secretBundle = keyVaultClient.setSecretAsync(setSecretRequest, null).get();
    Assert.assertNotNull(secretBundle);
    UpdateSecretRequest updateSecretRequest = new UpdateSecretRequest.Builder(secretBundle.id()).build();
    secretBundle = keyVaultClient.updateSecretAsync(updateSecretRequest, null).get();
    Assert.assertNotNull(secretBundle);
    secretBundle = keyVaultClient.getSecretAsync(secretBundle.id(), null).get();
    Assert.assertNotNull(secretBundle);
    List<SecretItem> secretItems = keyVaultClient.listSecretsAsync(vault, 2, null).get();
    Assert.assertNotNull(secretItems);
    List<SecretItem> secretVersionItems = keyVaultClient.listSecretVersionsAsync(vault, secretname, 2, null).get();
    Assert.assertNotNull(secretVersionItems);
    secretBundle = keyVaultClient.deleteSecretAsync(vault, secretname, null).get();
    Assert.assertNotNull(secretBundle);
    try {
        keyVaultClient.deleteSecretAsync(vault, secretname, null).get();
    } catch (ExecutionException ex) {
        Throwable t = ex.getCause();
        if (t instanceof KeyVaultErrorException) {
            Assert.assertEquals("SecretNotFound", ((KeyVaultErrorException) t).body().error().code());
        } else
            throw ex;
    }
}
Also used : SecretBundle(com.microsoft.azure.keyvault.models.SecretBundle) KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) SecretItem(com.microsoft.azure.keyvault.models.SecretItem) SetSecretRequest(com.microsoft.azure.keyvault.requests.SetSecretRequest) ExecutionException(java.util.concurrent.ExecutionException) UpdateSecretRequest(com.microsoft.azure.keyvault.requests.UpdateSecretRequest) Test(org.junit.Test)

Aggregations

KeyVaultErrorException (com.microsoft.azure.keyvault.models.KeyVaultErrorException)20 Test (org.junit.Test)20 CertificateBundle (com.microsoft.azure.keyvault.models.CertificateBundle)9 CertificatePolicy (com.microsoft.azure.keyvault.models.CertificatePolicy)9 SecretProperties (com.microsoft.azure.keyvault.models.SecretProperties)9 SecretBundle (com.microsoft.azure.keyvault.models.SecretBundle)8 CertificateOperation (com.microsoft.azure.keyvault.models.CertificateOperation)6 IssuerParameters (com.microsoft.azure.keyvault.models.IssuerParameters)6 X509CertificateProperties (com.microsoft.azure.keyvault.models.X509CertificateProperties)6 CreateCertificateRequest (com.microsoft.azure.keyvault.requests.CreateCertificateRequest)6 HashSet (java.util.HashSet)6 SecretIdentifier (com.microsoft.azure.keyvault.SecretIdentifier)5 KeyBundle (com.microsoft.azure.keyvault.models.KeyBundle)5 ArrayList (java.util.ArrayList)4 KeyIdentifier (com.microsoft.azure.keyvault.KeyIdentifier)3 AdministratorDetails (com.microsoft.azure.keyvault.models.AdministratorDetails)3 CertificateItem (com.microsoft.azure.keyvault.models.CertificateItem)3 IssuerBundle (com.microsoft.azure.keyvault.models.IssuerBundle)3 IssuerCredentials (com.microsoft.azure.keyvault.models.IssuerCredentials)3 KeyItem (com.microsoft.azure.keyvault.models.KeyItem)3