Search in sources :

Example 1 with AdministratorDetails

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

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

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

the class CertificateOperationsTest method issuerCrudOperations.

/**
     * CRUD for Certificate issuers
     */
@Test
public void issuerCrudOperations() 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");
    IssuerBundle certificateIssuer = new IssuerBundle().withProvider(ISSUER_TEST).withCredentials(credentials).withOrganizationDetails(organizationDetails);
    IssuerBundle createdCertificateIssuer = keyVaultClient.setCertificateIssuer(new SetCertificateIssuerRequest.Builder(getVaultUri(), "issuer1", certificateIssuer.provider()).withCredentials(certificateIssuer.credentials()).withOrganizationDetails(certificateIssuer.organizationDetails()).build());
    validateCertificateIssuer(certificateIssuer, createdCertificateIssuer);
    String certificateIssuerName = createdCertificateIssuer.issuerIdentifier().name();
    IssuerBundle retrievedCertificateIssuer = keyVaultClient.getCertificateIssuer(getVaultUri(), certificateIssuerName);
    validateCertificateIssuer(certificateIssuer, retrievedCertificateIssuer);
    IssuerCredentials updatedCredentials = new IssuerCredentials().withAccountId("account2").withPassword("Secur!Ty");
    retrievedCertificateIssuer.withCredentials(updatedCredentials);
    IssuerBundle updatedCertificateIssuer = keyVaultClient.updateCertificateIssuer(new UpdateCertificateIssuerRequest.Builder(getVaultUri(), certificateIssuerName).withProvider(ISSUER_TEST).withCredentials(updatedCredentials).withOrganizationDetails(retrievedCertificateIssuer.organizationDetails()).withAttributes(retrievedCertificateIssuer.attributes()).build());
    validateCertificateIssuer(retrievedCertificateIssuer, updatedCertificateIssuer);
    Assert.assertNotNull(updatedCertificateIssuer.organizationDetails());
    IssuerBundle deletedCertificateIssuer = keyVaultClient.deleteCertificateIssuer(getVaultUri(), certificateIssuerName);
    validateCertificateIssuer(updatedCertificateIssuer, deletedCertificateIssuer);
    try {
        keyVaultClient.getCertificateIssuer(getVaultUri(), certificateIssuerName);
    } catch (KeyVaultErrorException e) {
        Assert.assertNotNull(e.body().error());
        Assert.assertEquals("CertificateIssuerNotFound", e.body().error().code());
    }
}
Also used : IssuerBundle(com.microsoft.azure.keyvault.models.IssuerBundle) KeyVaultErrorException(com.microsoft.azure.keyvault.models.KeyVaultErrorException) IssuerCredentials(com.microsoft.azure.keyvault.models.IssuerCredentials) OrganizationDetails(com.microsoft.azure.keyvault.models.OrganizationDetails) ArrayList(java.util.ArrayList) AdministratorDetails(com.microsoft.azure.keyvault.models.AdministratorDetails) Test(org.junit.Test)

Aggregations

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