Search in sources :

Example 6 with Target

use of com.baidu.hugegraph.structure.auth.Target in project incubator-hugegraph-toolchain by apache.

the class AuthManagerTest method testAuth.

@Test
public void testAuth() {
    User user = new User();
    user.name("bob");
    user.password("123456");
    user = auth().createUser(user);
    Group group = new Group();
    group.name("managers");
    group = auth().createGroup(group);
    Target gremlin = new Target();
    gremlin.name("gremlin");
    gremlin.graph("hugegraph");
    gremlin.url("127.0.0.1:8080");
    gremlin.resources(new HugeResource(HugeResourceType.GREMLIN));
    gremlin = auth().createTarget(gremlin);
    Target task = new Target();
    task.name("task");
    task.graph("hugegraph");
    task.url("127.0.0.1:8080");
    task.resources(new HugeResource(HugeResourceType.TASK));
    task = auth().createTarget(task);
    Belong belong = new Belong();
    belong.user(user);
    belong.group(group);
    belong = auth().createBelong(belong);
    Access access1 = new Access();
    access1.group(group);
    access1.target(gremlin);
    access1.permission(HugePermission.EXECUTE);
    access1 = auth().createAccess(access1);
    Access access2 = new Access();
    access2.group(group);
    access2.target(task);
    access2.permission(HugePermission.READ);
    access2 = auth().createAccess(access2);
    Project project1 = new Project("test");
    project1 = auth().createProject(project1);
    Assert.assertEquals("test", project1.name());
    Project project2 = new Project("test2");
    project2 = auth().createProject(project2);
    Assert.assertEquals("test2", project2.name());
    Project newProject1 = auth().getProject(project1);
    Assert.assertEquals(newProject1.id(), project1.id());
    Assert.assertTrue(CollectionUtils.isEmpty(newProject1.graphs()));
    List<Project> projects = auth().listProjects();
    Assert.assertNotNull(projects);
    Assert.assertEquals(2, projects.size());
    Set<String> graphs = ImmutableSet.of("graph1", "graph2");
    newProject1 = auth().projectAddGraphs(project1, graphs);
    Assert.assertNotNull(newProject1);
    Assert.assertEquals(graphs, newProject1.graphs());
    graphs = ImmutableSet.of("graph2");
    newProject1 = auth().projectRemoveGraphs(project1, ImmutableSet.of("graph1"));
    Assert.assertNotNull(newProject1);
    Assert.assertEquals(graphs, newProject1.graphs());
    Object project1Id = project1.id();
    project1 = new Project(project1Id);
    project1.description("test description");
    newProject1 = auth().updateProject(project1);
    Assert.assertEquals(newProject1.description(), project1.description());
    auth().deleteProject(project2);
    projects.remove(project2);
    List<Project> newProjects = auth().listProjects();
    Assert.assertEquals(newProjects, projects);
    UserRole role = auth().getUserRole(user);
    String r = "{\"roles\":{\"hugegraph\":" + "{\"READ\":[{\"type\":\"TASK\",\"label\":\"*\",\"properties\":null}]," + "\"EXECUTE\":[{\"type\":\"GREMLIN\",\"label\":\"*\",\"properties\":null}]}}}";
    Assert.assertEquals(r, role.toString());
    Login login = new Login();
    login.name("bob");
    login.password("123456");
    LoginResult result = auth().login(login);
    String token = result.token();
    HugeClient client = baseClient();
    client.setAuthContext("Bearer " + token);
    TokenPayload payload = auth().verifyToken();
    Assert.assertEquals("bob", payload.username());
    Assert.assertEquals(user.id(), payload.userId());
    auth().logout();
    client.resetAuthContext();
}
Also used : Group(com.baidu.hugegraph.structure.auth.Group) HugeClient(com.baidu.hugegraph.driver.HugeClient) User(com.baidu.hugegraph.structure.auth.User) LoginResult(com.baidu.hugegraph.structure.auth.LoginResult) Access(com.baidu.hugegraph.structure.auth.Access) Login(com.baidu.hugegraph.structure.auth.Login) TokenPayload(com.baidu.hugegraph.structure.auth.TokenPayload) Project(com.baidu.hugegraph.structure.auth.Project) Target(com.baidu.hugegraph.structure.auth.Target) UserRole(com.baidu.hugegraph.structure.auth.User.UserRole) HugeResource(com.baidu.hugegraph.structure.auth.HugeResource) Belong(com.baidu.hugegraph.structure.auth.Belong) Test(org.junit.Test)

