Search in sources :

Example 31 with ValidationResult

use of org.graylog2.plugin.database.validators.ValidationResult in project graylog2-server by Graylog2.

the class EntitySharesServiceTest method validateOwnerless.

@DisplayName("Validates we can modify ownerless entitites")
@Test
void validateOwnerless() {
    final GRN entity = grnRegistry.newGRN(GRNTypes.DASHBOARD, "54e3deadbeefdeadbeefaffe");
    final GRN horst = grnRegistry.newGRN(GRNTypes.USER, "horst");
    final EntityShareRequest shareRequest = EntityShareRequest.create(ImmutableMap.of(horst, Capability.MANAGE));
    final User user = createMockUser("hans");
    final Subject subject = mock(Subject.class);
    final EntityShareResponse entityShareResponse = entitySharesService.prepareShare(entity, shareRequest, user, subject);
    assertThat(entityShareResponse.validationResult()).satisfies(validationResult -> {
        assertThat(validationResult.failed()).isFalse();
        assertThat(validationResult.getErrors()).isEmpty();
    });
}
Also used : GRN(org.graylog.grn.GRN) User(org.graylog2.plugin.database.users.User) Subject(org.apache.shiro.subject.Subject) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 32 with ValidationResult

use of org.graylog2.plugin.database.validators.ValidationResult in project graylog2-server by Graylog2.

the class EntitySharesServiceTest method validateLastOwnerCannotBeRemovedByChangingCapability.

@DisplayName("Validates we cannot remove the last owner by changing the own capability")
@Test
void validateLastOwnerCannotBeRemovedByChangingCapability() {
    final GRN entity = grnRegistry.newGRN(GRNTypes.EVENT_DEFINITION, "54e3deadbeefdeadbeefaffe");
    final GRN bob = grnRegistry.newGRN(GRNTypes.USER, "bob");
    final EntityShareRequest shareRequest = EntityShareRequest.create(ImmutableMap.of(bob, Capability.VIEW));
    final User user = createMockUser("requestingUser");
    when(granteeService.getAvailableGrantees(user)).thenReturn(ImmutableSet.of(Grantee.createUser(bob, "bob")));
    final Subject subject = mock(Subject.class);
    final EntityShareResponse entityShareResponse = entitySharesService.prepareShare(entity, shareRequest, user, subject);
    assertThat(entityShareResponse.validationResult()).satisfies(validationResult -> {
        assertThat(validationResult.failed()).isTrue();
        assertThat(validationResult.getErrors()).isNotEmpty();
        assertThat(validationResult.getErrors().get(EntityShareRequest.SELECTED_GRANTEE_CAPABILITIES)).contains("Removing the following owners <[grn::::user:bob]> will leave the entity ownerless.");
    });
}
Also used : GRN(org.graylog.grn.GRN) User(org.graylog2.plugin.database.users.User) Subject(org.apache.shiro.subject.Subject) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 33 with ValidationResult

use of org.graylog2.plugin.database.validators.ValidationResult in project graylog2-server by Graylog2.

the class EntitySharesServiceTest method noValidationOnEmptyRequest.

@DisplayName("Don't run validation on initial empty request")
@Test
void noValidationOnEmptyRequest() {
    final GRN entity = grnRegistry.newGRN(GRNTypes.DASHBOARD, "54e3deadbeefdeadbeefaffe");
    final EntityShareRequest shareRequest = EntityShareRequest.create(null);
    final User user = createMockUser("hans");
    final Subject subject = mock(Subject.class);
    final EntityShareResponse entityShareResponse = entitySharesService.prepareShare(entity, shareRequest, user, subject);
    assertThat(entityShareResponse.validationResult()).satisfies(validationResult -> {
        assertThat(validationResult.failed()).isFalse();
        assertThat(validationResult.getErrors()).isEmpty();
    });
}
Also used : GRN(org.graylog.grn.GRN) User(org.graylog2.plugin.database.users.User) Subject(org.apache.shiro.subject.Subject) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 34 with ValidationResult

