Skip to content

Commit

Permalink
adding tls for pgbouncer (#149)
Browse files Browse the repository at this point in the history
Signed-off-by: Hiranmoy Das Chowdhury <[email protected]>
  • Loading branch information
HiranmoyChowdhury authored Nov 7, 2024
1 parent 7e70d36 commit 8f47085
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions pgbouncer/kubedb_client_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import (
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"kmodules.xyz/client-go/tools/certholder"
appbinding "kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/client"
"xorm.io/xorm"
)

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

type Auth struct {
Expand Down Expand Up @@ -171,6 +171,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 +204,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 8f47085

Please sign in to comment.