-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: Redis server TLS support #104
Merged
+142
−1
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c00a4ed
Implemented verification of redis server certificates.
sicet7 107f23b
Removed "min_version" from configuration as it should always be 1.2
sicet7 8df4fd1
Added warning when empty cert pool is defaulted too.
sicet7 2086670
Fixed warning always being produced even without tls enabled.
sicet7 f656464
Added better error message when PEM parsing fails.
sicet7 4b64f14
Moved implementation to "tlsconfig.go" also implemented suggestions.
sicet7 8abbcc6
chore: some refactoring
rustatian c63b869
Fixed to set "GetClientCertificate" to nil when cert or key not provi…
sicet7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package kv | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"os" | ||
|
||
"golang.org/x/sys/cpu" | ||
) | ||
|
||
type TLSConfig struct { | ||
Cert string `mapstructure:"cert"` | ||
Key string `mapstructure:"key"` | ||
RootCa string `mapstructure:"root_ca"` | ||
} | ||
|
||
func tlsConfig(conf *TLSConfig) (*tls.Config, error) { | ||
if conf == nil { | ||
return nil, nil | ||
} | ||
|
||
tlsConfig := defaultTLSConfig(conf) | ||
if conf.RootCa != "" { | ||
// error is always nil here | ||
certPool, err := x509.SystemCertPool() | ||
if err != nil { | ||
// error is always nil here | ||
return nil, err | ||
} | ||
|
||
if certPool == nil { | ||
certPool = x509.NewCertPool() | ||
} | ||
|
||
// we already checked this file in the config.go | ||
rca, err := os.ReadFile(conf.RootCa) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if ok := certPool.AppendCertsFromPEM(rca); !ok { | ||
return nil, err | ||
} | ||
|
||
tlsConfig.RootCAs = certPool | ||
} | ||
|
||
if _, crtExistErr := os.Stat(conf.RootCa); crtExistErr != nil { | ||
return nil, crtExistErr | ||
} | ||
|
||
return tlsConfig, nil | ||
} | ||
Comment on lines
+17
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix critical issues in TLS configuration logic Several critical issues need to be addressed:
func tlsConfig(conf *TLSConfig) (*tls.Config, error) {
if conf == nil {
return nil, nil
}
+ // Check certificate existence first
+ if conf.CAFile != "" {
+ if _, err := os.Stat(conf.CAFile); err != nil {
+ return nil, fmt.Errorf("ca_file error: %w", err)
+ }
+ }
+
tlsConfig := defaultTLSConfig(conf)
- if conf.RootCa != "" {
- // error is always nil here
+
+ // Configure TLS version
+ if conf.MinVersion != "" {
+ version, err := parseTLSVersion(conf.MinVersion)
+ if err != nil {
+ return nil, err
+ }
+ tlsConfig.MinVersion = version
+ }
+
+ if conf.CAFile != "" {
certPool, err := x509.SystemCertPool()
if err != nil {
- // error is always nil here
return nil, err
}
@@ -31,12 +31,11 @@
certPool = x509.NewCertPool()
}
- // we already checked this file in the config.go
- rca, err := os.ReadFile(conf.RootCa)
+ rca, err := os.ReadFile(conf.CAFile)
if err != nil {
return nil, err
}
if ok := certPool.AppendCertsFromPEM(rca); !ok {
- return nil, err
+ return nil, fmt.Errorf("failed to append CA certificate")
}
tlsConfig.RootCAs = certPool
}
- if _, crtExistErr := os.Stat(conf.RootCa); crtExistErr != nil {
- return nil, crtExistErr
- }
-
return tlsConfig, nil
}
|
||
|
||
func defaultTLSConfig(cfg *TLSConfig) *tls.Config { | ||
var topCipherSuites []uint16 | ||
var defaultCipherSuitesTLS13 []uint16 | ||
|
||
hasGCMAsmAMD64 := cpu.X86.HasAES && cpu.X86.HasPCLMULQDQ | ||
hasGCMAsmARM64 := cpu.ARM64.HasAES && cpu.ARM64.HasPMULL | ||
// Keep in sync with crypto/aes/cipher_s390x.go. | ||
hasGCMAsmS390X := cpu.S390X.HasAES && cpu.S390X.HasAESCBC && cpu.S390X.HasAESCTR && (cpu.S390X.HasGHASH || cpu.S390X.HasAESGCM) | ||
|
||
hasGCMAsm := hasGCMAsmAMD64 || hasGCMAsmARM64 || hasGCMAsmS390X | ||
|
||
if hasGCMAsm { | ||
// If AES-GCM hardware is provided then priorities AES-GCM | ||
// cipher suites. | ||
topCipherSuites = []uint16{ | ||
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, | ||
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, | ||
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | ||
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | ||
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, | ||
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, | ||
} | ||
defaultCipherSuitesTLS13 = []uint16{ | ||
tls.TLS_AES_128_GCM_SHA256, | ||
tls.TLS_CHACHA20_POLY1305_SHA256, | ||
tls.TLS_AES_256_GCM_SHA384, | ||
} | ||
} else { | ||
// Without AES-GCM hardware, we put the ChaCha20-Poly1305 | ||
// cipher suites first. | ||
topCipherSuites = []uint16{ | ||
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, | ||
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, | ||
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, | ||
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, | ||
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | ||
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | ||
} | ||
defaultCipherSuitesTLS13 = []uint16{ | ||
tls.TLS_CHACHA20_POLY1305_SHA256, | ||
tls.TLS_AES_128_GCM_SHA256, | ||
tls.TLS_AES_256_GCM_SHA384, | ||
} | ||
} | ||
|
||
defaultCipherSuites := make([]uint16, 0, 22) | ||
defaultCipherSuites = append(defaultCipherSuites, topCipherSuites...) | ||
defaultCipherSuites = append(defaultCipherSuites, defaultCipherSuitesTLS13...) | ||
|
||
return &tls.Config{ | ||
CurvePreferences: []tls.CurveID{ | ||
tls.X25519, | ||
tls.CurveP256, | ||
tls.CurveP384, | ||
tls.CurveP521, | ||
}, | ||
GetClientCertificate: getClientCertificate(cfg), | ||
CipherSuites: defaultCipherSuites, | ||
MinVersion: tls.VersionTLS12, | ||
} | ||
} | ||
rustatian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// getClientCertificate is used for tls.Config struct field GetClientCertificate and enables re-fetching the client certificates when they expire | ||
func getClientCertificate(cfg *TLSConfig) func(_ *tls.CertificateRequestInfo) (*tls.Certificate, error) { | ||
if cfg == nil || cfg.Cert == "" || cfg.Key == "" { | ||
return nil | ||
} | ||
return func(_ *tls.CertificateRequestInfo) (*tls.Certificate, error) { | ||
cert, err := tls.LoadX509KeyPair(cfg.Cert, cfg.Key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &cert, nil | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Align struct with PR objectives and add documentation
The
TLSConfig
struct needs to be updated to match the PR objectives and documentation requirements:min_version
fieldcert
andkey
fields📝 Committable suggestion