Skip to content

Commit 2a0b999

Browse files
committed
test: move RBAC configuration to a separate container class
1 parent f3e0986 commit 2a0b999

File tree

4 files changed

+47
-52
lines changed

4 files changed

+47
-52
lines changed

src/main/java/io/weaviate/client/v1/rbac/model/RbacAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
interface RbacAction {
2525
String getValue();
2626

27-
static <E extends Enum<E> & RbacAction> E fromString(Class<E> enumClass, String value) {
27+
static <E extends Enum<E> & RbacAction> E fromString(Class<E> enumClass, String value)
28+
throws IllegalArgumentException {
2829
for (E action : enumClass.getEnumConstants()) {
2930
if (action.getValue().equals(value)) {
3031
return action;

src/test/java/io/weaviate/integration/client/WeaviateDockerCompose.java

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,19 @@ public class WeaviateDockerCompose implements TestRule {
1818
private final String weaviateVersion;
1919
private final boolean withOffloadS3;
2020

21-
/** Username of the admin user for instances using RBAC. */
22-
private final String adminUser;
23-
2421
public WeaviateDockerCompose() {
2522
this.weaviateVersion = WeaviateDockerImage.WEAVIATE_DOCKER_IMAGE;
2623
this.withOffloadS3 = false;
27-
this.adminUser = null;
2824
}
2925

3026
public WeaviateDockerCompose(String version) {
3127
this.weaviateVersion = String.format("semitechnologies/weaviate:%s", version);
3228
this.withOffloadS3 = false;
33-
this.adminUser = null;
3429
}
3530

3631
public WeaviateDockerCompose(String version, boolean withOffloadS3) {
3732
this.weaviateVersion = String.format("semitechnologies/weaviate:%s", version);
3833
this.withOffloadS3 = withOffloadS3;
39-
this.adminUser = null;
40-
}
41-
42-
public WeaviateDockerCompose(String version, String adminUser) {
43-
this.weaviateVersion = WeaviateDockerImage.WEAVIATE_DOCKER_IMAGE;
44-
this.withOffloadS3 = false;
45-
this.adminUser = adminUser;
46-
}
47-
48-
/** Create docker-compose deployment with auth and RBAC-authz enabled. */
49-
public static WeaviateDockerCompose rbac(String adminUser) {
50-
return new WeaviateDockerCompose(WeaviateDockerImage.WEAVIATE_DOCKER_IMAGE, adminUser);
5134
}
5235

5336
public static class Weaviate extends WeaviateContainer {
@@ -76,27 +59,6 @@ public Weaviate(String dockerImageName, boolean withOffloadS3) {
7659
withEnv("ENABLE_MODULES", String.join(",", enableModules));
7760
withCreateContainerCmdModifier(cmd -> cmd.withHostName("weaviate"));
7861
}
79-
80-
/** Create Weaviate container with RBAC authz and an admin user. */
81-
public Weaviate(String dockerImageName, boolean withOffloadS3, String adminUser) {
82-
this(dockerImageName, withOffloadS3);
83-
withEnv("AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED", "false");
84-
withEnv("AUTHENTICATION_APIKEY_ENABLED", "true");
85-
withEnv("AUTHORIZATION_RBAC_ENABLED", "true");
86-
withEnv("AUTHENTICATION_APIKEY_USERS", adminUser);
87-
withEnv("AUTHENTICATION_APIKEY_ALLOWED_KEYS", makeSecret(adminUser));
88-
withEnv("AUTHORIZATION_ADMIN_USERS", adminUser);
89-
}
90-
91-
/**
92-
* Generate API secret for a username. When running an instance with
93-
* authentication enabled, {@link Weaviate} will use this method to generate
94-
* secrets for all users.
95-
* Use this method to get a valid API key for a test client.
96-
*/
97-
public static String makeSecret(String user) {
98-
return user + "-secret";
99-
}
10062
}
10163

10264
public static class Contextionary extends GenericContainer<Contextionary> {
@@ -138,11 +100,7 @@ public void start() {
138100
}
139101
contextionary = new Contextionary();
140102
contextionary.start();
141-
if (adminUser == null) {
142-
weaviate = new Weaviate(this.weaviateVersion, this.withOffloadS3);
143-
} else {
144-
weaviate = new Weaviate(this.weaviateVersion, this.withOffloadS3, this.adminUser);
145-
}
103+
weaviate = new Weaviate(this.weaviateVersion, this.withOffloadS3);
146104
weaviate.start();
147105
}
148106

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.weaviate.integration.client;
2+
3+
import org.testcontainers.weaviate.WeaviateContainer;
4+
5+
public class WeaviateWithRbacContainer extends WeaviateContainer {
6+
7+
public WeaviateWithRbacContainer(String dockerImageName, String admin, String... viewers) {
8+
super(dockerImageName);
9+
10+
withEnv("AUTHENTICATION_ANONYMOUS_ACCESS_ENABLED", "false");
11+
withEnv("AUTHENTICATION_APIKEY_ENABLED", "true");
12+
withEnv("AUTHORIZATION_RBAC_ENABLED", "true");
13+
withEnv("AUTHENTICATION_APIKEY_ALLOWED_KEYS", makeSecret(admin));
14+
withEnv("AUTHENTICATION_APIKEY_USERS", admin);
15+
withEnv("AUTHORIZATION_ADMIN_USERS", admin);
16+
withEnv("PERSISTENCE_DATA_PATH", "./data");
17+
withEnv("BACKUP_FILESYSTEM_PATH", "/tmp/backups");
18+
withEnv("ENABLE_MODULES", "backup-filesystem");
19+
withEnv("CLUSTER_GOSSIP_BIND_PORT", "7100");
20+
withEnv("CLUSTER_DATA_BIND_PORT", "7101");
21+
22+
if (viewers.length > 0) {
23+
withEnv("AUTHORIZATION_VIEWER_USERS", String.join(",", viewers));
24+
}
25+
}
26+
27+
/**
28+
* Generate API secret for a username. When running an instance with
29+
* authentication enabled, {@link WeaviateWithRbacContainer} will use this
30+
* method to generate secrets for all users.
31+
* Use this method to get a valid API key for a test client.
32+
*/
33+
public static String makeSecret(String user) {
34+
return user + "-secret";
35+
}
36+
}

src/test/java/io/weaviate/integration/tests/rbac/ClientRbacTestSuite.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.junit.jupiter.api.Assertions;
1717
import org.junit.rules.TestName;
1818
import org.junit.runner.RunWith;
19+
import org.testcontainers.weaviate.WeaviateContainer;
1920

2021
import com.jparams.junit4.JParamsTestRunner;
2122
import com.jparams.junit4.data.DataMethod;
@@ -32,25 +33,27 @@
3233
import io.weaviate.client.v1.rbac.model.Role;
3334
import io.weaviate.client.v1.rbac.model.RolesPermission;
3435
import io.weaviate.client.v1.rbac.model.TenantsPermission;
35-
import io.weaviate.integration.client.WeaviateDockerCompose;
36-
import io.weaviate.integration.client.WeaviateDockerCompose.Weaviate;
36+
import io.weaviate.integration.client.WeaviateDockerImage;
37+
import io.weaviate.integration.client.WeaviateWithRbacContainer;
3738

3839
@RunWith(JParamsTestRunner.class)
3940
public class ClientRbacTestSuite {
4041

4142
private static final String adminRole = "admin";
4243
private static final String viewerRole = "viewer";
4344
private static final String adminUser = "john-doe";
44-
private static final String API_KEY = Weaviate.makeSecret(adminUser);
45+
private static final String API_KEY = WeaviateWithRbacContainer.makeSecret(adminUser);
4546

4647
@Rule
4748
public TestName currentTest = new TestName();
4849

4950
@ClassRule
50-
public static WeaviateDockerCompose compose = WeaviateDockerCompose.rbac(adminUser);
51+
public static WeaviateContainer weaviate = new WeaviateWithRbacContainer(
52+
WeaviateDockerImage.WEAVIATE_DOCKER_IMAGE,
53+
adminUser);
5154

5255
public static Config config() {
53-
return new Config("http", compose.getHttpHostAddress());
56+
return new Config("http", weaviate.getHttpHostAddress());
5457
}
5558

5659
public static Object[][] clients() {
@@ -113,9 +116,6 @@ public void testGetAssignedUsers(Supplier<Rbac> rbac) {
113116
assertEquals(adminUser, users.get(0), "wrong user assinged to " + adminRole + " role");
114117
}
115118

116-
// TODO: check if I can create a role with a name that's not a valid URL
117-
// paramter
118-
119119
/**
120120
* Created role should have all of the permissions it was created with.
121121
* Tests addition and fetching the role to.

0 commit comments

Comments
 (0)