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,33 @@ 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 [{}] because it does not exist in cluster state" ,
164+ indexName ,
165+ projectId ,
166+ indexMigrationVersion
167+ );
168+ return currentState ;
169+ }
170+ final var project = metadata .getProject (projectId );
143171 IndexMetadata .Builder indexMetadataBuilder = IndexMetadata .builder (project .indices ().get (indexName ));
144172 indexMetadataBuilder .putCustom (
145173 MIGRATION_VERSION_CUSTOM_KEY ,
@@ -168,20 +196,30 @@ protected void masterOperation(
168196 ClusterState state ,
169197 ActionListener <UpdateIndexMigrationVersionResponse > listener
170198 ) throws Exception {
199+ final ProjectId projectId = projectResolver .getProjectId ();
171200 updateIndexMigrationVersionTaskQueue .submitTask (
172201 "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- ),
202+ new UpdateIndexMigrationVersionTask (ActionListener .wrap (response -> {
203+ logger .info (
204+ "Updated project=[{}] index=[{}] to migration-version=[{}]" ,
205+ projectId ,
206+ request .getIndexName (),
207+ request .getIndexMigrationVersion ()
208+ );
209+ listener .onResponse (new UpdateIndexMigrationVersionResponse ());
210+ }, listener ::onFailure ), request .getIndexMigrationVersion (), request .getIndexName (), projectId ),
178211 null
179212 );
180213 }
181214
182215 @ Override
183216 protected ClusterBlockException checkBlock (Request request , ClusterState state ) {
184- return state .blocks ().indicesBlockedException (ClusterBlockLevel .METADATA_WRITE , new String [] { request .getIndexName () });
217+ return state .blocks ()
218+ .indicesBlockedException (
219+ projectResolver .getProjectId (),
220+ ClusterBlockLevel .METADATA_WRITE ,
221+ new String [] { request .getIndexName () }
222+ );
185223 }
186224 }
187225}
0 commit comments