Skip to content

Commit 4d26c49

Browse files
authored
Merge pull request #456 from weaviate/feat/groups-oidc
feat: add 'groups' namespace
2 parents e311151 + 9f44e94 commit 4d26c49

32 files changed

Lines changed: 951 additions & 13 deletions

src/main/java/io/weaviate/client/WeaviateClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.weaviate.client.v1.contextionary.Contextionary;
1919
import io.weaviate.client.v1.data.Data;
2020
import io.weaviate.client.v1.graphql.GraphQL;
21+
import io.weaviate.client.v1.groups.Groups;
2122
import io.weaviate.client.v1.grpc.GRPC;
2223
import io.weaviate.client.v1.misc.Misc;
2324
import io.weaviate.client.v1.misc.api.MetaGetter;
@@ -112,6 +113,10 @@ public Users users() {
112113
return new Users(httpClient, config);
113114
}
114115

116+
public Groups groups() {
117+
return new Groups(httpClient, config);
118+
}
119+
115120
public Aliases alias() {
116121
return new Aliases(httpClient, config);
117122
}

src/main/java/io/weaviate/client/base/Result.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,14 @@ public Result<R> parse(HttpResponse response, String body, ContentType contentTy
134134
* }</pre>
135135
*/
136136
public static <T> Result<List<T>> toList(Response<T[]> response) {
137-
return new Result<>(response, Arrays.asList(response.getBody()));
137+
return toList(response, Function.identity());
138+
}
139+
140+
public static <T, R> Result<List<R>> toList(Response<T[]> response, Function<? super T, ? extends R> mapper) {
141+
List<R> mapped = Optional.ofNullable(response.getBody())
142+
.map(Arrays::asList).orElse(new ArrayList<>())
143+
.stream().map(mapper).collect(Collectors.toList());
144+
return new Result<>(response, mapped);
138145
}
139146

