-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Add "request.id" to file audit logs #35536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
95c4da1
c0c9639
c79201a
cba24fb
bf400a9
d21d79c
5879bcc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,9 @@ | |
package org.elasticsearch.xpack.security.audit; | ||
|
||
import org.elasticsearch.action.IndicesRequest; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.UUIDs; | ||
import org.elasticsearch.common.util.concurrent.ThreadContext; | ||
import org.elasticsearch.common.xcontent.XContentHelper; | ||
import org.elasticsearch.rest.RestRequest; | ||
import org.elasticsearch.transport.TransportMessage; | ||
|
@@ -17,6 +20,8 @@ | |
|
||
public class AuditUtil { | ||
|
||
private static final String AUDIT_REQUEST_ID = "_xpack_audit_request_id"; | ||
|
||
public static String restRequestContent(RestRequest request) { | ||
if (request.hasContent()) { | ||
try { | ||
|
@@ -38,4 +43,34 @@ public static Set<String> indices(TransportMessage message) { | |
private static Set<String> arrayToSetOrNull(String[] indices) { | ||
return indices == null ? null : new HashSet<>(Arrays.asList(indices)); | ||
} | ||
|
||
public static String generateRequestId(ThreadContext threadContext) { | ||
return generateRequestId(threadContext, true); | ||
} | ||
|
||
public static String getOrGenerateRequestId(ThreadContext threadContext) { | ||
final String requestId = extractRequestId(threadContext); | ||
if (Strings.isEmpty(requestId)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can get rid of this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As in: be explicit when we generate these. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not really feasible to do that unfortunately. Well, I guess we could refactor a lot of stuff to make that explicit, but it's not right now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. |
||
return generateRequestId(threadContext, false); | ||
} | ||
return requestId; | ||
} | ||
|
||
private static String generateRequestId(ThreadContext threadContext, boolean checkExisting) { | ||
if (checkExisting) { | ||
final String existing = extractRequestId(threadContext); | ||
if (existing != null) { | ||
throw new IllegalStateException("Cannot generate a new audit request id - existing id [" | ||
+ existing + "] already registered"); | ||
} | ||
} | ||
final String requestId = UUIDs.randomBase64UUID(); | ||
// Store as a header (not transient) so that it is passed over the network if this request requires execution on other nodes | ||
threadContext.putHeader(AUDIT_REQUEST_ID, requestId); | ||
return requestId; | ||
} | ||
|
||
public static String extractRequestId(ThreadContext threadContext) { | ||
return threadContext.getHeader(AUDIT_REQUEST_ID); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