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 10.0KB

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