Search in sources :

Example 1 with X509CertificateProperties

use of com.microsoft.azure.keyvault.models.X509CertificateProperties 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 2 with X509CertificateProperties

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

the class CertificateOperationsTest method certificateAsyncRequestCancellation.

/**
     * Cancel the certificate create asynchronously
     * @throws IOException 
     * @throws IllegalArgumentException 
     * @throws KeyVaultErrorException 
     * 
     */
@Test
public void certificateAsyncRequestCancellation() throws KeyVaultErrorException, IllegalArgumentException, IOException {
    // Set content type to indicate the certificate is PKCS12 format.
    SecretProperties secretProperties = new SecretProperties().withContentType(MIME_PKCS12);
    String subjectName = "CN=SelfSignedJavaPkcs12";
    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 = "cancellationRequestedCertJava";
    keyVaultClient.createCertificate(new CreateCertificateRequest.Builder(vaultUri, certificateName).withPolicy(certificatePolicy).build());
    CertificateOperation cancelledCertificateOperation = keyVaultClient.updateCertificateOperation(new UpdateCertificateOperationRequest.Builder(vaultUri, certificateName, true).build());
    Assert.assertNotNull(cancelledCertificateOperation);
    Assert.assertTrue(cancelledCertificateOperation.cancellationRequested());
    keyVaultClient.deleteCertificateOperation(getVaultUri(), certificateName);
    keyVaultClient.deleteCertificate(getVaultUri(), certificateName);
}
Also used : CreateCertificateRequest(com.microsoft.azure.keyvault.requests.CreateCertificateRequest) SecretProperties(com.microsoft.azure.keyvault.models.SecretProperties) 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 3 with X509CertificateProperties

use of com.microsoft.azure.keyvault.models.X509CertificateProperties 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 4 with X509CertificateProperties

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

the class CertificateOperationsTest method createSelfSignedCertificatePkcs12.

/**
     * Create a self-signed certificate in PKCS12 format (which includes the
     * private key) certificate.
     * 
     * @throws Exception
     */
@Test
public void createSelfSignedCertificatePkcs12() throws Exception {
    // Set content type to indicate the certificate is PKCS12 format.
    SecretProperties secretProperties = new SecretProperties().withContentType(MIME_PKCS12);
    String subjectName = "CN=SelfSignedJavaPkcs12";
    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);
    Attributes attribute = new CertificateAttributes().withEnabled(true).withExpires(new DateTime().withYear(2050).withMonthOfYear(1)).withNotBefore(new DateTime().withYear(2000).withMonthOfYear(1));
    String vaultUri = getVaultUri();
    String certificateName = "createSelfSignedJavaPkcs12";
    CreateCertificateRequest createCertificateRequest = new CreateCertificateRequest.Builder(vaultUri, certificateName).withPolicy(certificatePolicy).withAttributes(attribute).withTags(sTags).build();
    CertificateOperation certificateOperation = keyVaultClient.createCertificate(createCertificateRequest);
    Assert.assertNotNull(certificateOperation);
    Assert.assertTrue(certificateOperation.status().equalsIgnoreCase(STATUS_IN_PROGRESS));
    CertificateBundle certificateBundle = pollOnCertificateOperation(certificateOperation);
    validateCertificateBundle(certificateBundle, certificatePolicy);
    compareAttributes(attribute, createCertificateRequest.certificateAttributes());
    // 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());
    // Retrieve the key backing the certificate
    KeyIdentifier keyIdentifier = certificateBundle.keyIdentifier();
    KeyBundle keyBundle = keyVaultClient.getKey(keyIdentifier.baseIdentifier());
    Assert.assertTrue(keyBundle.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 : KeyIdentifier(com.microsoft.azure.keyvault.KeyIdentifier) CertificateAttributes(com.microsoft.azure.keyvault.models.CertificateAttributes) 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) Attributes(com.microsoft.azure.keyvault.models.Attributes) CertificateAttributes(com.microsoft.azure.keyvault.models.CertificateAttributes) X509CertificateProperties(com.microsoft.azure.keyvault.models.X509CertificateProperties) CertificateOperation(com.microsoft.azure.keyvault.models.CertificateOperation) KeyStore(java.security.KeyStore) DateTime(org.joda.time.DateTime) X509Certificate(java.security.cert.X509Certificate) 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) KeyBundle(com.microsoft.azure.keyvault.models.KeyBundle) Test(org.junit.Test)

Example 5 with X509CertificateProperties

use of com.microsoft.azure.keyvault.models.X509CertificateProperties 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)

Aggregations

CertificateOperation (com.microsoft.azure.keyvault.models.CertificateOperation)7 CertificatePolicy (com.microsoft.azure.keyvault.models.CertificatePolicy)7 IssuerParameters (com.microsoft.azure.keyvault.models.IssuerParameters)7 SecretProperties (com.microsoft.azure.keyvault.models.SecretProperties)7 X509CertificateProperties (com.microsoft.azure.keyvault.models.X509CertificateProperties)7 CreateCertificateRequest (com.microsoft.azure.keyvault.requests.CreateCertificateRequest)7 Test (org.junit.Test)7 CertificateBundle (com.microsoft.azure.keyvault.models.CertificateBundle)6 KeyVaultErrorException (com.microsoft.azure.keyvault.models.KeyVaultErrorException)6 SecretIdentifier (com.microsoft.azure.keyvault.SecretIdentifier)2 AdministratorDetails (com.microsoft.azure.keyvault.models.AdministratorDetails)2 IssuerBundle (com.microsoft.azure.keyvault.models.IssuerBundle)2 IssuerCredentials (com.microsoft.azure.keyvault.models.IssuerCredentials)2 OrganizationDetails (com.microsoft.azure.keyvault.models.OrganizationDetails)2 SecretBundle (com.microsoft.azure.keyvault.models.SecretBundle)2 KeyStore (java.security.KeyStore)2 X509Certificate (java.security.cert.X509Certificate)2 ArrayList (java.util.ArrayList)2 KeyIdentifier (com.microsoft.azure.keyvault.KeyIdentifier)1 Attributes (com.microsoft.azure.keyvault.models.Attributes)1