Skip to content

Commit

Permalink
Merge pull request #59 from xataio/update-tls-lib
Browse files Browse the repository at this point in the history
Support PEM certificates as well as files
  • Loading branch information
eminano authored Aug 5, 2024
2 parents 8b098ab + 6500486 commit 7418324
Show file tree
Hide file tree
Showing 5 changed files with 360 additions and 27 deletions.
27 changes: 27 additions & 0 deletions pkg/tls/test/test.csr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN CERTIFICATE REQUEST-----
MIIEjTCCAnUCAQAwSDELMAkGA1UEBhMCU1AxEzARBgNVBAgMClNvbWUtU3RhdGUx
FTATBgNVBAoMDFBnc3RyZWFtIEx0ZDENMAsGA1UECwwEWGF0YTCCAiIwDQYJKoZI
hvcNAQEBBQADggIPADCCAgoCggIBAMZG8/obpyvJ+WkGkdO/hbExSN1nWR206/Dh
pSYzcZyI1Jj0R4Af0gD/EFVM+4KTDr20nOofmfBWOYHV+KwiKtWQQ+oT0+xVcTT6
IC5I5K9+AXERuTu/NbnjkxuC/1u7K511RrUK0lxUra2/B8mGTc9nu2g415GVk2hU
rJjWEX09hVH7xBSmnzYN+IfepsftxgnR5m2YzqOSnsphBBfyyOsL+3Jo5Uv4yY22
bnJCDxx/TPG37EcGMb4Q/aCWk5mXm4Io5mBfcl7SNxy867JpBO2CCP6fFaWRUGmF
/O+YBD5z0cSb1wZrMBRgezggpa2gacYVtsWrQYzAxMtCf3MwM89z8i+W9ME3cMhg
T7b6T0XCQ5gkqmLxDCsg9ocNV0W0wb5EEPlt4/TeqMmLbpLkIa6rnR2C6gg+wFSQ
Vk8c/aBtm9BjKFLWWUwDUPPzp1RdIVAfuobDK37dVqr+1m6oTDl+BoDuM5PsgTS4
bFW2ZJVZB+d4IFmGhqTtKKmYa7QbwhzF8i5ShV+419KOt6hRkJ/jREdea7ZS/uyK
wBAvPHZVkUk8tfTpcuKTFVKsgXXV8uwwnI2W6safYbTXaN+7gfA88wntaigjeij/
LrO+itAiv9GTGkpMlXyuAX/d5+4j0EHcV64NYL61GvMcbJ5G0SeQrXuBlgMTYaNr
O7miEujnAgMBAAGgADANBgkqhkiG9w0BAQsFAAOCAgEAGYFGSgMARWuH5VXK0/Fl
nNg+/rzdff1NYkY9QrvFxCQeVJ9rD1ml7VLZXDtXhMNEGJyYbkouc1Ehx0BsihT6
YztcnQ6TzWAzvr3Ns9X3riADzXxdDHV5xs+8VPV8RvT3XNcrlw2NQmzJ4Juc8PkT
4ZfguZBywAmFTw1oX8JqlQSp5pYtP7popsvGPS6ieUm0Kmv8kK3sRDs+JSc7iXtB
/HymqeSylFNHgFZsdbYmu32v2qbcqimAitB/v5tGNhuiMXx6vEeQnB69V+AV70Rl
9dnvAo7ihTRMzecUVsoDFtc8OWSPdTm6t8vDI2JqmeDN25Xhyf89cKwoY97NhA99
ds5WHs6TzsHohPZAsaxtZnjwxMEne7Y4FLFmTHVk5o0POTZcOC/sMB1iBDsd/YJe
AYsoiqLYtu6x6Avfe6LXWYWYa/R4/UXh8H6WiChsFXzOIilp3apjaeHM3z7iKx2S
VtGyVTrrcbzRiF0ShKVnbDXnvcoNZxPiXfh6Zz4SkBbV01T3hluBwzp4mjcWpiv3
AOAWChMnbmkg/T+OME6e1JVHDR5tAC/7vF2QkZYpiH2RVnZmCTDWBcRGpMkhkRgF
eycowzKBkgIOcJ99p0sGEqQ3W0J1M4bzuumncLID08EG/dEp1eIdunahcHHyhnnv
BcGFr2/OxuaVmxcy5/QQjAg=
-----END CERTIFICATE REQUEST-----
52 changes: 52 additions & 0 deletions pkg/tls/test/test.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-----BEGIN PRIVATE KEY-----
MIIJQQIBADANBgkqhkiG9w0BAQEFAASCCSswggknAgEAAoICAQDGRvP6G6cryflp
BpHTv4WxMUjdZ1kdtOvw4aUmM3GciNSY9EeAH9IA/xBVTPuCkw69tJzqH5nwVjmB
1fisIirVkEPqE9PsVXE0+iAuSOSvfgFxEbk7vzW545Mbgv9buyuddUa1CtJcVK2t
vwfJhk3PZ7to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osRWe7kw8UZlDIommUnX+tm1FGTWjyGuOLr99lVN7H1ohq5nzEuzDqMGmwQo
SAZNcR2xlZMRCPUYnYXg8AOalWTOa8v0g4KSyEMDdYlszNM54zKDpNNgfdebrtI4
L0o1ZDhpwKJ6/BRe7rf2SkoSyyN6MxpC+8TI2qsCggEAbN/X1VoHpyNso13cBhNw
E3Ng7CUGKEbeMDkGY0VEkfr/BWZMbWhSzQy4NcHrSlufJYKlUDCp3XRyUqPV7+BB
0GYSc13OaNC4TdyNYgreXnvmpl/rMczQbrGMqbFPSEIAD72kmx2toy5/9+OeMDdS
Jt9DYVRMHbPTg1TJAdD/TNhmquiVtnY7e24yzArcHw36YwCIVWAYGohNTIPPd8xl
Ottvq31cv0YgnG9C7qEX/eLuOpKEwXfhQecWmmGvKgn+i/FOOm83uvbYqS3TgP8W
NurAu5CYSpuVWddY7IaXfn9lI6/6c/2Olugcq3jij9Ye4N3Q+PjClnyxMmfu3gc3
uQKCAQAmpjnO3OukXxgEtDusQKh/IxsvKQMfSQNh68cQ6iroGDAguYBusSTYU1gA
IKvR6Uqqv52yPe6u5pYC0fA8L+2S3nuFA4f4a1JHdpf5X5HwBtTERG4Qxh7p1gss
3AZpLfYa442d8UuCCYoimBXqXXF0TLsfoRjcgrKd9yNO6Pa79jzUR+ixQmkyE7XA
XGHx7Qsl6E0E1DgK4MHPOALg/tJiLNJQgIKDtiBn4GTR8tSHRDY64rEuOClq14y1
cpXNj7lcV1xz1vLNRcksS0/QA7Hzol+Dt8xVOBK1smhCREMetUMXHwBYLnNemxNX
/WF38bnN3/Dej6Ns6FRE8gNSRmeT
-----END PRIVATE KEY-----
31 changes: 31 additions & 0 deletions pkg/tls/test/test.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-----BEGIN CERTIFICATE-----
MIIFPzCCAyegAwIBAgIULV6zejFwt/Tri8WKZFBxj15uvaIwDQYJKoZIhvcNAQEL
BQAwSDELMAkGA1UEBhMCU1AxEzARBgNVBAgMClNv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o55Tj9VCBhViGa7CbDtaW7ZYr/fXydZVcthXYZzZ7QEVyYaguWlzXLjy/qF8kgk
cDwinFa8hiJnP+BJUGnzq3LYQJ2labI4YUscc6p4inh9y8JZ3n33VqX2YjqCdHMA
j8nw5xpThdQ/a8z3Z8ugFCLO09Hts1eKFhs5PwaQvjkoX+dSE2FeX51OMlLOPDsu
C6ScDU7FG0J5JE36nRqp2XwSdGAfc5pHKmsuomxnoE/d/hL7O6zouo/jvQyCNFtn
5/pzhkhhOjUTP2gIW5ueNn8oQF9F32GWRNJGQVTBiK17dWvHxiSvIzgKqUyrD8lI
VefVEQgRbfHD3nSk6G30gAeWzt8T10lI8MtQWTtoFJGFBaSVr/lSyHo4QS4SyTmK
uvnFGJVivRtaAP4d+u/6/1Mvy5sNsSiWRRKfKTB/FEerbe7blhnqJhrBp98nKBv/
IJtmewD7lVGGDY8sWnxnpNyqLVhvRilO9d+4oQWqKgN8m1PXAI2jDYA0RZ+Qs/ko
5FB88mRi8hNOhmADXguKlnCid/X0StK6wpphvFIaNnGLFzjeZXc65BoV1c2+Boxe
cNwpkrW5tKaf/Ox1ntHnnQBpUhM4AxGoczfIj0dEnYio54gagsAMK/Pjq2KiX7Bi
RUm4
-----END CERTIFICATE-----
69 changes: 42 additions & 27 deletions pkg/tls/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,36 @@ 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.
// File path to the CA PEM certificate or 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
CaCertPEM string
// File path to the client PEM certificate or client PEM certificate
ClientCertFile string
// File path to the client PEM key
ClientCertPEM string
// File path to the client PEM key or client PEM key content
ClientKeyFile string
ClientKeyPEM string
}

