Skip to content

Commit 2c0a701

Browse files
committed
Moved user.indexMatcher() to user.reference()
Signed-off-by: Nils Bandener <[email protected]>
1 parent e3cc113 commit 2c0a701

File tree

9 files changed

+507
-443
lines changed

9 files changed

+507
-443
lines changed

src/integrationTest/java/org/opensearch/security/privileges/int_tests/DataStreamAuthorizationReadOnlyIntTests.java

Lines changed: 60 additions & 51 deletions
Large diffs are not rendered by default.

src/integrationTest/java/org/opensearch/security/privileges/int_tests/DataStreamAuthorizationReadWriteIntTests.java

Lines changed: 101 additions & 71 deletions
Large diffs are not rendered by default.

src/integrationTest/java/org/opensearch/security/privileges/int_tests/IndexAuthorizationReadOnlyIntTests.java

Lines changed: 140 additions & 116 deletions
Large diffs are not rendered by default.

src/integrationTest/java/org/opensearch/security/privileges/int_tests/IndexAuthorizationReadWriteIntTests.java

Lines changed: 153 additions & 143 deletions
Large diffs are not rendered by default.

src/integrationTest/java/org/opensearch/security/privileges/int_tests/SnapshotAuthorizationIntTests.java

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@
3030
import org.opensearch.test.framework.TestSecurityConfig;
3131
import org.opensearch.test.framework.cluster.LocalCluster;
3232
import org.opensearch.test.framework.cluster.TestRestClient;
33+
import org.opensearch.test.framework.matcher.RestIndexMatchers;
3334

3435
import static org.hamcrest.MatcherAssert.assertThat;
3536
import static org.opensearch.test.framework.TestSecurityConfig.AuthcDomain.AUTHC_HTTPBASIC_INTERNAL;
3637
import static org.opensearch.test.framework.cluster.TestRestClient.json;
37-
import static org.opensearch.test.framework.matcher.IndexApiResponseMatchers.OnResponseIndexMatcher.containsExactly;
38-
import static org.opensearch.test.framework.matcher.IndexApiResponseMatchers.OnUserIndexMatcher.limitedTo;
39-
import static org.opensearch.test.framework.matcher.IndexApiResponseMatchers.OnUserIndexMatcher.limitedToNone;
40-
import static org.opensearch.test.framework.matcher.IndexApiResponseMatchers.OnUserIndexMatcher.unlimitedIncludingOpenSearchSecurityIndex;
38+
import static org.opensearch.test.framework.matcher.RestIndexMatchers.OnResponseIndexMatcher.containsExactly;
39+
import static org.opensearch.test.framework.matcher.RestIndexMatchers.OnUserIndexMatcher.limitedTo;
40+
import static org.opensearch.test.framework.matcher.RestIndexMatchers.OnUserIndexMatcher.limitedToNone;
41+
import static org.opensearch.test.framework.matcher.RestIndexMatchers.OnUserIndexMatcher.unlimitedIncludingOpenSearchSecurityIndex;
4142
import static org.opensearch.test.framework.matcher.RestMatchers.isForbidden;
4243
import static org.opensearch.test.framework.matcher.RestMatchers.isOk;
4344

