diff --git a/core/src/main/scala-2.12/app/softnetwork/elastic/client/ElasticConfig.scala b/core/src/main/scala-2.12/app/softnetwork/elastic/client/ElasticConfig.scala index dd18dfacf..684efdaa4 100644 --- a/core/src/main/scala-2.12/app/softnetwork/elastic/client/ElasticConfig.scala +++ b/core/src/main/scala-2.12/app/softnetwork/elastic/client/ElasticConfig.scala @@ -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 @@ -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 } diff --git a/core/src/main/scala-2.13/app/softnetwork/elastic/client/ElasticConfig.scala b/core/src/main/scala-2.13/app/softnetwork/elastic/client/ElasticConfig.scala index fdfeb5c43..61384c6fa 100644 --- a/core/src/main/scala-2.13/app/softnetwork/elastic/client/ElasticConfig.scala +++ b/core/src/main/scala-2.13/app/softnetwork/elastic/client/ElasticConfig.scala @@ -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 @@ -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 } diff --git a/core/src/test/scala/app/softnetwork/elastic/client/ElasticConfigErrorLogSpec.scala b/core/src/test/scala/app/softnetwork/elastic/client/ElasticConfigErrorLogSpec.scala new file mode 100644 index 000000000..320e973d9 --- /dev/null +++ b/core/src/test/scala/app/softnetwork/elastic/client/ElasticConfigErrorLogSpec.scala @@ -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 + } +} diff --git a/sql/src/main/scala-2.12/app/softnetwork/elastic/sql/config/ElasticSqlConfig.scala b/sql/src/main/scala-2.12/app/softnetwork/elastic/sql/config/ElasticSqlConfig.scala index 92a5acb1c..d12e6e009 100644 --- a/sql/src/main/scala-2.12/app/softnetwork/elastic/sql/config/ElasticSqlConfig.scala +++ b/sql/src/main/scala-2.12/app/softnetwork/elastic/sql/config/ElasticSqlConfig.scala @@ -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 } diff --git a/sql/src/main/scala-2.13/app/softnetwork/elastic/sql/config/ElasticSqlConfig.scala b/sql/src/main/scala-2.13/app/softnetwork/elastic/sql/config/ElasticSqlConfig.scala index 9fd99a49d..70ba95658 100644 --- a/sql/src/main/scala-2.13/app/softnetwork/elastic/sql/config/ElasticSqlConfig.scala +++ b/sql/src/main/scala-2.13/app/softnetwork/elastic/sql/config/ElasticSqlConfig.scala @@ -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 } diff --git a/sql/src/main/scala/app/softnetwork/elastic/sql/config/ConfigErrorKeys.scala b/sql/src/main/scala/app/softnetwork/elastic/sql/config/ConfigErrorKeys.scala new file mode 100644 index 000000000..38488415d --- /dev/null +++ b/sql/src/main/scala/app/softnetwork/elastic/sql/config/ConfigErrorKeys.scala @@ -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(", ") +}