Search in sources :

Example 1 with KeyVaultErrorException

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

the class CertificateOperationsTest method listCertificateVersions.

/**
     * List versions of a certificate in a vault.
     */
@Test
public void listCertificateVersions() throws Exception {
    String certificateName = "listCertificateVersions";
    String certificateContent = "MIIJOwIBAzCCCPcGCSqGSIb3DQEHAaCCCOgEggjkMIII4DCCBgkGCSqGSIb3DQEHAaCCBfoEggX2MIIF8jCCBe4GCyqGSIb3DQEMCgECoIIE/jCCBPowHAYKKoZIhvcNAQwBAzAOBAj15YH9pOE58AICB9AEggTYLrI+SAru2dBZRQRlJY7XQ3LeLkah2FcRR3dATDshZ2h0IA2oBrkQIdsLyAAWZ32qYR1qkWxLHn9AqXgu27AEbOk35+pITZaiy63YYBkkpR+pDdngZt19Z0PWrGwHEq5z6BHS2GLyyN8SSOCbdzCz7blj3+7IZYoMj4WOPgOm/tQ6U44SFWek46QwN2zeA4i97v7ftNNns27ms52jqfhOvTA9c/wyfZKAY4aKJfYYUmycKjnnRl012ldS2lOkASFt+lu4QCa72IY6ePtRudPCvmzRv2pkLYS6z3cI7omT8nHP3DymNOqLbFqr5O2M1ZYaLC63Q3xt3eVvbcPh3N08D1hHkhz/KDTvkRAQpvrW8ISKmgDdmzN55Pe55xHfSWGB7gPw8sZea57IxFzWHTK2yvTslooWoosmGxanYY2IG/no3EbPOWDKjPZ4ilYJe5JJ2immlxPz+2e2EOCKpDI+7fzQcRz3PTd3BK+budZ8aXX8aW/lOgKS8WmxZoKnOJBNWeTNWQFugmktXfdPHAdxMhjUXqeGQd8wTvZ4EzQNNafovwkI7IV/ZYoa++RGofVR3ZbRSiBNF6TDj/qXFt0wN/CQnsGAmQAGNiN+D4mY7i25dtTu/Jc7OxLdhAUFpHyJpyrYWLfvOiS5WYBeEDHkiPUa/8eZSPA3MXWZR1RiuDvuNqMjct1SSwdXADTtF68l/US1ksU657+XSC+6ly1A/upz+X71+C4Ho6W0751j5ZMT6x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BBSxgh2xyF+88V4vAffBmZXv8Txt4AQU4O/NX4MjxSodbE7ApNAMIvrtREwCAgfQ";
    String certificatePassword = "123";
    // Set content type to indicate the certificate is PKCS12 format.
    SecretProperties secretProperties = new SecretProperties();
    secretProperties.withContentType(MIME_PKCS12);
    CertificatePolicy certificatePolicy = new CertificatePolicy();
    certificatePolicy.withSecretProperties(secretProperties);
    HashSet<String> certificates = new HashSet<String>();
    for (int i = 0; i < MAX_CERTS; ++i) {
        int failureCount = 0;
        for (; ; ) {
            try {
                CertificateBundle certificateBundle = keyVaultClient.importCertificate(new ImportCertificateRequest.Builder(getVaultUri(), certificateName, certificateContent).withPassword(certificatePassword).withPolicy(certificatePolicy).build());
                CertificateIdentifier id = certificateBundle.certificateIdentifier();
                certificates.add(id.identifier());
                break;
            } catch (KeyVaultErrorException e) {
                ++failureCount;
                if (e.body().error().code().equals("Throttled")) {
                    System.out.println("Waiting to avoid throttling");
                    Thread.sleep(failureCount * 1500);
                    continue;
                }
                throw e;
            }
        }
    }
    PagedList<CertificateItem> listResult = keyVaultClient.listCertificateVersions(getVaultUri(), certificateName, PAGELIST_MAX_CERTS);
    Assert.assertTrue(PAGELIST_MAX_CERTS >= listResult.currentPage().items().size());
    listResult = keyVaultClient.listCertificateVersions(getVaultUri(), certificateName);
    for (CertificateItem item : listResult) {
        if (item != null) {
            certificates.remove(item.id());
        }
    }
    Assert.assertEquals(0, certificates.size());
    keyVaultClient.deleteCertificate(getVaultUri(), certificateName);
}
Also used : SecretProperties(com.microsoft.azure.keyvault.models.SecretProperties) CertificateIdentifier(com.microsoft.azure.keyvault.CertificateIdentifier) CertificateBundle(com.microsoft.azure.keyvault.models.CertificateBundle) KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) CertificateItem(com.microsoft.azure.keyvault.models.CertificateItem) CertificatePolicy(com.microsoft.azure.keyvault.models.CertificatePolicy) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with KeyVaultErrorException

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

