Skip to content

Commit 9ef69f7

Browse files
Audit log filter and marker (#45456)
This adds a log marker and a marker filter for the audit log. Closes #47251
1 parent 9b33165 commit 9ef69f7

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrail.java

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
package org.elasticsearch.xpack.security.audit.logfile;
77

88
import org.apache.logging.log4j.Logger;
9+
import org.apache.logging.log4j.Marker;
10+
import org.apache.logging.log4j.MarkerManager;
11+
import org.apache.logging.log4j.core.Filter.Result;
12+
import org.apache.logging.log4j.core.LoggerContext;
13+
import org.apache.logging.log4j.core.filter.MarkerFilter;
914
import org.apache.logging.log4j.message.StringMapMessage;
1015
import org.elasticsearch.action.IndicesRequest;
1116
import org.elasticsearch.cluster.ClusterChangedEvent;
@@ -15,6 +20,7 @@
1520
import org.elasticsearch.common.Nullable;
1621
import org.elasticsearch.common.Strings;
1722
import org.elasticsearch.common.collect.MapBuilder;
23+
import org.elasticsearch.common.logging.Loggers;
1824
import org.elasticsearch.common.network.NetworkAddress;
1925
import org.elasticsearch.common.settings.Setting;
2026
import org.elasticsearch.common.settings.Setting.Property;
@@ -32,6 +38,7 @@
3238
import org.elasticsearch.xpack.core.security.user.SystemUser;
3339
import org.elasticsearch.xpack.core.security.user.User;
3440
import org.elasticsearch.xpack.core.security.user.XPackUser;
41+
import org.elasticsearch.xpack.security.Security;
3542
import org.elasticsearch.xpack.security.audit.AuditLevel;
3643
import org.elasticsearch.xpack.security.audit.AuditTrail;
3744
import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine.AuthorizationInfo;
@@ -151,6 +158,8 @@ public class LoggingAuditTrail implements AuditTrail, ClusterStateListener {
151158
"indices",
152159
(key) -> Setting.listSetting(key, Collections.singletonList("*"), Function.identity(), Property.NodeScope, Property.Dynamic));
153160

161+
private static final Marker AUDIT_MARKER = MarkerManager.getMarker("org.elasticsearch.xpack.security.audit");
162+
154163
private final Logger logger;
155164
private final ThreadContext threadContext;
156165
final EventFilterPolicyRegistry eventFilterPolicyRegistry;
@@ -166,7 +175,7 @@ public String name() {
166175
}
167176

168177
public LoggingAuditTrail(Settings settings, ClusterService clusterService, ThreadPool threadPool) {
169-
this(settings, clusterService, LogManager.getLogger(), threadPool.getThreadContext());
178+
this(settings, clusterService, LogManager.getLogger(LoggingAuditTrail.class), threadPool.getThreadContext());
170179
}
171180

172181
LoggingAuditTrail(Settings settings, ClusterService clusterService, Logger logger, ThreadContext threadContext) {
@@ -207,6 +216,14 @@ public LoggingAuditTrail(Settings settings, ClusterService clusterService, Threa
207216
final EventFilterPolicy newPolicy = policy.orElse(new EventFilterPolicy(policyName, settings)).changeIndicesFilter(filtersList);
208217
this.eventFilterPolicyRegistry.set(policyName, newPolicy);
209218
}, (policyName, filtersList) -> EventFilterPolicy.parsePredicate(filtersList));
219+
// this log filter ensures that audit events are not filtered out because of the log level
220+
final LoggerContext ctx = LoggerContext.getContext(false);
221+
MarkerFilter auditMarkerFilter = MarkerFilter.createFilter(AUDIT_MARKER.getName(), Result.ACCEPT, Result.NEUTRAL);
222+
ctx.addFilter(auditMarkerFilter);
223+
ctx.updateLoggers();
224+
clusterService.getClusterSettings().addSettingsUpdateConsumer(ignored -> {
225+
LogManager.getLogger(Security.class).warn("Changing log level for [" + LoggingAuditTrail.class.getName() + "] has no effect");
226+
}, Collections.singletonList(Loggers.LOG_LEVEL_SETTING.getConcreteSettingForNamespace(LoggingAuditTrail.class.getName())));
210227
}
211228

