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

Honor log_request_body setiing in compliance audit log #31

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -566,30 +566,33 @@ public void logDocumentWritten(ShardId shardId, GetResult originalResult, Index
msg.addComplianceDocVersion(result.getVersion());
msg.addComplianceOperation(result.isCreated() ? Operation.CREATE : Operation.UPDATE);

if (complianceConfig.shouldLogDiffsForWrite()
&& originalResult != null
&& originalResult.isExists()
&& originalResult.internalSourceRef() != null) {
if (complianceConfig.shouldLogDiffsForWrite()) {
try {
String originalSource = null;
String currentSource = null;
if (!(originalResult != null && originalResult.isExists() && originalResult.internalSourceRef() != null)) {
// originalSource is empty
originalSource = "{}";
}
if (securityIndex.equals(shardId.getIndexName())) {
try (
XContentParser parser = XContentHelper.createParser(
NamedXContentRegistry.EMPTY,
THROW_UNSUPPORTED_OPERATION,
originalResult.internalSourceRef(),
XContentType.JSON
)
) {
Object base64 = parser.map().values().iterator().next();
if (base64 instanceof String) {
originalSource = (new String(BaseEncoding.base64().decode((String) base64), StandardCharsets.UTF_8));
} else {
originalSource = XContentHelper.convertToJson(originalResult.internalSourceRef(), false, XContentType.JSON);
if (originalSource == null) {
try (
XContentParser parser = XContentHelper.createParser(
NamedXContentRegistry.EMPTY,
THROW_UNSUPPORTED_OPERATION,
originalResult.internalSourceRef(),
XContentType.JSON
)
) {
Object base64 = parser.map().values().iterator().next();
if (base64 instanceof String) {
originalSource = (new String(BaseEncoding.base64().decode((String) base64), StandardCharsets.UTF_8));
} else {
originalSource = XContentHelper.convertToJson(originalResult.internalSourceRef(), false, XContentType.JSON);
}
} catch (Exception e) {
log.error(e.toString());
}
} catch (Exception e) {
log.error(e.toString());
}

try (
Expand All @@ -615,7 +618,9 @@ public void logDocumentWritten(ShardId shardId, GetResult originalResult, Index
);
msg.addSecurityConfigWriteDiffSource(diffnode.size() == 0 ? "" : diffnode.toString(), id);
} else {
originalSource = XContentHelper.convertToJson(originalResult.internalSourceRef(), false, XContentType.JSON);
if (originalSource == null) {
originalSource = XContentHelper.convertToJson(originalResult.internalSourceRef(), false, XContentType.JSON);
}
currentSource = XContentHelper.convertToJson(currentIndex.source(), false, XContentType.JSON);
final JsonNode diffnode = JsonDiff.asJson(
DefaultObjectMapper.objectMapper.readTree(originalSource),
Expand All @@ -628,7 +633,7 @@ public void logDocumentWritten(ShardId shardId, GetResult originalResult, Index
}
}

if (!complianceConfig.shouldLogWriteMetadataOnly()) {
if (!complianceConfig.shouldLogWriteMetadataOnly() && !complianceConfig.shouldLogDiffsForWrite()) {
if (securityIndex.equals(shardId.getIndexName())) {
// current source, normally not null or empty
try (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.AnyOf.anyOf;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThrows;
Expand Down Expand Up @@ -443,4 +445,66 @@ public void testWriteHistory() throws Exception {
});
Assert.assertTrue(TestAuditlogImpl.sb.toString().split(".*audit_compliance_diff_content.*replace.*").length == 1);
}

@Test
public void testWriteLogDiffsEnabledAndLogRequestBodyDisabled() throws Exception {
Settings additionalSettings = Settings.builder().put("plugins.security.audit.type", TestAuditlogImpl.class.getName()).build();

setup(additionalSettings);

rh.sendAdminCertificate = true;
rh.keystore = "auditlog/kirk-keystore.jks";

// watch emp for write
AuditConfig auditConfig = new AuditConfig(
true,
AuditConfig.Filter.from(Settings.builder().put("plugins.security.audit.config.log_request_body", false).build()),
ComplianceConfig.from(
ImmutableMap.of(
"enabled",
true,
"write_watched_indices",
Collections.singletonList("emp"),
"write_log_diffs",
true,
"write_metadata_only",
false
),
additionalSettings
)
);
updateAuditConfig(AuditTestUtils.createAuditPayload(auditConfig));

List<AuditMessage> messages = TestAuditlogImpl.doThenWaitForMessages(() -> {
try (Client tc = getClient()) {
rh.executePutRequest("emp/_doc/0?refresh", "{\"name\" : \"Criag\", \"title\" : \"Software Engineer\"}");
}
}, 7);

AuditMessage complianceDocWriteMessage = messages.stream()
.filter(m -> m.getCategory().equals(AuditCategory.COMPLIANCE_DOC_WRITE))
.findFirst()
.orElse(null);
assertThat(complianceDocWriteMessage, notNullValue());
assertThat(
(String) complianceDocWriteMessage.getAsMap().get("audit_compliance_diff_content"),
containsString(
"[{\"op\":\"add\",\"path\":\"/name\",\"value\":\"Criag\"},{\"op\":\"add\",\"path\":\"/title\",\"value\":\"Software Engineer\"}]"
)
);
assertThat(complianceDocWriteMessage.getRequestBody(), nullValue());

messages = TestAuditlogImpl.doThenWaitForMessages(() -> {
try (Client tc = getClient()) {
rh.executePutRequest("emp/_doc/0?refresh", "{\"name\" : \"Craig\", \"title\" : \"Software Engineer\"}");
}
}, 1);

complianceDocWriteMessage = messages.get(0);
assertThat(
(String) complianceDocWriteMessage.getAsMap().get("audit_compliance_diff_content"),
containsString("[{\"op\":\"replace\",\"path\":\"/name\",\"value\":\"Craig\"}]")
);
assertThat(complianceDocWriteMessage.getRequestBody(), nullValue());
}
}
Loading