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.

UpdateAction.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 java.util.Optional;
  22. import javax.annotation.Nullable;
  23. import org.sonar.api.server.ws.Request;
  24. import org.sonar.api.server.ws.Response;
  25. import org.sonar.api.server.ws.WebService;
  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 org.apache.commons.lang.StringUtils.isBlank;
  33. import static org.sonar.server.exceptions.NotFoundException.checkFoundWithOptional;
  34. import static org.sonar.server.webhook.ws.WebhooksWsParameters.KEY_PARAM;
  35. import static org.sonar.server.webhook.ws.WebhooksWsParameters.KEY_PARAM_MAXIMUM_LENGTH;
  36. import static org.sonar.server.webhook.ws.WebhooksWsParameters.NAME_PARAM;
  37. import static org.sonar.server.webhook.ws.WebhooksWsParameters.NAME_PARAM_MAXIMUM_LENGTH;
  38. import static org.sonar.server.webhook.ws.WebhooksWsParameters.SECRET_PARAM;
  39. import static org.sonar.server.webhook.ws.WebhooksWsParameters.SECRET_PARAM_MAXIMUM_LENGTH;
  40. import static org.sonar.server.webhook.ws.WebhooksWsParameters.UPDATE_ACTION;
  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. public class UpdateAction implements WebhooksWsAction {
  47. private final DbClient dbClient;
  48. private final UserSession userSession;
  49. private final WebhookSupport webhookSupport;
  50. private final ComponentFinder componentFinder;
  51. public UpdateAction(DbClient dbClient, UserSession userSession, WebhookSupport webhookSupport, ComponentFinder componentFinder) {
  52. this.dbClient = dbClient;
  53. this.userSession = userSession;
  54. this.webhookSupport = webhookSupport;
  55. this.componentFinder = componentFinder;
  56. }
  57. @Override
  58. public void define(WebService.NewController controller) {
  59. WebService.NewAction action = controller.createAction(UPDATE_ACTION)
  60. .setPost(true)
  61. .setDescription("Update a Webhook.<br>" +
  62. "Requires 'Administer' permission on the specified project, or global 'Administer' permission.")
  63. .setSince("7.1")
  64. .setHandler(this);
  65. action.createParam(KEY_PARAM)
  66. .setRequired(true)
  67. .setMaximumLength(KEY_PARAM_MAXIMUM_LENGTH)
  68. .setDescription("The key of the webhook to be updated, " +
  69. "auto-generated value can be obtained through api/webhooks/create or api/webhooks/list")
  70. .setExampleValue(KEY_PROJECT_EXAMPLE_001);
  71. action.createParam(NAME_PARAM)
  72. .setRequired(true)
  73. .setMaximumLength(NAME_PARAM_MAXIMUM_LENGTH)
  74. .setDescription("new name of the webhook")
  75. .setExampleValue(NAME_WEBHOOK_EXAMPLE_001);
  76. action.createParam(URL_PARAM)
  77. .setRequired(true)
  78. .setMaximumLength(URL_PARAM_MAXIMUM_LENGTH)
  79. .setDescription("new url to be called by the webhook")
  80. .setExampleValue(URL_WEBHOOK_EXAMPLE_001);
  81. action.createParam(SECRET_PARAM)
  82. .setRequired(false)
  83. .setMaximumLength(SECRET_PARAM_MAXIMUM_LENGTH)
  84. .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. " +
  85. "If blank, any secret previously configured will be removed. If not set, the secret will remain unchanged.")
  86. .setExampleValue("your_secret")
  87. .setSince("7.8");
  88. }
  89. @Override
  90. public void handle(Request request, Response response) throws Exception {
  91. userSession.checkLoggedIn();
  92. String webhookKey = request.mandatoryParam(KEY_PARAM);
  93. String name = request.mandatoryParam(NAME_PARAM);
  94. String url = request.mandatoryParam(URL_PARAM);
  95. String secret = request.param(SECRET_PARAM);
  96. webhookSupport.checkUrlPattern(url, "Url parameter with value '%s' is not a valid url", url);
  97. try (DbSession dbSession = dbClient.openSession(false)) {
  98. Optional<WebhookDto> dtoOptional = dbClient.webhookDao().selectByUuid(dbSession, webhookKey);
  99. WebhookDto webhookDto = checkFoundWithOptional(dtoOptional, "No webhook with key '%s'", webhookKey);
  100. String projectUuid = webhookDto.getProjectUuid();
  101. if (projectUuid != null) {
  102. ProjectDto projectDto = componentFinder.getProjectByUuid(dbSession, projectUuid);
  103. webhookSupport.checkPermission(projectDto);
  104. updateWebhook(dbSession, webhookDto, name, url, secret, projectDto.getKey(), projectDto.getName());
  105. } else {
  106. webhookSupport.checkPermission();
  107. updateWebhook(dbSession, webhookDto, name, url, secret, null, null);
  108. }
  109. dbSession.commit();
  110. }
  111. response.noContent();
  112. }
  113. private void updateWebhook(DbSession dbSession, WebhookDto dto, String name, String url, @Nullable String secret,
  114. @Nullable String projectKey, @Nullable String projectName) {
  115. dto
  116. .setName(name)
  117. .setUrl(url);
  118. setSecret(dto, secret);
  119. dbClient.webhookDao().update(dbSession, dto, projectKey, projectName);
  120. }
  121. /**
  122. * <p>Sets the secret of the webhook. The secret is set according to the following rules:
  123. * <ul>
  124. * <li>If the secret is null, it will remain unchanged.</li>
  125. * <li>If the secret is blank (""), it will be removed.</li>
  126. * <li>If the secret is not null or blank, it will be set to the provided value.</li>
  127. * </ul>
  128. * </p>
  129. * @param dto The webhook to update. It holds the old secret value.
  130. * @param secret The new secret value. It can be null or blank.
  131. */
  132. private static void setSecret(WebhookDto dto, @Nullable String secret) {
  133. if (secret != null) {
  134. if (isBlank(secret)) {
  135. dto.setSecret(null);
  136. } else {
  137. dto.setSecret(secret);
  138. }
  139. }
  140. }
  141. }