Example 7 with Target

use of com.baidu.hugegraph.structure.auth.Target in project incubator-hugegraph-toolchain by apache.

the class TargetApiTest method testCreate.

@Test
public void testCreate() {
    Target target1 = new Target();
    target1.name("gremlin");
    target1.graph("hugegraph");
    target1.url("127.0.0.1:8080");
    HugeResource gremlin = new HugeResource(HugeResourceType.GREMLIN);
    target1.resources(gremlin);
    Target target2 = new Target();
    target2.name("task");
    target2.graph("hugegraph2");
    target2.url("127.0.0.1:8081");
    HugeResource task = new HugeResource(HugeResourceType.TASK);
    target2.resources(task);
    Target result1 = api.create(target1);
    Target result2 = api.create(target2);
    Assert.assertEquals("gremlin", result1.name());
    Assert.assertEquals("hugegraph", result1.graph());
    Assert.assertEquals("127.0.0.1:8080", result1.url());
    Assert.assertEquals(Arrays.asList(gremlin), result1.resources());
    Assert.assertEquals("task", result2.name());
    Assert.assertEquals("hugegraph2", result2.graph());
    Assert.assertEquals("127.0.0.1:8081", result2.url());
    Assert.assertEquals(Arrays.asList(task), result2.resources());
    Assert.assertThrows(ServerException.class, () -> {
        api.create(target1);
    }, e -> {
        Assert.assertContains("Can't save target", e.getMessage());
        Assert.assertContains("that already exists", e.getMessage());
    });
    Assert.assertThrows(ServerException.class, () -> {
        Target target3 = new Target();
        api.create(target3);
    }, e -> {
        Assert.assertContains("The name of target can't be null", e.getMessage());
    });
    Assert.assertThrows(ServerException.class, () -> {
        Target target3 = new Target();
        target3.name("test");
        api.create(target3);
    }, e -> {
        Assert.assertContains("The graph of target can't be null", e.getMessage());
    });
    Assert.assertThrows(ServerException.class, () -> {
        Target target3 = new Target();
        target3.name("test");
        target3.graph("hugegraph3");
        api.create(target3);
    }, e -> {
        Assert.assertContains("The url of target can't be null", e.getMessage());
    });
}
Also used : Target(com.baidu.hugegraph.structure.auth.Target) HugeResource(com.baidu.hugegraph.structure.auth.HugeResource) Test(org.junit.Test)

Example 8 with Target

use of com.baidu.hugegraph.structure.auth.Target in project incubator-hugegraph-toolchain by apache.

the class TargetApiTest method createTarget.

protected static Target createTarget(String name, HugeResourceType res) {
    Target target = new Target();
    target.name(name);
    target.graph("hugegraph");
    target.url("127.0.0.1:8080");
    target.resources(new HugeResource(res));
    return api.create(target);
}
Also used : Target(com.baidu.hugegraph.structure.auth.Target) HugeResource(com.baidu.hugegraph.structure.auth.HugeResource)

Example 9 with Target

use of com.baidu.hugegraph.structure.auth.Target in project incubator-hugegraph-toolchain by apache.

the class AccessApiTest method testListByTarget.

