|  | 
|  | 1 | +/* | 
|  | 2 | + * SPDX-License-Identifier: Apache-2.0 | 
|  | 3 | + * | 
|  | 4 | + * The OpenSearch Contributors require contributions made to | 
|  | 5 | + * this file be licensed under the Apache-2.0 license or a | 
|  | 6 | + * compatible open source license. | 
|  | 7 | + */ | 
|  | 8 | + | 
|  | 9 | +package org.opensearch.sample.resource; | 
|  | 10 | + | 
|  | 11 | +import java.util.HashMap; | 
|  | 12 | +import java.util.HashSet; | 
|  | 13 | +import java.util.Map; | 
|  | 14 | +import java.util.Set; | 
|  | 15 | + | 
|  | 16 | +import com.carrotsearch.randomizedtesting.RandomizedRunner; | 
|  | 17 | +import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope; | 
|  | 18 | +import org.apache.http.HttpStatus; | 
|  | 19 | +import org.junit.After; | 
|  | 20 | +import org.junit.Before; | 
|  | 21 | +import org.junit.ClassRule; | 
|  | 22 | +import org.junit.Test; | 
|  | 23 | +import org.junit.runner.RunWith; | 
|  | 24 | +import org.junit.runners.Suite; | 
|  | 25 | + | 
|  | 26 | +import org.opensearch.security.spi.resources.sharing.Recipient; | 
|  | 27 | +import org.opensearch.security.spi.resources.sharing.Recipients; | 
|  | 28 | +import org.opensearch.test.framework.cluster.LocalCluster; | 
|  | 29 | +import org.opensearch.test.framework.cluster.TestRestClient; | 
|  | 30 | + | 
|  | 31 | +import static org.hamcrest.MatcherAssert.assertThat; | 
|  | 32 | +import static org.hamcrest.Matchers.containsString; | 
|  | 33 | +import static org.hamcrest.Matchers.equalTo; | 
|  | 34 | +import static org.hamcrest.Matchers.not; | 
|  | 35 | +import static org.opensearch.sample.resource.TestUtils.FULL_ACCESS_USER; | 
|  | 36 | +import static org.opensearch.sample.resource.TestUtils.LIMITED_ACCESS_USER; | 
|  | 37 | +import static org.opensearch.sample.resource.TestUtils.NO_ACCESS_USER; | 
|  | 38 | +import static org.opensearch.sample.resource.TestUtils.RESOURCE_SHARING_INDEX; | 
|  | 39 | +import static org.opensearch.sample.resource.TestUtils.SECURITY_SHARE_ENDPOINT; | 
|  | 40 | +import static org.opensearch.sample.resource.TestUtils.newCluster; | 
|  | 41 | +import static org.opensearch.sample.resource.TestUtils.putSharingInfoPayload; | 
|  | 42 | +import static org.opensearch.sample.resource.TestUtils.sampleAllAG; | 
|  | 43 | +import static org.opensearch.sample.resource.TestUtils.sampleReadOnlyAG; | 
|  | 44 | +import static org.opensearch.sample.utils.Constants.RESOURCE_INDEX_NAME; | 
|  | 45 | +import static org.opensearch.test.framework.TestSecurityConfig.User.USER_ADMIN; | 
|  | 46 | + | 
|  | 47 | +/** | 
|  | 48 | + * This test file tests the share API defined by the security plugin. | 
|  | 49 | + * Resource access control feature and system index protection are assumed to be enabled | 
|  | 50 | + */ | 
|  | 51 | +@RunWith(Suite.class) | 
|  | 52 | +@Suite.SuiteClasses({ ShareApiTests.RoutesTests.class }) | 
|  | 53 | +public class ShareApiTests { | 
|  | 54 | +    /** | 
|  | 55 | +     * Base test class providing shared cluster setup and teardown | 
|  | 56 | +     */ | 
|  | 57 | +    public static abstract class BaseTests { | 
|  | 58 | +        @ClassRule | 
|  | 59 | +        public static LocalCluster cluster = newCluster(true, true); | 
|  | 60 | + | 
|  | 61 | +        @After | 
|  | 62 | +        public void clearIndices() { | 
|  | 63 | +            try (TestRestClient client = cluster.getRestClient(cluster.getAdminCertificate())) { | 
|  | 64 | +                client.delete(RESOURCE_INDEX_NAME); | 
|  | 65 | +                client.delete(RESOURCE_SHARING_INDEX); | 
|  | 66 | +            } | 
|  | 67 | +        } | 
|  | 68 | +    } | 
|  | 69 | + | 
|  | 70 | +    /** | 
|  | 71 | +     * Tests exercising the share API endpoints, GET, PUT & PATCH | 
|  | 72 | +     */ | 
|  | 73 | +    @RunWith(RandomizedRunner.class) | 
|  | 74 | +    @ThreadLeakScope(ThreadLeakScope.Scope.NONE) | 
|  | 75 | +    public static class RoutesTests extends BaseTests { | 
|  | 76 | +        private final TestUtils.ApiHelper api = new TestUtils.ApiHelper(cluster); | 
|  | 77 | +        private String adminResId; | 
|  | 78 | + | 
|  | 79 | +        @Before | 
|  | 80 | +        public void setup() { | 
|  | 81 | +            adminResId = api.createSampleResourceAs(USER_ADMIN); | 
|  | 82 | +            api.awaitSharingEntry(); | 
|  | 83 | +        } | 
|  | 84 | + | 
|  | 85 | +        @Test | 
|  | 86 | +        public void testPutSharingInfo() { | 
|  | 87 | +            // non-permission user cannot share resource | 
|  | 88 | +            try (TestRestClient client = cluster.getRestClient(LIMITED_ACCESS_USER)) { | 
|  | 89 | +                TestRestClient.HttpResponse response = client.putJson( | 
|  | 90 | +                    SECURITY_SHARE_ENDPOINT, | 
|  | 91 | +                    putSharingInfoPayload(adminResId, RESOURCE_INDEX_NAME, sampleReadOnlyAG.name(), NO_ACCESS_USER.getName()) | 
|  | 92 | +                ); | 
|  | 93 | +                response.assertStatusCode(HttpStatus.SC_FORBIDDEN); | 
|  | 94 | +            } | 
|  | 95 | + | 
|  | 96 | +            // a sharing entry should be created successfully since admin has access to share API | 
|  | 97 | +            try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { | 
|  | 98 | +                TestRestClient.HttpResponse response = client.putJson( | 
|  | 99 | +                    SECURITY_SHARE_ENDPOINT, | 
|  | 100 | +                    putSharingInfoPayload(adminResId, RESOURCE_INDEX_NAME, sampleAllAG.name(), LIMITED_ACCESS_USER.getName()) | 
|  | 101 | +                ); | 
|  | 102 | +                response.assertStatusCode(HttpStatus.SC_OK); | 
|  | 103 | +                assertThat(response.getBody(), containsString(LIMITED_ACCESS_USER.getName())); | 
|  | 104 | +                assertThat(response.getBody(), not(containsString(NO_ACCESS_USER.getName()))); | 
|  | 105 | +            } | 
|  | 106 | + | 
|  | 107 | +            // non-permission user will now have access to directly call share API | 
|  | 108 | +            try (TestRestClient client = cluster.getRestClient(LIMITED_ACCESS_USER)) { | 
|  | 109 | +                TestRestClient.HttpResponse response = client.putJson( | 
|  | 110 | +                    SECURITY_SHARE_ENDPOINT, | 
|  | 111 | +                    putSharingInfoPayload(adminResId, RESOURCE_INDEX_NAME, sampleReadOnlyAG.name(), NO_ACCESS_USER.getName()) | 
|  | 112 | +                ); | 
|  | 113 | +                response.assertStatusCode(HttpStatus.SC_OK); | 
|  | 114 | +                assertThat(response.getBody(), containsString(NO_ACCESS_USER.getName())); | 
|  | 115 | +            } | 
|  | 116 | +        } | 
|  | 117 | + | 
|  | 118 | +        @Test | 
|  | 119 | +        public void testGetSharingInfo() { | 
|  | 120 | +            // non-permission user cannot list shared resources, | 
|  | 121 | +            try (TestRestClient client = cluster.getRestClient(FULL_ACCESS_USER)) { | 
|  | 122 | +                TestRestClient.HttpResponse response = client.get( | 
|  | 123 | +                    SECURITY_SHARE_ENDPOINT + "?resource_id=" + adminResId + "&resource_type=" + RESOURCE_INDEX_NAME | 
|  | 124 | +                ); | 
|  | 125 | +                response.assertStatusCode(HttpStatus.SC_FORBIDDEN); | 
|  | 126 | +            } | 
|  | 127 | + | 
|  | 128 | +            // a sharing entry should be created successfully since admin has access to share API | 
|  | 129 | +            try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { | 
|  | 130 | +                TestRestClient.HttpResponse response = client.putJson( | 
|  | 131 | +                    SECURITY_SHARE_ENDPOINT, | 
|  | 132 | +                    putSharingInfoPayload(adminResId, RESOURCE_INDEX_NAME, sampleAllAG.name(), FULL_ACCESS_USER.getName()) | 
|  | 133 | +                ); | 
|  | 134 | +                response.assertStatusCode(HttpStatus.SC_OK); | 
|  | 135 | +                assertThat(response.getBody(), containsString(FULL_ACCESS_USER.getName())); | 
|  | 136 | +            } | 
|  | 137 | + | 
|  | 138 | +            // non-permission user can now list shared_with resources by calling share API | 
|  | 139 | +            try (TestRestClient client = cluster.getRestClient(FULL_ACCESS_USER)) { | 
|  | 140 | +                TestRestClient.HttpResponse response = client.get( | 
|  | 141 | +                    SECURITY_SHARE_ENDPOINT + "?resource_id=" + adminResId + "&resource_type=" + RESOURCE_INDEX_NAME | 
|  | 142 | +                ); | 
|  | 143 | +                response.assertStatusCode(HttpStatus.SC_OK); | 
|  | 144 | +                assertThat(response.bodyAsJsonNode().get("sharing_info").get("resource_id").asText(), equalTo(adminResId)); | 
|  | 145 | +            } | 
|  | 146 | +        } | 
|  | 147 | + | 
|  | 148 | +        @Test | 
|  | 149 | +        public void testPatchSharingInfo() { | 
|  | 150 | +            Map<Recipient, Set<String>> recs = new HashMap<>(); | 
|  | 151 | +            Set<String> users = new HashSet<>(); | 
|  | 152 | +            users.add(FULL_ACCESS_USER.getName()); | 
|  | 153 | +            recs.put(Recipient.USERS, users); | 
|  | 154 | +            Recipients recipients = new Recipients(recs); | 
|  | 155 | + | 
|  | 156 | +            TestUtils.PatchSharingInfoPayloadBuilder patchSharingInfoPayloadBuilder = new TestUtils.PatchSharingInfoPayloadBuilder(); | 
|  | 157 | +            patchSharingInfoPayloadBuilder.resourceId(adminResId).resourceIndex(RESOURCE_INDEX_NAME).share(recipients, sampleAllAG.name()); | 
|  | 158 | + | 
|  | 159 | +            // full-access user cannot share with itself since user doesn't have permission to share | 
|  | 160 | +            try (TestRestClient client = cluster.getRestClient(FULL_ACCESS_USER)) { | 
|  | 161 | +                TestRestClient.HttpResponse response = client.patch(SECURITY_SHARE_ENDPOINT, patchSharingInfoPayloadBuilder.build()); | 
|  | 162 | +                response.assertStatusCode(HttpStatus.SC_FORBIDDEN); | 
|  | 163 | +            } | 
|  | 164 | + | 
|  | 165 | +            // a sharing entry should be created successfully since admin has access to share API | 
|  | 166 | +            try (TestRestClient client = cluster.getRestClient(USER_ADMIN)) { | 
|  | 167 | +                TestRestClient.HttpResponse response = client.patch(SECURITY_SHARE_ENDPOINT, patchSharingInfoPayloadBuilder.build()); | 
|  | 168 | +                response.assertStatusCode(HttpStatus.SC_OK); | 
|  | 169 | +                assertThat(response.getBody(), containsString(FULL_ACCESS_USER.getName())); | 
|  | 170 | +            } | 
|  | 171 | + | 
|  | 172 | +            // limited access user will not be able to call patch endpoint | 
|  | 173 | +            try (TestRestClient client = cluster.getRestClient(LIMITED_ACCESS_USER)) { | 
|  | 174 | +                TestRestClient.HttpResponse response = client.patch(SECURITY_SHARE_ENDPOINT, patchSharingInfoPayloadBuilder.build()); | 
|  | 175 | +                response.assertStatusCode(HttpStatus.SC_FORBIDDEN); | 
|  | 176 | +            } | 
|  | 177 | + | 
|  | 178 | +            // full-access user will now be able to patch and grant access to limited access user | 
|  | 179 | +            // they can also shoot themselves in the foot and remove own access | 
|  | 180 | +            try (TestRestClient client = cluster.getRestClient(FULL_ACCESS_USER)) { | 
|  | 181 | +                // add limited user | 
|  | 182 | +                users.add(LIMITED_ACCESS_USER.getName()); | 
|  | 183 | +                patchSharingInfoPayloadBuilder.share(recipients, sampleAllAG.name()); | 
|  | 184 | +                // remove self | 
|  | 185 | +                Set<String> revokedUsers = new HashSet<>(); | 
|  | 186 | +                revokedUsers.add(FULL_ACCESS_USER.getName()); | 
|  | 187 | +                recs.put(Recipient.USERS, revokedUsers); | 
|  | 188 | +                recipients = new Recipients(recs); | 
|  | 189 | +                patchSharingInfoPayloadBuilder.revoke(recipients, sampleAllAG.name()); | 
|  | 190 | + | 
|  | 191 | +                TestRestClient.HttpResponse response = client.patch(SECURITY_SHARE_ENDPOINT, patchSharingInfoPayloadBuilder.build()); | 
|  | 192 | +                response.assertStatusCode(HttpStatus.SC_OK); | 
|  | 193 | +            } | 
|  | 194 | + | 
|  | 195 | +            // limited access user will now be able to call patch endpoint, but full-access won't | 
|  | 196 | +            try (TestRestClient client = cluster.getRestClient(LIMITED_ACCESS_USER)) { | 
|  | 197 | +                TestRestClient.HttpResponse response = client.patch(SECURITY_SHARE_ENDPOINT, patchSharingInfoPayloadBuilder.build()); | 
|  | 198 | +                response.assertStatusCode(HttpStatus.SC_OK); | 
|  | 199 | +            } | 
|  | 200 | +            try (TestRestClient client = cluster.getRestClient(FULL_ACCESS_USER)) { | 
|  | 201 | +                TestRestClient.HttpResponse response = client.patch(SECURITY_SHARE_ENDPOINT, patchSharingInfoPayloadBuilder.build()); | 
|  | 202 | +                response.assertStatusCode(HttpStatus.SC_FORBIDDEN); | 
|  | 203 | +            } | 
|  | 204 | +        } | 
|  | 205 | +    } | 
|  | 206 | + | 
|  | 207 | +} | 
0 commit comments