func NewConfig(cfg *Config) (*tls.Config, error) {
if !cfg.Enabled {
return nil, nil
}

certPool, err := getCertPool(cfg.CaCertFile)
certPool, err := getCertPool(cfg)
if err != nil {
return nil, err
}

certificates, err := getCertificates(cfg.ClientCertFile, cfg.ClientKeyFile)
certificates, err := getCertificates(cfg)
if err != nil {
return nil, err
}
Expand All @@ -46,23 +48,32 @@ func NewConfig(cfg *Config) (*tls.Config, error) {
}, 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
func getCertPool(cfg *Config) (*x509.CertPool, error) {
pemCertBytes, err := readPEMBytes(cfg.CaCertFile, cfg.CaCertPEM)
if err != nil {
return nil, fmt.Errorf("reading CA certificate file: %w", err)
}

if len(pemCertBytes) == 0 {
return x509.SystemCertPool()
}

return x509.SystemCertPool()
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(pemCertBytes)
return certPool, nil
}

func getCertificates(clientCertFile, clientKeyFile string) ([]tls.Certificate, error) {
if clientCertFile != "" && clientKeyFile != "" {
cert, err := tls.LoadX509KeyPair(clientCertFile, clientKeyFile)
func getCertificates(cfg *Config) ([]tls.Certificate, error) {
if cfg.IsClientCertProvided() {
pemCertBytes, err := readPEMBytes(cfg.ClientCertFile, cfg.ClientCertPEM)
if err != nil {
return nil, err
}
pemKeyBytes, err := readPEMBytes(cfg.ClientKeyFile, cfg.ClientKeyPEM)
if err != nil {
return nil, err
}
cert, err := tls.X509KeyPair(pemCertBytes, pemKeyBytes)
if err != nil {
return nil, err
}
Expand All @@ -72,12 +83,16 @@ func getCertificates(clientCertFile, clientKeyFile string) ([]tls.Certificate, e
return []tls.Certificate{}, nil
}

func readFile(path string) ([]byte, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
// readPEMBytes will parse the certificate on input and return the pem byte
// content. It accepts a pem certificate or the file path to a pem certificate.
func readPEMBytes(certFile, certPEM string) ([]byte, error) {
if certFile != "" {
return os.ReadFile(certFile)
}
defer file.Close()

return io.ReadAll(file)
return []byte(certPEM), nil
}

func (c *Config) IsClientCertProvided() bool {
return (c.ClientCertFile != "" || c.ClientCertPEM != "") && (c.ClientKeyFile != "" || c.ClientKeyPEM != "")
}
Loading

0 comments on commit 7418324

Please sign in to comment.