From d8a9c75a9646b0ffc344c49fbab73e7b2ab7f99f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janosch=20Sch=C3=A4fer?= Date: Thu, 8 Feb 2024 21:05:58 +0100 Subject: [PATCH 1/4] Add support for personal access tokens --- pom.xml | 4 ++++ .../org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/pom.xml b/pom.xml index 1949273e6f..8ad729f139 100644 --- a/pom.xml +++ b/pom.xml @@ -203,6 +203,10 @@ org.jenkins-ci.plugins ssh-credentials + + org.jenkins-ci.plugins + plain-credentials + org.jenkins-ci.plugins structs diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java index f27474cb78..79f3475e92 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java @@ -71,6 +71,7 @@ import org.eclipse.jgit.transport.RemoteConfig; import org.eclipse.jgit.transport.URIish; import org.jenkinsci.plugins.gitclient.cgit.GitCommandsExecutor; +import org.jenkinsci.plugins.plaincredentials.StringCredentials; import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted; import org.kohsuke.stapler.framework.io.WriterOutputStream; @@ -2150,6 +2151,14 @@ private String launchCommandWithCredentials( env = new EnvVars(env); env.put("GIT_ASKPASS", askpass.toAbsolutePath().toString()); env.put("SSH_ASKPASS", askpass.toAbsolutePath().toString()); + } else if (credentials instanceof StringCredentials) { + var stringCred = (StringCredentials) credentials; + listener.getLogger().println("using GIT_CONFIG to set token header " + stringCred.getDescription()); + + env = new EnvVars(env); + env.put("GIT_CONFIG_COUNT", "0"); + env.put("GIT_CONFIG_KEY_0", "http.extraHeader"); + env.put("GIT_CONFIG_VALUE_0", "Authorization: Bearer " + stringCred.getSecret()); } if ("http".equalsIgnoreCase(url.getScheme()) || "https".equalsIgnoreCase(url.getScheme())) { From c07068b164eeadaa0d84cd41d6e04c2b74671f8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janosch=20Sch=C3=A4fer?= Date: Thu, 8 Feb 2024 21:10:49 +0100 Subject: [PATCH 2/4] Code formatting --- pom.xml | 6 +++--- .../java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 8ad729f139..9b326213de 100644 --- a/pom.xml +++ b/pom.xml @@ -197,15 +197,15 @@ org.jenkins-ci.plugins - script-security + plain-credentials org.jenkins-ci.plugins - ssh-credentials + script-security org.jenkins-ci.plugins - plain-credentials + ssh-credentials org.jenkins-ci.plugins diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java index 79f3475e92..48ebd1dd42 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java @@ -2151,6 +2151,7 @@ private String launchCommandWithCredentials( env = new EnvVars(env); env.put("GIT_ASKPASS", askpass.toAbsolutePath().toString()); env.put("SSH_ASKPASS", askpass.toAbsolutePath().toString()); + } else if (credentials instanceof StringCredentials) { var stringCred = (StringCredentials) credentials; listener.getLogger().println("using GIT_CONFIG to set token header " + stringCred.getDescription()); From ed9b23d8aa4c5a2dcc3cb0967b3589981e1caad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janosch=20Sch=C3=A4fer?= Date: Tue, 13 Feb 2024 20:01:37 +0100 Subject: [PATCH 3/4] Fix config count --- .../java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java index 48ebd1dd42..dcf97e9245 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java @@ -2157,7 +2157,7 @@ private String launchCommandWithCredentials( listener.getLogger().println("using GIT_CONFIG to set token header " + stringCred.getDescription()); env = new EnvVars(env); - env.put("GIT_CONFIG_COUNT", "0"); + env.put("GIT_CONFIG_COUNT", "1"); env.put("GIT_CONFIG_KEY_0", "http.extraHeader"); env.put("GIT_CONFIG_VALUE_0", "Authorization: Bearer " + stringCred.getSecret()); } From 033ce58b51725058c15c5c94589154fb1afbe0f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janosch=20Sch=C3=A4fer?= Date: Tue, 13 Feb 2024 20:38:06 +0100 Subject: [PATCH 4/4] Add StringCredentials as supported type --- src/main/java/org/jenkinsci/plugins/gitclient/GitClient.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jenkinsci/plugins/gitclient/GitClient.java b/src/main/java/org/jenkinsci/plugins/gitclient/GitClient.java index 639c46488a..38c8bd36f5 100644 --- a/src/main/java/org/jenkinsci/plugins/gitclient/GitClient.java +++ b/src/main/java/org/jenkinsci/plugins/gitclient/GitClient.java @@ -23,6 +23,7 @@ import org.eclipse.jgit.transport.RefSpec; import org.eclipse.jgit.transport.RemoteConfig; import org.eclipse.jgit.transport.URIish; +import org.jenkinsci.plugins.plaincredentials.StringCredentials; /** * Interface to Git functionality. @@ -47,7 +48,8 @@ public interface GitClient { */ CredentialsMatcher CREDENTIALS_MATCHER = CredentialsMatchers.anyOf( CredentialsMatchers.instanceOf(StandardUsernamePasswordCredentials.class), - CredentialsMatchers.instanceOf(SSHUserPrivateKey.class)); + CredentialsMatchers.instanceOf(SSHUserPrivateKey.class), + CredentialsMatchers.instanceOf(StringCredentials.class)); /** * Remove all credentials from the client.