From: Antoine Vigneau Date: Thu, 8 Jun 2023 10:10:15 +0000 (+0200) Subject: SONAR-19337 Support invalid GitHub Private Key in the config check X-Git-Tag: 10.1.0.73491~130 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=514e75c10add5b1bbe95387dd6b03db5f6a8aa93;p=sonarqube.git SONAR-19337 Support invalid GitHub Private Key in the config check --- diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/config/GithubProvisioningConfigValidator.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/config/GithubProvisioningConfigValidator.java index 3e6bb8f001f..efa137121c2 100644 --- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/config/GithubProvisioningConfigValidator.java +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/config/GithubProvisioningConfigValidator.java @@ -39,11 +39,8 @@ import static org.sonar.alm.client.github.config.ConfigCheckResult.InstallationS @ComputeEngineSide public class GithubProvisioningConfigValidator { - private static final ConfigStatus APP_NOT_FOUND_STATUS = ConfigStatus.failed("Github App not found"); private static final String MEMBERS_PERMISSION = "Organization permissions -> Members"; - private static final String EMAILS_PERMISSION = "Account permissions -> Email addresses"; - private static final ConfigStatus INVALID_APP_CONFIG_STATUS = ConfigStatus.failed("The GitHub App configuration is not complete."); private static final ConfigStatus INVALID_APP_ID_STATUS = ConfigStatus.failed("GitHub App ID must be a number."); private static final ConfigStatus SUSPENDED_INSTALLATION_STATUS = ConfigStatus.failed("Installation suspended"); @@ -91,6 +88,9 @@ public class GithubProvisioningConfigValidator { } catch (HttpException e) { return failedApplicationStatus( ConfigStatus.failed("Error response from GitHub: " + e.getMessage())); + } catch (IllegalArgumentException e) { + return failedApplicationStatus( + ConfigStatus.failed(e.getMessage())); } } diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/security/GithubAppSecurityImpl.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/security/GithubAppSecurityImpl.java index 26430123424..07132847331 100644 --- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/security/GithubAppSecurityImpl.java +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/security/GithubAppSecurityImpl.java @@ -97,7 +97,7 @@ public class GithubAppSecurityImpl implements GithubAppSecurity { } }); } catch (Exception e) { - throw new IllegalArgumentException("Invalid Github Application private key", e); + throw new IllegalArgumentException("The Github App private key is not valid", e); } finally { Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME); } diff --git a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/github/config/GithubProvisioningConfigValidatorTest.java b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/github/config/GithubProvisioningConfigValidatorTest.java index e743102c734..6adc3467973 100644 --- a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/github/config/GithubProvisioningConfigValidatorTest.java +++ b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/github/config/GithubProvisioningConfigValidatorTest.java @@ -48,6 +48,7 @@ public class GithubProvisioningConfigValidatorTest { private static final String SUCCESS_STATUS = "SUCCESS"; private static final String GITHUB_CALL_FAILED = "Error response from GitHub: GitHub call failed."; + private static final String APP_FETCHING_FAILED = "Exception while fetching the App."; private static final String INVALID_APP_ID_STATUS = "GitHub App ID must be a number."; private static final String INCOMPLETE_APP_CONFIG_STATUS = "The GitHub App configuration is not complete."; private static final String MISSING_EMAIL_PERMISSION = "Missing permissions: Account permissions -> Email addresses"; @@ -79,6 +80,7 @@ public class GithubProvisioningConfigValidatorTest { assertThat(checkResult.application().jit()).isEqualTo(ConfigStatus.failed(INVALID_APP_ID_STATUS)); assertThat(checkResult.installations()).isEmpty(); } + @Test public void checkConfig_whenAppIdNotValid_shouldReturnFailedAppCheck() { when(gitHubSettings.appId()).thenReturn("not a number"); @@ -102,7 +104,7 @@ public class GithubProvisioningConfigValidatorTest { } @Test - public void checkConfig_whenErrorWhileFetchingTheApp_shouldReturnFailedAppCheck() { + public void checkConfig_whenHttpExceptionWhileFetchingTheApp_shouldReturnFailedAppCheck() { mockGithubConfiguration(); ArgumentCaptor appConfigurationCaptor = ArgumentCaptor.forClass(GithubAppConfiguration.class); @@ -118,6 +120,23 @@ public class GithubProvisioningConfigValidatorTest { assertThat(checkResult.installations()).isEmpty(); } + @Test + public void checkConfig_whenIllegalArgumentExceptionWhileFetchingTheApp_shouldReturnFailedAppCheck() { + mockGithubConfiguration(); + ArgumentCaptor appConfigurationCaptor = ArgumentCaptor.forClass(GithubAppConfiguration.class); + + IllegalArgumentException illegalArgumentException = mock(IllegalArgumentException.class); + when(illegalArgumentException.getMessage()).thenReturn("Exception while fetching the App."); + + when(githubClient.getApp(appConfigurationCaptor.capture())).thenThrow(illegalArgumentException); + + ConfigCheckResult checkResult = configValidator.checkConfig(); + + assertThat(checkResult.application().autoProvisioning()).isEqualTo(ConfigStatus.failed(APP_FETCHING_FAILED)); + assertThat(checkResult.application().jit()).isEqualTo(ConfigStatus.failed(APP_FETCHING_FAILED)); + assertThat(checkResult.installations()).isEmpty(); + } + @Test public void checkConfig_whenAppDoesntHaveEmailsPermissions_shouldReturnFailedAppJitCheck() { mockGithubConfiguration();