the class CertificateOperationsTest method createCertificatePkcs12.

/**
     * Create a test-issuer issued certificate in PKCS12 format (which includes
     * the private key) certificate.
     * 
     * @throws Exception
     */
@Test
public void createCertificatePkcs12() throws Exception {
    // Construct organization administrator details
    AdministratorDetails administratorDetails = new AdministratorDetails().withFirstName("John").withLastName("Doe").withEmailAddress("john.doe@contoso.com").withPhone("1234567890");
    // Construct organization details
    List<AdministratorDetails> administratorsDetails = new ArrayList<AdministratorDetails>();
    administratorsDetails.add(administratorDetails);
    OrganizationDetails organizationDetails = new OrganizationDetails().withAdminDetails(administratorsDetails);
    // Construct certificate issuer credentials
    IssuerCredentials credentials = new IssuerCredentials().withAccountId("account1").withPassword("Pa$$w0rd");
    String certificateIssuerName = "createCertificateJavaPkcs12Issuer01";
    IssuerBundle createdCertificateIssuer = keyVaultClient.setCertificateIssuer(new SetCertificateIssuerRequest.Builder(getVaultUri(), certificateIssuerName, ISSUER_TEST).withCredentials(credentials).withOrganizationDetails(organizationDetails).build());
    validateCertificateIssuer(createdCertificateIssuer, certificateIssuerName);
    // Set content type to indicate the certificate is PKCS12 format.
    SecretProperties secretProperties = new SecretProperties().withContentType(MIME_PKCS12);
    String subjectName = "CN=TestJavaPkcs12";
    X509CertificateProperties x509Properties = new X509CertificateProperties().withSubject(subjectName).withValidityInMonths(12);
    // Set issuer reference to the created issuer
    IssuerParameters issuerParameters = new IssuerParameters();
    issuerParameters.withName(createdCertificateIssuer.issuerIdentifier().name());
    CertificatePolicy certificatePolicy = new CertificatePolicy().withSecretProperties(secretProperties).withIssuerParameters(issuerParameters).withX509CertificateProperties(x509Properties);
    String vaultUri = getVaultUri();
    String certificateName = "createTestJavaPkcs12";
    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);
    // Load the CER part into X509Certificate object
    X509Certificate x509Certificate = loadCerToX509Certificate(certificateBundle);
    Assert.assertTrue(x509Certificate.getSubjectX500Principal().getName().equals(subjectName));
    Assert.assertTrue(x509Certificate.getIssuerX500Principal().getName().equals(subjectName));
    // Retrieve the secret backing the certificate
    SecretIdentifier secretIdentifier = certificateBundle.secretIdentifier();
    SecretBundle secret = keyVaultClient.getSecret(secretIdentifier.baseIdentifier());
    Assert.assertTrue(secret.managed());
    // Load the secret into a KeyStore
    String secretPassword = "";
    KeyStore keyStore = loadSecretToKeyStore(secret, secretPassword);
    // Validate the certificate and key in the KeyStore
    validateCertificateKeyInKeyStore(keyStore, x509Certificate, secretPassword);
    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 : 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) ArrayList(java.util.ArrayList) OrganizationDetails(com.microsoft.azure.keyvault.models.OrganizationDetails) X509CertificateProperties(com.microsoft.azure.keyvault.models.X509CertificateProperties) CertificateOperation(com.microsoft.azure.keyvault.models.CertificateOperation) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) IssuerBundle(com.microsoft.azure.keyvault.models.IssuerBundle) CreateCertificateRequest(com.microsoft.azure.keyvault.requests.CreateCertificateRequest) SecretIdentifier(com.microsoft.azure.keyvault.SecretIdentifier) SecretBundle(com.microsoft.azure.keyvault.models.SecretBundle) SecretProperties(com.microsoft.azure.keyvault.models.SecretProperties) IssuerCredentials(com.microsoft.azure.keyvault.models.IssuerCredentials) AdministratorDetails(com.microsoft.azure.keyvault.models.AdministratorDetails) Test(org.junit.Test)

Example 3 with KeyVaultErrorException

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

the class CertificateOperationsTest method listCertificates.

/**
     * List certificates in a vault.
     */
