Search in sources :

Example 1 with CommandResult

use of com.google.copybara.shell.CommandResult in project copybara by google.

the class GitCredential method fill.

/**
 * Execute 'git credential fill' for a url
 *
 * @param cwd the directory to execute the command. This is important if credential configuration
 * is set in the local git config.
 * @param url url to get the credentials from
 * @return a username and password
 * @throws RepoException If the url doesn't have protocol (For example https://), the url is
 * not valid or username/password couldn't be found
 */
public UserPassword fill(Path cwd, String url) throws RepoException, ValidationException {
    Map<String, String> env = gitEnv.withNoGitPrompt().getEnvironment();
    URI uri;
    try {
        uri = URI.create(url);
    } catch (IllegalArgumentException e) {
        throw new ValidationException("Cannot get credentials for " + url, e);
    }
    String protocol = uri.getScheme();
    checkCondition(!Strings.isNullOrEmpty(protocol), "Cannot find the protocol for %s", url);
    String host = uri.getHost();
    Command cmd = new Command(new String[] { gitBinary, "credential", "fill" }, env, cwd.toFile());
    String request = format("protocol=%s\nhost=%s\n", protocol, host);
    if (!Strings.isNullOrEmpty(uri.getPath())) {
        request += format("path=%s\n", CharMatcher.is('/').trimLeadingFrom(uri.getPath()));
    }
    request += "\n";
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayOutputStream err = new ByteArrayOutputStream();
    try {
        // DON'T REPLACE THIS WITH CommandRunner.execute(). WE DON'T WANT TO ACCIDENTALLY LOG THE
        // PASSWORD!
        CommandResult result = cmd.execute(new ByteArrayInputStream(request.getBytes(UTF_8)), new TimeoutKillableObserver(timeout), out, err);
        if (!result.getTerminationStatus().success()) {
            throw new RepoException("Error getting credentials:\n" + new String(err.toByteArray(), UTF_8));
        }
    } catch (BadExitStatusException e) {
        String errStr = new String(err.toByteArray(), UTF_8);
        checkCondition(!errStr.contains("could not read"), "Interactive prompting of passwords for git is disabled," + " use git credential store before calling Copybara.");
        throw new RepoException("Error getting credentials:\n" + errStr, e);
    } catch (CommandException e) {
        throw new RepoException("Error getting credentials", e);
    }
    Map<String, String> map = Splitter.on(NEW_LINE).omitEmptyStrings().withKeyValueSeparator("=").split(new String(out.toByteArray(), UTF_8));
    if (!map.containsKey("username")) {
        throw new RepoException("git credentials for " + url + " didn't return a username");
    }
    if (!map.containsKey("password")) {
        throw new RepoException("git credentials for " + url + " didn't return a password");
    }
    return new UserPassword(map.get("username"), map.get("password"));
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) TimeoutKillableObserver(com.google.copybara.shell.TimeoutKillableObserver) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RepoException(com.google.copybara.exception.RepoException) CommandException(com.google.copybara.shell.CommandException) URI(java.net.URI) CommandResult(com.google.copybara.shell.CommandResult) Command(com.google.copybara.shell.Command) ByteArrayInputStream(java.io.ByteArrayInputStream) BadExitStatusException(com.google.copybara.shell.BadExitStatusException)

Aggregations

RepoException (com.google.copybara.exception.RepoException)1 ValidationException (com.google.copybara.exception.ValidationException)1 BadExitStatusException (com.google.copybara.shell.BadExitStatusException)1 Command (com.google.copybara.shell.Command)1 CommandException (com.google.copybara.shell.CommandException)1 CommandResult (com.google.copybara.shell.CommandResult)1 TimeoutKillableObserver (com.google.copybara.shell.TimeoutKillableObserver)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 URI (java.net.URI)1