77
88package org .elasticsearch .xpack .core .security .action ;
99
10+ import org .apache .logging .log4j .LogManager ;
11+ import org .apache .logging .log4j .Logger ;
1012import org .elasticsearch .action .ActionListener ;
1113import org .elasticsearch .action .ActionRequestValidationException ;
1214import org .elasticsearch .action .ActionType ;
1921import org .elasticsearch .cluster .block .ClusterBlockException ;
2022import org .elasticsearch .cluster .block .ClusterBlockLevel ;
2123import org .elasticsearch .cluster .metadata .IndexMetadata ;
24+ import org .elasticsearch .cluster .metadata .Metadata ;
25+ import org .elasticsearch .cluster .metadata .ProjectId ;
2226import org .elasticsearch .cluster .metadata .ProjectMetadata ;
27+ import org .elasticsearch .cluster .project .ProjectResolver ;
2328import org .elasticsearch .cluster .service .ClusterService ;
2429import org .elasticsearch .cluster .service .MasterServiceTaskQueue ;
2530import org .elasticsearch .common .Priority ;
4146 */
4247public class UpdateIndexMigrationVersionAction extends ActionType <UpdateIndexMigrationVersionResponse > {
4348
49+ private static final Logger logger = LogManager .getLogger (UpdateIndexMigrationVersionAction .class );
50+
4451 public static final UpdateIndexMigrationVersionAction INSTANCE = new UpdateIndexMigrationVersionAction ();
4552 public static final String NAME = "internal:index/metadata/migration_version/update" ;
4653 public static final String MIGRATION_VERSION_CUSTOM_KEY = "migration_version" ;
@@ -89,13 +96,15 @@ public String getIndexName() {
8996
9097 public static class TransportAction extends TransportMasterNodeAction <Request , UpdateIndexMigrationVersionResponse > {
9198 private final MasterServiceTaskQueue <UpdateIndexMigrationVersionTask > updateIndexMigrationVersionTaskQueue ;
99+ private final ProjectResolver projectResolver ;
92100
93101 @ Inject
94102 public TransportAction (
95103 TransportService transportService ,
96104 ClusterService clusterService ,
97105 ThreadPool threadPool ,
98- ActionFilters actionFilters
106+ ActionFilters actionFilters ,
107+ ProjectResolver projectResolver
99108 ) {
100109 super (
101110 UpdateIndexMigrationVersionAction .NAME ,
@@ -112,6 +121,7 @@ public TransportAction(
112121 Priority .LOW ,
113122 UPDATE_INDEX_MIGRATION_VERSION_TASK_EXECUTOR
114123 );
124+ this .projectResolver = projectResolver ;
115125 }
116126
117127 private static final SimpleBatchedExecutor <UpdateIndexMigrationVersionTask , Void > UPDATE_INDEX_MIGRATION_VERSION_TASK_EXECUTOR =
@@ -131,15 +141,34 @@ static class UpdateIndexMigrationVersionTask implements ClusterStateTaskListener
131141 private final ActionListener <Void > listener ;
132142 private final int indexMigrationVersion ;
133143 private final String indexName ;
134-
135- UpdateIndexMigrationVersionTask (ActionListener <Void > listener , int indexMigrationVersion , String indexName ) {
144+ private final ProjectId projectId ;
145+
146+ UpdateIndexMigrationVersionTask (
147+ ActionListener <Void > listener ,
148+ int indexMigrationVersion ,
149+ String indexName ,
150+ ProjectId projectId
151+ ) {
136152 this .listener = listener ;
137153 this .indexMigrationVersion = indexMigrationVersion ;
138154 this .indexName = indexName ;
155+ this .projectId = projectId ;
139156 }
140157
141158 ClusterState execute (ClusterState currentState ) {
142- final var project = currentState .metadata ().getProject ();
159+ final Metadata metadata = currentState .metadata ();
160+ if (metadata .hasProject (projectId ) == false ) {
161+ // project has been deleted? nothing to do
162+ logger .warn (
163+ "Cannot update security index [{}] in project [{}] to migration-version [{}]"
164+ + " because it does not exist in cluster state" ,
165+ indexName ,
166+ projectId ,
167+ indexMigrationVersion
168+ );
169+ return currentState ;
170+ }
171+ final var project = metadata .getProject (projectId );
143172 IndexMetadata .Builder indexMetadataBuilder = IndexMetadata .builder (project .indices ().get (indexName ));
144173 indexMetadataBuilder .putCustom (
145174 MIGRATION_VERSION_CUSTOM_KEY ,
@@ -168,20 +197,30 @@ protected void masterOperation(
168197 ClusterState state ,
169198 ActionListener <UpdateIndexMigrationVersionResponse > listener
170199 ) throws Exception {
200+ final ProjectId projectId = projectResolver .getProjectId ();
171201 updateIndexMigrationVersionTaskQueue .submitTask (
172202 "Updating cluster state with a new index migration version" ,
173- new UpdateIndexMigrationVersionTask (
174- ActionListener .wrap (response -> listener .onResponse (new UpdateIndexMigrationVersionResponse ()), listener ::onFailure ),
175- request .getIndexMigrationVersion (),
176- request .getIndexName ()
177- ),
203+ new UpdateIndexMigrationVersionTask (ActionListener .wrap (response -> {
204+ logger .info (
205+ "Updated project=[{}] index=[{}] to migration-version=[{}]" ,
206+ projectId ,
207+ request .getIndexName (),
208+ request .getIndexMigrationVersion ()
209+ );
210+ listener .onResponse (new UpdateIndexMigrationVersionResponse ());
211+ }, listener ::onFailure ), request .getIndexMigrationVersion (), request .getIndexName (), projectId ),
178212 null
179213 );
180214 }
181215
182216 @ Override
183217 protected ClusterBlockException checkBlock (Request request , ClusterState state ) {
184- return state .blocks ().indicesBlockedException (ClusterBlockLevel .METADATA_WRITE , new String [] { request .getIndexName () });
218+ return state .blocks ()
219+ .indicesBlockedException (
220+ projectResolver .getProjectId (),
221+ ClusterBlockLevel .METADATA_WRITE ,
222+ new String [] { request .getIndexName () }
223+ );
185224 }
186225 }
187226}
0 commit comments