@@ -65,6 +66,23 @@ public class SnapshotAuthorizationIntTests {
6566
static final TestIndex index_bwx1 = TestIndex.name("index_bwx1").documentCount(10).seed(13).build(); // not initially created
6667
static final TestIndex index_bwx2 = TestIndex.name("index_bwx2").documentCount(10).seed(14).build(); // not initially created
6768

69+
/**
70+
* This key identifies assertion reference data for index search/read permissions of individual users.
71+
*/
72+
static final TestSecurityConfig.User.MetadataKey<RestIndexMatchers.IndexMatcher> READ = new TestSecurityConfig.User.MetadataKey<>(
73+
"read",
74+
RestIndexMatchers.IndexMatcher.class
75+
);
76+
77+
/**
78+
* This key identifies assertion reference data for index write permissions of individual users. This does
79+
* not include index creation permissions.
80+
*/
81+
static final TestSecurityConfig.User.MetadataKey<RestIndexMatchers.IndexMatcher> WRITE = new TestSecurityConfig.User.MetadataKey<>(
82+
"write",
83+
RestIndexMatchers.IndexMatcher.class
84+
);
85+
6886
static TestSecurityConfig.User LIMITED_USER_A = new TestSecurityConfig.User("limited_user_A")//
6987
.description("index_a*")//
7088
.roles(
@@ -75,8 +93,8 @@ public class SnapshotAuthorizationIntTests {
7593
.indexPermissions("write", "manage")
7694
.on("index_aw*")
7795
)//
78-
.indexMatcher("read", limitedTo(index_a1, index_a2, index_awx1, index_awx2))//
79-
.indexMatcher("write", limitedTo(index_awx1, index_awx2));
96+
.reference(READ, limitedTo(index_a1, index_a2, index_awx1, index_awx2))//
97+
.reference(WRITE, limitedTo(index_awx1, index_awx2));
8098

8199
static TestSecurityConfig.User LIMITED_USER_B = new TestSecurityConfig.User("limited_user_B")//
82100
.description("index_b*")//
@@ -88,8 +106,8 @@ public class SnapshotAuthorizationIntTests {
88106
.indexPermissions("write", "manage")
89107
.on("index_bw*")
90108
)//
91-
.indexMatcher("read", limitedTo(index_b1, index_b2, index_bwx1, index_bwx2))//
92-
.indexMatcher("write", limitedTo(index_bwx1, index_bwx2));
109+
.reference(READ, limitedTo(index_b1, index_b2, index_bwx1, index_bwx2))//
110+
.reference(WRITE, limitedTo(index_bwx1, index_bwx2));
93111

94112
static TestSecurityConfig.User LIMITED_USER_B_SYSTEM_INDEX = new TestSecurityConfig.User("limited_user_B_system_index")//
95113
.description("index_b*, .system_index_plugin")//
@@ -106,8 +124,8 @@ public class SnapshotAuthorizationIntTests {
106124
.on(".system_index_plugin_not_existing")
107125

108126
)//
109-
.indexMatcher("read", limitedTo(index_b1, index_b2, index_bwx1, index_bwx2))//
110-
.indexMatcher("write", limitedTo(index_bwx1, index_bwx2, system_index_plugin_not_existing));
127+
.reference(READ, limitedTo(index_b1, index_b2, index_bwx1, index_bwx2))//
128+
.reference(WRITE, limitedTo(index_bwx1, index_bwx2, system_index_plugin_not_existing));
111129

112130
static TestSecurityConfig.User LIMITED_USER_AB = new TestSecurityConfig.User("limited_user_AB")//
113131
.description("index_a*, index_b*")//
@@ -119,17 +137,17 @@ public class SnapshotAuthorizationIntTests {
119137
.indexPermissions("write", "manage")
120138
.on("index_aw*", "index_bw*")
121139
)//
122-
.indexMatcher("read", limitedTo(index_a1, index_a2, index_awx1, index_awx2, index_b1, index_b2, index_bwx1, index_bwx2))//
123-
.indexMatcher("write", limitedTo(index_awx1, index_awx2, index_bwx1, index_bwx2));
140+
.reference(READ, limitedTo(index_a1, index_a2, index_awx1, index_awx2, index_b1, index_b2, index_bwx1, index_bwx2))//
141+
.reference(WRITE, limitedTo(index_awx1, index_awx2, index_bwx1, index_bwx2));
124142

125143
static final TestSecurityConfig.User LIMITED_USER_NONE = new TestSecurityConfig.User("limited_user_none")//
126144
.description("no index privileges")//
127145
.roles(
128146
new TestSecurityConfig.Role("r1")//
129147
.clusterPermissions("cluster_composite_ops_ro", "cluster_monitor")
130148
)//
131-
.indexMatcher("read", limitedToNone())//
132-
.indexMatcher("write", limitedToNone());
149+
.reference(READ, limitedToNone())//
150+
.reference(WRITE, limitedToNone());
133151

134152
static final TestSecurityConfig.User UNLIMITED_USER = new TestSecurityConfig.User("unlimited_user")//
135153
.description("unlimited")//
@@ -140,12 +158,12 @@ public class SnapshotAuthorizationIntTests {
140158
.on("*")//
141159

142160
)//
143-
.indexMatcher(
144-
"read",
161+
.reference(
162+
READ,
145163
limitedTo(index_a1, index_a2, index_a3, index_awx1, index_awx2, index_b1, index_b2, index_b3, index_bwx1, index_bwx2)
146164
)//
147-
.indexMatcher(
148-
"write",
165+
.reference(
166+
WRITE,
149167
limitedTo(index_a1, index_a2, index_a3, index_awx1, index_awx2, index_b1, index_b2, index_b3, index_bwx1, index_bwx2)
150168
);
151169

@@ -156,8 +174,8 @@ public class SnapshotAuthorizationIntTests {
156174
static final TestSecurityConfig.User SUPER_UNLIMITED_USER = new TestSecurityConfig.User("super_unlimited_user")//
157175
.description("super unlimited (admin cert)")//
158176
.adminCertUser()//
159-
.indexMatcher("read", unlimitedIncludingOpenSearchSecurityIndex())//
160-
.indexMatcher("write", unlimitedIncludingOpenSearchSecurityIndex());
177+
.reference(READ, unlimitedIncludingOpenSearchSecurityIndex())//
178+
.reference(WRITE, unlimitedIncludingOpenSearchSecurityIndex());
161179

162180
static final List<TestSecurityConfig.User> USERS = ImmutableList.of(
163181
LIMITED_USER_A,
@@ -201,10 +219,7 @@ public void restore_singleIndex() throws Exception {
201219
"_snapshot/test_repository/single_index_snapshot/_restore?wait_for_completion=true"
202220
);
203221

204-
assertThat(
205-
httpResponse,
206-
containsExactly(index_awx1).at("snapshot.indices").butForbiddenIfIncomplete(user.indexMatcher("write"))
207-
);
222+
assertThat(httpResponse, containsExactly(index_awx1).at("snapshot.indices").butForbiddenIfIncomplete(user.reference(WRITE)));
208223

209224
} finally {
210225
delete("_snapshot/test_repository/single_index_snapshot");
@@ -223,10 +238,7 @@ public void restore_singleIndex_rename1() throws Exception {
223238
json("rename_pattern", "index_(.+)x1", "rename_replacement", "index_$1x2")
224239
);
225240

226-
assertThat(
227-
httpResponse,
228-
containsExactly(index_awx2).at("snapshot.indices").butForbiddenIfIncomplete(user.indexMatcher("write"))
229-
);
241+
assertThat(httpResponse, containsExactly(index_awx2).at("snapshot.indices").butForbiddenIfIncomplete(user.reference(WRITE)));
230242

231243
} finally {
232244
delete("_snapshot/test_repository/single_index_snapshot");
@@ -245,10 +257,7 @@ public void restore_singleIndex_rename2() throws Exception {
245257
json("rename_pattern", "index_a(.*)", "rename_replacement", "index_b$1")
246258
);
247259

248-
assertThat(
249-
httpResponse,
250-
containsExactly(index_bwx1).at("snapshot.indices").butForbiddenIfIncomplete(user.indexMatcher("write"))
251-
);
260+
assertThat(httpResponse, containsExactly(index_bwx1).at("snapshot.indices").butForbiddenIfIncomplete(user.reference(WRITE)));
252261

253262
} finally {
254263
delete("_snapshot/test_repository/single_index_snapshot");
@@ -270,8 +279,7 @@ public void restore_singleIndex_renameToSystemIndex() throws Exception {
270279
if (clusterConfig.systemIndexPrivilegeEnabled || user == SUPER_UNLIMITED_USER) {
271280
assertThat(
272281
httpResponse,
273-
containsExactly(system_index_plugin_not_existing).at("snapshot.indices")
274-
.butForbiddenIfIncomplete(user.indexMatcher("write"))
282+
containsExactly(system_index_plugin_not_existing).at("snapshot.indices").butForbiddenIfIncomplete(user.reference(WRITE))
275283
);
276284
} else {
277285
assertThat(httpResponse, isForbidden());
@@ -295,10 +303,7 @@ public void restore_singleIndexFromAllIndices() throws Exception {
295303
json("indices", "index_awx1")
296304
);
297305

298-
assertThat(
299-
httpResponse,
300-
containsExactly(index_awx1).at("snapshot.indices").butForbiddenIfIncomplete(user.indexMatcher("write"))
301-
);
306+
assertThat(httpResponse, containsExactly(index_awx1).at("snapshot.indices").butForbiddenIfIncomplete(user.reference(WRITE)));
302307

303308
} finally {
304309
delete("_snapshot/test_repository/all_index_snapshot");

src/integrationTest/java/org/opensearch/test/framework/TestDataStream.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public Builder rolloverAfter(int rolloverAfter) {
9595
return this;
9696
}
9797

98+
public Builder segmentCount(int segmentCount) {
99+
testDataBuilder.segmentCount(segmentCount);
100+
return this;
101+
}
102+
98103
public TestDataStream build() {
99104
if (testData == null) {
100105
testData = testDataBuilder.get();

src/integrationTest/java/org/opensearch/test/framework/TestIndex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public Builder setting(String name, int value) {
107107
}
108108

109109
public Builder shards(int value) {
110-
settings.put("index.number_of_shards", 5);
110+
settings.put("index.number_of_shards", value);
111111
return this;
112112
}
113113

src/integrationTest/java/org/opensearch/test/framework/TestSecurityConfig.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
import org.opensearch.security.securityconf.impl.v7.RoleV7;
7373
import org.opensearch.security.support.ConfigConstants;
7474
import org.opensearch.test.framework.cluster.OpenSearchClientProvider.UserCredentialsHolder;
75-
import org.opensearch.test.framework.matcher.IndexApiResponseMatchers;
75+
import org.opensearch.test.framework.matcher.RestIndexMatchers;
7676
import org.opensearch.transport.client.Client;
7777

7878
import static org.apache.http.HttpHeaders.AUTHORIZATION;
@@ -459,7 +459,7 @@ public static final class User implements UserCredentialsHolder, ToXContentObjec
459459
String requestedTenant;
460460
private Map<String, String> attributes = new HashMap<>();
461461
private Map<MetadataKey<?>, Object> matchers = new HashMap<>();
462-
private Map<String, IndexApiResponseMatchers.IndexMatcher> indexMatchers = new HashMap<>();
462+
private Map<String, RestIndexMatchers.IndexMatcher> indexMatchers = new HashMap<>();
463463
private boolean adminCertUser = false;
464464

465465
private Boolean hidden = null;
@@ -514,25 +514,6 @@ public User attr(String key, String value) {
514514
return this;
515515
}
516516

517-
/**
518-
* Associates an IndexMatcher with this test user. The IndexMatcher can be later used as a test oracle.
519-
* See IndexAuthorizationReadOnlyIntTests for examples.
520-
*/
521-
public User indexMatcher(String key, IndexApiResponseMatchers.IndexMatcher indexMatcher) {
522-
this.indexMatchers.put(key, indexMatcher);
523-
return this;
524-
}
525-
526-
public IndexApiResponseMatchers.IndexMatcher indexMatcher(String key) {
527-
IndexApiResponseMatchers.IndexMatcher result = this.indexMatchers.get(key);
528-
529-
if (result != null) {
530-
return result;
531-
} else {
532-
throw new RuntimeException("Unknown index matcher " + key + " in user " + this.name);
533-
}
534-
}
535-
536517
public User hash(String hash) {
537518
this.hash = hash;
538519
return this;
@@ -586,7 +567,7 @@ public <T> T reference(MetadataKey<T> key) {
586567
if (result != null) {
587568
return key.type.cast(result);
588569
} else {
589-
return null;
570+
throw new RuntimeException("Unknown reference " + key + " in user " + this.name);
590571
}
591572
}
592573

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
* <li>The results of REST API calls can be also associated with a maximum space of indices the operation could work on. Combined with the user specific index matcher, one can determine the intersection of the allowed indices and thus the indices that are allowed in the particular case. The matchers support JSON path expressions to extract information on indices from the HTTP response bodies. See IndexAuthorizationReadOnlyIntTests for examples.</li>
5151
* </ul>
5252
*/
53-
public class IndexApiResponseMatchers {
53+
public class RestIndexMatchers {
5454

5555
/**
5656
* Matchers that are directly used on HTTP responses

0 commit comments

Comments
 (0)