forked from cloudfoundry/stratos
/
datastore.go
257 lines (209 loc) · 6.83 KB
/
datastore.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
package datastore
import (
"database/sql"
"fmt"
"os"
"regexp"
"strings"
"github.com/SUSE/stratos-ui/config"
log "github.com/Sirupsen/logrus"
// Mysql driver
_ "github.com/go-sql-driver/mysql"
"github.com/kat-co/vala"
// Sqlite driver
_ "github.com/mattn/go-sqlite3"
"bitbucket.org/liamstask/goose/lib/goose"
)
const (
// SQLite DB Provider
SQLITE string = "sqlite"
// PGSQL DB Provider
PGSQL = "pgsql"
// MYSQL DB Provider
MYSQL = "mysql"
)
// DatabaseConfig represents the connection configuration parameters
type DatabaseConfig struct {
DatabaseProvider string `configName:"DATABASE_PROVIDER"`
Username string `configName:"DB_USER"`
Password string `configName:"DB_PASSWORD"`
Database string `configName:"DB_DATABASE_NAME"`
Host string `configName:"DB_HOST"`
Port int `configName:"DB_PORT"`
SSLMode string `configName:"DB_SSL_MODE"`
ConnectionTimeoutInSecs int `configName:"DB_CONNECT_TIMEOUT_IN_SECS"`
SSLCertificate string `configName:"DB_CERT"`
SSLKey string `configName:"DB_CERT_KEY"`
SSLRootCertificate string `configName:"DB_ROOT_CERT"`
}
// SSLValidationMode is the PostgreSQL driver SSL validation modes
type SSLValidationMode string
const (
// SSLDisabled means no checking of SSL
SSLDisabled SSLValidationMode = "disable"
// SSLRequired requires SSL without validation
SSLRequired SSLValidationMode = "require"
// SSLVerifyCA verifies the CA certificate
SSLVerifyCA SSLValidationMode = "verify-ca"
// SSLVerifyFull verifies the certificate and hostname and the CA
SSLVerifyFull SSLValidationMode = "verify-full"
// SQLiteSchemaFile - SQLite schema file
SQLiteSchemaFile = "./deploy/db/sqlite_schema.sql"
// SQLiteDatabaseFile - SQLite database file
SQLiteDatabaseFile = "./console-database.db"
// Default database provider when not specified
DefaultDatabaseProvider = MYSQL
)
const (
// UniqueConstraintViolation is the error code for a unique constraint violation
UniqueConstraintViolation = 23505
// DefaultConnectionTimeout is the default to timeout on connections
DefaultConnectionTimeout = 10
)
// NewDatabaseConnectionParametersFromConfig setup database connection parameters based on contents of config struct
func NewDatabaseConnectionParametersFromConfig(dc DatabaseConfig) (DatabaseConfig, error) {
if len(dc.DatabaseProvider) == 0 {
dc.DatabaseProvider = DefaultDatabaseProvider
}
// set default for connection timeout if necessary
if dc.ConnectionTimeoutInSecs <= 0 {
dc.ConnectionTimeoutInSecs = DefaultConnectionTimeout
}
// No configuration needed for SQLite
if dc.DatabaseProvider == SQLITE {
return dc, nil
}
// Database Config validation - check requried values and the SSL Mode
err := validateRequiredDatabaseParams(dc.Username, dc.Password, dc.Database, dc.Host, dc.Port)
if err != nil {
return dc, err
}
if dc.DatabaseProvider == PGSQL {
if dc.SSLMode == string(SSLDisabled) || dc.SSLMode == string(SSLRequired) ||
dc.SSLMode == string(SSLVerifyCA) || dc.SSLMode == string(SSLVerifyFull) {
return dc, nil
} else {
// Invalid SSL mode
return dc, fmt.Errorf("Invalid SSL mode: %s", dc.SSLMode)
}
} else if dc.DatabaseProvider == MYSQL {
return dc, nil
}
return dc, fmt.Errorf("Invalid provider %v", dc)
}
func validateRequiredDatabaseParams(username, password, database, host string, port int) (err error) {
log.Debug("validateRequiredDatabaseParams")
err = vala.BeginValidation().Validate(
vala.IsNotNil(username, "username"),
vala.IsNotNil(password, "password"),
vala.IsNotNil(database, "database"),
vala.IsNotNil(host, "host"),
vala.GreaterThan(port, 0, "port"),
vala.Not(vala.GreaterThan(port, 65535, "port")),
).Check()
if err != nil {
return err
}
return nil
}
// GetConnection returns a database connection to either PostgreSQL or SQLite
func GetConnection(dc DatabaseConfig) (*sql.DB, error) {
log.Debug("GetConnection")
if dc.DatabaseProvider == PGSQL {
return sql.Open("postgres", buildConnectionString(dc))
}
if dc.DatabaseProvider == MYSQL {
return sql.Open("mysql", buildConnectionStringForMysql(dc))
}
// SQL Lite
return GetSQLLiteConnection()
}
func GetSQLLiteConnection() (*sql.DB, error) {
if !config.IsSet("SQLITE_KEEP_DB") {
os.Remove(SQLiteDatabaseFile)
}
db, err := sql.Open("sqlite3", SQLiteDatabaseFile)
if err != nil {
return db, err
}
// Create fake goose db conf object for SQLite
d := goose.DBDriver{
Name: "sqlite3",
Import: "github.com/mattn/go-sqlite3",
Dialect: &goose.Sqlite3Dialect{},
}
conf := &goose.DBConf{
Driver: d,
}
ApplyMigrations(conf, db)
return db, err
}
func buildConnectionString(dc DatabaseConfig) string {
log.Debug("buildConnectionString")
escapeStr := func(in string) string {
return strings.Replace(in, `'`, `\'`, -1)
}
connStr := fmt.Sprintf("user='%s' password='%s' dbname='%s' host='%s' port=%d connect_timeout=%d",
escapeStr(dc.Username),
escapeStr(dc.Password),
escapeStr(dc.Database),
dc.Host,
dc.Port,
dc.ConnectionTimeoutInSecs)
if dc.SSLMode != "" {
connStr = connStr + fmt.Sprintf(" sslmode='%s'", dc.SSLMode)
}
if dc.SSLCertificate != "" {
connStr = connStr + fmt.Sprintf(" sslcert='%s'", escapeStr(dc.SSLCertificate))
}
if dc.SSLKey != "" {
connStr = connStr + fmt.Sprintf(" sslkey='%s'", escapeStr(dc.SSLKey))
}
if dc.SSLRootCertificate != "" {
connStr = connStr + fmt.Sprintf(" sslrootcert='%s'", escapeStr(dc.SSLRootCertificate))
}
log.Printf("DB Connection string: dbname='%s' host='%s' port=%d connect_timeout=%d",
escapeStr(dc.Database),
dc.Host,
dc.Port,
dc.ConnectionTimeoutInSecs)
return connStr
}
func buildConnectionStringForMysql(dc DatabaseConfig) string {
log.Debug("buildConnectionString")
escapeStr := func(in string) string {
return strings.Replace(in, `'`, `\'`, -1)
}
connStr := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=true",
escapeStr(dc.Username),
escapeStr(dc.Password),
dc.Host,
dc.Port,
escapeStr(dc.Database))
log.Printf("DB Connection string: %s:*********@tcp(%s:%d)/%s?parseTime=true",
escapeStr(dc.Username),
dc.Host,
dc.Port,
escapeStr(dc.Database))
return connStr
}
// Ping - ping the database to ensure the connection/pool works.
func Ping(db *sql.DB) error {
log.Debug("Ping database")
err := db.Ping()
if err != nil {
return fmt.Errorf("Unable to ping the database: %+v", err)
}
return nil
}
// ModifySQLStatement - Modify the given DB statement for the specified provider, as appropraite
// e.g Postgres uses $1, $2 etc
// SQLite uses ?
func ModifySQLStatement(sql string, databaseProvider string) string {
if databaseProvider == SQLITE || databaseProvider == MYSQL {
sqlParamReplace := regexp.MustCompile("\\$[0-9]")
return sqlParamReplace.ReplaceAllString(sql, "?")
}
// Default is to return the SQL provided directly
return sql
}