-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Disable local git hooks by default. #1237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8cdb1e0
Disable local git hooks by default.
rsandell 9b2694d
Update README.adoc
rsandell ee3da2b
Optimize imports
rsandell c1399bb
Debug bash
rsandell a6aa7d4
Merge branch 'master' into disablehooks
MarkEWaite 8c5bf5c
Do not run tests in parallel
MarkEWaite a68f7b5
Merge branch 'master' of github.com:jenkinsci/git-plugin into disable…
rsandell aa1f962
Skip the flaky test on windows
rsandell 83f1895
Merge branch 'master' into disablehooks
MarkEWaite 3aad6a2
Turn it all off for Jenkins CI Windows
rsandell 8be7b00
Reintroduce forkCount
rsandell ecd3d1f
Merge branch 'master' of github.com:jenkinsci/git-plugin into disable…
rsandell 235e5b6
Fix timeout when testing using newer core versions
rsandell 9832635
Reason for deleting file fails
rsandell 53e7191
Merge branch 'master' of github.com:jenkinsci/git-plugin into disable…
rsandell 4025e8e
Adapt test to latest pipeline groovy
rsandell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
src/main/java/jenkins/plugins/git/GitHooksConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/resources/jenkins/plugins/git/GitHooksConfiguration/config.jelly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?jelly escape-by-default='true'?> | ||
| <!-- | ||
| ~ 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. | ||
| --> | ||
| <j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form"> | ||
| <f:section title="${%Git Hooks}"> | ||
| <f:entry field="allowedOnController" | ||
| description="${%Allow git hooks to run on the Jenkins Controller}"> | ||
| <f:checkbox title="${%Allow on Controller}" /> | ||
| </f:entry> | ||
| <f:entry field="allowedOnAgents" | ||
| description="${%Allow git hooks to run on Jenkins Agents}"> | ||
| <f:checkbox title="${%Allow on Agents}" /> | ||
| </f:entry> | ||
| </f:section> | ||
| </j:jelly> |
23 changes: 23 additions & 0 deletions
23
src/main/resources/jenkins/plugins/git/GitHooksConfiguration/help-allowedOnAgents.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <div> | ||
| <p> | ||
| <a href="https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks">Git hooks</a> allow scripts to be invoked when certain important git repository actions occur. | ||
| This configuration controls the execution of client-side hooks on <i>Jenkins agents</i>. | ||
| It is recommended that git hooks be <strong>disabled</strong> on Jenkins agents. | ||
| </p> | ||
| <p> | ||
| 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 <strong>disabled</strong> on the Jenkins controller and on Jenkins agents. | ||
| </p> | ||
| <p> | ||
| Client-side hooks are <strong>not</strong> copied when the repository is cloned. | ||
| However, client-side hooks might be installed in a repository by build steps or by misconfiguration. | ||
| </p> | ||
| <p> | ||
| 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 <code>post-checkout</code> 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 <code>pre-auto-gc</code> hook, the hook script will run on the agent before any automatic git garbage collection task. | ||
| </p> | ||
| <p> | ||
| See <a href="https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks">"Customizing Git - Git Hooks"</a> for more details about git repository hooks. | ||
| </p> | ||
| </div> |
25 changes: 25 additions & 0 deletions
25
src/main/resources/jenkins/plugins/git/GitHooksConfiguration/help-allowedOnController.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <div> | ||
| <p> | ||
| <a href="https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks">Git hooks</a> allow scripts to be invoked when certain important git repository actions occur. | ||
| This configuration controls the execution of client-side hooks on the <i>Jenkins controller</i>. | ||
| It is recommended that git hooks be <strong>disabled</strong> on the controller. | ||
| </p> | ||
| <p> | ||
| 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 <a href="https://www.jenkins.io/doc/book/security/controller-isolation/#not-building-on-the-built-in-node">controller isolation documentation</a> for more details. | ||
| </p> | ||
| <p> | ||
| Client-side hooks are <strong>not</strong> copied when the repository is cloned. | ||
| However, client-side hooks might be installed in a repository by build steps or by misconfiguration. | ||
| </p> | ||
| <p> | ||
| 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 <code>post-checkout</code> 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 <code>pre-auto-gc</code> hook, the hook script will run on the controller before any automatic git garbage collection task. | ||
| </p> | ||
| <p> | ||
| See <a href="https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks">"Customizing Git - Git Hooks"</a> for more details about git repository hooks. | ||
| </p> | ||
| </div> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
depending on:
git-clientbecause it won't always when using thejgitprovider on windows, hooks are run on a posix file system, or if you are on a windows filesystem and cygwin or some othersh.exeis detected.