Skip to content

Commit df406cb

Browse files
authored
Merge pull request #438 from weaviate/v6-oidc
2 parents 5fc538a + 5a21081 commit df406cb

42 files changed

Lines changed: 1747 additions & 149 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ Official Weaviate Java Client.
99
To start using Weaviate Java Client add the dependency to `pom.xml`:
1010

1111
```xml
12-
1312
<dependency>
1413
<groupId>io.weaviate</groupId>
1514
<artifactId>client6</artifactId>
@@ -19,9 +18,18 @@ To start using Weaviate Java Client add the dependency to `pom.xml`:
1918

2019
### Uber JAR🫙
2120

22-
If you're building a uber-JAR with something like `maven-assembly-plugin`, use a shaded version with classifier `all`.
21+
If you're building an uber-JAR with something like `maven-assembly-plugin`, use a shaded version with classifier `all`.
2322
This ensures that all dynamically-loaded dependecies of `io.grpc` are resolved correctly.
2423

24+
```xml
25+
<dependency>
26+
<groupId>io.weaviate</groupId>
27+
<artifactId>client6</artifactId>
28+
<version>6.0.0-beta4</version>
29+
<classifier>all</classifier>
30+
</dependency>
31+
```
32+
2533
### SNAPSHOT releases
2634

2735
The latest development version of `client6` is released after every merged pull request. To include it in you project set the version to `6.0.0-SNAPSHOT` and [configure your `<repositories>` section accordingly](https://central.sonatype.org/publish/publish-portal-snapshots/#consuming-snapshot-releases-for-your-project).

src/it/java/io/weaviate/containers/Container.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ public class Container {
1818
public static final Img2VecNeural IMG2VEC_NEURAL = Img2VecNeural.createDefault();
1919
public static final MinIo MINIO = MinIo.createDefault();
2020

21-
static {
22-
startAll();
23-
}
24-
25-
/** Start all shared Testcontainers. */
26-
// TODO: start lazily!
27-
static void startAll() {
28-
// WEAVIATE.start();
29-
}
30-
3121
/**
3222
* Stop all shared Testcontainers created in {@link #startAll}.
3323
* <p>

src/it/java/io/weaviate/containers/Weaviate.java

Lines changed: 97 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,81 @@
66
import java.util.HashSet;
77
import java.util.Map;
88
import java.util.Set;
9+
import java.util.function.Function;
910

1011
import org.testcontainers.weaviate.WeaviateContainer;
1112

13+
import io.weaviate.client6.v1.api.Config;
1214
import io.weaviate.client6.v1.api.WeaviateClient;
15+
import io.weaviate.client6.v1.internal.ObjectBuilder;
1316

1417
public class Weaviate extends WeaviateContainer {
15-
private WeaviateClient clientInstance;
16-
17-
public static final String VERSION = "1.29.1";
18+
public static final String VERSION = "1.32.3";
1819
public static final String DOCKER_IMAGE = "semitechnologies/weaviate";
1920

21+
private volatile SharedClient clientInstance;
22+
23+
public WeaviateClient getClient() {
24+
return getClient(ObjectBuilder.identity());
25+
}
26+
2027
/**
21-
* Get a client for the current Weaviate container.
22-
* As we aren't running tests in parallel at the moment,
23-
* this is not made thread-safe.
28+
* Create a new instance of WeaviateClient connected to this container if none
29+
* exist. Get an existing client otherwise.
30+
*
31+
* The lifetime of this client is tied to that of its container, which means
32+
* that you do not need to {@code close} it manually. It will only truly close
33+
* after the parent Testcontainer is stopped.
2434
*/
25-
public WeaviateClient getClient() {
26-
// FIXME: control from containers?
35+
public WeaviateClient getClient(Function<Config.Custom, ObjectBuilder<Config>> fn) {
2736
if (!isRunning()) {
2837
start();
2938
}
30-
if (clientInstance == null) {
31-
try {
32-
clientInstance = WeaviateClient.local(
39+
if (clientInstance != null) {
40+
return clientInstance;
41+
}
42+
43+
synchronized (this) {
44+
if (clientInstance == null) {
45+
var host = getHost();
46+
var customFn = ObjectBuilder.partial(fn,
3347
conn -> conn
34-
.host(getHost())
48+
.scheme("http")
49+
.httpHost(host)
50+
.grpcHost(host)
3551
.httpPort(getMappedPort(8080))
3652
.grpcPort(getMappedPort(50051)));
37-
} catch (Exception e) {
38-
throw new RuntimeException("create WeaviateClient for Weaviate container", e);
53+
var config = customFn.apply(new Config.Custom()).build();
54+
try {
55+
clientInstance = new SharedClient(config, this);
56+
} catch (Exception e) {
57+
throw new RuntimeException("create WeaviateClient for Weaviate container", e);
58+
}
3959
}
4060
}
4161
return clientInstance;
4262
}
4363

64+
/**
65+
* Create a new instance of WeaviateClient connected to this container.
66+
* Prefer using {@link #getClient} unless your test needs the initialization
67+
* steps to run, e.g. OIDC authorization grant exchange.
68+
*/
69+
public WeaviateClient getNewClient(Function<Config.Custom, ObjectBuilder<Config>> fn) {
70+
if (!isRunning()) {
71+
start();
72+
}
73+
var host = getHost();
74+
var customFn = ObjectBuilder.partial(fn,
75+
conn -> conn
76+
.scheme("http")
77+
.httpHost(host)
78+
.grpcHost(host)
79+
.httpPort(getMappedPort(8080))
80+
.grpcPort(getMappedPort(50051)));
81+
return WeaviateClient.custom(customFn);
82+
}
83+
4484
public static Weaviate createDefault() {
4585
return new Builder().build();
4686
}
@@ -99,20 +139,32 @@ public Builder withOffloadS3(String accessKey, String secretKey) {
99139
}
100140

101141
public Builder enableTelemetry(boolean enable) {
102-
telemetry = enable;
142+
environment.put("DISABLE_TELEMETRY", Boolean.toString(!enable));
143+
return this;
144+
}
145+
146+
public Builder enableAnonymousAccess(boolean enable) {
147+
environment.put("AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED", Boolean.toString(enable));
148+
return this;
149+
}
150+
151+
public Builder withOIDC(String clientId, String issuer, String usernameClaim, String groupsClaim) {
152+
enableAnonymousAccess(false);
153+
environment.put("AUTHENTICATION_OIDC_ENABLED", "true");
154+
environment.put("AUTHENTICATION_OIDC_CLIENT_ID", clientId);
155+
environment.put("AUTHENTICATION_OIDC_ISSUER", issuer);
156+
environment.put("AUTHENTICATION_OIDC_USERNAME_CLAIM", usernameClaim);
157+
environment.put("AUTHENTICATION_OIDC_GROUPS_CLAIM", groupsClaim);
103158
return this;
104159
}
105160

106161
public Weaviate build() {
107162
var c = new Weaviate(DOCKER_IMAGE + ":" + versionTag);
108163

109164
if (!enableModules.isEmpty()) {
110-
c.withEnv("ENABLE_API_BASED_MODULES", "'true'");
165+
c.withEnv("ENABLE_API_BASED_MODULES", Boolean.TRUE.toString());
111166
c.withEnv("ENABLE_MODULES", String.join(",", enableModules));
112167
}
113-
if (!telemetry) {
114-
c.withEnv("DISABLE_TELEMETRY", "true");
115-
}
116168

117169
environment.forEach((name, value) -> c.withEnv(name, value));
118170
c.withCreateContainerCmdModifier(cmd -> cmd.withHostName("weaviate"));
@@ -134,10 +186,32 @@ public void stop() {
134186
if (clientInstance == null) {
135187
return;
136188
}
137-
try {
138-
clientInstance.close();
139-
} catch (IOException e) {
140-
// TODO: log error
189+
synchronized (this) {
190+
try {
191+
clientInstance.close(this);
192+
} catch (Exception e) {
193+
throw new RuntimeException(e);
194+
}
195+
}
196+
}
197+
198+
/** SharedClient's lifetime is tied to that of it's parent container. */
199+
private class SharedClient extends WeaviateClient {
200+
private final Weaviate parent;
201+
202+
private SharedClient(Config config, Weaviate parent) {
203+
super(config);
204+
this.parent = parent;
205+
}
206+
207+
private void close(Weaviate caller) throws Exception {
208+
if (caller == parent) {
209+
super.close();
210+
}
211+
}
212+
213+
@Override
214+
public void close() throws IOException {
141215
}
142216
}
143217
}

0 commit comments

Comments
 (0)