diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/FeedReader.kt b/src/main/kotlin/net/thauvin/erik/mobibot/FeedReader.kt index 5930ca2..3ee5aad 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/FeedReader.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/FeedReader.kt @@ -42,7 +42,7 @@ import java.io.IOException import java.net.URL /** - * Reads a RSS feed. + * Reads an RSS feed. */ class FeedReader( // Bot diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/TellMessage.kt b/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/TellMessage.kt index 6a058f6..2f88e86 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/TellMessage.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/commands/tell/TellMessage.kt @@ -66,7 +66,7 @@ class TellMessage internal constructor( var id: String = queued.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")) /** - * Returns {@code true) if a notification was send. + * Returns {@code true) if a notification was sent. */ var isNotified = false @@ -82,7 +82,7 @@ class TellMessage internal constructor( } /** - * Return the message creating date. + * Returns the message creating date. */ var receptionDate: LocalDateTime = LocalDateTime.MIN diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/Sanitize.kt b/src/test/kotlin/net/thauvin/erik/mobibot/Sanitize.kt index 9e861c6..e8cc448 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/Sanitize.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/Sanitize.kt @@ -34,23 +34,29 @@ package net.thauvin.erik.mobibot import net.thauvin.erik.mobibot.Utils.obfuscate import net.thauvin.erik.mobibot.Utils.replaceEach +import net.thauvin.erik.mobibot.modules.ModuleException object Sanitize { /** - * Return the sanitized exception message (e.g. remove API keys, etc.) + * Returns a sanitized exception to avoid displaying api keys, etc. in CI logs. */ - fun sanitizedMessage(e: Throwable, vararg sanitize: String): String { - val obfuscate = sanitize.map { it.obfuscate() }.toTypedArray() - with(e) { - return when { - cause?.message != null -> { - cause!!.javaClass.name + ": " + cause!!.message!!.replaceEach(sanitize, obfuscate) + fun sanitizeException(e: ModuleException, vararg sanitize: String): ModuleException { + var sanitizedException = e + val search = sanitize.filter { it.isNotBlank() }.toTypedArray() + if (search.isNotEmpty()) { + val obfuscate = search.map { it.obfuscate() }.toTypedArray() + with(e) { + if (cause?.message != null) { + sanitizedException = ModuleException( + debugMessage, + cause!!.javaClass.name + ": " + cause!!.message!!.replaceEach(search, obfuscate), + this + ) + } else if (message != null) { + sanitizedException = ModuleException(debugMessage, message!!.replaceEach(search, obfuscate), this) } - message != null -> { - message!!.javaClass.name + ": " + message!!.replaceEach(sanitize, obfuscate) - } - else -> "" } } + return sanitizedException } } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearchTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearchTest.kt index 100689d..5dfc1f2 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearchTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearchTest.kt @@ -32,7 +32,7 @@ package net.thauvin.erik.mobibot.modules import net.thauvin.erik.mobibot.LocalProperties -import net.thauvin.erik.mobibot.Sanitize.sanitizedMessage +import net.thauvin.erik.mobibot.Sanitize.sanitizeException import net.thauvin.erik.mobibot.modules.GoogleSearch.Companion.searchGoogle import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy @@ -64,8 +64,8 @@ class GoogleSearchTest : LocalProperties() { .describedAs("no query").isInstanceOf(ModuleException::class.java).hasNoCause() } catch (e: ModuleException) { // Avoid displaying api keys in CI logs - if ("true" == System.getenv("CI") && (apiKey.isNotBlank() || cseKey.isNotBlank())) { - throw ModuleException(e.debugMessage, sanitizedMessage(e, apiKey, cseKey), e) + if ("true" == System.getenv("CI")) { + throw sanitizeException(e, apiKey, cseKey) } else { throw e } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.kt index b6250c9..707f617 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.kt @@ -1,7 +1,7 @@ /* - * ModuleExceptionTest.java + * ModuleExceptionTest.kt * - * Copyright (c) 2004-2019, Erik C. Thauvin (erik@thauvin.net) + * Copyright (c) 2004-2021, Erik C. Thauvin (erik@thauvin.net) * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,7 +31,7 @@ */ package net.thauvin.erik.mobibot.modules -import net.thauvin.erik.mobibot.Sanitize.sanitizedMessage +import net.thauvin.erik.mobibot.Sanitize.sanitizeException import org.assertj.core.api.Assertions.assertThat import org.testng.annotations.DataProvider import org.testng.annotations.Test @@ -63,25 +63,31 @@ class ModuleExceptionTest { @Test(dataProvider = "dp") fun testGetMessage(e: ModuleException) { - assertThat(e.message).describedAs("get message").isEqualTo(message) + assertThat(e).describedAs("get message").hasMessage(message) } @Test fun testSanitizeMessage() { val apiKey = "1234567890" var e = ModuleException(debugMessage, message, IOException("URL http://foo.com?apiKey=$apiKey&userID=me")) - assertThat(sanitizedMessage(e, apiKey)).describedAs("sanitized url") - .contains("xxxxxxxxxx").doesNotContain(apiKey) + assertThat(sanitizeException(e, apiKey, "", "me")).describedAs("sanitized url") + .hasMessageContainingAll("xxxxxxxxxx", "userID=xx").hasMessageNotContainingAny(apiKey, "me") e = ModuleException(debugMessage, message, null) - assertThat(sanitizedMessage(e, apiKey)).describedAs("no cause").contains(message) + assertThat(sanitizeException(e, apiKey)).describedAs("no cause").hasMessage(message) + + e = ModuleException(debugMessage, message, IOException()) + assertThat(sanitizeException(e, apiKey)).describedAs("no cause message").hasMessage(message) + + e = ModuleException(apiKey) + assertThat(sanitizeException(e, apiKey)).describedAs("api key in message").hasMessageNotContaining(apiKey) val msg: String? = null e = ModuleException(debugMessage, msg, IOException(msg)) - assertThat(sanitizedMessage(e, apiKey)).describedAs("no message").isEqualTo("") + assertThat(sanitizeException(e, apiKey).message).describedAs("null message").isNull() - - e = ModuleException(msg, msg, IOException(apiKey)) - assertThat(sanitizedMessage(e, apiKey)).describedAs("null message").doesNotContain(apiKey) + e = ModuleException(msg, msg, IOException("foo is $apiKey")) + assertThat(sanitizeException(e, " ", apiKey, "foo").message).describedAs("key in cause") + .doesNotContain(apiKey).endsWith("xxx is xxxxxxxxxx") } } diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt b/src/test/kotlin/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt index d8a00f3..b1dc7c0 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt @@ -32,7 +32,7 @@ package net.thauvin.erik.mobibot.modules import net.thauvin.erik.mobibot.LocalProperties -import net.thauvin.erik.mobibot.Sanitize.sanitizedMessage +import net.thauvin.erik.mobibot.Sanitize.sanitizeException import net.thauvin.erik.mobibot.modules.StockQuote.Companion.getQuote import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy @@ -64,8 +64,8 @@ class StockQuoteTest : LocalProperties() { .isInstanceOf(ModuleException::class.java).hasNoCause() } catch (e: ModuleException) { // Avoid displaying api keys in CI logs - if ("true" == System.getenv("CI") && apiKey.isNotBlank()) { - throw ModuleException(e.debugMessage, sanitizedMessage(e, apiKey), e) + if ("true" == System.getenv("CI")) { + throw sanitizeException(e, apiKey) } else { throw e }