aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao
diff options
context:
space:
mode:
authorAurelien Poscia <aurelien.poscia@sonarsource.com>2022-09-05 14:25:16 +0200
committersonartech <sonartech@sonarsource.com>2022-09-13 20:03:08 +0000
commit32fbddcc7a8607e56d2a1e0ab4831cd640e1e444 (patch)
tree53299e38d4ec7f532673828aa434768d1ba4c487 /server/sonar-db-dao
parent38ba648c3606e27dc0968bca56041fe7714f5662 (diff)
downloadsonarqube-32fbddcc7a8607e56d2a1e0ab4831cd640e1e444.tar.gz
sonarqube-32fbddcc7a8607e56d2a1e0ab4831cd640e1e444.zip
SONAR-17266 Implement authentification for GitHub webhook requests
Diffstat (limited to 'server/sonar-db-dao')
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDao.java7
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingMapper.java1
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/alm/setting/AlmSettingMapper.xml1
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/AlmSettingDaoTest.java62
-rw-r--r--server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java13
5 files changed, 60 insertions, 24 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDao.java
index 7a2ccb744fe..a382b9d3ed6 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDao.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingDao.java
@@ -64,6 +64,13 @@ public class AlmSettingDao implements Dao {
return Optional.ofNullable(getMapper(dbSession).selectByKey(key));
}
+ public Optional<AlmSettingDto> selectByAlmAndAppId(DbSession dbSession, ALM alm, String appId) {
+ return selectByAlm(dbSession, alm)
+ .stream()
+ .filter(almSettingDto -> appId.equals(almSettingDto.getAppId()))
+ .findAny();
+ }
+
public List<AlmSettingDto> selectByAlm(DbSession dbSession, ALM alm) {
return getMapper(dbSession).selectByAlm(alm.getId());
}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingMapper.java
index bb3382c61c3..8c9203cb95a 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingMapper.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/alm/setting/AlmSettingMapper.java
@@ -40,4 +40,5 @@ public interface AlmSettingMapper {
void update(@Param("dto") AlmSettingDto almSettingDto);
int deleteByKey(@Param("key") String key);
+
}
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/setting/AlmSettingMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/setting/AlmSettingMapper.xml
index 2e45e8bc34c..76eb4fffd51 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/setting/AlmSettingMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/alm/setting/AlmSettingMapper.xml
@@ -102,5 +102,4 @@
DELETE FROM alm_settings WHERE kee = #{key, jdbcType=VARCHAR}
</delete>
-
</mapper>
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/AlmSettingDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/AlmSettingDaoTest.java
index 74e3c69a238..44eee7b433f 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/AlmSettingDaoTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/alm/setting/AlmSettingDaoTest.java
@@ -19,7 +19,11 @@
*/
package org.sonar.db.alm.setting;
+import java.util.Iterator;
import java.util.List;
+import java.util.Optional;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -33,14 +37,15 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.db.alm.setting.ALM.GITHUB;
-import static org.sonar.db.almsettings.AlmSettingsTesting.newAlmSettingDtoWithEmptySecrets;
+import static org.sonar.db.alm.setting.ALM.GITLAB;
import static org.sonar.db.almsettings.AlmSettingsTesting.newGithubAlmSettingDto;
-import static org.sonar.db.almsettings.AlmSettingsTesting.newGithubAlmSettingDtoWithNonRequiredField;
public class AlmSettingDaoTest {
private static final long NOW = 1000000L;
private static final String A_UUID = "SOME_UUID";
+ private static final AlmSettingDto ALM_SETTING_WITH_WEBHOOK_SECRET = newGithubAlmSettingDto().setWebhookSecret("webhook_secret");
+
private final TestSystem2 system2 = new TestSystem2().setNow(NOW);
@Rule
public DbTester db = DbTester.create(system2);
@@ -52,22 +57,23 @@ public class AlmSettingDaoTest {
@Before
public void setUp() {
- when(uuidFactory.create()).thenReturn(A_UUID);
+ Iterator<Integer> values = Stream.iterate(0, i -> i + 1).iterator();
+ when(uuidFactory.create()).thenAnswer(answer -> A_UUID + "_" + values.next());
}
@Test
public void selectByUuid() {
- AlmSettingDto expected = newGithubAlmSettingDtoWithNonRequiredField();
+ AlmSettingDto expected = ALM_SETTING_WITH_WEBHOOK_SECRET;
underTest.insert(dbSession, expected);
- AlmSettingDto result = underTest.selectByUuid(dbSession, A_UUID).orElse(null);
+ AlmSettingDto result = underTest.selectByUuid(dbSession, expected.getUuid()).orElse(null);
assertThat(result).usingRecursiveComparison().isEqualTo(expected);
}
@Test
public void selectByUuid_shouldNotFindResult_whenUuidIsNotPresent() {
- AlmSettingDto expected = newGithubAlmSettingDtoWithNonRequiredField();
+ AlmSettingDto expected = ALM_SETTING_WITH_WEBHOOK_SECRET;
underTest.insert(dbSession, expected);
assertThat(underTest.selectByUuid(dbSession, "foo")).isNotPresent();
@@ -75,7 +81,7 @@ public class AlmSettingDaoTest {
@Test
public void selectByKey() {
- AlmSettingDto expected = newGithubAlmSettingDtoWithNonRequiredField();
+ AlmSettingDto expected = ALM_SETTING_WITH_WEBHOOK_SECRET;
underTest.insert(dbSession, expected);
AlmSettingDto result = underTest.selectByKey(dbSession, expected.getKey()).orElse(null);
@@ -85,7 +91,7 @@ public class AlmSettingDaoTest {
@Test
public void selectByKey_shouldNotFindResult_whenKeyIsNotPresent() {
- AlmSettingDto expected = newGithubAlmSettingDtoWithNonRequiredField();
+ AlmSettingDto expected = ALM_SETTING_WITH_WEBHOOK_SECRET;
underTest.insert(dbSession, expected);
assertThat(underTest.selectByKey(dbSession, "foo")).isNotPresent();
@@ -93,7 +99,7 @@ public class AlmSettingDaoTest {
@Test
public void selectByKey_withEmptySecrets() {
- AlmSettingDto expected = newAlmSettingDtoWithEmptySecrets();
+ AlmSettingDto expected = newGithubAlmSettingDto().setWebhookSecret(null);
underTest.insert(dbSession, expected);
AlmSettingDto result = underTest.selectByKey(dbSession, expected.getKey()).orElse(null);
@@ -142,7 +148,7 @@ public class AlmSettingDaoTest {
//WHEN
underTest.update(dbSession, expected, false);
//THEN
- AlmSettingDto result = underTest.selectByUuid(dbSession, A_UUID).orElse(null);
+ AlmSettingDto result = underTest.selectByUuid(dbSession, expected.getUuid()).orElse(null);
assertThat(result).usingRecursiveComparison().isEqualTo(expected);
}
@@ -156,4 +162,40 @@ public class AlmSettingDaoTest {
assertThat(underTest.selectByKey(dbSession, almSettingDto.getKey())).isNotPresent();
}
+ @Test
+ public void selectByAlmAndAppId_whenSingleMatch_returnsCorrectObject() {
+ String appId = "APP_ID";
+ AlmSettingDto expectedAlmSettingDto = db.almSettings().insertGitHubAlmSetting(almSettingDto -> almSettingDto.setAppId(appId));
+ db.almSettings().insertGitHubAlmSetting(almSettingDto -> almSettingDto.setAppId(null));
+
+ Optional<AlmSettingDto> result = underTest.selectByAlmAndAppId(dbSession, GITHUB, appId);
+
+ assertThat(result).isPresent();
+ assertThat(result.get()).usingRecursiveComparison().isEqualTo(expectedAlmSettingDto);
+ }
+
+ @Test
+ public void selectByAlmAndAppId_whenAppIdSharedWithAnotherAlm_returnsCorrectOne() {
+ String appId = "APP_ID";
+ db.almSettings().insertGitHubAlmSetting(almSettingDto -> almSettingDto.setAppId(appId));
+ AlmSettingDto gitLabAlmSettingDto = db.almSettings().insertGitlabAlmSetting(almSettingDto -> almSettingDto.setAppId(appId));
+
+ Optional<AlmSettingDto> result = underTest.selectByAlmAndAppId(dbSession, GITLAB, appId);
+
+ assertThat(result).isPresent();
+ assertThat(result.get()).usingRecursiveComparison().isEqualTo(gitLabAlmSettingDto);
+ }
+
+ @Test
+ public void selectByAlmAndAppId_withMultipleConfigurationWithSameAppId_returnsAnyAndDoesNotFail() {
+ String appId = "APP_ID";
+ IntStream.of(1, 10).forEach(i -> db.almSettings().insertGitHubAlmSetting(almSettingDto -> almSettingDto.setAppId(appId)));
+ IntStream.of(1, 5).forEach(i -> db.almSettings().insertGitHubAlmSetting(almSettingDto -> almSettingDto.setAppId(null)));
+
+ Optional<AlmSettingDto> result = underTest.selectByAlmAndAppId(dbSession, GITHUB, appId);
+
+ assertThat(result).isPresent();
+ assertThat(result.get().getAppId()).isEqualTo(appId);
+ }
+
}
diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java
index 8745acb1631..b880b05a624 100644
--- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java
+++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/almsettings/AlmSettingsTesting.java
@@ -33,10 +33,6 @@ public class AlmSettingsTesting {
}
- public static AlmSettingDto newGithubAlmSettingDtoWithNonRequiredField() {
- return newGithubAlmSettingDto().setWebhookSecret(randomAlphanumeric(160));
- }
-
public static AlmSettingDto newGithubAlmSettingDto() {
return new AlmSettingDto()
.setKey(randomAlphanumeric(200))
@@ -48,15 +44,6 @@ public class AlmSettingsTesting {
.setAlm(ALM.GITHUB);
}
- public static AlmSettingDto newAlmSettingDtoWithEmptySecrets() {
- return new AlmSettingDto()
- .setKey(randomAlphanumeric(200))
- .setUrl(randomAlphanumeric(2000))
- .setAppId(randomNumeric(8))
- .setClientId(randomNumeric(8))
- .setAlm(ALM.GITHUB);
- }
-
public static AlmSettingDto newAzureAlmSettingDto() {
return new AlmSettingDto()
.setKey(randomAlphanumeric(200))