@@ -35,6 +35,7 @@ const (
35
35
defaultRequestTimeout = "30000" // millis
36
36
defaultDisconnectTimeout = "30000" // millis
37
37
defaultReadyTimeout = "0" // millis
38
+ insecureWarning = "WARNING: you have turned off SSL certificate validation. This is insecure and not recommended."
38
39
)
39
40
40
41
// Session provides APIs to create NamedCaches. The [NewSession] method creates a
@@ -69,6 +70,7 @@ type SessionOptions struct {
69
70
RequestTimeout time.Duration
70
71
DisconnectTimeout time.Duration
71
72
ReadyTimeout time.Duration
73
+ TlSConfig * tls.Config
72
74
}
73
75
74
76
// NewSession creates a new Session with the specified sessionOptions.
@@ -96,7 +98,14 @@ type SessionOptions struct {
96
98
//
97
99
// To Configure SSL, you must first enable SSL on the gRPC Proxy, see [gRPC Proxy Server] for details.
98
100
//
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:
100
109
//
101
110
// session, err := coherence.NewSession(ctx, coherence.WithTLSClientCert("/path/to/client/certificate"),
102
111
// coherence.WithTLSClientKey("/path/path/to/client/key"),
@@ -278,6 +287,14 @@ func WithReadyTimeout(timeout time.Duration) func(sessionOptions *SessionOptions
278
287
}
279
288
}
280
289
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
+
281
298
// ID returns the identifier of a session.
282
299
func (s * Session ) ID () string {
283
300
return s .sessionID .String ()
@@ -596,6 +613,14 @@ func (s *SessionOptions) createTLSOption() (grpc.DialOption, error) {
596
613
return grpc .WithTransportCredentials (insecure .NewCredentials ()), nil
597
614
}
598
615
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
+
599
624
var (
600
625
err error
601
626
cp * x509.CertPool
@@ -612,7 +637,7 @@ func (s *SessionOptions) createTLSOption() (grpc.DialOption, error) {
612
637
613
638
ignoreInvalidCerts := ignoreInvalidCertsEnv == "true"
614
639
if ignoreInvalidCerts {
615
- log .Println ("WARNING: you have turned off SSL certificate validation. This is insecure and not recommended." )
640
+ log .Println (insecureWarning )
616
641
}
617
642
s .IgnoreInvalidCerts = ignoreInvalidCerts
618
643
@@ -695,8 +720,12 @@ func (s *SessionOptions) String() string {
695
720
s .Address , s .PlainText , s .Scope , s .Format , s .RequestTimeout , s .DisconnectTimeout , s .ReadyTimeout ))
696
721
697
722
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
+ }
700
729
}
701
730
702
731
return sb .String ()
0 commit comments