Skip to content

Commit 6ac04d5

Browse files
authored
feat: Add makeMaybe method to SslContextModule (#183)
1 parent 06846ec commit 6ac04d5

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

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

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

33
import cats.effect.Sync
4+
import cats.syntax.functor._
45
import com.typesafe.config.{Config, ConfigFactory}
56
import com.typesafe.sslconfig.ssl.{
67
ConfigSSLContextBuilder,
@@ -12,6 +13,8 @@ import javax.net.ssl.{KeyManagerFactory, SSLContext, TrustManagerFactory}
1213

1314
object SslContextModule {
1415

16+
private val SslContextEnabledKey = "enabled"
17+
1518
/** Initializes [[javax.net.ssl.SSLContext]] from the provided config.
1619
*
1720
* @param withReference Whether we should use reference config of "ssl-config" library as well.
@@ -27,6 +30,22 @@ object SslContextModule {
2730
).build
2831
}
2932

33+
/** Initializes [[javax.net.ssl.SSLContext]] from the provided config if it is enabled.
34+
*
35+
* Expects a boolean value `enabled` at the root of the provided [[com.typesafe.config.Config]]
36+
* which determines whether to initialize the context or not.
37+
*
38+
* @param withReference Whether we should use reference config of "ssl-config" library as well.
39+
*/
40+
def makeMaybe[F[_]: Sync](config: Config, withReference: Boolean = true): F[Option[SSLContext]] = {
41+
if (config.hasPath(SslContextEnabledKey) && config.getBoolean(SslContextEnabledKey)) {
42+
make(config, withReference).map(Some(_))
43+
} else {
44+
Sync[F].delay(None)
45+
}
46+
47+
}
48+
3049
private def referenceConfigUnsafe(): Config = ConfigFactory.defaultReference().getConfig("ssl-config")
3150

3251
}

0 commit comments

Comments
 (0)