Skip to content

Commit

Permalink
obfuscate ip addresses in alert error message
Browse files Browse the repository at this point in the history
Signed-off-by: Surya Sashank Nistala <snistala@amazon.com>
  • Loading branch information
eirsep committed Aug 17, 2023
1 parent fad4525 commit a3823a3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken
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(
Expand Down Expand Up @@ -55,6 +58,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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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]"
)
}
}

0 comments on commit a3823a3

Please sign in to comment.