212229
@Override
@@ -225,7 +242,7 @@ public void authenticationSuccess(String requestId, String realm, User user, Res
225242
.withOpaqueId(threadContext)
226243
.withXForwardedFor(threadContext)
227244
.build();
228-
logger.info(logEntry);
245+
logger.info(AUDIT_MARKER, logEntry);
229246
}
230247
}
231248

@@ -248,7 +265,7 @@ public void authenticationSuccess(String requestId, String realm, User user, Str
248265
.withOpaqueId(threadContext)
249266
.withXForwardedFor(threadContext)
250267
.build();
251-
logger.info(logEntry);
268+
logger.info(AUDIT_MARKER, logEntry);
252269
}
253270
}
254271
}
@@ -270,7 +287,7 @@ public void anonymousAccessDenied(String requestId, String action, TransportMess
270287
.withOpaqueId(threadContext)
271288
.withXForwardedFor(threadContext)
272289
.build();
273-
logger.info(logEntry);
290+
logger.info(AUDIT_MARKER, logEntry);
274291
}
275292
}
276293
}
@@ -289,7 +306,7 @@ public void anonymousAccessDenied(String requestId, RestRequest request) {
289306
.withOpaqueId(threadContext)
290307
.withXForwardedFor(threadContext)
291308
.build();
292-
logger.info(logEntry);
309+
logger.info(AUDIT_MARKER, logEntry);
293310
}
294311
}
295312

@@ -311,7 +328,7 @@ public void authenticationFailed(String requestId, AuthenticationToken token, St
311328
.withOpaqueId(threadContext)
312329
.withXForwardedFor(threadContext)
313330
.build();
314-
logger.info(logEntry);
331+
logger.info(AUDIT_MARKER, logEntry);
315332
}
316333
}
317334
}
@@ -329,7 +346,7 @@ public void authenticationFailed(String requestId, RestRequest request) {
329346
.withOpaqueId(threadContext)
330347
.withXForwardedFor(threadContext)
331348
.build();
332-
logger.info(logEntry);
349+
logger.info(AUDIT_MARKER, logEntry);
333350
}
334351
}
335352

@@ -350,7 +367,7 @@ public void authenticationFailed(String requestId, String action, TransportMessa
350367
.withOpaqueId(threadContext)
351368
.withXForwardedFor(threadContext)
352369
.build();
353-
logger.info(logEntry);
370+
logger.info(AUDIT_MARKER, logEntry);
354371
}
355372
}
356373
}
@@ -370,7 +387,7 @@ public void authenticationFailed(String requestId, AuthenticationToken token, Re
370387
.withOpaqueId(threadContext)
371388
.withXForwardedFor(threadContext)
372389
.build();
373-
logger.info(logEntry);
390+
logger.info(AUDIT_MARKER, logEntry);
374391
}
375392
}
376393

@@ -393,7 +410,7 @@ public void authenticationFailed(String requestId, String realm, AuthenticationT
393410
.withOpaqueId(threadContext)
394411
.withXForwardedFor(threadContext)
395412
.build();
396-
logger.info(logEntry);
413+
logger.info(AUDIT_MARKER, logEntry);
397414
}
398415
}
399416
}
@@ -414,7 +431,7 @@ public void authenticationFailed(String requestId, String realm, AuthenticationT
414431
.withOpaqueId(threadContext)
415432
.withXForwardedFor(threadContext)
416433
.build();
417-
logger.info(logEntry);
434+
logger.info(AUDIT_MARKER, logEntry);
418435
}
419436
}
420437

