|
| 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.security; |
| 9 | + |
| 10 | +import org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsAction; |
| 11 | +import org.elasticsearch.action.search.TransportSearchShardsAction; |
| 12 | +import org.elasticsearch.action.support.TransportAction; |
| 13 | +import org.elasticsearch.action.support.single.shard.TransportSingleShardAction; |
| 14 | +import org.elasticsearch.common.inject.Binding; |
| 15 | +import org.elasticsearch.common.inject.TypeLiteral; |
| 16 | +import org.elasticsearch.datastreams.DataStreamsPlugin; |
| 17 | +import org.elasticsearch.index.rankeval.RankEvalPlugin; |
| 18 | +import org.elasticsearch.ingest.IngestTestPlugin; |
| 19 | +import org.elasticsearch.ingest.common.IngestCommonPlugin; |
| 20 | +import org.elasticsearch.node.Node; |
| 21 | +import org.elasticsearch.plugins.Plugin; |
| 22 | +import org.elasticsearch.reindex.ReindexPlugin; |
| 23 | +import org.elasticsearch.script.mustache.MustachePlugin; |
| 24 | +import org.elasticsearch.test.ESSingleNodeTestCase; |
| 25 | +import org.elasticsearch.xpack.analytics.AnalyticsPlugin; |
| 26 | +import org.elasticsearch.xpack.autoscaling.Autoscaling; |
| 27 | +import org.elasticsearch.xpack.ccr.Ccr; |
| 28 | +import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; |
| 29 | +import org.elasticsearch.xpack.core.security.action.apikey.CrossClusterApiKeyRoleDescriptorBuilder; |
| 30 | +import org.elasticsearch.xpack.core.security.authz.privilege.IndexPrivilege; |
| 31 | +import org.elasticsearch.xpack.downsample.Downsample; |
| 32 | +import org.elasticsearch.xpack.downsample.DownsampleShardPersistentTaskExecutor; |
| 33 | +import org.elasticsearch.xpack.eql.plugin.EqlPlugin; |
| 34 | +import org.elasticsearch.xpack.esql.plugin.EsqlPlugin; |
| 35 | +import org.elasticsearch.xpack.frozen.FrozenIndices; |
| 36 | +import org.elasticsearch.xpack.graph.Graph; |
| 37 | +import org.elasticsearch.xpack.ilm.IndexLifecycle; |
| 38 | +import org.elasticsearch.xpack.inference.InferencePlugin; |
| 39 | +import org.elasticsearch.xpack.profiling.ProfilingPlugin; |
| 40 | +import org.elasticsearch.xpack.rollup.Rollup; |
| 41 | +import org.elasticsearch.xpack.search.AsyncSearch; |
| 42 | +import org.elasticsearch.xpack.slm.SnapshotLifecycle; |
| 43 | +import org.elasticsearch.xpack.sql.plugin.SqlPlugin; |
| 44 | + |
| 45 | +import java.util.ArrayList; |
| 46 | +import java.util.Collection; |
| 47 | +import java.util.HashSet; |
| 48 | +import java.util.List; |
| 49 | +import java.util.Locale; |
| 50 | +import java.util.Set; |
| 51 | +import java.util.function.Predicate; |
| 52 | + |
| 53 | +public class CrossClusterShardTests extends ESSingleNodeTestCase { |
| 54 | + |
| 55 | + Set<String> MANUALLY_CHECKED_SHARD_ACTIONS = Set.of( |
| 56 | + // The request types for these actions are all subtypes of SingleShardRequest, and have been evaluated to make sure their |
| 57 | + // `shards()` methods return the correct thing. |
| 58 | + TransportSearchShardsAction.NAME, |
| 59 | + |
| 60 | + // These types have had the interface implemented manually. |
| 61 | + DownsampleShardPersistentTaskExecutor.DelegatingAction.NAME, |
| 62 | + |
| 63 | + // These actions do not have any references to shard IDs in their requests. |
| 64 | + ClusterSearchShardsAction.NAME |
| 65 | + ); |
| 66 | + |
| 67 | + Set<Class<?>> CHECKED_ABSTRACT_CLASSES = Set.of( |
| 68 | + // This abstract class implements the interface so we can assume all of its subtypes do so properly as well. |
| 69 | + TransportSingleShardAction.class |
| 70 | + ); |
| 71 | + |
| 72 | + @Override |
| 73 | + protected Collection<Class<? extends Plugin>> getPlugins() { |
| 74 | + final ArrayList<Class<? extends Plugin>> plugins = new ArrayList<>(super.getPlugins()); |
| 75 | + plugins.addAll( |
| 76 | + List.of( |
| 77 | + LocalStateCompositeXPackPlugin.class, |
| 78 | + AnalyticsPlugin.class, |
| 79 | + AsyncSearch.class, |
| 80 | + Autoscaling.class, |
| 81 | + Ccr.class, |
| 82 | + DataStreamsPlugin.class, |
| 83 | + Downsample.class, |
| 84 | + EqlPlugin.class, |
| 85 | + EsqlPlugin.class, |
| 86 | + FrozenIndices.class, |
| 87 | + Graph.class, |
| 88 | + IndexLifecycle.class, |
| 89 | + InferencePlugin.class, |
| 90 | + IngestCommonPlugin.class, |
| 91 | + IngestTestPlugin.class, |
| 92 | + MustachePlugin.class, |
| 93 | + ProfilingPlugin.class, |
| 94 | + RankEvalPlugin.class, |
| 95 | + ReindexPlugin.class, |
| 96 | + Rollup.class, |
| 97 | + SnapshotLifecycle.class, |
| 98 | + SqlPlugin.class |
| 99 | + ) |
| 100 | + ); |
| 101 | + return plugins; |
| 102 | + } |
| 103 | + |
| 104 | + @SuppressWarnings("rawtypes") |
| 105 | + public void testCheckForNewShardLevelTransportActions() throws Exception { |
| 106 | + Node node = node(); |
| 107 | + List<Binding<TransportAction>> transportActionBindings = node.injector().findBindingsByType(TypeLiteral.get(TransportAction.class)); |
| 108 | + Set<String> crossClusterPrivilegeNames = new HashSet<>(); |
| 109 | + crossClusterPrivilegeNames.addAll(List.of(CrossClusterApiKeyRoleDescriptorBuilder.CCS_INDICES_PRIVILEGE_NAMES)); |
| 110 | + crossClusterPrivilegeNames.addAll(List.of(CrossClusterApiKeyRoleDescriptorBuilder.CCR_INDICES_PRIVILEGE_NAMES)); |
| 111 | + |
| 112 | + List<String> shardActions = transportActionBindings.stream() |
| 113 | + .map(binding -> binding.getProvider().get()) |
| 114 | + .filter(action -> IndexPrivilege.get(crossClusterPrivilegeNames).predicate().test(action.actionName)) |
| 115 | + .filter(this::actionIsLikelyShardAction) |
| 116 | + .map(action -> action.actionName) |
| 117 | + .toList(); |
| 118 | + |
| 119 | + List<String> actionsNotOnAllowlist = shardActions.stream().filter(Predicate.not(MANUALLY_CHECKED_SHARD_ACTIONS::contains)).toList(); |
| 120 | + if (actionsNotOnAllowlist.isEmpty() == false) { |
| 121 | + fail(""" |
| 122 | + If this test fails, you likely just added a transport action, probably with `shard` in the name. Transport actions which |
| 123 | + operate on shards directly and can be used across clusters must meet some additional requirements in order to be |
| 124 | + handled correctly by all Elasticsearch infrastructure, so please make sure you have read the javadoc on the |
| 125 | + IndicesRequest.RemoteClusterShardRequest interface and implemented it if appropriate and not already appropriately |
| 126 | + implemented by a supertype, then add the name (as in "indices:data/read/get") of your new transport action to |
| 127 | + MANUALLY_CHECKED_SHARD_ACTIONS above. Found actions not in allowlist: |
| 128 | + """ + actionsNotOnAllowlist); |
| 129 | + } |
| 130 | + |
| 131 | + // Also make sure the allowlist stays up to date and doesn't have any unnecessary entries. |
| 132 | + List<String> actionsOnAllowlistNotFound = MANUALLY_CHECKED_SHARD_ACTIONS.stream() |
| 133 | + .filter(Predicate.not(shardActions::contains)) |
| 134 | + .toList(); |
| 135 | + if (actionsOnAllowlistNotFound.isEmpty() == false) { |
| 136 | + fail( |
| 137 | + "Some actions were on the allowlist but not found in the list of cross-cluster capable transport actions, please remove " |
| 138 | + + "these from MANUALLY_CHECKED_SHARD_ACTIONS if they have been removed from Elasticsearch: " |
| 139 | + + actionsOnAllowlistNotFound |
| 140 | + ); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Getting to the actual request classes themselves is made difficult by the design of Elasticsearch's transport |
| 146 | + * protocol infrastructure combined with JVM type erasure. Therefore, we resort to a crude heuristic here. |
| 147 | + * @param transportAction The transportport action to be checked. |
| 148 | + * @return True if the action is suspected of being an action which may operate on shards directly. |
| 149 | + */ |
| 150 | + private boolean actionIsLikelyShardAction(TransportAction<?, ?> transportAction) { |
| 151 | + Class<?> clazz = transportAction.getClass(); |
| 152 | + Set<Class<?>> classHeirarchy = new HashSet<>(); |
| 153 | + while (clazz != TransportAction.class) { |
| 154 | + classHeirarchy.add(clazz); |
| 155 | + clazz = clazz.getSuperclass(); |
| 156 | + } |
| 157 | + boolean hasCheckedSuperclass = classHeirarchy.stream().anyMatch(clz -> CHECKED_ABSTRACT_CLASSES.contains(clz)); |
| 158 | + boolean shardInClassName = classHeirarchy.stream().anyMatch(clz -> clz.getName().toLowerCase(Locale.ROOT).contains("shard")); |
| 159 | + return hasCheckedSuperclass == false |
| 160 | + && (shardInClassName |
| 161 | + || transportAction.actionName.toLowerCase(Locale.ROOT).contains("shard") |
| 162 | + || transportAction.actionName.toLowerCase(Locale.ROOT).contains("[s]")); |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments