From 27d2fa8770ef313a9bd0156dba76c8211c789ef9 Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Wed, 10 Apr 2024 17:08:03 -0700 Subject: [PATCH] obfuscate ip addresses in alert error message (#511) (#530) * obfuscate ip addresses in alert error message * make ip obfuscation lower case * update test with lowercase ip obfuscation --------- (cherry picked from commit b8e6e75e1f51df9bd4c7209bbe71764552996f3d) Signed-off-by: Surya Sashank Nistala Signed-off-by: github-actions[bot] Co-authored-by: github-actions[bot] --- .../commons/alerting/alerts/AlertError.kt | 11 +++++++- .../alerting/alerts/AlertErrorTests.kt | 25 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/test/kotlin/org/opensearch/commons/alerting/alerts/AlertErrorTests.kt diff --git a/src/main/kotlin/org/opensearch/commons/alerting/alerts/AlertError.kt b/src/main/kotlin/org/opensearch/commons/alerting/alerts/AlertError.kt index 11c8ae17..85e8b398 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/alerts/AlertError.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/alerts/AlertError.kt @@ -12,7 +12,10 @@ import org.opensearch.core.xcontent.XContentParser import java.io.IOException import java.time.Instant -data class AlertError(val timestamp: Instant, val message: String) : Writeable, ToXContent { +data class AlertError(val timestamp: Instant, var message: String) : Writeable, ToXContent { + init { + this.message = obfuscateIPAddresses(message) + } @Throws(IOException::class) constructor(sin: StreamInput) : this( @@ -54,6 +57,12 @@ data class AlertError(val timestamp: Instant, val message: String) : Writeable, fun readFrom(sin: StreamInput): AlertError { return AlertError(sin) } + + fun obfuscateIPAddresses(exceptionMessage: String): String { + val ipAddressPattern = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" + val obfuscatedMessage = exceptionMessage.replace(ipAddressPattern.toRegex(), "x.x.x.x") + return obfuscatedMessage + } } override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { diff --git a/src/test/kotlin/org/opensearch/commons/alerting/alerts/AlertErrorTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/alerts/AlertErrorTests.kt new file mode 100644 index 00000000..c5c6d439 --- /dev/null +++ b/src/test/kotlin/org/opensearch/commons/alerting/alerts/AlertErrorTests.kt @@ -0,0 +1,25 @@ +package org.opensearch.commons.alerting.alerts + +import org.junit.Assert +import org.junit.jupiter.api.Test +import java.time.Instant + +class AlertErrorTests { + + @Test + fun `test alertError obfuscates IP addresses in message`() { + val message = + "AlertingException[[5f32db4e2a4fa94f6778cb895dae7a24][10.212.77.91:9300][indices:admin/create]]; " + + "nested: Exception[org.opensearch.transport.RemoteTransportException: [5f32db4e2a4fa94f6778cb895dae7a24][10.212.77.91:9300]" + + "[indices:admin/create]];; java.lang.Exception: org.opensearch.transport.RemoteTransportException: [5f32db4e2a4fa94f6778cb895" + + "dae7a24][10.212.77.91:9300][indices:admin/create]" + val alertError = AlertError(Instant.now(), message = message) + Assert.assertEquals( + alertError.message, + "AlertingException[[5f32db4e2a4fa94f6778cb895dae7a24][x.x.x.x:9300][indices:admin/create]]; " + + "nested: Exception[org.opensearch.transport.RemoteTransportException: [5f32db4e2a4fa94f6778cb895dae7a24][x.x.x.x:9300]" + + "[indices:admin/create]];; java.lang.Exception: org.opensearch.transport.RemoteTransportException: " + + "[5f32db4e2a4fa94f6778cb895dae7a24][x.x.x.x:9300][indices:admin/create]" + ) + } +}