]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19580 fixed an issue with project badges for private applications
authorLukasz Jarocki <lukasz.jarocki@sonarsource.com>
Tue, 27 Jun 2023 09:47:17 +0000 (11:47 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 3 Jul 2023 20:03:25 +0000 (20:03 +0000)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/ProjectBadgesWs.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/TokenAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/TokenRenewAction.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/TokenActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/TokenRenewActionTest.java

index 146d1794608ba844821b0c821f9143ac31eda6a4..2f0b68bcb53f7beb24cd9a5421636e7c554c3e4c 100644 (file)
@@ -24,6 +24,8 @@ import org.sonar.api.server.ws.WebService;
 
 public class ProjectBadgesWs implements WebService {
 
+  static final String PROJECT_OR_APP_NOT_FOUND = "Project or Application not found";
+
   private final List<ProjectBadgesWsAction> actions;
 
   public ProjectBadgesWs(List<ProjectBadgesWsAction> actions) {
index 0d83e52cc1ab3d931e2c1b8c4a1556c2f0eff989..910979e4011e965e6fa0d30ca32f4f254145a6ce 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.badge.ws;
 
 import com.google.common.io.Resources;
+import org.sonar.api.server.ws.Change;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
@@ -34,6 +35,8 @@ import org.sonar.server.user.UserSession;
 import org.sonar.server.usertoken.TokenGenerator;
 import org.sonarqube.ws.ProjectBadgeToken.TokenWsResponse;
 
+import static java.lang.String.format;
+import static org.sonar.server.badge.ws.ProjectBadgesWs.PROJECT_OR_APP_NOT_FOUND;
 import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
 
@@ -55,9 +58,10 @@ public class TokenAction implements ProjectBadgesWsAction {
     NewAction action = controller.createAction("token")
       .setHandler(this)
       .setSince("9.2")
-      .setDescription("Retrieve a token to use for project badge access for private projects.<br/>" +
+      .setChangelog(new Change("9.9", format("Application key can be used for %s parameter.", PROJECT_KEY_PARAM)))
+      .setDescription("Retrieve a token to use for project or application badge access for private projects or applications.<br/>" +
         "This token can be used to authenticate with api/project_badges/quality_gate and api/project_badges/measure endpoints.<br/>" +
-        "Requires 'Browse' permission on the specified project.")
+        "Requires 'Browse' permission on the specified project or application.")
       .setResponseExample(Resources.getResource(getClass(), "token-example.json"));
     action.createParam(PROJECT_KEY_PARAM)
       .setDescription("Project or application key")
@@ -75,7 +79,8 @@ public class TokenAction implements ProjectBadgesWsAction {
     try (DbSession dbSession = dbClient.openSession(false)) {
       String projectKey = request.mandatoryParam(PROJECT_KEY_PARAM);
 
-      ProjectDto projectDto = dbClient.projectDao().selectProjectByKey(dbSession, projectKey).orElseThrow(() -> new IllegalArgumentException("project not found"));
+      ProjectDto projectDto = dbClient.projectDao().selectProjectOrAppByKey(dbSession, projectKey)
+        .orElseThrow(() -> new IllegalArgumentException(PROJECT_OR_APP_NOT_FOUND));
       userSession.checkProjectPermission(UserRole.USER, projectDto);
       ProjectBadgeTokenDto projectBadgeTokenDto = dbClient.projectBadgeTokenDao().selectTokenByProject(dbSession, projectDto);
 
index a79f479c4715f2423b7afa35589bd1cae2511935..7c751010f752eeb8298b2c9dffc79e9376d18717 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.badge.ws;
 
+import org.sonar.api.server.ws.Change;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
@@ -31,6 +32,8 @@ import org.sonar.db.user.TokenType;
 import org.sonar.server.user.UserSession;
 import org.sonar.server.usertoken.TokenGenerator;
 
+import static java.lang.String.format;
+import static org.sonar.server.badge.ws.ProjectBadgesWs.PROJECT_OR_APP_NOT_FOUND;
 import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
 
 public class TokenRenewAction implements ProjectBadgesWsAction {
@@ -52,11 +55,13 @@ public class TokenRenewAction implements ProjectBadgesWsAction {
       .setHandler(this)
       .setSince("9.2")
       .setPost(true)
-      .setDescription("Creates new token replacing any existing token for project badge access for private projects.<br/>" +
+      .setChangelog(new Change("9.9", format("Application key can be used for %s parameter.", PROJECT_KEY_PARAM)))
+      .setDescription("Creates new token replacing any existing token for project or application badge access for private projects and " +
+        "applications.<br/>" +
         "This token can be used to authenticate with api/project_badges/quality_gate and api/project_badges/measure endpoints.<br/>" +
-        "Requires 'Administer' permission on the specified project.");
+        "Requires 'Administer' permission on the specified project or application.");
     action.createParam(PROJECT_KEY_PARAM)
-      .setDescription("Project key")
+      .setDescription("Project or application key")
       .setRequired(true)
       .setExampleValue(KEY_PROJECT_EXAMPLE_001);
   }
@@ -71,7 +76,8 @@ public class TokenRenewAction implements ProjectBadgesWsAction {
     try (DbSession dbSession = dbClient.openSession(false)) {
       String projectKey = request.mandatoryParam(PROJECT_KEY_PARAM);
 
-      ProjectDto projectDto = dbClient.projectDao().selectProjectByKey(dbSession, projectKey).orElseThrow(() -> new IllegalArgumentException("project not found"));
+      ProjectDto projectDto = dbClient.projectDao().selectProjectOrAppByKey(dbSession, projectKey)
+        .orElseThrow(() -> new IllegalArgumentException(PROJECT_OR_APP_NOT_FOUND));
       userSession.checkProjectPermission(UserRole.ADMIN, projectDto);
       String newGeneratedToken = tokenGenerator.generate(TokenType.PROJECT_BADGE_TOKEN);
       dbClient.projectBadgeTokenDao().upsert(dbSession, newGeneratedToken, projectDto, userSession.getUuid(), userSession.getLogin());
index 701e7db0e27d9e41cfd2b05b19b2dcd1a6633ce2..e8b606e7c441faea9024f607b681026049ae0d80 100644 (file)
@@ -81,6 +81,18 @@ public class TokenActionTest {
     response.assertJson("{\"token\":\"generated_token\"}");
   }
 
+  @Test
+  public void handle_whenApplicationKeyPassed_shouldReturnToken() {
+    ComponentDto application = db.components().insertPrivateApplication();
+    userSession.logIn().addProjectPermission(UserRole.USER, application);
+    when(tokenGenerator.generate(TokenType.PROJECT_BADGE_TOKEN)).thenReturn("generated_token");
+
+    TestResponse response = ws.newRequest().setParam("project", application.getKey()).execute();
+
+    response.assertJson("{\"token\":\"generated_token\"}");
+  }
+
+
   @Test
   public void should_reuse_generated_token() {
     ComponentDto project = db.components().insertPrivateProject();
index 55eb86bb59ab8df36b9ea5c4540c59489355ab14..1fa68c502604f0d96b3ed0fb3866384f3c665aff 100644 (file)
@@ -97,6 +97,20 @@ public class TokenRenewActionTest {
     response.assertNoContent();
   }
 
+  @Test
+  public void handle_whenApplicationKeyPassed_shouldAddTokenAndReturn204() {
+    ProjectDto application = db.components().insertPrivateApplicationDto();
+    userSession.logIn().addProjectPermission(UserRole.ADMIN, application);
+    when(tokenGenerator.generate(TokenType.PROJECT_BADGE_TOKEN)).thenReturn("generated_token");
+
+    TestResponse response = ws.newRequest().setParam("project", application.getKey()).execute();
+
+    ProjectBadgeTokenDto projectBadgeTokenDto = db.getDbClient().projectBadgeTokenDao().selectTokenByProject(db.getSession(), application);
+    assertThat(projectBadgeTokenDto).isNotNull();
+    assertThat(projectBadgeTokenDto.getToken()).isEqualTo("generated_token");
+    response.assertNoContent();
+  }
+
   @Test
   public void should_replace_existing_token_when__token_already_present_and_update_update_at() {
     ProjectDto project = db.components().insertPrivateProjectDto();