|
| 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 | +package org.opensearch.security.api; |
| 9 | + |
| 10 | +import java.net.URLEncoder; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | + |
| 15 | +import org.junit.ClassRule; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import org.opensearch.security.DefaultObjectMapper; |
| 19 | +import org.opensearch.test.framework.cluster.LocalCluster; |
| 20 | +import org.opensearch.test.framework.cluster.TestRestClient; |
| 21 | + |
| 22 | +import static org.junit.Assert.assertEquals; |
| 23 | +import static org.junit.Assert.assertFalse; |
| 24 | +import static org.junit.Assert.assertTrue; |
| 25 | + |
| 26 | +public class SecurityApiRequestValidationIntegrationTest { |
| 27 | + private static final String BASE = "_plugins/_security/api/"; |
| 28 | + private static final String EXPRESSION = "${env.SECURITY_API_VALIDATION_TEST:-synthetic}"; |
| 29 | + private static final String ROLE_BODY = "{\"cluster_permissions\":[\"cluster:monitor/main\"]}"; |
| 30 | + |
| 31 | + @ClassRule |
| 32 | + public static final LocalCluster CLUSTER = new LocalCluster.Builder().singleNode().build(); |
| 33 | + |
| 34 | + @Test |
| 35 | + public void rejectsBodyExpressionsAcrossEndpoints() throws Exception { |
| 36 | + try (TestRestClient admin = CLUSTER.getAdminCertRestClient()) { |
| 37 | + for (var entry : Map.of( |
| 38 | + "roles", |
| 39 | + Map.of("cluster_permissions", List.of(EXPRESSION)), |
| 40 | + "rolesmapping", |
| 41 | + Map.of("backend_roles", List.of(EXPRESSION)), |
| 42 | + "tenants", |
| 43 | + Map.of("description", EXPRESSION), |
| 44 | + "actiongroups", |
| 45 | + Map.of("allowed_actions", List.of(EXPRESSION)), |
| 46 | + "internalusers", |
| 47 | + Map.of("password", "Synthetic-test-password!42", "attributes", Map.of(EXPRESSION, "value")), |
| 48 | + "authfailurelisteners", |
| 49 | + Map.of("type", "ip", "ignore_hosts", List.of(EXPRESSION)) |
| 50 | + ).entrySet()) { |
| 51 | + String path = BASE + entry.getKey() + "/environment_validation_test"; |
| 52 | + var original = admin.get(BASE + entry.getKey()).bodyAsJsonNode(); |
| 53 | + assertRejected(admin.putJson(path, DefaultObjectMapper.writeValueAsString(entry.getValue(), false))); |
| 54 | + assertEquals(original, admin.get(BASE + entry.getKey()).bodyAsJsonNode()); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void rejectsEncodedRouteNames() { |
| 61 | + try (TestRestClient admin = CLUSTER.getAdminCertRestClient()) { |
| 62 | + String encodedName = URLEncoder.encode(EXPRESSION, StandardCharsets.UTF_8); |
| 63 | + for (var endpoint : Map.of( |
| 64 | + "roles", |
| 65 | + ROLE_BODY, |
| 66 | + "tenants", |
| 67 | + "{\"description\":\"ordinary\"}", |
| 68 | + "authfailurelisteners", |
| 69 | + "{\"type\":\"ip\"}" |
| 70 | + ).entrySet()) { |
| 71 | + String path = BASE + endpoint.getKey() + "/" + encodedName; |
| 72 | + var original = admin.get(BASE + endpoint.getKey()).bodyAsJsonNode(); |
| 73 | + assertRejected(admin.putJson(path, endpoint.getValue())); |
| 74 | + assertEquals(original, admin.get(BASE + endpoint.getKey()).bodyAsJsonNode()); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void rejectsEscapedExpressionsAndAsyncWritesWithoutChangingRole() { |
| 81 | + try (TestRestClient admin = CLUSTER.getAdminCertRestClient()) { |
| 82 | + String path = BASE + "roles/environment_validation_test"; |
| 83 | + try { |
| 84 | + assertEquals(201, admin.putJson(path, ROLE_BODY).getStatusCode()); |
| 85 | + var original = admin.get(path).bodyAsJsonNode(); |
| 86 | + for (String suffix : List.of("", "?wait_for_completion=false")) { |
| 87 | + assertRejected( |
| 88 | + admin.putJson(path + suffix, "{\"cluster_permissions\":[\"\\u0024{env.SECURITY_API_VALIDATION_TEST:-synthetic}\"]}") |
| 89 | + ); |
| 90 | + assertEquals(original, admin.get(path).bodyAsJsonNode()); |
| 91 | + } |
| 92 | + } finally { |
| 93 | + admin.delete(path); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void rejectsSingleAndMultiEntityPatchesAtomically() throws Exception { |
| 100 | + try (TestRestClient admin = CLUSTER.getAdminCertRestClient()) { |
| 101 | + String path = BASE + "roles/environment_validation_test"; |
| 102 | + try { |
| 103 | + assertEquals(201, admin.putJson(path, ROLE_BODY).getStatusCode()); |
| 104 | + var original = admin.get(path).bodyAsJsonNode(); |
| 105 | + String singlePatch = DefaultObjectMapper.writeValueAsString( |
| 106 | + List.of(Map.of("op", "add", "path", "/cluster_permissions/-", "value", EXPRESSION)), |
| 107 | + false |
| 108 | + ); |
| 109 | + assertRejected(admin.patch(path, singlePatch)); |
| 110 | + String bulkPatch = DefaultObjectMapper.writeValueAsString( |
| 111 | + List.of( |
| 112 | + Map.of("op", "add", "path", "/ordinary_patch_role", "value", Map.of("cluster_permissions", List.of())), |
| 113 | + Map.of("op", "add", "path", "/" + EXPRESSION, "value", Map.of("cluster_permissions", List.of())) |
| 114 | + ), |
| 115 | + false |
| 116 | + ); |
| 117 | + assertRejected(admin.patch(BASE + "roles", bulkPatch)); |
| 118 | + assertEquals(original, admin.get(path).bodyAsJsonNode()); |
| 119 | + assertEquals(404, admin.get(BASE + "roles/ordinary_patch_role").getStatusCode()); |
| 120 | + } finally { |
| 121 | + admin.delete(path); |
| 122 | + admin.delete(BASE + "roles/ordinary_patch_role"); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void permitsDlsUserAttributeTemplates() { |
| 129 | + try (TestRestClient admin = CLUSTER.getAdminCertRestClient()) { |
| 130 | + String path = BASE + "roles/environment_validation_template"; |
| 131 | + try { |
| 132 | + assertEquals(201, admin.putJson(path, """ |
| 133 | + {"index_permissions":[{"index_patterns":["test"],"allowed_actions":["read"], |
| 134 | + "dls":"{\\"term\\":{\\"owner\\":\\"${user.name}\\"}}"}]} |
| 135 | + """).getStatusCode()); |
| 136 | + } finally { |
| 137 | + admin.delete(path); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private void assertRejected(TestRestClient.HttpResponse response) { |
| 143 | + assertEquals(response.getBody(), 400, response.getStatusCode()); |
| 144 | + assertTrue(response.getBody(), response.getBody().contains("must not contain environment variable expressions")); |
| 145 | + assertFalse(response.getBody().contains("SECURITY_API_VALIDATION_TEST")); |
| 146 | + } |
| 147 | +} |
0 commit comments