Skip to content

Commit bbeb3a8

Browse files
Audit log filter and marker (elastic#49145)
This adds a log marker and a marker filter for the audit log. Closes elastic#47251
1 parent 2d84ad2 commit bbeb3a8

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;
@@ -16,6 +21,7 @@
1621
import org.elasticsearch.common.Strings;
1722
import org.elasticsearch.common.collect.MapBuilder;
1823
import org.elasticsearch.common.component.AbstractComponent;
24+
import org.elasticsearch.common.logging.Loggers;
1925
import org.elasticsearch.common.network.NetworkAddress;
2026
import org.elasticsearch.common.settings.Setting;
2127
import org.elasticsearch.common.settings.Setting.Property;
@@ -33,6 +39,7 @@
3339
import org.elasticsearch.xpack.core.security.user.SystemUser;
3440
import org.elasticsearch.xpack.core.security.user.User;
3541
import org.elasticsearch.xpack.core.security.user.XPackUser;
42+
import org.elasticsearch.xpack.security.Security;
3643
import org.elasticsearch.xpack.security.audit.AuditLevel;
3744
import org.elasticsearch.xpack.security.audit.AuditTrail;
3845
import org.elasticsearch.xpack.core.security.authz.AuthorizationEngine.AuthorizationInfo;
@@ -157,6 +164,8 @@ public class LoggingAuditTrail extends AbstractComponent implements AuditTrail,
157164
"indices",
158165
(key) -> Setting.listSetting(key, Collections.singletonList("*"), Function.identity(), Property.NodeScope, Property.Dynamic));
159166

167+
private static final Marker AUDIT_MARKER = MarkerManager.getMarker("org.elasticsearch.xpack.security.audit");
168+
160169
private final Logger logger;
161170
private final ThreadContext threadContext;
162171
final EventFilterPolicyRegistry eventFilterPolicyRegistry;
@@ -172,7 +181,7 @@ public String name() {
172181
}
173182

174183
public LoggingAuditTrail(Settings settings, ClusterService clusterService, ThreadPool threadPool) {
175-
this(settings, clusterService, LogManager.getLogger(), threadPool.getThreadContext());
184+
this(settings, clusterService, LogManager.getLogger(LoggingAuditTrail.class), threadPool.getThreadContext());
176185
}
177186

178187
LoggingAuditTrail(Settings settings, ClusterService clusterService, Logger logger, ThreadContext threadContext) {
@@ -214,6 +223,14 @@ public LoggingAuditTrail(Settings settings, ClusterService clusterService, Threa
214223
final EventFilterPolicy newPolicy = policy.orElse(new EventFilterPolicy(policyName, settings)).changeIndicesFilter(filtersList);
215224
this.eventFilterPolicyRegistry.set(policyName, newPolicy);
216225
}, (policyName, filtersList) -> EventFilterPolicy.parsePredicate(filtersList));
226+
// this log filter ensures that audit events are not filtered out because of the log level
227+
final LoggerContext ctx = LoggerContext.getContext(false);
228+
MarkerFilter auditMarkerFilter = MarkerFilter.createFilter(AUDIT_MARKER.getName(), Result.ACCEPT, Result.NEUTRAL);
229+
ctx.addFilter(auditMarkerFilter);
230+
ctx.updateLoggers();
231+
clusterService.getClusterSettings().addSettingsUpdateConsumer(ignored -> {
232+
LogManager.getLogger(Security.class).warn("Changing log level for [" + LoggingAuditTrail.class.getName() + "] has no effect");
233+
}, Collections.singletonList(Loggers.LOG_LEVEL_SETTING.getConcreteSettingForNamespace(LoggingAuditTrail.class.getName())));
217234
}
218235

219236
@Override
@@ -232,7 +249,7 @@ public void authenticationSuccess(String requestId, String realm, User user, Res
232249
.withOpaqueId(threadContext)
233250
.withXForwardedFor(threadContext)
234251
.build();
235-
logger.info(logEntry);
252+
logger.info(AUDIT_MARKER, logEntry);
236253
}
237254
}
238255

