]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-13515 Telemetry - include external authentication providers
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Wed, 24 Feb 2021 20:11:53 +0000 (14:11 -0600)
committersonartech <sonartech@sonarsource.com>
Thu, 4 Mar 2021 20:12:48 +0000 (20:12 +0000)
12 files changed:
server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/UserMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDaoTest.java
server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryData.java
server/sonar-server-common/src/main/java/org/sonar/server/telemetry/TelemetryDataJsonWriter.java
server/sonar-server-common/src/test/java/org/sonar/server/telemetry/TelemetryDataJsonWriterTest.java
server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/ClusterSystemInfoWriterTest.java
server/sonar-webserver-core/src/test/java/org/sonar/server/platform/StandaloneSystemInfoWriterTest.java
server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDaemonTest.java
server/sonar-webserver-core/src/test/java/org/sonar/server/telemetry/TelemetryDataLoaderImplTest.java

index 874eec147aa28248b83bd56e6ede318172b96fec..b498d86c0ddb6b990f908c6a5872da386a7adf7d 100644 (file)
@@ -158,6 +158,10 @@ public class UserDao implements Dao {
     return mapper(dbSession).selectByExternalIdAndIdentityProvider(externalId, externalIdentityProvider);
   }
 
+  public List<String> selectExternalIdentityProviders(DbSession dbSession) {
+    return mapper(dbSession).selectExternalIdentityProviders();
+  }
+
   public List<UserDto> selectByExternalIdsAndIdentityProvider(DbSession dbSession, Collection<String> externalIds, String externalIdentityProvider) {
     return executeLargeInputs(externalIds, e -> mapper(dbSession).selectByExternalIdsAndIdentityProvider(e, externalIdentityProvider));
   }
