/
config.go
101 lines (93 loc) · 2.39 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
package config
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/rds/rdsutils"
"github.com/shogo82148/rdsmysql/internal/certificate"
)
// Generate generates the configuration file for mysql.
func Generate(session *session.Session, dir string, config *Config) error {
credentials := session.Config.Credentials
token, err := rdsutils.BuildAuthToken(
fmt.Sprintf("%s:%d", config.Host, config.Port),
aws.StringValue(session.Config.Region),
config.User,
credentials,
)
if err != nil {
return fmt.Errorf("fail to build auth token: %w", err)
}
pempath := filepath.Join(dir, "rds-combined-ca-bundle.pem")
confpath := filepath.Join(dir, "my.conf")
conf := fmt.Sprintf(`[client]
host = %s
user = %s
port = %d
password = %s
ssl-ca = %s
enable-cleartext-plugin
`, config.Host, config.User, config.Port, token, pempath)
now := time.Now()
if err := ioutil.WriteFile(fmt.Sprintf("%s.%d", confpath, now.UnixNano()), []byte(conf), 0600); err != nil {
return err
}
if err := ioutil.WriteFile(fmt.Sprintf("%s.%d", pempath, now.UnixNano()), []byte(certificate.Certificate), 0600); err != nil {
return err
}
if err := os.Rename(fmt.Sprintf("%s.%d", confpath, now.UnixNano()), confpath); err != nil {
return err
}
if err := os.Rename(fmt.Sprintf("%s.%d", pempath, now.UnixNano()), pempath); err != nil {
return err
}
return nil
}
// Config is the configuration for connecting to mysql servers.
type Config struct {
User string
Host string
Port int
Args []string
}
// Parse parses the args of mysql command.
func Parse(args []string) (*Config, error) {
var conf Config
conf.Port = 3306 // default port
conf.Args = make([]string, 0, len(args))
for i := 0; i < len(args); i++ {
switch args[i] {
case "-u", "--user":
i++
if i >= len(args) {
return nil, errors.New("invalid user option")
}
conf.User = args[i]
case "-h", "--host":
i++
if i >= len(args) {
return nil, errors.New("invalid host option")
}
conf.Host = args[i]
case "-P", "--port":
i++
if i >= len(args) {
return nil, errors.New("invalid port option")
}
port, err := strconv.Atoi(args[i])
if err != nil {
return nil, errors.New("fail to parse port")
}
conf.Port = port
default:
conf.Args = append(conf.Args, args[i])
}
}
return &conf, nil
}