Search in sources :

Example 1 with HugeAccess

use of com.baidu.hugegraph.auth.HugeAccess in project incubator-hugegraph by apache.

the class AccessAPI method create.

@POST
@Timed
@Status(Status.CREATED)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String create(@Context GraphManager manager, @PathParam("graph") String graph, JsonAccess jsonAccess) {
    LOG.debug("Graph [{}] create access: {}", graph, jsonAccess);
    checkCreatingBody(jsonAccess);
    HugeGraph g = graph(manager, graph);
    HugeAccess access = jsonAccess.build();
    access.id(manager.authManager().createAccess(access));
    return manager.serializer(g).writeAuthElement(access);
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeAccess(com.baidu.hugegraph.auth.HugeAccess) Status(com.baidu.hugegraph.api.filter.StatusFilter.Status) POST(jakarta.ws.rs.POST) Consumes(jakarta.ws.rs.Consumes) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed)

Example 2 with HugeAccess

use of com.baidu.hugegraph.auth.HugeAccess in project incubator-hugegraph by apache.

the class AuthTest method makeAccess.

private static HugeAccess makeAccess(Id group, Id target, HugePermission permission) {
    HugeAccess access = new HugeAccess(group, target, permission);
    access.creator("admin");
    return access;
}
Also used : HugeAccess(com.baidu.hugegraph.auth.HugeAccess)

Example 3 with HugeAccess

use of com.baidu.hugegraph.auth.HugeAccess in project incubator-hugegraph by apache.

the class AuthTest method testListAccess.

@Test
public void testListAccess() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id group = authManager.createGroup(makeGroup("group1"));
    Id target1 = authManager.createTarget(makeTarget("graph1", "url1"));
    Id target2 = authManager.createTarget(makeTarget("graph2", "url2"));
    Id id1 = authManager.createAccess(makeAccess(group, target1, HugePermission.READ));
    Id id2 = authManager.createAccess(makeAccess(group, target2, HugePermission.READ));
    List<HugeAccess> access = authManager.listAccess(ImmutableList.of(id1, id2));
    Assert.assertEquals(2, access.size());
    Assert.assertEquals(group, access.get(0).source());
    Assert.assertEquals(group, access.get(1).source());
    Assert.assertEquals(target1, access.get(0).target());
    Assert.assertEquals(target2, access.get(1).target());
    access = authManager.listAccess(ImmutableList.of(id1, id2, id2));
    Assert.assertEquals(3, access.size());
    access = authManager.listAccess(ImmutableList.of(id1, id2, IdGenerator.of("fake")));
    Assert.assertEquals(2, access.size());
    access = authManager.listAccessByGroup(group, -1);
    Assert.assertEquals(2, access.size());
    Assert.assertEquals(group, access.get(0).source());
    Assert.assertEquals(group, access.get(1).source());
    access = authManager.listAccessByTarget(target1, -1);
    Assert.assertEquals(1, access.size());
    Assert.assertEquals(group, access.get(0).source());
    Assert.assertEquals(target1, access.get(0).target());
    access = authManager.listAccessByTarget(target2, -1);
    Assert.assertEquals(1, access.size());
    Assert.assertEquals(group, access.get(0).source());
    Assert.assertEquals(target2, access.get(0).target());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) HugeAccess(com.baidu.hugegraph.auth.HugeAccess) Id(com.baidu.hugegraph.backend.id.Id) Test(org.junit.Test)

Example 4 with HugeAccess

use of com.baidu.hugegraph.auth.HugeAccess in project incubator-hugegraph by apache.

the class AuthTest method testCreateAccess.

