Search in sources :

Example 1 with PrompterException

use of org.codehaus.plexus.components.interactivity.PrompterException in project fabric8-maven-plugin by fabric8io.

the class ImportMojo method chooseSshKeyPairs.

private void chooseSshKeyPairs(Map<String, String> secretData, String host) throws MojoExecutionException {
    String homeDir = System.getProperty("user.home", ".");
    File sshDir = new File(homeDir, ".ssh");
    SortedMap<String, String> keyPairs = new TreeMap<>();
    if (sshDir.isDirectory() && sshDir.exists()) {
        File[] files = sshDir.listFiles();
        if (files != null) {
            for (File file : files) {
                String publicName = file.getName();
                if (file.isFile() && publicName.endsWith(".pub")) {
                    String privateName = Strings.stripSuffix(publicName, ".pub");
                    if (new File(sshDir, privateName).isFile()) {
                        keyPairs.put(privateName, publicName);
                    }
                }
            }
        }
    }
    if (keyPairs.isEmpty()) {
        log.warn("No SSH key pairs could be found in %s to choose from!", sshDir);
        log.warn("You may want to clone the git repository over https:// instead to avoid ssh key pairs?");
    } else {
        if (keyPairs.size() == 0) {
            String privateName = keyPairs.firstKey();
            importSshKeys(secretData, sshDir, privateName, keyPairs.get(privateName));
        } else {
            List<String> privateKeys = new ArrayList<>(keyPairs.keySet());
            String privateKey = null;
            try {
                privateKey = prompter.prompt("Which public / private key pair do you wish to use for SSH authentication with host: " + host, privateKeys);
            } catch (PrompterException e) {
                log.warn("Failed to get user input: %s", e);
            }
            if (Strings.isNotBlank(privateKey)) {
                String publicKey = keyPairs.get(privateKey);
                if (Strings.isNullOrBlank(publicKey)) {
                    log.warn("Invalid answer: %s when available values are: %s", privateKey, privateKeys);
                } else {
                    importSshKeys(secretData, sshDir, privateKey, publicKey);
                }
            }
        }
    }
}
Also used : PrompterException(org.codehaus.plexus.components.interactivity.PrompterException) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) File(java.io.File)

Example 2 with PrompterException

use of org.codehaus.plexus.components.interactivity.PrompterException in project fabric8-maven-plugin by fabric8io.

the class ImportMojo method getGogsSecretField.

private String getGogsSecretField(KubernetesClient kubernetes, Secret gogsSecret, String gitRepoHost, String propertyName) throws MojoExecutionException {
    Map<String, String> data = gogsSecret.getData();
    if (data == null) {
        data = new HashMap<>();
        gogsSecret.setData(data);
    }
    String value = data.get(propertyName);
    if (Strings.isNullOrBlank(value) || retryPassword) {
        try {
            if (propertyName.equals("password")) {
                value = prompter.promptForPassword("Please enter your password/access token for git repo " + gitRepoHost);
            } else {
                value = prompter.prompt("Please enter your username for git repo " + gitRepoHost);
            }
        } catch (PrompterException e) {
            throw new MojoExecutionException("Failed to input required data: " + e, e);
        }
        data.put(propertyName, Base64Encoder.encode(value));
        gitSecretUpdated = true;
        return value;
    }
    return Base64Encoder.decode(value);
}
Also used : PrompterException(org.codehaus.plexus.components.interactivity.PrompterException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Example 3 with PrompterException

use of org.codehaus.plexus.components.interactivity.PrompterException in project maven-archetype by apache.

the class ArchetypePrompter method prompt.

@SuppressWarnings({ "rawtypes", "unchecked" })
public String prompt(String message, List possibleValues, String defaultReply) throws PrompterException {
    String formattedMessage = formatMessage(message, possibleValues, defaultReply);
    String line;
    do {
        writePrompt(formattedMessage);
        line = readLine();
        if (StringUtils.isEmpty(line)) {
            line = defaultReply;
        }
        if (line != null && !possibleValues.contains(line)) {
            try {
                outputHandler.writeLine("Invalid selection.");
            } catch (IOException e) {
                throw new PrompterException("Failed to present feedback", e);
            }
        }
    } while (line == null || !possibleValues.contains(line));
    return line;
}
Also used : PrompterException(org.codehaus.plexus.components.interactivity.PrompterException) IOException(java.io.IOException)

Example 4 with PrompterException

use of org.codehaus.plexus.components.interactivity.PrompterException in project fabric8-maven-plugin by fabric8io.

the class ImportMojo method ensureExternalGitSecretsAreSetupFor.

protected void ensureExternalGitSecretsAreSetupFor(KubernetesClient kubernetes, String namespace, String gitRemoteURL) throws MojoExecutionException {
    String secretNamespace = getSecretNamespace();
    ensureNamespaceExists(kubernetes, secretNamespace);
    ConfigMap configMap = getSecretGitConfigMap(kubernetes, namespace, secretNamespace);
    String host = GitUtils.getGitHostName(gitRemoteURL);
    if (host == null) {
        host = "default";
    }
    String protocol = GitUtils.getGitProtocol(gitRemoteURL);
    boolean isSsh = Objects.equal("ssh", protocol);
    String currentSecretName = configMap.getData().get(host);
    if (currentSecretName == null) {
        currentSecretName = createGitSecretName(namespace, host);
    }
    Secret secret = findOrCreateGitSecret(kubernetes, currentSecretName, host);
    if (isSsh) {
        // lets see if we need to import ssh keys
        Map<String, String> secretData = secret.getData();
        if (secretData == null) {
            secretData = new HashMap<>();
        }
        if (!secretData.containsKey(PROPERTY_PRIVATE_KEY) || !secretData.containsKey(PROPERTY_PUBLIC_KEY)) {
            String answer = null;
            try {
                answer = prompter.prompt("Would you like to import your local SSH public/private key pair from your ~/.ssh folder? (Y/n)");
            } catch (PrompterException e) {
                log.warn("Failed to get prompt: %s", e);
            }
            if (answer != null && answer.trim().isEmpty() || answer.trim().toUpperCase().startsWith("Y")) {
                chooseSshKeyPairs(secretData, host);
                secret.setData(secretData);
            }
        }
    } else {
        // if empty or retrying lets re-enter the user/pwd
        getGogsSecretField(kubernetes, secret, host, "username");
        getGogsSecretField(kubernetes, secret, host, "password");
    }
    createOrUpdateSecret(kubernetes, secret);
    updateSecretGitConfigMap(kubernetes, secretNamespace, configMap, host, currentSecretName);
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) PrompterException(org.codehaus.plexus.components.interactivity.PrompterException) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap)

Aggregations

PrompterException (org.codehaus.plexus.components.interactivity.PrompterException)4 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)1 Secret (io.fabric8.kubernetes.api.model.Secret)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1