@@ -35,6 +35,7 @@ const (
3535 defaultRequestTimeout = "30000" // millis
3636 defaultDisconnectTimeout = "30000" // millis
3737 defaultReadyTimeout = "0" // millis
38+ insecureWarning = "WARNING: you have turned off SSL certificate validation. This is insecure and not recommended."
3839)
3940
4041// Session provides APIs to create NamedCaches. The [NewSession] method creates a
@@ -69,6 +70,7 @@ type SessionOptions struct {
6970 RequestTimeout time.Duration
7071 DisconnectTimeout time.Duration
7172 ReadyTimeout time.Duration
73+ TlSConfig * tls.Config
7274}
7375
7476// NewSession creates a new Session with the specified sessionOptions.
@@ -96,7 +98,14 @@ type SessionOptions struct {
9698//
9799// To Configure SSL, you must first enable SSL on the gRPC Proxy, see [gRPC Proxy Server] for details.
98100//
99- // You can use the following to set the required TLS options when creating a session:
101+ // There are a number of ways to set the TLS options when creating a session.
102+ // You can use [WithTLSConfig] to specify a custom tls.Config or specify the client certificate, key and trust
103+ // certificate using additional session options or using environment variables. See below for more details.
104+ //
105+ // myTlSConfig = &tls.Config{....}
106+ // session, err := coherence.NewSession(ctx, coherence.WithTLSConfig(myTLSConfig))
107+ //
108+ // You can also use the following to set the required TLS options when creating a session:
100109//
101110// session, err := coherence.NewSession(ctx, coherence.WithTLSClientCert("/path/to/client/certificate"),
102111// coherence.WithTLSClientKey("/path/path/to/client/key"),
@@ -278,6 +287,14 @@ func WithReadyTimeout(timeout time.Duration) func(sessionOptions *SessionOptions
278287 }
279288}
280289
290+ // WithTLSConfig returns a function to set the tls.Config directly. This is typically used
291+ // when you require fine-grained control over these options.
292+ func WithTLSConfig (tlsConfig * tls.Config ) func (sessionOptions * SessionOptions ) {
293+ return func (s * SessionOptions ) {
294+ s .TlSConfig = tlsConfig
295+ }
296+ }
297+
281298// ID returns the identifier of a session.
282299func (s * Session ) ID () string {
283300 return s .sessionID .String ()
@@ -596,6 +613,14 @@ func (s *SessionOptions) createTLSOption() (grpc.DialOption, error) {
596613 return grpc .WithTransportCredentials (insecure .NewCredentials ()), nil
597614 }
598615
616+ // check if a tls.Config has been set and use this, otherwise continue to check for env and other options
617+ if s .TlSConfig != nil {
618+ if s .TlSConfig .InsecureSkipVerify {
619+ log .Println (insecureWarning )
620+ }
621+ return grpc .WithTransportCredentials (credentials .NewTLS (s .TlSConfig )), nil
622+ }
623+
599624 var (
600625 err error
601626 cp * x509.CertPool
@@ -612,7 +637,7 @@ func (s *SessionOptions) createTLSOption() (grpc.DialOption, error) {
612637
613638 ignoreInvalidCerts := ignoreInvalidCertsEnv == "true"
614639 if ignoreInvalidCerts {
615- log .Println ("WARNING: you have turned off SSL certificate validation. This is insecure and not recommended." )
640+ log .Println (insecureWarning )
616641 }
617642 s .IgnoreInvalidCerts = ignoreInvalidCerts
618643
@@ -695,8 +720,12 @@ func (s *SessionOptions) String() string {
695720 s .Address , s .PlainText , s .Scope , s .Format , s .RequestTimeout , s .DisconnectTimeout , s .ReadyTimeout ))
696721
697722 if ! s .PlainText {
698- sb .WriteString (fmt .Sprintf (" clientCertPath=%v, clientKeyPath=%v, caCertPath=%v, igoreInvalidCerts=%v" ,
699- s .ClientCertPath , s .ClientKeyPath , s .CaCertPath , s .IgnoreInvalidCerts ))
723+ if s .TlSConfig == nil {
724+ sb .WriteString (fmt .Sprintf (" clientCertPath=%v, clientKeyPath=%v, caCertPath=%v, igoreInvalidCerts=%v" ,
725+ s .ClientCertPath , s .ClientKeyPath , s .CaCertPath , s .IgnoreInvalidCerts ))
726+ } else {
727+ sb .WriteString ("tls.Config specified" )
728+ }
700729 }
701730
702731 return sb .String ()
0 commit comments