Search in sources :

Example 6 with AzureDockerException

use of com.microsoft.azure.docker.model.AzureDockerException in project azure-tools-for-java by Microsoft.

the class AzureDockerCertVaultOps method getSSHKeysFromLocalFile.

public static AzureDockerCertVault getSSHKeysFromLocalFile(String localPath) throws AzureDockerException {
    AzureDockerCertVault certVault = new AzureDockerCertVault();
    try {
        certVault.sshKey = new String(Files.readAllBytes(Paths.get(localPath, "id_rsa")));
        certVault.sshPubKey = new String(Files.readAllBytes(Paths.get(localPath, "id_rsa.pub")));
    } catch (Exception e) {
        throw new AzureDockerException(e.getMessage());
    }
    return certVault;
}
Also used : AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) AzureDockerCertVault(com.microsoft.azure.docker.model.AzureDockerCertVault) AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) CloudException(com.microsoft.azure.CloudException)

Example 7 with AzureDockerException

use of com.microsoft.azure.docker.model.AzureDockerException 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)

Example 8 with AzureDockerException

use of com.microsoft.azure.docker.model.AzureDockerException in project azure-tools-for-java by Microsoft.

the class AzureDockerCertVaultOps method getVault.

public static AzureDockerCertVault getVault(Azure azureClient, AzureDockerCertVault certVault, KeyVaultClient keyVaultClient) throws AzureDockerException {
    if (azureClient == null || certVault == null || keyVaultClient == null || certVault.name == null || certVault.resourceGroupName == null) {
        throw new AzureDockerException("Unexpected argument values; azureClient, vault name and resourceGroupName cannot be null");
    }
    Vault vault;
    try {
        vault = azureClient.vaults().getByResourceGroup(certVault.resourceGroupName, certVault.name);
        certVault.uri = vault.vaultUri();
    } catch (Exception e) {
        throw new AzureDockerException(e.getMessage());
    }
    return getVault(certVault, keyVaultClient);
}
Also used : AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) Vault(com.microsoft.azure.management.keyvault.Vault) AzureDockerCertVault(com.microsoft.azure.docker.model.AzureDockerCertVault) AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) CloudException(com.microsoft.azure.CloudException)

Example 9 with AzureDockerException

use of com.microsoft.azure.docker.model.AzureDockerException in project azure-tools-for-java by Microsoft.

the class AzureDockerCertVaultOps method getVault.

public static AzureDockerCertVault getVault(Azure azureClient, String name, String resourceGroupName, KeyVaultClient keyVaultClient) throws AzureDockerException {
    if (azureClient == null || keyVaultClient == null || name == null || resourceGroupName == null) {
        throw new AzureDockerException("Unexpected argument values; azureClient, vault name and resourceGroupName cannot be null");
    }
    AzureDockerCertVault tempVault = new AzureDockerCertVault();
    tempVault.name = name;
    tempVault.resourceGroupName = resourceGroupName;
    return getVault(azureClient, tempVault, keyVaultClient);
}
Also used : AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) AzureDockerCertVault(com.microsoft.azure.docker.model.AzureDockerCertVault)

Example 10 with AzureDockerException

use of com.microsoft.azure.docker.model.AzureDockerException in project azure-tools-for-java by Microsoft.

the class AzureDockerCertVaultOps method saveTlsCertsToLocalFiles.

public static void saveTlsCertsToLocalFiles(String localPath, AzureDockerCertVault certVault) throws AzureDockerException {
    try {
        String sep = AzureDockerUtils.getPathSeparator();
        if (certVault.tlsCACert != null) {
            FileWriter file = new FileWriter(localPath + sep + "ca.pem");
            file.write(certVault.tlsCACert);
            file.close();
        }
        if (certVault.tlsCAKey != null) {
            FileWriter file = new FileWriter(localPath + sep + "ca-key.pem");
            file.write(certVault.tlsCACert);
            file.close();
        }
        if (certVault.tlsClientCert != null) {
            FileWriter file = new FileWriter(localPath + sep + "cert.pem");
            file.write(certVault.tlsClientCert);
            file.close();
        }
        if (certVault.tlsClientKey != null) {
            FileWriter file = new FileWriter(localPath + sep + "key.pem");
            file.write(certVault.tlsClientKey);
            file.close();
        }
        if (certVault.tlsServerCert != null) {
            FileWriter file = new FileWriter(localPath + sep + "server.pem");
            file.write(certVault.tlsServerCert);
            file.close();
        }
        if (certVault.tlsServerKey != null) {
            FileWriter file = new FileWriter(localPath + sep + "server-key.pem");
            file.write(certVault.tlsServerKey);
            file.close();
        }
    } catch (Exception e) {
        throw new AzureDockerException(e.getMessage());
    }
}
Also used : AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) FileWriter(java.io.FileWriter) AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) CloudException(com.microsoft.azure.CloudException)

Aggregations

AzureDockerException (com.microsoft.azure.docker.model.AzureDockerException)16 CloudException (com.microsoft.azure.CloudException)9 AzureDockerCertVault (com.microsoft.azure.docker.model.AzureDockerCertVault)8 Vault (com.microsoft.azure.management.keyvault.Vault)3 FileWriter (java.io.FileWriter)3 JSch (com.jcraft.jsch.JSch)2 KeyPair (com.jcraft.jsch.KeyPair)2 ResourceGroup (com.microsoft.azure.management.resources.ResourceGroup)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 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 Pair (com.microsoft.azuretools.utils.Pair)1 ServiceCallback (com.microsoft.rest.ServiceCallback)1