use of com.microsoft.azure.keyvault.models.SecretBundle in project azure-sdk-for-java by Azure.
the class KeyVaultClientImpl method setSecretWithServiceResponseAsync.
/**
* Sets a secret in a specified key vault.
*
* @param vaultBaseUrl The vault name, for example https://myvault.vault.azure.net.
* @param secretName The name of the secret.
* @param value The value of the secret.
* @param tags Application specific metadata in the form of key-value pairs.
* @param contentType Type of the secret value such as a password.
* @param secretAttributes The secret management attributes.
* @return the observable to the SecretBundle object
*/
public Observable<ServiceResponse<SecretBundle>> setSecretWithServiceResponseAsync(String vaultBaseUrl, String secretName, String value, Map<String, String> tags, String contentType, SecretAttributes secretAttributes) {
if (vaultBaseUrl == null) {
throw new IllegalArgumentException("Parameter vaultBaseUrl is required and cannot be null.");
}
if (secretName == null) {
throw new IllegalArgumentException("Parameter secretName is required and cannot be null.");
}
if (this.apiVersion() == null) {
throw new IllegalArgumentException("Parameter this.apiVersion() is required and cannot be null.");
}
if (value == null) {
throw new IllegalArgumentException("Parameter value is required and cannot be null.");
}
Validator.validate(tags);
Validator.validate(secretAttributes);
SecretSetParameters parameters = new SecretSetParameters();
parameters.withValue(value);
parameters.withTags(tags);
parameters.withContentType(contentType);
parameters.withSecretAttributes(secretAttributes);
String parameterizedHost = Joiner.on(", ").join("{vaultBaseUrl}", vaultBaseUrl);
return service.setSecret(secretName, this.apiVersion(), this.acceptLanguage(), parameters, parameterizedHost, this.userAgent()).flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<SecretBundle>>>() {
@Override
public Observable<ServiceResponse<SecretBundle>> call(Response<ResponseBody> response) {
try {
ServiceResponse<SecretBundle> clientResponse = setSecretDelegate(response);
return Observable.just(clientResponse);
} catch (Throwable t) {
return Observable.error(t);
}
}
});
}
use of com.microsoft.azure.keyvault.models.SecretBundle in project azure-sdk-for-java by Azure.
the class KeyVaultClientImpl method updateSecretWithServiceResponseAsync.
/**
* Updates the attributes associated with a specified secret in a given key vault.
*
* @param vaultBaseUrl The vault name, for example https://myvault.vault.azure.net.
* @param secretName The name of the secret.
* @param secretVersion The version of the secret.
* @param contentType Type of the secret value such as a password.
* @param secretAttributes The secret management attributes.
* @param tags Application specific metadata in the form of key-value pairs.
* @return the observable to the SecretBundle object
*/
public Observable<ServiceResponse<SecretBundle>> updateSecretWithServiceResponseAsync(String vaultBaseUrl, String secretName, String secretVersion, String contentType, SecretAttributes secretAttributes, Map<String, String> tags) {
if (vaultBaseUrl == null) {
throw new IllegalArgumentException("Parameter vaultBaseUrl is required and cannot be null.");
}
if (secretName == null) {
throw new IllegalArgumentException("Parameter secretName is required and cannot be null.");
}
if (secretVersion == null) {
throw new IllegalArgumentException("Parameter secretVersion is required and cannot be null.");
}
if (this.apiVersion() == null) {
throw new IllegalArgumentException("Parameter this.apiVersion() is required and cannot be null.");
}
Validator.validate(secretAttributes);
Validator.validate(tags);
SecretUpdateParameters parameters = new SecretUpdateParameters();
parameters.withContentType(contentType);
parameters.withSecretAttributes(secretAttributes);
parameters.withTags(tags);
String parameterizedHost = Joiner.on(", ").join("{vaultBaseUrl}", vaultBaseUrl);
return service.updateSecret(secretName, secretVersion, this.apiVersion(), this.acceptLanguage(), parameters, parameterizedHost, this.userAgent()).flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<SecretBundle>>>() {
@Override
public Observable<ServiceResponse<SecretBundle>> call(Response<ResponseBody> response) {
try {
ServiceResponse<SecretBundle> clientResponse = updateSecretDelegate(response);
return Observable.just(clientResponse);
} catch (Throwable t) {
return Observable.error(t);
}
}
});
}
use of com.microsoft.azure.keyvault.models.SecretBundle in project azure-sdk-for-java by Azure.
the class CertificateOperationsTest method validatePem.
private void validatePem(CertificateBundle certificateBundle, String subjectName) throws CertificateException, IOException, KeyVaultErrorException, IllegalArgumentException, InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
// 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());
String secretValue = secret.value();
// Extract private key from PEM
PrivateKey secretPrivateKey = extractPrivateKeyFromPemContents(secretValue);
Assert.assertNotNull(secretPrivateKey);
// Extract certificates from PEM
List<X509Certificate> certificates = extractCertificatesFromPemContents(secretValue);
Assert.assertNotNull(certificates);
Assert.assertTrue(certificates.size() == 1);
// has the public key corresponding to the private key.
X509Certificate secretCertificate = certificates.get(0);
Assert.assertNotNull(secretCertificate);
Assert.assertTrue(secretCertificate.getSubjectX500Principal().getName().equals(x509Certificate.getSubjectX500Principal().getName()));
Assert.assertTrue(secretCertificate.getIssuerX500Principal().getName().equals(x509Certificate.getIssuerX500Principal().getName()));
Assert.assertTrue(secretCertificate.getSerialNumber().equals(x509Certificate.getSerialNumber()));
// Create a KeyPair with the private key from the KeyStore and public
// key from the certificate to verify they match
KeyPair keyPair = new KeyPair(secretCertificate.getPublicKey(), secretPrivateKey);
Assert.assertNotNull(keyPair);
verifyRSAKeyPair(keyPair);
}
use of com.microsoft.azure.keyvault.models.SecretBundle 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());
}
}
use of com.microsoft.azure.keyvault.models.SecretBundle in project azure-sdk-for-java by Azure.
the class KeyVaultClientImpl method updateSecretWithServiceResponseAsync.
/**
* Updates the attributes associated with a specified secret in a given key vault.
*
* @param vaultBaseUrl The vault name, for example https://myvault.vault.azure.net.
* @param secretName The name of the secret.
* @param secretVersion The version of the secret.
* @return the observable to the SecretBundle object
*/
public Observable<ServiceResponse<SecretBundle>> updateSecretWithServiceResponseAsync(String vaultBaseUrl, String secretName, String secretVersion) {
if (vaultBaseUrl == null) {
throw new IllegalArgumentException("Parameter vaultBaseUrl is required and cannot be null.");
}
if (secretName == null) {
throw new IllegalArgumentException("Parameter secretName is required and cannot be null.");
}
if (secretVersion == null) {
throw new IllegalArgumentException("Parameter secretVersion is required and cannot be null.");
}
if (this.apiVersion() == null) {
throw new IllegalArgumentException("Parameter this.apiVersion() is required and cannot be null.");
}
final String contentType = null;
final SecretAttributes secretAttributes = null;
final Map<String, String> tags = null;
SecretUpdateParameters parameters = new SecretUpdateParameters();
parameters.withContentType(null);
parameters.withSecretAttributes(null);
parameters.withTags(null);
String parameterizedHost = Joiner.on(", ").join("{vaultBaseUrl}", vaultBaseUrl);
return service.updateSecret(secretName, secretVersion, this.apiVersion(), this.acceptLanguage(), parameters, parameterizedHost, this.userAgent()).flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<SecretBundle>>>() {
@Override
public Observable<ServiceResponse<SecretBundle>> call(Response<ResponseBody> response) {
try {
ServiceResponse<SecretBundle> clientResponse = updateSecretDelegate(response);
return Observable.just(clientResponse);
} catch (Throwable t) {
return Observable.error(t);
}
}
});
}
Aggregations