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.

FederationServlet.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright 2011 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;
  17. import java.io.File;
  18. import java.text.MessageFormat;
  19. import java.util.ArrayList;
  20. import java.util.Date;
  21. import java.util.HashMap;
  22. import java.util.HashSet;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Set;
  26. import javax.servlet.http.HttpServletResponse;
  27. import com.gitblit.Constants.FederationRequest;
  28. import com.gitblit.manager.IFederationManager;
  29. import com.gitblit.manager.IRepositoryManager;
  30. import com.gitblit.manager.IRuntimeManager;
  31. import com.gitblit.manager.IUserManager;
  32. import com.gitblit.models.FederationModel;
  33. import com.gitblit.models.FederationProposal;
  34. import com.gitblit.models.TeamModel;
  35. import com.gitblit.models.UserModel;
  36. import com.gitblit.utils.FederationUtils;
  37. import com.gitblit.utils.FileUtils;
  38. import com.gitblit.utils.HttpUtils;
  39. import com.gitblit.utils.StringUtils;
  40. import com.gitblit.utils.TimeUtils;
  41. /**
  42. * Handles federation requests.
  43. *
  44. * @author James Moger
  45. *
  46. */
  47. public class FederationServlet extends JsonServlet {
  48. private static final long serialVersionUID = 1L;
  49. public FederationServlet() {
  50. super();
  51. }
  52. /**
  53. * Processes a federation request.
  54. *
  55. * @param request
  56. * @param response
  57. * @throws javax.servlet.ServletException
  58. * @throws java.io.IOException
  59. */
  60. @Override
  61. protected void processRequest(javax.servlet.http.HttpServletRequest request,
  62. javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException,
  63. java.io.IOException {
  64. IStoredSettings settings = GitBlit.getManager(IRuntimeManager.class).getSettings();
  65. IUserManager userManager = GitBlit.getManager(IUserManager.class);
  66. IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class);
  67. IFederationManager federationManager = GitBlit.getManager(IFederationManager.class);
  68. FederationRequest reqType = FederationRequest.fromName(request.getParameter("req"));
  69. logger.info(MessageFormat.format("Federation {0} request from {1}", reqType,
  70. request.getRemoteAddr()));
  71. if (FederationRequest.POKE.equals(reqType)) {
  72. // Gitblit always responds to POKE requests to verify a connection
  73. logger.info("Received federation POKE from " + request.getRemoteAddr());
  74. return;
  75. }
  76. if (!settings.getBoolean(Keys.git.enableGitServlet, true)) {
  77. logger.warn(Keys.git.enableGitServlet + " must be set TRUE for federation requests.");
  78. response.sendError(HttpServletResponse.SC_FORBIDDEN);
  79. return;
  80. }
  81. String uuid = settings.getString(Keys.federation.passphrase, "");
  82. if (StringUtils.isEmpty(uuid)) {
  83. logger.warn(Keys.federation.passphrase
  84. + " is not properly set! Federation request denied.");
  85. response.sendError(HttpServletResponse.SC_FORBIDDEN);
  86. return;
  87. }
  88. if (FederationRequest.PROPOSAL.equals(reqType)) {
  89. // Receive a gitblit federation proposal
  90. FederationProposal proposal = deserialize(request, response, FederationProposal.class);
  91. if (proposal == null) {
  92. return;
  93. }
  94. // reject proposal, if not receipt prohibited
  95. if (!settings.getBoolean(Keys.federation.allowProposals, false)) {
  96. logger.error(MessageFormat.format("Rejected {0} federation proposal from {1}",
  97. proposal.tokenType.name(), proposal.url));
  98. response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
  99. return;
  100. }
  101. // poke the origin Gitblit instance that is proposing federation
  102. boolean poked = false;
  103. try {
  104. poked = FederationUtils.poke(proposal.url);
  105. } catch (Exception e) {
  106. logger.error("Failed to poke origin", e);
  107. }
  108. if (!poked) {
  109. logger.error(MessageFormat.format("Failed to send federation poke to {0}",
  110. proposal.url));
  111. response.setStatus(HttpServletResponse.SC_NOT_ACCEPTABLE);
  112. return;
  113. }
  114. String url = HttpUtils.getGitblitURL(request);
  115. federationManager.submitFederationProposal(proposal, url);
  116. logger.info(MessageFormat.format(
  117. "Submitted {0} federation proposal to pull {1} repositories from {2}",
  118. proposal.tokenType.name(), proposal.repositories.size(), proposal.url));
  119. response.setStatus(HttpServletResponse.SC_OK);
  120. return;
  121. }
  122. if (FederationRequest.STATUS.equals(reqType)) {
  123. // Receive a gitblit federation status acknowledgment
  124. String remoteId = StringUtils.decodeFromHtml(request.getParameter("url"));
  125. String identification = MessageFormat.format("{0} ({1})", remoteId,
  126. request.getRemoteAddr());
  127. // deserialize the status data
  128. FederationModel results = deserialize(request, response, FederationModel.class);
  129. if (results == null) {
  130. return;
  131. }
  132. // setup the last and netx pull dates
  133. results.lastPull = new Date();
  134. int mins = TimeUtils.convertFrequencyToMinutes(results.frequency);
  135. results.nextPull = new Date(System.currentTimeMillis() + (mins * 60 * 1000L));
  136. // acknowledge the receipt of status
  137. federationManager.acknowledgeFederationStatus(identification, results);
  138. logger.info(MessageFormat.format(
  139. "Received status of {0} federated repositories from {1}", results
  140. .getStatusList().size(), identification));
  141. response.setStatus(HttpServletResponse.SC_OK);
  142. return;
  143. }
  144. // Determine the federation tokens for this gitblit instance
  145. String token = request.getParameter("token");
  146. List<String> tokens = federationManager.getFederationTokens();
  147. if (!tokens.contains(token)) {
  148. logger.warn(MessageFormat.format(
  149. "Received Federation token ''{0}'' does not match the server tokens", token));
  150. response.sendError(HttpServletResponse.SC_FORBIDDEN);
  151. return;
  152. }
  153. Object result = null;
  154. if (FederationRequest.PULL_REPOSITORIES.equals(reqType)) {
  155. String gitblitUrl = HttpUtils.getGitblitURL(request);
  156. result = federationManager.getRepositories(gitblitUrl, token);
  157. } else {
  158. if (FederationRequest.PULL_SETTINGS.equals(reqType)) {
  159. // pull settings
  160. if (!federationManager.validateFederationRequest(reqType, token)) {
  161. // invalid token to pull users or settings
  162. logger.warn(MessageFormat.format(
  163. "Federation token from {0} not authorized to pull SETTINGS",
  164. request.getRemoteAddr()));
  165. response.sendError(HttpServletResponse.SC_FORBIDDEN);
  166. return;
  167. }
  168. Map<String, String> map = new HashMap<String, String>();
  169. List<String> keys = settings.getAllKeys(null);
  170. for (String key : keys) {
  171. map.put(key, settings.getString(key, ""));
  172. }
  173. result = map;
  174. } else if (FederationRequest.PULL_USERS.equals(reqType)) {
  175. // pull users
  176. if (!federationManager.validateFederationRequest(reqType, token)) {
  177. // invalid token to pull users or settings
  178. logger.warn(MessageFormat.format(
  179. "Federation token from {0} not authorized to pull USERS",
  180. request.getRemoteAddr()));
  181. response.sendError(HttpServletResponse.SC_FORBIDDEN);
  182. return;
  183. }
  184. List<String> usernames = userManager.getAllUsernames();
  185. List<UserModel> users = new ArrayList<UserModel>();
  186. for (String username : usernames) {
  187. UserModel user = userManager.getUserModel(username);
  188. if (!user.excludeFromFederation) {
  189. users.add(user);
  190. }
  191. }
  192. result = users;
  193. } else if (FederationRequest.PULL_TEAMS.equals(reqType)) {
  194. // pull teams
  195. if (!federationManager.validateFederationRequest(reqType, token)) {
  196. // invalid token to pull teams
  197. logger.warn(MessageFormat.format(
  198. "Federation token from {0} not authorized to pull TEAMS",
  199. request.getRemoteAddr()));
  200. response.sendError(HttpServletResponse.SC_FORBIDDEN);
  201. return;
  202. }
  203. List<String> teamnames = userManager.getAllTeamNames();
  204. List<TeamModel> teams = new ArrayList<TeamModel>();
  205. for (String teamname : teamnames) {
  206. TeamModel user = userManager.getTeamModel(teamname);
  207. teams.add(user);
  208. }
  209. result = teams;
  210. } else if (FederationRequest.PULL_SCRIPTS.equals(reqType)) {
  211. // pull scripts
  212. if (!federationManager.validateFederationRequest(reqType, token)) {
  213. // invalid token to pull script
  214. logger.warn(MessageFormat.format(
  215. "Federation token from {0} not authorized to pull SCRIPTS",
  216. request.getRemoteAddr()));
  217. response.sendError(HttpServletResponse.SC_FORBIDDEN);
  218. return;
  219. }
  220. Map<String, String> scripts = new HashMap<String, String>();
  221. Set<String> names = new HashSet<String>();
  222. names.addAll(settings.getStrings(Keys.groovy.preReceiveScripts));
  223. names.addAll(settings.getStrings(Keys.groovy.postReceiveScripts));
  224. for (TeamModel team : userManager.getAllTeams()) {
  225. names.addAll(team.preReceiveScripts);
  226. names.addAll(team.postReceiveScripts);
  227. }
  228. File scriptsFolder = repositoryManager.getHooksFolder();
  229. for (String name : names) {
  230. File file = new File(scriptsFolder, name);
  231. if (!file.exists() && !file.getName().endsWith(".groovy")) {
  232. file = new File(scriptsFolder, name + ".groovy");
  233. }
  234. if (file.exists()) {
  235. // read the script
  236. String content = FileUtils.readContent(file, "\n");
  237. scripts.put(name, content);
  238. } else {
  239. // missing script?!
  240. logger.warn(MessageFormat.format("Failed to find push script \"{0}\"", name));
  241. }
  242. }
  243. result = scripts;
  244. }
  245. }
  246. // send the result of the request
  247. serialize(response, result);
  248. }
  249. }