|
| 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.ArrayList; |
| 14 | +import java.util.Collection; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Set; |
| 18 | +import java.util.StringJoiner; |
| 19 | +import java.util.stream.Collectors; |
| 20 | + |
| 21 | +import com.carrotsearch.randomizedtesting.RandomizedContext; |
| 22 | +import com.fasterxml.jackson.databind.JsonNode; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import org.opensearch.security.dlic.rest.api.Endpoint; |
| 26 | +import org.opensearch.security.dlic.rest.api.ssl.CertificateType; |
| 27 | +import org.opensearch.test.framework.TestSecurityConfig; |
| 28 | +import org.opensearch.test.framework.certificate.TestCertificates; |
| 29 | +import org.opensearch.test.framework.cluster.LocalOpenSearchCluster; |
| 30 | +import org.opensearch.test.framework.cluster.TestRestClient; |
| 31 | + |
| 32 | +import static org.hamcrest.CoreMatchers.containsString; |
| 33 | +import static org.hamcrest.CoreMatchers.is; |
| 34 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 35 | +import static org.opensearch.security.OpenSearchSecurityPlugin.PLUGINS_PREFIX; |
| 36 | +import static org.opensearch.security.dlic.rest.api.RestApiAdminPrivilegesEvaluator.CERTS_INFO_ACTION; |
| 37 | +import static org.opensearch.security.support.ConfigConstants.SECURITY_RESTAPI_ADMIN_ENABLED; |
| 38 | + |
| 39 | +public class CertificatesRestApiIntegrationTest extends AbstractApiIntegrationTest { |
| 40 | + |
| 41 | + final static String REST_API_ADMIN_SSL_INFO = "rest-api-admin-ssl-info"; |
| 42 | + |
| 43 | + final static String REGULAR_USER = "regular_user"; |
| 44 | + |
| 45 | + static { |
| 46 | + clusterSettings.put(SECURITY_RESTAPI_ADMIN_ENABLED, true); |
| 47 | + testSecurityConfig.roles( |
| 48 | + new TestSecurityConfig.Role("simple_user_role").clusterPermissions("cluster:admin/security/certificates/info") |
| 49 | + ) |
| 50 | + .rolesMapping(new TestSecurityConfig.RoleMapping("simple_user_role").users(REGULAR_USER, ADMIN_USER_NAME)) |
| 51 | + .user(new TestSecurityConfig.User(REGULAR_USER)) |
| 52 | + .withRestAdminUser(REST_ADMIN_USER, allRestAdminPermissions()) |
| 53 | + .withRestAdminUser(REST_API_ADMIN_SSL_INFO, restAdminPermission(Endpoint.SSL, CERTS_INFO_ACTION)); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + protected String apiPathPrefix() { |
| 58 | + return PLUGINS_PREFIX; |
| 59 | + } |
| 60 | + |
| 61 | + protected String sslCertsPath(String... path) { |
| 62 | + final var fullPath = new StringJoiner("/"); |
| 63 | + fullPath.add(super.apiPath("certificates")); |
| 64 | + if (path != null) { |
| 65 | + for (final var p : path) { |
| 66 | + fullPath.add(p); |
| 67 | + } |
| 68 | + } |
| 69 | + return fullPath.toString(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void forbiddenForRegularUser() throws Exception { |
| 74 | + withUser(REGULAR_USER, client -> forbidden(() -> client.get(sslCertsPath()))); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void forbiddenForAdminUser() throws Exception { |
| 79 | + withUser(ADMIN_USER_NAME, client -> forbidden(() -> client.get(sslCertsPath()))); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void availableForTlsAdmin() throws Exception { |
| 84 | + withUser(ADMIN_USER_NAME, localCluster.getAdminCertificate(), this::verifySSLCertsInfo); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void availableForRestAdmin() throws Exception { |
| 89 | + withUser(REST_ADMIN_USER, this::verifySSLCertsInfo); |
| 90 | + withUser(REST_API_ADMIN_SSL_INFO, this::verifySSLCertsInfo); |
| 91 | + } |
| 92 | + |
| 93 | + private void verifySSLCertsInfo(final TestRestClient client) throws Exception { |
| 94 | + assertSSLCertsInfo( |
| 95 | + localCluster.nodes(), |
| 96 | + Set.of(CertificateType.HTTP, CertificateType.TRANSPORT), |
| 97 | + ok(() -> client.get(sslCertsPath())) |
| 98 | + ); |
| 99 | + if (localCluster.nodes().size() > 1) { |
| 100 | + final var randomNodes = randomNodes(); |
| 101 | + final var nodeIds = randomNodes.stream().map(n -> n.esNode().getNodeEnvironment().nodeId()).collect(Collectors.joining(",")); |
| 102 | + assertSSLCertsInfo( |
| 103 | + randomNodes, |
| 104 | + Set.of(CertificateType.HTTP, CertificateType.TRANSPORT), |
| 105 | + ok(() -> client.get(sslCertsPath(nodeIds))) |
| 106 | + ); |
| 107 | + } |
| 108 | + final var randomCertType = randomFrom(List.of(CertificateType.HTTP, CertificateType.TRANSPORT)); |
| 109 | + assertSSLCertsInfo( |
| 110 | + localCluster.nodes(), |
| 111 | + Set.of(randomCertType), |
| 112 | + ok(() -> client.get(String.format("%s?cert_type=%s", sslCertsPath(), randomCertType))) |
| 113 | + ); |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + private void assertSSLCertsInfo( |
| 118 | + final List<LocalOpenSearchCluster.Node> expectedNode, |
| 119 | + final Set<CertificateType> expectedCertTypes, |
| 120 | + final TestRestClient.HttpResponse response |
| 121 | + ) { |
| 122 | + final var body = response.bodyAsJsonNode(); |
| 123 | + final var prettyStringBody = body.toPrettyString(); |
| 124 | + |
| 125 | + final var _nodes = body.get("_nodes"); |
| 126 | + assertThat(prettyStringBody, _nodes.get("total").asInt(), is(expectedNode.size())); |
| 127 | + assertThat(prettyStringBody, _nodes.get("successful").asInt(), is(expectedNode.size())); |
| 128 | + assertThat(prettyStringBody, _nodes.get("failed").asInt(), is(0)); |
| 129 | + assertThat(prettyStringBody, body.get("cluster_name").asText(), is(localCluster.getClusterName())); |
| 130 | + |
| 131 | + final var nodes = body.get("nodes"); |
| 132 | + |
| 133 | + for (final var n : expectedNode) { |
| 134 | + final var esNode = n.esNode(); |
| 135 | + final var node = nodes.get(esNode.getNodeEnvironment().nodeId()); |
| 136 | + assertThat(prettyStringBody, node.get("name").asText(), is(n.getNodeName())); |
| 137 | + assertThat(prettyStringBody, node.has("certificates")); |
| 138 | + final var certificates = node.get("certificates"); |
| 139 | + if (expectedCertTypes.contains(CertificateType.HTTP)) { |
| 140 | + final var httpCertificates = certificates.get(CertificateType.HTTP.value()); |
| 141 | + assertThat(prettyStringBody, httpCertificates.isArray()); |
| 142 | + assertThat(prettyStringBody, httpCertificates.size(), is(1)); |
| 143 | + verifyCertsJson(n.nodeNumber(), httpCertificates.get(0)); |
| 144 | + } |
| 145 | + if (expectedCertTypes.contains(CertificateType.TRANSPORT)) { |
| 146 | + final var transportCertificates = certificates.get(CertificateType.TRANSPORT.value()); |
| 147 | + assertThat(prettyStringBody, transportCertificates.isArray()); |
| 148 | + assertThat(prettyStringBody, transportCertificates.size(), is(1)); |
| 149 | + verifyCertsJson(n.nodeNumber(), transportCertificates.get(0)); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + } |
| 154 | + |
| 155 | + private void verifyCertsJson(final int nodeNumber, final JsonNode jsonNode) { |
| 156 | + assertThat(jsonNode.toPrettyString(), jsonNode.get("issuer_dn").asText(), is(TestCertificates.CA_SUBJECT)); |
| 157 | + assertThat( |
| 158 | + jsonNode.toPrettyString(), |
| 159 | + jsonNode.get("subject_dn").asText(), |
| 160 | + is(String.format(TestCertificates.NODE_SUBJECT_PATTERN, nodeNumber)) |
| 161 | + ); |
| 162 | + assertThat( |
| 163 | + jsonNode.toPrettyString(), |
| 164 | + jsonNode.get("san").asText(), |
| 165 | + containsString(String.format("node-%s.example.com", nodeNumber)) |
| 166 | + ); |
| 167 | + assertThat(jsonNode.toPrettyString(), jsonNode.has("not_before")); |
| 168 | + assertThat(jsonNode.toPrettyString(), jsonNode.has("not_after")); |
| 169 | + } |
| 170 | + |
| 171 | + private List<LocalOpenSearchCluster.Node> randomNodes() { |
| 172 | + final var nodes = localCluster.nodes(); |
| 173 | + int leaveElements = randomIntBetween(1, nodes.size() - 1); |
| 174 | + return randomSubsetOf(leaveElements, nodes); |
| 175 | + } |
| 176 | + |
| 177 | + public <T> List<T> randomSubsetOf(int size, Collection<T> collection) { |
| 178 | + if (size > collection.size()) { |
| 179 | + throw new IllegalArgumentException( |
| 180 | + "Can't pick " + size + " random objects from a collection of " + collection.size() + " objects" |
| 181 | + ); |
| 182 | + } |
| 183 | + List<T> tempList = new ArrayList<>(collection); |
| 184 | + Collections.shuffle(tempList, RandomizedContext.current().getRandom()); |
| 185 | + return tempList.subList(0, size); |
| 186 | + } |
| 187 | + |
| 188 | +} |
0 commit comments