Skip to content

Commit

Permalink
Adapt writing ASKPASS from git-client-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Seros committed Jul 11, 2023
1 parent 12ae8e5 commit 54cfe20
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions src/main/java/jenkins/plugins/git/GitUsernamePasswordBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,28 @@ protected GenerateGitScript(String gitUsername, String gitPassword,
protected FilePath write(StandardUsernamePasswordCredentials credentials, FilePath workspace)
throws IOException, InterruptedException {
FilePath gitEcho;
//Hard Coded platform dependent newLine

FilePath usernameFile = workspace.createTempFile("username", ".txt");
usernameFile.write(this.userVariable + "\n", null);
FilePath passwordFile = workspace.createTempFile("password", ".txt");
passwordFile.write(this.passVariable + "\n", null);

//Hard Coded platform dependent newLine
if (this.unixNodeType) {
gitEcho = workspace.createTempFile("auth", ".sh");
// [#!/usr/bin/env sh] to be used if required, could have some corner cases
gitEcho.write("case $1 in\n"
+ " Username*) echo '" + this.userVariable.replace("'", "'\\''") + "'"
+ " ;;\n"
+ " Password*) echo '" + this.passVariable.replace("'", "'\\''") + "'"
+ " ;;\n"
gitEcho.write("#!/bin/sh\n"
+ "\n"
+ "case \"$1\" in\n"
+ " Username*) cat " + unixArgEncodeFileName(usernameFile.getRemote()) + ";;\n"
+ " Password*) cat " + unixArgEncodeFileName(passwordFile.getRemote()) + ";;\n"
+ " esac\n", null);
gitEcho.chmod(0500);
} else {
gitEcho = workspace.createTempFile("auth", ".bat");
gitEcho.write("@ECHO OFF\r\n"
+ "SET ARG=%~1\r\n"
+ "IF %ARG:~0,8%==Username (ECHO \"" + this.userVariable.replace("%", "%%") + "\")\r\n"
+ "IF %ARG:~0,8%==Password (ECHO \"" + this.passVariable.replace("%", "%%") + "\")", null);
+ "IF %ARG:~0,8%==Username type " + windowsArgEncodeFileName(usernameFile.getRemote()) + "\r\n"

Check warning on line 168 in src/main/java/jenkins/plugins/git/GitUsernamePasswordBinding.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 168 is not covered by tests
+ "IF %ARG:~0,8%==Password type " + windowsArgEncodeFileName(passwordFile.getRemote()), null);

Check warning on line 169 in src/main/java/jenkins/plugins/git/GitUsernamePasswordBinding.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 169 is not covered by tests
}
return gitEcho;
}
Expand All @@ -170,6 +175,20 @@ protected FilePath write(StandardUsernamePasswordCredentials credentials, FilePa
protected Class<StandardUsernamePasswordCredentials> type() {
return StandardUsernamePasswordCredentials.class;
}

protected String unixArgEncodeFileName(String filename) {
if (filename.contains("'")) {

Check warning on line 180 in src/main/java/jenkins/plugins/git/GitUsernamePasswordBinding.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 180 is only partially covered, one branch is missing
filename = filename.replace("'", "'\\''");

Check warning on line 181 in src/main/java/jenkins/plugins/git/GitUsernamePasswordBinding.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 181 is not covered by tests
}
return "'" + filename + "'";
}

protected String windowsArgEncodeFileName(String filename) {
if (filename.contains("\"")) {

Check warning on line 187 in src/main/java/jenkins/plugins/git/GitUsernamePasswordBinding.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 187 is only partially covered, 2 branches are missing
filename = filename.replace("\"", "^\"");

Check warning on line 188 in src/main/java/jenkins/plugins/git/GitUsernamePasswordBinding.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 188 is not covered by tests
}
return "\"" + filename + "\"";

Check warning on line 190 in src/main/java/jenkins/plugins/git/GitUsernamePasswordBinding.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 190 is not covered by tests
}
}

// Mistakenly defined GitUsernamePassword in first release, prefer gitUsernamePassword as symbol
Expand Down

0 comments on commit 54cfe20

Please sign in to comment.