Skip to content

Commit e45ce3e

Browse files
committed
feat: add async entrypoint
1 parent 8105907 commit e45ce3e

File tree

10 files changed

+428
-4
lines changed

10 files changed

+428
-4
lines changed

src/it/java/io/weaviate/integration/RbacITest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void test_roles_Lifecycle() throws IOException {
8080
client.roles.create(nsRole, permissions);
8181

8282
var role = client.roles.get(nsRole);
83-
Assertions.assertThat(role)
83+
Assertions.assertThat(role).get()
8484
.as("created role")
8585
.returns(nsRole, Role::name)
8686
.extracting(Role::permissions, InstanceOfAssertFactories.list(Permission.class))

src/main/java/io/weaviate/client6/v1/api/WeaviateClientAsync.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import io.weaviate.client6.v1.api.alias.WeaviateAliasClientAsync;
88
import io.weaviate.client6.v1.api.collections.WeaviateCollectionsClient;
99
import io.weaviate.client6.v1.api.collections.WeaviateCollectionsClientAsync;
10+
import io.weaviate.client6.v1.api.rbac.groups.WeaviateGroupsClientAsync;
11+
import io.weaviate.client6.v1.api.rbac.roles.WeaviateRolesClientAsync;
12+
import io.weaviate.client6.v1.api.rbac.users.WeaviateUsersClientAsync;
1013
import io.weaviate.client6.v1.internal.ObjectBuilder;
1114
import io.weaviate.client6.v1.internal.TokenProvider;
1215
import io.weaviate.client6.v1.internal.grpc.DefaultGrpcTransport;
@@ -30,6 +33,21 @@ public class WeaviateClientAsync implements AutoCloseable {
3033
/** Client for {@code /aliases} endpoints for managing collection aliases. */
3134
public final WeaviateAliasClientAsync alias;
3235

36+
/**
37+
* Client for {@code /authz/roles} endpoints for managing RBAC roles.
38+
*/
39+
public final WeaviateRolesClientAsync roles;
40+
41+
/**
42+
* Client for {@code /authz/groups} endpoints for managing RBAC groups.
43+
*/
44+
public final WeaviateGroupsClientAsync groups;
45+
46+
/**
47+
* Client for {@code /users} endpoints for managing DB / OIDC users.
48+
*/
49+
public final WeaviateUsersClientAsync users;
50+
3351
/**
3452
* This constructor is blocking if {@link Authentication} configured,
3553
* as the client will need to do the initial token exchange.
@@ -84,6 +102,9 @@ public WeaviateClientAsync(Config config) {
84102
this.restTransport = _restTransport;
85103
this.grpcTransport = new DefaultGrpcTransport(grpcOpt);
86104
this.alias = new WeaviateAliasClientAsync(restTransport);
105+
this.roles = new WeaviateRolesClientAsync(restTransport);
106+
this.groups = new WeaviateGroupsClientAsync(restTransport);
107+
this.users = new WeaviateUsersClientAsync(restTransport);
87108
this.collections = new WeaviateCollectionsClientAsync(restTransport, grpcTransport);
88109
}
89110

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.weaviate.client6.v1.api.rbac.groups;
2+
3+
import java.io.IOException;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.concurrent.CompletableFuture;
7+
import java.util.function.Function;
8+
9+
import io.weaviate.client6.v1.api.rbac.Role;
10+
import io.weaviate.client6.v1.internal.ObjectBuilder;
11+
import io.weaviate.client6.v1.internal.rest.RestTransport;
12+
13+
public class WeaviateGroupsClientAsync {
14+
private final RestTransport restTransport;
15+
16+
public WeaviateGroupsClientAsync(RestTransport restTransport) {
17+
this.restTransport = restTransport;
18+
}
19+
20+
/**
21+
* Get the roles assigned an OIDC group.
22+
*
23+
* @param groupId OIDC group ID.
24+
*/
25+
public CompletableFuture<List<Role>> assignedRoles(String groupId) {
26+
return this.restTransport.performRequestAsync(GetAssignedRolesRequest.of(groupId),
27+
GetAssignedRolesRequest._ENDPOINT);
28+
}
29+
30+
/**
31+
* Get the roles assigned an OIDC group.
32+
*
33+
* @param groupId OIDC group ID.
34+
* @param fn Lambda expression for optional parameters.
35+
*/
36+
public CompletableFuture<List<Role>> assignedRoles(String groupId,
37+
Function<GetAssignedRolesRequest.Builder, ObjectBuilder<GetAssignedRolesRequest>> fn) {
38+
return this.restTransport.performRequestAsync(GetAssignedRolesRequest.of(groupId, fn),
39+
GetAssignedRolesRequest._ENDPOINT);
40+
}
41+
42+
/** Get the names of known OIDC groups. */
43+
public CompletableFuture<List<String>> knownGroupNames() {
44+
return this.restTransport.performRequestAsync(null, GetKnownGroupNamesRequest._ENDPOINT);
45+
}
46+
47+
/**
48+
* Assign roles to OIDC group.
49+
*
50+
* @param groupId OIDC group ID.
51+
* @param roleNames Role names.
52+
*/
53+
public CompletableFuture<Void> assignRoles(String groupId, String... roleNames) {
54+
return this.restTransport.performRequestAsync(new AssignRolesRequest(groupId, Arrays.asList(roleNames)),
55+
AssignRolesRequest._ENDPOINT);
56+
}
57+
58+
/**
59+
* Revoke roles from OIDC group.
60+
*
61+
* @param groupId OIDC group ID.
62+
* @param roleNames Role names.
63+
*/
64+
public CompletableFuture<Void> revokeRoles(String groupId, String... roleNames) {
65+
return this.restTransport.performRequestAsync(new RevokeRolesRequest(groupId, Arrays.asList(roleNames)),
66+
RevokeRolesRequest._ENDPOINT);
67+
}
68+
}

src/main/java/io/weaviate/client6/v1/api/rbac/roles/GetRoleRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package io.weaviate.client6.v1.api.rbac.roles;
22

33
import java.util.Collections;
4+
import java.util.Optional;
45

56
import io.weaviate.client6.v1.api.rbac.Role;
67
import io.weaviate.client6.v1.internal.json.JSON;
78
import io.weaviate.client6.v1.internal.rest.Endpoint;
8-
import io.weaviate.client6.v1.internal.rest.SimpleEndpoint;
9+
import io.weaviate.client6.v1.internal.rest.OptionalEndpoint;
910
import io.weaviate.client6.v1.internal.rest.UrlEncoder;
1011

1112
public record GetRoleRequest(String roleName) {
12-
public static final Endpoint<GetRoleRequest, Role> _ENDPOINT = SimpleEndpoint.noBody(
13+
public static final Endpoint<GetRoleRequest, Optional<Role>> _ENDPOINT = OptionalEndpoint.noBodyOptional(
1314
__ -> "GET",
1415
request -> "/authz/roles/" + UrlEncoder.encodeValue(request.roleName),
1516
__ -> Collections.emptyMap(),

src/main/java/io/weaviate/client6/v1/api/rbac/roles/WeaviateRolesClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.util.Arrays;
55
import java.util.List;
6+
import java.util.Optional;
67

78
import io.weaviate.client6.v1.api.WeaviateApiException;
89
import io.weaviate.client6.v1.api.rbac.Permission;
@@ -56,7 +57,7 @@ public boolean exists(String roleName) throws IOException {
5657
* due to a malformed request, a networking error
5758
* or the server being unavailable.
5859
*/
59-
public Role get(String roleName) throws IOException {
60+
public Optional<Role> get(String roleName) throws IOException {
6061
return this.restTransport.performRequest(new GetRoleRequest(roleName), GetRoleRequest._ENDPOINT);
6162
}
6263

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package io.weaviate.client6.v1.api.rbac.roles;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Optional;
6+
import java.util.concurrent.CompletableFuture;
7+
8+
import io.weaviate.client6.v1.api.rbac.Permission;
9+
import io.weaviate.client6.v1.api.rbac.Role;
10+
import io.weaviate.client6.v1.internal.rest.RestTransport;
11+
12+
public class WeaviateRolesClientAsync {
13+
private final RestTransport restTransport;
14+
15+
public WeaviateRolesClientAsync(RestTransport restTransport) {
16+
this.restTransport = restTransport;
17+
}
18+
19+
/**
20+
* Create a new role.
21+
*
22+
* @param roleName Role name.
23+
* @param permissions Permissions granted to the role.
24+
*/
25+
public CompletableFuture<Void> create(String roleName, Permission... permissions) {
26+
var role = new Role(roleName, permissions);
27+
return this.restTransport.performRequestAsync(new CreateRoleRequest(role), CreateRoleRequest._ENDPOINT);
28+
}
29+
30+
/**
31+
* Check if a role with a given name exists.
32+
*
33+
* @param roleName Role name.
34+
*/
35+
public CompletableFuture<Boolean> exists(String roleName) {
36+
return this.restTransport.performRequestAsync(new RoleExistsRequest(roleName), RoleExistsRequest._ENDPOINT);
37+
}
38+
39+
/**
40+
* Fetch role definition.
41+
*
42+
* @param roleName Role name.
43+
*/
44+
public CompletableFuture<Optional<Role>> get(String roleName) {
45+
return this.restTransport.performRequestAsync(new GetRoleRequest(roleName), GetRoleRequest._ENDPOINT);
46+
}
47+
48+
/** List all existing roles. */
49+
public CompletableFuture<List<Role>> list() {
50+
return this.restTransport.performRequestAsync(null, ListRolesRequest._ENDPOINT);
51+
}
52+
53+
/**
54+
* Delete a role.
55+
*
56+
* @param roleName Role name.
57+
*/
58+
public CompletableFuture<Void> delete(String roleName) {
59+
return this.restTransport.performRequestAsync(new DeleteRoleRequest(roleName), DeleteRoleRequest._ENDPOINT);
60+
}
61+
62+
/**
63+
* Add permissions to a role.
64+
*
65+
* @param roleName Role name.
66+
* @param permissions Permissions to add to the role.
67+
*/
68+
public CompletableFuture<Void> addPermissions(String roleName, Permission... permissions) {
69+
return this.restTransport.performRequestAsync(new AddPermissionsRequest(roleName, Arrays.asList(permissions)),
70+
AddPermissionsRequest._ENDPOINT);
71+
}
72+
73+
/**
74+
* Remove permissions from a role.
75+
*
76+
* @param roleName Role name.
77+
* @param permissions Permissions to remove from the role.
78+
*/
79+
public CompletableFuture<Void> removePermissions(String roleName, Permission... permissions) {
80+
return this.restTransport.performRequestAsync(new RemovePermissionsRequest(roleName, Arrays.asList(permissions)),
81+
RemovePermissionsRequest._ENDPOINT);
82+
}
83+
84+
/**
85+
* Check if a role has a set of permissions.
86+
*
87+
* @param roleName Role name.
88+
* @param permission Permission to check.
89+
*/
90+
public CompletableFuture<Boolean> hasPermission(String roleName, Permission permission) {
91+
return this.restTransport.performRequestAsync(new HasPermissionRequest(roleName, permission),
92+
HasPermissionRequest._ENDPOINT);
93+
}
94+
95+
/**
96+
* Get IDs of all users this role is assigned to.
97+
*
98+
* @param roleName Role name.
99+
*/
100+
public CompletableFuture<List<String>> assignedUserIds(String roleName) {
101+
return this.restTransport.performRequestAsync(new GetAssignedUsersRequest(roleName),
102+
GetAssignedUsersRequest._ENDPOINT);
103+
}
104+
105+
/**
106+
* Get IDs of all users this role is assigned to along with their user type.
107+
*
108+
* <p>
109+
* Note that, unlike {@link #assignedUserIds}, this method MAY return multiple
110+
* entries for the same user ID if OIDCS authentication is enabled: once with
111+
* "db_*" and another time with "oidc" user type.
112+
*
113+
* @param roleName Role name.
114+
*/
115+
public CompletableFuture<List<UserAssignment>> userAssignments(String roleName) {
116+
return this.restTransport.performRequestAsync(new GetUserAssignementsRequest(roleName),
117+
GetUserAssignementsRequest._ENDPOINT);
118+
}
119+
120+
/**
121+
* Get IDs of all groups this role is assigned to along with their group type.
122+
*
123+
* @param roleName Role name.
124+
*/
125+
public CompletableFuture<List<GroupAssignment>> groupAssignments(String roleName) {
126+
return this.restTransport.performRequestAsync(new GetGroupAssignementsRequest(roleName),
127+
GetGroupAssignementsRequest._ENDPOINT);
128+
}
129+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package io.weaviate.client6.v1.api.rbac.users;
2+
3+
import java.io.IOException;
4+
import java.util.List;
5+
import java.util.Optional;
6+
import java.util.concurrent.CompletableFuture;
7+
import java.util.function.Function;
8+
9+
import io.weaviate.client6.v1.internal.ObjectBuilder;
10+
import io.weaviate.client6.v1.internal.rest.RestTransport;
11+
12+
public class DbUsersClientAsync extends NamespacedUsersClientAsync {
13+
14+
public DbUsersClientAsync(RestTransport restTransport) {
15+
super(restTransport, UserType.DB_USER);
16+
}
17+
18+
/**
19+
* Create a new "db" user.
20+
*
21+
* @param userId User ID.
22+
*/
23+
public CompletableFuture<Void> create(String userId) throws IOException {
24+
return this.restTransport.performRequestAsync(new CreateDbUserRequest(userId), CreateDbUserRequest._ENDPOINT);
25+
}
26+
27+
/**
28+
* Delete a "db" user.
29+
*
30+
* @param userId User ID.
31+
*/
32+
public CompletableFuture<Void> delete(String userId) throws IOException {
33+
return this.restTransport.performRequestAsync(new DeleteDbUserRequest(userId), DeleteDbUserRequest._ENDPOINT);
34+
}
35+
36+
/**
37+
* Activate a "db" user.
38+
*
39+
* @param userId User ID.
40+
*/
41+
public CompletableFuture<Void> activate(String userId) throws IOException {
42+
return this.restTransport.performRequestAsync(new ActivateDbUserRequest(userId), ActivateDbUserRequest._ENDPOINT);
43+
}
44+
45+
/**
46+
* Deactivate a "db" user.
47+
*
48+
* @param userId User ID.
49+
*/
50+
public CompletableFuture<Void> deactivate(String userId) throws IOException {
51+
return this.restTransport.performRequestAsync(new DeactivateDbUserRequest(userId),
52+
DeactivateDbUserRequest._ENDPOINT);
53+
}
54+
55+
/**
56+
* Rotate API key of the "db" user.
57+
*
58+
* @param userId User ID.
59+
*/
60+
public CompletableFuture<String> rotateKey(String userId) throws IOException {
61+
return this.restTransport.performRequestAsync(new RotateDbUserKeyRequest(userId), RotateDbUserKeyRequest._ENDPOINT);
62+
}
63+
64+
/**
65+
* Fetch "db" user info.
66+
*
67+
* @param userId User ID.
68+
*/
69+
public CompletableFuture<Optional<DbUser>> byName(String userId) throws IOException {
70+
return this.restTransport.performRequestAsync(GetDbUserRequest.of(userId), GetDbUserRequest._ENDPOINT);
71+
}
72+
73+
/**
74+
* Fetch "db" user info.
75+
*
76+
* @param userId User ID.
77+
* @param fn Lambda expression for optional parameters.
78+
*/
79+
public CompletableFuture<Optional<DbUser>> byName(String userId,
80+
Function<GetDbUserRequest.Builder, ObjectBuilder<GetDbUserRequest>> fn)
81+
throws IOException {
82+
return this.restTransport.performRequestAsync(GetDbUserRequest.of(userId, fn), GetDbUserRequest._ENDPOINT);
83+
}
84+
85+
/** List all "db" users. */
86+
public CompletableFuture<List<DbUser>> list()
87+
throws IOException {
88+
return this.restTransport.performRequestAsync(ListDbUsersRequest.of(), ListDbUsersRequest._ENDPOINT);
89+
}
90+
91+
/**
92+
* List all "db" users.
93+
*
94+
* @param fn Lambda expression for optional parameters.
95+
*/
96+
public CompletableFuture<List<DbUser>> list(
97+
Function<ListDbUsersRequest.Builder, ObjectBuilder<ListDbUsersRequest>> fn)
98+
throws IOException {
99+
return this.restTransport.performRequestAsync(ListDbUsersRequest.of(fn), ListDbUsersRequest._ENDPOINT);
100+
}
101+
}

0 commit comments

Comments
 (0)