109109import static org .elasticsearch .xpack .security .audit .AuditUtil .indices ;
110110import static org .elasticsearch .xpack .security .audit .AuditUtil .restRequestContent ;
111111import static org .elasticsearch .xpack .security .audit .index .IndexNameResolver .resolve ;
112+ import static org .elasticsearch .xpack .security .audit .index .IndexNameResolver .resolveNext ;
112113import static org .elasticsearch .xpack .security .support .SecurityIndexManager .SECURITY_VERSION_STRING ;
113114
114115/**
@@ -308,6 +309,17 @@ private String getIndexName() {
308309 return index ;
309310 }
310311
312+ private String getNextIndexName () {
313+ final Message first = peek ();
314+ final String index ;
315+ if (first == null ) {
316+ index = resolveNext (IndexAuditTrailField .INDEX_NAME_PREFIX , DateTime .now (DateTimeZone .UTC ), rollover );
317+ } else {
318+ index = resolveNext (IndexAuditTrailField .INDEX_NAME_PREFIX , first .timestamp , rollover );
319+ }
320+ return index ;
321+ }
322+
311323 private boolean hasStaleMessage () {
312324 final Message first = peek ();
313325 if (first == null ) {
@@ -337,7 +349,7 @@ public void onResponse(ClusterStateResponse clusterStateResponse) {
337349 updateCurrentIndexMappingsIfNecessary (clusterStateResponse .getState ());
338350 } else if (TemplateUtils .checkTemplateExistsAndVersionMatches (INDEX_TEMPLATE_NAME ,
339351 SECURITY_VERSION_STRING , clusterStateResponse .getState (), logger ,
340- Version .CURRENT ::onOrAfter ) == false ) {
352+ Version .CURRENT ::onOrBefore ) == false ) {
341353 putTemplate (customAuditIndexSettings (settings , logger ),
342354 e -> {
343355 logger .error ("failed to put audit trail template" , e );
@@ -377,6 +389,7 @@ public void onFailure(Exception e) {
377389
378390 // pkg private for tests
379391 void updateCurrentIndexMappingsIfNecessary (ClusterState state ) {
392+ final String nextIndex = getNextIndexName ();
380393 final String index = getIndexName ();
381394
382395 AliasOrIndex aliasOrIndex = state .getMetaData ().getAliasAndIndexLookup ().get (index );
@@ -391,48 +404,60 @@ void updateCurrentIndexMappingsIfNecessary(ClusterState state) {
391404 MappingMetaData docMapping = indexMetaData .mapping ("doc" );
392405 if (docMapping == null ) {
393406 if (indexToRemoteCluster || state .nodes ().isLocalNodeElectedMaster () || hasStaleMessage ()) {
394- putAuditIndexMappingsAndStart (index );
407+ putAuditIndexMappingsAndStart (index , nextIndex );
395408 } else {
396- logger .trace ("audit index [{}] is missing mapping for type [{}]" , index , DOC_TYPE );
409+ logger .debug ("audit index [{}] is missing mapping for type [{}]" , index , DOC_TYPE );
397410 transitionStartingToInitialized ();
398411 }
399412 } else {
400413 @ SuppressWarnings ("unchecked" )
401414 Map <String , Object > meta = (Map <String , Object >) docMapping .sourceAsMap ().get ("_meta" );
402415 if (meta == null ) {
403- logger .info ("Missing _meta field in mapping [{}] of index [{}]" , docMapping .type (), index );
404- throw new IllegalStateException ("Cannot read security-version string in index " + index );
405- }
406-
407- final String versionString = (String ) meta .get (SECURITY_VERSION_STRING );
408- if (versionString != null && Version .fromString (versionString ).onOrAfter (Version .CURRENT )) {
409- innerStart ();
410- } else {
416+ logger .warn ("Missing _meta field in mapping [{}] of index [{}]" , docMapping .type (), index );
411417 if (indexToRemoteCluster || state .nodes ().isLocalNodeElectedMaster () || hasStaleMessage ()) {
412- putAuditIndexMappingsAndStart (index );
413- } else if (versionString == null ) {
414- logger .trace ("audit index [{}] mapping is missing meta field [{}]" , index , SECURITY_VERSION_STRING );
415- transitionStartingToInitialized ();
418+ putAuditIndexMappingsAndStart (index , nextIndex );
416419 } else {
417- logger .trace ("audit index [{}] has the incorrect version [{}]" , index , versionString );
420+ logger .debug ("audit index [{}] is missing _meta for type [{}]" , index , DOC_TYPE );
418421 transitionStartingToInitialized ();
419422 }
423+ } else {
424+ final String versionString = (String ) meta .get (SECURITY_VERSION_STRING );
425+ if (versionString != null && Version .fromString (versionString ).onOrAfter (Version .CURRENT )) {
426+ innerStart ();
427+ } else {
428+ if (indexToRemoteCluster || state .nodes ().isLocalNodeElectedMaster () || hasStaleMessage ()) {
429+ putAuditIndexMappingsAndStart (index , nextIndex );
430+ } else if (versionString == null ) {
431+ logger .debug ("audit index [{}] mapping is missing meta field [{}]" , index , SECURITY_VERSION_STRING );
432+ transitionStartingToInitialized ();
433+ } else {
434+ logger .debug ("audit index [{}] has the incorrect version [{}]" , index , versionString );
435+ transitionStartingToInitialized ();
436+ }
437+ }
420438 }
421439 }
422440 } else {
423441 innerStart ();
424442 }
425443 }
426444
427- private void putAuditIndexMappingsAndStart (String index ) {
428- putAuditIndexMappings (index , getPutIndexTemplateRequest (Settings .EMPTY ).mappings ().get (DOC_TYPE ),
429- ActionListener .wrap (ignore -> {
430- logger .trace ("updated mappings on audit index [{}]" , index );
445+ private void putAuditIndexMappingsAndStart (String index , String nextIndex ) {
446+ final String docMapping = getPutIndexTemplateRequest (Settings .EMPTY ).mappings ().get (DOC_TYPE );
447+ putAuditIndexMappings (index , docMapping , ActionListener .wrap (ignore -> {
448+ logger .debug ("updated mappings on audit index [{}]" , index );
449+ putAuditIndexMappings (nextIndex , docMapping , ActionListener .wrap (ignoreToo -> {
450+ logger .debug ("updated mappings on next audit index [{}]" , nextIndex );
451+ innerStart ();
452+ }, e2 -> {
453+ // best effort only
454+ logger .debug ("Failed to update mappings on next audit index [{}]" , nextIndex );
431455 innerStart ();
432- }, e -> {
433- logger .error (new ParameterizedMessage ("failed to update mappings on audit index [{}]" , index ), e );
434- transitionStartingToInitialized (); // reset to initialized so we can retry
435456 }));
457+ }, e -> {
458+ logger .error (new ParameterizedMessage ("failed to update mappings on audit index [{}]" , index ), e );
459+ transitionStartingToInitialized (); // reset to initialized so we can retry
460+ }));
436461 }
437462
438463 private void transitionStartingToInitialized () {
@@ -451,7 +476,7 @@ void innerStart() {
451476 assert false : message ;
452477 logger .error (message );
453478 } else {
454- logger .trace ("successful state transition from starting to started, current value: [{}]" , state .get ());
479+ logger .debug ("successful state transition from starting to started, current value: [{}]" , state .get ());
455480 }
456481 }
457482
0 commit comments