109
109
import static org .elasticsearch .xpack .security .audit .AuditUtil .indices ;
110
110
import static org .elasticsearch .xpack .security .audit .AuditUtil .restRequestContent ;
111
111
import static org .elasticsearch .xpack .security .audit .index .IndexNameResolver .resolve ;
112
+ import static org .elasticsearch .xpack .security .audit .index .IndexNameResolver .resolveNext ;
112
113
import static org .elasticsearch .xpack .security .support .SecurityIndexManager .SECURITY_VERSION_STRING ;
113
114
114
115
/**
@@ -308,6 +309,17 @@ private String getIndexName() {
308
309
return index ;
309
310
}
310
311
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
+
311
323
private boolean hasStaleMessage () {
312
324
final Message first = peek ();
313
325
if (first == null ) {
@@ -337,7 +349,7 @@ public void onResponse(ClusterStateResponse clusterStateResponse) {
337
349
updateCurrentIndexMappingsIfNecessary (clusterStateResponse .getState ());
338
350
} else if (TemplateUtils .checkTemplateExistsAndVersionMatches (INDEX_TEMPLATE_NAME ,
339
351
SECURITY_VERSION_STRING , clusterStateResponse .getState (), logger ,
340
- Version .CURRENT ::onOrAfter ) == false ) {
352
+ Version .CURRENT ::onOrBefore ) == false ) {
341
353
putTemplate (customAuditIndexSettings (settings , logger ),
342
354
e -> {
343
355
logger .error ("failed to put audit trail template" , e );
@@ -377,6 +389,7 @@ public void onFailure(Exception e) {
377
389
378
390
// pkg private for tests
379
391
void updateCurrentIndexMappingsIfNecessary (ClusterState state ) {
392
+ final String nextIndex = getNextIndexName ();
380
393
final String index = getIndexName ();
381
394
382
395
AliasOrIndex aliasOrIndex = state .getMetaData ().getAliasAndIndexLookup ().get (index );
@@ -391,48 +404,60 @@ void updateCurrentIndexMappingsIfNecessary(ClusterState state) {
391
404
MappingMetaData docMapping = indexMetaData .mapping ("doc" );
392
405
if (docMapping == null ) {
393
406
if (indexToRemoteCluster || state .nodes ().isLocalNodeElectedMaster () || hasStaleMessage ()) {
394
- putAuditIndexMappingsAndStart (index );
407
+ putAuditIndexMappingsAndStart (index , nextIndex );
395
408
} 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 );
397
410
transitionStartingToInitialized ();
398
411
}
399
412
} else {
400
413
@ SuppressWarnings ("unchecked" )
401
414
Map <String , Object > meta = (Map <String , Object >) docMapping .sourceAsMap ().get ("_meta" );
402
415
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 );
411
417
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 );
416
419
} 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 );
418
421
transitionStartingToInitialized ();
419
422
}
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
+ }
420
438
}
421
439
}
422
440
} else {
423
441
innerStart ();
424
442
}
425
443
}
426
444
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 );
431
455
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
435
456
}));
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
+ }));
436
461
}
437
462
438
463
private void transitionStartingToInitialized () {
@@ -451,7 +476,7 @@ void innerStart() {
451
476
assert false : message ;
452
477
logger .error (message );
453
478
} 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 ());
455
480
}
456
481
}
457
482
0 commit comments