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.

GithubProjectCreatorFactory.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.common.almsettings.github;
  21. import java.util.Map;
  22. import java.util.Optional;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import org.sonar.alm.client.github.GithubGlobalSettingsValidator;
  26. import org.sonar.alm.client.github.GithubPermissionConverter;
  27. import org.sonar.api.server.ServerSide;
  28. import org.sonar.auth.github.AppInstallationToken;
  29. import org.sonar.auth.github.GitHubSettings;
  30. import org.sonar.auth.github.GithubAppConfiguration;
  31. import org.sonar.auth.github.client.GithubApplicationClient;
  32. import org.sonar.auth.github.security.AccessToken;
  33. import org.sonar.auth.github.security.UserAccessToken;
  34. import org.sonar.db.DbClient;
  35. import org.sonar.db.DbSession;
  36. import org.sonar.db.alm.pat.AlmPatDto;
  37. import org.sonar.db.alm.setting.ALM;
  38. import org.sonar.db.alm.setting.AlmSettingDto;
  39. import org.sonar.server.common.almintegration.ProjectKeyGenerator;
  40. import org.sonar.server.common.almsettings.DevOpsProjectCreator;
  41. import org.sonar.server.common.almsettings.DevOpsProjectCreatorFactory;
  42. import org.sonar.server.common.almsettings.DevOpsProjectDescriptor;
  43. import org.sonar.server.common.permission.PermissionUpdater;
  44. import org.sonar.server.common.permission.UserPermissionChange;
  45. import org.sonar.server.common.project.ProjectCreator;
  46. import org.sonar.server.exceptions.BadConfigurationException;
  47. import org.sonar.server.management.ManagedProjectService;
  48. import org.sonar.server.permission.PermissionService;
  49. import org.sonar.server.user.UserSession;
  50. import static java.lang.String.format;
  51. import static java.util.Objects.requireNonNull;
  52. import static org.sonar.core.ce.CeTaskCharacteristics.DEVOPS_PLATFORM_PROJECT_IDENTIFIER;
  53. import static org.sonar.core.ce.CeTaskCharacteristics.DEVOPS_PLATFORM_URL;
  54. @ServerSide
  55. public class GithubProjectCreatorFactory implements DevOpsProjectCreatorFactory {
  56. private static final Logger LOG = LoggerFactory.getLogger(GithubProjectCreatorFactory.class);
  57. private final DbClient dbClient;
  58. private final GithubGlobalSettingsValidator githubGlobalSettingsValidator;
  59. private final GithubApplicationClient githubApplicationClient;
  60. private final ProjectKeyGenerator projectKeyGenerator;
  61. private final ProjectCreator projectCreator;
  62. private final UserSession userSession;
  63. private final GitHubSettings gitHubSettings;
  64. private final GithubPermissionConverter githubPermissionConverter;
  65. private final PermissionUpdater<UserPermissionChange> permissionUpdater;
  66. private final PermissionService permissionService;
  67. private final ManagedProjectService managedProjectService;
  68. public GithubProjectCreatorFactory(DbClient dbClient, GithubGlobalSettingsValidator githubGlobalSettingsValidator,
  69. GithubApplicationClient githubApplicationClient, ProjectKeyGenerator projectKeyGenerator, UserSession userSession,
  70. ProjectCreator projectCreator, GitHubSettings gitHubSettings, GithubPermissionConverter githubPermissionConverter,
  71. PermissionUpdater<UserPermissionChange> permissionUpdater, PermissionService permissionService, ManagedProjectService managedProjectService) {
  72. this.dbClient = dbClient;
  73. this.githubGlobalSettingsValidator = githubGlobalSettingsValidator;
  74. this.githubApplicationClient = githubApplicationClient;
  75. this.projectKeyGenerator = projectKeyGenerator;
  76. this.userSession = userSession;
  77. this.projectCreator = projectCreator;
  78. this.gitHubSettings = gitHubSettings;
  79. this.githubPermissionConverter = githubPermissionConverter;
  80. this.permissionUpdater = permissionUpdater;
  81. this.permissionService = permissionService;
  82. this.managedProjectService = managedProjectService;
  83. }
  84. @Override
  85. public Optional<DevOpsProjectCreator> getDevOpsProjectCreator(DbSession dbSession, Map<String, String> characteristics) {
  86. String githubApiUrl = characteristics.get(DEVOPS_PLATFORM_URL);
  87. String githubRepository = characteristics.get(DEVOPS_PLATFORM_PROJECT_IDENTIFIER);
  88. if (githubApiUrl == null || githubRepository == null) {
  89. return Optional.empty();
  90. }
  91. DevOpsProjectDescriptor devOpsProjectDescriptor = new DevOpsProjectDescriptor(ALM.GITHUB, githubApiUrl, githubRepository);
  92. return dbClient.almSettingDao().selectByAlm(dbSession, ALM.GITHUB).stream()
  93. .filter(almSettingDto -> devOpsProjectDescriptor.url().equals(almSettingDto.getUrl()))
  94. .map(almSettingDto -> findInstallationIdAndCreateDevOpsProjectCreator(devOpsProjectDescriptor, almSettingDto))
  95. .flatMap(Optional::stream)
  96. .findFirst();
  97. }
  98. private Optional<DevOpsProjectCreator> findInstallationIdAndCreateDevOpsProjectCreator(DevOpsProjectDescriptor devOpsProjectDescriptor,
  99. AlmSettingDto almSettingDto) {
  100. GithubAppConfiguration githubAppConfiguration = githubGlobalSettingsValidator.validate(almSettingDto);
  101. return findInstallationIdToAccessRepo(githubAppConfiguration, devOpsProjectDescriptor.projectIdentifier())
  102. .map(installationId -> generateAppInstallationToken(githubAppConfiguration, installationId))
  103. .map(appInstallationToken -> createGithubProjectCreator(devOpsProjectDescriptor, almSettingDto, appInstallationToken));
  104. }
  105. private GithubProjectCreator createGithubProjectCreator(DevOpsProjectDescriptor devOpsProjectDescriptor, AlmSettingDto almSettingDto,
  106. AppInstallationToken appInstallationToken) {
  107. LOG.info("DevOps configuration {} auto-detected for project {}", almSettingDto.getKey(), devOpsProjectDescriptor.projectIdentifier());
  108. Optional<AppInstallationToken> authAppInstallationToken = getAuthAppInstallationTokenIfNecessary(devOpsProjectDescriptor);
  109. GithubProjectCreationParameters githubProjectCreationParameters = new GithubProjectCreationParameters(devOpsProjectDescriptor, almSettingDto, userSession, appInstallationToken,
  110. authAppInstallationToken.orElse(null));
  111. return new GithubProjectCreator(dbClient, githubApplicationClient, githubPermissionConverter, projectKeyGenerator, permissionUpdater, permissionService,
  112. managedProjectService, projectCreator, githubProjectCreationParameters, gitHubSettings);
  113. }
  114. @Override
  115. public Optional<DevOpsProjectCreator> getDevOpsProjectCreator(AlmSettingDto almSettingDto,
  116. DevOpsProjectDescriptor devOpsProjectDescriptor) {
  117. if (almSettingDto.getAlm() != ALM.GITHUB) {
  118. return Optional.empty();
  119. }
  120. try (DbSession dbSession = dbClient.openSession(false)) {
  121. AccessToken accessToken = getAccessToken(dbSession, almSettingDto);
  122. Optional<AppInstallationToken> authAppInstallationToken = getAuthAppInstallationTokenIfNecessary(devOpsProjectDescriptor);
  123. GithubProjectCreationParameters githubProjectCreationParameters = new GithubProjectCreationParameters(devOpsProjectDescriptor, almSettingDto, userSession, accessToken,
  124. authAppInstallationToken.orElse(null));
  125. GithubProjectCreator githubProjectCreator = new GithubProjectCreator(dbClient, githubApplicationClient, githubPermissionConverter, projectKeyGenerator, permissionUpdater,
  126. permissionService, managedProjectService, this.projectCreator, githubProjectCreationParameters, gitHubSettings);
  127. return Optional.of(githubProjectCreator);
  128. }
  129. }
  130. private AccessToken getAccessToken(DbSession dbSession, AlmSettingDto almSettingDto) {
  131. String userUuid = requireNonNull(userSession.getUuid(), "User UUID cannot be null.");
  132. return dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto)
  133. .map(AlmPatDto::getPersonalAccessToken)
  134. .map(UserAccessToken::new)
  135. .orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
  136. }
  137. private Optional<AppInstallationToken> getAuthAppInstallationTokenIfNecessary(DevOpsProjectDescriptor devOpsProjectDescriptor) {
  138. if (gitHubSettings.isProvisioningEnabled()) {
  139. GithubAppConfiguration githubAppConfiguration = new GithubAppConfiguration(Long.parseLong(gitHubSettings.appId()), gitHubSettings.privateKey(), gitHubSettings.apiURL());
  140. long installationId = findInstallationIdToAccessRepo(githubAppConfiguration, devOpsProjectDescriptor.projectIdentifier())
  141. .orElseThrow(() -> new BadConfigurationException("PROJECT",
  142. format("GitHub auto-provisioning is activated. However the repo %s is not in the scope of the authentication application. "
  143. + "The permissions can't be checked, and the project can not be created.",
  144. devOpsProjectDescriptor.projectIdentifier())));
  145. return Optional.of(generateAppInstallationToken(githubAppConfiguration, installationId));
  146. }
  147. return Optional.empty();
  148. }
  149. private Optional<Long> findInstallationIdToAccessRepo(GithubAppConfiguration githubAppConfiguration, String repositoryKey) {
  150. return githubApplicationClient.getInstallationId(githubAppConfiguration, repositoryKey);
  151. }
  152. private AppInstallationToken generateAppInstallationToken(GithubAppConfiguration githubAppConfiguration, long installationId) {
  153. return githubApplicationClient.createAppInstallationToken(githubAppConfiguration, installationId)
  154. .orElseThrow(() -> new IllegalStateException(format("Error while generating token for GitHub Api Url %s (installation id: %s)",
  155. githubAppConfiguration.getApiEndpoint(), installationId)));
  156. }
  157. }