-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
59 lines (51 loc) · 1.54 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
package mysql
import (
"encoding/json"
"fmt"
"github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/sshtunnel"
"github.com/rudderlabs/sqlconnect-go/sqlconnect/internal/util"
)
type Config struct {
Host string `json:"host"`
Port int `json:"port"`
DBName string `json:"dbname"`
User string `json:"user"`
Password string `json:"password"`
SSLMode string `json:"sslmode"`
TunnelInfo *sshtunnel.Config `json:"tunnel_info,omitempty"`
// SkipHostValidation is used to skip host validation during tests
SkipHostValidation bool `json:"skipHostValidation"`
UseLegacyMappings bool `json:"useLegacyMappings"`
}
func (c Config) ConnectionString() (string, error) {
tls, err := c.TLS()
if err != nil {
return "", fmt.Errorf("error while creating connecton string, %w", err)
}
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?tls=%s", c.User, c.Password, c.Host, c.Port, c.DBName, tls), nil
}
func (c Config) TLS() (string, error) {
switch c.SSLMode {
case "skip-verify", "false":
return c.SSLMode, nil
case "":
return "false", nil
default:
return "", fmt.Errorf("sslmode %s for mysql connection is not supported", c.SSLMode)
}
}
func (c *Config) Parse(input json.RawMessage) error {
err := json.Unmarshal(input, c)
if err != nil {
return err
}
if c.TunnelInfo == nil { // if tunnel info is not provided as a separate json object, try to parse it from the input
if c.TunnelInfo, err = sshtunnel.ParseInlineConfig(input); err != nil {
return err
}
}
if !c.SkipHostValidation {
return util.ValidateHost(c.Host)
}
return nil
}