@Test
public void testCreateAccess() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id group1 = authManager.createGroup(makeGroup("group1"));
    Id group2 = authManager.createGroup(makeGroup("group2"));
    Id target1 = authManager.createTarget(makeTarget("graph1", "url1"));
    Id target2 = authManager.createTarget(makeTarget("graph2", "url2"));
    Id id1 = authManager.createAccess(makeAccess(group1, target1, HugePermission.READ));
    Id id2 = authManager.createAccess(makeAccess(group1, target1, HugePermission.WRITE));
    Id id3 = authManager.createAccess(makeAccess(group1, target2, HugePermission.READ));
    Id id4 = authManager.createAccess(makeAccess(group2, target2, HugePermission.READ));
    HugeAccess access = authManager.getAccess(id1);
    Assert.assertEquals(group1, access.source());
    Assert.assertEquals(target1, access.target());
    Assert.assertEquals(HugePermission.READ, access.permission());
    Assert.assertEquals(access.create(), access.update());
    Map<String, Object> expected = new HashMap<>();
    expected.putAll(ImmutableMap.of("id", access.id(), "group", group1, "target", target1, "access_permission", HugePermission.READ, "access_creator", "admin"));
    expected.putAll(ImmutableMap.of("access_create", access.create(), "access_update", access.update()));
    Assert.assertEquals(expected, access.asMap());
    access = authManager.getAccess(id2);
    Assert.assertEquals(group1, access.source());
    Assert.assertEquals(target1, access.target());
    Assert.assertEquals(HugePermission.WRITE, access.permission());
    Assert.assertEquals(access.create(), access.update());
    expected = new HashMap<>();
    expected.putAll(ImmutableMap.of("id", access.id(), "group", group1, "target", target1, "access_permission", HugePermission.WRITE, "access_creator", "admin"));
    expected.putAll(ImmutableMap.of("access_create", access.create(), "access_update", access.update()));
    Assert.assertEquals(expected, access.asMap());
    access = authManager.getAccess(id3);
    Assert.assertEquals(group1, access.source());
    Assert.assertEquals(target2, access.target());
    Assert.assertEquals(HugePermission.READ, access.permission());
    Assert.assertEquals(access.create(), access.update());
    expected = new HashMap<>();
    expected.putAll(ImmutableMap.of("id", access.id(), "group", group1, "target", target2, "access_permission", HugePermission.READ, "access_creator", "admin"));
    expected.putAll(ImmutableMap.of("access_create", access.create(), "access_update", access.update()));
    Assert.assertEquals(expected, access.asMap());
    access = authManager.getAccess(id4);
    Assert.assertEquals(group2, access.source());
    Assert.assertEquals(target2, access.target());
    Assert.assertEquals(HugePermission.READ, access.permission());
    Assert.assertEquals(access.create(), access.update());
    expected = new HashMap<>();
    expected.putAll(ImmutableMap.of("id", access.id(), "group", group2, "target", target2, "access_permission", HugePermission.READ, "access_creator", "admin"));
    expected.putAll(ImmutableMap.of("access_create", access.create(), "access_update", access.update()));
    Assert.assertEquals(expected, access.asMap());
    List<HugeAccess> accesses = authManager.listAccessByGroup(group1, -1);
    Assert.assertEquals(3, accesses.size());
    accesses = authManager.listAccessByGroup(group2, -1);
    Assert.assertEquals(1, accesses.size());
    accesses = authManager.listAccessByTarget(target1, -1);
    Assert.assertEquals(2, accesses.size());
    accesses = authManager.listAccessByTarget(target2, -1);
    Assert.assertEquals(2, accesses.size());
    // Create access with description
    access = makeAccess(group2, target2, HugePermission.WRITE);
    access.description("something3");
    Id id5 = authManager.createAccess(access);
    access = authManager.getAccess(id5);
    Assert.assertEquals(group2, access.source());
    Assert.assertEquals(target2, access.target());
    Assert.assertEquals(HugePermission.WRITE, access.permission());
    Assert.assertEquals("something3", access.description());
    Assert.assertEquals(access.create(), access.update());
    expected = new HashMap<>();
    expected.putAll(ImmutableMap.of("id", access.id(), "group", group2, "target", target2, "access_permission", HugePermission.WRITE, "access_creator", "admin"));
    expected.putAll(ImmutableMap.of("access_description", "something3", "access_create", access.create(), "access_update", access.update()));
    Assert.assertEquals(expected, access.asMap());
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        authManager.createAccess(makeAccess(group1, target1, HugePermission.READ));
    }, e -> {
        Assert.assertContains("Can't save access", e.getMessage());
        Assert.assertContains("that already exists", e.getMessage());
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) HashMap(java.util.HashMap) HugeAccess(com.baidu.hugegraph.auth.HugeAccess) Id(com.baidu.hugegraph.backend.id.Id) Test(org.junit.Test)

