You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

protect-refs.groovy 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright 2012 Philip L. McMahon.
  3. *
  4. * Derived from blockpush.groovy, copyright 2011 gitblit.com.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. import com.gitblit.GitBlit
  19. import com.gitblit.models.RepositoryModel
  20. import com.gitblit.models.UserModel
  21. import org.eclipse.jgit.transport.ReceiveCommand
  22. import org.eclipse.jgit.transport.ReceiveCommand.Result
  23. import org.slf4j.Logger
  24. /**
  25. * Sample Gitblit Pre-Receive Hook: protect-refs
  26. *
  27. * This script provides basic authorization of receive command types for a list
  28. * of known ref patterns. Command types and unmatched ref patterns will be
  29. * ignored, meaning this script has an "allow by default" policy.
  30. *
  31. * This script works best when a repository requires authentication on push, but
  32. * can be used to enforce fast-forward commits or prohibit ref deletion by
  33. * setting the *authorizedTeams* variable to an empty list and adding a ".+"
  34. * entry to the *protectedRefs* list.
  35. *
  36. * The Pre-Receive hook is executed after an incoming push has been parsed,
  37. * validated, and objects have been written but BEFORE the refs are updated.
  38. * This is the appropriate point to block a push for some reason.
  39. *
  40. * This script is only executed when pushing to *Gitblit*, not to other Git
  41. * tooling you may be using.
  42. *
  43. * If this script is specified in *groovy.preReceiveScripts* of gitblit.properties
  44. * or web.xml then it will be executed by any repository when it receives a
  45. * push. If you choose to share your script then you may have to consider
  46. * tailoring control-flow based on repository access restrictions.
  47. *
  48. * Scripts may also be specified per-repository in the repository settings page.
  49. * Shared scripts will be excluded from this list of available scripts.
  50. *
  51. * This script is dynamically reloaded and it is executed within it's own
  52. * exception handler so it will not crash another script nor crash Gitblit.
  53. *
  54. * This script may reject one or more commands, but will never return false.
  55. * Subsequent scripts, if any, will always be invoked.
  56. *
  57. * Bound Variables:
  58. * gitblit Gitblit Server com.gitblit.GitBlit
  59. * repository Gitblit Repository com.gitblit.models.RepositoryModel
  60. * user Gitblit User com.gitblit.models.UserModel
  61. * commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand>
  62. * url Base url for Gitblit String
  63. * logger Logs messages to Gitblit org.slf4j.Logger
  64. * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger
  65. *
  66. * Accessing Gitblit Custom Fields:
  67. * def myCustomField = repository.customFields.myCustomField
  68. *
  69. */
  70. // map of protected command types to returned results type
  71. // commands not included will skip authz check
  72. def protectedCmds = [
  73. UPDATE_NONFASTFORWARD: Result.REJECTED_NONFASTFORWARD,
  74. DELETE: Result.REJECTED_NODELETE
  75. ]
  76. // list of regex patterns for protected refs
  77. def protectedRefs = [
  78. "refs/heads/master",
  79. "refs/tags/.+"
  80. ]
  81. // teams which are authorized to perform protected commands on protected refs
  82. def authorizedTeams = [ "admins" ]
  83. for (ReceiveCommand command : commands) {
  84. def updateType = command.type
  85. def updatedRef = command.refName
  86. // find first regex which matches updated ref, if any
  87. def refPattern = protectedRefs.find { updatedRef.matches ~it }
  88. // find rejection result for update type, if any
  89. def result = protectedCmds[updateType.name()]
  90. // command requires authz if ref is protected and has a mapped rejection result
  91. if (refPattern && result) {
  92. // verify user is a member of any authorized team
  93. def team = authorizedTeams.find { user.isTeamMember it }
  94. if (team) {
  95. // don't adjust command result
  96. logger.info "${user.username} authorized for ${updateType} of protected ref ${repository.name}:${updatedRef} (${command.oldId.name} -> ${command.newId.name})"
  97. } else {
  98. // mark command result as rejected
  99. command.setResult(result, "${user.username} cannot ${updateType} protected ref ${repository.name}:${updatedRef} matching pattern ${refPattern}")
  100. }
  101. }
  102. }