Add support of postgresql TLS configuration

Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
gearnode
2025-03-08 21:11:27 +01:00
parent 4d1305f192
commit 93801e1d11
3 changed files with 64 additions and 15 deletions

View File

@@ -15,16 +15,20 @@
package probod
import (
"crypto/x509"
"encoding/pem"
"go.gearno.de/kit/pg"
)
type (
pgConfig struct {
Addr string `json:"addr"`
Username string `json:"username"`
Password string `json:"password"`
Database string `json:"database"`
PoolSize int32 `json:"pool-size"`
Addr string `json:"addr"`
Username string `json:"username"`
Password string `json:"password"`
Database string `json:"database"`
PoolSize int32 `json:"pool-size"`
CACertBundle string `json:"ca-cert-bundle,omitempty"`
}
)
@@ -37,6 +41,31 @@ func (cfg pgConfig) Options(options ...pg.Option) []pg.Option {
pg.WithPoolSize(cfg.PoolSize),
}
if cfg.CACertBundle != "" {
var certs []*x509.Certificate
pemData := []byte(cfg.CACertBundle)
for len(pemData) > 0 {
var block *pem.Block
block, pemData = pem.Decode(pemData)
if block == nil {
break
}
if block.Type != "CERTIFICATE" {
continue
}
cert, err := x509.ParseCertificate(block.Bytes)
if err == nil {
certs = append(certs, cert)
}
}
if len(certs) > 0 {
opts = append(opts, pg.WithTLS(certs))
}
}
opts = append(opts, options...)
return opts