|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.remotecluster; |
| 9 | + |
| 10 | +import org.elasticsearch.client.Request; |
| 11 | +import org.elasticsearch.client.RequestOptions; |
| 12 | +import org.elasticsearch.client.Response; |
| 13 | +import org.elasticsearch.common.xcontent.support.XContentMapValues; |
| 14 | +import org.elasticsearch.tasks.Task; |
| 15 | +import org.elasticsearch.test.cluster.ElasticsearchCluster; |
| 16 | +import org.elasticsearch.test.cluster.local.distribution.DistributionType; |
| 17 | +import org.elasticsearch.test.cluster.util.resource.Resource; |
| 18 | +import org.elasticsearch.xcontent.XContentParser; |
| 19 | +import org.elasticsearch.xcontent.XContentParserConfiguration; |
| 20 | +import org.elasticsearch.xcontent.spi.XContentProvider; |
| 21 | +import org.hamcrest.Matcher; |
| 22 | +import org.hamcrest.StringDescription; |
| 23 | +import org.junit.ClassRule; |
| 24 | +import org.junit.rules.RuleChain; |
| 25 | +import org.junit.rules.TestRule; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.HashSet; |
| 31 | +import java.util.Map; |
| 32 | +import java.util.Set; |
| 33 | +import java.util.concurrent.CountDownLatch; |
| 34 | +import java.util.concurrent.TimeUnit; |
| 35 | +import java.util.concurrent.atomic.AtomicReference; |
| 36 | +import java.util.function.Consumer; |
| 37 | +import java.util.function.Predicate; |
| 38 | +import java.util.stream.Collectors; |
| 39 | + |
| 40 | +import static org.hamcrest.Matchers.equalTo; |
| 41 | + |
| 42 | +public class RemoteClusterSecurityWithApmTracingRestIT extends AbstractRemoteClusterSecurityTestCase { |
| 43 | + private static final AtomicReference<Map<String, Object>> API_KEY_MAP_REF = new AtomicReference<>(); |
| 44 | + private static final XContentProvider.FormatProvider XCONTENT = XContentProvider.provider().getJsonXContent(); |
| 45 | + final String traceIdValue = "0af7651916cd43dd8448eb211c80319c"; |
| 46 | + final String traceParentValue = "00-" + traceIdValue + "-b7ad6b7169203331-01"; |
| 47 | + |
| 48 | + private static final ConsumingTestServer mockApmServer = new ConsumingTestServer(); |
| 49 | + |
| 50 | + static { |
| 51 | + fulfillingCluster = ElasticsearchCluster.local() |
| 52 | + .distribution(DistributionType.DEFAULT) |
| 53 | + .name("fulfilling-cluster") |
| 54 | + .apply(commonClusterConfig) |
| 55 | + .setting("telemetry.metrics.enabled", "false") |
| 56 | + .setting("telemetry.tracing.enabled", "true") |
| 57 | + .setting("telemetry.agent.metrics_interval", "1s") |
| 58 | + .setting("telemetry.agent.server_url", () -> "http://127.0.0.1:" + mockApmServer.getPort()) |
| 59 | + // to ensure tracestate header is always set to cover RCS 2.0 handling of the tracestate header |
| 60 | + .setting("telemetry.agent.transaction_sample_rate", "1.0") |
| 61 | + .setting("remote_cluster_server.enabled", "true") |
| 62 | + .setting("remote_cluster.port", "0") |
| 63 | + .setting("xpack.security.remote_cluster_server.ssl.enabled", "true") |
| 64 | + .setting("xpack.security.remote_cluster_server.ssl.key", "remote-cluster.key") |
| 65 | + .setting("xpack.security.remote_cluster_server.ssl.certificate", "remote-cluster.crt") |
| 66 | + .keystore("xpack.security.remote_cluster_server.ssl.secure_key_passphrase", "remote-cluster-password") |
| 67 | + .rolesFile(Resource.fromClasspath("roles.yml")) |
| 68 | + .build(); |
| 69 | + |
| 70 | + queryCluster = ElasticsearchCluster.local() |
| 71 | + .distribution(DistributionType.DEFAULT) |
| 72 | + .name("query-cluster") |
| 73 | + .apply(commonClusterConfig) |
| 74 | + .setting("telemetry.metrics.enabled", "false") |
| 75 | + .setting("telemetry.tracing.enabled", "true") |
| 76 | + // to ensure tracestate header is always set to cover RCS 2.0 handling of the tracestate header |
| 77 | + .setting("telemetry.agent.transaction_sample_rate", "1.0") |
| 78 | + .setting("telemetry.agent.metrics_interval", "1s") |
| 79 | + .setting("telemetry.agent.server_url", () -> "http://127.0.0.1:" + mockApmServer.getPort()) |
| 80 | + .setting("xpack.security.remote_cluster_client.ssl.enabled", "true") |
| 81 | + .setting("xpack.security.remote_cluster_client.ssl.certificate_authorities", "remote-cluster-ca.crt") |
| 82 | + .keystore("cluster.remote.my_remote_cluster.credentials", () -> { |
| 83 | + if (API_KEY_MAP_REF.get() == null) { |
| 84 | + final Map<String, Object> apiKeyMap = createCrossClusterAccessApiKey(""" |
| 85 | + { |
| 86 | + "search": [ |
| 87 | + { |
| 88 | + "names": ["*"] |
| 89 | + } |
| 90 | + ] |
| 91 | + }"""); |
| 92 | + API_KEY_MAP_REF.set(apiKeyMap); |
| 93 | + } |
| 94 | + return (String) API_KEY_MAP_REF.get().get("encoded"); |
| 95 | + }) |
| 96 | + .rolesFile(Resource.fromClasspath("roles.yml")) |
| 97 | + .user(REMOTE_METRIC_USER, PASS.toString(), "read_remote_shared_metrics", false) |
| 98 | + .build(); |
| 99 | + } |
| 100 | + |
| 101 | + @ClassRule |
| 102 | + // Use a RuleChain to ensure that fulfilling cluster is started before query cluster |
| 103 | + public static TestRule clusterRule = RuleChain.outerRule(mockApmServer).around(fulfillingCluster).around(queryCluster); |
| 104 | + |
| 105 | + @SuppressWarnings("unchecked") |
| 106 | + public void testTracingCrossCluster() throws Exception { |
| 107 | + configureRemoteCluster(); |
| 108 | + Set<Predicate<Map<String, Object>>> assertions = new HashSet<>( |
| 109 | + Set.of( |
| 110 | + // REST action on query cluster |
| 111 | + allTrue( |
| 112 | + transactionValue("name", equalTo("GET /_resolve/cluster/{name}")), |
| 113 | + transactionValue("trace_id", equalTo(traceIdValue)) |
| 114 | + ), |
| 115 | + // transport action on fulfilling cluster |
| 116 | + allTrue( |
| 117 | + transactionValue("name", equalTo("indices:admin/resolve/cluster")), |
| 118 | + transactionValue("trace_id", equalTo(traceIdValue)) |
| 119 | + ) |
| 120 | + ) |
| 121 | + ); |
| 122 | + |
| 123 | + CountDownLatch finished = new CountDownLatch(1); |
| 124 | + |
| 125 | + // a consumer that will remove the assertions from a map once it matched |
| 126 | + Consumer<String> messageConsumer = (String message) -> { |
| 127 | + var apmMessage = parseMap(message); |
| 128 | + if (isTransactionTraceMessage(apmMessage)) { |
| 129 | + logger.info("Apm transaction message received: {}", message); |
| 130 | + assertions.removeIf(e -> e.test(apmMessage)); |
| 131 | + } |
| 132 | + |
| 133 | + if (assertions.isEmpty()) { |
| 134 | + finished.countDown(); |
| 135 | + } |
| 136 | + }; |
| 137 | + |
| 138 | + mockApmServer.addMessageConsumer(messageConsumer); |
| 139 | + |
| 140 | + // Trigger an action that we know will cross clusters -- doesn't much matter which one |
| 141 | + final Request resolveRequest = new Request("GET", "/_resolve/cluster/my_remote_cluster:*"); |
| 142 | + resolveRequest.setOptions( |
| 143 | + RequestOptions.DEFAULT.toBuilder() |
| 144 | + .addHeader("Authorization", headerFromRandomAuthMethod(REMOTE_METRIC_USER, PASS)) |
| 145 | + .addHeader(Task.TRACE_PARENT_HTTP_HEADER, traceParentValue) |
| 146 | + ); |
| 147 | + final Response response = client().performRequest(resolveRequest); |
| 148 | + assertOK(response); |
| 149 | + |
| 150 | + finished.await(30, TimeUnit.SECONDS); |
| 151 | + assertThat(assertions, equalTo(Collections.emptySet())); |
| 152 | + } |
| 153 | + |
| 154 | + private boolean isTransactionTraceMessage(Map<String, Object> apmMessage) { |
| 155 | + return apmMessage.containsKey("transaction"); |
| 156 | + } |
| 157 | + |
| 158 | + @SuppressWarnings("unchecked") |
| 159 | + private Predicate<Map<String, Object>> allTrue(Predicate<Map<String, Object>>... predicates) { |
| 160 | + var allTrueTest = Arrays.stream(predicates).reduce(v -> true, Predicate::and); |
| 161 | + return new Predicate<>() { |
| 162 | + @Override |
| 163 | + public boolean test(Map<String, Object> map) { |
| 164 | + return allTrueTest.test(map); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public String toString() { |
| 169 | + return Arrays.stream(predicates).map(Object::toString).collect(Collectors.joining(" and ")); |
| 170 | + } |
| 171 | + }; |
| 172 | + } |
| 173 | + |
| 174 | + @SuppressWarnings("unchecked") |
| 175 | + private <T> Predicate<Map<String, Object>> transactionValue(String path, Matcher<T> expected) { |
| 176 | + return new Predicate<>() { |
| 177 | + @Override |
| 178 | + public boolean test(Map<String, Object> map) { |
| 179 | + var transaction = (Map<String, Object>) map.get("transaction"); |
| 180 | + var value = XContentMapValues.extractValue(path, transaction); |
| 181 | + return expected.matches((T) value); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public String toString() { |
| 186 | + StringDescription matcherDescription = new StringDescription(); |
| 187 | + expected.describeTo(matcherDescription); |
| 188 | + return path + " " + matcherDescription; |
| 189 | + } |
| 190 | + }; |
| 191 | + } |
| 192 | + |
| 193 | + private Map<String, Object> parseMap(String message) { |
| 194 | + try (XContentParser parser = XCONTENT.XContent().createParser(XContentParserConfiguration.EMPTY, message)) { |
| 195 | + return parser.map(); |
| 196 | + } catch (IOException e) { |
| 197 | + fail(e); |
| 198 | + return Collections.emptyMap(); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments