use of org.sonar.server.exceptions.ForbiddenException in project sonarqube by SonarSource.
the class SafeModeMonitoringMetricAction method handle.
@Override
public void handle(Request request, Response response) throws Exception {
if (!systemPasscode.isValid(request) && !isSystemAdmin() && !bearerPasscode.isValid(request)) {
throw new ForbiddenException("Insufficient privileges");
}
String requestContentType = request.getHeaders().get("accept");
String contentType = TextFormat.chooseContentType(requestContentType);
response.setHeader(HttpHeaders.CONTENT_TYPE, contentType);
response.stream().setStatus(200);
try (Writer writer = new OutputStreamWriter(response.stream().output(), UTF_8)) {
TextFormat.writeFormat(contentType, writer, CollectorRegistry.defaultRegistry.metricFamilySamples());
writer.flush();
}
}
use of org.sonar.server.exceptions.ForbiddenException in project sonarqube by SonarSource.
the class SonarLintClientPermissionsValidator method validateUserCanReceivePushEventForProjects.
public void validateUserCanReceivePushEventForProjects(String userUUID, Set<String> projectKeys) {
UserDto userDto;
try (DbSession dbSession = dbClient.openSession(false)) {
userDto = dbClient.userDao().selectByUuid(dbSession, userUUID);
}
if (userDto == null) {
throw new ForbiddenException("User does not exist");
}
validateUserCanReceivePushEventForProjects(userSessionFactory.create(userDto), projectKeys);
}
use of org.sonar.server.exceptions.ForbiddenException in project sonarqube by SonarSource.
the class SonarLintClientsRegistryTest method listen_givenUserNotPermittedToReceiveEvent_closeConnection.
@Test
public void listen_givenUserNotPermittedToReceiveEvent_closeConnection() {
RuleChange javaRuleChange = createRuleChange();
RuleChange[] activatedRules = {};
RuleChange[] deactivatedRules = { javaRuleChange };
RuleSetChangedEvent ruleSetChangedEvent = new RuleSetChangedEvent(exampleKeys.toArray(String[]::new), activatedRules, deactivatedRules);
SonarLintClient sonarLintClient = createSampleSLClient();
underTest.registerClient(sonarLintClient);
doThrow(new ForbiddenException("Access forbidden")).when(permissionsValidator).validateUserCanReceivePushEventForProjects(anyString(), anySet());
underTest.listen(ruleSetChangedEvent);
verify(sonarLintClient).close();
}
use of org.sonar.server.exceptions.ForbiddenException in project sonarqube by SonarSource.
the class MeasureAction method handle.
@Override
public void handle(Request request, Response response) throws Exception {
response.setHeader("Cache-Control", "no-cache");
response.stream().setMediaType(SVG);
String metricKey = request.mandatoryParam(PARAM_METRIC);
try (DbSession dbSession = dbClient.openSession(false)) {
support.validateToken(request);
BranchDto branch = support.getBranch(dbSession, request);
MetricDto metric = dbClient.metricDao().selectByKey(dbSession, metricKey);
checkState(metric != null && metric.isEnabled(), "Metric '%s' hasn't been found", metricKey);
LiveMeasureDto measure = getMeasure(dbSession, branch, metricKey);
String result = generateSvg(metric, measure);
String eTag = getETag(result);
Optional<String> requestedETag = request.header("If-None-Match");
if (requestedETag.filter(eTag::equals).isPresent()) {
response.stream().setStatus(304);
return;
}
response.setHeader("ETag", eTag);
write(result, response.stream().output(), UTF_8);
} catch (ProjectBadgesException | ForbiddenException | NotFoundException e) {
// There is an issue, so do not return any ETag but make this response expire now
SimpleDateFormat sdf = new SimpleDateFormat(RFC1123_DATE, Locale.US);
response.setHeader("Expires", sdf.format(new Date()));
write(svgGenerator.generateError(e.getMessage()), response.stream().output(), UTF_8);
}
}
use of org.sonar.server.exceptions.ForbiddenException in project sonarqube by SonarSource.
the class SonarLintClientsRegistry method broadcastMessage.
public void broadcastMessage(RuleSetChangedEvent event, Predicate<SonarLintClient> filter) {
clients.stream().filter(filter).forEach(c -> {
Set<String> projectKeysInterestingForClient = new HashSet<>(c.getClientProjectKeys());
projectKeysInterestingForClient.retainAll(Set.of(event.getProjects()));
try {
sonarLintClientPermissionsValidator.validateUserCanReceivePushEventForProjects(c.getUserUuid(), projectKeysInterestingForClient);
RuleSetChangedEvent personalizedEvent = new RuleSetChangedEvent(projectKeysInterestingForClient.toArray(String[]::new), event.getActivatedRules(), event.getDeactivatedRules());
String message = getMessage(personalizedEvent);
c.writeAndFlush(message);
} catch (ForbiddenException forbiddenException) {
LOG.debug("Client is no longer authenticated: " + forbiddenException.getMessage());
unregisterClient(c);
} catch (IllegalStateException | IOException e) {
LOG.error("Unable to send message to a client: " + e.getMessage());
unregisterClient(c);
}
});
}
Aggregations