140147
/**
@@ -243,10 +250,10 @@ public static <T, R> ResponseParser<List<R>> arrayToListParser(Class<T[]> cls,
243250
@Override
244251
public Result<List<R>> parse(HttpResponse response, String body, ContentType contentType) {
245252
Response<T[]> resp = this.serializer.toResponse(response.getCode(), body, cls);
246-
List<R> roles = Optional.ofNullable(resp.getBody())
253+
List<R> mapped = Optional.ofNullable(resp.getBody())
247254
.map(Arrays::asList).orElse(new ArrayList<>())
248255
.stream().map(mapper).collect(Collectors.toList());
249-
return new Result<>(resp.getStatusCode(), roles, resp.getErrors());
256+
return new Result<>(resp.getStatusCode(), mapped, resp.getErrors());
250257
}
251258
};
252259
}

src/main/java/io/weaviate/client/v1/async/WeaviateAsyncClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.weaviate.client.v1.async.cluster.Cluster;
2020
import io.weaviate.client.v1.async.data.Data;
2121
import io.weaviate.client.v1.async.graphql.GraphQL;
22+
import io.weaviate.client.v1.async.groups.Groups;
2223
import io.weaviate.client.v1.async.misc.Misc;
2324
import io.weaviate.client.v1.async.rbac.Roles;
2425
import io.weaviate.client.v1.async.schema.Schema;
@@ -85,6 +86,10 @@ public Users users() {
8586
return new Users(client, config, tokenProvider);
8687
}
8788

89+
public Groups groups() {
90+
return new Groups(client, config, tokenProvider);
91+
}
92+
8893
public Aliases alias() {
8994
return new Aliases(client, config, tokenProvider);
9095
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.weaviate.client.v1.async.groups;
2+
3+
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
4+
5+
import io.weaviate.client.Config;
6+
import io.weaviate.client.v1.auth.provider.AccessTokenProvider;
7+
import lombok.RequiredArgsConstructor;
8+
9+
@RequiredArgsConstructor
10+
public class Groups {
11+
private final CloseableHttpAsyncClient client;
12+
private final Config config;
13+
private final AccessTokenProvider tokenProvider;
14+
15+
public OidcGroups oidc() {
16+
return new OidcGroups(client, config, tokenProvider);
17+
}
18+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.weaviate.client.v1.async.groups;
2+
3+
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
4+
5+
import io.weaviate.client.Config;
6+
import io.weaviate.client.v1.async.groups.api.oidc.AssignedRolesGetter;
7+
import io.weaviate.client.v1.async.groups.api.oidc.KnownGroupNamesGetter;
8+
import io.weaviate.client.v1.async.groups.api.oidc.RoleAssigner;
9+
import io.weaviate.client.v1.async.groups.api.oidc.RoleRevoker;
10+
import io.weaviate.client.v1.auth.provider.AccessTokenProvider;
11+
import lombok.RequiredArgsConstructor;
12+
13+
@RequiredArgsConstructor
14+
public class OidcGroups {
15+
private final CloseableHttpAsyncClient client;
16+
private final Config config;
17+
private final AccessTokenProvider tokenProvider;
18+
19+
public RoleAssigner roleAssigner() {
20+
return new RoleAssigner(client, config, tokenProvider);
21+
}
22+
23+
public RoleRevoker roleRevoker() {
24+
return new RoleRevoker(client, config, tokenProvider);
25+
}
26+
27+
public AssignedRolesGetter assignedRolesGetter() {
28+
return new AssignedRolesGetter(client, config, tokenProvider);
29+
}
30+
31+
public KnownGroupNamesGetter knownGroupNamesGetter() {
32+
return new KnownGroupNamesGetter(client, config, tokenProvider);
33+
}
34+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.weaviate.client.v1.async.groups.api.oidc;
2+
3+
import java.util.List;
4+
import java.util.concurrent.Future;
5+
6+
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
7+
import org.apache.hc.core5.concurrent.FutureCallback;
8+
9+
import io.weaviate.client.Config;
10+
import io.weaviate.client.base.AsyncBaseClient;
11+
import io.weaviate.client.base.AsyncClientResult;
12+
import io.weaviate.client.base.Result;
13+
import io.weaviate.client.base.util.UrlEncoder;
14+
import io.weaviate.client.v1.auth.provider.AccessTokenProvider;
15+
import io.weaviate.client.v1.rbac.api.WeaviateRole;
16+
import io.weaviate.client.v1.rbac.model.Role;
17+
18+
public class AssignedRolesGetter extends AsyncBaseClient<List<Role>> implements AsyncClientResult<List<Role>> {
19+
private String groupId;
20+
private boolean includePermissions = false;
21+
22+
public AssignedRolesGetter(CloseableHttpAsyncClient httpClient, Config config, AccessTokenProvider tokenProvider) {
23+
super(httpClient, config, tokenProvider);
24+
}
25+
26+
public AssignedRolesGetter withGroupId(String id) {
27+
this.groupId = id;
28+
return this;
29+
}
30+
31+
public AssignedRolesGetter includePermissions(boolean include) {
32+
this.includePermissions = include;
33+
return this;
34+
}
35+
36+
private String encodeGroupId() {
37+
return UrlEncoder.encode(this.groupId);
38+
}
39+
40+
@Override
41+
public Future<Result<List<Role>>> run(FutureCallback<Result<List<Role>>> callback) {
42+
return sendGetRequest(path(), callback, Result.arrayToListParser(WeaviateRole[].class, WeaviateRole::toRole));
43+
}
44+
45+
private String path() {
46+
return String.format("/authz/groups/%s/roles/oidc?includeFullRoles=%s", encodeGroupId(), includePermissions);
47+
}
48+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.weaviate.client.v1.async.groups.api.oidc;
2+
3+
import java.util.List;
4+
import java.util.concurrent.Future;
5+
6+
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
7+
import org.apache.hc.core5.concurrent.FutureCallback;
8+
9+
import io.weaviate.client.Config;
10+
import io.weaviate.client.base.AsyncBaseClient;
11+
import io.weaviate.client.base.AsyncClientResult;
12+
import io.weaviate.client.base.Result;
13+
import io.weaviate.client.v1.aliases.model.Alias;
14+
import io.weaviate.client.v1.auth.provider.AccessTokenProvider;
15+
16+
public class KnownGroupNamesGetter extends AsyncBaseClient<List<String>> implements AsyncClientResult<List<String>> {
17+
18+
public KnownGroupNamesGetter(CloseableHttpAsyncClient httpClient, Config config, AccessTokenProvider tokenProvider) {
19+
super(httpClient, config, tokenProvider);
20+
}
21+
22+
static class ResponseBody {
23+
List<Alias> aliases;
24+
}
25+
26+
@Override
27+
public Future<Result<List<String>>> run(FutureCallback<Result<List<String>>> callback) {
28+
return sendGetRequest("/authz/groups/oidc", callback, Result.arrayToListParser(String[].class));
29+
}
30+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.weaviate.client.v1.async.groups.api.oidc;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.concurrent.Future;
7+
8+
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
9+
import org.apache.hc.core5.concurrent.FutureCallback;
10+
11+
import com.google.gson.annotations.SerializedName;
12+
13+
import io.weaviate.client.Config;
14+
import io.weaviate.client.base.AsyncBaseClient;
15+
import io.weaviate.client.base.AsyncClientResult;
16+
import io.weaviate.client.base.Result;
17+
import io.weaviate.client.base.util.UrlEncoder;
18+
import io.weaviate.client.v1.auth.provider.AccessTokenProvider;
19+
import lombok.AllArgsConstructor;
20+
21+
public class RoleAssigner extends AsyncBaseClient<Boolean> implements AsyncClientResult<Boolean> {
22+
private String groupId;
23+
private List<String> roles = new ArrayList<>();
24+
25+
public RoleAssigner(CloseableHttpAsyncClient httpClient, Config config, AccessTokenProvider tokenProvider) {
26+
super(httpClient, config, tokenProvider);
27+
}
28+
29+
public RoleAssigner withGroupId(String id) {
30+
this.groupId = id;
31+
return this;
32+
}
33+
34+
public RoleAssigner witRoles(String... roles) {
35+
this.roles = Arrays.asList(roles);
36+
return this;
37+
}
38+
39+
private String encodeGroupId() {
40+
return UrlEncoder.encode(this.groupId);
41+
}
42+
43+
/** The API signature for this method is { "roles": [...] } */
44+
@AllArgsConstructor
45+
private class Body {
46+
@SerializedName("roles")
47+
final List<String> roles;
48+
@SerializedName("groupType")
49+
final String groupType = "oidc";
50+
}
51+
52+
@Override
53+
public Future<Result<Boolean>> run(FutureCallback<Result<Boolean>> callback) {
54+
return sendPostRequest(path(), new Body(this.roles), callback,
55+
Result.voidToBooleanParser());
56+
}
57+
58+
private String path() {
59+
return String.format("/authz/groups/%s/assign", encodeGroupId());
60+
}
61+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.weaviate.client.v1.async.groups.api.oidc;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.concurrent.Future;
7+
8+
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
9+
import org.apache.hc.core5.concurrent.FutureCallback;
10+
11+
import com.google.gson.annotations.SerializedName;
12+
13+
import io.weaviate.client.Config;
14+
import io.weaviate.client.base.AsyncBaseClient;
15+
import io.weaviate.client.base.AsyncClientResult;
16+
import io.weaviate.client.base.Result;
17+
import io.weaviate.client.base.util.UrlEncoder;
18+
import io.weaviate.client.v1.auth.provider.AccessTokenProvider;
19+
import lombok.AllArgsConstructor;
20+
21+
public class RoleRevoker extends AsyncBaseClient<Boolean> implements AsyncClientResult<Boolean> {
22+
private String groupId;
23+
private List<String> roles = new ArrayList<>();
24+
25+
public RoleRevoker(CloseableHttpAsyncClient httpClient, Config config, AccessTokenProvider tokenProvider) {
26+
super(httpClient, config, tokenProvider);
27+
}
28+
29+
public RoleRevoker withGroupId(String id) {
30+
this.groupId = id;
31+
return this;
32+
}
33+
34+
public RoleRevoker witRoles(String... roles) {
35+
this.roles = Arrays.asList(roles);
36+
return this;
37+
}
38+
39+
private String encodeGroupId() {
40+
return UrlEncoder.encode(this.groupId);
41+
}
42+
43+
/** The API signature for this method is { "roles": [...] } */
44+
@AllArgsConstructor
45+
private class Body {
46+
@SerializedName("roles")
47+
final List<String> roles;
48+
@SerializedName("groupType")
49+
final String groupType = "oidc";
50+
}
51+
52+
@Override
53+
public Future<Result<Boolean>> run(FutureCallback<Result<Boolean>> callback) {
54+
return sendPostRequest(path(), new Body(this.roles), callback,
55+
Result.voidToBooleanParser());
56+
}
57+
58+
private String path() {
59+
return String.format("/authz/groups/%s/revoke", encodeGroupId());
60+
}
61+
}

