Search in sources :

Example 1 with ServiceCallback

use of com.microsoft.rest.ServiceCallback in project azure-tools-for-java by Microsoft.

the class AzureDockerCertVaultOps method getVault.

public static AzureDockerCertVault getVault(AzureDockerCertVault certVault, KeyVaultClient keyVaultClient) throws AzureDockerException {
    if (certVault == null || keyVaultClient == null || certVault.uri == null) {
        throw new AzureDockerException("Unexpected argument values; azureClient, vault name and resourceGroupName cannot be null");
    }
    String vaultUri = certVault.uri;
    try {
        SecretBundle secret = keyVaultClient.getSecret(vaultUri, SECRETENTRY_DOCKERHOSTNAMES);
        if (secret != null) {
            certVault.hostName = secret.value();
        } else {
            certVault.hostName = null;
            return null;
        }
    } catch (Exception e) {
        return null;
    }
    //Execute Key Vault Secret read in parallel
    Map<String, String> secretNamesAndValueMap = new HashMap<>();
    Observable.from(DOCKERHOST_SECRETS).flatMap(secretName -> {
        return Observable.create(new Observable.OnSubscribe<Pair<String, String>>() {

            @Override
            public void call(Subscriber<? super Pair<String, String>> subscriber) {
                keyVaultClient.getSecretAsync(vaultUri, secretName, new ServiceCallback<SecretBundle>() {

                    @Override
                    public void failure(Throwable throwable) {
                        subscriber.onCompleted();
                    }

                    @Override
                    public void success(SecretBundle secretBundle) {
                        if (secretBundle != null) {
                            subscriber.onNext(new Pair<>(secretName, secretBundle.value()));
                        }
                        subscriber.onCompleted();
                    }
                });
            }
        }).subscribeOn(Schedulers.io());
    }, 5).subscribeOn(Schedulers.io()).toBlocking().subscribe(new Action1<Pair<String, String>>() {

        @Override
        public void call(Pair<String, String> secretNameAndValue) {
            secretNamesAndValueMap.put(secretNameAndValue.first(), secretNameAndValue.second());
        }
    });
    String currentSecretValue;
    currentSecretValue = secretNamesAndValueMap.get("vmUsername");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.vmUsername = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("vmPwd");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.vmPwd = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("sshKey");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.sshKey = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("sshPubKey");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.sshPubKey = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("tlsCACert");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.tlsCACert = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("tlsCAKey");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.tlsCAKey = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("tlsClientCert");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.tlsClientCert = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("tlsClientKey");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.tlsClientKey = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("tlsServerCert");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.tlsServerCert = currentSecretValue;
    }
    currentSecretValue = secretNamesAndValueMap.get("tlsServerKey");
    if (currentSecretValue != null && !currentSecretValue.isEmpty()) {
        certVault.tlsServerKey = currentSecretValue;
    }
    return certVault;
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) JSch(com.jcraft.jsch.JSch) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashMap(java.util.HashMap) Action1(rx.functions.Action1) DEBUG(com.microsoft.azure.docker.ops.utils.AzureDockerUtils.DEBUG) SecretBundle(com.microsoft.azure.keyvault.models.SecretBundle) Observable(rx.Observable) Azure(com.microsoft.azure.management.Azure) Map(java.util.Map) Schedulers(rx.schedulers.Schedulers) DefaultLoader(com.microsoft.tooling.msservices.components.DefaultLoader) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) Vault(com.microsoft.azure.management.keyvault.Vault) Subscriber(rx.Subscriber) ServiceCallback(com.microsoft.rest.ServiceCallback) Files(java.nio.file.Files) AzureDockerUtils(com.microsoft.azure.docker.ops.utils.AzureDockerUtils) FileWriter(java.io.FileWriter) Pair(com.microsoft.azuretools.utils.Pair) SetSecretRequest(com.microsoft.azure.keyvault.requests.SetSecretRequest) SecretPermissions(com.microsoft.azure.management.keyvault.SecretPermissions) List(java.util.List) AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) AzureDockerCertVault(com.microsoft.azure.docker.model.AzureDockerCertVault) Paths(java.nio.file.Paths) CloudException(com.microsoft.azure.CloudException) KeyVaultClient(com.microsoft.azure.keyvault.KeyVaultClient) HashMap(java.util.HashMap) AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) CloudException(com.microsoft.azure.CloudException) Observable(rx.Observable) SecretBundle(com.microsoft.azure.keyvault.models.SecretBundle) ServiceCallback(com.microsoft.rest.ServiceCallback) AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) KeyPair(com.jcraft.jsch.KeyPair) Pair(com.microsoft.azuretools.utils.Pair)

Aggregations

JSch (com.jcraft.jsch.JSch)1 KeyPair (com.jcraft.jsch.KeyPair)1 CloudException (com.microsoft.azure.CloudException)1 AzureDockerCertVault (com.microsoft.azure.docker.model.AzureDockerCertVault)1 AzureDockerException (com.microsoft.azure.docker.model.AzureDockerException)1 AzureDockerUtils (com.microsoft.azure.docker.ops.utils.AzureDockerUtils)1 DEBUG (com.microsoft.azure.docker.ops.utils.AzureDockerUtils.DEBUG)1 KeyVaultClient (com.microsoft.azure.keyvault.KeyVaultClient)1 SecretBundle (com.microsoft.azure.keyvault.models.SecretBundle)1 SetSecretRequest (com.microsoft.azure.keyvault.requests.SetSecretRequest)1 Azure (com.microsoft.azure.management.Azure)1 SecretPermissions (com.microsoft.azure.management.keyvault.SecretPermissions)1 Vault (com.microsoft.azure.management.keyvault.Vault)1 ResourceGroup (com.microsoft.azure.management.resources.ResourceGroup)1 Pair (com.microsoft.azuretools.utils.Pair)1 ServiceCallback (com.microsoft.rest.ServiceCallback)1 DefaultLoader (com.microsoft.tooling.msservices.components.DefaultLoader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileWriter (java.io.FileWriter)1 Files (java.nio.file.Files)1