-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
290 lines (265 loc) · 7.35 KB
/
main.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
package main
import (
"crypto/tls"
"crypto/x509"
"database/sql"
"flag"
"fmt"
"os"
"strconv"
"github.com/go-sql-driver/mysql"
migrate "github.com/golang-migrate/migrate/v4"
migrate_mysql "github.com/golang-migrate/migrate/v4/database/mysql"
_ "github.com/golang-migrate/migrate/v4/source/file"
)
var defaultMysqlSocketPath = "/var/run/mysqld/mysqld.sock"
func main() {
var (
mysqlCA string
mysqlClientKey string
mysqlClientCert string
mysqlDatabase string
mysqlDsn string
mysqlDsnEmpty bool
mysqlHost string
mysqlPass string
mysqlPort int
mysqlPortEmpty bool
mysqlSocket string
mysqlUser string
migrationsPath string
migrationsTable string
noLock bool
)
fs := flag.NewFlagSet("migrate-mysql", flag.ExitOnError)
{
fs.StringVar(&migrationsPath, "path", "", "the folder with the migrations in")
fs.StringVar(&migrationsTable, "migrations-table", "", "the table name to use for schema versioning")
fs.StringVar(&mysqlCA, "server-ca", "", "MySQL TLS Certificate Authority, provide if you want to connect to MySQL with TLS")
fs.StringVar(&mysqlClientCert, "client-cert", "", "MySQL TLS Client Certificate, provide if you want to connect to MySQL with TLS")
fs.StringVar(&mysqlClientKey, "client-key", "", "MySQL TLS Client Key, provide if you want to connect to MySQL with TLS")
fs.StringVar(&mysqlDatabase, "database", "", "the database to use when connected to MySQL")
fs.StringVar(&mysqlDsn, "dsn", "", "the complete dsn to use when connecting to MySQL")
fs.StringVar(&mysqlHost, "host", "", "the host to use when connecting to MySQL")
fs.StringVar(&mysqlPass, "pass", "", "the password to use when connecting to MySQL")
fs.IntVar(&mysqlPort, "port", 0, "the port to use when connecting to MySQL")
fs.StringVar(&mysqlSocket, "sock", "", "the socket to use when connecting to MySQL")
fs.StringVar(&mysqlUser, "user", "", "the user to use when connecting to MySQL")
fs.BoolVar(&noLock, "no-lock", false, "use no lock with migrate tool")
// already set to exit on error
_ = fs.Parse(os.Args[1:])
if mysqlCA == "" {
mysqlCA = os.Getenv("MYSQL_CA")
}
if mysqlClientCert == "" {
mysqlClientCert = os.Getenv("MYSQL_CLIENT_CERT")
}
if mysqlClientKey == "" {
mysqlClientKey = os.Getenv("MYSQL_CLIENT_KEY")
}
if mysqlDatabase == "" {
mysqlDatabase = os.Getenv("MYSQL_DATABASE")
}
if mysqlDsn == "" {
mysqlDsnEnv, set := os.LookupEnv("MYSQL_DSN")
mysqlDsnEmpty = !set
if set {
mysqlDsn = mysqlDsnEnv
}
}
if mysqlHost == "" {
mysqlHostEnv, set := os.LookupEnv("MYSQL_HOST")
if set {
mysqlHost = mysqlHostEnv
}
}
if mysqlPass == "" {
mysqlPass = os.Getenv("MYSQL_PASS")
}
if mysqlPort == 0 {
portEnv, set := os.LookupEnv("MYSQL_PORT")
mysqlPortEmpty = !set
if set {
port, err := strconv.Atoi(portEnv)
if err != nil {
fmt.Fprintf(os.Stderr, "could not set MYSQL_PORT to %s, expected integer\n", portEnv)
os.Exit(1)
}
mysqlPort = port
}
}
if mysqlUser == "" {
mysqlUser = os.Getenv("MYSQL_USER")
}
if mysqlSocket == "" {
mysqlSocketEnv, set := os.LookupEnv("MYSQL_SOCK")
if set {
mysqlSocket = mysqlSocketEnv
} else {
mysqlSocket = defaultMysqlSocketPath
}
}
}
mySQLClientConfig := &ClientConfig{
User: mysqlUser,
Pass: mysqlPass,
Host: mysqlHost,
Port: mysqlPort,
PortEmpty: mysqlPortEmpty,
SocketPath: mysqlSocket,
Database: mysqlDatabase,
FullDsn: mysqlDsn,
FullDsnEmpty: mysqlDsnEmpty,
ClientKey: mysqlClientKey,
ClientCert: mysqlClientCert,
CA: mysqlCA,
}
db, err := NewClient(mySQLClientConfig)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
defer db.Close()
driver, err := migrate_mysql.WithInstance(db, &migrate_mysql.Config{
DatabaseName: mysqlDatabase,
MigrationsTable: migrationsTable,
NoLock: noLock,
})
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
m, err := migrate.NewWithDatabaseInstance(
fmt.Sprintf("file://%v", migrationsPath),
mysqlDatabase,
driver,
)
m.Log = &logger{}
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
commandParameters := fs.Args()
if len(commandParameters) < 1 {
fmt.Fprintln(os.Stderr, "expected command up/down/step/version")
return
}
var commandErr error
switch commandParameters[0] {
case "up":
commandErr = m.Up()
case "down":
commandErr = m.Down()
case "drop":
commandErr = m.Drop()
case "force":
versionParam, err := strconv.Atoi(commandParameters[1])
if err != nil {
fmt.Fprintln(os.Stderr, "version parameter must be an int")
}
commandErr = m.Force(versionParam)
case "step":
stepsParam, err := strconv.Atoi(commandParameters[1])
if err != nil {
fmt.Fprintln(os.Stderr, "step parameter must be an int")
}
commandErr = m.Steps(stepsParam)
case "version":
version, dirty, err := m.Version()
if err != nil {
if err == migrate.ErrNilVersion {
fmt.Fprintln(os.Stderr, "database not versioned")
} else {
fmt.Fprintln(os.Stderr, err)
}
}
fmt.Printf("database: %s, version: %v, dirty %v\n", mysqlDatabase, version, dirty)
return
}
if commandErr != nil {
fmt.Fprintln(os.Stderr, commandErr)
}
}
type logger struct{}
func (l *logger) Printf(format string, v ...interface{}) {
fmt.Printf(format, v...)
}
func (l *logger) Verbose() bool {
return false
}
type ClientConfig struct {
User string
Pass string
Host string
Port int
PortEmpty bool
Database string
FullDsn string
FullDsnEmpty bool
ServerName string
SocketPath string
ClientKey string
ClientCert string
CA string
}
func (c *ClientConfig) MySQLConfig() (string, error) {
if !c.FullDsnEmpty {
dsn, err := mysql.ParseDSN(c.FullDsn)
if err != nil {
return "", err
}
return dsn.FormatDSN(), nil
}
cfg := mysql.NewConfig()
cfg.User = c.User
cfg.Passwd = c.Pass
if !c.PortEmpty {
cfg.Net = "tcp"
cfg.Addr = fmt.Sprintf("%s:%d", c.Host, c.Port)
} else {
cfg.Net = "unix"
socketPath := c.SocketPath
if socketPath == "" {
socketPath = defaultMysqlSocketPath
}
cfg.Addr = socketPath
}
cfg.DBName = c.Database
if c.SSLEnabled() {
rootCertPool := x509.NewCertPool()
if ok := rootCertPool.AppendCertsFromPEM([]byte(c.CA)); !ok {
return "", fmt.Errorf("failed to append PEM.")
}
clientCert := make([]tls.Certificate, 0, 1)
certs, err := tls.X509KeyPair([]byte(c.ClientCert), []byte(c.ClientKey))
if err != nil {
return "", fmt.Errorf("failed to produce x509 keypair: %w", err)
}
clientCert = append(clientCert, certs)
err = mysql.RegisterTLSConfig("custom", &tls.Config{
RootCAs: rootCertPool,
Certificates: clientCert,
InsecureSkipVerify: true,
})
if err != nil {
return "", fmt.Errorf("failed to register mysql tls config: %w", err)
}
cfg.TLSConfig = "custom"
}
cfg.MultiStatements = true
return cfg.FormatDSN(), nil
}
func (c *ClientConfig) SSLEnabled() bool {
return c.ClientKey != "" && c.ClientCert != "" && c.CA != ""
}
func NewClient(cfg *ClientConfig) (*sql.DB, error) {
dsn, err := cfg.MySQLConfig()
if err != nil {
return nil, fmt.Errorf("failed to configure mysql connection config: %w", err)
}
db, err := sql.Open("mysql", dsn)
if err != nil {
return nil, fmt.Errorf("failed to connect to mysql: %w", err)
}
return db, nil
}