|
| 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 | +package org.opensearch.security.api; |
| 12 | + |
| 13 | +import java.util.StringJoiner; |
| 14 | + |
| 15 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +import org.opensearch.security.DefaultObjectMapper; |
| 19 | +import org.opensearch.security.dlic.rest.api.Endpoint; |
| 20 | +import org.opensearch.test.framework.cluster.TestRestClient; |
| 21 | + |
| 22 | +import static org.opensearch.security.api.PatchPayloadHelper.patch; |
| 23 | +import static org.opensearch.security.api.PatchPayloadHelper.replaceOp; |
| 24 | +import static org.opensearch.security.dlic.rest.api.RestApiAdminPrivilegesEvaluator.SECURITY_CONFIG_UPDATE; |
| 25 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_RESTAPI_ADMIN_ENABLED; |
| 26 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_UNSUPPORTED_RESTAPI_ALLOW_SECURITYCONFIG_MODIFICATION; |
| 27 | + |
| 28 | +public class ConfigRestApiIntegrationTest extends AbstractApiIntegrationTest { |
| 29 | + |
| 30 | + final static String REST_API_ADMIN_CONFIG_UPDATE = "rest-api-admin-config-update"; |
| 31 | + |
| 32 | + static { |
| 33 | + clusterSettings.put(SECURITY_UNSUPPORTED_RESTAPI_ALLOW_SECURITYCONFIG_MODIFICATION, true).put(SECURITY_RESTAPI_ADMIN_ENABLED, true); |
| 34 | + testSecurityConfig.withRestAdminUser(REST_ADMIN_USER, allRestAdminPermissions()) |
| 35 | + .withRestAdminUser(REST_API_ADMIN_CONFIG_UPDATE, restAdminPermission(Endpoint.CONFIG, SECURITY_CONFIG_UPDATE)); |
| 36 | + } |
| 37 | + |
| 38 | + private String securityConfigPath(final String... path) { |
| 39 | + final var fullPath = new StringJoiner("/").add(super.apiPath("securityconfig")); |
| 40 | + if (path != null) for (final var p : path) |
| 41 | + fullPath.add(p); |
| 42 | + return fullPath.toString(); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void forbiddenForRegularUsers() throws Exception { |
| 47 | + withUser(NEW_USER, client -> { |
| 48 | + forbidden(() -> client.get(securityConfigPath())); |
| 49 | + forbidden(() -> client.putJson(securityConfigPath("config"), EMPTY_BODY)); |
| 50 | + forbidden(() -> client.patch(securityConfigPath(), EMPTY_BODY)); |
| 51 | + verifyNotAllowedMethods(client); |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void partiallyAvailableForAdminUser() throws Exception { |
| 57 | + withUser(ADMIN_USER_NAME, client -> ok(() -> client.get(securityConfigPath()))); |
| 58 | + withUser(ADMIN_USER_NAME, client -> { |
| 59 | + badRequest(() -> client.putJson(securityConfigPath("xxx"), EMPTY_BODY)); |
| 60 | + forbidden(() -> client.putJson(securityConfigPath("config"), EMPTY_BODY)); |
| 61 | + forbidden(() -> client.patch(securityConfigPath(), EMPTY_BODY)); |
| 62 | + }); |
| 63 | + withUser(ADMIN_USER_NAME, this::verifyNotAllowedMethods); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void availableForTlsAdminUser() throws Exception { |
| 68 | + withUser(ADMIN_USER_NAME, localCluster.getAdminCertificate(), client -> ok(() -> client.get(securityConfigPath()))); |
| 69 | + withUser(ADMIN_USER_NAME, localCluster.getAdminCertificate(), this::verifyUpdate); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void availableForRestAdminUser() throws Exception { |
| 74 | + withUser(REST_ADMIN_USER, client -> ok(() -> client.get(securityConfigPath()))); |
| 75 | + withUser(REST_ADMIN_USER, this::verifyUpdate); |
| 76 | + withUser(REST_API_ADMIN_CONFIG_UPDATE, this::verifyUpdate); |
| 77 | + } |
| 78 | + |
| 79 | + void verifyUpdate(final TestRestClient client) throws Exception { |
| 80 | + badRequest(() -> client.putJson(securityConfigPath("xxx"), EMPTY_BODY)); |
| 81 | + verifyNotAllowedMethods(client); |
| 82 | + |
| 83 | + final var configJson = ok(() -> client.get(securityConfigPath())).bodyAsJsonNode(); |
| 84 | + final var authFailureListeners = DefaultObjectMapper.objectMapper.createObjectNode(); |
| 85 | + authFailureListeners.set( |
| 86 | + "ip_rate_limiting", |
| 87 | + DefaultObjectMapper.objectMapper.createObjectNode() |
| 88 | + .put("type", "ip") |
| 89 | + .put("allowed_tries", 10) |
| 90 | + .put("time_window_seconds", 3_600) |
| 91 | + .put("block_expiry_seconds", 600) |
| 92 | + .put("max_blocked_clients", 100_000) |
| 93 | + .put("max_tracked_clients", 100_000) |
| 94 | + ); |
| 95 | + authFailureListeners.set( |
| 96 | + "internal_authentication_backend_limiting", |
| 97 | + DefaultObjectMapper.objectMapper.createObjectNode() |
| 98 | + .put("type", "username") |
| 99 | + .put("authentication_backend", "intern") |
| 100 | + .put("allowed_tries", 10) |
| 101 | + .put("time_window_seconds", 3_600) |
| 102 | + .put("block_expiry_seconds", 600) |
| 103 | + .put("max_blocked_clients", 100_000) |
| 104 | + .put("max_tracked_clients", 100_000) |
| 105 | + ); |
| 106 | + final var dynamicConfigJson = (ObjectNode) configJson.get("config").get("dynamic"); |
| 107 | + dynamicConfigJson.set("auth_failure_listeners", authFailureListeners); |
| 108 | + ok(() -> client.putJson(securityConfigPath("config"), DefaultObjectMapper.writeValueAsString(configJson.get("config"), false))); |
| 109 | + ok(() -> client.patch(securityConfigPath(), patch(replaceOp("/config/dynamic/hosts_resolver_mode", "other")))); |
| 110 | + } |
| 111 | + |
| 112 | + void verifyNotAllowedMethods(final TestRestClient client) throws Exception { |
| 113 | + methodNotAllowed(() -> client.postJson(securityConfigPath(), EMPTY_BODY)); |
| 114 | + methodNotAllowed(() -> client.delete(securityConfigPath())); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments