-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from xataio/add-config-tls-support
Add config tls support
- Loading branch information
Showing
5 changed files
with
113 additions
and
75 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
This file was deleted.
Oops, something went wrong.
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,83 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package tls | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
type Config struct { | ||
// Enabled determines if TLS should be used. Defaults to false. | ||
Enabled bool | ||
// File path to the CA PEM certificate to be used for TLS connection. If TLS is | ||
// enabled and no CA cert file is provided, the system certificate pool is | ||
// used as default. | ||
CaCertFile string | ||
// File path to the client PEM certificate | ||
ClientCertFile string | ||
// File path to the client PEM key | ||
ClientKeyFile string | ||
} | ||
|
||
func NewConfig(cfg *Config) (*tls.Config, error) { | ||
if !cfg.Enabled { | ||
return nil, nil | ||
} | ||
|
||
certPool, err := getCertPool(cfg.CaCertFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
certificates, err := getCertificates(cfg.ClientCertFile, cfg.ClientKeyFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &tls.Config{ | ||
MinVersion: tls.VersionTLS12, | ||
MaxVersion: 0, | ||
Certificates: certificates, | ||
RootCAs: certPool, | ||
}, nil | ||
} | ||
|
||
func getCertPool(caCertFile string) (*x509.CertPool, error) { | ||
if caCertFile != "" { | ||
pemCertBytes, err := readFile(caCertFile) | ||
if err != nil { | ||
return nil, fmt.Errorf("reading CA certificate file: %w", err) | ||
} | ||
certPool := x509.NewCertPool() | ||
certPool.AppendCertsFromPEM(pemCertBytes) | ||
return certPool, nil | ||
} | ||
|
||
return x509.SystemCertPool() | ||
} | ||
|
||
func getCertificates(clientCertFile, clientKeyFile string) ([]tls.Certificate, error) { | ||
if clientCertFile != "" && clientKeyFile != "" { | ||
cert, err := tls.LoadX509KeyPair(clientCertFile, clientKeyFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return []tls.Certificate{cert}, nil | ||
} | ||
|
||
return []tls.Certificate{}, nil | ||
} | ||
|
||
func readFile(path string) ([]byte, error) { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
|
||
return io.ReadAll(file) | ||
} |