|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.security; |
| 7 | + |
| 8 | +import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_ACCOUNT; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.util.Base64; |
| 12 | +import java.util.Locale; |
| 13 | +import org.json.JSONObject; |
| 14 | +import org.junit.Test; |
| 15 | +import org.opensearch.client.Request; |
| 16 | +import org.opensearch.client.RequestOptions; |
| 17 | +import org.opensearch.client.Response; |
| 18 | +import org.opensearch.sql.legacy.SQLIntegTestCase; |
| 19 | +import org.opensearch.sql.legacy.TestUtils; |
| 20 | + |
| 21 | +/** |
| 22 | + * Regression test for SQL cursor pagination under Fine-Grained Access Control (FGAC). |
| 23 | + * |
| 24 | + * <p>Exercises the legacy V1 cursor path (triggered by {@code SELECT ... LIMIT n} with {@code |
| 25 | + * fetch_size}). Before the fix, page 2 would return 403 because the continuation SearchRequest was |
| 26 | + * created with no indices, which Security resolves to a wildcard and denies under FGAC. |
| 27 | + */ |
| 28 | +public class SQLCursorPermissionsIT extends SQLIntegTestCase { |
| 29 | + |
| 30 | + private static final String ACCOUNT_USER = "account_cursor_user"; |
| 31 | + private static final String ACCOUNT_ROLE = "account_cursor_role"; |
| 32 | + private static final String STRONG_PASSWORD = "StrongPassword123!"; |
| 33 | + |
| 34 | + private boolean initialized = false; |
| 35 | + |
| 36 | + @Override |
| 37 | + protected void init() throws Exception { |
| 38 | + loadIndex(Index.ACCOUNT); |
| 39 | + createSecurityRolesAndUsers(); |
| 40 | + } |
| 41 | + |
| 42 | + private void createSecurityRolesAndUsers() throws IOException { |
| 43 | + if (initialized) { |
| 44 | + return; |
| 45 | + } |
| 46 | + createRole(ACCOUNT_ROLE, TEST_INDEX_ACCOUNT); |
| 47 | + createUser(ACCOUNT_USER, ACCOUNT_ROLE); |
| 48 | + initialized = true; |
| 49 | + } |
| 50 | + |
| 51 | + private void createRole(String roleName, String indexPattern) throws IOException { |
| 52 | + Request request = new Request("PUT", "/_plugins/_security/api/roles/" + roleName); |
| 53 | + request.setJsonEntity( |
| 54 | + String.format( |
| 55 | + Locale.ROOT, |
| 56 | + """ |
| 57 | + { |
| 58 | + "cluster_permissions": [ |
| 59 | + "cluster:admin/opensearch/ppl", |
| 60 | + "cluster:admin/opensearch/sql" |
| 61 | + ], |
| 62 | + "index_permissions": [{ |
| 63 | + "index_patterns": [ |
| 64 | + "%s" |
| 65 | + ], |
| 66 | + "allowed_actions": [ |
| 67 | + "indices:data/read/search*", |
| 68 | + "indices:admin/mappings/get", |
| 69 | + "indices:admin/mappings/fields/get*", |
| 70 | + "indices:monitor/settings/get", |
| 71 | + "indices:data/read/point_in_time/create", |
| 72 | + "indices:data/read/point_in_time/delete" |
| 73 | + ] |
| 74 | + }] |
| 75 | + } |
| 76 | + """, |
| 77 | + indexPattern)); |
| 78 | + RequestOptions.Builder opts = RequestOptions.DEFAULT.toBuilder(); |
| 79 | + opts.addHeader("Content-Type", "application/json"); |
| 80 | + request.setOptions(opts); |
| 81 | + |
| 82 | + Response response = client().performRequest(request); |
| 83 | + int status = response.getStatusLine().getStatusCode(); |
| 84 | + assertTrue(status == 200 || status == 201); |
| 85 | + } |
| 86 | + |
| 87 | + private void createUser(String username, String roleName) throws IOException { |
| 88 | + Request userRequest = new Request("PUT", "/_plugins/_security/api/internalusers/" + username); |
| 89 | + userRequest.setJsonEntity( |
| 90 | + String.format( |
| 91 | + Locale.ROOT, |
| 92 | + """ |
| 93 | + { |
| 94 | + "password": "%s", |
| 95 | + "backend_roles": [], |
| 96 | + "attributes": {} |
| 97 | + } |
| 98 | + """, |
| 99 | + STRONG_PASSWORD)); |
| 100 | + RequestOptions.Builder opts = RequestOptions.DEFAULT.toBuilder(); |
| 101 | + opts.addHeader("Content-Type", "application/json"); |
| 102 | + userRequest.setOptions(opts); |
| 103 | + |
| 104 | + Response userResponse = client().performRequest(userRequest); |
| 105 | + int userStatus = userResponse.getStatusLine().getStatusCode(); |
| 106 | + assertTrue(userStatus == 200 || userStatus == 201); |
| 107 | + |
| 108 | + Request mappingRequest = new Request("PUT", "/_plugins/_security/api/rolesmapping/" + roleName); |
| 109 | + mappingRequest.setJsonEntity( |
| 110 | + String.format( |
| 111 | + Locale.ROOT, |
| 112 | + """ |
| 113 | + { |
| 114 | + "backend_roles": [], |
| 115 | + "hosts": [], |
| 116 | + "users": ["%s"] |
| 117 | + } |
| 118 | + """, |
| 119 | + username)); |
| 120 | + mappingRequest.setOptions(opts); |
| 121 | + |
| 122 | + Response mappingResponse = client().performRequest(mappingRequest); |
| 123 | + int mappingStatus = mappingResponse.getStatusLine().getStatusCode(); |
| 124 | + assertTrue(mappingStatus == 200 || mappingStatus == 201); |
| 125 | + } |
| 126 | + |
| 127 | + private JSONObject executeSqlAsUser(String body, String username) throws IOException { |
| 128 | + Request request = new Request("POST", "/_plugins/_sql"); |
| 129 | + request.setJsonEntity(body); |
| 130 | + RequestOptions.Builder opts = RequestOptions.DEFAULT.toBuilder(); |
| 131 | + opts.addHeader("Content-Type", "application/json"); |
| 132 | + opts.addHeader( |
| 133 | + "Authorization", |
| 134 | + "Basic " |
| 135 | + + Base64.getEncoder().encodeToString((username + ":" + STRONG_PASSWORD).getBytes())); |
| 136 | + request.setOptions(opts); |
| 137 | + |
| 138 | + Response response = client().performRequest(request); |
| 139 | + assertEquals(200, response.getStatusLine().getStatusCode()); |
| 140 | + return new JSONObject(TestUtils.getResponseBody(response, true)); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void simpleSelectUnderFgacSucceeds() throws IOException { |
| 145 | + JSONObject result = |
| 146 | + executeSqlAsUser( |
| 147 | + String.format( |
| 148 | + Locale.ROOT, |
| 149 | + "{\"query\": \"SELECT firstname FROM %s LIMIT 1\"}", |
| 150 | + TEST_INDEX_ACCOUNT), |
| 151 | + ACCOUNT_USER); |
| 152 | + assertTrue(result.has("datarows")); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Regression for SQL cursor pagination under FGAC. Triggers the V1 cursor path (LIMIT with |
| 157 | + * fetch_size) and advances through multiple continuation pages. Before the fix, page 2 returned |
| 158 | + * 403 because the continuation SearchRequest carried no indices, which Security resolves to a |
| 159 | + * wildcard and denies. |
| 160 | + */ |
| 161 | + @Test |
| 162 | + public void cursorPaginationUnderFgacSucceedsAcrossPages() throws IOException { |
| 163 | + // LIMIT forces the V1 cursor path (V2's CanPaginateVisitor rejects LIMIT). The V1 path is |
| 164 | + // the one that constructs the continuation SearchRequest without indices, which Security |
| 165 | + // denies under FGAC before this fix. |
| 166 | + JSONObject firstPage = |
| 167 | + executeSqlAsUser( |
| 168 | + String.format( |
| 169 | + Locale.ROOT, |
| 170 | + "{\"fetch_size\": 50, \"query\": \"SELECT age, balance FROM %s LIMIT 234\"}", |
| 171 | + TEST_INDEX_ACCOUNT), |
| 172 | + ACCOUNT_USER); |
| 173 | + assertTrue("first page must include a cursor; body=" + firstPage, firstPage.has("cursor")); |
| 174 | + String cursor = firstPage.getString("cursor"); |
| 175 | + assertFalse("first page cursor must not be empty", cursor.isEmpty()); |
| 176 | + assertTrue( |
| 177 | + "expected V1 cursor (prefix 'd:'), got: " |
| 178 | + + cursor.substring(0, Math.min(6, cursor.length())), |
| 179 | + cursor.startsWith("d:")); |
| 180 | + |
| 181 | + int pages = 1; |
| 182 | + while (!cursor.isEmpty()) { |
| 183 | + JSONObject next = |
| 184 | + executeSqlAsUser( |
| 185 | + String.format(Locale.ROOT, "{\"cursor\": \"%s\"}", cursor), ACCOUNT_USER); |
| 186 | + cursor = next.optString("cursor", ""); |
| 187 | + pages++; |
| 188 | + } |
| 189 | + // 234 rows / 50 per page = 5 pages |
| 190 | + assertEquals("expected 5 V1 cursor pages under FGAC", 5, pages); |
| 191 | + } |
| 192 | +} |
0 commit comments