Skip to content

Commit 911d549

Browse files
authored
advancedTLS: Combine ClientOptions and ServerOptions to just Options (#7202)
* rename to Options * added some documentation * typos
1 parent 4879d51 commit 911d549

File tree

4 files changed

+54
-95
lines changed

4 files changed

+54
-95
lines changed

security/advancedtls/advancedtls.go

+25-66
Original file line numberDiff line numberDiff line change
@@ -184,66 +184,18 @@ const (
184184
)
185185

186186
// ClientOptions contains the fields needed to be filled by the client.
187-
type ClientOptions struct {
188-
// IdentityOptions is OPTIONAL on client side. This field only needs to be
189-
// set if mutual authentication is required on server side.
190-
IdentityOptions IdentityCertificateOptions
191-
// AdditionalPeerVerification is a custom verification check after certificate signature
192-
// check.
193-
// If this is set, we will perform this customized check after doing the
194-
// normal check(s) indicated by setting VerificationType.
195-
AdditionalPeerVerification PostHandshakeVerificationFunc
196-
// VerifyPeer is a custom verification check after certificate signature
197-
// check.
198-
// If this is set, we will perform this customized check after doing the
199-
// normal check(s) indicated by setting VerificationType.
200-
//
201-
// Deprecated: use AdditionalPeerVerification instead.
202-
VerifyPeer PostHandshakeVerificationFunc
203-
// RootOptions is OPTIONAL on client side. If not set, we will try to use the
204-
// default trust certificates in users' OS system.
205-
RootOptions RootCertificateOptions
206-
// VerificationType defines what type of server verification is done. See
207-
// the `VerificationType` enum for the different options.
208-
// Default: CertAndHostVerification
209-
VerificationType VerificationType
210-
// VType is the verification type on the client side.
211-
//
212-
// Deprecated: use VerificationType instead.
213-
VType VerificationType
214-
// RevocationOptions is the configurations for certificate revocation checks.
215-
// It could be nil if such checks are not needed.
216-
RevocationOptions *RevocationOptions
217-
// MinVersion contains the minimum TLS version that is acceptable.
218-
//
219-
// Deprecated: use MinTLSVersion instead.
220-
MinVersion uint16
221-
// MaxVersion contains the maximum TLS version that is acceptable.
222-
//
223-
// Deprecated: use MaxTLSVersion instead.
224-
MaxVersion uint16
225-
// MinTLSVersion contains the minimum TLS version that is acceptable.
226-
// The value should be set using tls.VersionTLSxx from https://pkg.go.dev/crypto/tls
227-
// By default, TLS 1.2 is currently used as the minimum when acting as a
228-
// client, and TLS 1.0 when acting as a server. TLS 1.0 is the minimum
229-
// supported by this package, both as a client and as a server. This
230-
// default may be changed over time affecting backwards compatibility.
231-
MinTLSVersion uint16
232-
// MaxTLSVersion contains the maximum TLS version that is acceptable.
233-
// The value should be set using tls.VersionTLSxx from https://pkg.go.dev/crypto/tls
234-
// By default, the maximum version supported by this package is used,
235-
// which is currently TLS 1.3. This default may be changed over time
236-
// affecting backwards compatibility.
237-
MaxTLSVersion uint16
238-
// serverNameOverride is for testing only. If set to a non-empty string, it
239-
// will override the virtual host name of authority (e.g. :authority header
240-
// field) in requests and the target hostname used during server cert
241-
// verification.
242-
serverNameOverride string
243-
}
187+
// Deprecated: use Options instead.
188+
type ClientOptions = Options
244189

245190
// ServerOptions contains the fields needed to be filled by the server.
246-
type ServerOptions struct {
191+
// Deprecated: use Options instead.
192+
type ServerOptions = Options
193+
194+
// Options contains the fields a user can configure when setting up TLS clients
195+
// and servers
196+
type Options struct {
197+
// IdentityOptions is OPTIONAL on client side. This field only needs to be
198+
// set if mutual authentication is required on server side.
247199
// IdentityOptions is REQUIRED on server side.
248200
IdentityOptions IdentityCertificateOptions
249201
// AdditionalPeerVerification is a custom verification check after certificate signature
@@ -261,9 +213,11 @@ type ServerOptions struct {
261213
// RootOptions is OPTIONAL on server side. This field only needs to be set if
262214
// mutual authentication is required(RequireClientCert is true).
263215
RootOptions RootCertificateOptions
264-
// If the server want the client to send certificates.
216+
// If the server requires the client to send certificates. This value is only
217+
// relevant when configuring options for the server. Is not used for
218+
// client-side configuration.
265219
RequireClientCert bool
266-
// VerificationType defines what type of client verification is done. See
220+
// VerificationType defines what type of peer verification is done. See
267221
// the `VerificationType` enum for the different options.
268222
// Default: CertAndHostVerification
269223
VerificationType VerificationType
@@ -295,9 +249,14 @@ type ServerOptions struct {
295249
// which is currently TLS 1.3. This default may be changed over time
296250
// affecting backwards compatibility.
297251
MaxTLSVersion uint16
252+
// serverNameOverride is for testing only and only relevant on the client
253+
// side. If set to a non-empty string, it will override the virtual host
254+
// name of authority (e.g. :authority header field) in requests and the
255+
// target hostname used during server cert verification.
256+
serverNameOverride string
298257
}
299258

300-
func (o *ClientOptions) config() (*tls.Config, error) {
259+
func (o *Options) clientConfig() (*tls.Config, error) {
301260
// TODO(gtcooke94) Remove this block when o.VerifyPeer is remoed.
302261
// VerifyPeer is deprecated, but do this to aid the transitory migration time.
303262
if o.AdditionalPeerVerification == nil {
@@ -401,7 +360,7 @@ func (o *ClientOptions) config() (*tls.Config, error) {
401360
return config, nil
402361
}
403362

404-
func (o *ServerOptions) config() (*tls.Config, error) {
363+
func (o *Options) serverConfig() (*tls.Config, error) {
405364
// TODO(gtcooke94) Remove this block when o.VerifyPeer is remoed.
406365
// VerifyPeer is deprecated, but do this to aid the transitory migration time.
407366
if o.AdditionalPeerVerification == nil {
@@ -696,8 +655,8 @@ func buildVerifyFunc(c *advancedTLSCreds,
696655

697656
// NewClientCreds uses ClientOptions to construct a TransportCredentials based
698657
// on TLS.
699-
func NewClientCreds(o *ClientOptions) (credentials.TransportCredentials, error) {
700-
conf, err := o.config()
658+
func NewClientCreds(o *Options) (credentials.TransportCredentials, error) {
659+
conf, err := o.clientConfig()
701660
if err != nil {
702661
return nil, err
703662
}
@@ -715,8 +674,8 @@ func NewClientCreds(o *ClientOptions) (credentials.TransportCredentials, error)
715674

716675
// NewServerCreds uses ServerOptions to construct a TransportCredentials based
717676
// on TLS.
718-
func NewServerCreds(o *ServerOptions) (credentials.TransportCredentials, error) {
719-
conf, err := o.config()
677+
func NewServerCreds(o *Options) (credentials.TransportCredentials, error) {
678+
conf, err := o.serverConfig()
720679
if err != nil {
721680
return nil, err
722681
}

security/advancedtls/advancedtls_integration_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ func (s) TestEnd2End(t *testing.T) {
336336
test := test
337337
t.Run(test.desc, func(t *testing.T) {
338338
// Start a server using ServerOptions in another goroutine.
339-
serverOptions := &ServerOptions{
339+
serverOptions := &Options{
340340
IdentityOptions: IdentityCertificateOptions{
341341
Certificates: test.serverCert,
342342
GetIdentityCertificatesForServer: test.serverGetCert,
@@ -363,7 +363,7 @@ func (s) TestEnd2End(t *testing.T) {
363363
addr := fmt.Sprintf("localhost:%v", lis.Addr().(*net.TCPAddr).Port)
364364
pb.RegisterGreeterServer(s, greeterServer{})
365365
go s.Serve(lis)
366-
clientOptions := &ClientOptions{
366+
clientOptions := &Options{
367367
IdentityOptions: IdentityCertificateOptions{
368368
Certificates: test.clientCert,
369369
GetIdentityCertificatesForClient: test.clientGetCert,
@@ -627,7 +627,7 @@ func (s) TestPEMFileProviderEnd2End(t *testing.T) {
627627
defer serverIdentityProvider.Close()
628628
defer serverRootProvider.Close()
629629
// Start a server and create a client using advancedtls API with Provider.
630-
serverOptions := &ServerOptions{
630+
serverOptions := &Options{
631631
IdentityOptions: IdentityCertificateOptions{
632632
IdentityProvider: serverIdentityProvider,
633633
},
@@ -654,7 +654,7 @@ func (s) TestPEMFileProviderEnd2End(t *testing.T) {
654654
addr := fmt.Sprintf("localhost:%v", lis.Addr().(*net.TCPAddr).Port)
655655
pb.RegisterGreeterServer(s, greeterServer{})
656656
go s.Serve(lis)
657-
clientOptions := &ClientOptions{
657+
clientOptions := &Options{
658658
IdentityOptions: IdentityCertificateOptions{
659659
IdentityProvider: clientIdentityProvider,
660660
},
@@ -764,7 +764,7 @@ func (s) TestDefaultHostNameCheck(t *testing.T) {
764764
test := test
765765
t.Run(test.desc, func(t *testing.T) {
766766
// Start a server using ServerOptions in another goroutine.
767-
serverOptions := &ServerOptions{
767+
serverOptions := &Options{
768768
IdentityOptions: IdentityCertificateOptions{
769769
Certificates: test.serverCert,
770770
},
@@ -785,7 +785,7 @@ func (s) TestDefaultHostNameCheck(t *testing.T) {
785785
addr := fmt.Sprintf("localhost:%v", lis.Addr().(*net.TCPAddr).Port)
786786
pb.RegisterGreeterServer(s, greeterServer{})
787787
go s.Serve(lis)
788-
clientOptions := &ClientOptions{
788+
clientOptions := &Options{
789789
RootOptions: RootCertificateOptions{
790790
RootCACerts: test.clientRoot,
791791
},
@@ -902,7 +902,7 @@ func (s) TestTLSVersions(t *testing.T) {
902902
test := test
903903
t.Run(test.desc, func(t *testing.T) {
904904
// Start a server using ServerOptions in another goroutine.
905-
serverOptions := &ServerOptions{
905+
serverOptions := &Options{
906906
IdentityOptions: IdentityCertificateOptions{
907907
Certificates: []tls.Certificate{cs.ServerPeerLocalhost1},
908908
},
@@ -925,7 +925,7 @@ func (s) TestTLSVersions(t *testing.T) {
925925
addr := fmt.Sprintf("localhost:%v", lis.Addr().(*net.TCPAddr).Port)
926926
pb.RegisterGreeterServer(s, greeterServer{})
927927
go s.Serve(lis)
928-
clientOptions := &ClientOptions{
928+
clientOptions := &Options{
929929
RootOptions: RootCertificateOptions{
930930
RootCACerts: cs.ClientTrust1,
931931
},

security/advancedtls/advancedtls_test.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ func (s) TestClientOptionsConfigErrorCases(t *testing.T) {
134134
for _, test := range tests {
135135
test := test
136136
t.Run(test.desc, func(t *testing.T) {
137-
clientOptions := &ClientOptions{
137+
clientOptions := &Options{
138138
VerificationType: test.clientVerificationType,
139139
IdentityOptions: test.IdentityOptions,
140140
RootOptions: test.RootOptions,
141141
MinTLSVersion: test.MinVersion,
142142
MaxTLSVersion: test.MaxVersion,
143143
}
144-
_, err := clientOptions.config()
144+
_, err := clientOptions.clientConfig()
145145
if err == nil {
146146
t.Fatalf("ClientOptions{%v}.config() returns no err, wantErr != nil", clientOptions)
147147
}
@@ -154,10 +154,10 @@ func (s) TestClientOptionsConfigErrorCases(t *testing.T) {
154154
// VerificationType. This should error because one cannot skip default
155155
// verification and provide no root credentials",
156156
func (s) TestClientOptionsWithDeprecatedVType(t *testing.T) {
157-
clientOptions := &ClientOptions{
157+
clientOptions := &Options{
158158
VType: SkipVerification,
159159
}
160-
_, err := clientOptions.config()
160+
_, err := clientOptions.clientConfig()
161161
if err == nil {
162162
t.Fatalf("ClientOptions{%v}.config() returns no err, wantErr != nil", clientOptions)
163163
}
@@ -192,14 +192,14 @@ func (s) TestClientOptionsConfigSuccessCases(t *testing.T) {
192192
for _, test := range tests {
193193
test := test
194194
t.Run(test.desc, func(t *testing.T) {
195-
clientOptions := &ClientOptions{
195+
clientOptions := &Options{
196196
VerificationType: test.clientVerificationType,
197197
IdentityOptions: test.IdentityOptions,
198198
RootOptions: test.RootOptions,
199199
MinTLSVersion: test.MinVersion,
200200
MaxTLSVersion: test.MaxVersion,
201201
}
202-
clientConfig, err := clientOptions.config()
202+
clientConfig, err := clientOptions.clientConfig()
203203
if err != nil {
204204
t.Fatalf("ClientOptions{%v}.config() = %v, wantErr == nil", clientOptions, err)
205205
}
@@ -288,17 +288,17 @@ func (s) TestServerOptionsConfigErrorCases(t *testing.T) {
288288
for _, test := range tests {
289289
test := test
290290
t.Run(test.desc, func(t *testing.T) {
291-
serverOptions := &ServerOptions{
291+
serverOptions := &Options{
292292
VerificationType: test.serverVerificationType,
293293
RequireClientCert: test.requireClientCert,
294294
IdentityOptions: test.IdentityOptions,
295295
RootOptions: test.RootOptions,
296296
MinTLSVersion: test.MinVersion,
297297
MaxTLSVersion: test.MaxVersion,
298298
}
299-
_, err := serverOptions.config()
299+
_, err := serverOptions.serverConfig()
300300
if err == nil {
301-
t.Fatalf("ServerOptions{%v}.config() returns no err, wantErr != nil", serverOptions)
301+
t.Fatalf("ServerOptions{%v}.serverConfig() returns no err, wantErr != nil", serverOptions)
302302
}
303303
})
304304
}
@@ -309,10 +309,10 @@ func (s) TestServerOptionsConfigErrorCases(t *testing.T) {
309309
// VerificationType. This should error because one cannot skip default
310310
// verification and provide no root credentials",
311311
func (s) TestServerOptionsWithDeprecatedVType(t *testing.T) {
312-
serverOptions := &ServerOptions{
312+
serverOptions := &Options{
313313
VType: SkipVerification,
314314
}
315-
_, err := serverOptions.config()
315+
_, err := serverOptions.serverConfig()
316316
if err == nil {
317317
t.Fatalf("ClientOptions{%v}.config() returns no err, wantErr != nil", serverOptions)
318318
}
@@ -355,15 +355,15 @@ func (s) TestServerOptionsConfigSuccessCases(t *testing.T) {
355355
for _, test := range tests {
356356
test := test
357357
t.Run(test.desc, func(t *testing.T) {
358-
serverOptions := &ServerOptions{
358+
serverOptions := &Options{
359359
VerificationType: test.serverVerificationType,
360360
RequireClientCert: test.requireClientCert,
361361
IdentityOptions: test.IdentityOptions,
362362
RootOptions: test.RootOptions,
363363
MinTLSVersion: test.MinVersion,
364364
MaxTLSVersion: test.MaxVersion,
365365
}
366-
serverConfig, err := serverOptions.config()
366+
serverConfig, err := serverOptions.serverConfig()
367367
if err != nil {
368368
t.Fatalf("ServerOptions{%v}.config() = %v, wantErr == nil", serverOptions, err)
369369
}
@@ -829,7 +829,7 @@ func (s) TestClientServerHandshake(t *testing.T) {
829829
t.Fatalf("Failed to listen: %v", err)
830830
}
831831
// Start a server using ServerOptions in another goroutine.
832-
serverOptions := &ServerOptions{
832+
serverOptions := &Options{
833833
IdentityOptions: IdentityCertificateOptions{
834834
Certificates: test.serverCert,
835835
GetIdentityCertificatesForServer: test.serverGetCert,
@@ -845,7 +845,7 @@ func (s) TestClientServerHandshake(t *testing.T) {
845845
VerificationType: test.serverVerificationType,
846846
RevocationOptions: test.serverRevocationOptions,
847847
}
848-
go func(done chan credentials.AuthInfo, lis net.Listener, serverOptions *ServerOptions) {
848+
go func(done chan credentials.AuthInfo, lis net.Listener, serverOptions *Options) {
849849
serverRawConn, err := lis.Accept()
850850
if err != nil {
851851
close(done)
@@ -873,7 +873,7 @@ func (s) TestClientServerHandshake(t *testing.T) {
873873
t.Fatalf("Client failed to connect to %s. Error: %v", lisAddr, err)
874874
}
875875
defer conn.Close()
876-
clientOptions := &ClientOptions{
876+
clientOptions := &Options{
877877
IdentityOptions: IdentityCertificateOptions{
878878
Certificates: test.clientCert,
879879
GetIdentityCertificatesForClient: test.clientGetCert,
@@ -944,7 +944,7 @@ func (s) TestAdvancedTLSOverrideServerName(t *testing.T) {
944944
if err := cs.LoadCerts(); err != nil {
945945
t.Fatalf("cs.LoadCerts() failed, err: %v", err)
946946
}
947-
clientOptions := &ClientOptions{
947+
clientOptions := &Options{
948948
RootOptions: RootCertificateOptions{
949949
RootCACerts: cs.ClientTrust1,
950950
},
@@ -988,16 +988,16 @@ func (s) TestGetCertificatesSNI(t *testing.T) {
988988
for _, test := range tests {
989989
test := test
990990
t.Run(test.desc, func(t *testing.T) {
991-
serverOptions := &ServerOptions{
991+
serverOptions := &Options{
992992
IdentityOptions: IdentityCertificateOptions{
993993
GetIdentityCertificatesForServer: func(info *tls.ClientHelloInfo) ([]*tls.Certificate, error) {
994994
return []*tls.Certificate{&cs.ServerCert1, &cs.ServerCert2, &cs.ServerPeer3}, nil
995995
},
996996
},
997997
}
998-
serverConfig, err := serverOptions.config()
998+
serverConfig, err := serverOptions.serverConfig()
999999
if err != nil {
1000-
t.Fatalf("serverOptions.config() failed: %v", err)
1000+
t.Fatalf("serverOptions.serverConfig() failed: %v", err)
10011001
}
10021002
pointFormatUncompressed := uint8(0)
10031003
clientHello := &tls.ClientHelloInfo{

security/advancedtls/sni.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525

2626
// buildGetCertificates returns the certificate that matches the SNI field
2727
// for the given ClientHelloInfo, defaulting to the first element of o.GetCertificates.
28-
func buildGetCertificates(clientHello *tls.ClientHelloInfo, o *ServerOptions) (*tls.Certificate, error) {
28+
func buildGetCertificates(clientHello *tls.ClientHelloInfo, o *Options) (*tls.Certificate, error) {
2929
if o.IdentityOptions.GetIdentityCertificatesForServer == nil {
3030
return nil, fmt.Errorf("function GetCertificates must be specified")
3131
}

0 commit comments

Comments
 (0)