Skip to content

Commit

Permalink
adding tls for pgbouncer
Browse files Browse the repository at this point in the history
Signed-off-by: Hiranmoy Das Chowdhury <[email protected]>
  • Loading branch information
HiranmoyChowdhury committed Nov 7, 2024
1 parent 7e70d36 commit 9bdc4c6
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions pgbouncer/kubedb_client_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package pgbouncer
import (
"context"
"fmt"

"kmodules.xyz/client-go/tools/certholder"
"kubedb.dev/apimachinery/apis/kubedb"
dbapi "kubedb.dev/apimachinery/apis/kubedb/v1"

Expand All @@ -34,7 +34,6 @@ import (

const (
DefaultBackendDBType = "postgres"
TLSModeDisable = "disable"
)

type Auth struct {
Expand Down Expand Up @@ -171,6 +170,25 @@ func (o *KubeDBClientBuilder) getBackendAuth() (string, string, error) {
return string(user), string(pass), nil
}

func (o *KubeDBClientBuilder) getTLSConfig(ctx context.Context) (*certholder.Paths, error) {
secretName := o.pgbouncer.GetCertSecretName(dbapi.PgBouncerClientCert)

var certSecret core.Secret
err := o.kc.Get(ctx, client.ObjectKey{Namespace: o.pgbouncer.Namespace, Name: secretName}, &certSecret)
if err != nil {
klog.Error(err, "failed to get certificate secret.", secretName)
return nil, err
}

certs, _ := certholder.DefaultHolder.ForResource(dbapi.SchemeGroupVersion.WithResource(dbapi.ResourcePluralPgBouncer), o.pgbouncer.ObjectMeta)
paths, err := certs.Save(&certSecret)
if err != nil {
klog.Error(err, "failed to save certificate")
return nil, err
}
return paths, nil
}

func (o *KubeDBClientBuilder) getConnectionString() (string, error) {
user, pass, err := o.getBackendAuth()
if err != nil {
Expand All @@ -185,8 +203,25 @@ func (o *KubeDBClientBuilder) getConnectionString() (string, error) {
if o.pbContainerPort != nil {
listeningPort = int(*o.pbContainerPort)
}
// TODO ssl mode is disable now need to work on this after adding tls support
connector := fmt.Sprintf("user=%s password=%s host=%s port=%d connect_timeout=10 dbname=%s sslmode=%s", user, pass, o.url, listeningPort, o.backendDBName, TLSModeDisable)

sslMode := o.pgbouncer.Spec.SSLMode
if sslMode == "" {
sslMode = dbapi.PgBouncerSSLModeDisable
}
connector := ""
if o.pgbouncer.Spec.TLS != nil {
paths, err := o.getTLSConfig(o.ctx)
if err != nil {
return "", err
}
if o.pgbouncer.Spec.ConnectionPool.AuthType == dbapi.PgBouncerClientAuthModeCert || o.pgbouncer.Spec.SSLMode == dbapi.PgBouncerSSLModeVerifyCA || o.pgbouncer.Spec.SSLMode == dbapi.PgBouncerSSLModeVerifyFull {
connector = fmt.Sprintf("user=%s password=%s host=%s port=%d connect_timeout=10 dbname=%s sslmode=%s sslrootcert=%s sslcert=%s sslkey=%s", user, pass, o.url, listeningPort, o.backendDBName, sslMode, paths.CACert, paths.Cert, paths.Key)
} else {
connector = fmt.Sprintf("user=%s password=%s host=%s port=%d connect_timeout=10 dbname=%s sslmode=%s sslrootcert=%s", user, pass, o.url, listeningPort, o.backendDBName, sslMode, paths.CACert)
}
} else {
connector = fmt.Sprintf("user=%s password=%s host=%s port=%d connect_timeout=10 dbname=%s sslmode=%s", user, pass, o.url, listeningPort, o.backendDBName, sslMode)
}
return connector, nil
}

Expand Down

0 comments on commit 9bdc4c6

Please sign in to comment.