Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package app.softnetwork.elastic.client

import app.softnetwork.elastic.client.metrics.MetricsConfig
import app.softnetwork.elastic.sql.config.ConfigErrorKeys
import com.typesafe.config.{Config, ConfigFactory}
import com.typesafe.scalalogging.StrictLogging
import configs.Configs
Expand Down Expand Up @@ -78,7 +79,7 @@ object ElasticConfig extends StrictLogging {
.get(config.withFallback(defaults), "elastic")
.toEither match {
case Left(configError) =>
logger.error(s"Something went wrong with the provided arguments $configError")
logger.error(s"Invalid elastic configuration: ${ConfigErrorKeys(configError)}")
throw configError.configException
case Right(r) => r
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package app.softnetwork.elastic.client

import app.softnetwork.elastic.client.metrics.MetricsConfig
import app.softnetwork.elastic.sql.config.ConfigErrorKeys
import com.typesafe.config.{Config, ConfigFactory}
import com.typesafe.scalalogging.StrictLogging
import configs.ConfigReader
Expand Down Expand Up @@ -78,7 +79,7 @@ object ElasticConfig extends StrictLogging {
.read(config.withFallback(defaults), "elastic")
.toEither match {
case Left(configError) =>
logger.error(s"Something went wrong with the provided arguments $configError")
logger.error(s"Invalid elastic configuration: ${ConfigErrorKeys(configError)}")
throw configError.configException
case Right(r) => r
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2025 SOFTNETWORK
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.softnetwork.elastic.client

import app.softnetwork.elastic.client.spi.ClientLogCapture
import com.typesafe.config.{ConfigException, ConfigFactory}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

/** A configuration that cannot be read is logged by key, never by value: the value may be a secret
* set under the wrong key, and a log line travels further than the exception its caller receives.
*/
class ElasticConfigErrorLogSpec extends AnyFlatSpec with Matchers {

// letters only: Typesafe finds no number in it and quotes the whole value back
// ("No number in duration value '…'"); a value with digits would be split into number and unit
private val secret = "hunterTwoSecretValue"

"ElasticConfig" should "log the key it could not read, never the value" in {
val hocon = s"""elastic.connection-timeout = "$secret""""
val (thrown, events) = ClientLogCapture.capture(
intercept[ConfigException](ElasticConfig(ConfigFactory.parseString(hocon)))
)

// the premise: this failure's message quotes the value (the exception the caller receives is
// unchanged), so a log line built from the message would carry it too
thrown.getMessage should include(secret)

val logged = events.filter(_.logger.startsWith(classOf[ElasticConfig].getName))
logged should not be empty
val text = logged.map(_.text).mkString("\n")
text should include("connection-timeout")
text should not include secret
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object ElasticSqlConfig extends StrictLogging {
.get(config.withFallback(ConfigFactory.load("softnetwork-sql.conf")), "sql")
.toEither match {
case Left(configError) =>
logger.error(s"Something went wrong with the provided arguments $configError")
logger.error(s"Invalid sql configuration: ${ConfigErrorKeys(configError)}")
throw configError.configException
case Right(r) => r
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object ElasticSqlConfig extends StrictLogging {
.read(config.withFallback(ConfigFactory.load("softnetwork-sql.conf")), "sql")
.toEither match {
case Left(configError) =>
logger.error(s"Something went wrong with the provided arguments $configError")
logger.error(s"Invalid sql configuration: ${ConfigErrorKeys(configError)}")
throw configError.configException
case Right(r) => r
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2025 SOFTNETWORK
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package app.softnetwork.elastic.sql.config

import configs.ConfigError

/** What a configuration failure may write to the log: each key that could not be read and the kind
* of failure, never the failure's message. A Typesafe "bad value" message quotes the value it
* could not read, and that value may be a secret set under the wrong key.
*
* The exception thrown to the caller is not this summary: it keeps the library's message, which
* names what is wrong (a `require` of the settings, a missing key) for the caller who supplied the
* configuration.
*
* The kind is the throwable's full class name: `getSimpleName` can throw on JDK 8 for a Scala
* class nested in an object, and is empty for an anonymous class.
*/
private[elastic] object ConfigErrorKeys {

def apply(error: ConfigError): String =
error.entries
.map(entry => s"${entry.paths.mkString(".")} (${entry.throwable.getClass.getName})")
.distinct
.mkString(", ")
}
Loading