Skip to content

Commit 87a93c0

Browse files
committed
Fix tests
Signed-off-by: Craig Perkins <[email protected]>
1 parent 2a1f18a commit 87a93c0

File tree

7 files changed

+26
-16
lines changed

7 files changed

+26
-16
lines changed

sample-resource-plugin/src/integrationTest/java/org/opensearch/sample/resource/TestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ public String createSampleResourceGroupAs(TestSecurityConfig.User user, Header..
373373

374374
public String createRawResourceAs(CertificateData adminCert) {
375375
try (TestRestClient client = cluster.getRestClient(adminCert)) {
376-
String sample = "{\"name\":\"sample\"}";
376+
String sample = "{\"name\":\"sample\",\"resource_type\":\"" + RESOURCE_TYPE + "\"}";
377377
TestRestClient.HttpResponse resp = client.postJson(RESOURCE_INDEX_NAME + "/_doc", sample);
378378
resp.assertStatusCode(HttpStatus.SC_CREATED);
379379
return resp.getTextFromJsonBody("/_id");

sample-resource-plugin/src/integrationTest/java/org/opensearch/sample/resource/feature/disabled/DirectIndexAccessTests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import static org.opensearch.sample.resource.TestUtils.SAMPLE_RESOURCE_SEARCH_ENDPOINT;
3232
import static org.opensearch.sample.resource.TestUtils.newCluster;
3333
import static org.opensearch.sample.utils.Constants.RESOURCE_INDEX_NAME;
34+
import static org.opensearch.sample.utils.Constants.RESOURCE_TYPE;
3435
import static org.opensearch.security.api.AbstractApiIntegrationTest.forbidden;
3536
import static org.opensearch.test.framework.TestSecurityConfig.User.USER_ADMIN;
3637

@@ -66,7 +67,7 @@ public void testRawAccess_noAccessUser() throws Exception {
6667

6768
// cannot access any raw request
6869
try (TestRestClient client = cluster.getRestClient(NO_ACCESS_USER)) {
69-
String sample = "{\"name\":\"sampleUser\"}";
70+
String sample = "{\"name\":\"sampleUser\",\"resource_type\":\"" + RESOURCE_TYPE + "\"}";
7071
HttpResponse resp = client.postJson(RESOURCE_INDEX_NAME + "/_doc", sample);
7172
resp.assertStatusCode(HttpStatus.SC_FORBIDDEN);
7273
}
@@ -89,7 +90,7 @@ public void testRawAccess_limitedAccessUser() {
8990

9091
// cannot create a resource since user doesn't have indices:data/write/index permission
9192
try (TestRestClient client = cluster.getRestClient(LIMITED_ACCESS_USER)) {
92-
String sample = "{\"name\":\"sampleUser\"}";
93+
String sample = "{\"name\":\"sampleUser\",\"resource_type\":\"" + RESOURCE_TYPE + "\"}";
9394
HttpResponse resp = client.postJson(RESOURCE_INDEX_NAME + "/_doc", sample);
9495
resp.assertStatusCode(HttpStatus.SC_FORBIDDEN);
9596
}
@@ -114,7 +115,7 @@ public void testRawAccess_allAccessUser() {
114115

115116
// cannot create a resource directly since system index protection (SIP) is enabled
116117
try (TestRestClient client = cluster.getRestClient(FULL_ACCESS_USER)) {
117-
String sample = "{\"name\":\"sampleUser\"}";
118+
String sample = "{\"name\":\"sampleUser\",\"resource_type\":\"" + RESOURCE_TYPE + "\"}";
118119
HttpResponse resp = client.postJson(RESOURCE_INDEX_NAME + "/_doc", sample);
119120
resp.assertStatusCode(HttpStatus.SC_FORBIDDEN);
120121
}
@@ -179,7 +180,7 @@ public void testRawAccess_noAccessUser() {
179180

180181
// cannot access any raw request
181182
try (TestRestClient client = cluster.getRestClient(NO_ACCESS_USER)) {
182-
String sample = "{\"name\":\"sampleUser\"}";
183+
String sample = "{\"name\":\"sampleUser\",\"resource_type\":\"" + RESOURCE_TYPE + "\"}";
183184
TestRestClient.HttpResponse resp = client.postJson(RESOURCE_INDEX_NAME + "/_doc", sample);
184185
resp.assertStatusCode(HttpStatus.SC_FORBIDDEN);
185186
}
@@ -202,7 +203,7 @@ public void testRawAccess_limitedAccessUser() {
202203

203204
// cannot create a resource since user doesn't have indices:data/write/index permission
204205
try (TestRestClient client = cluster.getRestClient(LIMITED_ACCESS_USER)) {
205-
String sample = "{\"name\":\"sampleUser\"}";
206+
String sample = "{\"name\":\"sampleUser\",\"resource_type\":\"" + RESOURCE_TYPE + "\"}";
206207
TestRestClient.HttpResponse resp = client.postJson(RESOURCE_INDEX_NAME + "/_doc", sample);
207208
resp.assertStatusCode(HttpStatus.SC_FORBIDDEN);
208209
}
@@ -228,7 +229,7 @@ public void testRawAccess_allAccessUser() {
228229
// can create a resource
229230
String userResId;
230231
try (TestRestClient client = cluster.getRestClient(FULL_ACCESS_USER)) {
231-
String sample = "{\"name\":\"sampleUser\"}";
232+
String sample = "{\"name\":\"sampleUser\",\"resource_type\":\"" + RESOURCE_TYPE + "\"}";
232233
TestRestClient.HttpResponse resp = client.postJson(RESOURCE_INDEX_NAME + "/_doc", sample);
233234
resp.assertStatusCode(HttpStatus.SC_CREATED);
234235
userResId = resp.getTextFromJsonBody("/_id");

sample-resource-plugin/src/integrationTest/java/org/opensearch/sample/resource/feature/enabled/DirectIndexAccessTests.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import static org.opensearch.sample.resource.TestUtils.directSharePayload;
3737
import static org.opensearch.sample.resource.TestUtils.newCluster;
3838
import static org.opensearch.sample.utils.Constants.RESOURCE_INDEX_NAME;
39+
import static org.opensearch.sample.utils.Constants.RESOURCE_TYPE;
3940
import static org.opensearch.test.framework.TestSecurityConfig.User.USER_ADMIN;
4041

4142
/**
@@ -62,7 +63,7 @@ public static class SystemIndexEnabled {
6263
private void assertResourceIndexAccess(String id, TestSecurityConfig.User user) {
6364
// cannot interact with resource index
6465
try (TestRestClient client = cluster.getRestClient(user)) {
65-
String sample = "{\"name\":\"sampleUser\"}";
66+
String sample = "{\"name\":\"sampleUser\",\"resource_type\":\"" + RESOURCE_TYPE + "\"}";
6667
HttpResponse resp = client.postJson(RESOURCE_INDEX_NAME + "/_doc", sample);
6768
resp.assertStatusCode(HttpStatus.SC_FORBIDDEN);
6869
}
@@ -196,7 +197,7 @@ public void testRawAccess_noAccessUser() {
196197

197198
// cannot access any raw request
198199
try (TestRestClient client = cluster.getRestClient(NO_ACCESS_USER)) {
199-
String sample = "{\"name\":\"sampleUser\"}";
200+
String sample = "{\"name\":\"sampleUser\",\"resource_type\":\"" + RESOURCE_TYPE + "\"}";
200201
HttpResponse resp = client.postJson(RESOURCE_INDEX_NAME + "/_doc", sample);
201202
resp.assertStatusCode(HttpStatus.SC_FORBIDDEN);
202203
}
@@ -221,7 +222,7 @@ public void testRawAccess_limitedAccessUser() {
221222

222223
// cannot create a resource since user doesn't have indices:data/write/index permission
223224
try (TestRestClient client = cluster.getRestClient(LIMITED_ACCESS_USER)) {
224-
String sample = "{\"name\":\"sampleUser\"}";
225+
String sample = "{\"name\":\"sampleUser\",\"resource_type\":\"" + RESOURCE_TYPE + "\"}";
225226
HttpResponse resp = client.postJson(RESOURCE_INDEX_NAME + "/_doc", sample);
226227
resp.assertStatusCode(HttpStatus.SC_FORBIDDEN);
227228
}
@@ -259,7 +260,7 @@ public void testRawAccess_allAccessUser() {
259260
// can create a resource
260261
String userResId;
261262
try (TestRestClient client = cluster.getRestClient(FULL_ACCESS_USER)) {
262-
String sample = "{\"name\":\"sampleUser\"}";
263+
String sample = "{\"name\":\"sampleUser\",\"resource_type\":\"" + RESOURCE_TYPE + "\"}";
263264
HttpResponse resp = client.postJson(RESOURCE_INDEX_NAME + "/_doc?refresh=true", sample);
264265
resp.assertStatusCode(HttpStatus.SC_CREATED);
265266
userResId = resp.getTextFromJsonBody("/_id");

sample-resource-plugin/src/integrationTest/java/org/opensearch/sample/resource/securityapis/MigrateApiTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ private ArrayNode expectedHits(String resourceId, String accessLevel) {
301301
// 3) Build the _source sub-object
302302
ObjectNode source = hit.putObject("_source");
303303
source.put("resource_id", resourceId);
304+
source.put("resource_type", RESOURCE_TYPE);
304305

305306
ObjectNode createdBy = source.putObject("created_by");
306307
createdBy.put("user", MIGRATION_USER.getName());

sample-resource-plugin/src/integrationTest/java/org/opensearch/sample/resource/securityapis/ShareableResourceTypesInfoApiTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ public void testTypesApi_mustListSampleResourceAsAType() {
5959
Map<String, Object> firstType = (Map<String, Object>) types.get(0);
6060
assertThat(firstType.get("type"), equalTo("sample-resource"));
6161
assertThat(
62-
(List<String>) firstType.get("action_groups"),
62+
(List<String>) firstType.get("access_levels"),
6363
containsInAnyOrder("sample_read_only", "sample_read_write", "sample_full_access")
6464
);
6565
Map<String, Object> secondType = (Map<String, Object>) types.get(1);
6666
assertThat(secondType.get("type"), equalTo("sample-resource-group"));
6767
assertThat(
68-
(List<String>) secondType.get("action_groups"),
68+
(List<String>) secondType.get("access_levels"),
6969
containsInAnyOrder("sample_group_read_only", "sample_group_read_write", "sample_group_full_access")
7070
);
7171
}

src/main/java/org/opensearch/security/resources/api/migrate/MigrateResourceSharingInfoApiAction.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,15 @@ private ValidationResult<MigrationStats> createNewSharingRecords(Triple<String,
322322
failureCount.getAndIncrement();
323323
migrationStatsLatch.countDown();
324324
});
325-
// TODO account for hierarchy in migration as well
326-
ResourceSharing sharingInfo = new ResourceSharing(resourceId, createdBy, shareWith);
325+
// TODO account for hierarchy in migration as well (i.e. parent id)
326+
ResourceSharing.Builder builder = ResourceSharing.builder()
327+
.resourceId(resourceId)
328+
.createdBy(createdBy)
329+
.shareWith(shareWith);
330+
builder.resourceType(provider.resourceType());
331+
// TODO uncomment when hierarchy fully supported
332+
// builder.parentType(provider.parentType());
333+
ResourceSharing sharingInfo = builder.build();
327334
sharingIndexHandler.indexResourceSharing(sourceInfo.getLeft(), sharingInfo, listener);
328335
} catch (Exception e) {
329336
LOGGER.warn("Failed indexing sharing info for [{}]: {}", resourceId, e.getMessage());

src/main/java/org/opensearch/security/resources/sharing/ResourceSharing.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public CreatedBy getCreatedBy() {
114114
public ShareWith getShareWith() {
115115
if (shareWith == null) {
116116
// never been shared before, private access
117-
return new ShareWith(new HashMap<>());
117+
shareWith = new ShareWith(new HashMap<>());
118118
}
119119
return shareWith;
120120
}

0 commit comments

Comments
 (0)