use of com.microsoft.azure.keyvault.models.CertificateOperation in project azure-sdk-for-java by Azure.
the class KeyVaultClientImpl method createCertificateWithServiceResponseAsync.
/**
* Creates a new certificate. If this is the first version, the certificate resource is created.
*
* @param vaultBaseUrl The vault name, for example https://myvault.vault.azure.net.
* @param certificateName The name of the certificate.
* @param certificatePolicy The management policy for the certificate.
* @param certificateAttributes The attributes of the certificate (optional).
* @param tags Application specific metadata in the form of key-value pairs.
* @return the observable to the CertificateOperation object
*/
public Observable<ServiceResponse<CertificateOperation>> createCertificateWithServiceResponseAsync(String vaultBaseUrl, String certificateName, CertificatePolicy certificatePolicy, CertificateAttributes certificateAttributes, Map<String, String> tags) {
if (vaultBaseUrl == null) {
throw new IllegalArgumentException("Parameter vaultBaseUrl is required and cannot be null.");
}
if (certificateName == null) {
throw new IllegalArgumentException("Parameter certificateName is required and cannot be null.");
}
if (this.apiVersion() == null) {
throw new IllegalArgumentException("Parameter this.apiVersion() is required and cannot be null.");
}
Validator.validate(certificatePolicy);
Validator.validate(certificateAttributes);
Validator.validate(tags);
CertificateCreateParameters parameters = new CertificateCreateParameters();
parameters.withCertificatePolicy(certificatePolicy);
parameters.withCertificateAttributes(certificateAttributes);
parameters.withTags(tags);
String parameterizedHost = Joiner.on(", ").join("{vaultBaseUrl}", vaultBaseUrl);
return service.createCertificate(certificateName, this.apiVersion(), this.acceptLanguage(), parameters, parameterizedHost, this.userAgent()).flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<CertificateOperation>>>() {
@Override
public Observable<ServiceResponse<CertificateOperation>> call(Response<ResponseBody> response) {
try {
ServiceResponse<CertificateOperation> clientResponse = createCertificateDelegate(response);
return Observable.just(clientResponse);
} catch (Throwable t) {
return Observable.error(t);
}
}
});
}
use of com.microsoft.azure.keyvault.models.CertificateOperation 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());
}
}
use of com.microsoft.azure.keyvault.models.CertificateOperation 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;
}
}
use of com.microsoft.azure.keyvault.models.CertificateOperation 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());
}
}
use of com.microsoft.azure.keyvault.models.CertificateOperation 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());
}
}
Aggregations