-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved implementation to "tlsconfig.go" also implemented suggestions.
Suggestions implemented: #104 (comment) #104 (comment) #104 (comment) #104 (comment) Signed-off-by: Martin René Sørensen <[email protected]>
- Loading branch information
Showing
3 changed files
with
51 additions
and
40 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package kv | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"github.com/roadrunner-server/errors" | ||
"go.uber.org/zap" | ||
"os" | ||
) | ||
|
||
type TLSConfig struct { | ||
RootCa string `mapstructure:"root_ca"` | ||
} | ||
|
||
func NewTLSConfig(c *TLSConfig, log *zap.Logger) (*tls.Config, error) { | ||
if c == nil || c.RootCa == "" { | ||
return nil, nil | ||
} | ||
tlsConfig := &tls.Config{ | ||
MinVersion: tls.VersionTLS12, | ||
} | ||
rootCAs, sysCertErr := x509.SystemCertPool() | ||
if sysCertErr != nil { | ||
rootCAs = x509.NewCertPool() | ||
log.Warn("unable to load system certificate pool, using empty pool", zap.Error(sysCertErr)) | ||
} | ||
|
||
if _, crtExistErr := os.Stat(c.RootCa); crtExistErr != nil { | ||
return nil, crtExistErr | ||
} | ||
|
||
bytes, crtReadErr := os.ReadFile(c.RootCa) | ||
if crtReadErr != nil { | ||
return nil, crtReadErr | ||
} | ||
|
||
if !rootCAs.AppendCertsFromPEM(bytes) { | ||
return nil, errors.Errorf("failed to parse certificates from PEM file '%s'. Please ensure the file contains valid PEM-encoded certificates", c.RootCa) | ||
} | ||
tlsConfig.RootCAs = rootCAs | ||
return tlsConfig, nil | ||
} |