Search in sources :

Example 1 with ForbiddenException

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();
    }
}
Also used : ForbiddenException(org.sonar.server.exceptions.ForbiddenException) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 2 with ForbiddenException

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);
}
Also used : DbSession(org.sonar.db.DbSession) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) UserDto(org.sonar.db.user.UserDto)

Example 3 with ForbiddenException

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();
}
Also used : RuleChange(org.sonar.core.util.RuleChange) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) RuleSetChangedEvent(org.sonar.core.util.RuleSetChangedEvent) Test(org.junit.Test)

Example 4 with ForbiddenException

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);
    }
}
Also used : DbSession(org.sonar.db.DbSession) BranchDto(org.sonar.db.component.BranchDto) MetricDto(org.sonar.db.metric.MetricDto) ForbiddenException(org.sonar.server.exceptions.ForbiddenException) LiveMeasureDto(org.sonar.db.measure.LiveMeasureDto) NotFoundException(org.sonar.server.exceptions.NotFoundException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 5 with ForbiddenException

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);
        }
    });
}
Also used : ForbiddenException(org.sonar.server.exceptions.ForbiddenException) IOException(java.io.IOException) RuleSetChangedEvent(org.sonar.core.util.RuleSetChangedEvent) HashSet(java.util.HashSet)

Aggregations

ForbiddenException (org.sonar.server.exceptions.ForbiddenException)6 DbSession (org.sonar.db.DbSession)3 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 RuleSetChangedEvent (org.sonar.core.util.RuleSetChangedEvent)2 BranchDto (org.sonar.db.component.BranchDto)2 NotFoundException (org.sonar.server.exceptions.NotFoundException)2 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 Level (org.sonar.api.measures.Metric.Level)1 RuleChange (org.sonar.core.util.RuleChange)1 LiveMeasureDto (org.sonar.db.measure.LiveMeasureDto)1 MetricDto (org.sonar.db.metric.MetricDto)1 UserDto (org.sonar.db.user.UserDto)1