@@ -23,31 +23,62 @@ type postgresDB struct {
23
23
dsn string
24
24
endpoint string
25
25
port string
26
- sslmode string
26
+ options Options
27
+ }
28
+
29
+ type Options struct {
30
+ SSLMode string
31
+ SSLCert string
32
+ SSLKey string
33
+ SSLRootCert string
34
+ }
35
+
36
+ func (o Options ) queryString () string {
37
+ values := url.Values {}
38
+
39
+ if o .SSLMode != "" {
40
+ values .Add ("sslmode" , o .SSLMode )
41
+ }
42
+
43
+ if o .SSLCert != "" {
44
+ values .Add ("sslcert" , o .SSLCert )
45
+ }
46
+
47
+ if o .SSLKey != "" {
48
+ values .Add ("sslkey" , o .SSLKey )
49
+ }
50
+
51
+ if o .SSLRootCert != "" {
52
+ values .Add ("sslrootcert" , o .SSLRootCert )
53
+ }
54
+
55
+ return values .Encode ()
27
56
}
28
57
29
58
// New returns a new PostgreSQL database client. The default database name is
30
59
// an empty string. The underlying pq library will default to either using the
31
60
// value of PGDATABASE, or if unset, the hardcoded string 'postgres'.
32
- // The sslmode defines the mode used to set up the connection for the provider.
33
- func New (creds map [string ][]byte , database , sslmode string ) xsql.DB {
61
+ // The options provide additional settings to set up the connection for the
62
+ // provider.
63
+ func New (creds map [string ][]byte , database string , options Options ) xsql.DB {
34
64
// TODO(negz): Support alternative connection secret formats?
35
65
endpoint := string (creds [xpv1 .ResourceCredentialsSecretEndpointKey ])
36
66
port := string (creds [xpv1 .ResourceCredentialsSecretPortKey ])
37
67
username := string (creds [xpv1 .ResourceCredentialsSecretUserKey ])
38
68
password := string (creds [xpv1 .ResourceCredentialsSecretPasswordKey ])
39
- dsn := DSN (username , password , endpoint , port , database , sslmode )
69
+
70
+ dsn := DSN (username , password , endpoint , port , database , options .queryString ())
40
71
41
72
return postgresDB {
42
73
dsn : dsn ,
43
74
endpoint : endpoint ,
44
75
port : port ,
45
- sslmode : sslmode ,
76
+ options : options ,
46
77
}
47
78
}
48
79
49
80
// DSN returns the DSN URL
50
- func DSN (username , password , endpoint , port , database , sslmode string ) string {
81
+ func DSN (username , password , endpoint , port , database , options string ) string {
51
82
// Use net/url UserPassword to encode the username and password
52
83
// This will ensure that any special characters in the username or password
53
84
// are percent-encoded for use in the user info portion of the DSN URL
@@ -57,7 +88,8 @@ func DSN(username, password, endpoint, port, database, sslmode string) string {
57
88
endpoint + ":" +
58
89
port + "/" +
59
90
database +
60
- "?sslmode=" + sslmode
91
+ "?" + options
92
+
61
93
}
62
94
63
95
// ExecTx executes an array of queries, committing if all are successful and
@@ -130,10 +162,13 @@ func (c postgresDB) Scan(ctx context.Context, q xsql.Query, dest ...interface{})
130
162
// GetConnectionDetails returns the connection details for a user of this DB
131
163
func (c postgresDB ) GetConnectionDetails (username , password string ) managed.ConnectionDetails {
132
164
return managed.ConnectionDetails {
133
- xpv1 .ResourceCredentialsSecretUserKey : []byte (username ),
134
- xpv1 .ResourceCredentialsSecretPasswordKey : []byte (password ),
135
- xpv1 .ResourceCredentialsSecretEndpointKey : []byte (c .endpoint ),
136
- xpv1 .ResourceCredentialsSecretPortKey : []byte (c .port ),
165
+ xpv1 .ResourceCredentialsSecretUserKey : []byte (username ),
166
+ xpv1 .ResourceCredentialsSecretPasswordKey : []byte (password ),
167
+ xpv1 .ResourceCredentialsSecretEndpointKey : []byte (c .endpoint ),
168
+ xpv1 .ResourceCredentialsSecretPortKey : []byte (c .port ),
169
+ xpv1 .ResourceCredentialsSecretClientCertKey : []byte (c .options .SSLCert ),
170
+ xpv1 .ResourceCredentialsSecretClientKeyKey : []byte (c .options .SSLKey ),
171
+ xpv1 .ResourceCredentialsSecretCAKey : []byte (c .options .SSLRootCert ),
137
172
}
138
173
}
139
174
0 commit comments