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.

CreateAction.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.webhook.ws;
  21. import javax.annotation.Nullable;
  22. import org.sonar.api.server.ws.Request;
  23. import org.sonar.api.server.ws.Response;
  24. import org.sonar.api.server.ws.WebService;
  25. import org.sonar.core.util.UuidFactory;
  26. import org.sonar.db.DbClient;
  27. import org.sonar.db.DbSession;
  28. import org.sonar.db.project.ProjectDto;
  29. import org.sonar.db.webhook.WebhookDto;
  30. import org.sonar.server.component.ComponentFinder;
  31. import org.sonar.server.user.UserSession;
  32. import static java.lang.String.format;
  33. import static org.apache.commons.lang.StringUtils.isNotBlank;
  34. import static org.sonar.server.webhook.ws.WebhooksWsParameters.ACTION_CREATE;
  35. import static org.sonar.server.webhook.ws.WebhooksWsParameters.NAME_PARAM;
  36. import static org.sonar.server.webhook.ws.WebhooksWsParameters.NAME_PARAM_MAXIMUM_LENGTH;
  37. import static org.sonar.server.webhook.ws.WebhooksWsParameters.PROJECT_KEY_PARAM;
  38. import static org.sonar.server.webhook.ws.WebhooksWsParameters.PROJECT_KEY_PARAM_MAXIMUM_LENGTH;
  39. import static org.sonar.server.webhook.ws.WebhooksWsParameters.SECRET_PARAM;
  40. import static org.sonar.server.webhook.ws.WebhooksWsParameters.SECRET_PARAM_MAXIMUM_LENGTH;
  41. import static org.sonar.server.webhook.ws.WebhooksWsParameters.URL_PARAM;
  42. import static org.sonar.server.webhook.ws.WebhooksWsParameters.URL_PARAM_MAXIMUM_LENGTH;
  43. import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
  44. import static org.sonar.server.ws.KeyExamples.NAME_WEBHOOK_EXAMPLE_001;
  45. import static org.sonar.server.ws.KeyExamples.URL_WEBHOOK_EXAMPLE_001;
  46. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  47. import static org.sonarqube.ws.Webhooks.CreateWsResponse.Webhook;
  48. import static org.sonarqube.ws.Webhooks.CreateWsResponse.newBuilder;
  49. public class CreateAction implements WebhooksWsAction {
  50. private static final int MAX_NUMBER_OF_WEBHOOKS = 10;
  51. private final DbClient dbClient;
  52. private final UserSession userSession;
  53. private final UuidFactory uuidFactory;
  54. private final WebhookSupport webhookSupport;
  55. private final ComponentFinder componentFinder;
  56. public CreateAction(DbClient dbClient, UserSession userSession, UuidFactory uuidFactory, WebhookSupport webhookSupport, ComponentFinder componentFinder) {
  57. this.dbClient = dbClient;
  58. this.userSession = userSession;
  59. this.uuidFactory = uuidFactory;
  60. this.webhookSupport = webhookSupport;
  61. this.componentFinder = componentFinder;
  62. }
  63. @Override
  64. public void define(WebService.NewController controller) {
  65. WebService.NewAction action = controller.createAction(ACTION_CREATE)
  66. .setPost(true)
  67. .setDescription("Create a Webhook.<br>" +
  68. "Requires 'Administer' permission on the specified project, or global 'Administer' permission.")
  69. .setSince("7.1")
  70. .setResponseExample(getClass().getResource("example-webhook-create.json"))
  71. .setHandler(this);
  72. action.createParam(NAME_PARAM)
  73. .setRequired(true)
  74. .setMaximumLength(NAME_PARAM_MAXIMUM_LENGTH)
  75. .setDescription("Name displayed in the administration console of webhooks")
  76. .setExampleValue(NAME_WEBHOOK_EXAMPLE_001);
  77. action.createParam(URL_PARAM)
  78. .setRequired(true)
  79. .setMaximumLength(URL_PARAM_MAXIMUM_LENGTH)
  80. .setDescription("Server endpoint that will receive the webhook payload, for example 'http://my_server/foo'." +
  81. " If HTTP Basic authentication is used, HTTPS is recommended to avoid man in the middle attacks." +
  82. " Example: 'https://myLogin:myPassword@my_server/foo'")
  83. .setExampleValue(URL_WEBHOOK_EXAMPLE_001);
  84. action.createParam(PROJECT_KEY_PARAM)
  85. .setRequired(false)
  86. .setMaximumLength(PROJECT_KEY_PARAM_MAXIMUM_LENGTH)
  87. .setDescription("The key of the project that will own the webhook")
  88. .setExampleValue(KEY_PROJECT_EXAMPLE_001);
  89. action.createParam(SECRET_PARAM)
  90. .setRequired(false)
  91. .setMinimumLength(1)
  92. .setMaximumLength(SECRET_PARAM_MAXIMUM_LENGTH)
  93. .setDescription("If provided, secret will be used as the key to generate the HMAC hex (lowercase) digest value in the 'X-Sonar-Webhook-HMAC-SHA256' header")
  94. .setExampleValue("your_secret")
  95. .setSince("7.8");
  96. }
  97. @Override
  98. public void handle(Request request, Response response) throws Exception {
  99. userSession.checkLoggedIn();
  100. String name = request.mandatoryParam(NAME_PARAM);
  101. String url = request.mandatoryParam(URL_PARAM);
  102. String projectKey = request.param(PROJECT_KEY_PARAM);
  103. String secret = request.param(SECRET_PARAM);
  104. try (DbSession dbSession = dbClient.openSession(false)) {
  105. ProjectDto projectDto = null;
  106. if (isNotBlank(projectKey)) {
  107. projectDto = componentFinder.getProjectByKey(dbSession, projectKey);
  108. webhookSupport.checkPermission(projectDto);
  109. } else {
  110. webhookSupport.checkPermission();
  111. }
  112. webhookSupport.checkUrlPattern(url, "Url parameter with value '%s' is not a valid url", url);
  113. WebhookDto dto = doHandle(dbSession, projectDto, name, url, secret);
  114. String projectName = projectDto == null ? null : projectDto.getName();
  115. dbClient.webhookDao().insert(dbSession, dto, projectKey, projectName);
  116. dbSession.commit();
  117. writeResponse(request, response, dto);
  118. }
  119. }
  120. private WebhookDto doHandle(DbSession dbSession, @Nullable ProjectDto project, String name, String url, @Nullable String secret) {
  121. WebhookDto dto = new WebhookDto()
  122. .setUuid(uuidFactory.create())
  123. .setName(name)
  124. .setUrl(url)
  125. .setSecret(secret);
  126. if (project != null) {
  127. checkNumberOfWebhook(numberOfWebhookOf(dbSession, project), project.getKey());
  128. dto.setProjectUuid(project.getUuid());
  129. } else {
  130. checkNumberOfGlobalWebhooks(dbSession);
  131. }
  132. return dto;
  133. }
  134. private static void writeResponse(Request request, Response response, WebhookDto dto) {
  135. Webhook.Builder webhookBuilder = Webhook.newBuilder();
  136. webhookBuilder
  137. .setKey(dto.getUuid())
  138. .setName(dto.getName())
  139. .setUrl(dto.getUrl())
  140. .setHasSecret(dto.getSecret() != null);
  141. writeProtobuf(newBuilder().setWebhook(webhookBuilder).build(), request, response);
  142. }
  143. private static void checkNumberOfWebhook(int nbOfWebhooks, String projectKey) {
  144. if (nbOfWebhooks >= MAX_NUMBER_OF_WEBHOOKS) {
  145. throw new IllegalArgumentException(format("Maximum number of webhook reached for project '%s'", projectKey));
  146. }
  147. }
  148. private int numberOfWebhookOf(DbSession dbSession, ProjectDto projectDto) {
  149. return dbClient.webhookDao().selectByProject(dbSession, projectDto).size();
  150. }
  151. private void checkNumberOfGlobalWebhooks(DbSession dbSession) {
  152. int globalWebhooksCount = dbClient.webhookDao().selectGlobalWebhooks(dbSession).size();
  153. if (globalWebhooksCount >= MAX_NUMBER_OF_WEBHOOKS) {
  154. throw new IllegalArgumentException("Maximum number of global webhooks reached");
  155. }
  156. }
  157. }