index cce552fcc15587e387c9a29e516b07138d3e0599..930ce1d9464e045bb6eeed3171799d632084336c 100644 (file)
@@ -62,6 +62,8 @@ public interface UserMapper {
   @CheckForNull
   UserDto selectByExternalLoginAndIdentityProvider(@Param("externalLogin") String externalLogin, @Param("externalIdentityProvider") String externalExternalIdentityProvider);
 
+  List<String> selectExternalIdentityProviders();
+
   void scrollAll(ResultHandler<UserDto> handler);
 
   /**
index 8909e23aafac1c81bdd167145072925e44675965..323ef1d27bb08ab34b2559d15538d8e9712a8096 100644 (file)
         AND u.active=${_true}
     </select>
 
+    <select id="selectExternalIdentityProviders" resultType="string">
+        SELECT distinct(external_identity_provider) from users
+    </select>
+
     <select id="selectByExternalIdAndIdentityProvider" parameterType="map" resultType="User">
         SELECT
         <include refid="userColumns"/>
index f6a7eea6954fb1574b2b6a45fe2cf2c1ad60e30e..d69d1ce652992f94d011765ba5b9f37b75368ff5 100644 (file)
@@ -79,6 +79,15 @@ public class UserDaoTest {
     assertThat(user).isNull();
   }
 
+  @Test
+  public void selectExternalIdentityProviders() {
+    db.users().insertUser(user -> user.setLogin("user1").setExternalIdentityProvider("github"));
+    db.users().insertUser(user -> user.setLogin("user2").setExternalIdentityProvider("sonarqube"));
+    db.users().insertUser(user -> user.setLogin("user3").setExternalIdentityProvider("github"));
+
+    assertThat(underTest.selectExternalIdentityProviders(session)).containsExactlyInAnyOrder("github", "sonarqube");
+  }
+
   @Test
   public void selectUserByLogin_not_found() {
     db.users().insertUser(user -> user.setLogin("user"));
index c963b15d36eb601681497d28a9fb0794b68a1aaf..4710e3f9d75fd520f458b1921adfa0c3bb81d2b2 100644 (file)
@@ -42,6 +42,7 @@ public class TelemetryData {
   private final Map<String, Long> projectCountByLanguage;
   private final Map<String, Long> almIntegrationCountByAlm;
   private final Map<String, Long> nclocByLanguage;
+  private final List<String> externalAuthenticationProviders;
   private final EditionProvider.Edition edition;
   private final String licenseType;
   private final Long installationDate;
@@ -71,6 +72,7 @@ public class TelemetryData {
     hasUnanalyzedC = builder.hasUnanalyzedC;
     hasUnanalyzedCpp = builder.hasUnanalyzedCpp;
     customSecurityConfigs = builder.customSecurityConfigs == null ? emptyList() : builder.customSecurityConfigs;
+    externalAuthenticationProviders = builder.externalAuthenticationProviders;
   }
 
   public String getServerId() {
@@ -149,6 +151,10 @@ public class TelemetryData {
     return customSecurityConfigs;
   }
 
+  public List<String> getExternalAuthenticationProviders() {
+    return externalAuthenticationProviders;
+  }
+
   static Builder builder() {
     return new Builder();
   }
@@ -171,11 +177,17 @@ public class TelemetryData {
     private Boolean hasUnanalyzedC;
     private Boolean hasUnanalyzedCpp;
     private List<String> customSecurityConfigs;
+    private List<String> externalAuthenticationProviders;
 
     private Builder() {
       // enforce static factory method
     }
 
+    Builder setExternalAuthenticationProviders(List<String> providers) {
+      this.externalAuthenticationProviders = providers;
+      return this;
+    }
+
     Builder setServerId(String serverId) {
       this.serverId = serverId;
       return this;
@@ -270,6 +282,7 @@ public class TelemetryData {
       requireNonNull(ncloc);
       requireNonNull(database);
       requireNonNull(usingBranches);
+      requireNonNull(externalAuthenticationProviders);
 
       return new TelemetryData(this);
     }
index f29efbe3e69cbfe5db09b6e9ae0a7a2966f5965c..17366c51ebedcb64683ff01eb1d5f7017a3aa6d7 100644 (file)
@@ -87,6 +87,12 @@ public class TelemetryDataJsonWriter {
 
     statistics.hasUnanalyzedC().ifPresent(hasUnanalyzedC -> json.prop("hasUnanalyzedC", hasUnanalyzedC));
     statistics.hasUnanalyzedCpp().ifPresent(hasUnanalyzedCpp -> json.prop("hasUnanalyzedCpp", hasUnanalyzedCpp));
+
+    json.name("externalAuthProviders");
+    json.beginArray();
+    statistics.getExternalAuthenticationProviders().forEach(json::value);
+    json.endArray();
+
     if (statistics.getInstallationDate() != null) {
       json.prop("installationDate", statistics.getInstallationDate());
     }
index dc8443f3744c790532df8168a47d0f6a4a3b0b2a..141e4b5719876f66f5df21bccb9343a6caa5a29b 100644 (file)
@@ -56,6 +56,7 @@ public class TelemetryDataJsonWriterTest {
       .setNclocByLanguage(Collections.emptyMap())
       .build())
     .setNcloc(42L)
+    .setExternalAuthenticationProviders(Arrays.asList("github", "gitlab"))
     .setDatabase(new TelemetryData.Database("H2", "11"))
     .setUsingBranches(true);
 
@@ -84,6 +85,15 @@ public class TelemetryDataJsonWriterTest {
     assertThat(json).doesNotContain("edition");
   }
 
+  @Test
+  public void write_external_auth_providers() {
+    TelemetryData data = SOME_TELEMETRY_DATA.build();
+
+    String json = writeTelemetryData(data);
+
+    assertJson(json).isSimilarTo("{ \"externalAuthProviders\": [ \"github\", \"gitlab\" ] }");
+  }
+
   @Test
   @UseDataProvider("allEditions")
   public void writes_edition_if_non_null(EditionProvider.Edition edition) {
index c0c518b4e33347ec755e42153e0a5f93fe6ccaeb..266a6db942a468e5df4f7ac53f35c027382f7f31 100644 (file)
@@ -136,6 +136,8 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader {
         });
 
       data.setAlmIntegrationCountByAlm(countAlmUsage(dbSession));
+      data.setExternalAuthenticationProviders(dbClient.userDao().selectExternalIdentityProviders(dbSession));
+
     }
 
     setSecurityCustomConfigIfPresent(data);
index fe3cb49a125377d8dcc13e48182a629d4f79e268..f3096ecfe32b38d70a9bfb4c57514448b9330673 100644 (file)
@@ -74,7 +74,7 @@ public class ClusterSystemInfoWriterTest {
       + "\"Search Nodes\":[{\"Name\":\"searchNodes\",\"\":{\"name\":\"searchNodes\"}}],"
       + "\"Statistics\":{\"id\":\"\",\"version\":\"\",\"database\":{\"name\":\"\",\"version\":\"\"},\"plugins\":[],"
       + "\"userCount\":0,\"projectCount\":0,\"usingBranches\":false,\"ncloc\":0,\"projectCountByLanguage\":[]," +
-      "\"nclocByLanguage\":[],\"almIntegrationCount\":[],\"installationDate\":0,\"installationVersion\":\"\",\"docker\":false}}");
+      "\"nclocByLanguage\":[],\"almIntegrationCount\":[],\"externalAuthProviders\":[],\"installationDate\":0,\"installationVersion\":\"\",\"docker\":false}}");
   }
 
   private static NodeInfo createNodeInfo(String name) {
index 20f0badf332670212afbdb3c35b26c65c4570c73..a7e99d4f40267f24b643e09b877e29ae5a32e37f 100644 (file)
@@ -81,7 +81,8 @@ public class StandaloneSystemInfoWriterTest {
     // response does not contain empty "Section Three"
     assertThat(writer.toString()).isEqualTo("{\"Health\":\"GREEN\",\"Health Causes\":[],\"Section One\":{\"foo\":\"bar\"},\"Section Two\":{\"one\":1,\"two\":2}," +
       "\"Statistics\":{\"id\":\"\",\"version\":\"\",\"database\":{\"name\":\"\",\"version\":\"\"},\"plugins\":[],\"userCount\":0,\"projectCount\":0,\"usingBranches\":false," +
-      "\"ncloc\":0,\"projectCountByLanguage\":[],\"nclocByLanguage\":[],\"almIntegrationCount\":[],\"installationDate\":0,\"installationVersion\":\"\",\"docker\":false}}");
+      "\"ncloc\":0,\"projectCountByLanguage\":[],\"nclocByLanguage\":[],\"almIntegrationCount\":[],\"externalAuthProviders\":[],\"installationDate\":0," +
+      "\"installationVersion\":\"\",\"docker\":false}}");
   }
 
   private void logInAsSystemAdministrator() {
index ad8c5cea40c11b8a6ffea91e5a08ca9fbcb033aa..423c00cee027e5276326f939f726636b6fbe4f48 100644 (file)
@@ -71,6 +71,7 @@ public class TelemetryDaemonTest {
       .setNclocByLanguage(Collections.emptyMap())
       .build())
     .setNcloc(42L)
+    .setExternalAuthenticationProviders(Collections.emptyList())
     .setDatabase(new TelemetryData.Database("H2", "11"))
     .setUsingBranches(true)
     .build();
index 981839617a3add77c51ec010dcc77f2409276c19..dd78c60e233d18943b8eb4abceb4f4e1c77dc008 100644 (file)
@@ -98,8 +98,8 @@ public class TelemetryDataLoaderImplTest {
     when(editionProvider.get()).thenReturn(Optional.of(DEVELOPER));
 
     int userCount = 3;
-    IntStream.range(0, userCount).forEach(i -> db.users().insertUser());
-    db.users().insertUser(u -> u.setActive(false));
+    IntStream.range(0, userCount).forEach(i -> db.users().insertUser(u -> u.setExternalIdentityProvider("provider" + i)));
+    db.users().insertUser(u -> u.setActive(false).setExternalIdentityProvider("provider0"));
     userIndexer.indexAll();
 
     MetricDto lines = db.measures().insertMetric(m -> m.setKey(LINES_KEY));
@@ -155,6 +155,7 @@ public class TelemetryDataLoaderImplTest {
       .containsEntry("gitlab_cloud", 1L)
       .containsEntry("github_cloud", 1L)
       .containsEntry("github_server", 1L);
+    assertThat(data.getExternalAuthenticationProviders()).containsExactlyInAnyOrder("provider0", "provider1", "provider2");
   }
 
   private void assertDatabaseMetadata(TelemetryData.Database database) {