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.

GitblitReceivePack.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Copyright 2013 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.git;
  17. import static org.eclipse.jgit.transport.BasePackPushConnection.CAPABILITY_SIDE_BAND_64K;
  18. import groovy.lang.Binding;
  19. import groovy.util.GroovyScriptEngine;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.text.MessageFormat;
  23. import java.util.Collection;
  24. import java.util.LinkedHashSet;
  25. import java.util.List;
  26. import java.util.Set;
  27. import java.util.concurrent.TimeUnit;
  28. import org.eclipse.jgit.lib.BatchRefUpdate;
  29. import org.eclipse.jgit.lib.NullProgressMonitor;
  30. import org.eclipse.jgit.lib.PersonIdent;
  31. import org.eclipse.jgit.lib.ProgressMonitor;
  32. import org.eclipse.jgit.lib.Repository;
  33. import org.eclipse.jgit.revwalk.RevCommit;
  34. import org.eclipse.jgit.transport.PostReceiveHook;
  35. import org.eclipse.jgit.transport.PreReceiveHook;
  36. import org.eclipse.jgit.transport.ReceiveCommand;
  37. import org.eclipse.jgit.transport.ReceiveCommand.Result;
  38. import org.eclipse.jgit.transport.ReceivePack;
  39. import org.slf4j.Logger;
  40. import org.slf4j.LoggerFactory;
  41. import com.gitblit.Constants;
  42. import com.gitblit.Constants.AccessRestrictionType;
  43. import com.gitblit.IStoredSettings;
  44. import com.gitblit.Keys;
  45. import com.gitblit.client.Translation;
  46. import com.gitblit.extensions.ReceiveHook;
  47. import com.gitblit.manager.IGitblit;
  48. import com.gitblit.models.RepositoryModel;
  49. import com.gitblit.models.UserModel;
  50. import com.gitblit.tickets.BranchTicketService;
  51. import com.gitblit.utils.ArrayUtils;
  52. import com.gitblit.utils.ClientLogger;
  53. import com.gitblit.utils.CommitCache;
  54. import com.gitblit.utils.JGitUtils;
  55. import com.gitblit.utils.RefLogUtils;
  56. import com.gitblit.utils.StringUtils;
  57. /**
  58. * GitblitReceivePack processes receive commands. It also executes Groovy pre-
  59. * and post- receive hooks.
  60. *
  61. * The general execution flow is:
  62. * <ol>
  63. * <li>onPreReceive()</li>
  64. * <li>executeCommands()</li>
  65. * <li>onPostReceive()</li>
  66. * </ol>
  67. *
  68. * @author Android Open Source Project
  69. * @author James Moger
  70. *
  71. */
  72. public class GitblitReceivePack extends ReceivePack implements PreReceiveHook, PostReceiveHook {
  73. private static final Logger LOGGER = LoggerFactory.getLogger(GitblitReceivePack.class);
  74. protected final RepositoryModel repository;
  75. protected final UserModel user;
  76. protected final File groovyDir;
  77. protected String gitblitUrl;
  78. protected GroovyScriptEngine gse;
  79. protected final IStoredSettings settings;
  80. protected final IGitblit gitblit;
  81. public GitblitReceivePack(
  82. IGitblit gitblit,
  83. Repository db,
  84. RepositoryModel repository,
  85. UserModel user) {
  86. super(db);
  87. this.settings = gitblit.getSettings();
  88. this.gitblit = gitblit;
  89. this.repository = repository;
  90. this.user = user;
  91. this.groovyDir = gitblit.getHooksFolder();
  92. try {
  93. // set Grape root
  94. File grapeRoot = gitblit.getGrapesFolder();
  95. grapeRoot.mkdirs();
  96. System.setProperty("grape.root", grapeRoot.getAbsolutePath());
  97. this.gse = new GroovyScriptEngine(groovyDir.getAbsolutePath());
  98. } catch (IOException e) {
  99. }
  100. // set advanced ref permissions
  101. setAllowCreates(user.canCreateRef(repository));
  102. setAllowDeletes(user.canDeleteRef(repository));
  103. setAllowNonFastForwards(user.canRewindRef(repository));
  104. int maxObjectSz = settings.getInteger(Keys.git.maxObjectSizeLimit, -1);
  105. if (maxObjectSz >= 0) {
  106. setMaxObjectSizeLimit(maxObjectSz);
  107. }
  108. int maxPackSz = settings.getInteger(Keys.git.maxPackSizeLimit, -1);
  109. if (maxPackSz >= 0) {
  110. setMaxPackSizeLimit(maxPackSz);
  111. }
  112. setCheckReceivedObjects(settings.getBoolean(Keys.git.checkReceivedObjects, true));
  113. setCheckReferencedObjectsAreReachable(settings.getBoolean(Keys.git.checkReferencedObjectsAreReachable, true));
  114. // setup pre and post receive hook
  115. setPreReceiveHook(this);
  116. setPostReceiveHook(this);
  117. }
  118. /**
  119. * Returns true if the user is permitted to apply the receive commands to
  120. * the repository.
  121. *
  122. * @param commands
  123. * @return true if the user may push these commands
  124. */
  125. protected boolean canPush(Collection<ReceiveCommand> commands) {
  126. // TODO Consider supporting branch permissions here (issue-36)
  127. // Not sure if that should be Gerrit-style, refs/meta/config, or
  128. // gitolite-style, permissions in users.conf
  129. //
  130. // How could commands be empty?
  131. //
  132. // Because a subclass, like PatchsetReceivePack, filters receive
  133. // commands before this method is called. This makes it possible for
  134. // this method to test an empty list. In this case, we assume that the
  135. // subclass receive pack properly enforces push restrictions. for the
  136. // ref.
  137. //
  138. // The empty test is not explicitly required, it's written here to
  139. // clarify special-case behavior.
  140. return commands.isEmpty() ? true : user.canPush(repository);
  141. }
  142. /**
  143. * Instrumentation point where the incoming push event has been parsed,
  144. * validated, objects created BUT refs have not been updated. You might
  145. * use this to enforce a branch-write permissions model.
  146. */
  147. @Override
  148. public void onPreReceive(ReceivePack rp, Collection<ReceiveCommand> commands) {
  149. if (commands.size() == 0) {
  150. // no receive commands to process
  151. // this can happen if receive pack subclasses intercept and filter
  152. // the commands
  153. LOGGER.debug("skipping pre-receive processing, no refs created, updated, or removed");
  154. return;
  155. }
  156. if (repository.isMirror) {
  157. // repository is a mirror
  158. for (ReceiveCommand cmd : commands) {
  159. sendRejection(cmd, "Gitblit does not allow pushes to \"{0}\" because it is a mirror!", repository.name);
  160. }
  161. return;
  162. }
  163. if (repository.isFrozen) {
  164. // repository is frozen/readonly
  165. for (ReceiveCommand cmd : commands) {
  166. sendRejection(cmd, "Gitblit does not allow pushes to \"{0}\" because it is frozen!", repository.name);
  167. }
  168. return;
  169. }
  170. if (!repository.isBare) {
  171. // repository has a working copy
  172. for (ReceiveCommand cmd : commands) {
  173. sendRejection(cmd, "Gitblit does not allow pushes to \"{0}\" because it has a working copy!", repository.name);
  174. }
  175. return;
  176. }
  177. if (!canPush(commands)) {
  178. // user does not have push permissions
  179. for (ReceiveCommand cmd : commands) {
  180. sendRejection(cmd, "User \"{0}\" does not have push permissions for \"{1}\"!", user.username, repository.name);
  181. }
  182. return;
  183. }
  184. if (repository.accessRestriction.atLeast(AccessRestrictionType.PUSH) && repository.verifyCommitter) {
  185. // enforce committer verification
  186. if (StringUtils.isEmpty(user.emailAddress)) {
  187. // reject the push because the pushing account does not have an email address
  188. for (ReceiveCommand cmd : commands) {
  189. sendRejection(cmd, "Sorry, the account \"{0}\" does not have an email address set for committer verification!", user.username);
  190. }
  191. return;
  192. }
  193. // Optionally enforce that the committer of first parent chain
  194. // match the account being used to push the commits.
  195. //
  196. // This requires all merge commits are executed with the "--no-ff"
  197. // option to force a merge commit even if fast-forward is possible.
  198. // This ensures that the chain first parents has the commit
  199. // identity of the merging user.
  200. boolean allRejected = false;
  201. for (ReceiveCommand cmd : commands) {
  202. String firstParent = null;
  203. try {
  204. List<RevCommit> commits = JGitUtils.getRevLog(rp.getRepository(), cmd.getOldId().name(), cmd.getNewId().name());
  205. for (RevCommit commit : commits) {
  206. if (firstParent != null) {
  207. if (!commit.getName().equals(firstParent)) {
  208. // ignore: commit is right-descendant of a merge
  209. continue;
  210. }
  211. }
  212. // update expected next commit id
  213. if (commit.getParentCount() == 0) {
  214. firstParent = null;
  215. } else {
  216. firstParent = commit.getParents()[0].getId().getName();
  217. }
  218. PersonIdent committer = commit.getCommitterIdent();
  219. if (!user.is(committer.getName(), committer.getEmailAddress())) {
  220. // verification failed
  221. String reason = MessageFormat.format("{0} by {1} <{2}> was not committed by {3} ({4}) <{5}>",
  222. commit.getId().name(), committer.getName(), StringUtils.isEmpty(committer.getEmailAddress()) ? "?":committer.getEmailAddress(), user.getDisplayName(), user.username, user.emailAddress);
  223. LOGGER.warn(reason);
  224. cmd.setResult(Result.REJECTED_OTHER_REASON, reason);
  225. allRejected &= true;
  226. break;
  227. } else {
  228. allRejected = false;
  229. }
  230. }
  231. } catch (Exception e) {
  232. LOGGER.error("Failed to verify commits were made by pushing user", e);
  233. }
  234. }
  235. if (allRejected) {
  236. // all ref updates rejected, abort
  237. return;
  238. }
  239. }
  240. for (ReceiveCommand cmd : commands) {
  241. String ref = cmd.getRefName();
  242. if (ref.startsWith(Constants.R_HEADS)) {
  243. switch (cmd.getType()) {
  244. case UPDATE_NONFASTFORWARD:
  245. case DELETE:
  246. // reset branch commit cache on REWIND and DELETE
  247. CommitCache.instance().clear(repository.name, ref);
  248. break;
  249. default:
  250. break;
  251. }
  252. } else if (ref.equals(BranchTicketService.BRANCH)) {
  253. // ensure pushing user is an administrator OR an owner
  254. // i.e. prevent ticket tampering
  255. boolean permitted = user.canAdmin() || repository.isOwner(user.username);
  256. if (!permitted) {
  257. sendRejection(cmd, "{0} is not permitted to push to {1}", user.username, ref);
  258. }
  259. } else if (ref.startsWith(Constants.R_FOR)) {
  260. // prevent accidental push to refs/for
  261. sendRejection(cmd, "{0} is not configured to receive patchsets", repository.name);
  262. }
  263. }
  264. // call pre-receive plugins
  265. for (ReceiveHook hook : gitblit.getExtensions(ReceiveHook.class)) {
  266. try {
  267. hook.onPreReceive(this, commands);
  268. } catch (Exception e) {
  269. LOGGER.error("Failed to execute extension", e);
  270. }
  271. }
  272. Set<String> scripts = new LinkedHashSet<String>();
  273. scripts.addAll(gitblit.getPreReceiveScriptsInherited(repository));
  274. if (!ArrayUtils.isEmpty(repository.preReceiveScripts)) {
  275. scripts.addAll(repository.preReceiveScripts);
  276. }
  277. runGroovy(commands, scripts);
  278. for (ReceiveCommand cmd : commands) {
  279. if (!Result.NOT_ATTEMPTED.equals(cmd.getResult())) {
  280. LOGGER.warn(MessageFormat.format("{0} {1} because \"{2}\"", cmd.getNewId()
  281. .getName(), cmd.getResult(), cmd.getMessage()));
  282. }
  283. }
  284. }
  285. /**
  286. * Instrumentation point where the incoming push has been applied to the
  287. * repository. This is the point where we would trigger a Jenkins build
  288. * or send an email.
  289. */
  290. @Override
  291. public void onPostReceive(ReceivePack rp, Collection<ReceiveCommand> commands) {
  292. if (commands.size() == 0) {
  293. LOGGER.debug("skipping post-receive processing, no refs created, updated, or removed");
  294. return;
  295. }
  296. // log ref changes
  297. for (ReceiveCommand cmd : commands) {
  298. if (Result.OK.equals(cmd.getResult())) {
  299. // add some logging for important ref changes
  300. switch (cmd.getType()) {
  301. case DELETE:
  302. LOGGER.info(MessageFormat.format("{0} DELETED {1} in {2} ({3})", user.username, cmd.getRefName(), repository.name, cmd.getOldId().name()));
  303. break;
  304. case CREATE:
  305. LOGGER.info(MessageFormat.format("{0} CREATED {1} in {2}", user.username, cmd.getRefName(), repository.name));
  306. break;
  307. case UPDATE:
  308. LOGGER.info(MessageFormat.format("{0} UPDATED {1} in {2} (from {3} to {4})", user.username, cmd.getRefName(), repository.name, cmd.getOldId().name(), cmd.getNewId().name()));
  309. break;
  310. case UPDATE_NONFASTFORWARD:
  311. LOGGER.info(MessageFormat.format("{0} UPDATED NON-FAST-FORWARD {1} in {2} (from {3} to {4})", user.username, cmd.getRefName(), repository.name, cmd.getOldId().name(), cmd.getNewId().name()));
  312. break;
  313. default:
  314. break;
  315. }
  316. }
  317. }
  318. if (repository.useIncrementalPushTags) {
  319. // tag each pushed branch tip
  320. String emailAddress = user.emailAddress == null ? rp.getRefLogIdent().getEmailAddress() : user.emailAddress;
  321. PersonIdent userIdent = new PersonIdent(user.getDisplayName(), emailAddress);
  322. for (ReceiveCommand cmd : commands) {
  323. if (!cmd.getRefName().startsWith(Constants.R_HEADS)) {
  324. // only tag branch ref changes
  325. continue;
  326. }
  327. if (!ReceiveCommand.Type.DELETE.equals(cmd.getType())
  328. && ReceiveCommand.Result.OK.equals(cmd.getResult())) {
  329. String objectId = cmd.getNewId().getName();
  330. String branch = cmd.getRefName().substring(Constants.R_HEADS.length());
  331. // get translation based on the server's locale setting
  332. String template = Translation.get("gb.incrementalPushTagMessage");
  333. String msg = MessageFormat.format(template, branch);
  334. String prefix;
  335. if (StringUtils.isEmpty(repository.incrementalPushTagPrefix)) {
  336. prefix = settings.getString(Keys.git.defaultIncrementalPushTagPrefix, "r");
  337. } else {
  338. prefix = repository.incrementalPushTagPrefix;
  339. }
  340. JGitUtils.createIncrementalRevisionTag(
  341. rp.getRepository(),
  342. objectId,
  343. userIdent,
  344. prefix,
  345. "0",
  346. msg);
  347. }
  348. }
  349. }
  350. // update push log
  351. try {
  352. RefLogUtils.updateRefLog(user, rp.getRepository(), commands);
  353. LOGGER.debug(MessageFormat.format("{0} push log updated", repository.name));
  354. } catch (Exception e) {
  355. LOGGER.error(MessageFormat.format("Failed to update {0} pushlog", repository.name), e);
  356. }
  357. // check for updates pushed to the BranchTicketService branch
  358. // if the BranchTicketService is active it will reindex, as appropriate
  359. for (ReceiveCommand cmd : commands) {
  360. if (Result.OK.equals(cmd.getResult())
  361. && BranchTicketService.BRANCH.equals(cmd.getRefName())) {
  362. rp.getRepository().fireEvent(new ReceiveCommandEvent(repository, cmd));
  363. }
  364. }
  365. // call post-receive plugins
  366. for (ReceiveHook hook : gitblit.getExtensions(ReceiveHook.class)) {
  367. try {
  368. hook.onPostReceive(this, commands);
  369. } catch (Exception e) {
  370. LOGGER.error("Failed to execute extension", e);
  371. }
  372. }
  373. // run Groovy hook scripts
  374. Set<String> scripts = new LinkedHashSet<String>();
  375. scripts.addAll(gitblit.getPostReceiveScriptsInherited(repository));
  376. if (!ArrayUtils.isEmpty(repository.postReceiveScripts)) {
  377. scripts.addAll(repository.postReceiveScripts);
  378. }
  379. runGroovy(commands, scripts);
  380. }
  381. /** Execute commands to update references. */
  382. @Override
  383. protected void executeCommands() {
  384. List<ReceiveCommand> toApply = filterCommands(Result.NOT_ATTEMPTED);
  385. if (toApply.isEmpty()) {
  386. return;
  387. }
  388. ProgressMonitor updating = NullProgressMonitor.INSTANCE;
  389. boolean sideBand = isCapabilityEnabled(CAPABILITY_SIDE_BAND_64K);
  390. if (sideBand) {
  391. SideBandProgressMonitor pm = new SideBandProgressMonitor(msgOut);
  392. pm.setDelayStart(250, TimeUnit.MILLISECONDS);
  393. updating = pm;
  394. }
  395. BatchRefUpdate batch = getRepository().getRefDatabase().newBatchUpdate();
  396. batch.setAllowNonFastForwards(isAllowNonFastForwards());
  397. batch.setRefLogIdent(getRefLogIdent());
  398. batch.setRefLogMessage("push", true);
  399. for (ReceiveCommand cmd : toApply) {
  400. if (Result.NOT_ATTEMPTED != cmd.getResult()) {
  401. // Already rejected by the core receive process.
  402. continue;
  403. }
  404. batch.addCommand(cmd);
  405. }
  406. if (!batch.getCommands().isEmpty()) {
  407. try {
  408. batch.execute(getRevWalk(), updating);
  409. } catch (IOException err) {
  410. for (ReceiveCommand cmd : toApply) {
  411. if (cmd.getResult() == Result.NOT_ATTEMPTED) {
  412. sendRejection(cmd, "lock error: {0}", err.getMessage());
  413. }
  414. }
  415. }
  416. }
  417. }
  418. protected void setGitblitUrl(String url) {
  419. this.gitblitUrl = url;
  420. }
  421. public void sendRejection(final ReceiveCommand cmd, final String why, Object... objects) {
  422. String text;
  423. if (ArrayUtils.isEmpty(objects)) {
  424. text = why;
  425. } else {
  426. text = MessageFormat.format(why, objects);
  427. }
  428. cmd.setResult(Result.REJECTED_OTHER_REASON, text);
  429. LOGGER.error(text + " (" + user.username + ")");
  430. }
  431. public void sendHeader(String msg, Object... objects) {
  432. sendInfo("--> ", msg, objects);
  433. }
  434. public void sendInfo(String msg, Object... objects) {
  435. sendInfo(" ", msg, objects);
  436. }
  437. private void sendInfo(String prefix, String msg, Object... objects) {
  438. String text;
  439. if (ArrayUtils.isEmpty(objects)) {
  440. text = msg;
  441. super.sendMessage(prefix + msg);
  442. } else {
  443. text = MessageFormat.format(msg, objects);
  444. super.sendMessage(prefix + text);
  445. }
  446. if (!StringUtils.isEmpty(msg)) {
  447. LOGGER.info(text + " (" + user.username + ")");
  448. }
  449. }
  450. public void sendError(String msg, Object... objects) {
  451. String text;
  452. if (ArrayUtils.isEmpty(objects)) {
  453. text = msg;
  454. super.sendError(msg);
  455. } else {
  456. text = MessageFormat.format(msg, objects);
  457. super.sendError(text);
  458. }
  459. if (!StringUtils.isEmpty(msg)) {
  460. LOGGER.error(text + " (" + user.username + ")");
  461. }
  462. }
  463. /**
  464. * Runs the specified Groovy hook scripts.
  465. *
  466. * @param repository
  467. * @param user
  468. * @param commands
  469. * @param scripts
  470. */
  471. private void runGroovy(Collection<ReceiveCommand> commands, Set<String> scripts) {
  472. if (scripts == null || scripts.size() == 0) {
  473. // no Groovy scripts to execute
  474. return;
  475. }
  476. Binding binding = new Binding();
  477. binding.setVariable("gitblit", gitblit);
  478. binding.setVariable("repository", repository);
  479. binding.setVariable("receivePack", this);
  480. binding.setVariable("user", user);
  481. binding.setVariable("commands", commands);
  482. binding.setVariable("url", gitblitUrl);
  483. binding.setVariable("logger", LOGGER);
  484. binding.setVariable("clientLogger", new ClientLogger(this));
  485. for (String script : scripts) {
  486. if (StringUtils.isEmpty(script)) {
  487. continue;
  488. }
  489. // allow script to be specified without .groovy extension
  490. // this is easier to read in the settings
  491. File file = new File(groovyDir, script);
  492. if (!file.exists() && !script.toLowerCase().endsWith(".groovy")) {
  493. file = new File(groovyDir, script + ".groovy");
  494. if (file.exists()) {
  495. script = file.getName();
  496. }
  497. }
  498. try {
  499. Object result = gse.run(script, binding);
  500. if (result instanceof Boolean) {
  501. if (!((Boolean) result)) {
  502. LOGGER.error(MessageFormat.format(
  503. "Groovy script {0} has failed! Hook scripts aborted.", script));
  504. break;
  505. }
  506. }
  507. } catch (Exception e) {
  508. LOGGER.error(
  509. MessageFormat.format("Failed to execute Groovy script {0}", script), e);
  510. }
  511. }
  512. }
  513. public IGitblit getGitblit() {
  514. return gitblit;
  515. }
  516. public RepositoryModel getRepositoryModel() {
  517. return repository;
  518. }
  519. public UserModel getUserModel() {
  520. return user;
  521. }
  522. }