@@ -255,7 +272,7 @@ public void authenticationSuccess(String requestId, String realm, User user, Str
255272
.withOpaqueId(threadContext)
256273
.withXForwardedFor(threadContext)
257274
.build();
258-
logger.info(logEntry);
275+
logger.info(AUDIT_MARKER, logEntry);
259276
}
260277
}
261278
}
@@ -277,7 +294,7 @@ public void anonymousAccessDenied(String requestId, String action, TransportMess
277294
.withOpaqueId(threadContext)
278295
.withXForwardedFor(threadContext)
279296
.build();
280-
logger.info(logEntry);
297+
logger.info(AUDIT_MARKER, logEntry);
281298
}
282299
}
283300
}
@@ -296,7 +313,7 @@ public void anonymousAccessDenied(String requestId, RestRequest request) {
296313
.withOpaqueId(threadContext)
297314
.withXForwardedFor(threadContext)
298315
.build();
299-
logger.info(logEntry);
316+
logger.info(AUDIT_MARKER, logEntry);
300317
}
301318
}
302319

@@ -318,7 +335,7 @@ public void authenticationFailed(String requestId, AuthenticationToken token, St
318335
.withOpaqueId(threadContext)
319336
.withXForwardedFor(threadContext)
320337
.build();
321-
logger.info(logEntry);
338+
logger.info(AUDIT_MARKER, logEntry);
322339
}
323340
}
324341
}
@@ -336,7 +353,7 @@ public void authenticationFailed(String requestId, RestRequest request) {
336353
.withOpaqueId(threadContext)
337354
.withXForwardedFor(threadContext)
338355
.build();
339-
logger.info(logEntry);
356+
logger.info(AUDIT_MARKER, logEntry);
340357
}
341358
}
342359

@@ -357,7 +374,7 @@ public void authenticationFailed(String requestId, String action, TransportMessa
357374
.withOpaqueId(threadContext)
358375
.withXForwardedFor(threadContext)
359376
.build();
360-
logger.info(logEntry);
377+
logger.info(AUDIT_MARKER, logEntry);
361378
}
362379
}
363380
}
@@ -377,7 +394,7 @@ public void authenticationFailed(String requestId, AuthenticationToken token, Re
377394
.withOpaqueId(threadContext)
378395
.withXForwardedFor(threadContext)
379396
.build();
380-
logger.info(logEntry);
397+
logger.info(AUDIT_MARKER, logEntry);
381398
}
382399
}
383400

@@ -400,7 +417,7 @@ public void authenticationFailed(String requestId, String realm, AuthenticationT
400417
.withOpaqueId(threadContext)
401418
.withXForwardedFor(threadContext)
402419
.build();
403-
logger.info(logEntry);
420+
logger.info(AUDIT_MARKER, logEntry);
404421
}
405422
}
406423
}
@@ -421,7 +438,7 @@ public void authenticationFailed(String requestId, String realm, AuthenticationT
421438
.withOpaqueId(threadContext)
422439
.withXForwardedFor(threadContext)
423440
.build();
424-
logger.info(logEntry);
441+
logger.info(AUDIT_MARKER, logEntry);
425442
}
426443
}
427444

