-
Notifications
You must be signed in to change notification settings - Fork 26
Add RBAC support (sync client only) #348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9a25f8a
chore: gitignore .factorypath generated by maven-lombok-plugin
bevzzz 7012d4d
chore: upgrage maven-compiler-plugin to work with later Java version …
bevzzz 7aa7d94
feat: serialize different permissions type to the API format
bevzzz 0a4643c
feat: handle permission deserialization
bevzzz 8fcdd3b
feat: expose all existing endpoints in the sync client
bevzzz 4895040
test: add configuration to run Weaviate Testcontainer with RBAC
bevzzz 01a55ad
test: add integration tests for add-/remove-/has-permission
bevzzz c323e93
test: add test for getting user roles
bevzzz 33c4306
test: add test for assinging and revoking roles
bevzzz 51a893e
test: reduce code duplication
bevzzz 32da1fc
test: fix test case -- manage_collections is not currently supported
bevzzz 5f61f4c
ci: fix expected Weaviate server version
bevzzz 4e30f73
chore: delete debug print stmts
bevzzz fc3f74b
ci: fix expected commit hash
bevzzz 1e0213e
test: update expected error message for invalid tokenizer
bevzzz 0f6d7e4
test: remove duplicated env variable
bevzzz 0489b47
chore: fix documentation
bevzzz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,3 +9,5 @@ | |
| *.iml | ||
| # Maven | ||
| target/ | ||
| # maven-lombok-plugin | ||
| .factorypath | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package io.weaviate.client.v1.rbac; | ||
|
|
||
| import io.weaviate.client.Config; | ||
| import io.weaviate.client.base.http.HttpClient; | ||
| import io.weaviate.client.v1.rbac.api.AssignedUsersGetter; | ||
| import io.weaviate.client.v1.rbac.api.PermissionAdder; | ||
| import io.weaviate.client.v1.rbac.api.PermissionChecker; | ||
| import io.weaviate.client.v1.rbac.api.PermissionRemover; | ||
| import io.weaviate.client.v1.rbac.api.RoleAllGetter; | ||
| import io.weaviate.client.v1.rbac.api.RoleAssigner; | ||
| import io.weaviate.client.v1.rbac.api.RoleCreator; | ||
| import io.weaviate.client.v1.rbac.api.RoleDeleter; | ||
| import io.weaviate.client.v1.rbac.api.RoleExists; | ||
| import io.weaviate.client.v1.rbac.api.RoleGetter; | ||
| import io.weaviate.client.v1.rbac.api.RoleRevoker; | ||
| import io.weaviate.client.v1.rbac.api.UserRolesGetter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @RequiredArgsConstructor | ||
| public class Roles { | ||
|
|
||
| private final HttpClient httpClient; | ||
| private final Config config; | ||
|
|
||
| /** Create a new role. */ | ||
| public RoleCreator creator() { | ||
| return new RoleCreator(httpClient, config); | ||
| } | ||
|
|
||
| /** Delete a role. */ | ||
| public RoleDeleter deleter() { | ||
| return new RoleDeleter(httpClient, config); | ||
| } | ||
|
|
||
| /** | ||
| * Add permissions to an existing role. | ||
| * Note: This method is an upsert operation. If the permission already exists, | ||
| * it will be updated. If it does not exist, it will be created. | ||
| */ | ||
| public PermissionAdder permissionAdder() { | ||
| return new PermissionAdder(httpClient, config); | ||
| } | ||
|
|
||
| /** | ||
| * Remove permissions from a role. | ||
| * Note: This method is a downsert operation. If the permission does not | ||
| * exist, it will be ignored. If these permissions are the only permissions of | ||
| * the role, the role will be deleted. | ||
| */ | ||
| public PermissionRemover permissionRemover() { | ||
| return new PermissionRemover(httpClient, config); | ||
| } | ||
|
|
||
| /** Check if a role has a permission. */ | ||
| public PermissionChecker permissionChecker() { | ||
| return new PermissionChecker(httpClient, config); | ||
| } | ||
|
|
||
| /** Get all existing roles. */ | ||
| public RoleAllGetter allGetter() { | ||
| return new RoleAllGetter(httpClient, config); | ||
| }; | ||
|
|
||
| /** Get role and its associated permissions. */ | ||
| public RoleGetter getter() { | ||
| return new RoleGetter(httpClient, config); | ||
| }; | ||
|
|
||
| /** Get roles assigned to a user. */ | ||
| public UserRolesGetter userRolesGetter() { | ||
| return new UserRolesGetter(httpClient, config); | ||
| }; | ||
|
|
||
| /** Get users assigned to a role. */ | ||
| public AssignedUsersGetter assignedUsersGetter() { | ||
| return new AssignedUsersGetter(httpClient, config); | ||
| }; | ||
|
|
||
| /** Check if a role exists. */ | ||
| public RoleExists exists() { | ||
| return new RoleExists(httpClient, config); | ||
| } | ||
|
|
||
| /** Assign a role to a user. Note that 'root' cannot be assigned. */ | ||
| public RoleAssigner assigner() { | ||
| return new RoleAssigner(httpClient, config); | ||
| } | ||
|
|
||
| /** Revoke a role from a user. Note that 'root' cannot be revoked. */ | ||
| public RoleRevoker revoker() { | ||
| return new RoleRevoker(httpClient, config); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
src/main/java/io/weaviate/client/v1/rbac/api/AssignedUsersGetter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package io.weaviate.client.v1.rbac.api; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import io.weaviate.client.Config; | ||
| import io.weaviate.client.base.BaseClient; | ||
| import io.weaviate.client.base.ClientResult; | ||
| import io.weaviate.client.base.Response; | ||
| import io.weaviate.client.base.Result; | ||
| import io.weaviate.client.base.http.HttpClient; | ||
|
|
||
| public class AssignedUsersGetter extends BaseClient<String[]> implements ClientResult<List<String>> { | ||
| private String role; | ||
|
|
||
| public AssignedUsersGetter(HttpClient httpClient, Config config) { | ||
| super(httpClient, config); | ||
| } | ||
|
|
||
| public AssignedUsersGetter withRole(String role) { | ||
| this.role = role; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public Result<List<String>> run() { | ||
| Response<String[]> resp = sendGetRequest(path(), String[].class); | ||
| List<String> roles = Optional.ofNullable(resp.getBody()) | ||
| .map(Arrays::asList) | ||
| .orElse(new ArrayList<>()); | ||
| return new Result<>(resp.getStatusCode(), roles, resp.getErrors()); | ||
| } | ||
|
|
||
| private String path() { | ||
| return String.format("/authz/roles/%s/users", this.role); | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
src/main/java/io/weaviate/client/v1/rbac/api/PermissionAdder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package io.weaviate.client.v1.rbac.api; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import io.weaviate.client.Config; | ||
| import io.weaviate.client.base.BaseClient; | ||
| import io.weaviate.client.base.ClientResult; | ||
| import io.weaviate.client.base.Result; | ||
| import io.weaviate.client.base.http.HttpClient; | ||
| import io.weaviate.client.v1.rbac.model.Permission; | ||
| import lombok.AllArgsConstructor; | ||
|
|
||
| public class PermissionAdder extends BaseClient<Void> implements ClientResult<Void> { | ||
| private String role; | ||
| private List<Permission<?>> permissions = new ArrayList<>(); | ||
|
|
||
| public PermissionAdder(HttpClient httpClient, Config config) { | ||
| super(httpClient, config); | ||
| } | ||
|
|
||
| public PermissionAdder withRole(String name) { | ||
| this.role = name; | ||
| return this; | ||
| } | ||
|
|
||
| public PermissionAdder withPermissions(Permission<?>... permissions) { | ||
| this.permissions = Arrays.asList(permissions); | ||
| return this; | ||
| } | ||
|
|
||
| @AllArgsConstructor | ||
| private static class Body { | ||
| public final List<?> permissions; | ||
| } | ||
|
|
||
| @Override | ||
| public Result<Void> run() { | ||
| List<WeaviatePermission> permissions = WeaviatePermission.mergePermissions(this.permissions); | ||
| return new Result<Void>(sendPostRequest(path(), new Body(permissions), Void.class)); | ||
| } | ||
|
|
||
| private String path() { | ||
| return String.format("/authz/roles/%s/add-permissions", this.role); | ||
| } | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
src/main/java/io/weaviate/client/v1/rbac/api/PermissionChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package io.weaviate.client.v1.rbac.api; | ||
|
|
||
| import io.weaviate.client.Config; | ||
| import io.weaviate.client.base.BaseClient; | ||
| import io.weaviate.client.base.ClientResult; | ||
| import io.weaviate.client.base.Result; | ||
| import io.weaviate.client.base.http.HttpClient; | ||
| import io.weaviate.client.v1.rbac.model.Permission; | ||
|
|
||
| public class PermissionChecker extends BaseClient<Boolean> implements ClientResult<Boolean> { | ||
| private String role; | ||
| private Permission<?> permission; | ||
|
|
||
| public PermissionChecker(HttpClient httpClient, Config config) { | ||
| super(httpClient, config); | ||
| } | ||
|
|
||
| public PermissionChecker withRole(String role) { | ||
| this.role = role; | ||
| return this; | ||
| } | ||
|
|
||
| public PermissionChecker withPermission(Permission<?> permission) { | ||
| this.permission = permission; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public Result<Boolean> run() { | ||
| return new Result<Boolean>(sendPostRequest(path(), permission.toWeaviate(), Boolean.class)); | ||
| } | ||
|
|
||
| private String path() { | ||
| return String.format("/authz/roles/%s/has-permission", this.role); | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
src/main/java/io/weaviate/client/v1/rbac/api/PermissionRemover.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package io.weaviate.client.v1.rbac.api; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import io.weaviate.client.Config; | ||
| import io.weaviate.client.base.BaseClient; | ||
| import io.weaviate.client.base.ClientResult; | ||
| import io.weaviate.client.base.Result; | ||
| import io.weaviate.client.base.http.HttpClient; | ||
| import io.weaviate.client.v1.rbac.model.Permission; | ||
| import lombok.AllArgsConstructor; | ||
|
|
||
| public class PermissionRemover extends BaseClient<Void> implements ClientResult<Void> { | ||
| private String role; | ||
| private List<Permission<?>> permissions = new ArrayList<>(); | ||
|
|
||
| public PermissionRemover(HttpClient httpClient, Config config) { | ||
| super(httpClient, config); | ||
| } | ||
|
|
||
| public PermissionRemover withRole(String role) { | ||
| this.role = role; | ||
| return this; | ||
| } | ||
|
|
||
| public PermissionRemover withPermissions(Permission<?>... permissions) { | ||
| this.permissions = Arrays.asList(permissions); | ||
| return this; | ||
| } | ||
|
|
||
| @AllArgsConstructor | ||
| private static class Body { | ||
| public final List<?> permissions; | ||
| } | ||
|
|
||
| @Override | ||
| public Result<Void> run() { | ||
bevzzz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| List<WeaviatePermission> permissions = WeaviatePermission.mergePermissions(this.permissions); | ||
| return new Result<Void>(sendPostRequest(path(), new Body(permissions), Void.class)); | ||
| } | ||
|
|
||
| private String path() { | ||
| return String.format("/authz/roles/%s/remove-permissions", this.role); | ||
| } | ||
| } | ||
33 changes: 33 additions & 0 deletions
33
src/main/java/io/weaviate/client/v1/rbac/api/RoleAllGetter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package io.weaviate.client.v1.rbac.api; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import io.weaviate.client.Config; | ||
| import io.weaviate.client.base.BaseClient; | ||
| import io.weaviate.client.base.ClientResult; | ||
| import io.weaviate.client.base.Response; | ||
| import io.weaviate.client.base.Result; | ||
| import io.weaviate.client.base.http.HttpClient; | ||
| import io.weaviate.client.v1.rbac.model.Role; | ||
|
|
||
| public class RoleAllGetter extends BaseClient<WeaviateRole[]> implements ClientResult<List<Role>> { | ||
|
|
||
| public RoleAllGetter(HttpClient httpClient, Config config) { | ||
| super(httpClient, config); | ||
| } | ||
|
|
||
| @Override | ||
| public Result<List<Role>> run() { | ||
| Response<WeaviateRole[]> resp = sendGetRequest("/authz/roles", WeaviateRole[].class); | ||
| List<Role> roles = Optional.ofNullable(resp.getBody()) | ||
| .map(Arrays::asList) | ||
| .orElse(new ArrayList<>()) | ||
| .stream() | ||
| .map(w -> w.toRole()) | ||
| .toList(); | ||
| return new Result<>(resp.getStatusCode(), roles, resp.getErrors()); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.