From 8cdb1e0d839dee49b84dc71a4a3420199fe90f26 Mon Sep 17 00:00:00 2001 From: rsandell Date: Tue, 15 Mar 2022 18:56:58 +0100 Subject: [PATCH 01/11] Disable local git hooks by default. And added a configuration to enable them again if needed. --- README.adoc | 26 ++ images/git-security-configuration.png | Bin 0 -> 18966 bytes src/main/java/hudson/plugins/git/GitSCM.java | 3 + .../plugins/git/AbstractGitSCMSource.java | 2 + .../plugins/git/GitHooksConfiguration.java | 141 ++++++++++ .../jenkins/plugins/git/GitSCMFileSystem.java | 2 + .../git/GitHooksConfiguration/config.jelly | 36 +++ .../help-allowedOnAgents.html | 23 ++ .../help-allowedOnController.html | 25 ++ .../java/hudson/plugins/git/GitHooksTest.java | 258 ++++++++++++++++++ .../git/GitHooksConfigurationTest.java | 211 ++++++++++++++ 11 files changed, 727 insertions(+) create mode 100644 images/git-security-configuration.png create mode 100644 src/main/java/jenkins/plugins/git/GitHooksConfiguration.java create mode 100644 src/main/resources/jenkins/plugins/git/GitHooksConfiguration/config.jelly create mode 100644 src/main/resources/jenkins/plugins/git/GitHooksConfiguration/help-allowedOnAgents.html create mode 100644 src/main/resources/jenkins/plugins/git/GitHooksConfiguration/help-allowedOnController.html create mode 100644 src/test/java/hudson/plugins/git/GitHooksTest.java create mode 100644 src/test/java/jenkins/plugins/git/GitHooksConfigurationTest.java diff --git a/README.adoc b/README.adoc index 8f54a592a5..dc74c8c86c 100644 --- a/README.adoc +++ b/README.adoc @@ -277,6 +277,32 @@ If the workspace is removed, the tag that was applied is lost. Tagging a workspace made sense when using centralized repositories that automatically applied the tag to the centralized repository. Applying a git tag in an agent workspace doesn't have many practical uses. +[#security-configuration] +=== Security Configuration + +image:/images/git-security-configuration.png[Security Configuration] + +In the `Configure Global Security` page, the Git Plugin provides the following option: + +[[global-security-git-hooks]] +Git Hooks:: + + link:https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks[Git hooks] allow scripts to be invoked when certain important git repository actions occur. + This configuration controls the execution of client-side hooks on the controller and on agents. + It is recommended that git hooks be **disabled** on the controller and on agents. ++ +Most git repositories do not use hooks in the repository and do not need repository hooks. +In those rare cases where repository hooks are needed, it is highly recommended that they are **disabled** on the Jenkins controller and on Jenkins agents. ++ +Client-side hooks are **not** copied when the repository is cloned. +However, client-side hooks might be installed in a repository by build steps or by misconfiguration. ++ +If hook scripts are allowed, a client-side hook script installed in a repository will execute when the matching git operation is performed. +For example, if hooks are allowed and a git repository includes a `post-checkout` hook, the hook script will run after any checkout in that repository. +If hooks are allowed and a git repository includes a `pre-auto-gc` hook, the hook script will run before any automatic git garbage collection task. ++ +See link:https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks["Customizing Git - Git Hooks"] for more details about git repository hooks. + [#repository-browser] === Repository Browser diff --git a/images/git-security-configuration.png b/images/git-security-configuration.png new file mode 100644 index 0000000000000000000000000000000000000000..711daaff4677fb6441e785f952bc7acbc2593def GIT binary patch literal 18966 zcmb@O1yEc~x2}`m!QCN1aCe6Q10*;Ehk@Yk?g{P$f(4i0?l3rmJHg#`aCh$T{r{fSn4=gO{vnW@>;d-v+zy;eW#T@$9HAdUK-=>3~FZ&1H{2B^Gw^9~Gszl{hF{a?Rx z{|Wl?)=5QL{7v~N$v*T0teKd+*qb+1G02YwaL~_4_Mf$!-n>EUe*Jr!z=%ft=FOLq zF90!BcfF%!bnmYw{;ZGWcmX*s1YnL8HomA$CF9^wD*-;#p#_xYvRkwupKMQ?Bd@Y0 z!P>)GQc>1v;No$s&9Yir34QZplVIL4bJ>aJ1L0?2c6w3K5I5<(vj!sPXHD+N&r*WM z@KIm6l-x6S(|+;1q~zBI8T#srgrxcLzc{8mo^SDw{dQ1(g9$y9--D#QK2kyt8GaNn z0D8n%69k8ahKACau%lqVo-t@hKm+|gC%%VG?)3!v2!mwk_n|6yV7AvL@|c1R{%Jt# zziJ@GL3#Y17RW}>jX<8(UEp_%L+IGJ5S=iTdtCZ>MJu{{pZX0B;S`-Qo!dLiud<{9k_|4#Nek{kXz z(sfl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H8!hqlNV)bvA;UAJK>Ib(DKmw)q>}C-6@>%XReTYLdtEG z%gv?^=cP}5bKQK!&SsNs@`fwdvTiRu{%NXM#r^)cq(#@gOurdD`cT>K{c`H03%qL^ z%JwXsn5=c4U44&Ts>6q*@JhWss-V@vy*Lyw_^H3O@f_f=yW`Npep?Kk%TCDoJIY-48kbE?LjY z_*DI$%4)A8!oMnS{kKcldt8NSS~$z3iSyShcP_Y*-cqx1$2kzb~DZr?0b>~FEwODS8dotQL&bAAFUBRtwSn_{~ zT*S_rGaoDX0@|@GBrf+%n;n$@{78!rDGXFo>Vz+Qh!=))TcdLPg#jGe`2mi_WQ;M(rZ+AM) zTN%SL3E068dk5~|`(o|jM@(O>yV|eiNZ=|@jnkerjIlA^+O8R27rERH`>e&j=J%HQ zAA!SaYRogFI-a%$Jrqwo`Im3~KY>Lo>mPEz_;NzLu_{IWtcIhp)MFd{Sy4+BOXj#r z=w4Inaej1Ni}TjeWWnEB-j1_fj!YNt&phpGcjwgOg=~+W=-=SpZ=oyRH+A7ES>1jCX$K#pjJLj@cbg*(wC;;@2_WV{-}d1$OEeX{WcXu~_ppUNq&# z8t2@g#ECB^ot&mE)o`mY!M0yK~npp)Dp3B`Z@rH{`62k`B>hNC{;F>eb(~dL6W{pR)55Yyd!x(8)x&>K%~} b|CtR7xQeBvtd9cEa$@jw^>bP0l+XkKleNQ< literal 0 HcmV?d00001 diff --git a/src/main/java/hudson/plugins/git/GitSCM.java b/src/main/java/hudson/plugins/git/GitSCM.java index 085c0f5670..7c959f0e9a 100644 --- a/src/main/java/hudson/plugins/git/GitSCM.java +++ b/src/main/java/hudson/plugins/git/GitSCM.java @@ -51,6 +51,7 @@ import hudson.util.FormValidation; import hudson.util.ListBoxModel; import jenkins.model.Jenkins; +import jenkins.plugins.git.GitHooksConfiguration; import jenkins.plugins.git.GitSCMMatrixUtil; import jenkins.plugins.git.GitToolChooser; import net.sf.json.JSONObject; @@ -791,6 +792,7 @@ private PollingResult compareRemoteRevisionWithImpl(Job project, Launcher GitClient git = createClient(listener, environment, project, node, workingDirectory); if (git.hasGitRepo(false)) { + GitHooksConfiguration.configure(git); // Repo is there - do a fetch listener.getLogger().println("Fetching changes from the remote Git repositories"); @@ -1232,6 +1234,7 @@ private void retrieveChanges(Run build, GitClient git, TaskListener listener) th throw new AbortException("Error cloning remote repo '" + rc.getName() + "'"); } } + GitHooksConfiguration.configure(git); for (RemoteConfig remoteRepository : repos) { if (remoteRepository.equals(repos.get(0)) && removeSecondFetch){ diff --git a/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java b/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java index a9e51aa5d3..9178ff0438 100644 --- a/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java +++ b/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java @@ -374,6 +374,8 @@ private , R extends GitSCMSourceRequest> listener.getLogger().println("Creating git repository in " + cacheDir); client.init(); } + GitHooksConfiguration.configure(client, GitHooksConfiguration.get().isAllowedOnController()); + String remoteName = context.remoteName(); listener.getLogger().println("Setting " + remoteName + " to " + getRemote()); client.setRemoteUrl(remoteName, getRemote()); diff --git a/src/main/java/jenkins/plugins/git/GitHooksConfiguration.java b/src/main/java/jenkins/plugins/git/GitHooksConfiguration.java new file mode 100644 index 0000000000..f1ce3cc633 --- /dev/null +++ b/src/main/java/jenkins/plugins/git/GitHooksConfiguration.java @@ -0,0 +1,141 @@ +/* + * The MIT License + * + * Copyright (c) 2021 CloudBees, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ +package jenkins.plugins.git; + +import edu.umd.cs.findbugs.annotations.NonNull; +import hudson.Extension; +import hudson.Functions; +import hudson.model.PersistentDescriptor; +import hudson.remoting.Channel; +import jenkins.model.GlobalConfiguration; +import jenkins.model.GlobalConfigurationCategory; +import org.apache.commons.lang.StringUtils; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.lib.StoredConfig; +import org.jenkinsci.Symbol; +import org.jenkinsci.plugins.gitclient.GitClient; +import org.kohsuke.accmod.Restricted; +import org.kohsuke.accmod.restrictions.NoExternalUse; + +import java.io.IOException; +import java.util.logging.Logger; + + + +@Extension @Symbol("gitHooks") @Restricted(NoExternalUse.class) +public class GitHooksConfiguration extends GlobalConfiguration implements PersistentDescriptor { + + public static final String DISABLED_WIN = "NUL:"; + public static final String DISABLED_NIX = "/dev/null"; + static final Logger LOGGER = Logger.getLogger(GitHooksConfiguration.class.getName()); + + private boolean allowedOnController = false; + private boolean allowedOnAgents = false; + + @NonNull + public static GitHooksConfiguration get() { + final GitHooksConfiguration configuration = GlobalConfiguration.all().get(GitHooksConfiguration.class); + if (configuration == null) { + throw new IllegalStateException("[BUG] No configuration registered, make sure not running on an agent or that Jenkins has started properly."); + } + return configuration; + } + + public boolean isAllowedOnController() { + return allowedOnController; + } + + public void setAllowedOnController(final boolean allowedOnController) { + this.allowedOnController = allowedOnController; + save(); + } + + public boolean isAllowedOnAgents() { + return allowedOnAgents; + } + + public void setAllowedOnAgents(final boolean allowedOnAgents) { + this.allowedOnAgents = allowedOnAgents; + save(); + } + + @Override @NonNull + public GlobalConfigurationCategory getCategory() { + return GlobalConfigurationCategory.get(GlobalConfigurationCategory.Security.class); + } + + public static void configure(GitClient client) throws IOException, InterruptedException { + final GitHooksConfiguration configuration = GitHooksConfiguration.get(); + configure(client, configuration.isAllowedOnController(), configuration.isAllowedOnAgents()); + } + + public static void configure(GitClient client, final boolean allowedOnController, final boolean allowedOnAgents) throws IOException, InterruptedException { + if (Channel.current() == null) { + //Running on controller + try (Repository ignored = client.getRepository()){ + //That went well, so the code runs on the controller and the repo is local + configure(client, allowedOnController); + } catch (UnsupportedOperationException e) { + // Client represents a remote repository, so this code runs on the controller but the repo is on an agent + configure(client, allowedOnAgents); + } + } else { + //Running on agent + configure(client, allowedOnAgents); + } + } + + public static void configure(GitClient client, final boolean allowed) throws IOException, InterruptedException { + if (!allowed) { + client.withRepository((repo, channel) -> { + disable(repo); + return null; + }); + } else { + client.withRepository((repo, channel) -> { + unset(repo); + return null; + }); + } + } + + private static void unset(final Repository repo) throws IOException { + final StoredConfig repoConfig = repo.getConfig(); + final String val = repoConfig.getString("core", null, "hooksPath"); + if (!StringUtils.isEmpty(val) && !(DISABLED_NIX.equals(val) || DISABLED_WIN.equals(val))) { + LOGGER.warning(() -> String.format("core.hooksPath explicitly set to %s and will be left intact on %s.", val, repo.getDirectory())); + } else { + repoConfig.unset("core", null, "hooksPath"); + repoConfig.save(); + } + } + + private static void disable(final Repository repo) throws IOException { + final String VAL = Functions.isWindows() ? DISABLED_WIN : DISABLED_NIX; + final StoredConfig repoConfig = repo.getConfig(); + repoConfig.setString("core", null, "hooksPath", VAL); + repoConfig.save(); + } +} diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index 6e7847d271..e585139d0b 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -334,6 +334,7 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull listener.getLogger().println("Creating git repository in " + cacheDir); client.init(); } + GitHooksConfiguration.configure(client, GitHooksConfiguration.get().isAllowedOnController()); String remoteName = StringUtils.defaultIfBlank(config.getName(), Constants.DEFAULT_REMOTE_NAME); listener.getLogger().println("Setting " + remoteName + " to " + remote); client.setRemoteUrl(remoteName, remote); @@ -396,6 +397,7 @@ public SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead head, @Ch listener.getLogger().println("Creating git repository in " + cacheDir); client.init(); } + GitHooksConfiguration.configure(client, GitHooksConfiguration.get().isAllowedOnController()); String remoteName = builder.remoteName(); listener.getLogger().println("Setting " + remoteName + " to " + gitSCMSource.getRemote()); client.setRemoteUrl(remoteName, gitSCMSource.getRemote()); diff --git a/src/main/resources/jenkins/plugins/git/GitHooksConfiguration/config.jelly b/src/main/resources/jenkins/plugins/git/GitHooksConfiguration/config.jelly new file mode 100644 index 0000000000..dea843a38a --- /dev/null +++ b/src/main/resources/jenkins/plugins/git/GitHooksConfiguration/config.jelly @@ -0,0 +1,36 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/jenkins/plugins/git/GitHooksConfiguration/help-allowedOnAgents.html b/src/main/resources/jenkins/plugins/git/GitHooksConfiguration/help-allowedOnAgents.html new file mode 100644 index 0000000000..c2dd80b4ce --- /dev/null +++ b/src/main/resources/jenkins/plugins/git/GitHooksConfiguration/help-allowedOnAgents.html @@ -0,0 +1,23 @@ +
+