@Test
public void listCertificates() throws Exception {
    String certificateName = "listCertificate";
    String certificateContent = "MIIJOwIBAzCCCPcGCSqGSIb3DQEHAaCCCOgEggjkMIII4DCCBgkGCSqGSIb3DQEHAaCCBfoEggX2MIIF8jCCBe4GCyqGSIb3DQEMCgECoIIE/jCCBPowHAYKKoZIhvcNAQwBAzAOBAj15YH9pOE58AICB9AEggTYLrI+SAru2dBZRQRlJY7XQ3LeLkah2FcRR3dATDshZ2h0IA2oBrkQIdsLyAAWZ32qYR1qkWxLHn9AqXgu27AEbOk35+pITZaiy63YYBkkpR+pDdngZt19Z0PWrGwHEq5z6BHS2GLyyN8SSOCbdzCz7blj3+7IZYoMj4WOPgOm/tQ6U44SFWek46QwN2zeA4i97v7ftNNns27ms52jqfhOvTA9c/wyfZKAY4aKJfYYUmycKjnnRl012ldS2lOkASFt+lu4QCa72IY6ePtRudPCvmzRv2pkLYS6z3cI7omT8nHP3DymNOqLbFqr5O2M1ZYaLC63Q3xt3eVvbcPh3N08D1hHkhz/KDTvkRAQpvrW8ISKmgDdmzN55Pe55xHfSWGB7gPw8sZea57IxFzWHTK2yvTslooWoosmGxanYY2IG/no3EbPOWDKjPZ4ilYJe5JJ2immlxPz+2e2EOCKpDI+7fzQcRz3PTd3BK+budZ8aXX8aW/lOgKS8WmxZoKnOJBNWeTNWQFugmktXfdPHAdxMhjUXqeGQd8wTvZ4EzQNNafovwkI7IV/ZYoa++RGofVR3ZbRSiBNF6TDj/qXFt0wN/CQnsGAmQA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PzZshV1ZGsBQFRaoYkyLAwdJWIXTi+c37YaC8FRSEnnNmS79Dou1Kc3BvK4EYKAD2KxjtUebrV174gD0Q+9YuJ0GXOTspBvCFd5VT2Rw5zDNrA/J3F5fMCk4wOzAfMAcGBSsOAwIaBBSxgh2xyF+88V4vAffBmZXv8Txt4AQU4O/NX4MjxSodbE7ApNAMIvrtREwCAgfQ";
    String certificatePassword = "123";
    // Set content type to indicate the certificate is PKCS12 format.
    SecretProperties secretProperties = new SecretProperties();
    secretProperties.withContentType(MIME_PKCS12);
    CertificatePolicy certificatePolicy = new CertificatePolicy();
    certificatePolicy.withSecretProperties(secretProperties);
    HashSet<String> certificates = new HashSet<String>();
    for (int i = 0; i < MAX_CERTS; ++i) {
        int failureCount = 0;
        for (; ; ) {
            try {
                CertificateBundle certificateBundle = keyVaultClient.importCertificate(new ImportCertificateRequest.Builder(getVaultUri(), certificateName + i, certificateContent).withPassword(certificatePassword).withPolicy(certificatePolicy).build());
                CertificateIdentifier id = certificateBundle.certificateIdentifier();
                certificates.add(id.baseIdentifier());
                break;
            } catch (KeyVaultErrorException e) {
                ++failureCount;
                if (e.body().error().code().equals("Throttled")) {
                    System.out.println("Waiting to avoid throttling");
                    Thread.sleep(failureCount * 1500);
                    continue;
                }
                throw e;
            }
        }
    }
    PagedList<CertificateItem> listResult = keyVaultClient.listCertificates(getVaultUri(), PAGELIST_MAX_CERTS);
    Assert.assertTrue(PAGELIST_MAX_CERTS >= listResult.currentPage().items().size());
    HashSet<String> toDelete = new HashSet<String>();
    for (CertificateItem item : listResult) {
        if (item != null) {
            CertificateIdentifier id = new CertificateIdentifier(item.id());
            toDelete.add(id.name());
            certificates.remove(item.id());
        }
    }
    Assert.assertEquals(0, certificates.size());
    for (String toDeleteCertificateName : toDelete) {
        keyVaultClient.deleteCertificate(getVaultUri(), toDeleteCertificateName);
    }
}
Also used : SecretProperties(com.microsoft.azure.keyvault.models.SecretProperties) CertificateIdentifier(com.microsoft.azure.keyvault.CertificateIdentifier) CertificateBundle(com.microsoft.azure.keyvault.models.CertificateBundle) KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) CertificateItem(com.microsoft.azure.keyvault.models.CertificateItem) CertificatePolicy(com.microsoft.azure.keyvault.models.CertificatePolicy) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with KeyVaultErrorException

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

