-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
284 lines (242 loc) · 6.97 KB
/
client.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
package bun
import (
"context"
"crypto/tls"
"crypto/x509"
"database/sql"
"encoding/pem"
"errors"
"fmt"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/stdlib"
"github.com/spf13/viper"
"github.com/tyrm/megabot/internal/config"
"github.com/tyrm/megabot/internal/db"
"github.com/uptrace/bun/dialect"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/dialect/sqlitedialect"
"modernc.org/sqlite"
"os"
"runtime"
"strings"
"github.com/uptrace/bun"
)
const (
dbTypePostgres = "postgres"
dbTypeSqlite = "sqlite"
dbTLSModeDisable = "disable"
dbTLSModeEnable = "enable"
dbTLSModeRequire = "require"
dbTLSModeUnset = ""
)
// Bun represents a bun db connection and it's error handler
type Bun struct {
errProc func(error) db.Error
*bun.DB
}
// Client is a DB interface compatible client for Bun
type Client struct {
commonDB
userDB
bun *Bun
}
// New creates a new bun database client
func New(ctx context.Context) (db.DB, error) {
var newBun *Bun
var err error
dbType := strings.ToLower(viper.GetString(config.Keys.DbType))
switch dbType {
case dbTypePostgres:
newBun, err = pgConn(ctx)
if err != nil {
return nil, err
}
case dbTypeSqlite:
newBun, err = sqliteConn(ctx)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("database type %s not supported for bundb", dbType)
}
ps := &Client{
commonDB: commonDB{
bun: newBun,
},
userDB: userDB{
bun: newBun,
},
bun: newBun,
}
return ps, nil
}
func sqliteConn(ctx context.Context) (*Bun, error) {
l := logger.WithField("func", "sqliteConn")
// validate bun address has actually been set
dbAddress := viper.GetString(config.Keys.DbAddress)
if dbAddress == "" {
return nil, fmt.Errorf("'%s' was not set when attempting to start sqlite", config.Keys.DbAddress)
}
// Drop anything fancy from DB address
dbAddress = strings.Split(dbAddress, "?")[0]
dbAddress = strings.TrimPrefix(dbAddress, "file:")
// Append our own SQLite preferences
dbAddress = "file:" + dbAddress + "?cache=shared"
// Open new DB instance
sqldb, err := sql.Open("sqlite", dbAddress)
if err != nil {
if errWithCode, ok := err.(*sqlite.Error); ok {
err = errors.New(sqlite.ErrorCodeString[errWithCode.Code()])
}
return nil, fmt.Errorf("could not open sqlite bun: %s", err)
}
setConnectionValues(sqldb)
if dbAddress == "file::memory:?cache=shared" {
l.Warn("sqlite in-memory database should only be used for debugging")
// don't close connections on disconnect -- otherwise
// the SQLite database will be deleted when there
// are no active connections
sqldb.SetConnMaxLifetime(0)
}
conn := getErrConn(bun.NewDB(sqldb, sqlitedialect.New()))
// ping to check the bun is there and listening
if err := conn.PingContext(ctx); err != nil {
if errWithCode, ok := err.(*sqlite.Error); ok {
err = errors.New(sqlite.ErrorCodeString[errWithCode.Code()])
}
return nil, fmt.Errorf("sqlite ping: %s", err)
}
l.Info("connected to SQLITE database")
return conn, nil
}
func pgConn(ctx context.Context) (*Bun, error) {
l := logger.WithField("func", "pgConn")
opts, err := deriveBunDBPGOptions()
if err != nil {
return nil, fmt.Errorf("could not create bundb postgres options: %s", err)
}
sqldb := stdlib.OpenDB(*opts)
setConnectionValues(sqldb)
conn := getErrConn(bun.NewDB(sqldb, pgdialect.New()))
// ping to check the bun is there and listening
if err := conn.PingContext(ctx); err != nil {
return nil, fmt.Errorf("postgres ping: %s", err)
}
l.Info("connected to POSTGRES database")
return conn, nil
}
func deriveBunDBPGOptions() (*pgx.ConnConfig, error) {
keys := config.Keys
if strings.ToUpper(viper.GetString(keys.DbType)) != db.TypePostgres {
return nil, fmt.Errorf("expected bun type of %s but got %s", db.TypePostgres, viper.GetString(keys.DbType))
}
// these are all optional, the bun adapter figures out defaults
port := viper.GetInt(keys.DbPort)
address := viper.GetString(keys.DbAddress)
username := viper.GetString(keys.DbUser)
password := viper.GetString(keys.DbPassword)
// validate database
database := viper.GetString(keys.DbDatabase)
if database == "" {
return nil, errors.New("no database set")
}
var tlsConfig *tls.Config
tlsMode := viper.GetString(keys.DbTLSMode)
switch tlsMode {
case dbTLSModeDisable, dbTLSModeUnset:
break // nothing to do
case dbTLSModeEnable:
/* #nosec G402 */
tlsConfig = &tls.Config{
InsecureSkipVerify: true,
}
case dbTLSModeRequire:
tlsConfig = &tls.Config{
InsecureSkipVerify: false,
ServerName: viper.GetString(keys.DbAddress),
MinVersion: tls.VersionTLS12,
}
}
caCertPath := viper.GetString(keys.DbTLSCACert)
if tlsConfig != nil && caCertPath != "" {
// load the system cert pool first -- we'll append the given CA cert to this
certPool, err := x509.SystemCertPool()
if err != nil {
return nil, fmt.Errorf("error fetching system CA cert pool: %s", err)
}
// open the file itself and make sure there's something in it
/* #nosec G304 */
caCertBytes, err := os.ReadFile(caCertPath)
if err != nil {
return nil, fmt.Errorf("error opening CA certificate at %s: %s", caCertPath, err)
}
if len(caCertBytes) == 0 {
return nil, fmt.Errorf("ca cert at %s was empty", caCertPath)
}
// make sure we have a PEM block
caPem, _ := pem.Decode(caCertBytes)
if caPem == nil {
return nil, fmt.Errorf("could not parse cert at %s into PEM", caCertPath)
}
// parse the PEM block into the certificate
caCert, err := x509.ParseCertificate(caPem.Bytes)
if err != nil {
return nil, fmt.Errorf("could not parse cert at %s into x509 certificate: %s", caCertPath, err)
}
// we're happy, add it to the existing pool and then use this pool in our tls config
certPool.AddCert(caCert)
tlsConfig.RootCAs = certPool
}
cfg, _ := pgx.ParseConfig("")
if address != "" {
cfg.Host = address
}
if port > 0 {
cfg.Port = uint16(port)
}
if username != "" {
cfg.User = username
}
if password != "" {
cfg.Password = password
}
if tlsConfig != nil {
cfg.TLSConfig = tlsConfig
}
cfg.Database = database
cfg.PreferSimpleProtocol = true
cfg.RuntimeParams["application_name"] = viper.GetString(keys.ApplicationName)
return cfg, nil
}
// https://bun.uptrace.dev/postgres/running-bun-in-production.html#database-sql
func setConnectionValues(sqldb *sql.DB) {
maxOpenConns := 4 * runtime.GOMAXPROCS(0)
sqldb.SetMaxOpenConns(maxOpenConns)
sqldb.SetMaxIdleConns(maxOpenConns)
}
func getErrConn(dbConn *bun.DB) *Bun {
var errProc func(error) db.Error
switch dbConn.Dialect().Name() {
case dialect.PG:
errProc = processPostgresError
case dialect.SQLite:
errProc = processSQLiteError
default:
panic("unknown dialect name: " + dbConn.Dialect().Name().String())
}
return &Bun{
errProc: errProc,
DB: dbConn,
}
}
// ProcessError replaces any known values with our own db.Error types
func (conn *Bun) ProcessError(err error) db.Error {
switch {
case err == nil:
return nil
case err == sql.ErrNoRows:
return db.ErrNoEntries
default:
return conn.errProc(err)
}
}