Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/91171.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 91171
summary: Fix NPE in auditing `authenticationSuccess` for non-existing run-as user
area: Audit
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -1629,14 +1629,16 @@ LogEntryBuilder withAuthentication(Authentication authentication) {
final Authentication.RealmRef authenticatedBy = authentication.getAuthenticatingSubject().getRealm();
if (authentication.isRunAs()) {
final Authentication.RealmRef lookedUpBy = authentication.getEffectiveSubject().getRealm();
logEntry.with(PRINCIPAL_REALM_FIELD_NAME, lookedUpBy.getName())
.with(PRINCIPAL_RUN_BY_FIELD_NAME, authentication.getAuthenticatingSubject().getUser().principal())
if (lookedUpBy != null) {
logEntry.with(PRINCIPAL_REALM_FIELD_NAME, lookedUpBy.getName());
if (lookedUpBy.getDomain() != null) {
logEntry.with(PRINCIPAL_DOMAIN_FIELD_NAME, lookedUpBy.getDomain().name());
}
}
logEntry.with(PRINCIPAL_RUN_BY_FIELD_NAME, authentication.getAuthenticatingSubject().getUser().principal())
// API key can run-as, when that happens, the following field will be _es_api_key,
// not the API key owner user's realm.
.with(PRINCIPAL_RUN_BY_REALM_FIELD_NAME, authenticatedBy.getName());
if (lookedUpBy.getDomain() != null) {
logEntry.with(PRINCIPAL_DOMAIN_FIELD_NAME, lookedUpBy.getDomain().name());
}
if (authenticatedBy.getDomain() != null) {
logEntry.with(PRINCIPAL_RUN_BY_DOMAIN_FIELD_NAME, authenticatedBy.getDomain().name());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2471,7 +2471,6 @@ public void testAuthenticationSuccessRest() throws Exception {
traceId(threadContext, checkedFields);
forwardedFor(threadContext, checkedFields);
assertMsg(logger, checkedFields.map());

CapturingLogger.output(logger.getName(), Level.INFO).clear();

// audit for authn with API Key
Expand All @@ -2497,6 +2496,32 @@ public void testAuthenticationSuccessRest() throws Exception {
traceId(threadContext, checkedFields);
forwardedFor(threadContext, checkedFields);
assertMsg(logger, checkedFields.map());
CapturingLogger.output(logger.getName(), Level.INFO).clear();

// authentication success but run-as user does not exist
authentication = AuthenticationTestHelper.builder().realm().build(false).runAs(new User(randomAlphaOfLengthBetween(3, 8)), null);
checkedFields = new MapBuilder<>(commonFields);
auditTrail.authenticationSuccess(requestId, authentication, request);
checkedFields.put(LoggingAuditTrail.EVENT_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE)
.put(LoggingAuditTrail.EVENT_ACTION_FIELD_NAME, "authentication_success")
.put(LoggingAuditTrail.REALM_FIELD_NAME, authentication.getAuthenticatingSubject().getRealm().getName())
.put(LoggingAuditTrail.ORIGIN_TYPE_FIELD_NAME, LoggingAuditTrail.REST_ORIGIN_FIELD_VALUE)
.put(LoggingAuditTrail.ORIGIN_ADDRESS_FIELD_NAME, NetworkAddress.format(address))
.put(LoggingAuditTrail.REQUEST_METHOD_FIELD_NAME, request.method().toString())
.put(LoggingAuditTrail.REQUEST_ID_FIELD_NAME, requestId)
.put(LoggingAuditTrail.URL_PATH_FIELD_NAME, "_uri");
if (includeRequestBody && Strings.hasLength(expectedMessage)) {
checkedFields.put(LoggingAuditTrail.REQUEST_BODY_FIELD_NAME, expectedMessage);
}
if (params.isEmpty() == false) {
checkedFields.put(LoggingAuditTrail.URL_QUERY_FIELD_NAME, "foo=bar&evac=true");
}
authentication(authentication, checkedFields);
opaqueId(threadContext, checkedFields);
traceId(threadContext, checkedFields);
forwardedFor(threadContext, checkedFields);
assertMsg(logger, checkedFields.map());
CapturingLogger.output(logger.getName(), Level.INFO).clear();
}

public void testAuthenticationSuccessTransport() throws Exception {
Expand Down Expand Up @@ -2896,12 +2921,16 @@ private static void authentication(Authentication authentication, MapBuilder<Str
final RealmRef authenticatedBy = authentication.getAuthenticatingSubject().getRealm();
if (authentication.isRunAs()) {
final RealmRef lookedUpBy = authentication.getEffectiveSubject().getRealm();
checkedFields.put(LoggingAuditTrail.PRINCIPAL_REALM_FIELD_NAME, lookedUpBy.getName())
.put(LoggingAuditTrail.PRINCIPAL_RUN_BY_FIELD_NAME, authentication.getAuthenticatingSubject().getUser().principal())
.put(LoggingAuditTrail.PRINCIPAL_RUN_BY_REALM_FIELD_NAME, authenticatedBy.getName());
if (lookedUpBy.getDomain() != null) {
checkedFields.put(LoggingAuditTrail.PRINCIPAL_DOMAIN_FIELD_NAME, lookedUpBy.getDomain().name());
if (lookedUpBy != null) {
checkedFields.put(LoggingAuditTrail.PRINCIPAL_REALM_FIELD_NAME, lookedUpBy.getName());
if (lookedUpBy.getDomain() != null) {
checkedFields.put(LoggingAuditTrail.PRINCIPAL_DOMAIN_FIELD_NAME, lookedUpBy.getDomain().name());
}
}
checkedFields.put(
LoggingAuditTrail.PRINCIPAL_RUN_BY_FIELD_NAME,
authentication.getAuthenticatingSubject().getUser().principal()
).put(LoggingAuditTrail.PRINCIPAL_RUN_BY_REALM_FIELD_NAME, authenticatedBy.getName());
if (authenticatedBy.getDomain() != null) {
checkedFields.put(LoggingAuditTrail.PRINCIPAL_RUN_BY_DOMAIN_FIELD_NAME, authenticatedBy.getDomain().name());
}
Expand Down