Skip to content

Commit

Permalink
fix(gitrepo): Support to authenticate with user and token (#5733) (#5736
Browse files Browse the repository at this point in the history
)

Co-authored-by: Nemesis Osorio <nemesis211_6@hotmail.com>

Co-authored-by: Ovidiu Popa <ovidiu.popa@armory.io>
(cherry picked from commit 8d8686b)

Co-authored-by: nemesisOsorio <nemesis211_6@hotmail.com>
  • Loading branch information
mergify[bot] and nemesisOsorio authored Jun 14, 2022
1 parent 1cf66a0 commit 2cbecd7
Showing 1 changed file with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class GitJobExecutor {

private enum AuthType {
USER_PASS,
USER_TOKEN,
TOKEN,
SSH,
NONE
Expand All @@ -68,8 +69,10 @@ public GitJobExecutor(
if (!StringUtils.isEmpty(account.getUsername())
&& !StringUtils.isEmpty(account.getPassword())) {
authType = AuthType.USER_PASS;
} else if (account.getTokenAsString().isPresent()
&& !StringUtils.isEmpty(account.getTokenAsString())) {
} else if (!StringUtils.isEmpty(account.getUsername())
&& account.getTokenAsString().filter(t -> !StringUtils.isEmpty(t)).isPresent()) {
authType = AuthType.USER_TOKEN;
} else if (account.getTokenAsString().filter(t -> !StringUtils.isEmpty(t)).isPresent()) {
authType = AuthType.TOKEN;
} else if (!StringUtils.isEmpty(account.getSshPrivateKeyFilePath())) {
authType = AuthType.SSH;
Expand Down Expand Up @@ -285,7 +288,9 @@ private Path initAskPass() throws IOException {
}

private boolean isValidReference(String reference) {
if (authType == AuthType.USER_PASS || authType == AuthType.TOKEN) {
if (authType == AuthType.USER_PASS
|| authType == AuthType.USER_TOKEN
|| authType == AuthType.TOKEN) {
return reference.startsWith("http");
}
if (authType == AuthType.SSH) {
Expand All @@ -298,6 +303,7 @@ private List<String> cmdToList(String cmd) {
List<String> cmdList = new ArrayList<>();
switch (authType) {
case USER_PASS:
case USER_TOKEN:
case TOKEN:
// "sh" subshell is used so that environment variables can be used as part of the command
cmdList.add("sh");
Expand All @@ -313,13 +319,17 @@ private List<String> cmdToList(String cmd) {
}

private String repoUrlWithAuth(String repoUrl) {
if (authType != AuthType.USER_PASS && authType != AuthType.TOKEN) {
if (authType != AuthType.USER_PASS
&& authType != AuthType.USER_TOKEN
&& authType != AuthType.TOKEN) {
return repoUrl;
}

String authPart;
if (authType == AuthType.USER_PASS) {
authPart = "$GIT_USER:$GIT_PASS";
} else if (authType == AuthType.USER_TOKEN) {
authPart = "$GIT_USER:$GIT_TOKEN";
} else {
authPart = "token:$GIT_TOKEN";
}
Expand All @@ -346,6 +356,18 @@ private Map<String, String> addEnvVars(Map<String, String> env) {
result.put("GIT_USER", encodeURIComponent(account.getUsername()));
result.put("GIT_PASS", encodeURIComponent(account.getPassword()));
break;
case USER_TOKEN:
result.put("GIT_USER", encodeURIComponent(account.getUsername()));
result.put(
"GIT_TOKEN",
encodeURIComponent(
account
.getTokenAsString()
.orElseThrow(
() ->
new IllegalArgumentException(
"Token or TokenFile must be present if using token auth."))));
break;
case TOKEN:
result.put(
"GIT_TOKEN",
Expand Down

0 comments on commit 2cbecd7

Please sign in to comment.