|
| 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.support; |
| 9 | + |
| 10 | +import org.elasticsearch.action.ActionListener; |
| 11 | +import org.elasticsearch.action.search.SearchRequest; |
| 12 | +import org.elasticsearch.action.search.SearchResponse; |
| 13 | +import org.elasticsearch.action.support.WriteRequest; |
| 14 | +import org.elasticsearch.cluster.ClusterState; |
| 15 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 16 | +import org.elasticsearch.cluster.service.ClusterService; |
| 17 | +import org.elasticsearch.core.TimeValue; |
| 18 | +import org.elasticsearch.index.query.QueryBuilders; |
| 19 | +import org.elasticsearch.painless.PainlessPlugin; |
| 20 | +import org.elasticsearch.plugins.Plugin; |
| 21 | +import org.elasticsearch.search.SearchHit; |
| 22 | +import org.elasticsearch.test.ESIntegTestCase; |
| 23 | +import org.elasticsearch.test.ESTestCase; |
| 24 | +import org.elasticsearch.test.SecurityIntegTestCase; |
| 25 | +import org.elasticsearch.xcontent.ToXContent; |
| 26 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 27 | +import org.elasticsearch.xpack.core.security.action.UpdateIndexMigrationVersionAction; |
| 28 | +import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; |
| 29 | +import org.elasticsearch.xpack.security.authz.store.NativeRolesStore; |
| 30 | +import org.junit.Before; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.util.Collection; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Map; |
| 36 | +import java.util.concurrent.ExecutorService; |
| 37 | +import java.util.concurrent.Executors; |
| 38 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 39 | +import java.util.concurrent.atomic.AtomicLong; |
| 40 | +import java.util.stream.Collectors; |
| 41 | +import java.util.stream.Stream; |
| 42 | + |
| 43 | +import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; |
| 44 | +import static org.elasticsearch.xpack.core.security.action.UpdateIndexMigrationVersionAction.MIGRATION_VERSION_CUSTOM_DATA_KEY; |
| 45 | +import static org.elasticsearch.xpack.core.security.action.UpdateIndexMigrationVersionAction.MIGRATION_VERSION_CUSTOM_KEY; |
| 46 | +import static org.elasticsearch.xpack.core.security.authz.RoleDescriptor.ROLE_TYPE; |
| 47 | +import static org.elasticsearch.xpack.core.security.test.TestRestrictedIndices.INTERNAL_SECURITY_MAIN_INDEX_7; |
| 48 | +import static org.elasticsearch.xpack.security.support.SecurityMigrations.ROLE_METADATA_FLATTENED_MIGRATION_VERSION; |
| 49 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 50 | + |
| 51 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0, autoManageMasterNodes = false) |
| 52 | +public class MetadataFlattenedMigrationIT extends SecurityIntegTestCase { |
| 53 | + |
| 54 | + private final AtomicLong versionCounter = new AtomicLong(1); |
| 55 | + |
| 56 | + @Before |
| 57 | + public void resetVersion() { |
| 58 | + versionCounter.set(1); |
| 59 | + } |
| 60 | + |
| 61 | + public void testMigrationSuccessful() throws Exception { |
| 62 | + internalCluster().setBootstrapMasterNodeIndex(0); |
| 63 | + internalCluster().startNode(); |
| 64 | + ensureGreen(); |
| 65 | + createRoles(); |
| 66 | + |
| 67 | + resetMigration(); |
| 68 | + waitForMigrationCompletion(ROLE_METADATA_FLATTENED_MIGRATION_VERSION); |
| 69 | + assertAllRolesHaveMetadataFlattened(); |
| 70 | + } |
| 71 | + |
| 72 | + public void testMigrationWithConcurrentUpdates() throws Exception { |
| 73 | + internalCluster().setBootstrapMasterNodeIndex(0); |
| 74 | + internalCluster().startNode(); |
| 75 | + ensureGreen(); |
| 76 | + |
| 77 | + waitForMigrationCompletion(ROLE_METADATA_FLATTENED_MIGRATION_VERSION); |
| 78 | + var roles = createRoles(); |
| 79 | + final var nativeRoleStore = internalCluster().getInstance(NativeRolesStore.class); |
| 80 | + |
| 81 | + try (ExecutorService executor = Executors.newSingleThreadExecutor()) { |
| 82 | + final AtomicBoolean runUpdateRolesBackground = new AtomicBoolean(true); |
| 83 | + executor.submit(() -> { |
| 84 | + while (runUpdateRolesBackground.get()) { |
| 85 | + // Only update half the list so the other half can be verified as migrated |
| 86 | + RoleDescriptor roleToUpdate = randomFrom(roles.subList(0, roles.size() / 2)); |
| 87 | + |
| 88 | + RoleDescriptor updatedRole = new RoleDescriptor( |
| 89 | + roleToUpdate.getName(), |
| 90 | + new String[] { "monitor" }, |
| 91 | + null, |
| 92 | + null, |
| 93 | + null, |
| 94 | + null, |
| 95 | + Map.of("test", "value", "timestamp", System.currentTimeMillis(), "random", randomAlphaOfLength(10)), |
| 96 | + null |
| 97 | + ); |
| 98 | + nativeRoleStore.putRole( |
| 99 | + WriteRequest.RefreshPolicy.IMMEDIATE, |
| 100 | + updatedRole, |
| 101 | + ActionListener.wrap(resp -> {}, ESTestCase::fail) |
| 102 | + ); |
| 103 | + try { |
| 104 | + Thread.sleep(10); |
| 105 | + } catch (InterruptedException e) { |
| 106 | + throw new RuntimeException(e); |
| 107 | + } |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + resetMigration(); |
| 112 | + try { |
| 113 | + waitForMigrationCompletion(ROLE_METADATA_FLATTENED_MIGRATION_VERSION); |
| 114 | + } finally { |
| 115 | + runUpdateRolesBackground.set(false); |
| 116 | + executor.shutdown(); |
| 117 | + } |
| 118 | + } |
| 119 | + assertAllRolesHaveMetadataFlattened(); |
| 120 | + } |
| 121 | + |
| 122 | + private void resetMigration() { |
| 123 | + client().execute( |
| 124 | + UpdateIndexMigrationVersionAction.INSTANCE, |
| 125 | + new UpdateIndexMigrationVersionAction.Request( |
| 126 | + TimeValue.MAX_VALUE, |
| 127 | + ROLE_METADATA_FLATTENED_MIGRATION_VERSION - 1, |
| 128 | + INTERNAL_SECURITY_MAIN_INDEX_7 |
| 129 | + ) |
| 130 | + ).actionGet(); |
| 131 | + } |
| 132 | + |
| 133 | + private List<RoleDescriptor> createRoles() throws IOException { |
| 134 | + var roles = randomList( |
| 135 | + 25, |
| 136 | + 50, |
| 137 | + () -> new RoleDescriptor( |
| 138 | + randomAlphaOfLength(20), |
| 139 | + null, |
| 140 | + null, |
| 141 | + null, |
| 142 | + null, |
| 143 | + null, |
| 144 | + Map.of("test", "value", "timestamp", System.currentTimeMillis(), "random", randomAlphaOfLength(10)), |
| 145 | + Map.of() |
| 146 | + ) |
| 147 | + ); |
| 148 | + for (RoleDescriptor role : roles) { |
| 149 | + indexRoleDirectly(role); |
| 150 | + } |
| 151 | + indicesAdmin().prepareRefresh(INTERNAL_SECURITY_MAIN_INDEX_7).get(); |
| 152 | + return roles; |
| 153 | + } |
| 154 | + |
| 155 | + private void indexRoleDirectly(RoleDescriptor role) throws IOException { |
| 156 | + XContentBuilder builder = buildRoleDocument(role); |
| 157 | + prepareIndex(INTERNAL_SECURITY_MAIN_INDEX_7).setId(ROLE_TYPE + "-" + role.getName()) |
| 158 | + .setSource(builder) |
| 159 | + .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) |
| 160 | + .get(); |
| 161 | + } |
| 162 | + |
| 163 | + private XContentBuilder buildRoleDocument(RoleDescriptor role) throws IOException { |
| 164 | + XContentBuilder builder = jsonBuilder().startObject(); |
| 165 | + // metadata_flattened is populated by the native role store, so write directly to index to simulate pre-migration state |
| 166 | + role.innerToXContent(builder, ToXContent.EMPTY_PARAMS, true); |
| 167 | + builder.endObject(); |
| 168 | + return builder; |
| 169 | + } |
| 170 | + |
| 171 | + private void assertMigrationVersionAtLeast(int expectedVersion) { |
| 172 | + assertThat(getCurrentMigrationVersion(), greaterThanOrEqualTo(expectedVersion)); |
| 173 | + } |
| 174 | + |
| 175 | + private int getCurrentMigrationVersion(ClusterState state) { |
| 176 | + IndexMetadata indexMetadata = state.metadata().getProject().index(INTERNAL_SECURITY_MAIN_INDEX_7); |
| 177 | + if (indexMetadata == null || indexMetadata.getCustomData(MIGRATION_VERSION_CUSTOM_KEY) == null) { |
| 178 | + return 0; |
| 179 | + } |
| 180 | + return Integer.parseInt(indexMetadata.getCustomData(MIGRATION_VERSION_CUSTOM_KEY).get(MIGRATION_VERSION_CUSTOM_DATA_KEY)); |
| 181 | + } |
| 182 | + |
| 183 | + private int getCurrentMigrationVersion() { |
| 184 | + ClusterService clusterService = internalCluster().getInstance(ClusterService.class); |
| 185 | + return getCurrentMigrationVersion(clusterService.state()); |
| 186 | + } |
| 187 | + |
| 188 | + private void waitForMigrationCompletion(int version) throws Exception { |
| 189 | + assertBusy(() -> assertMigrationVersionAtLeast(version)); |
| 190 | + } |
| 191 | + |
| 192 | + private void assertAllRolesHaveMetadataFlattened() { |
| 193 | + SearchRequest searchRequest = new SearchRequest(INTERNAL_SECURITY_MAIN_INDEX_7); |
| 194 | + searchRequest.source().query(QueryBuilders.termQuery("type", "role")).size(1000); |
| 195 | + SearchResponse response = client().search(searchRequest).actionGet(); |
| 196 | + for (SearchHit hit : response.getHits().getHits()) { |
| 197 | + @SuppressWarnings("unchecked") |
| 198 | + Map<String, Object> metadata = (Map<String, Object>) hit.getSourceAsMap().get("metadata_flattened"); |
| 199 | + // Only check non-reserved roles |
| 200 | + if (metadata.get("_reserved") == null) { |
| 201 | + assertEquals("value", metadata.get("test")); |
| 202 | + } |
| 203 | + } |
| 204 | + response.decRef(); |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 209 | + return Stream.concat(super.nodePlugins().stream(), Stream.of(PainlessPlugin.class)).collect(Collectors.toList()); |
| 210 | + } |
| 211 | +} |
0 commit comments