Added Sanitize function.

This commit is contained in:
Erik C. Thauvin 2021-10-25 13:16:59 -07:00
parent 384331b287
commit 0d144d4b10
7 changed files with 70 additions and 31 deletions

View file

@ -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
}

View file

@ -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"
)

View file

@ -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)
}
/**

View file

@ -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 -> ""
}
}
}
}

View file

@ -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
}

View file

@ -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)
}
}

View file

@ -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
}