@@ -277,7 +277,7 @@ private boolean canStart(ClusterState clusterState) {
277277 }
278278
279279 if (TemplateUtils .checkTemplateExistsAndVersionMatches (INDEX_TEMPLATE_NAME , SECURITY_VERSION_STRING ,
280- clusterState , logger , Version .CURRENT ::onOrAfter ) == false ) {
280+ clusterState , logger , Version .CURRENT ::onOrBefore ) == false ) {
281281 logger .debug ("security audit index template [{}] is not up to date" , INDEX_TEMPLATE_NAME );
282282 return false ;
283283 }
@@ -308,6 +308,15 @@ private String getIndexName() {
308308 return index ;
309309 }
310310
311+ private boolean hasStaleMessage () {
312+ final Message first = peek ();
313+ if (first == null ) {
314+ return false ;
315+ }
316+ return false == IndexNameResolver .resolve (first .timestamp , rollover )
317+ .equals (IndexNameResolver .resolve (DateTime .now (DateTimeZone .UTC ), rollover ));
318+ }
319+
311320 /**
312321 * Starts the service. The state is moved to {@link org.elasticsearch.xpack.security.audit.index.IndexAuditTrail.State#STARTING}
313322 * at the beginning of the method. The service's components are initialized and if the current node is the master, the index
@@ -381,7 +390,7 @@ void updateCurrentIndexMappingsIfNecessary(ClusterState state) {
381390 IndexMetaData indexMetaData = indices .get (0 );
382391 MappingMetaData docMapping = indexMetaData .mapping ("doc" );
383392 if (docMapping == null ) {
384- if (indexToRemoteCluster || state .nodes ().isLocalNodeElectedMaster ()) {
393+ if (indexToRemoteCluster || state .nodes ().isLocalNodeElectedMaster () || hasStaleMessage () ) {
385394 putAuditIndexMappingsAndStart (index );
386395 } else {
387396 logger .trace ("audit index [{}] is missing mapping for type [{}]" , index , DOC_TYPE );
@@ -399,7 +408,7 @@ void updateCurrentIndexMappingsIfNecessary(ClusterState state) {
399408 if (versionString != null && Version .fromString (versionString ).onOrAfter (Version .CURRENT )) {
400409 innerStart ();
401410 } else {
402- if (indexToRemoteCluster || state .nodes ().isLocalNodeElectedMaster ()) {
411+ if (indexToRemoteCluster || state .nodes ().isLocalNodeElectedMaster () || hasStaleMessage () ) {
403412 putAuditIndexMappingsAndStart (index );
404413 } else if (versionString == null ) {
405414 logger .trace ("audit index [{}] mapping is missing meta field [{}]" , index , SECURITY_VERSION_STRING );
0 commit comments