/
dbconf.go
170 lines (156 loc) · 4.81 KB
/
dbconf.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
package config
import (
"errors"
"fmt"
"io/ioutil"
"github.com/ducesoft/ulsp/dialect"
"golang.org/x/crypto/ssh"
)
type Proto string
const (
ProtoTCP Proto = "tcp"
ProtoUDP Proto = "udp"
ProtoUnix Proto = "unix"
)
type DBConfig struct {
Alias string `json:"alias" yaml:"alias"`
Driver dialect.DatabaseDriver `json:"driver" yaml:"driver"`
DataSourceName string `json:"dataSourceName" yaml:"dataSourceName"`
Proto Proto `json:"proto" yaml:"proto"`
User string `json:"user" yaml:"user"`
Passwd string `json:"passwd" yaml:"passwd"`
Host string `json:"host" yaml:"host"`
Port int `json:"port" yaml:"port"`
Path string `json:"path" yaml:"path"`
DBName string `json:"dbName" yaml:"dbName"`
Params map[string]string `json:"params" yaml:"params"`
SSHCfg *SSHConfig `json:"sshConfig" yaml:"sshConfig"`
}
func (c *DBConfig) Validate() error {
if c.Driver == "" {
return errors.New("required: connections[].driver")
}
switch c.Driver {
case
dialect.DatabaseDriverMySQL,
dialect.DatabaseDriverMySQL8,
dialect.DatabaseDriverMySQL57,
dialect.DatabaseDriverMySQL56,
dialect.DatabaseDriverPostgreSQL:
if c.DataSourceName == "" && c.Proto == "" {
return errors.New("required: connections[].dataSourceName or connections[].proto")
}
if c.DataSourceName == "" && c.Proto != "" {
if c.User == "" {
return errors.New("required: connections[].user")
}
switch c.Proto {
case ProtoTCP, ProtoUDP:
if c.Host == "" {
return errors.New("required: connections[].host")
}
case ProtoUnix:
if c.Path == "" {
return errors.New("required: connections[].path")
}
default:
return errors.New("invalid: connections[].proto")
}
if c.SSHCfg != nil {
return c.SSHCfg.Validate()
}
}
case dialect.DatabaseDriverSQLite3:
if c.DataSourceName == "" {
return errors.New("required: connections[].dataSourceName")
}
case dialect.DatabaseDriverMssql:
if c.DataSourceName == "" && c.Proto == "" {
return errors.New("required: connections[].dataSourceName or connections[].proto")
}
if c.DataSourceName == "" && c.Proto != "" {
if c.User == "" {
return errors.New("required: connections[].user")
}
switch c.Proto {
case ProtoTCP:
if c.Host == "" {
return errors.New("required: connections[].host")
}
case ProtoUDP, ProtoUnix:
default:
return errors.New("invalid: connections[].proto")
}
}
case dialect.DatabaseDriverOracle:
if c.DataSourceName == "" && c.Proto == "" {
return errors.New("required: connections[].dataSourceName or connections[].proto")
}
if c.DataSourceName == "" {
if c.User == "" {
return errors.New("required: connections[].user")
}
if c.Passwd == "" {
return errors.New("required: connections[].Passwd")
}
if c.Host == "" {
return errors.New("required: connections[].Host")
}
if c.Port <= 0 {
return errors.New("required: connections[].Port")
}
if c.DBName == "" {
return errors.New("required: connections[].DBName")
}
}
default:
return errors.New("invalid: connections[].driver")
}
return nil
}
type SSHConfig struct {
Host string `json:"host" yaml:"host"`
Port int `json:"port" yaml:"port"`
User string `json:"user" yaml:"user"`
PassPhrase string `json:"passPhrase" yaml:"passPhrase"`
PrivateKey string `json:"privateKey" yaml:"privateKey"`
}
func (c *SSHConfig) Validate() error {
if c.Host == "" {
return errors.New("required: connections[]sshConfig.host")
}
if c.User == "" {
return errors.New("required: connections[].sshConfig.user")
}
if c.PrivateKey == "" {
return errors.New("required: connections[].sshConfig.privateKey")
}
return nil
}
func (s *SSHConfig) Endpoint() string {
return fmt.Sprintf("%s:%d", s.Host, s.Port)
}
func (s *SSHConfig) ClientConfig() (*ssh.ClientConfig, error) {
buffer, err := ioutil.ReadFile(s.PrivateKey)
if err != nil {
return nil, fmt.Errorf("cannot read SSH private key file, PrivateKey=%s, %w", s.PrivateKey, err)
}
var key ssh.Signer
if s.PassPhrase != "" {
key, err = ssh.ParsePrivateKeyWithPassphrase(buffer, []byte(s.PassPhrase))
if err != nil {
return nil, fmt.Errorf("cannot parse SSH private key file with passphrase, PrivateKey=%s, %w", s.PrivateKey, err)
}
} else {
key, err = ssh.ParsePrivateKey(buffer)
if err != nil {
return nil, fmt.Errorf("cannot parse SSH private key file, PrivateKey=%s, %w", s.PrivateKey, err)
}
}
sshConfig := &ssh.ClientConfig{
User: s.User,
Auth: []ssh.AuthMethod{ssh.PublicKeys(key)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
return sshConfig, nil
}