@@ -20,7 +20,7 @@ import (
20
20
// GetClientTLSConfig returns a tls.Config for client use configured with the
21
21
// sign certificate, and a new certificate pool with the sign root certificate.
22
22
// The client certificate will automatically rotate before expiring.
23
- func (c * Client ) GetClientTLSConfig (ctx context.Context , sign * api.SignResponse , pk crypto.PrivateKey ) (* tls.Config , error ) {
23
+ func (c * Client ) GetClientTLSConfig (ctx context.Context , sign * api.SignResponse , pk crypto.PrivateKey , options ... TLSOption ) (* tls.Config , error ) {
24
24
cert , err := TLSCertificate (sign , pk )
25
25
if err != nil {
26
26
return nil , err
@@ -36,10 +36,15 @@ func (c *Client) GetClientTLSConfig(ctx context.Context, sign *api.SignResponse,
36
36
tlsConfig .GetClientCertificate = renewer .GetClientCertificate
37
37
tlsConfig .PreferServerCipherSuites = true
38
38
// Build RootCAs with given root certificate
39
- if pool := c . getCertPool (sign ); pool != nil {
39
+ if pool := getCertPool (sign ); pool != nil {
40
40
tlsConfig .RootCAs = pool
41
41
}
42
42
43
+ // Apply options if given
44
+ if err := setTLSOptions (tlsConfig , options ); err != nil {
45
+ return nil , err
46
+ }
47
+
43
48
// Update renew function with transport
44
49
tr , err := getDefaultTransport (tlsConfig )
45
50
if err != nil {
@@ -56,7 +61,7 @@ func (c *Client) GetClientTLSConfig(ctx context.Context, sign *api.SignResponse,
56
61
// sign certificate, and a new certificate pool with the sign root certificate.
57
62
// The returned tls.Config will only verify the client certificate if provided.
58
63
// The server certificate will automatically rotate before expiring.
59
- func (c * Client ) GetServerTLSConfig (ctx context.Context , sign * api.SignResponse , pk crypto.PrivateKey ) (* tls.Config , error ) {
64
+ func (c * Client ) GetServerTLSConfig (ctx context.Context , sign * api.SignResponse , pk crypto.PrivateKey , options ... TLSOption ) (* tls.Config , error ) {
60
65
cert , err := TLSCertificate (sign , pk )
61
66
if err != nil {
62
67
return nil , err
@@ -74,13 +79,18 @@ func (c *Client) GetServerTLSConfig(ctx context.Context, sign *api.SignResponse,
74
79
tlsConfig .GetClientCertificate = renewer .GetClientCertificate
75
80
tlsConfig .PreferServerCipherSuites = true
76
81
// Build RootCAs with given root certificate
77
- if pool := c . getCertPool (sign ); pool != nil {
82
+ if pool := getCertPool (sign ); pool != nil {
78
83
tlsConfig .ClientCAs = pool
79
- tlsConfig .ClientAuth = tls .VerifyClientCertIfGiven
84
+ tlsConfig .ClientAuth = tls .RequireAndVerifyClientCert
80
85
// Add RootCAs for refresh client
81
86
tlsConfig .RootCAs = pool
82
87
}
83
88
89
+ // Apply options if given
90
+ if err := setTLSOptions (tlsConfig , options ); err != nil {
91
+ return nil , err
92
+ }
93
+
84
94
// Update renew function with transport
85
95
tr , err := getDefaultTransport (tlsConfig )
86
96
if err != nil {
@@ -93,44 +103,15 @@ func (c *Client) GetServerTLSConfig(ctx context.Context, sign *api.SignResponse,
93
103
return tlsConfig , nil
94
104
}
95
105
96
- // GetServerMutualTLSConfig returns a tls.Config for server use configured with
97
- // the sign certificate, and a new certificate pool with the sign root certificate.
98
- // The returned tls.Config will always require and verify a client certificate.
99
- // The server certificate will automatically rotate before expiring.
100
- func (c * Client ) GetServerMutualTLSConfig (ctx context.Context , sign * api.SignResponse , pk crypto.PrivateKey ) (* tls.Config , error ) {
101
- tlsConfig , err := c .GetServerTLSConfig (ctx , sign , pk )
102
- if err != nil {
103
- return nil , err
104
- }
105
- tlsConfig .ClientAuth = tls .RequireAndVerifyClientCert
106
- return tlsConfig , nil
107
- }
108
-
109
106
// Transport returns an http.Transport configured to use the client certificate from the sign response.
110
- func (c * Client ) Transport (ctx context.Context , sign * api.SignResponse , pk crypto.PrivateKey ) (* http.Transport , error ) {
111
- tlsConfig , err := c .GetClientTLSConfig (ctx , sign , pk )
107
+ func (c * Client ) Transport (ctx context.Context , sign * api.SignResponse , pk crypto.PrivateKey , options ... TLSOption ) (* http.Transport , error ) {
108
+ tlsConfig , err := c .GetClientTLSConfig (ctx , sign , pk , options ... )
112
109
if err != nil {
113
110
return nil , err
114
111
}
115
112
return getDefaultTransport (tlsConfig )
116
113
}
117
114
118
- // getCertPool returns the transport x509.CertPool or the one from the sign
119
- // request.
120
- func (c * Client ) getCertPool (sign * api.SignResponse ) * x509.CertPool {
121
- // Return the transport certPool
122
- if c .certPool != nil {
123
- return c .certPool
124
- }
125
- // Return certificate used in sign request.
126
- if root , err := RootCertificate (sign ); err == nil {
127
- pool := x509 .NewCertPool ()
128
- pool .AddCert (root )
129
- return pool
130
- }
131
- return nil
132
- }
133
-
134
115
// Certificate returns the server or client certificate from the sign response.
135
116
func Certificate (sign * api.SignResponse ) (* x509.Certificate , error ) {
136
117
if sign .ServerPEM .Certificate == nil {
@@ -189,6 +170,17 @@ func TLSCertificate(sign *api.SignResponse, pk crypto.PrivateKey) (*tls.Certific
189
170
return & cert , nil
190
171
}
191
172
173
+ // getCertPool returns the transport x509.CertPool or the one from the sign
174
+ // request.
175
+ func getCertPool (sign * api.SignResponse ) * x509.CertPool {
176
+ if root , err := RootCertificate (sign ); err == nil {
177
+ pool := x509 .NewCertPool ()
178
+ pool .AddCert (root )
179
+ return pool
180
+ }
181
+ return nil
182
+ }
183
+
192
184
func getDefaultTLSConfig (sign * api.SignResponse ) * tls.Config {
193
185
if sign .TLSOptions != nil {
194
186
return sign .TLSOptions .TLSConfig ()
0 commit comments