Skip to content

Commit 06adb19

Browse files
Fix IndexAuditTrail rolling restart on rollover edge. (#35988)
This fixes two independent bugs , both tripping integ test failures. They are both facilitated by the rolling nature of the audit index. Moreover, they will both manifest only during a rolling upgrade executed while the audit index rolls over.
1 parent e592bd1 commit 06adb19

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/index/IndexAuditTrail.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

x-pack/qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/IndexAuditUpgradeIT.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public void findMinVersionInCluster() throws IOException {
6262
}
6363
}
6464

65-
@AwaitsFix(bugUrl="https://github.com/elastic/elasticsearch/issues/35867")
6665
public void testAuditLogs() throws Exception {
6766
assertBusy(() -> {
6867
assertAuditDocsExist();

0 commit comments

Comments
 (0)