use of org.sonar.alm.client.github.config.GithubAppConfiguration in project sonarqube by SonarSource.
the class GithubAppSecurityImplTest method getApplicationJWTToken_returns_token_if_app_config_and_private_key_are_valid.
@Test
public void getApplicationJWTToken_returns_token_if_app_config_and_private_key_are_valid() {
GithubAppConfiguration githubAppConfiguration = createAppConfiguration(true);
assertThat(underTest.createAppToken(githubAppConfiguration.getId(), githubAppConfiguration.getPrivateKey())).isNotNull();
}
use of org.sonar.alm.client.github.config.GithubAppConfiguration in project sonarqube by SonarSource.
the class GithubApplicationClientImplTest method checkApiEndpoint.
@Test
@UseDataProvider("validApiEndpoints")
public void checkApiEndpoint(String url) {
GithubAppConfiguration configuration = new GithubAppConfiguration(1L, "", url);
assertThatCode(() -> underTest.checkApiEndpoint(configuration)).isNull();
}
use of org.sonar.alm.client.github.config.GithubAppConfiguration in project sonarqube by SonarSource.
the class GithubApplicationClientImplTest method checkApiEndpoint_Invalid.
@Test
@UseDataProvider("invalidApiEndpoints")
public void checkApiEndpoint_Invalid(String url, String expectedMessage) {
GithubAppConfiguration configuration = new GithubAppConfiguration(1L, "", url);
assertThatThrownBy(() -> underTest.checkApiEndpoint(configuration)).isInstanceOf(IllegalArgumentException.class).hasMessage(expectedMessage);
}
use of org.sonar.alm.client.github.config.GithubAppConfiguration in project sonarqube by SonarSource.
the class GithubApplicationClientImpl method checkAppPermissions.
@Override
public void checkAppPermissions(GithubAppConfiguration githubAppConfiguration) {
AppToken appToken = appSecurity.createAppToken(githubAppConfiguration.getId(), githubAppConfiguration.getPrivateKey());
Map<String, String> permissions = new HashMap<>();
permissions.put("checks", WRITE_PERMISSION_NAME);
permissions.put("pull_requests", WRITE_PERMISSION_NAME);
permissions.put("statuses", READ_PERMISSION_NAME);
permissions.put("metadata", READ_PERMISSION_NAME);
String endPoint = "/app";
GetResponse response;
try {
response = appHttpClient.get(githubAppConfiguration.getApiEndpoint(), appToken, endPoint);
} catch (IOException e) {
LOG.warn(FAILED_TO_REQUEST_BEGIN_MSG + githubAppConfiguration.getApiEndpoint() + endPoint, e);
throw new IllegalArgumentException("Failed to validate configuration, check URL and Private Key");
}
if (response.getCode() == HTTP_OK) {
Map<String, String> perms = handleResponse(response, endPoint, GsonApp.class).map(GsonApp::getPermissions).orElseThrow(() -> new IllegalArgumentException("Failed to get app permissions, unexpected response body"));
List<String> missingPermissions = permissions.entrySet().stream().filter(permission -> !Objects.equals(permission.getValue(), perms.get(permission.getKey()))).map(Map.Entry::getKey).collect(toList());
if (!missingPermissions.isEmpty()) {
String message = missingPermissions.stream().map(perm -> perm + " is '" + perms.get(perm) + "', should be '" + permissions.get(perm) + "'").collect(Collectors.joining(", "));
throw new IllegalArgumentException("Missing permissions; permission granted on " + message);
}
} else if (response.getCode() == HTTP_UNAUTHORIZED || response.getCode() == HTTP_FORBIDDEN) {
throw new IllegalArgumentException("Authentication failed, verify the Client Id, Client Secret and Private Key fields");
} else {
throw new IllegalArgumentException("Failed to check permissions with Github, check the configuration");
}
}
use of org.sonar.alm.client.github.config.GithubAppConfiguration in project sonarqube by SonarSource.
the class GithubGlobalSettingsValidator method validate.
public GithubAppConfiguration validate(AlmSettingDto settings) {
long appId;
try {
appId = Long.parseLong(Optional.ofNullable(settings.getAppId()).orElseThrow(() -> new IllegalArgumentException("Missing appId")));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid appId; " + e.getMessage());
}
if (isBlank(settings.getClientId())) {
throw new IllegalArgumentException("Missing Client Id");
}
if (isBlank(settings.getDecryptedClientSecret(encryption))) {
throw new IllegalArgumentException("Missing Client Secret");
}
GithubAppConfiguration configuration = new GithubAppConfiguration(appId, settings.getDecryptedPrivateKey(encryption), settings.getUrl());
githubApplicationClient.checkApiEndpoint(configuration);
githubApplicationClient.checkAppPermissions(configuration);
return configuration;
}
Aggregations