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.

SetPatAction.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.almintegration.ws;
  21. import com.google.common.base.Strings;
  22. import java.util.List;
  23. import java.util.Optional;
  24. import javax.annotation.Nullable;
  25. import org.sonar.api.server.ws.Change;
  26. import org.sonar.api.server.ws.Request;
  27. import org.sonar.api.server.ws.Response;
  28. import org.sonar.api.server.ws.WebService;
  29. import org.sonar.api.utils.Preconditions;
  30. import org.sonar.db.DbClient;
  31. import org.sonar.db.DbSession;
  32. import org.sonar.db.alm.pat.AlmPatDto;
  33. import org.sonar.db.alm.setting.AlmSettingDto;
  34. import org.sonar.server.exceptions.NotFoundException;
  35. import org.sonar.server.user.UserSession;
  36. import static java.util.Objects.requireNonNull;
  37. import static org.sonar.db.alm.setting.ALM.BITBUCKET_CLOUD;
  38. import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
  39. public class SetPatAction implements AlmIntegrationsWsAction {
  40. private static final String PARAM_ALM_SETTING = "almSetting";
  41. private static final String PARAM_PAT = "pat";
  42. private static final String PARAM_USERNAME = "username";
  43. private final DbClient dbClient;
  44. private final UserSession userSession;
  45. private final ImportHelper importHelper;
  46. public SetPatAction(DbClient dbClient, UserSession userSession, ImportHelper importHelper) {
  47. this.dbClient = dbClient;
  48. this.userSession = userSession;
  49. this.importHelper = importHelper;
  50. }
  51. @Override
  52. public void define(WebService.NewController context) {
  53. WebService.NewAction action = context.createAction("set_pat")
  54. .setDescription("Set a Personal Access Token for the given DevOps Platform setting<br/>" +
  55. "Requires the 'Create Projects' permission")
  56. .setPost(true)
  57. .setSince("8.2")
  58. .setHandler(this)
  59. .setChangelog(
  60. new Change("9.0", "Bitbucket Cloud support and optional Username parameter were added"),
  61. new Change("10.3", "Allow setting Personal Access Tokens for all DevOps platforms"),
  62. new Change("10.3", String.format("Parameter %s becomes optional if you have only one DevOps Platform configuration", PARAM_ALM_SETTING)));
  63. action.createParam(PARAM_ALM_SETTING)
  64. .setDescription("DevOps Platform configuration key. This parameter is optional if you have only one single DevOps Platform integration.");
  65. action.createParam(PARAM_PAT)
  66. .setRequired(true)
  67. .setMaximumLength(2000)
  68. .setDescription("Personal Access Token");
  69. action.createParam(PARAM_USERNAME)
  70. .setRequired(false)
  71. .setMaximumLength(2000)
  72. .setDescription("Username");
  73. }
  74. @Override
  75. public void handle(Request request, Response response) {
  76. doHandle(request);
  77. response.noContent();
  78. }
  79. private void doHandle(Request request) {
  80. try (DbSession dbSession = dbClient.openSession(false)) {
  81. userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
  82. String pat = request.mandatoryParam(PARAM_PAT);
  83. String username = request.param(PARAM_USERNAME);
  84. String userUuid = requireNonNull(userSession.getUuid(), "User UUID cannot be null");
  85. AlmSettingDto almSettingDto = importHelper.getAlmSettingDto(request);
  86. if (almSettingDto.getAlm().equals(BITBUCKET_CLOUD)) {
  87. Preconditions.checkArgument(!Strings.isNullOrEmpty(username), "Username cannot be null for Bitbucket Cloud");
  88. }
  89. String resultingPat = CredentialsEncoderHelper.encodeCredentials(almSettingDto.getAlm(), pat, username);
  90. Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
  91. if (almPatDto.isPresent()) {
  92. AlmPatDto almPat = almPatDto.get();
  93. almPat.setPersonalAccessToken(resultingPat);
  94. dbClient.almPatDao().update(dbSession, almPat, userSession.getLogin(), almSettingDto.getKey());
  95. } else {
  96. AlmPatDto almPat = new AlmPatDto()
  97. .setPersonalAccessToken(resultingPat)
  98. .setAlmSettingUuid(almSettingDto.getUuid())
  99. .setUserUuid(userUuid);
  100. dbClient.almPatDao().insert(dbSession, almPat, userSession.getLogin(), almSettingDto.getKey());
  101. }
  102. dbSession.commit();
  103. }
  104. }
  105. public AlmSettingDto getAlmConfig(@Nullable String almSettingKey) {
  106. try (DbSession dbSession = dbClient.openSession(false)) {
  107. if (almSettingKey != null) {
  108. return getAlmSettingDtoFromKey(dbSession, almSettingKey);
  109. }
  110. return getAlmSettingDtoFromAlm(dbSession);
  111. }
  112. }
  113. private AlmSettingDto getAlmSettingDtoFromKey(DbSession dbSession, String almSettingKey) {
  114. return dbClient.almSettingDao().selectByKey(dbSession, almSettingKey)
  115. .orElseThrow(() -> new NotFoundException(String.format("DevOps Platform configuration '%s' not found.", almSettingKey)));
  116. }
  117. private AlmSettingDto getAlmSettingDtoFromAlm(DbSession dbSession) {
  118. List<AlmSettingDto> almSettingDtos = dbClient.almSettingDao().selectAll(dbSession);
  119. if (almSettingDtos.isEmpty()) {
  120. throw new NotFoundException("There is no configuration for DevOps Platforms. Please add one.");
  121. }
  122. if (almSettingDtos.size() == 1) {
  123. return almSettingDtos.get(0);
  124. }
  125. throw new IllegalArgumentException(String.format("Parameter %s is required as there are multiple DevOps Platform configurations.", PARAM_ALM_SETTING));
  126. }
  127. }