Skip to content

Commit de9f697

Browse files
committed
refactor(go): refactor WithTLS
1 parent 18588f1 commit de9f697

File tree

2 files changed

+59
-40
lines changed

2 files changed

+59
-40
lines changed

foreign/go/client/tcp/tcp_core.go

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,7 @@ type config struct {
6363
serverAddress string
6464
// tlsEnabled indicates whether to use TLS when connecting to the server
6565
tlsEnabled bool
66-
// tlsDomain is the domain to use for TLS when connecting to the server
67-
// If empty, automatically extracts the hostname/IP from serverAddress
68-
tlsDomain string
69-
// tlsCAFile is the path to the CA file to use for TLS
70-
tlsCAFile string
71-
// tlsValidateCertificate indicates whether to validate the server's TLS certificate
72-
tlsValidateCertificate bool
66+
tls tlsConfig
7367
// autoLogin indicates whether to automatically login user after establishing connection.
7468
autoLogin AutoLogin
7569
// reconnection indicates whether to automatically reconnect when disconnected
@@ -80,14 +74,12 @@ type config struct {
8074

8175
func defaultTcpClientConfig() config {
8276
return config{
83-
serverAddress: "127.0.0.1:8090",
84-
tlsEnabled: false,
85-
tlsDomain: "",
86-
tlsCAFile: "",
87-
tlsValidateCertificate: true,
88-
autoLogin: AutoLogin{},
89-
reconnection: defaultTcpClientReconnectionConfig(),
90-
noDelay: false,
77+
serverAddress: "127.0.0.1:8090",
78+
tlsEnabled: false,
79+
tls: defaultTLSConfig(),
80+
autoLogin: AutoLogin{},
81+
reconnection: defaultTcpClientReconnectionConfig(),
82+
noDelay: false,
9183
}
9284
}
9385

@@ -107,6 +99,24 @@ func defaultTcpClientReconnectionConfig() tcpClientReconnectionConfig {
10799
}
108100
}
109101

102+
type tlsConfig struct {
103+
// tlsDomain is the domain to use for TLS when connecting to the server
104+
// If empty, automatically extracts the hostname/IP from serverAddress
105+
tlsDomain string
106+
// tlsCAFile is the path to the CA file to use for TLS
107+
tlsCAFile string
108+
// tlsValidateCertificate indicates whether to validate the server's TLS certificate
109+
tlsValidateCertificate bool
110+
}
111+
112+
func defaultTLSConfig() tlsConfig {
113+
return tlsConfig{
114+
tlsDomain: "",
115+
tlsCAFile: "",
116+
tlsValidateCertificate: true,
117+
}
118+
}
119+
110120
type AutoLogin struct {
111121
enabled bool
112122
credentials Credentials
@@ -145,32 +155,40 @@ func WithServerAddress(address string) Option {
145155
}
146156
}
147157

148-
// WithTLS enables or disables TLS for the TCP client.
149-
func WithTLS(enabled bool) Option {
158+
// TLSOption is a functional option for configuring TLS settings.
159+
type TLSOption func(cfg *tlsConfig)
160+
161+
// WithTLS enables TLS for the TCP client and applies the given TLS options.
162+
func WithTLS(tlsOpts ...TLSOption) Option {
150163
return func(opts *Options) {
151-
opts.config.tlsEnabled = enabled
164+
opts.config.tlsEnabled = true
165+
for _, tlsOpt := range tlsOpts {
166+
if tlsOpt != nil {
167+
tlsOpt(&opts.config.tls)
168+
}
169+
}
152170
}
153171
}
154172

155173
// WithTLSDomain sets the TLS domain for server name indication (SNI).
156-
// If empty, the domain will be automatically extracted from the server address.
157-
func WithTLSDomain(domain string) Option {
158-
return func(opts *Options) {
159-
opts.config.tlsDomain = domain
174+
// If not provided, the domain will be automatically extracted from the server address.
175+
func WithTLSDomain(domain string) TLSOption {
176+
return func(cfg *tlsConfig) {
177+
cfg.tlsDomain = domain
160178
}
161179
}
162180

163-
// WithTLSCAFile sets the path to the CA certificate file for TLS verification.
164-
func WithTLSCAFile(path string) Option {
165-
return func(opts *Options) {
166-
opts.config.tlsCAFile = path
181+
// WithCAFile sets the path to the CA certificate file for TLS verification.
182+
func WithCAFile(path string) TLSOption {
183+
return func(cfg *tlsConfig) {
184+
cfg.tlsCAFile = path
167185
}
168186
}
169187

170188
// WithTLSValidateCertificate enables or disables TLS certificate validation.
171-
func WithTLSValidateCertificate(validate bool) Option {
172-
return func(opts *Options) {
173-
opts.config.tlsValidateCertificate = validate
189+
func WithTLSValidateCertificate(validate bool) TLSOption {
190+
return func(cfg *tlsConfig) {
191+
cfg.tlsValidateCertificate = validate
174192
}
175193
}
176194

@@ -392,11 +410,11 @@ func (c *IggyTcpClient) connect() error {
392410

393411
func (c *IggyTcpClient) createTLSConfig() (*tls.Config, error) {
394412
tlsConfig := &tls.Config{
395-
InsecureSkipVerify: !c.config.tlsValidateCertificate,
413+
InsecureSkipVerify: !c.config.tls.tlsValidateCertificate,
396414
}
397415

398416
// Set server name for SNI
399-
serverName := c.config.tlsDomain
417+
serverName := c.config.tls.tlsDomain
400418
if serverName == "" {
401419
// Extract hostname from server address (format: "host:port")
402420
host := c.currentServerAddress
@@ -412,8 +430,8 @@ func (c *IggyTcpClient) createTLSConfig() (*tls.Config, error) {
412430
tlsConfig.ServerName = serverName
413431

414432
// Load CA certificate if provided
415-
if c.config.tlsCAFile != "" {
416-
caCert, err := os.ReadFile(c.config.tlsCAFile)
433+
if c.config.tls.tlsCAFile != "" {
434+
caCert, err := os.ReadFile(c.config.tls.tlsCAFile)
417435
if err != nil {
418436
return nil, ierror.ErrInvalidTlsCertificatePath
419437
}

foreign/go/tests/tls_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,10 @@ func TestTCPTLSConnection_WithCA_Success(t *testing.T) {
155155
cli, err := client.NewIggyClient(
156156
client.WithTcp(
157157
tcp.WithServerAddress(connectAddr),
158-
tcp.WithTLS(true),
159-
tcp.WithTLSCAFile(caFile),
160-
tcp.WithTLSDomain("localhost"),
158+
tcp.WithTLS(
159+
tcp.WithTLSDomain("localhost"),
160+
tcp.WithCAFile(caFile),
161+
),
161162
),
162163
)
163164
require.NoError(t, err, "Failed to create TLS client")
@@ -183,7 +184,6 @@ func TestTCPTLSConnection_WithoutTLS_Failure(t *testing.T) {
183184
cli, err := client.NewIggyClient(
184185
client.WithTcp(
185186
tcp.WithServerAddress(connectAddr),
186-
tcp.WithTLS(false),
187187
),
188188
)
189189

@@ -211,9 +211,10 @@ func TestTCPTLSConnection_MessageFlow_Success(t *testing.T) {
211211
cli, err := client.NewIggyClient(
212212
client.WithTcp(
213213
tcp.WithServerAddress(connectAddr),
214-
tcp.WithTLS(true),
215-
tcp.WithTLSCAFile(caFile),
216-
tcp.WithTLSDomain("localhost"),
214+
tcp.WithTLS(
215+
tcp.WithTLSDomain("localhost"),
216+
tcp.WithCAFile(caFile),
217+
),
217218
),
218219
)
219220
require.NoError(t, err, "Failed to create TLS client")

0 commit comments

Comments
 (0)