use of org.graylog2.plugin.database.validators.ValidationResult in project graylog2-server by Graylog2.

the class CollectorResource method updateCollector.

@PUT
@Path("/{id}")
@RequiresPermissions(SidecarRestPermissions.COLLECTORS_UPDATE)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Update a collector")
@AuditEvent(type = SidecarAuditEventTypes.COLLECTOR_UPDATE)
public Response updateCollector(@ApiParam(name = "id", required = true) @PathParam("id") String id, @ApiParam(name = "JSON body", required = true) @Valid @NotNull Collector request) throws BadRequestException {
    Collector collector = collectorService.fromRequest(id, request);
    final ValidationResult validationResult = validate(collector);
    if (validationResult.failed()) {
        return Response.status(Response.Status.BAD_REQUEST).entity(validationResult).build();
    }
    etagService.invalidateAll();
    return Response.ok().entity(collectorService.save(collector)).build();
}
Also used : Collector(org.graylog.plugins.sidecar.rest.models.Collector) ValidationResult(org.graylog2.plugin.rest.ValidationResult) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT)

Example 35 with ValidationResult

use of org.graylog2.plugin.database.validators.ValidationResult in project graylog2-server by Graylog2.

the class ConfigurationVariableResource method updateConfigurationVariable.

@PUT
@Path("/{id}")
@RequiresPermissions(SidecarRestPermissions.CONFIGURATIONS_UPDATE)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Update a configuration variable")
@AuditEvent(type = SidecarAuditEventTypes.CONFIGURATION_VARIABLE_UPDATE)
public Response updateConfigurationVariable(@ApiParam(name = "id", required = true) @PathParam("id") String id, @ApiParam(name = "JSON body", required = true) @Valid @NotNull ConfigurationVariable request) {
    final ConfigurationVariable previousConfigurationVariable = findVariableOrFail(id);
    ValidationResult validationResult = validateConfigurationVariableHelper(request);
    if (validationResult.failed()) {
        return Response.status(Response.Status.BAD_REQUEST).entity(validationResult).build();
    }
    if (!previousConfigurationVariable.name().equals(request.name())) {
        configurationService.replaceVariableNames(previousConfigurationVariable.fullName(), request.fullName());
    }
    final ConfigurationVariable updatedConfigurationVariable = persistConfigurationVariable(id, request);
    etagService.invalidateAll();
    return Response.ok().entity(updatedConfigurationVariable).build();
}
Also used : ConfigurationVariable(org.graylog.plugins.sidecar.rest.models.ConfigurationVariable) ValidationResult(org.graylog2.plugin.rest.ValidationResult) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent) PUT(javax.ws.rs.PUT)

Aggregations

ValidationResult (org.graylog2.plugin.rest.ValidationResult)43 Test (org.junit.Test)24 ApiOperation (io.swagger.annotations.ApiOperation)16 NoAuditEvent (org.graylog2.audit.jersey.NoAuditEvent)16 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)14 Path (javax.ws.rs.Path)12 AuditEvent (org.graylog2.audit.jersey.AuditEvent)12 POST (javax.ws.rs.POST)11 List (java.util.List)10 Subject (org.apache.shiro.subject.Subject)8 GRN (org.graylog.grn.GRN)8 User (org.graylog2.plugin.database.users.User)8 ArrayList (java.util.ArrayList)7 Produces (javax.ws.rs.Produces)7 ValidationException (org.graylog2.plugin.database.ValidationException)6 DisplayName (org.junit.jupiter.api.DisplayName)6 Test (org.junit.jupiter.api.Test)6 BasicDBObject (com.mongodb.BasicDBObject)5 Collectors (java.util.stream.Collectors)5 NotFoundException (javax.ws.rs.NotFoundException)5