@@ -447,7 +464,7 @@ public void accessGranted(String requestId, Authentication authentication, Strin
447464
.withXForwardedFor(threadContext)
448465
.with(authorizationInfo.asMap())
449466
.build();
450-
logger.info(logEntry);
467+
logger.info(AUDIT_MARKER, logEntry);
451468
}
452469
}
453470
}
@@ -487,7 +504,7 @@ public void explicitIndexAccessEvent(String requestId, AuditLevel eventType, Aut
487504
.with(ORIGIN_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE)
488505
.with(ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(remoteAddress.address()));
489506
}
490-
logger.info(logEntryBuilder.build());
507+
logger.info(AUDIT_MARKER, logEntryBuilder.build());
491508
}
492509
}
493510
}
@@ -512,7 +529,7 @@ public void accessDenied(String requestId, Authentication authentication, String
512529
.withOpaqueId(threadContext)
513530
.withXForwardedFor(threadContext)
514531
.build();
515-
logger.info(logEntry);
532+
logger.info(AUDIT_MARKER, logEntry);
516533
}
517534
}
518535
}
@@ -530,7 +547,7 @@ public void tamperedRequest(String requestId, RestRequest request) {
530547
.withOpaqueId(threadContext)
531548
.withXForwardedFor(threadContext)
532549
.build();
533-
logger.info(logEntry);
550+
logger.info(AUDIT_MARKER, logEntry);
534551
}
535552
}
536553

@@ -551,7 +568,7 @@ public void tamperedRequest(String requestId, String action, TransportMessage me
551568
.withOpaqueId(threadContext)
552569
.withXForwardedFor(threadContext)
553570
.build();
554-
logger.info(logEntry);
571+
logger.info(AUDIT_MARKER, logEntry);
555572
}
556573
}
557574
}
@@ -574,7 +591,7 @@ public void tamperedRequest(String requestId, User user, String action, Transpor
574591
.withOpaqueId(threadContext)
575592
.withXForwardedFor(threadContext)
576593
.build();
577-
logger.info(logEntry);
594+
logger.info(AUDIT_MARKER, logEntry);
578595
}
579596
}
580597
}
@@ -593,7 +610,7 @@ public void connectionGranted(InetAddress inetAddress, String profile, SecurityI
593610
.withOpaqueId(threadContext)
594611
.withXForwardedFor(threadContext)
595612
.build();
596-
logger.info(logEntry);
613+
logger.info(AUDIT_MARKER, logEntry);
597614
}
598615
}
599616

@@ -611,7 +628,7 @@ public void connectionDenied(InetAddress inetAddress, String profile, SecurityIp
611628
.withOpaqueId(threadContext)
612629
.withXForwardedFor(threadContext)
613630
.build();
614-
logger.info(logEntry);
631+
logger.info(AUDIT_MARKER, logEntry);
615632
}
616633
}
617634

@@ -635,7 +652,7 @@ public void runAsGranted(String requestId, Authentication authentication, String
635652
.withOpaqueId(threadContext)
636653
.withXForwardedFor(threadContext)
637654
.build();
638-
logger.info(logEntry);
655+
logger.info(AUDIT_MARKER, logEntry);
639656
}
640657
}
641658
}
@@ -660,7 +677,7 @@ public void runAsDenied(String requestId, Authentication authentication, String
660677
.withOpaqueId(threadContext)
661678
.withXForwardedFor(threadContext)
662679
.build();
663-
logger.info(logEntry);
680+
logger.info(AUDIT_MARKER, logEntry);
664681
}
665682
}
666683
}
@@ -682,7 +699,7 @@ public void runAsDenied(String requestId, Authentication authentication, RestReq
682699
.withOpaqueId(threadContext)
683700
.withXForwardedFor(threadContext)
684701
.build();
685-
logger.info(logEntry);
702+
logger.info(AUDIT_MARKER, logEntry);
686703
}
687704
}
688705

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
@@ -197,7 +197,7 @@ public void init() throws Exception {
197197
threadContext.putHeader(AuditTrail.X_FORWARDED_FOR_HEADER,
198198
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"));
199199
}
200-
logger = CapturingLogger.newCapturingLogger(Level.INFO, patternLayout);
200+
logger = CapturingLogger.newCapturingLogger(randomFrom(Level.OFF, Level.FATAL, Level.ERROR, Level.WARN, Level.INFO), patternLayout);
201201
auditTrail = new LoggingAuditTrail(settings, clusterService, logger, threadContext);
202202
}
203203

0 commit comments

Comments
 (0)