Added Sanitize function.
This commit is contained in:
parent
384331b287
commit
0d144d4b10
7 changed files with 70 additions and 31 deletions
56
src/test/kotlin/net/thauvin/erik/mobibot/Sanitize.kt
Normal file
56
src/test/kotlin/net/thauvin/erik/mobibot/Sanitize.kt
Normal 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 -> ""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue