Skip to content

Commit b1e4233

Browse files
Fix auditing of API Key authn without the owner realm name (#59470)
The `Authentication` object that gets built following an API Key authentication contains the realm name of the owner user that created the key (which is audited), but the specific field used for storing it changed in #51305 . This PR makes it so that auditing tolerates an "unfound" realm name, so it doesn't throw an NPE, because the owner realm name is not found in the expected field. Closes #59425
1 parent 3ccc767 commit b1e4233

File tree

4 files changed

+186
-71
lines changed

4 files changed

+186
-71
lines changed

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

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ public void authenticationSuccess(String requestId, Authentication authenticatio
239239
if (events.contains(AUTHENTICATION_SUCCESS) && eventFilterPolicyRegistry.ignorePredicate()
240240
.test(new AuditEventMetaInfo(
241241
Optional.of(authentication.getUser()),
242-
Optional.of(ApiKeyService.getCreatorRealmName(authentication)),
242+
// can be null for API keys created before version 7.7
243+
Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)),
243244
Optional.empty(),
244245
Optional.empty())) == false) {
245246
// this is redundant information maintained for bwc purposes
@@ -267,7 +268,8 @@ public void authenticationSuccess(String requestId, Authentication authenticatio
267268
if (eventFilterPolicyRegistry.ignorePredicate()
268269
.test(new AuditEventMetaInfo(
269270
Optional.of(authentication.getUser()),
270-
Optional.of(ApiKeyService.getCreatorRealmName(authentication)),
271+
// can be null for API keys created before version 7.7
272+
Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)),
271273
Optional.empty(),
272274
indices)) == false) {
273275
final StringMapMessage logEntry = new LogEntryBuilder()
@@ -461,7 +463,9 @@ public void accessGranted(String requestId, Authentication authentication, Strin
461463
if ((isSystem && events.contains(SYSTEM_ACCESS_GRANTED)) || ((isSystem == false) && events.contains(ACCESS_GRANTED))) {
462464
final Optional<String[]> indices = indices(msg);
463465
if (eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(user),
464-
Optional.of(ApiKeyService.getCreatorRealmName(authentication)), Optional.of(authorizationInfo), indices)) == false) {
466+
// can be null for API keys created before version 7.7
467+
Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)),
468+
Optional.of(authorizationInfo), indices)) == false) {
465469
final StringMapMessage logEntry = new LogEntryBuilder()
466470
.with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE)
467471
.with(EVENT_ACTION_FIELD_NAME, "access_granted")
@@ -491,7 +495,9 @@ public void explicitIndexAccessEvent(String requestId, AuditLevel eventType, Aut
491495
}
492496
if (events.contains(eventType)) {
493497
if (eventFilterPolicyRegistry.ignorePredicate()
494-
.test(new AuditEventMetaInfo(Optional.of(user), Optional.of(ApiKeyService.getCreatorRealmName(authentication)),
498+
.test(new AuditEventMetaInfo(Optional.of(user),
499+
// can be null for API keys created before version 7.7
500+
Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)),
495501
Optional.of(authorizationInfo), Optional.ofNullable(indices))) == false) {
496502
final LogEntryBuilder logEntryBuilder = new LogEntryBuilder()
497503
.with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE)
@@ -525,7 +531,9 @@ public void accessDenied(String requestId, Authentication authentication, String
525531
if (events.contains(ACCESS_DENIED)) {
526532
final Optional<String[]> indices = indices(transportRequest);
527533
if (eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(authentication.getUser()),
528-
Optional.of(ApiKeyService.getCreatorRealmName(authentication)), Optional.of(authorizationInfo), indices)) == false) {
534+
// can be null for API keys created before version 7.7
535+
Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)),
536+
Optional.of(authorizationInfo), indices)) == false) {
529537
final StringMapMessage logEntry = new LogEntryBuilder()
530538
.with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE)
531539
.with(EVENT_ACTION_FIELD_NAME, "access_denied")
@@ -589,7 +597,8 @@ public void tamperedRequest(String requestId, Authentication authentication, Str
589597
final Optional<String[]> indices = indices(transportRequest);
590598
if (eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(
591599
Optional.of(authentication.getUser()),
592-
Optional.of(ApiKeyService.getCreatorRealmName(authentication)),
600+
// can be null for API keys created before version 7.7
601+
Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)),
593602
Optional.empty(),
594603
indices)) == false) {
595604
final StringMapMessage logEntry = new LogEntryBuilder()
@@ -651,7 +660,9 @@ public void runAsGranted(String requestId, Authentication authentication, String
651660
if (events.contains(RUN_AS_GRANTED)) {
652661
final Optional<String[]> indices = indices(transportRequest);
653662
if (eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(authentication.getUser()),
654-
Optional.of(ApiKeyService.getCreatorRealmName(authentication)), Optional.of(authorizationInfo), indices)) == false) {
663+
// can be null for API keys created before version 7.7
664+
Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)),
665+
Optional.of(authorizationInfo), indices)) == false) {
655666
final StringMapMessage logEntry = new LogEntryBuilder()
656667
.with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE)
657668
.with(EVENT_ACTION_FIELD_NAME, "run_as_granted")
@@ -676,7 +687,9 @@ public void runAsDenied(String requestId, Authentication authentication, String
676687
if (events.contains(RUN_AS_DENIED)) {
677688
final Optional<String[]> indices = indices(transportRequest);
678689
if (eventFilterPolicyRegistry.ignorePredicate().test(new AuditEventMetaInfo(Optional.of(authentication.getUser()),
679-
Optional.of(ApiKeyService.getCreatorRealmName(authentication)), Optional.of(authorizationInfo), indices)) == false) {
690+
// can be null for API keys created before version 7.7
691+
Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)),
692+
Optional.of(authorizationInfo), indices)) == false) {
680693
final StringMapMessage logEntry = new LogEntryBuilder()
681694
.with(EVENT_TYPE_FIELD_NAME, TRANSPORT_ORIGIN_FIELD_VALUE)
682695
.with(EVENT_ACTION_FIELD_NAME, "run_as_denied")
@@ -699,7 +712,8 @@ public void runAsDenied(String requestId, Authentication authentication, String
699712
public void runAsDenied(String requestId, Authentication authentication, RestRequest request, AuthorizationInfo authorizationInfo) {
700713
if (events.contains(RUN_AS_DENIED) && eventFilterPolicyRegistry.ignorePredicate().test(
701714
new AuditEventMetaInfo(Optional.of(authentication.getUser()),
702-
Optional.of(ApiKeyService.getCreatorRealmName(authentication)),
715+
// can be null for API keys created before version 7.7
716+
Optional.ofNullable(ApiKeyService.getCreatorRealmName(authentication)),
703717
Optional.of(authorizationInfo), Optional.empty())) == false) {
704718
final StringMapMessage logEntry = new LogEntryBuilder()
705719
.with(EVENT_TYPE_FIELD_NAME, REST_ORIGIN_FIELD_VALUE)
@@ -819,9 +833,12 @@ LogEntryBuilder withAuthentication(Authentication authentication) {
819833
logEntry.with(AUTHENTICATION_TYPE_FIELD_NAME, authentication.getAuthenticationType().toString());
820834
if (Authentication.AuthenticationType.API_KEY == authentication.getAuthenticationType()) {
821835
logEntry.with(API_KEY_ID_FIELD_NAME, (String) authentication.getMetadata().get(ApiKeyService.API_KEY_ID_KEY))
822-
.with(API_KEY_NAME_FIELD_NAME, (String) authentication.getMetadata().get(ApiKeyService.API_KEY_NAME_KEY))
823-
.with(PRINCIPAL_REALM_FIELD_NAME,
824-
(String) authentication.getMetadata().get(ApiKeyService.API_KEY_CREATOR_REALM_NAME));
836+
.with(API_KEY_NAME_FIELD_NAME, (String) authentication.getMetadata().get(ApiKeyService.API_KEY_NAME_KEY));
837+
String creatorRealmName = (String) authentication.getMetadata().get(ApiKeyService.API_KEY_CREATOR_REALM_NAME);
838+
if (creatorRealmName != null) {
839+
// can be null for API keys created before version 7.7
840+
logEntry.with(PRINCIPAL_REALM_FIELD_NAME, creatorRealmName);
841+
}
825842
} else {
826843
if (authentication.getUser().isRunAs()) {
827844
logEntry.with(PRINCIPAL_REALM_FIELD_NAME, authentication.getLookedUpBy().getName())

0 commit comments

Comments
 (0)