the class CertificateOperationsTest method createCertificatePem.

/**
     * Create a test-issuer certificate in PEM format (which includes the
     * private key) certificate.
     * 
     * @throws Exception
     */
@Test
public void createCertificatePem() throws Exception {
    // Construct organization administrator details
    AdministratorDetails administratorDetails = new AdministratorDetails().withFirstName("John").withLastName("Doe").withEmailAddress("john.doe@contoso.com").withPhone("1234567890");
    // Construct organization details
    OrganizationDetails organizationDetails = new OrganizationDetails();
    List<AdministratorDetails> administratorsDetails = new ArrayList<AdministratorDetails>();
    administratorsDetails.add(administratorDetails);
    organizationDetails.withAdminDetails(administratorsDetails);
    // Construct certificate issuer credentials
    IssuerCredentials credentials = new IssuerCredentials().withAccountId("account1").withPassword("Pa$$w0rd");
    String certificateIssuerName = "createCertificateJavaPemIssuer01";
    IssuerBundle createdCertificateIssuer = keyVaultClient.setCertificateIssuer(new SetCertificateIssuerRequest.Builder(getVaultUri(), certificateIssuerName, ISSUER_TEST).withCredentials(credentials).withOrganizationDetails(organizationDetails).build());
    validateCertificateIssuer(createdCertificateIssuer, certificateIssuerName);
    // Set content type to indicate the certificate is PEM format.
    SecretProperties secretProperties = new SecretProperties();
    secretProperties.withContentType(MIME_PEM);
    X509CertificateProperties x509Properties = new X509CertificateProperties();
    String subjectName = "CN=TestJavaPem";
    x509Properties.withSubject(subjectName);
    x509Properties.withValidityInMonths(12);
    // Set issuer reference to the created issuer
    IssuerParameters issuerParameters = new IssuerParameters();
    issuerParameters.withName(createdCertificateIssuer.issuerIdentifier().name());
    CertificatePolicy certificatePolicy = new CertificatePolicy().withSecretProperties(secretProperties).withIssuerParameters(issuerParameters).withX509CertificateProperties(x509Properties);
    String vaultUri = getVaultUri();
    String certificateName = "createTestJavaPem";
    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 : 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) OrganizationDetails(com.microsoft.azure.keyvault.models.OrganizationDetails) ArrayList(java.util.ArrayList) X509CertificateProperties(com.microsoft.azure.keyvault.models.X509CertificateProperties) CertificateOperation(com.microsoft.azure.keyvault.models.CertificateOperation) IssuerBundle(com.microsoft.azure.keyvault.models.IssuerBundle) CreateCertificateRequest(com.microsoft.azure.keyvault.requests.CreateCertificateRequest) SecretProperties(com.microsoft.azure.keyvault.models.SecretProperties) IssuerCredentials(com.microsoft.azure.keyvault.models.IssuerCredentials) AdministratorDetails(com.microsoft.azure.keyvault.models.AdministratorDetails) Test(org.junit.Test)

Example 5 with KeyVaultErrorException

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

the class SecretOperationsTest method listSecretVersions.

@Test
public void listSecretVersions() throws Exception {
    HashSet<String> secrets = new HashSet<String>();
    for (int i = 0; i < MAX_SECRETS; ++i) {
        int failureCount = 0;
        for (; ; ) {
            try {
                SecretBundle secret = keyVaultClient.setSecret(new SetSecretRequest.Builder(getVaultUri(), SECRET_NAME, SECRET_VALUE).build());
                secrets.add(secret.id());
                break;
            } catch (KeyVaultErrorException e) {
                ++failureCount;
                if (e.body().error().code().equals("Throttled")) {
                    System.out.println("Throttled!");
                    Thread.sleep(failureCount * 1500);
                    continue;
                }
                throw e;
            }
        }
    }
    PagedList<SecretItem> listResult = keyVaultClient.listSecretVersions(getVaultUri(), SECRET_NAME, PAGELIST_MAX_SECRETS);
    Assert.assertTrue(PAGELIST_MAX_SECRETS >= listResult.currentPage().items().size());
    listResult = keyVaultClient.listSecretVersions(getVaultUri(), SECRET_NAME);
    for (SecretItem item : listResult) {
        if (item != null) {
            secrets.remove(item.id());
        }
    }
    Assert.assertEquals(0, secrets.size());
    keyVaultClient.deleteSecret(getVaultUri(), SECRET_NAME);
}
Also used : SecretBundle(com.microsoft.azure.keyvault.models.SecretBundle) KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) SecretItem(com.microsoft.azure.keyvault.models.SecretItem) HashSet(java.util.HashSet) 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