Search in sources :

Example 11 with GRN

use of org.graylog.grn.GRN in project graylog2-server by Graylog2.

the class DBGrantServiceTest method getForGranteeWithCapability.

@Test
@MongoDBFixtures("grants.json")
public void getForGranteeWithCapability() {
    final GRN jane = grnRegistry.newGRN("user", "jane");
    final GRN john = grnRegistry.newGRN("user", "john");
    assertThat(dbService.getForGranteeWithCapability(jane, Capability.MANAGE)).hasSize(1);
    assertThat(dbService.getForGranteeWithCapability(jane, Capability.OWN)).hasSize(1);
    assertThat(dbService.getForGranteeWithCapability(john, Capability.VIEW)).hasSize(1);
}
Also used : GRN(org.graylog.grn.GRN) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 12 with GRN

use of org.graylog.grn.GRN in project graylog2-server by Graylog2.

the class DBGrantServiceTest method ensure.

@Test
@MongoDBFixtures("grants.json")
public void ensure() {
    final GRN jane = grnRegistry.parse("grn::::user:jane");
    final GRN stream1 = grnRegistry.parse("grn::::stream:54e3deadbeefdeadbeef0000");
    final GRN newStream = grnRegistry.parse("grn::::stream:54e3deadbeefdeadbeef0888");
    // Matches existing grant. Returns original
    final GrantDTO stream1Grant = dbService.getForTargetAndGrantee(stream1, jane).get(0);
    GrantDTO result = dbService.ensure(jane, Capability.VIEW, stream1, "admin");
    assertThat(result).isEqualTo(stream1Grant);
    // Updates to a higher capability
    result = dbService.ensure(jane, Capability.MANAGE, stream1, "admin");
    assertThat(result.capability()).isEqualTo(Capability.MANAGE);
    // Don't downgrade to a lower capability
    result = dbService.ensure(jane, Capability.VIEW, stream1, "admin");
    assertThat(result.capability()).isEqualTo(Capability.MANAGE);
    // Create a new grant
    assertThat(dbService.ensure(jane, Capability.MANAGE, newStream, "admin")).isNotNull();
    assertThat(dbService.getForTarget(newStream)).satisfies(grantDTOS -> {
        assertThat(grantDTOS.size()).isEqualTo(1);
        assertThat(grantDTOS.get(0).grantee()).isEqualTo(jane);
        assertThat(grantDTOS.get(0).capability()).isEqualTo(Capability.MANAGE);
        assertThat(grantDTOS.get(0).target()).isEqualTo(newStream);
    });
}
Also used : GRN(org.graylog.grn.GRN) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 13 with GRN

use of org.graylog.grn.GRN in project graylog2-server by Graylog2.

the class DBGrantServiceTest method createWithGrantDTOAndUserObject.

@Test
public void createWithGrantDTOAndUserObject() {
    final ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
    final GRN grantee = GRNTypes.USER.toGRN("jane");
    final GRN target = GRNTypes.DASHBOARD.toGRN("54e3deadbeefdeadbeef0000");
    final User user = mock(User.class);
    when(user.getName()).thenReturn("john");
    final GrantDTO grantDTO = GrantDTO.of(grantee, Capability.OWN, target).toBuilder().createdAt(now.minusHours(1)).updatedAt(now.minusHours(1)).build();
    final GrantDTO grant = dbService.create(grantDTO, user);
    assertThat(grant.id()).isNotBlank();
    assertThat(grant.grantee()).isEqualTo(grantee);
    assertThat(grant.capability()).isEqualTo(Capability.OWN);
    assertThat(grant.target()).isEqualTo(target);
    assertThat(grant.createdBy()).isEqualTo("john");
    assertThat(grant.createdAt()).isAfter(grantDTO.createdAt());
    assertThat(grant.updatedBy()).isEqualTo("john");
    assertThat(grant.updatedAt()).isAfter(grantDTO.updatedAt());
}
Also used : GRN(org.graylog.grn.GRN) User(org.graylog2.plugin.database.users.User) ZonedDateTime(java.time.ZonedDateTime) Test(org.junit.Test)

Example 14 with GRN

use of org.graylog.grn.GRN in project graylog2-server by Graylog2.

the class DBGrantServiceTest method getForGrantee.

@Test
@MongoDBFixtures("grants.json")
public void getForGrantee() {
    final GRN jane = grnRegistry.newGRN("user", "jane");
    final GRN john = grnRegistry.newGRN("user", "john");
    assertThat(dbService.getForGrantee(jane)).hasSize(3);
    assertThat(dbService.getForGrantee(john)).hasSize(2);
}
Also used : GRN(org.graylog.grn.GRN) MongoDBFixtures(org.graylog.testing.mongodb.MongoDBFixtures) Test(org.junit.Test)

Example 15 with GRN

use of org.graylog.grn.GRN in project graylog2-server by Graylog2.

the class EntitySharesResource method updateEntityShares.

@POST
@ApiOperation(value = "Create / update shares for an entity or collection")
@Path("entities/{entityGRN}")
@NoAuditEvent("Audit events are created within EntitySharesService")
public Response updateEntityShares(@ApiParam(name = "entityGRN", required = true) @PathParam("entityGRN") @NotBlank String entityGRN, @ApiParam(name = "JSON Body", required = true) @NotNull @Valid EntityShareRequest request) {
    final GRN entity = grnRegistry.parse(entityGRN);
    checkOwnership(entity);
    final EntityShareResponse entityShareResponse = entitySharesService.updateEntityShares(entity, request, requireNonNull(getCurrentUser()));
    if (entityShareResponse.validationResult().failed()) {
        return Response.status(Response.Status.BAD_REQUEST).entity(entityShareResponse).build();
    } else {
        return Response.ok(entityShareResponse).build();
    }
}
Also used : EntityShareResponse(org.graylog.security.shares.EntityShareResponse) GRN(org.graylog.grn.GRN) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Aggregations

GRN (org.graylog.grn.GRN)51 User (org.graylog2.plugin.database.users.User)19 DisplayName (org.junit.jupiter.api.DisplayName)16 Test (org.junit.jupiter.api.Test)16 Test (org.junit.Test)13 MongoDBFixtures (org.graylog.testing.mongodb.MongoDBFixtures)11 Subject (org.apache.shiro.subject.Subject)10 ImmutableSet (com.google.common.collect.ImmutableSet)7 Collectors (java.util.stream.Collectors)5 GRNRegistry (org.graylog.grn.GRNRegistry)5 Capability (org.graylog.security.Capability)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 EventBus (com.google.common.eventbus.EventBus)4 Set (java.util.Set)4 DBGrantService (org.graylog.security.DBGrantService)4 ZonedDateTime (java.time.ZonedDateTime)3 Collection (java.util.Collection)3 List (java.util.List)3 Map (java.util.Map)3 Objects (java.util.Objects)3