-
Notifications
You must be signed in to change notification settings - Fork 212
/
config.go
341 lines (280 loc) · 9.32 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package postgresql
import (
"context"
"database/sql"
"fmt"
"net/url"
"strconv"
"strings"
"sync"
"unicode"
"github.com/blang/semver"
_ "github.com/lib/pq" //PostgreSQL db
"gocloud.dev/postgres"
_ "gocloud.dev/postgres/awspostgres"
_ "gocloud.dev/postgres/gcppostgres"
)
type featureName uint
const (
featureCreateRoleWith featureName = iota
featureDBAllowConnections
featureDBIsTemplate
featureFallbackApplicationName
featureRLS
featureSchemaCreateIfNotExist
featureReplication
featureExtension
featurePrivileges
featureProcedure
featureRoutine
featurePrivilegesOnSchemas
featureForceDropDatabase
featurePid
featurePublishViaRoot
featurePubTruncate
featurePublication
featurePubWithoutTruncate
featureFunction
featureServer
)
var (
dbRegistryLock sync.Mutex
dbRegistry map[string]*DBConnection = make(map[string]*DBConnection, 1)
// Mapping of feature flags to versions
featureSupported = map[featureName]semver.Range{
// CREATE ROLE WITH
featureCreateRoleWith: semver.MustParseRange(">=8.1.0"),
// CREATE DATABASE has ALLOW_CONNECTIONS support
featureDBAllowConnections: semver.MustParseRange(">=9.5.0"),
// CREATE DATABASE has IS_TEMPLATE support
featureDBIsTemplate: semver.MustParseRange(">=9.5.0"),
// https://www.postgresql.org/docs/9.0/static/libpq-connect.html
featureFallbackApplicationName: semver.MustParseRange(">=9.0.0"),
// CREATE SCHEMA IF NOT EXISTS
featureSchemaCreateIfNotExist: semver.MustParseRange(">=9.3.0"),
// row-level security
featureRLS: semver.MustParseRange(">=9.5.0"),
// CREATE ROLE has REPLICATION support.
featureReplication: semver.MustParseRange(">=9.1.0"),
// CREATE EXTENSION support.
featureExtension: semver.MustParseRange(">=9.1.0"),
// We do not support postgresql_grant and postgresql_default_privileges
// for Postgresql < 9.
featurePrivileges: semver.MustParseRange(">=9.0.0"),
// Object PROCEDURE support
featureProcedure: semver.MustParseRange(">=11.0.0"),
// Object ROUTINE support
featureRoutine: semver.MustParseRange(">=11.0.0"),
// ALTER DEFAULT PRIVILEGES has ON SCHEMAS support
// for Postgresql >= 10
featurePrivilegesOnSchemas: semver.MustParseRange(">=10.0.0"),
// DROP DATABASE WITH FORCE
// for Postgresql >= 13
featureForceDropDatabase: semver.MustParseRange(">=13.0.0"),
// Column procpid was replaced by pid in pg_stat_activity
// for Postgresql >= 9.2 and above
featurePid: semver.MustParseRange(">=9.2.0"),
// attribute publish_via_partition_root for partition is supported
featurePublishViaRoot: semver.MustParseRange(">=13.0.0"),
// attribute pubtruncate for publications is supported
featurePubTruncate: semver.MustParseRange(">=11.0.0"),
// attribute pubtruncate for publications is supported
featurePubWithoutTruncate: semver.MustParseRange("<11.0.0"),
// publication is Supported
featurePublication: semver.MustParseRange(">=10.0.0"),
// We do not support CREATE FUNCTION for Postgresql < 8.4
featureFunction: semver.MustParseRange(">=8.4.0"),
// CREATE SERVER support
featureServer: semver.MustParseRange(">=10.0.0"),
}
)
type DBConnection struct {
*sql.DB
client *Client
// version is the version number of the database as determined by parsing the
// output of `SELECT VERSION()`.x
version semver.Version
}
// featureSupported returns true if a given feature is supported or not. This is
// slightly different from Config's featureSupported in that here we're
// evaluating against the fingerprinted version, not the expected version.
func (db *DBConnection) featureSupported(name featureName) bool {
fn, found := featureSupported[name]
if !found {
// panic'ing because this is a provider-only bug
panic(fmt.Sprintf("unknown feature flag %v", name))
}
return fn(db.version)
}
// isSuperuser returns true if connected user is a Postgres SUPERUSER
func (db *DBConnection) isSuperuser() (bool, error) {
var superuser bool
if err := db.QueryRow("SELECT rolsuper FROM pg_roles WHERE rolname = CURRENT_USER").Scan(&superuser); err != nil {
return false, fmt.Errorf("could not check if current user is superuser: %w", err)
}
return superuser, nil
}
type ClientCertificateConfig struct {
CertificatePath string
KeyPath string
}
// Config - provider config
type Config struct {
Scheme string
Host string
Port int
Username string
Password string
DatabaseUsername string
Superuser bool
SSLMode string
ApplicationName string
Timeout int
ConnectTimeoutSec int
MaxConns int
ExpectedVersion semver.Version
SSLClientCert *ClientCertificateConfig
SSLRootCertPath string
}
// Client struct holding connection string
type Client struct {
// Configuration for the client
config Config
databaseName string
}
// NewClient returns client config for the specified database.
func (c *Config) NewClient(database string) *Client {
return &Client{
config: *c,
databaseName: database,
}
}
// featureSupported returns true if a given feature is supported or not. This
// is slightly different from Client's featureSupported in that here we're
// evaluating against the expected version, not the fingerprinted version.
func (c *Config) featureSupported(name featureName) bool {
fn, found := featureSupported[name]
if !found {
// panic'ing because this is a provider-only bug
panic(fmt.Sprintf("unknown feature flag %v", name))
}
return fn(c.ExpectedVersion)
}
func (c *Config) connParams() []string {
params := map[string]string{}
// sslmode and connect_timeout are not allowed with gocloud
// (TLS is provided by gocloud directly)
if c.Scheme == "postgres" {
params["sslmode"] = c.SSLMode
params["connect_timeout"] = strconv.Itoa(c.ConnectTimeoutSec)
}
if c.featureSupported(featureFallbackApplicationName) {
params["fallback_application_name"] = c.ApplicationName
}
if c.SSLClientCert != nil {
params["sslcert"] = c.SSLClientCert.CertificatePath
params["sslkey"] = c.SSLClientCert.KeyPath
}
if c.SSLRootCertPath != "" {
params["sslrootcert"] = c.SSLRootCertPath
}
paramsArray := []string{}
for key, value := range params {
paramsArray = append(paramsArray, fmt.Sprintf("%s=%s", key, url.QueryEscape(value)))
}
return paramsArray
}
func (c *Config) connStr(database string) string {
host := c.Host
// For GCP, support both project/region/instance and project:region:instance
// (The second one allows to use the output of google_sql_database_instance as host
if c.Scheme == "gcppostgres" {
host = strings.ReplaceAll(host, ":", "/")
}
connStr := fmt.Sprintf(
"%s://%s:%s@%s:%d/%s?%s",
c.Scheme,
url.QueryEscape(c.Username),
url.QueryEscape(c.Password),
host,
c.Port,
database,
strings.Join(c.connParams(), "&"),
)
return connStr
}
func (c *Config) getDatabaseUsername() string {
if c.DatabaseUsername != "" {
return c.DatabaseUsername
}
return c.Username
}
// Connect returns a copy to an sql.Open()'ed database connection wrapped in a DBConnection struct.
// Callers must return their database resources. Use of QueryRow() or Exec() is encouraged.
// Query() must have their rows.Close()'ed.
func (c *Client) Connect() (*DBConnection, error) {
dbRegistryLock.Lock()
defer dbRegistryLock.Unlock()
dsn := c.config.connStr(c.databaseName)
conn, found := dbRegistry[dsn]
if !found {
var db *sql.DB
var err error
if c.config.Scheme == "postgres" {
db, err = sql.Open(proxyDriverName, dsn)
} else {
db, err = postgres.Open(context.Background(), dsn)
}
if err == nil {
err = db.Ping()
}
if err != nil {
errString := strings.Replace(err.Error(), c.config.Password, "XXXX", 2)
return nil, fmt.Errorf("Error connecting to PostgreSQL server %s (scheme: %s): %s", c.config.Host, c.config.Scheme, errString)
}
// We don't want to retain connection
// So when we connect on a specific database which might be managed by terraform,
// we don't keep opened connection in case of the db has to be dopped in the plan.
db.SetMaxIdleConns(0)
db.SetMaxOpenConns(c.config.MaxConns)
defaultVersion, _ := semver.Parse(defaultExpectedPostgreSQLVersion)
version := &c.config.ExpectedVersion
if defaultVersion.Equals(c.config.ExpectedVersion) {
// Version hint not set by user, need to fingerprint
version, err = fingerprintCapabilities(db)
if err != nil {
db.Close()
return nil, fmt.Errorf("error detecting capabilities: %w", err)
}
}
conn = &DBConnection{
db,
c,
*version,
}
dbRegistry[dsn] = conn
}
return conn, nil
}
// fingerprintCapabilities queries PostgreSQL to populate a local catalog of
// capabilities. This is only run once per Client.
func fingerprintCapabilities(db *sql.DB) (*semver.Version, error) {
var pgVersion string
err := db.QueryRow(`SELECT VERSION()`).Scan(&pgVersion)
if err != nil {
return nil, fmt.Errorf("error PostgreSQL version: %w", err)
}
// PostgreSQL 9.2.21 on x86_64-apple-darwin16.5.0, compiled by Apple LLVM version 8.1.0 (clang-802.0.42), 64-bit
// PostgreSQL 9.6.7, compiled by Visual C++ build 1800, 64-bit
fields := strings.FieldsFunc(pgVersion, func(c rune) bool {
return unicode.IsSpace(c) || c == ','
})
if len(fields) < 2 {
return nil, fmt.Errorf("error determining the server version: %q", pgVersion)
}
version, err := semver.ParseTolerant(fields[1])
if err != nil {
return nil, fmt.Errorf("error parsing version: %w", err)
}
return &version, nil
}