src/main/java/io/weaviate/client/v1/async/rbac/Roles.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import io.weaviate.client.Config;
66
import io.weaviate.client.v1.async.rbac.api.AssignedUsersGetter;
7+
import io.weaviate.client.v1.async.rbac.api.GroupAssignmentsGetter;
78
import io.weaviate.client.v1.async.rbac.api.PermissionAdder;
89
import io.weaviate.client.v1.async.rbac.api.PermissionChecker;
910
import io.weaviate.client.v1.async.rbac.api.PermissionRemover;
@@ -64,7 +65,7 @@ public RoleAllGetter allGetter() {
6465
/** Get role and its assiciated permissions. */
6566
public RoleGetter getter() {
6667
return new RoleGetter(client, config, tokenProvider);
67-
};
68+
}
6869

6970
/**
7071
* Get users assigned to a role.
@@ -74,7 +75,7 @@ public RoleGetter getter() {
7475
@Deprecated
7576
public AssignedUsersGetter assignedUsersGetter() {
7677
return new AssignedUsersGetter(client, config, tokenProvider);
77-
};
78+
}
7879

7980
/**
8081
* Get role assignments.
@@ -89,7 +90,11 @@ public AssignedUsersGetter assignedUsersGetter() {
8990
*/
9091
public UserAssignmentsGetter userAssignmentsGetter() {
9192
return new UserAssignmentsGetter(client, config, tokenProvider);
92-
};
93+
}
94+
95+
public GroupAssignmentsGetter groupAssignmentsGetter() {
96+
return new GroupAssignmentsGetter(client, config, tokenProvider);
97+
}
9398

9499
/** Check if a role exists. */
95100
public RoleExists exists() {

0 commit comments

Comments
 (0)