Example 5 with HugeAccess

use of com.baidu.hugegraph.auth.HugeAccess in project incubator-hugegraph by apache.

the class AuthTest method testUpdateAccess.

@Test
public void testUpdateAccess() throws InterruptedException {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id group = authManager.createGroup(makeGroup("group1"));
    Id target = authManager.createTarget(makeTarget("graph1", "url1"));
    Id id = authManager.createAccess(makeAccess(group, target, HugePermission.READ));
    HugeAccess access = authManager.getAccess(id);
    Assert.assertEquals(group, access.source());
    Assert.assertEquals(target, access.target());
    Assert.assertEquals(HugePermission.READ, access.permission());
    Assert.assertEquals(access.create(), access.update());
    Date oldUpdateTime = access.update();
    Thread.sleep(1L);
    access.permission(HugePermission.READ);
    authManager.updateAccess(access);
    HugeAccess access2 = authManager.getAccess(id);
    Assert.assertEquals(group, access.source());
    Assert.assertEquals(target, access.target());
    Assert.assertEquals(HugePermission.READ, access.permission());
    Assert.assertEquals(oldUpdateTime, access2.create());
    Assert.assertNotEquals(oldUpdateTime, access2.update());
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        access.permission(HugePermission.WRITE);
        authManager.updateAccess(access);
    }, e -> {
        Assert.assertContains("Can't save access", e.getMessage());
        Assert.assertContains("that not exists", e.getMessage());
    });
    access.permission(HugePermission.READ);
    access.description("description updated");
    id = authManager.updateAccess(access);
    HugeAccess access3 = authManager.getAccess(id);
    Assert.assertEquals(group, access3.source());
    Assert.assertEquals(target, access3.target());
    Assert.assertEquals("description updated", access3.description());
    Assert.assertEquals(HugePermission.READ, access3.permission());
    Assert.assertEquals(oldUpdateTime, access3.create());
    Assert.assertNotEquals(access3.create(), access3.update());
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        HugeAccess access4 = makeAccess(group, target, HugePermission.DELETE);
        authManager.updateAccess(access4);
    }, e -> {
        Assert.assertContains("Can't save access", e.getMessage());
        Assert.assertContains("that not exists", e.getMessage());
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) HugeAccess(com.baidu.hugegraph.auth.HugeAccess) Id(com.baidu.hugegraph.backend.id.Id) Date(java.util.Date) Test(org.junit.Test)

Aggregations

HugeAccess (com.baidu.hugegraph.auth.HugeAccess)11 HugeGraph (com.baidu.hugegraph.HugeGraph)10 Id (com.baidu.hugegraph.backend.id.Id)7 AuthManager (com.baidu.hugegraph.auth.AuthManager)6 Test (org.junit.Test)6 Timed (com.codahale.metrics.annotation.Timed)4 Produces (jakarta.ws.rs.Produces)4 Consumes (jakarta.ws.rs.Consumes)2 GET (jakarta.ws.rs.GET)2 Path (jakarta.ws.rs.Path)2 Status (com.baidu.hugegraph.api.filter.StatusFilter.Status)1 NotFoundException (com.baidu.hugegraph.exception.NotFoundException)1 POST (jakarta.ws.rs.POST)1 PUT (jakarta.ws.rs.PUT)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1