From 0d144d4b103094d45fde87b6279ace398574a620 Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Mon, 25 Oct 2021 13:16:59 -0700 Subject: [PATCH] Added Sanitize function. --- .../erik/mobibot/modules/ModuleException.kt | 19 ------- .../erik/mobibot/modules/StockQuote.kt | 2 +- .../thauvin/erik/mobibot/modules/WorldTime.kt | 5 +- .../net/thauvin/erik/mobibot/Sanitize.kt | 56 +++++++++++++++++++ .../erik/mobibot/modules/GoogleSearchTest.kt | 5 +- .../mobibot/modules/ModuleExceptionTest.kt | 11 ++-- .../erik/mobibot/modules/StockQuoteTest.kt | 3 +- 7 files changed, 70 insertions(+), 31 deletions(-) create mode 100644 src/test/kotlin/net/thauvin/erik/mobibot/Sanitize.kt diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/ModuleException.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/ModuleException.kt index 5318787..71c5004 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/ModuleException.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/ModuleException.kt @@ -31,9 +31,6 @@ */ package net.thauvin.erik.mobibot.modules -import net.thauvin.erik.mobibot.Utils.obfuscate -import net.thauvin.erik.mobibot.Utils.replaceEach - /** * The `ModuleException` class. */ @@ -64,22 +61,6 @@ class ModuleException : Exception { this.debugMessage = debugMessage } - /** - * Return the sanitized message (e.g. remove API keys, etc.) - */ - fun getSanitizedMessage(vararg sanitize: String): String { - val obfuscate = sanitize.map { it.obfuscate() }.toTypedArray() - return when { - cause?.message != null -> { - cause.javaClass.name + ": " + cause.message!!.replaceEach(sanitize, obfuscate) - } - message != null -> { - message.javaClass.name + ": " + message.replaceEach(sanitize, obfuscate) - } - else -> "" - } - } - companion object { private const val serialVersionUID = 1L } diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt index 333d9d4..f418185 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/StockQuote.kt @@ -190,7 +190,7 @@ class StockQuote(bot: Mobibot) : ThreadedModule(bot) { val data = arrayOf( "Open" to "02. open", "High" to "03. high", - "Low" to "04. low", + "Low" to "04. low", "Volume" to "06. volume", "Latest" to "07. latest trading day" ) diff --git a/src/main/kotlin/net/thauvin/erik/mobibot/modules/WorldTime.kt b/src/main/kotlin/net/thauvin/erik/mobibot/modules/WorldTime.kt index 75fc7f5..e4deddb 100644 --- a/src/main/kotlin/net/thauvin/erik/mobibot/modules/WorldTime.kt +++ b/src/main/kotlin/net/thauvin/erik/mobibot/modules/WorldTime.kt @@ -42,7 +42,6 @@ import java.time.ZonedDateTime import java.time.format.DateTimeFormatter import java.time.temporal.ChronoField import java.util.Collections -import java.util.Locale /** * The WorldTime module. @@ -65,12 +64,12 @@ class WorldTime(bot: Mobibot) : AbstractModule(bot) { /** * Returns the current Internet (beat) Time. */ - @Suppress("MagicNumber") + @Suppress("MagicNumber", "ImplicitDefaultLocale") private fun internetTime(): String { val zdt = ZonedDateTime.now(ZoneId.of("UTC+01:00")) val beats = ((zdt[ChronoField.SECOND_OF_MINUTE] + zdt[ChronoField.MINUTE_OF_HOUR] * 60 + zdt[ChronoField.HOUR_OF_DAY] * 3600) / 86.4).toInt() - return String.format(Locale.getDefault(), "%c%03d", '@', beats) + return String.format("%c%03d", '@', beats) } /** diff --git a/src/test/kotlin/net/thauvin/erik/mobibot/Sanitize.kt b/src/test/kotlin/net/thauvin/erik/mobibot/Sanitize.kt new file mode 100644 index 0000000..9e861c6 --- /dev/null +++ b/src/test/kotlin/net/thauvin/erik/mobibot/Sanitize.kt @@ -0,0 +1,56 @@ +/* + * Sanitize.kt + * + * Copyright (c) 2004-2021, Erik C. Thauvin (erik@thauvin.net) + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of this project nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package net.thauvin.erik.mobibot + +import net.thauvin.erik.mobibot.Utils.obfuscate +import net.thauvin.erik.mobibot.Utils.replaceEach + +object Sanitize { + /** + * Return the sanitized exception message (e.g. remove API keys, etc.) + */ + 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) + } + message != null -> { + message!!.javaClass.name + ": " + message!!.replaceEach(sanitize, obfuscate) + } + else -> "" + } + } + } +} 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 aa4d98e..100689d 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearchTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/GoogleSearchTest.kt @@ -32,6 +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.modules.GoogleSearch.Companion.searchGoogle import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy @@ -63,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, e.getSanitizedMessage(apiKey, cseKey), e) + if ("true" == System.getenv("CI") && (apiKey.isNotBlank() || cseKey.isNotBlank())) { + throw ModuleException(e.debugMessage, sanitizedMessage(e, apiKey, cseKey), e) } 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 4e63def..b6250c9 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/ModuleExceptionTest.kt @@ -31,6 +31,7 @@ */ package net.thauvin.erik.mobibot.modules +import net.thauvin.erik.mobibot.Sanitize.sanitizedMessage import org.assertj.core.api.Assertions.assertThat import org.testng.annotations.DataProvider import org.testng.annotations.Test @@ -66,21 +67,21 @@ class ModuleExceptionTest { } @Test - fun testGetSanitizedMessage() { + fun testSanitizeMessage() { val apiKey = "1234567890" var e = ModuleException(debugMessage, message, IOException("URL http://foo.com?apiKey=$apiKey&userID=me")) - assertThat(e.getSanitizedMessage(apiKey)).describedAs("sanitized url") + assertThat(sanitizedMessage(e, apiKey)).describedAs("sanitized url") .contains("xxxxxxxxxx").doesNotContain(apiKey) e = ModuleException(debugMessage, message, null) - assertThat(e.getSanitizedMessage(apiKey)).describedAs("no cause").contains(message) + assertThat(sanitizedMessage(e, apiKey)).describedAs("no cause").contains(message) val msg: String? = null e = ModuleException(debugMessage, msg, IOException(msg)) - assertThat(e.getSanitizedMessage(apiKey)).describedAs("no message").isEqualTo("") + assertThat(sanitizedMessage(e, apiKey)).describedAs("no message").isEqualTo("") e = ModuleException(msg, msg, IOException(apiKey)) - assertThat(e.getSanitizedMessage(apiKey)).describedAs("null message").doesNotContain(apiKey) + assertThat(sanitizedMessage(e, apiKey)).describedAs("null message").doesNotContain(apiKey) } } 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 7f46935..d8a00f3 100644 --- a/src/test/kotlin/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt +++ b/src/test/kotlin/net/thauvin/erik/mobibot/modules/StockQuoteTest.kt @@ -32,6 +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.modules.StockQuote.Companion.getQuote import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy @@ -64,7 +65,7 @@ class StockQuoteTest : LocalProperties() { } catch (e: ModuleException) { // Avoid displaying api keys in CI logs if ("true" == System.getenv("CI") && apiKey.isNotBlank()) { - throw ModuleException(e.debugMessage, e.getSanitizedMessage(apiKey), e) + throw ModuleException(e.debugMessage, sanitizedMessage(e, apiKey), e) } else { throw e }