@@ -239,7 +239,8 @@ public void authenticationSuccess(String requestId, Authentication authenticatio
239
239
if (events .contains (AUTHENTICATION_SUCCESS ) && eventFilterPolicyRegistry .ignorePredicate ()
240
240
.test (new AuditEventMetaInfo (
241
241
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 )),
243
244
Optional .empty (),
244
245
Optional .empty ())) == false ) {
245
246
// this is redundant information maintained for bwc purposes
@@ -267,7 +268,8 @@ public void authenticationSuccess(String requestId, Authentication authenticatio
267
268
if (eventFilterPolicyRegistry .ignorePredicate ()
268
269
.test (new AuditEventMetaInfo (
269
270
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 )),
271
273
Optional .empty (),
272
274
indices )) == false ) {
273
275
final StringMapMessage logEntry = new LogEntryBuilder ()
@@ -461,7 +463,9 @@ public void accessGranted(String requestId, Authentication authentication, Strin
461
463
if ((isSystem && events .contains (SYSTEM_ACCESS_GRANTED )) || ((isSystem == false ) && events .contains (ACCESS_GRANTED ))) {
462
464
final Optional <String []> indices = indices (msg );
463
465
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 ) {
465
469
final StringMapMessage logEntry = new LogEntryBuilder ()
466
470
.with (EVENT_TYPE_FIELD_NAME , TRANSPORT_ORIGIN_FIELD_VALUE )
467
471
.with (EVENT_ACTION_FIELD_NAME , "access_granted" )
@@ -491,7 +495,9 @@ public void explicitIndexAccessEvent(String requestId, AuditLevel eventType, Aut
491
495
}
492
496
if (events .contains (eventType )) {
493
497
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 )),
495
501
Optional .of (authorizationInfo ), Optional .ofNullable (indices ))) == false ) {
496
502
final LogEntryBuilder logEntryBuilder = new LogEntryBuilder ()
497
503
.with (EVENT_TYPE_FIELD_NAME , TRANSPORT_ORIGIN_FIELD_VALUE )
@@ -525,7 +531,9 @@ public void accessDenied(String requestId, Authentication authentication, String
525
531
if (events .contains (ACCESS_DENIED )) {
526
532
final Optional <String []> indices = indices (transportRequest );
527
533
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 ) {
529
537
final StringMapMessage logEntry = new LogEntryBuilder ()
530
538
.with (EVENT_TYPE_FIELD_NAME , TRANSPORT_ORIGIN_FIELD_VALUE )
531
539
.with (EVENT_ACTION_FIELD_NAME , "access_denied" )
@@ -589,7 +597,8 @@ public void tamperedRequest(String requestId, Authentication authentication, Str
589
597
final Optional <String []> indices = indices (transportRequest );
590
598
if (eventFilterPolicyRegistry .ignorePredicate ().test (new AuditEventMetaInfo (
591
599
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 )),
593
602
Optional .empty (),
594
603
indices )) == false ) {
595
604
final StringMapMessage logEntry = new LogEntryBuilder ()
@@ -651,7 +660,9 @@ public void runAsGranted(String requestId, Authentication authentication, String
651
660
if (events .contains (RUN_AS_GRANTED )) {
652
661
final Optional <String []> indices = indices (transportRequest );
653
662
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 ) {
655
666
final StringMapMessage logEntry = new LogEntryBuilder ()
656
667
.with (EVENT_TYPE_FIELD_NAME , TRANSPORT_ORIGIN_FIELD_VALUE )
657
668
.with (EVENT_ACTION_FIELD_NAME , "run_as_granted" )
@@ -676,7 +687,9 @@ public void runAsDenied(String requestId, Authentication authentication, String
676
687
if (events .contains (RUN_AS_DENIED )) {
677
688
final Optional <String []> indices = indices (transportRequest );
678
689
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 ) {
680
693
final StringMapMessage logEntry = new LogEntryBuilder ()
681
694
.with (EVENT_TYPE_FIELD_NAME , TRANSPORT_ORIGIN_FIELD_VALUE )
682
695
.with (EVENT_ACTION_FIELD_NAME , "run_as_denied" )
@@ -699,7 +712,8 @@ public void runAsDenied(String requestId, Authentication authentication, String
699
712
public void runAsDenied (String requestId , Authentication authentication , RestRequest request , AuthorizationInfo authorizationInfo ) {
700
713
if (events .contains (RUN_AS_DENIED ) && eventFilterPolicyRegistry .ignorePredicate ().test (
701
714
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 )),
703
717
Optional .of (authorizationInfo ), Optional .empty ())) == false ) {
704
718
final StringMapMessage logEntry = new LogEntryBuilder ()
705
719
.with (EVENT_TYPE_FIELD_NAME , REST_ORIGIN_FIELD_VALUE )
@@ -819,9 +833,12 @@ LogEntryBuilder withAuthentication(Authentication authentication) {
819
833
logEntry .with (AUTHENTICATION_TYPE_FIELD_NAME , authentication .getAuthenticationType ().toString ());
820
834
if (Authentication .AuthenticationType .API_KEY == authentication .getAuthenticationType ()) {
821
835
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
+ }
825
842
} else {
826
843
if (authentication .getUser ().isRunAs ()) {
827
844
logEntry .with (PRINCIPAL_REALM_FIELD_NAME , authentication .getLookedUpBy ().getName ())
0 commit comments