+ Git hooks allow scripts to be invoked when certain important git repository actions occur. + This configuration controls the execution of client-side hooks on Jenkins agents. + It is recommended that git hooks be disabled on Jenkins agents. +

+

+ Most git repositories do not use hooks in the repository and do not need repository hooks. + In those rare cases where repository hooks are needed, it is highly recommended that they are disabled on the Jenkins controller and on Jenkins agents. +

+

+ Client-side hooks are not copied when the repository is cloned. + However, client-side hooks might be installed in a repository by build steps or by misconfiguration. +

+

+ If hook scripts are allowed on agents, a client-side hook script installed in a repository on a Jenkins agent will execute when the matching git operation is performed. + For example, if hooks are allowed on agents and a git repository on an agent includes a post-checkout hook, the hook script will run on the agent after any checkout in that repository. + If hooks are allowed on agents and a git repository on an agent includes a pre-auto-gc hook, the hook script will run on the agent before any automatic git garbage collection task. +

+

+ See "Customizing Git - Git Hooks" for more details about git repository hooks. +

+
diff --git a/src/main/resources/jenkins/plugins/git/GitHooksConfiguration/help-allowedOnController.html b/src/main/resources/jenkins/plugins/git/GitHooksConfiguration/help-allowedOnController.html new file mode 100644 index 0000000000..42144f7ccb --- /dev/null +++ b/src/main/resources/jenkins/plugins/git/GitHooksConfiguration/help-allowedOnController.html @@ -0,0 +1,25 @@ +
+