@Test
public void testListByTarget() {
    createAccess(HugePermission.READ, "description 1");
    createAccess(HugePermission.WRITE, "description 2");
    createAccess(HugePermission.EXECUTE, "description 3");
    Group hk = GroupApiTest.createGroup("group-hk", "group for hongkong");
    createAccess(hk, gremlin, HugePermission.READ, "description 4");
    createAccess(hk, gremlin, HugePermission.WRITE, "description 5");
    Target task = TargetApiTest.createTarget("task", HugeResourceType.TASK);
    createAccess(hk, task, HugePermission.READ, "description 6");
    createAccess(hk, task, HugePermission.WRITE, "description 7");
    List<Access> accesss = api.list(null, null, -1);
    Assert.assertEquals(7, accesss.size());
    accesss = api.list(null, task, -1);
    Assert.assertEquals(2, accesss.size());
    accesss.sort((t1, t2) -> t1.permission().compareTo(t2.permission()));
    Assert.assertEquals("description 6", accesss.get(0).description());
    Assert.assertEquals("description 7", accesss.get(1).description());
    accesss = api.list(null, gremlin, -1);
    Assert.assertEquals(5, accesss.size());
    accesss.sort((t1, t2) -> {
        String s1 = "" + t1.group() + t1.permission().ordinal();
        String s2 = "" + t2.group() + t2.permission().ordinal();
        return s1.compareTo(s2);
    });
    Assert.assertEquals("description 1", accesss.get(0).description());
    Assert.assertEquals("description 2", accesss.get(1).description());
    Assert.assertEquals("description 3", accesss.get(2).description());
    Assert.assertEquals("description 4", accesss.get(3).description());
    Assert.assertEquals("description 5", accesss.get(4).description());
    accesss = api.list(null, gremlin, 1);
    Assert.assertEquals(1, accesss.size());
    accesss = api.list(null, gremlin, 2);
    Assert.assertEquals(2, accesss.size());
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        api.list(null, gremlin, 0);
    }, e -> {
        Assert.assertContains("Limit must be > 0 or == -1", e.getMessage());
    });
    Assert.assertThrows(ServerException.class, () -> {
        api.list(hk, task, -1);
    }, e -> {
        Assert.assertContains("Can't pass both group and target " + "at the same time", e.getMessage());
    });
}
Also used : Group(com.baidu.hugegraph.structure.auth.Group) Target(com.baidu.hugegraph.structure.auth.Target) Access(com.baidu.hugegraph.structure.auth.Access) Test(org.junit.Test)

Example 10 with Target

use of com.baidu.hugegraph.structure.auth.Target in project incubator-hugegraph-toolchain by apache.

the class AuthManager method deleteAll.

public void deleteAll() {
    for (Belong belong : this.listBelongs()) {
        this.deleteBelong(belong.id());
    }
    for (Access access : this.listAccesses()) {
        this.deleteAccess(access.id());
    }
    for (User user : this.listUsers()) {
        if (user.name().equals("admin")) {
            continue;
        }
        this.deleteUser(user.id());
    }
    for (Group group : this.listGroups()) {
        this.deleteGroup(group.id());
    }
    for (Target target : this.listTargets()) {
        this.deleteTarget(target.id());
    }
    for (Project project : this.listProjects()) {
        Set<String> graphs = project.graphs();
        if (CollectionUtils.isNotEmpty(graphs)) {
            this.projectRemoveGraphs(project.id(), graphs);
        }
        this.deleteProject(project.id());
    }
}
Also used : Group(com.baidu.hugegraph.structure.auth.Group) Project(com.baidu.hugegraph.structure.auth.Project) Target(com.baidu.hugegraph.structure.auth.Target) User(com.baidu.hugegraph.structure.auth.User) Access(com.baidu.hugegraph.structure.auth.Access) Belong(com.baidu.hugegraph.structure.auth.Belong)

Aggregations

Target (com.baidu.hugegraph.structure.auth.Target)10 Test (org.junit.Test)8 Access (com.baidu.hugegraph.structure.auth.Access)5 Group (com.baidu.hugegraph.structure.auth.Group)5 HugeResource (com.baidu.hugegraph.structure.auth.HugeResource)4 Belong (com.baidu.hugegraph.structure.auth.Belong)3 User (com.baidu.hugegraph.structure.auth.User)3 Project (com.baidu.hugegraph.structure.auth.Project)2 HugeClient (com.baidu.hugegraph.driver.HugeClient)1 Login (com.baidu.hugegraph.structure.auth.Login)1 LoginResult (com.baidu.hugegraph.structure.auth.LoginResult)1 TokenPayload (com.baidu.hugegraph.structure.auth.TokenPayload)1 UserRole (com.baidu.hugegraph.structure.auth.User.UserRole)1