Skip to content

feat: Use reference config for ssl context #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 19, 2019
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
14 changes: 10 additions & 4 deletions ssl-config/src/main/scala/com/avast/sst/ssl/SslContextModule.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.avast.sst.ssl

import cats.effect.Sync
import com.typesafe.config.Config
import com.typesafe.config.{Config, ConfigFactory}
import com.typesafe.sslconfig.ssl.{
ConfigSSLContextBuilder,
DefaultKeyManagerFactoryWrapper,
Expand All @@ -14,13 +14,19 @@ import scala.language.higherKinds

object SslContextModule {

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

private def referenceConfigUnsafe(): Config = ConfigFactory.defaultReference().getConfig("ssl-config")

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@ import org.scalatest.funsuite.AnyFunSuite

class SslContextModuleTest extends AnyFunSuite {

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

test("SslContextModule initializes properly from JKS store with provided config") {
val sslContext = SslContextModule.make[SyncIO](ConfigFactory.load().getConfig("ssl-config"), withReference = false).unsafeRunSync()
assert(sslContext.getProtocol === "TLSv1.2")
}

test("SslContextModule fails to initialize for empty config and no reference config") {
val result = SslContextModule.make[SyncIO](ConfigFactory.empty(), withReference = false).attempt.unsafeRunSync()
assert(result.isLeft)
}

}