From e43765098b995bc9e4b5e0a21dc459df09b9506b Mon Sep 17 00:00:00 2001 From: Satoshi Konno Date: Mon, 23 Dec 2024 20:41:45 +0900 Subject: [PATCH] Update Config interface # Please enter the commit message for your changes. Lines starting --- redis/config_impl.go | 50 +++++++++++++++++------------------ redis/server_impl.go | 8 +++--- redistest/certs/certs_test.go | 39 --------------------------- 3 files changed, 29 insertions(+), 68 deletions(-) delete mode 100644 redistest/certs/certs_test.go diff --git a/redis/config_impl.go b/redis/config_impl.go index 4b87d45..319d868 100644 --- a/redis/config_impl.go +++ b/redis/config_impl.go @@ -29,8 +29,8 @@ const ( tlsCACertFile = "tls-ca-cert-file" ) -// ServerConfig is a configuration for the Redis server. -type ServerConfig struct { +// serverConfig is a configuration for the Redis server. +type serverConfig struct { *configMap ServerCert []byte ServerKey []byte @@ -38,9 +38,9 @@ type ServerConfig struct { TLSConfig *tls.Config } -// NewDefaultServerConfig returns a default server configuration. -func NewDefaultServerConfig() *ServerConfig { - return &ServerConfig{ +// newDefaultServerConfig returns a default server configuration. +func newDefaultServerConfig() *serverConfig { + return &serverConfig{ configMap: newConfig(), ServerCert: nil, ServerKey: nil, @@ -50,12 +50,12 @@ func NewDefaultServerConfig() *ServerConfig { } // SetPort sets a listen port number. -func (cfg *ServerConfig) SetPort(port int) { +func (cfg *serverConfig) SetPort(port int) { cfg.SetConfig(portConfig, strconv.Itoa(port)) } // ConfigPort returns a listen port number. -func (cfg *ServerConfig) ConfigPort() int { +func (cfg *serverConfig) ConfigPort() int { port, ok := cfg.ConfigInteger(portConfig) if !ok { return DefaultTLSPort @@ -64,7 +64,7 @@ func (cfg *ServerConfig) ConfigPort() int { } // IsPortEnabled returns true if a listen port is enabled. -func (cfg *ServerConfig) IsPortEnabled() bool { +func (cfg *serverConfig) IsPortEnabled() bool { port, ok := cfg.ConfigInteger(portConfig) if !ok { return false @@ -73,12 +73,12 @@ func (cfg *ServerConfig) IsPortEnabled() bool { } // SetTLSPort sets a listen port number for TLS. -func (cfg *ServerConfig) SetTLSPort(port int) { +func (cfg *serverConfig) SetTLSPort(port int) { cfg.SetConfig(tlsPortConfig, strconv.Itoa(port)) } // ConfigTLSPort returns a listen port number for TLS. -func (cfg *ServerConfig) ConfigTLSPort() int { +func (cfg *serverConfig) ConfigTLSPort() int { port, ok := cfg.ConfigInteger(tlsPortConfig) if !ok { return DefaultTLSPort @@ -87,7 +87,7 @@ func (cfg *ServerConfig) ConfigTLSPort() int { } // IsTLSPortEnabled returns true if a listen port for TLS is enabled. -func (cfg *ServerConfig) IsTLSPortEnabled() bool { +func (cfg *serverConfig) IsTLSPortEnabled() bool { port, ok := cfg.ConfigInteger(tlsPortConfig) if !ok { return false @@ -96,7 +96,7 @@ func (cfg *ServerConfig) IsTLSPortEnabled() bool { } // SetTLSCertFile sets a certificate file. -func (cfg *ServerConfig) SetTLSCertFile(certFile string) error { +func (cfg *serverConfig) SetTLSCertFile(certFile string) error { cert, err := os.ReadFile(certFile) if err != nil { return err @@ -107,12 +107,12 @@ func (cfg *ServerConfig) SetTLSCertFile(certFile string) error { } // ConfigTLSCertFile returns a certificate file. -func (cfg *ServerConfig) ConfigTLSCertFile() (string, bool) { +func (cfg *serverConfig) ConfigTLSCertFile() (string, bool) { return cfg.ConfigString(tlsCertFile) } // ConfigTLSCert returns a certificate. -func (cfg *ServerConfig) ConfigTLSCert() ([]byte, bool) { +func (cfg *serverConfig) ConfigTLSCert() ([]byte, bool) { if cfg.ServerCert == nil { return nil, false } @@ -120,7 +120,7 @@ func (cfg *ServerConfig) ConfigTLSCert() ([]byte, bool) { } // SetTLSKeyFile sets a key file. -func (cfg *ServerConfig) SetTLSKeyFile(keyFile string) error { +func (cfg *serverConfig) SetTLSKeyFile(keyFile string) error { key, err := os.ReadFile(keyFile) if err != nil { return err @@ -131,12 +131,12 @@ func (cfg *ServerConfig) SetTLSKeyFile(keyFile string) error { } // ConfigTLSKeyFile returns a key file. -func (cfg *ServerConfig) ConfigTLSKeyFile() (string, bool) { +func (cfg *serverConfig) ConfigTLSKeyFile() (string, bool) { return cfg.ConfigString(tlsKeyFile) } // ConfigTLSKey returns a key. -func (cfg *ServerConfig) ConfigTLSKey() ([]byte, bool) { +func (cfg *serverConfig) ConfigTLSKey() ([]byte, bool) { if cfg.ServerKey == nil { return nil, false } @@ -144,7 +144,7 @@ func (cfg *ServerConfig) ConfigTLSKey() ([]byte, bool) { } // SetTLSCaCertFile sets a CA certificate file. -func (cfg *ServerConfig) SetTLSCaCertFile(caCertFile string) error { +func (cfg *serverConfig) SetTLSCaCertFile(caCertFile string) error { rootCert, err := os.ReadFile(caCertFile) if err != nil { return err @@ -155,12 +155,12 @@ func (cfg *ServerConfig) SetTLSCaCertFile(caCertFile string) error { } // ConfigTLSCACertFile returns a CA certificate file. -func (cfg *ServerConfig) ConfigTLSCACertFile() (string, bool) { +func (cfg *serverConfig) ConfigTLSCACertFile() (string, bool) { return cfg.ConfigString(tlsCACertFile) } // ConfigTLSCACert returns a CA certificate. -func (cfg *ServerConfig) ConfigTLSCACert() ([]byte, bool) { +func (cfg *serverConfig) ConfigTLSCACert() ([]byte, bool) { if cfg.CACerts == nil { return nil, false } @@ -168,12 +168,12 @@ func (cfg *ServerConfig) ConfigTLSCACert() ([]byte, bool) { } // SetTLSConfig sets a TLS configuration. -func (cfg *ServerConfig) SetTLSConfig(tlsConfig *tls.Config) { +func (cfg *serverConfig) SetTLSConfig(tlsConfig *tls.Config) { cfg.TLSConfig = tlsConfig } // ConfigTLSConfig returns a TLS configuration. -func (cfg *ServerConfig) ConfigTLSConfig() (*tls.Config, bool) { +func (cfg *serverConfig) ConfigTLSConfig() (*tls.Config, bool) { if cfg.TLSConfig == nil { return nil, false } @@ -181,16 +181,16 @@ func (cfg *ServerConfig) ConfigTLSConfig() (*tls.Config, bool) { } // SetRequirePass sets a password. -func (cfg *ServerConfig) SetRequirePass(password string) { +func (cfg *serverConfig) SetRequirePass(password string) { cfg.SetConfig(requirePass, password) } // ConfigRequirePass returns a password. -func (cfg *ServerConfig) ConfigRequirePass() (string, bool) { +func (cfg *serverConfig) ConfigRequirePass() (string, bool) { return cfg.ConfigString(requirePass) } // RemoveRequirePass removes a password. -func (cfg *ServerConfig) RemoveRequirePass() { +func (cfg *serverConfig) RemoveRequirePass() { cfg.RemoveConfig(requirePass) } diff --git a/redis/server_impl.go b/redis/server_impl.go index 69e8ef9..5fcd584 100644 --- a/redis/server_impl.go +++ b/redis/server_impl.go @@ -28,7 +28,7 @@ import ( ) type server struct { - *ServerConfig + *serverConfig *auth.AuthManager *ConnManager tracer.Tracer @@ -45,7 +45,7 @@ type server struct { // NewServer returns a new server instance. func NewServer() Server { server := &server{ - ServerConfig: NewDefaultServerConfig(), + serverConfig: newDefaultServerConfig(), AuthManager: auth.NewAuthManager(), ConnManager: NewConnManager(), Tracer: tracer.NullTracer, @@ -73,7 +73,7 @@ func (server *server) SetTracer(t tracer.Tracer) { // Config returns the server configuration. func (server *server) Config() Config { - return server.ServerConfig + return server.serverConfig } // SetAuthCommandHandler sets a auth handler to handle auth commands. @@ -170,7 +170,7 @@ func (server *server) open() error { if ok { server.tlsConfig = tlsConfig } else { - tlsConfig, err := NewTLSConfigFrom(server.ServerConfig) + tlsConfig, err := NewTLSConfigFrom(server.serverConfig) if err != nil { return err } diff --git a/redistest/certs/certs_test.go b/redistest/certs/certs_test.go deleted file mode 100644 index 4b22cc9..0000000 --- a/redistest/certs/certs_test.go +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (C) 2019 The go-redis Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package certs - -import ( - "testing" - - "github.com/cybergarage/go-redis/redis" -) - -const ( - certFile = "./cert.pem" - keyFile = "./key.pem" - caCertFile = "./root_cert.pem" -) - -func TestCerts(t *testing.T) { - conf := redis.NewDefaultServerConfig() - conf.SetTLSCertFile(certFile) - conf.SetTLSKeyFile(keyFile) - conf.SetTLSCaCertFile(caCertFile) - - _, err := redis.NewTLSConfigFrom(conf) - if err != nil { - t.Error(err) - } -}