-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
202 lines (172 loc) · 5.01 KB
/
pool.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
package db
import (
"context"
"crypto/tls"
"crypto/x509"
"database/sql"
"io/ioutil"
"net"
"net/url"
"strconv"
"time"
"github.com/jackc/pgx"
"github.com/jackc/pgx/stdlib"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/sean-/vpc/internal/buildtime"
"github.com/sean-/vpc/internal/logger"
)
type Pool struct {
pool *pgx.ConnPool
stdDriverConfig *stdlib.DriverConfig
config Config
}
func New(cfg Config) (*Pool, error) {
pool := &Pool{
config: cfg,
}
tlsConfig, err := cfg.TLSConfig()
if err != nil {
return nil, errors.Wrap(err, "unable to generate a TLS config")
}
const keepAliveTimeout = 5 * time.Minute
// TODO(jen20) do we even want to support password auth vs TLS?
poolConfig := pgx.ConnPoolConfig{
ConnConfig: pgx.ConnConfig{
Logger: logger.NewPGX(log.Logger),
Database: cfg.Database,
User: cfg.User,
//Password: cfg.Password,
Host: cfg.Host,
Port: cfg.Port,
Dial: (&net.Dialer{Timeout: cfg.ConnTimeout, KeepAlive: keepAliveTimeout}).Dial,
UseFallbackTLS: false,
TLSConfig: tlsConfig,
// TODO(seanc): Need to write a zerolog facade that satisfies the pgx logger interface
// Logger: log.Logger.With().Str("module", "pgx").Logger(),
LogLevel: pgx.LogLevelTrace, //pgxLogLevel,
RuntimeParams: map[string]string{
"application_name": buildtime.PROGNAME,
},
},
}
p, err := pgx.NewConnPool(poolConfig)
if err != nil {
return nil, errors.Wrap(err, "unable to create a new DB connection pool")
}
pool.pool = p
if err := pool.Ping(); err != nil {
return nil, errors.Wrap(err, "unable to ping database")
}
pool.stdDriverConfig = &stdlib.DriverConfig{
ConnConfig: pgx.ConnConfig{
Database: cfg.Database,
Dial: (&net.Dialer{Timeout: cfg.ConnTimeout, KeepAlive: keepAliveTimeout}).Dial,
Host: cfg.Host,
LogLevel: pgx.LogLevelTrace, //pgxLogLevel,
Logger: logger.NewPGX(log.Logger),
Port: cfg.Port,
TLSConfig: tlsConfig,
UseFallbackTLS: false,
User: cfg.User,
RuntimeParams: map[string]string{
"application_name": buildtime.PROGNAME,
},
},
AfterConnect: func(c *pgx.Conn) error {
return nil
},
}
stdlib.RegisterDriverConfig(pool.stdDriverConfig)
return pool, nil
}
func (p *Pool) Close() error {
p.pool.Close()
return nil
}
func (p *Pool) Ping() error {
pingCtx, pingCancel := context.WithTimeout(context.Background(), p.config.ConnTimeout)
defer pingCancel()
conn, err := p.pool.Acquire()
if err != nil {
return errors.Wrap(err, "unable to acquire database connection for ping")
}
defer p.pool.Release(conn)
if err := conn.Ping(pingCtx); err != nil {
return errors.Wrap(err, "unable to ping database")
}
return nil
}
func (p *Pool) Pool() *pgx.ConnPool {
return p.pool
}
func (p *Pool) STDDB() (*sql.DB, error) {
username := p.config.User
hostname := p.config.Host
port := p.config.Port
database := p.config.Database
v := url.Values{}
if !p.config.InsecureSkipVerify {
sslMode := "require"
v.Set("sslmode", sslMode)
v.Set("sslrootcert", p.config.CAPath)
v.Set("sslcert", p.config.CertPath)
v.Set("sslkey", p.config.KeyPath)
}
u := url.URL{
Scheme: p.config.Scheme,
User: url.User(username),
Host: net.JoinHostPort(hostname, strconv.Itoa(int(port))),
Path: database,
RawQuery: v.Encode(),
}
encodedURI := p.stdDriverConfig.ConnectionString(u.String())
db, err := sql.Open("pgx", encodedURI)
if err != nil {
return nil, errors.Wrap(err, "unable to open a standard database connection")
}
return db, nil
}
func (p *Config) TLSConfig() (*tls.Config, error) {
caCertPool := x509.NewCertPool()
{
caPath := p.CAPath
caCert, err := ioutil.ReadFile(caPath)
if err != nil {
return nil, errors.Wrapf(err, "unable to read CA file %q", caPath)
}
if !caCertPool.AppendCertsFromPEM(caCert) {
return nil, errors.Wrap(err, "unable to add CA to cert pool")
}
}
cert, err := tls.LoadX509KeyPair(p.CertPath, p.KeyPath)
if err != nil {
return nil, errors.Wrap(err, "unable to read cert")
}
tlsConfig := &tls.Config{
InsecureSkipVerify: p.InsecureSkipVerify,
ServerName: p.Host,
MinVersion: tls.VersionTLS12,
PreferServerCipherSuites: true,
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
ClientCAs: caCertPool,
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
},
}
return tlsConfig, nil
}