Skip to content

Commit

Permalink
Merge pull request #2061 from yann-soubeyrand/support-sslrootcert-system
Browse files Browse the repository at this point in the history
Add support for sslrootcert=system
  • Loading branch information
jackc authored Jun 29, 2024
2 parents 9907b87 + c407c42 commit 6b9ff97
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions pgconn/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,36 @@ func configTLS(settings map[string]string, thisHost string, parseConfigOptions P

tlsConfig := &tls.Config{}

if sslrootcert != "" {
var caCertPool *x509.CertPool

if sslrootcert == "system" {
var err error

caCertPool, err = x509.SystemCertPool()
if err != nil {
return nil, fmt.Errorf("unable to load system certificate pool: %w", err)
}

sslmode = "verify-full"
} else {
caCertPool = x509.NewCertPool()

caPath := sslrootcert
caCert, err := os.ReadFile(caPath)
if err != nil {
return nil, fmt.Errorf("unable to read CA file: %w", err)
}

if !caCertPool.AppendCertsFromPEM(caCert) {
return nil, errors.New("unable to add CA to cert pool")
}
}

tlsConfig.RootCAs = caCertPool
tlsConfig.ClientCAs = caCertPool
}

switch sslmode {
case "disable":
return []*tls.Config{nil}, nil
Expand Down Expand Up @@ -711,23 +741,6 @@ func configTLS(settings map[string]string, thisHost string, parseConfigOptions P
return nil, errors.New("sslmode is invalid")
}

if sslrootcert != "" {
caCertPool := x509.NewCertPool()

caPath := sslrootcert
caCert, err := os.ReadFile(caPath)
if err != nil {
return nil, fmt.Errorf("unable to read CA file: %w", err)
}

if !caCertPool.AppendCertsFromPEM(caCert) {
return nil, errors.New("unable to add CA to cert pool")
}

tlsConfig.RootCAs = caCertPool
tlsConfig.ClientCAs = caCertPool
}

if (sslcert != "" && sslkey == "") || (sslcert == "" && sslkey != "") {
return nil, errors.New(`both "sslcert" and "sslkey" are required`)
}
Expand Down

0 comments on commit 6b9ff97

Please sign in to comment.