+ Git hooks allow scripts to be invoked when certain important git repository actions occur. + This configuration controls the execution of client-side hooks on the Jenkins controller. + It is recommended that git hooks be disabled on the controller. +

+

+ The Jenkins controller uses git repositories to checkout Pipeline definitions, to detect changes in remote repositories, and to cache Pipeline shared libraries. + Jenkins jobs that run on the controller may use git repositories in many other ways. + It is strongly recommended that jobs are not run on the Jenkins controller. + Refer to the controller isolation documentation for more details. +

+

+ Client-side hooks are not copied when the repository is cloned. + However, client-side hooks might be installed in a repository by build steps or by misconfiguration. +

+

+ If hook scripts are allowed on the controller, a client-side hook script installed in a repository on the Jenkins controller will execute when the matching git operation is performed. + For example, if hooks are allowed on the controller and a git repository on the controller includes a post-checkout hook, the hook script will run on the controller after any checkout in that repository. + If hooks are allowed on the controller and a git repository on the controller includes a pre-auto-gc hook, the hook script will run on the controller before any automatic git garbage collection task. +

+

+ See "Customizing Git - Git Hooks" for more details about git repository hooks. +

+
diff --git a/src/test/java/hudson/plugins/git/GitHooksTest.java b/src/test/java/hudson/plugins/git/GitHooksTest.java new file mode 100644 index 0000000000..1aa6904bac --- /dev/null +++ b/src/test/java/hudson/plugins/git/GitHooksTest.java @@ -0,0 +1,258 @@ +package hudson.plugins.git; + +import hudson.FilePath; +import hudson.model.Label; +import hudson.plugins.filesystem_scm.FSSCM; +import hudson.slaves.DumbSlave; +import hudson.tools.ToolProperty; +import jenkins.plugins.git.CliGitCommand; +import jenkins.plugins.git.GitHooksConfiguration; +import jenkins.plugins.git.GitSCMFileSystem; +import jenkins.scm.api.SCMFile; +import jenkins.scm.api.SCMFileSystem; +import jenkins.scm.impl.mock.AbstractSampleRepoRule; +import org.apache.commons.io.FileUtils; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition; +import org.jenkinsci.plugins.workflow.flow.FlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.jvnet.hudson.test.Issue; +import org.jvnet.hudson.test.LoggerRule; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.Collections; +import java.util.Locale; +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.IsIterableContaining.hasItem; +import static org.hamcrest.core.IsNot.not; +import static org.hamcrest.core.StringStartsWith.startsWith; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +public class GitHooksTest extends AbstractGitTestCase { + + @Rule + public LoggerRule lr = new LoggerRule(); + + @BeforeClass + public static void setGitDefaults() throws Exception { + CliGitCommand gitCmd = new CliGitCommand(null); + gitCmd.setDefaults(); + } + + @Before + public void setGitTool() { + lr.record(GitHooksConfiguration.class.getName(), Level.ALL).capture(1024); + GitTool tool = new GitTool("my-git", "git", Collections.>emptyList()); + rule.jenkins.getDescriptorByType(GitTool.DescriptorImpl.class).setInstallations(tool); + } + + @After + public void tearDown() { + GitHooksConfiguration.get().setAllowedOnController(false); + GitHooksConfiguration.get().setAllowedOnAgents(false); + assertThat(lr.getMessages(), not(hasItem(startsWith("core.hooksPath explicitly set to ")))); + } + + @Test + public void testPipelineFromScm() throws Exception { + GitHooksConfiguration.get().setAllowedOnController(true); + GitHooksConfiguration.get().setAllowedOnAgents(true); + final DumbSlave agent = rule.createOnlineSlave(Label.get("somewhere")); + commit("test.txt", "Test", johnDoe, "First"); + String jenkinsfile = lines( + "node('somewhere') {", + " checkout scm", + " echo 'Hello Pipeline'", + "}" + ); + commit("Jenkinsfile", jenkinsfile, johnDoe, "Jenkinsfile"); + final WorkflowJob job = rule.createProject(WorkflowJob.class); + final GitSCM scm = new GitSCM( + this.createRemoteRepositories(), + Collections.singletonList(new BranchSpec("master")), + null, "my-git", Collections.emptyList() + ); + CpsScmFlowDefinition definition = new CpsScmFlowDefinition(scm, "Jenkinsfile"); + definition.setLightweight(false); + job.setDefinition(definition); + job.save(); + WorkflowRun run = rule.buildAndAssertSuccess(job); + rule.assertLogContains("Hello Pipeline", run); + + final FilePath workspace = agent.getWorkspaceFor(job); + assertNotNull(workspace); + TemporaryFolder tf = new TemporaryFolder(); + tf.create(); + final File postCheckoutOutput1 = new File(tf.newFolder(), "svn-git-fun-post-checkout-1"); + final File postCheckoutOutput2 = new File(tf.newFolder(), "svn-git-fun-post-checkout-2"); + + //Add hook on agent workspace + FilePath hook = workspace.child(".git/hooks/post-checkout"); + createHookScriptAt(postCheckoutOutput1, hook); + + final FilePath scriptWorkspace = rule.jenkins.getWorkspaceFor(job).withSuffix("@script"); //TODO change when pom is updated to latest security fixes + createHookScriptAt(postCheckoutOutput2, scriptWorkspace.child(".git/hooks/post-checkout")); + + commit("test.txt", "Second", johnDoe, "Second"); + commit("Jenkinsfile", "/*2*/\n" + jenkinsfile, johnDoe, "Jenkinsfile"); + + //Allowed + Thread.sleep(TimeUnit.SECONDS.toMillis(2)); + Instant before = Instant.now().minus(2, ChronoUnit.SECONDS); + run = rule.buildAndAssertSuccess(job); + assertTrue(postCheckoutOutput1.exists()); + assertTrue(postCheckoutOutput2.exists()); + rule.assertLogContains("Hello Pipeline", run); + Instant after = Instant.now().plus(2, ChronoUnit.SECONDS); + checkFileOutput(postCheckoutOutput1, before, after); + assertFalse(postCheckoutOutput1.exists()); + checkFileOutput(postCheckoutOutput2, before, after); + assertFalse(postCheckoutOutput2.exists()); + + commit("test.txt", "Third", johnDoe, "Third"); + commit("Jenkinsfile", "/*3*/\n" + jenkinsfile, johnDoe, "Jenkinsfile"); + //Denied + GitHooksConfiguration.get().setAllowedOnController(false); + GitHooksConfiguration.get().setAllowedOnAgents(false); + run = rule.buildAndAssertSuccess(job); + rule.assertLogContains("Hello Pipeline", run); + assertFalse(postCheckoutOutput1.exists()); + assertFalse(postCheckoutOutput2.exists()); + + commit("test.txt", "Four", johnDoe, "Four"); + commit("Jenkinsfile", "/*4*/\n" + jenkinsfile, johnDoe, "Jenkinsfile"); + //Allowed On Agent + GitHooksConfiguration.get().setAllowedOnController(false); + GitHooksConfiguration.get().setAllowedOnAgents(true); + Thread.sleep(TimeUnit.SECONDS.toMillis(2)); + before = Instant.now().minus(2, ChronoUnit.SECONDS); + run = rule.buildAndAssertSuccess(job); + assertFalse(postCheckoutOutput2.exists()); + assertTrue(postCheckoutOutput1.exists()); + rule.assertLogContains("Hello Pipeline", run); + after = Instant.now().plus(2, ChronoUnit.SECONDS); + checkFileOutput(postCheckoutOutput1, before, after); + assertFalse(postCheckoutOutput1.exists()); + + commit("test.txt", "Five", johnDoe, "Five"); + commit("Jenkinsfile", "/*5*/\n" + jenkinsfile, johnDoe, "Jenkinsfile"); + //Denied + GitHooksConfiguration.get().setAllowedOnController(false); + GitHooksConfiguration.get().setAllowedOnAgents(false); + run = rule.buildAndAssertSuccess(job); + rule.assertLogContains("Hello Pipeline", run); + assertFalse(postCheckoutOutput1.exists()); + assertFalse(postCheckoutOutput2.exists()); + } + + private void createHookScriptAt(final File postCheckoutOutput, final FilePath hook) throws IOException, InterruptedException { + final String nl = System.lineSeparator(); + StringBuilder scriptContent = new StringBuilder("#!/bin/bash").append(nl); + scriptContent.append("date +%s > \"") + .append(postCheckoutOutput.getAbsolutePath().replace("\\", "\\\\")) //Git bash does the bash escaping so need to do more escaping + .append('"').append(nl); + hook.write(scriptContent.toString(), Charset.defaultCharset().name()); + hook.chmod(0777); + } + + private void checkFileOutput(final File postCheckoutOutput, final Instant before, final Instant after) throws IOException { + assertTrue("Output file should exist", postCheckoutOutput.exists()); + final String s = FileUtils.readFileToString(postCheckoutOutput, Charset.defaultCharset()).trim(); + final Instant when = Instant.ofEpochSecond(Integer.parseInt(s)); + assertTrue("Sometime else", when.isAfter(before) && when.isBefore(after)); + assert postCheckoutOutput.delete(); + } + + @Test + public void testPipelineCheckoutController() throws Exception { + //assumeFalse("Not Windows", Functions.isWindows()); + + final WorkflowJob job = setupAndRunPipelineCheckout("master"); + WorkflowRun run; + commit("Commit3", janeDoe, "Commit number 3"); + GitHooksConfiguration.get().setAllowedOnController(true); + run = rule.buildAndAssertSuccess(job); + rule.assertLogContains("h4xor3d", run); + GitHooksConfiguration.get().setAllowedOnController(false); + GitHooksConfiguration.get().setAllowedOnAgents(true); + commit("Commit4", janeDoe, "Commit number 4"); + run = rule.buildAndAssertSuccess(job); + rule.assertLogNotContains("h4xor3d", run); + } + + @Test + public void testPipelineCheckoutAgent() throws Exception { + //assumeFalse("Not Windows", Functions.isWindows()); + + rule.createOnlineSlave(Label.get("belsebob")); + final WorkflowJob job = setupAndRunPipelineCheckout("belsebob"); + WorkflowRun run; + commit("Commit3", janeDoe, "Commit number 3"); + GitHooksConfiguration.get().setAllowedOnAgents(true); + run = rule.buildAndAssertSuccess(job); + rule.assertLogContains("h4xor3d", run); + GitHooksConfiguration.get().setAllowedOnAgents(false); + GitHooksConfiguration.get().setAllowedOnController(true); + commit("Commit4", janeDoe, "Commit number 4"); + run = rule.buildAndAssertSuccess(job); + rule.assertLogNotContains("h4xor3d", run); + } + + private WorkflowJob setupAndRunPipelineCheckout(String node) throws Exception { + final String commitFile1 = "commitFile1"; + commit(commitFile1, johnDoe, "Commit number 1"); + + final WorkflowJob job = rule.createProject(WorkflowJob.class); + final String uri = testRepo.gitDir.getAbsolutePath().replace("\\", "/"); + job.setDefinition(new CpsFlowDefinition(lines( + "node('" + node + "') {", + " checkout([$class: 'GitSCM', branches: [[name: '*/master']], userRemoteConfigs: [[url: '" + uri + "']]])", + " if (!fileExists('.git/hooks/post-checkout')) {", + " writeFile file: '.git/hooks/post-checkout', text: \"#!/bin/bash\\necho h4xor3d\"", + " if (isUnix()) {", + " sh 'chmod +x .git/hooks/post-checkout'", + " }", + " } else {", + " if (isUnix()) {", + " sh 'git checkout -B test origin/master'", + " } else {", + " bat 'git.exe checkout -B test origin/master'", + " }", + " }", + "}") + , true)); + WorkflowRun run = rule.buildAndAssertSuccess(job); + rule.assertLogNotContains("h4xor3d", run); + final String commitFile2 = "commitFile2"; + commit(commitFile2, janeDoe, "Commit number 2"); + run = rule.buildAndAssertSuccess(job); + rule.assertLogNotContains("h4xor3d", run); + return job; + } + + /** + * Approximates a multiline string in Java. + * + * @param lines the lines to concatenate with a newline separator + * @return the concatenated multiline string + */ + private static String lines(String... lines) { + return String.join("\n", lines); + } +} diff --git a/src/test/java/jenkins/plugins/git/GitHooksConfigurationTest.java b/src/test/java/jenkins/plugins/git/GitHooksConfigurationTest.java new file mode 100644 index 0000000000..c12e16f5d2 --- /dev/null +++ b/src/test/java/jenkins/plugins/git/GitHooksConfigurationTest.java @@ -0,0 +1,211 @@ +/* + * The MIT License + * + * Copyright 2021 CloudBees, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package jenkins.plugins.git; + +import hudson.EnvVars; +import hudson.model.TaskListener; +import java.io.File; +import java.io.IOException; +import java.util.Random; +import org.eclipse.jgit.lib.StoredConfig; +import org.jenkinsci.plugins.gitclient.Git; +import org.jenkinsci.plugins.gitclient.GitClient; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class GitHooksConfigurationTest { + + @Rule + public JenkinsRule j = new JenkinsRule(); + + private GitHooksConfiguration configuration; + private GitClient client; + + private final Random random = new Random(); + private static final String NULL_HOOKS_PATH = isWindows() ? "NUL:" : "/dev/null"; + + public GitHooksConfigurationTest() { + } + + @Before + public void setUp() throws IOException, InterruptedException { + configuration = GitHooksConfiguration.get(); + Git git = Git.with(TaskListener.NULL, new EnvVars()); + client = git.getClient(); + } + + @After + public void resetHooksPath() throws IOException, InterruptedException { + client.withRepository((repo, channel) -> { + final StoredConfig repoConfig = repo.getConfig(); + repoConfig.unset("core", null, "hooksPath"); + repoConfig.save(); + return null; + }); + } + + @Test + public void testGet() { + assertThat(GitHooksConfiguration.get(), is(configuration)); + } + + @Test + public void testIsAllowedOnController() { + assertFalse(configuration.isAllowedOnController()); + } + + @Test + public void testSetAllowedOnController() { + configuration.setAllowedOnController(true); + assertTrue(configuration.isAllowedOnController()); + } + + @Test + public void testSetAllowedOnControllerFalse() { + configuration.setAllowedOnController(false); + assertFalse(configuration.isAllowedOnController()); + } + + @Test + public void testIsAllowedOnAgents() { + assertFalse(configuration.isAllowedOnAgents()); + } + + @Test + public void testSetAllowedOnAgents() { + configuration.setAllowedOnAgents(true); + assertTrue(configuration.isAllowedOnAgents()); + } + + @Test + public void testSetAllowedOnAgentsFalse() { + configuration.setAllowedOnAgents(false); + assertFalse(configuration.isAllowedOnAgents()); + } + + @Test + public void testGetCategory() { + assertThat(GitHooksConfiguration.get().getCategory(), is(configuration.getCategory())); + } + + private void setCoreHooksPath(String hooksPath) throws IOException, InterruptedException { + /* Configure a core.hook with path `hooksPath` */ + client.withRepository((repo, channel) -> { + final StoredConfig repoConfig = repo.getConfig(); + repoConfig.setString("core", null, "hooksPath", hooksPath); + repoConfig.save(); + return null; + }); + } + + private String getCoreHooksPath() throws IOException, InterruptedException { + String hooksPath = client.withRepository((repo, channel) -> { + final StoredConfig repoConfig = repo.getConfig(); + return repoConfig.getString("core", null, "hooksPath"); + }); + return hooksPath; + } + + @Test + public void testConfigure_GitClient() throws Exception { + GitHooksConfiguration.configure(client); + + /* Check configured value from repository */ + String hooksPath = getCoreHooksPath(); + assertThat(hooksPath, is(NULL_HOOKS_PATH)); + } + + @Test + public void testConfigure_GitClient_boolean() throws Exception { + boolean allowed = true; + GitHooksConfiguration.configure(client, allowed); + + /* Check configured value from repository */ + String hooksPath = getCoreHooksPath(); + assertThat(hooksPath, is(nullValue())); + } + + @Test + public void testConfigure_GitClient_booleanFalse() throws Exception { + boolean allowed = false; + GitHooksConfiguration.configure(client, allowed); + + /* Check configured value from repository */ + String hooksPath = getCoreHooksPath(); + assertThat(hooksPath, is(NULL_HOOKS_PATH)); + } + + private final String ALTERNATE_HOOKS_PATH = "not-a-valid-hooks-path"; + + private void configure_3args(boolean allowedOnController) throws Exception { + /* Change the hooksPath in repository */ + setCoreHooksPath(ALTERNATE_HOOKS_PATH); + + /* Confirm the hooksPath was changed in repository */ + String hooksPathBefore = getCoreHooksPath(); + assertThat(hooksPathBefore, is(ALTERNATE_HOOKS_PATH)); + + /* Reconfigure repository. + * Agent arg is ignored on controller, thus pass a random boolean + */ + GitHooksConfiguration.configure(client, allowedOnController, random.nextBoolean()); + } + + @Test + public void testConfigure_3args() throws Exception { + boolean allowedOnController = true; + + /* Change the hooksPath in repository */ + configure_3args(allowedOnController); + + /* Check configured value from repository */ + String hooksPath = getCoreHooksPath(); + assertThat(hooksPath, is(ALTERNATE_HOOKS_PATH)); + } + + @Test + public void testConfigure_3argsFalse() throws Exception { + boolean allowedOnController = false; + + /* Change the hooksPath in repository */ + configure_3args(allowedOnController); + + /* Check configured value from repository */ + String hooksPath = getCoreHooksPath(); + assertThat(hooksPath, is(NULL_HOOKS_PATH)); + } + + private static boolean isWindows() { + return File.pathSeparatorChar == ';'; + } +} From 9b2694d9518ce54e61bbee0044d0f55feca22e64 Mon Sep 17 00:00:00 2001 From: Robert Sandell Date: Wed, 16 Mar 2022 11:58:31 +0100 Subject: [PATCH 02/11] Update README.adoc Co-authored-by: James Nord --- README.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.adoc b/README.adoc index dc74c8c86c..9848ba3a3c 100644 --- a/README.adoc +++ b/README.adoc @@ -294,7 +294,7 @@ Git Hooks:: Most git repositories do not use hooks in the repository and do not need repository hooks. In those rare cases where repository hooks are needed, it is highly recommended that they are **disabled** on the Jenkins controller and on Jenkins agents. + -Client-side hooks are **not** copied when the repository is cloned. +Client-side hooks are **not** copied when the repository is cloned by Jenkins using the inbuilt SCM methods. However, client-side hooks might be installed in a repository by build steps or by misconfiguration. + If hook scripts are allowed, a client-side hook script installed in a repository will execute when the matching git operation is performed. From ee3da2b63ad0c2851c535010749f7db6b6295011 Mon Sep 17 00:00:00 2001 From: rsandell Date: Wed, 16 Mar 2022 12:01:19 +0100 Subject: [PATCH 03/11] Optimize imports --- src/test/java/hudson/plugins/git/GitHooksTest.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/test/java/hudson/plugins/git/GitHooksTest.java b/src/test/java/hudson/plugins/git/GitHooksTest.java index 1aa6904bac..c40a7051ea 100644 --- a/src/test/java/hudson/plugins/git/GitHooksTest.java +++ b/src/test/java/hudson/plugins/git/GitHooksTest.java @@ -2,19 +2,13 @@ import hudson.FilePath; import hudson.model.Label; -import hudson.plugins.filesystem_scm.FSSCM; import hudson.slaves.DumbSlave; import hudson.tools.ToolProperty; import jenkins.plugins.git.CliGitCommand; import jenkins.plugins.git.GitHooksConfiguration; -import jenkins.plugins.git.GitSCMFileSystem; -import jenkins.scm.api.SCMFile; -import jenkins.scm.api.SCMFileSystem; -import jenkins.scm.impl.mock.AbstractSampleRepoRule; import org.apache.commons.io.FileUtils; import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition; -import org.jenkinsci.plugins.workflow.flow.FlowDefinition; import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; import org.junit.After; @@ -23,7 +17,6 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import org.jvnet.hudson.test.Issue; import org.jvnet.hudson.test.LoggerRule; import java.io.File; @@ -32,7 +25,6 @@ import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.Collections; -import java.util.Locale; import java.util.concurrent.TimeUnit; import java.util.logging.Level; From c1399bb49e8737bff0d08767022792dd2732af1a Mon Sep 17 00:00:00 2001 From: rsandell Date: Wed, 16 Mar 2022 13:15:58 +0100 Subject: [PATCH 04/11] Debug bash --- src/test/java/hudson/plugins/git/GitHooksTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/hudson/plugins/git/GitHooksTest.java b/src/test/java/hudson/plugins/git/GitHooksTest.java index c40a7051ea..751e96da50 100644 --- a/src/test/java/hudson/plugins/git/GitHooksTest.java +++ b/src/test/java/hudson/plugins/git/GitHooksTest.java @@ -155,7 +155,7 @@ public void testPipelineFromScm() throws Exception { private void createHookScriptAt(final File postCheckoutOutput, final FilePath hook) throws IOException, InterruptedException { final String nl = System.lineSeparator(); - StringBuilder scriptContent = new StringBuilder("#!/bin/bash").append(nl); + StringBuilder scriptContent = new StringBuilder("#!/bin/bash -v").append(nl); scriptContent.append("date +%s > \"") .append(postCheckoutOutput.getAbsolutePath().replace("\\", "\\\\")) //Git bash does the bash escaping so need to do more escaping .append('"').append(nl); From 8c5bf5cb7ab4b4fa31ec6583cc4d0f42c6c45299 Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Mon, 21 Mar 2022 15:26:54 -0600 Subject: [PATCH 05/11] Do not run tests in parallel James Nord recommends that parallel testing be the choice of the user and that it be run through an argument to the mvn command rather than configuring it in the pom file. Since there is a report that the tests fail when forked on Windows, let's follow the recommendation from James and require the user to choose to run parallel tests by command line argument. I don't see the test failure consistently on my Windows computers, but have seen it at least once on them. --- pom.xml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/pom.xml b/pom.xml index 2d87afa3f7..28aa7d7fba 100644 --- a/pom.xml +++ b/pom.xml @@ -343,13 +343,6 @@ - - maven-surefire-plugin - - 0.8C - true - - From aa1f962ca10af5f6598265b543098ea4ecfd0ec2 Mon Sep 17 00:00:00 2001 From: rsandell Date: Mon, 4 Apr 2022 15:09:12 +0200 Subject: [PATCH 06/11] Skip the flaky test on windows --- src/test/java/hudson/plugins/git/GitHooksTest.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/test/java/hudson/plugins/git/GitHooksTest.java b/src/test/java/hudson/plugins/git/GitHooksTest.java index 751e96da50..63c9388c2a 100644 --- a/src/test/java/hudson/plugins/git/GitHooksTest.java +++ b/src/test/java/hudson/plugins/git/GitHooksTest.java @@ -1,6 +1,7 @@ package hudson.plugins.git; import hudson.FilePath; +import hudson.Functions; import hudson.model.Label; import hudson.slaves.DumbSlave; import hudson.tools.ToolProperty; @@ -35,6 +36,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeFalse; public class GitHooksTest extends AbstractGitTestCase { @@ -63,6 +65,8 @@ public void tearDown() { @Test public void testPipelineFromScm() throws Exception { + //Tested and is working on Windows, but for unknown reason is not working on the Win agents on ci.jenkins.io + assumeFalse("Not Windows", Functions.isWindows()); GitHooksConfiguration.get().setAllowedOnController(true); GitHooksConfiguration.get().setAllowedOnAgents(true); final DumbSlave agent = rule.createOnlineSlave(Label.get("somewhere")); @@ -98,7 +102,8 @@ public void testPipelineFromScm() throws Exception { FilePath hook = workspace.child(".git/hooks/post-checkout"); createHookScriptAt(postCheckoutOutput1, hook); - final FilePath scriptWorkspace = rule.jenkins.getWorkspaceFor(job).withSuffix("@script"); //TODO change when pom is updated to latest security fixes + //TODO change when pom is updated to the related security fixes in "Pipeline: Groovy" > 2648.va9433432b33c + final FilePath scriptWorkspace = rule.jenkins.getWorkspaceFor(job).withSuffix("@script"); createHookScriptAt(postCheckoutOutput2, scriptWorkspace.child(".git/hooks/post-checkout")); commit("test.txt", "Second", johnDoe, "Second"); From 3aad6a2ae88567743e8c2bf935bb82a1b39f6e00 Mon Sep 17 00:00:00 2001 From: rsandell Date: Tue, 5 Apr 2022 15:26:23 +0200 Subject: [PATCH 07/11] Turn it all off for Jenkins CI Windows --- .../java/hudson/plugins/git/GitHooksTest.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/test/java/hudson/plugins/git/GitHooksTest.java b/src/test/java/hudson/plugins/git/GitHooksTest.java index 63c9388c2a..0449fa56e8 100644 --- a/src/test/java/hudson/plugins/git/GitHooksTest.java +++ b/src/test/java/hudson/plugins/git/GitHooksTest.java @@ -65,8 +65,7 @@ public void tearDown() { @Test public void testPipelineFromScm() throws Exception { - //Tested and is working on Windows, but for unknown reason is not working on the Win agents on ci.jenkins.io - assumeFalse("Not Windows", Functions.isWindows()); + assumeNotJenkinsCiWindows(); GitHooksConfiguration.get().setAllowedOnController(true); GitHooksConfiguration.get().setAllowedOnAgents(true); final DumbSlave agent = rule.createOnlineSlave(Label.get("somewhere")); @@ -178,7 +177,7 @@ private void checkFileOutput(final File postCheckoutOutput, final Instant before @Test public void testPipelineCheckoutController() throws Exception { - //assumeFalse("Not Windows", Functions.isWindows()); + assumeNotJenkinsCiWindows(); final WorkflowJob job = setupAndRunPipelineCheckout("master"); WorkflowRun run; @@ -195,7 +194,7 @@ public void testPipelineCheckoutController() throws Exception { @Test public void testPipelineCheckoutAgent() throws Exception { - //assumeFalse("Not Windows", Functions.isWindows()); + assumeNotJenkinsCiWindows(); rule.createOnlineSlave(Label.get("belsebob")); final WorkflowJob job = setupAndRunPipelineCheckout("belsebob"); @@ -252,4 +251,15 @@ private WorkflowJob setupAndRunPipelineCheckout(String node) throws Exception { private static String lines(String... lines) { return String.join("\n", lines); } + + /** + * Assume that it is not running on a ci.jenkins.io Windows agent. + * + * The tests are tested and confirmed working on Windows, + * but for unknown reason is not working on the Win agents on ci.jenkins.io. + */ + private void assumeNotJenkinsCiWindows() { + final String jenkinsUrl = System.getenv("JENKINS_URL"); + assumeFalse(Functions.isWindows() && jenkinsUrl.contains("ci.jenkins.io")); + } } From 8be7b004f976c9e241c120437fe5010924cec898 Mon Sep 17 00:00:00 2001 From: rsandell Date: Wed, 6 Apr 2022 15:38:07 +0200 Subject: [PATCH 08/11] Reintroduce forkCount But in a way that makes it easier to change from the commandline --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 8576718dc5..98f93f813d 100644 --- a/pom.xml +++ b/pom.xml @@ -71,6 +71,7 @@ Max true Medium + 0.8C From 235e5b61ec7a7d4893e0cd6c74f97fe89b1375f3 Mon Sep 17 00:00:00 2001 From: rsandell Date: Wed, 25 May 2022 18:08:29 +0200 Subject: [PATCH 09/11] Fix timeout when testing using newer core versions --- src/test/java/hudson/plugins/git/GitHooksTest.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/test/java/hudson/plugins/git/GitHooksTest.java b/src/test/java/hudson/plugins/git/GitHooksTest.java index 0449fa56e8..3b1c7e9024 100644 --- a/src/test/java/hudson/plugins/git/GitHooksTest.java +++ b/src/test/java/hudson/plugins/git/GitHooksTest.java @@ -15,9 +15,11 @@ import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.jvnet.hudson.test.BuildWatcher; import org.jvnet.hudson.test.LoggerRule; import java.io.File; @@ -42,6 +44,8 @@ public class GitHooksTest extends AbstractGitTestCase { @Rule public LoggerRule lr = new LoggerRule(); + @ClassRule + public static BuildWatcher watcher = new BuildWatcher(); @BeforeClass public static void setGitDefaults() throws Exception { @@ -50,10 +54,14 @@ public static void setGitDefaults() throws Exception { } @Before - public void setGitTool() { + public void setGitTool() throws IOException { lr.record(GitHooksConfiguration.class.getName(), Level.ALL).capture(1024); GitTool tool = new GitTool("my-git", "git", Collections.>emptyList()); rule.jenkins.getDescriptorByType(GitTool.DescriptorImpl.class).setInstallations(tool); + //Jenkins 2.308 changes the default label to "built-in" causing test failures when testing with newer core + // e.g. java 17 testing + rule.jenkins.setLabelString("master"); + rule.jenkins.setNumExecutors(3); //In case this changes in the future as well. } @After From 983263535719713cb4d11a96572895c804a53e89 Mon Sep 17 00:00:00 2001 From: rsandell Date: Mon, 30 May 2022 17:41:26 +0200 Subject: [PATCH 10/11] Reason for deleting file fails --- src/test/java/hudson/plugins/git/GitHooksTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/hudson/plugins/git/GitHooksTest.java b/src/test/java/hudson/plugins/git/GitHooksTest.java index 3b1c7e9024..cc35f9c297 100644 --- a/src/test/java/hudson/plugins/git/GitHooksTest.java +++ b/src/test/java/hudson/plugins/git/GitHooksTest.java @@ -25,6 +25,7 @@ import java.io.File; import java.io.IOException; import java.nio.charset.Charset; +import java.nio.file.Files; import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.Collections; @@ -180,7 +181,7 @@ private void checkFileOutput(final File postCheckoutOutput, final Instant before final String s = FileUtils.readFileToString(postCheckoutOutput, Charset.defaultCharset()).trim(); final Instant when = Instant.ofEpochSecond(Integer.parseInt(s)); assertTrue("Sometime else", when.isAfter(before) && when.isBefore(after)); - assert postCheckoutOutput.delete(); + Files.delete(postCheckoutOutput.toPath()); } @Test From 4025e8e8242f437c078eec23d15716f48831465f Mon Sep 17 00:00:00 2001 From: rsandell Date: Mon, 30 May 2022 22:00:47 +0200 Subject: [PATCH 11/11] Adapt test to latest pipeline groovy --- src/test/java/hudson/plugins/git/GitHooksTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/hudson/plugins/git/GitHooksTest.java b/src/test/java/hudson/plugins/git/GitHooksTest.java index cc35f9c297..909a1c3bda 100644 --- a/src/test/java/hudson/plugins/git/GitHooksTest.java +++ b/src/test/java/hudson/plugins/git/GitHooksTest.java @@ -110,8 +110,8 @@ public void testPipelineFromScm() throws Exception { FilePath hook = workspace.child(".git/hooks/post-checkout"); createHookScriptAt(postCheckoutOutput1, hook); - //TODO change when pom is updated to the related security fixes in "Pipeline: Groovy" > 2648.va9433432b33c - final FilePath scriptWorkspace = rule.jenkins.getWorkspaceFor(job).withSuffix("@script"); + FilePath scriptWorkspace = rule.jenkins.getWorkspaceFor(job).withSuffix("@script"); + scriptWorkspace = scriptWorkspace.listDirectories().stream().findFirst().get(); createHookScriptAt(postCheckoutOutput2, scriptWorkspace.child(".git/hooks/post-checkout")); commit("test.txt", "Second", johnDoe, "Second");