Search in sources :

Example 16 with AzureDockerCertVault

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

the class AzureDockerUIResources method createDockerKeyVault.

public static void createDockerKeyVault(DockerHost dockerHost, AzureDockerHostsManager dockerManager) {
    Job createDockerHostJob = new Job(String.format("Creating Azure Key Vault %s for %s", dockerHost.certVault.name, dockerHost.name)) {

        @Override
        protected IStatus run(IProgressMonitor progressMonitor) {
            progressMonitor.beginTask("start task", 100);
            try {
                progressMonitor.subTask(String.format("Reading subscription details for Docker host %s ...", dockerHost.apiUrl));
                progressMonitor.worked(5);
                Azure azureClient = dockerManager.getSubscriptionsMap().get(dockerHost.sid).azureClient;
                KeyVaultClient keyVaultClient = dockerManager.getSubscriptionsMap().get(dockerHost.sid).keyVaultClient;
                if (progressMonitor.isCanceled()) {
                    progressMonitor.done();
                    return Status.CANCEL_STATUS;
                }
                String retryMsg = "Create";
                int retries = 5;
                AzureDockerCertVault certVault = null;
                do {
                    progressMonitor.subTask(String.format("%s new key vault %s ...", retryMsg, dockerHost.certVault.name));
                    progressMonitor.worked(15 + 15 * retries);
                    if (AzureDockerUtils.DEBUG)
                        System.out.println(retryMsg + " new Docker key vault: " + new Date().toString());
                    AzureDockerCertVaultOps.createOrUpdateVault(azureClient, dockerHost.certVault, keyVaultClient);
                    if (AzureDockerUtils.DEBUG)
                        System.out.println("Done creating new key vault: " + new Date().toString());
                    if (progressMonitor.isCanceled()) {
                        progressMonitor.done();
                        return Status.CANCEL_STATUS;
                    }
                    certVault = AzureDockerCertVaultOps.getVault(azureClient, dockerHost.certVault.name, dockerHost.certVault.resourceGroupName, keyVaultClient);
                    retries++;
                    retryMsg = "Retry creating";
                } while (// Retry couple times
                retries < 5 && (certVault == null || certVault.vmUsername == null));
                progressMonitor.subTask("Updating key vaults ...");
                progressMonitor.worked(95);
                if (AzureDockerUtils.DEBUG)
                    System.out.println("Refreshing key vaults: " + new Date().toString());
                dockerManager.refreshDockerVaults();
                dockerManager.refreshDockerVaultDetails();
                if (AzureDockerUtils.DEBUG)
                    System.out.println("Done refreshing key vaults: " + new Date().toString());
                //					progressMonitor.subTask("");
                //					progressMonitor.worked(1);
                //					if (progressMonitor.isCanceled()) {
                //						if (displayWarningOnCreateKeyVaultCancelAction() == 0) {
                //							progressMonitor.done();
                //							return Status.CANCEL_STATUS;
                //						}
                //					}
                //
                progressMonitor.done();
                return Status.OK_STATUS;
            } catch (Exception e) {
                String msg = "An error occurred while attempting to create a new Azure Key Vault." + "\n" + e.getMessage();
                log.log(Level.SEVERE, "createDockerKeyVault: " + msg, e);
                e.printStackTrace();
                PluginUtil.displayErrorDialog(Display.getDefault().getActiveShell(), "Error Creating Azure Key Vault " + dockerHost.certVault.name, "An error occurred while attempting to create a new Azure Key Vault." + "\n" + e.getMessage());
                return Status.CANCEL_STATUS;
            }
        }
    };
    createDockerHostJob.schedule();
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Azure(com.microsoft.azure.management.Azure) KeyVaultClient(com.microsoft.azure.keyvault.KeyVaultClient) AzureDockerCertVault(com.microsoft.azure.docker.model.AzureDockerCertVault) Job(org.eclipse.core.runtime.jobs.Job) Date(java.util.Date)

Example 17 with AzureDockerCertVault

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

the class AzureDockerCertVaultOps method cloneVault.

public static void cloneVault(Azure azureClientSource, AzureDockerCertVault certVaultSource, Azure azureClientDest, AzureDockerCertVault certVaultDest, KeyVaultClient keyVaultClient) throws AzureDockerException {
    if (azureClientSource == null || certVaultSource == null || certVaultSource.name == null || certVaultSource.resourceGroupName == null || azureClientDest == null || certVaultDest == null || certVaultDest.name == null || certVaultDest.resourceGroupName == null || certVaultDest.region == null || (certVaultDest.servicePrincipalId == null && certVaultDest.userId == null) || keyVaultClient == null) {
        throw new AzureDockerException("Unexpected argument values; azureClient, vault name, hostName, resourceGroupName and destination region and userName/servicePrincipalId cannot be null");
    }
    try {
        AzureDockerCertVault certVaultResult = getVault(azureClientSource, certVaultSource, keyVaultClient);
        certVaultResult.name = certVaultDest.name;
        certVaultResult.id = certVaultDest.id;
        certVaultResult.resourceGroupName = certVaultDest.resourceGroupName;
        certVaultResult.region = certVaultDest.region;
        certVaultResult.userId = certVaultDest.userId;
        certVaultResult.servicePrincipalId = certVaultDest.servicePrincipalId;
        createOrUpdateVault(azureClientDest, certVaultResult, keyVaultClient);
    } catch (Exception e) {
        throw new AzureDockerException(e.getMessage());
    }
}
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 18 with AzureDockerCertVault

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

the class AzureDockerCertVaultOps method generateSSHKeys.

public static AzureDockerCertVault generateSSHKeys(String passPhrase, String comment) throws AzureDockerException {
    try {
        AzureDockerCertVault result = new AzureDockerCertVault();
        JSch jsch = new JSch();
        KeyPair keyPair = KeyPair.genKeyPair(jsch, KeyPair.RSA);
        ByteArrayOutputStream privateKeyBuff = new ByteArrayOutputStream(2048);
        ByteArrayOutputStream publicKeyBuff = new ByteArrayOutputStream(2048);
        keyPair.writePublicKey(publicKeyBuff, (comment != null) ? comment : "DockerSSHCerts");
        if (passPhrase == null || passPhrase.isEmpty()) {
            keyPair.writePrivateKey(privateKeyBuff);
        } else {
            keyPair.writePrivateKey(privateKeyBuff, passPhrase.getBytes());
        }
        result.sshKey = privateKeyBuff.toString();
        result.sshPubKey = publicKeyBuff.toString();
        return result;
    } catch (Exception e) {
        throw new AzureDockerException(e.getMessage());
    }
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) AzureDockerCertVault(com.microsoft.azure.docker.model.AzureDockerCertVault) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSch(com.jcraft.jsch.JSch) AzureDockerException(com.microsoft.azure.docker.model.AzureDockerException) CloudException(com.microsoft.azure.CloudException)

Aggregations

AzureDockerCertVault (com.microsoft.azure.docker.model.AzureDockerCertVault)18 AzureDockerException (com.microsoft.azure.docker.model.AzureDockerException)8 CloudException (com.microsoft.azure.CloudException)7 KeyVaultClient (com.microsoft.azure.keyvault.KeyVaultClient)3 Azure (com.microsoft.azure.management.Azure)3 Vault (com.microsoft.azure.management.keyvault.Vault)3 ValidationInfo (com.intellij.openapi.ui.ValidationInfo)2 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 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ModifyEvent (org.eclipse.swt.events.ModifyEvent)2 ModifyListener (org.eclipse.swt.events.ModifyListener)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 DirectoryDialog (org.eclipse.swt.widgets.DirectoryDialog)2 Text (org.eclipse.swt.widgets.Text)2