Skip to content
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

Audit API key ID when create or grant API keys #88456

Merged
merged 2 commits into from
Jul 12, 2022
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/88456.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 88456
summary: Audit API key ID when create or grant API keys
area: Audit
type: enhancement
issues: []
2 changes: 1 addition & 1 deletion x-pack/docs/en/security/auditing/event-types.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ the <<mapping-roles, API request for mapping roles>>.
+
[source,js]
----
`{"name": <string>, "expiration": <string>, "role_descriptors" [<object>]}`
`{"id": <string>, "name": <string>, "expiration": <string>, "role_descriptors" [<object>]}`
----
// NOTCONSOLE
+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,7 @@ LogEntryBuilder withRequestBody(GrantApiKeyRequest grantApiKeyRequest) throws IO
private void withRequestBody(XContentBuilder builder, CreateApiKeyRequest createApiKeyRequest) throws IOException {
TimeValue expiration = createApiKeyRequest.getExpiration();
builder.startObject("apikey")
.field("id", createApiKeyRequest.getId())
.field("name", createApiKeyRequest.getName())
.field("expiration", expiration != null ? expiration.toString() : null)
.startArray("role_descriptors");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,13 @@ public void testSecurityConfigChangeEventFormattingForRoles() throws IOException
createApiKeyRequest.setRefreshPolicy(randomFrom(WriteRequest.RefreshPolicy.values()));
auditTrail.accessGranted(requestId, authentication, CreateApiKeyAction.NAME, createApiKeyRequest, authorizationInfo);
String expectedCreateKeyAuditEventString = """
"create":{"apikey":{"name":"%s","expiration":%s,%s}}\
""".formatted(keyName, expiration != null ? "\"" + expiration + "\"" : "null", roleDescriptorsStringBuilder);
"create":{"apikey":{"id":"%s","name":"%s","expiration":%s,%s}}\
""".formatted(
createApiKeyRequest.getId(),
keyName,
expiration != null ? "\"" + expiration + "\"" : "null",
roleDescriptorsStringBuilder
);
List<String> output = CapturingLogger.output(logger.getName(), Level.INFO);
assertThat(output.size(), is(2));
String generatedCreateKeyAuditEventString = output.get(1);
Expand Down Expand Up @@ -617,7 +622,9 @@ public void testSecurityConfigChangeEventFormattingForRoles() throws IOException
output = CapturingLogger.output(logger.getName(), Level.INFO);
assertThat(output.size(), is(2));
String generatedGrantKeyAuditEventString = output.get(1);
StringBuilder grantKeyAuditEventStringBuilder = new StringBuilder().append("\"create\":{\"apikey\":{\"name\":\"")
StringBuilder grantKeyAuditEventStringBuilder = new StringBuilder().append("\"create\":{\"apikey\":{\"id\":\"")
.append(grantApiKeyRequest.getApiKeyRequest().getId())
.append("\",\"name\":\"")
.append(keyName)
.append("\",\"expiration\":")
.append(expiration != null ? "\"" + expiration + "\"" : "null")
Expand Down