@@ -440,7 +457,7 @@ public void accessGranted(String requestId, Authentication authentication, Strin
440457
.withXForwardedFor(threadContext)
441458
.with(authorizationInfo.asMap())
442459
.build();
443-
logger.info(logEntry);
460+
logger.info(AUDIT_MARKER, logEntry);
444461
}
445462
}
446463
}
@@ -480,7 +497,7 @@ public void explicitIndexAccessEvent(String requestId, AuditLevel eventType, Aut
480497
.with(ORIGIN_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE)
481498
.with(ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(remoteAddress.address()));
482499
}
483-
logger.info(logEntryBuilder.build());
500+
logger.info(AUDIT_MARKER, logEntryBuilder.build());
484501
}
485502
}
486503
}
@@ -505,7 +522,7 @@ public void accessDenied(String requestId, Authentication authentication, String
505522
.withOpaqueId(threadContext)
506523
.withXForwardedFor(threadContext)
507524
.build();
508-
logger.info(logEntry);
525+
logger.info(AUDIT_MARKER, logEntry);
509526
}
510527
}
511528
}
@@ -523,7 +540,7 @@ public void tamperedRequest(String requestId, RestRequest request) {
523540
.withOpaqueId(threadContext)
524541
.withXForwardedFor(threadContext)
525542
.build();
526-
logger.info(logEntry);
543+
logger.info(AUDIT_MARKER, logEntry);
527544
}
528545
}
529546

@@ -544,7 +561,7 @@ public void tamperedRequest(String requestId, String action, TransportMessage me
544561
.withOpaqueId(threadContext)
545562
.withXForwardedFor(threadContext)
546563
.build();
547-
logger.info(logEntry);
564+
logger.info(AUDIT_MARKER, logEntry);
548565
}
549566
}
550567
}
@@ -567,7 +584,7 @@ public void tamperedRequest(String requestId, User user, String action, Transpor
567584
.withOpaqueId(threadContext)
568585
.withXForwardedFor(threadContext)
569586
.build();
570-
logger.info(logEntry);
587+
logger.info(AUDIT_MARKER, logEntry);
571588
}
572589
}
573590
}
@@ -586,7 +603,7 @@ public void connectionGranted(InetAddress inetAddress, String profile, SecurityI
586603
.withOpaqueId(threadContext)
587604
.withXForwardedFor(threadContext)
588605
.build();
589-
logger.info(logEntry);
606+
logger.info(AUDIT_MARKER, logEntry);
590607
}
591608
}
592609

@@ -604,7 +621,7 @@ public void connectionDenied(InetAddress inetAddress, String profile, SecurityIp
604621
.withOpaqueId(threadContext)
605622
.withXForwardedFor(threadContext)
606623
.build();
607-
logger.info(logEntry);
624+
logger.info(AUDIT_MARKER, logEntry);
608625
}
609626
}
610627

@@ -628,7 +645,7 @@ public void runAsGranted(String requestId, Authentication authentication, String
628645
.withOpaqueId(threadContext)
629646
.withXForwardedFor(threadContext)
630647
.build();
631-
logger.info(logEntry);
648+
logger.info(AUDIT_MARKER, logEntry);
632649
}
633650
}
634651
}
@@ -653,7 +670,7 @@ public void runAsDenied(String requestId, Authentication authentication, String
653670
.withOpaqueId(threadContext)
654671
.withXForwardedFor(threadContext)
655672
.build();
656-
logger.info(logEntry);
673+
logger.info(AUDIT_MARKER, logEntry);
657674
}
658675
}
659676
}
@@ -675,7 +692,7 @@ public void runAsDenied(String requestId, Authentication authentication, RestReq
675692
.withOpaqueId(threadContext)
676693
.withXForwardedFor(threadContext)
677694
.build();
678-
logger.info(logEntry);
695+
logger.info(AUDIT_MARKER, logEntry);
679696
}
680697
}
681698

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/audit/logfile/LoggingAuditTrailTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public void init() throws Exception {
198198
threadContext.putHeader(AuditTrail.X_FORWARDED_FOR_HEADER,
199199
randomFrom("2001:db8:85a3:8d3:1319:8a2e:370:7348", "203.0.113.195", "203.0.113.195, 70.41.3.18, 150.172.238.178"));
200200
}
201-
logger = CapturingLogger.newCapturingLogger(Level.INFO, patternLayout);
201+
logger = CapturingLogger.newCapturingLogger(randomFrom(Level.OFF, Level.FATAL, Level.ERROR, Level.WARN, Level.INFO), patternLayout);
202202
auditTrail = new LoggingAuditTrail(settings, clusterService, logger, threadContext);
203203
}
204204

0 commit comments

Comments
 (0)