|
| 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 | + * Modifications Copyright OpenSearch Contributors. See |
| 9 | + * GitHub history for details. |
| 10 | + */ |
| 11 | + |
| 12 | +package org.opensearch.security.dlic.dlsfls; |
| 13 | + |
| 14 | +import org.apache.http.Header; |
| 15 | +import org.apache.http.HttpStatus; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import org.opensearch.action.index.IndexRequest; |
| 19 | +import org.opensearch.action.support.WriteRequest.RefreshPolicy; |
| 20 | +import org.opensearch.client.Client; |
| 21 | +import org.opensearch.common.xcontent.XContentType; |
| 22 | +import org.opensearch.security.test.DynamicSecurityConfig; |
| 23 | +import org.opensearch.security.test.helper.rest.RestHelper.HttpResponse; |
| 24 | + |
| 25 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 26 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 27 | +import static org.hamcrest.core.IsNot.not; |
| 28 | +import static org.hamcrest.core.StringContains.containsString; |
| 29 | + |
| 30 | +public class FlsIndexingTests extends AbstractDlsFlsTest { |
| 31 | + |
| 32 | + protected void populateData(final Client tc) { |
| 33 | + // Create several documents in different indices with shared field names, |
| 34 | + // different roles will have different levels of FLS restrictions |
| 35 | + tc.index(new IndexRequest("yellow-pages").id("1").setRefreshPolicy(RefreshPolicy.IMMEDIATE) |
| 36 | + .source("{\"phone-all\":1001,\"phone-some\":1002,\"phone-one\":1003}", XContentType.JSON)).actionGet(); |
| 37 | + tc.index(new IndexRequest("green-pages").id("2").setRefreshPolicy(RefreshPolicy.IMMEDIATE) |
| 38 | + .source("{\"phone-all\":2001,\"phone-some\":2002,\"phone-one\":2003}", XContentType.JSON)).actionGet(); |
| 39 | + tc.index(new IndexRequest("blue-book").id("3").setRefreshPolicy(RefreshPolicy.IMMEDIATE) |
| 40 | + .source("{\"phone-all\":3001,\"phone-some\":3002,\"phone-one\":3003}", XContentType.JSON)).actionGet(); |
| 41 | + |
| 42 | + // Seperate index used to test aliasing |
| 43 | + tc.index(new IndexRequest(".hidden").id("1").setRefreshPolicy(RefreshPolicy.IMMEDIATE) |
| 44 | + .source("{}", XContentType.JSON)).actionGet(); |
| 45 | + } |
| 46 | + |
| 47 | + private Header asPhoneOneUser = encodeBasicHeader("user_aaa", "password"); |
| 48 | + private Header asPhoneSomeUser = encodeBasicHeader("user_bbb", "password"); |
| 49 | + private Header asPhoneAllUser = encodeBasicHeader("user_ccc", "password"); |
| 50 | + |
| 51 | + private final String searchQuery = "/*/_search?filter_path=hits.hits&pretty"; |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testSingleIndexFlsApplied() throws Exception { |
| 55 | + setup(new DynamicSecurityConfig() |
| 56 | + .setSecurityRoles("roles_fls_indexing.yml") |
| 57 | + .setSecurityRolesMapping("roles_mapping_fls_indexing.yml")); |
| 58 | + |
| 59 | + final HttpResponse phoneOneFilteredResponse = rh.executeGetRequest(searchQuery, asPhoneOneUser); |
| 60 | + assertThat(phoneOneFilteredResponse.getStatusCode(), equalTo(HttpStatus.SC_OK)); |
| 61 | + assertThat(phoneOneFilteredResponse.getBody(), not(containsString("1003"))); |
| 62 | + assertThat(phoneOneFilteredResponse.getBody(), containsString("1002")); |
| 63 | + assertThat(phoneOneFilteredResponse.getBody(), containsString("1001")); |
| 64 | + |
| 65 | + assertThat(phoneOneFilteredResponse.getBody(), containsString("2003")); |
| 66 | + assertThat(phoneOneFilteredResponse.getBody(), containsString("2002")); |
| 67 | + assertThat(phoneOneFilteredResponse.getBody(), containsString("2001")); |
| 68 | + |
| 69 | + assertThat(phoneOneFilteredResponse.getBody(), containsString("3003")); |
| 70 | + assertThat(phoneOneFilteredResponse.getBody(), containsString("3002")); |
| 71 | + assertThat(phoneOneFilteredResponse.getBody(), containsString("3001")); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testSingleIndexFlsAppliedForLimitedResults() throws Exception { |
| 76 | + setup(new DynamicSecurityConfig() |
| 77 | + .setSecurityRoles("roles_fls_indexing.yml") |
| 78 | + .setSecurityRolesMapping("roles_mapping_fls_indexing.yml")); |
| 79 | + |
| 80 | + final HttpResponse phoneOneFilteredResponse = rh.executeGetRequest("/yellow-pages/_search?filter_path=hits.hits&pretty", asPhoneOneUser); |
| 81 | + assertThat(phoneOneFilteredResponse.getStatusCode(), equalTo(HttpStatus.SC_OK)); |
| 82 | + assertThat(phoneOneFilteredResponse.getBody(), not(containsString("1003"))); |
| 83 | + assertThat(phoneOneFilteredResponse.getBody(), containsString("1002")); |
| 84 | + assertThat(phoneOneFilteredResponse.getBody(), containsString("1001")); |
| 85 | + |
| 86 | + assertThat(phoneOneFilteredResponse.getBody(), not(containsString("2003"))); |
| 87 | + assertThat(phoneOneFilteredResponse.getBody(), not(containsString("2002"))); |
| 88 | + assertThat(phoneOneFilteredResponse.getBody(), not(containsString("2001"))); |
| 89 | + |
| 90 | + assertThat(phoneOneFilteredResponse.getBody(), not(containsString("3003"))); |
| 91 | + assertThat(phoneOneFilteredResponse.getBody(), not(containsString("3002"))); |
| 92 | + assertThat(phoneOneFilteredResponse.getBody(), not(containsString("3001"))); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testSeveralIndexFlsApplied() throws Exception { |
| 97 | + setup(new DynamicSecurityConfig() |
| 98 | + .setSecurityRoles("roles_fls_indexing.yml") |
| 99 | + .setSecurityRolesMapping("roles_mapping_fls_indexing.yml")); |
| 100 | + |
| 101 | + final HttpResponse phoneSomeFilteredResponse = rh.executeGetRequest(searchQuery, asPhoneSomeUser); |
| 102 | + assertThat(phoneSomeFilteredResponse.getStatusCode(), equalTo(HttpStatus.SC_OK)); |
| 103 | + assertThat(phoneSomeFilteredResponse.getBody(), containsString("1003")); |
| 104 | + assertThat(phoneSomeFilteredResponse.getBody(), not(containsString("1002"))); |
| 105 | + assertThat(phoneSomeFilteredResponse.getBody(), containsString("1001")); |
| 106 | + |
| 107 | + assertThat(phoneSomeFilteredResponse.getBody(), containsString("2003")); |
| 108 | + assertThat(phoneSomeFilteredResponse.getBody(), not(containsString("2002"))); |
| 109 | + assertThat(phoneSomeFilteredResponse.getBody(), containsString("2001")); |
| 110 | + |
| 111 | + assertThat(phoneSomeFilteredResponse.getBody(), containsString("3003")); |
| 112 | + assertThat(phoneSomeFilteredResponse.getBody(), containsString("3002")); |
| 113 | + assertThat(phoneSomeFilteredResponse.getBody(), containsString("3001")); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testAllIndexFlsApplied() throws Exception { |
| 118 | + setup(new DynamicSecurityConfig() |
| 119 | + .setSecurityRoles("roles_fls_indexing.yml") |
| 120 | + .setSecurityRolesMapping("roles_mapping_fls_indexing.yml")); |
| 121 | + |
| 122 | + final HttpResponse phoneAllFilteredResponse = rh.executeGetRequest(searchQuery, asPhoneAllUser); |
| 123 | + assertThat(phoneAllFilteredResponse.getStatusCode(), equalTo(HttpStatus.SC_OK)); |
| 124 | + assertThat(phoneAllFilteredResponse.getBody(), containsString("1003")); |
| 125 | + assertThat(phoneAllFilteredResponse.getBody(), containsString("1002")); |
| 126 | + assertThat(phoneAllFilteredResponse.getBody(), not(containsString("1001"))); |
| 127 | + |
| 128 | + assertThat(phoneAllFilteredResponse.getBody(), containsString("2003")); |
| 129 | + assertThat(phoneAllFilteredResponse.getBody(), containsString("2002")); |
| 130 | + assertThat(phoneAllFilteredResponse.getBody(), not(containsString("2001"))); |
| 131 | + |
| 132 | + assertThat(phoneAllFilteredResponse.getBody(), containsString("3003")); |
| 133 | + assertThat(phoneAllFilteredResponse.getBody(), containsString("3002")); |
| 134 | + assertThat(phoneAllFilteredResponse.getBody(), not(containsString("3001"))); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testAllIndexFlsAppliedWithAlias() throws Exception { |
| 139 | + setup(new DynamicSecurityConfig() |
| 140 | + .setSecurityRoles("roles_fls_indexing.yml") |
| 141 | + .setSecurityRolesMapping("roles_mapping_fls_indexing.yml")); |
| 142 | + |
| 143 | + final HttpResponse createAlias = rh.executePostRequest("_aliases", "{\"actions\":[{\"add\":{\"index\":\".hidden\",\"alias\":\"ducky\"}}]}", asPhoneAllUser); |
| 144 | + assertThat(createAlias.getStatusCode(), equalTo(HttpStatus.SC_OK)); |
| 145 | + |
| 146 | + final HttpResponse phoneAllFilteredResponse = rh.executeGetRequest(searchQuery, asPhoneAllUser); |
| 147 | + assertThat(phoneAllFilteredResponse.getStatusCode(), equalTo(HttpStatus.SC_OK)); |
| 148 | + assertThat(phoneAllFilteredResponse.getBody(), containsString("1003")); |
| 149 | + assertThat(phoneAllFilteredResponse.getBody(), containsString("1002")); |
| 150 | + assertThat(phoneAllFilteredResponse.getBody(), not(containsString("1001"))); |
| 151 | + |
| 152 | + assertThat(phoneAllFilteredResponse.getBody(), containsString("2003")); |
| 153 | + assertThat(phoneAllFilteredResponse.getBody(), containsString("2002")); |
| 154 | + assertThat(phoneAllFilteredResponse.getBody(), not(containsString("2001"))); |
| 155 | + |
| 156 | + assertThat(phoneAllFilteredResponse.getBody(), containsString("3003")); |
| 157 | + assertThat(phoneAllFilteredResponse.getBody(), containsString("3002")); |
| 158 | + assertThat(phoneAllFilteredResponse.getBody(), not(containsString("3001"))); |
| 159 | + } |
| 160 | +} |
0 commit comments