@@ -5,13 +5,16 @@ import (
55 "context"
66 "encoding/json"
77 "fmt"
8+ "os"
9+ "path/filepath"
810 "regexp"
911
1012 "github.com/jackc/pgx/v5"
1113 "github.com/pkg/errors"
1214 troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
1315 "k8s.io/client-go/kubernetes"
1416 "k8s.io/client-go/rest"
17+ "k8s.io/klog/v2"
1518)
1619
1720type CollectPostgres struct {
@@ -37,20 +40,74 @@ func (c *CollectPostgres) createConnectConfig() (*pgx.ConnConfig, error) {
3740 return nil , errors .New ("postgres uri cannot be empty" )
3841 }
3942
40- cfg , err := pgx .ParseConfig (c .Collector .URI )
41- if err != nil {
42- return nil , errors .Wrap (err , "failed to parse postgres config" )
43- }
44-
4543 if c .Collector .TLS != nil {
46- tlsCfg , err := createTLSConfig (c .Context , c .Client , c .Collector .TLS )
44+ klog .V (2 ).Infof ("Connecting to postgres with TLS client config" )
45+ // Set the libpq TLS environment variables since pgx parses them to
46+ // create the TLS configuration (tls.Config instance) to connect with
47+ // https://www.postgresql.org/docs/current/libpq-envars.html
48+ caCert , clientCert , clientKey , err := getTLSParamTriplet (c .Context , c .Client , c .Collector .TLS )
4749 if err != nil {
4850 return nil , err
4951 }
5052
51- tlsCfg .ServerName = cfg .Host
52- cfg .TLSConfig = tlsCfg
53+ // Drop the TLS params to files and set the paths to their
54+ // respective environment variables
55+ // The environment variables are unset after the connection config
56+ // is created. Their respective files are deleted as well.
57+ tmpdir , err := os .MkdirTemp ("" , "ts-postgres-collector" )
58+ if err != nil {
59+ return nil , errors .Wrap (err , "failed to create temp dir to store postgres collector TLS files" )
60+ }
61+ defer os .RemoveAll (tmpdir )
62+
63+ if caCert != "" {
64+ caCertPath := filepath .Join (tmpdir , "ca.crt" )
65+ err = os .WriteFile (caCertPath , []byte (caCert ), 0644 )
66+ if err != nil {
67+ return nil , errors .Wrap (err , "failed to write ca cert to file" )
68+ }
69+ err = os .Setenv ("PGSSLROOTCERT" , caCertPath )
70+ if err != nil {
71+ return nil , errors .Wrap (err , "failed to set PGSSLROOTCERT environment variable" )
72+ }
73+ klog .V (2 ).Infof ("'PGSSLROOTCERT' environment variable set to %q" , caCertPath )
74+ defer os .Unsetenv ("PGSSLROOTCERT" )
75+ }
76+
77+ if clientCert != "" {
78+ clientCertPath := filepath .Join (tmpdir , "client.crt" )
79+ err = os .WriteFile (clientCertPath , []byte (clientCert ), 0644 )
80+ if err != nil {
81+ return nil , errors .Wrap (err , "failed to write client cert to file" )
82+ }
83+ err = os .Setenv ("PGSSLCERT" , clientCertPath )
84+ if err != nil {
85+ return nil , errors .Wrap (err , "failed to set PGSSLCERT environment variable" )
86+ }
87+ klog .V (2 ).Infof ("'PGSSLCERT' environment variable set to %q" , clientCertPath )
88+ defer os .Unsetenv ("PGSSLCERT" )
89+ }
90+
91+ if clientKey != "" {
92+ clientKeyPath := filepath .Join (tmpdir , "client.key" )
93+ err = os .WriteFile (clientKeyPath , []byte (clientKey ), 0600 )
94+ if err != nil {
95+ return nil , errors .Wrap (err , "failed to write client key to file" )
96+ }
97+ err = os .Setenv ("PGSSLKEY" , clientKeyPath )
98+ if err != nil {
99+ return nil , errors .Wrap (err , "failed to set PGSSLKEY environment variable" )
100+ }
101+ klog .V (2 ).Infof ("'PGSSLKEY' environment variable set to %q" , clientKeyPath )
102+ defer os .Unsetenv ("PGSSLKEY" )
103+ }
104+ }
105+
106+ cfg , err := pgx .ParseConfig (c .Collector .URI )
107+ if err != nil {
108+ return nil , errors .Wrap (err , "failed to parse postgres config" )
53109 }
110+ klog .V (2 ).Infof ("Successfully parsed postgres config" )
54111
55112 return cfg , nil
56113}
@@ -74,8 +131,10 @@ func (c *CollectPostgres) Collect(progressChan chan<- interface{}) (CollectorRes
74131
75132 conn , err := c .connect ()
76133 if err != nil {
134+ klog .V (2 ).Infof ("Postgres connection error: %s" , err .Error ())
77135 databaseConnection .Error = err .Error ()
78136 } else {
137+ klog .V (2 ).Info ("Successfully connected to postgres" )
79138 defer conn .Close (c .Context )
80139
81140 query := `select version()`
0 commit comments