Skip to content

Commit 4ded34b

Browse files
hanny24mergify[bot]
authored andcommitted
feat: Use reference config for ssl context (#110)
* feat: Use reference config for ssl context * comments
1 parent 7c76ce1 commit 4ded34b

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

ssl-config/src/main/scala/com/avast/sst/ssl/SslContextModule.scala

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.avast.sst.ssl
22

33
import cats.effect.Sync
4-
import com.typesafe.config.Config
4+
import com.typesafe.config.{Config, ConfigFactory}
55
import com.typesafe.sslconfig.ssl.{
66
ConfigSSLContextBuilder,
77
DefaultKeyManagerFactoryWrapper,
@@ -14,13 +14,19 @@ import scala.language.higherKinds
1414

1515
object SslContextModule {
1616

17-
/** Initializes [[javax.net.ssl.SSLContext]] from the provided config. */
18-
def make[F[_]: Sync](config: Config): F[SSLContext] = Sync[F].delay {
17+
/**
18+
* Initializes [[javax.net.ssl.SSLContext]] from the provided config.
19+
* @param withReference Whether we should use reference config of "ssl-config" library as well.
20+
*/
21+
def make[F[_]: Sync](config: Config, withReference: Boolean = true): F[SSLContext] = Sync[F].delay {
1922
val loggerFactory = Slf4jLogger.factory
23+
val finalConfig = if (withReference) config.withFallback(referenceConfigUnsafe()) else config
2024
new ConfigSSLContextBuilder(loggerFactory,
21-
SSLConfigFactory.parse(config, loggerFactory),
25+
SSLConfigFactory.parse(finalConfig, loggerFactory),
2226
new DefaultKeyManagerFactoryWrapper(KeyManagerFactory.getDefaultAlgorithm),
2327
new DefaultTrustManagerFactoryWrapper(TrustManagerFactory.getDefaultAlgorithm)).build
2428
}
2529

30+
private def referenceConfigUnsafe(): Config = ConfigFactory.defaultReference().getConfig("ssl-config")
31+
2632
}

ssl-config/src/test/scala/com/avast/sst/ssl/SslContextModuleTest.scala

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@ import org.scalatest.funsuite.AnyFunSuite
66

77
class SslContextModuleTest extends AnyFunSuite {
88

9-
test("SslContextModule initializes properly from JKS store") {
10-
val sslContext = SslContextModule.make[SyncIO](ConfigFactory.load().getConfig("ssl-config")).unsafeRunSync()
9+
test("SslContextModule initializes properly from JKS store with reference config") {
10+
val sslContext = SslContextModule.make[SyncIO](ConfigFactory.empty()).unsafeRunSync()
1111
assert(sslContext.getProtocol === "TLSv1.2")
1212
}
1313

14+
test("SslContextModule initializes properly from JKS store with provided config") {
15+
val sslContext = SslContextModule.make[SyncIO](ConfigFactory.load().getConfig("ssl-config"), withReference = false).unsafeRunSync()
16+
assert(sslContext.getProtocol === "TLSv1.2")
17+
}
18+
19+
test("SslContextModule fails to initialize for empty config and no reference config") {
20+
val result = SslContextModule.make[SyncIO](ConfigFactory.empty(), withReference = false).attempt.unsafeRunSync()
21+
assert(result.isLeft)
22+
}
23+
1424
}

0 commit comments

Comments
 (0)