/
config.go
147 lines (126 loc) · 3.69 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
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
/*
Copyright 2022-present The Ztalab Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/tidwall/gjson"
"gopkg.in/yaml.v3"
)
const (
YamlStorage = "yaml"
VaultStorage = "vault"
)
var DefaultConfig *Config
type Config struct {
Server ServerS `json:"server" yaml:"server"`
Confidential ConfidentialS `json:"confidential" yaml:"confidential"`
FakeIdentity FakeIdentityS `json:"fakeIdentity" yaml:"fakeIdentity"`
}
type ServerS struct {
Addr string `json:"addr" yaml:"addr"`
}
type ConfidentialS struct {
Storage string `json:"storage" yaml:"storage"`
Vault VaultS `json:"vault" yaml:"vault"`
Yaml Mysql `json:"yaml" yaml:"yaml"`
}
type VaultS struct {
Addr string `json:"addr" yaml:"addr"`
Token string `json:"token" yaml:"token"`
DataPath string `json:"dataPath" yaml:"dataPath"`
}
type Mysql struct {
Host string `json:"host" yaml:"host"`
Port int `json:"port" yaml:"port"`
Username string `json:"username" yaml:"username"`
Password string `json:"password" yaml:"password"`
DBName string `json:"dbname" yaml:"dbname"`
}
type FakeIdentityS struct {
Username string `json:"username" yaml:"username"`
Password string `json:"password" yaml:"password"`
}
func InitConfig(path string) {
if DefaultConfig != nil {
return
}
DefaultConfig = &Config{}
f, err := os.Open(path)
if err != nil {
panic(err)
}
if err = yaml.NewDecoder(f).Decode(DefaultConfig); err != nil {
panic(err)
}
switch DefaultConfig.Confidential.Storage {
case YamlStorage:
case VaultStorage:
default:
panic("Unsupported storage type!")
}
if len(DefaultConfig.FakeIdentity.Username) == 0 || len(DefaultConfig.FakeIdentity.Password) == 0 {
panic("fake-identity empty")
}
}
func Get() *Config {
return DefaultConfig
}
func GetRemoteMysql() *Mysql {
if DefaultConfig.Confidential.Storage == YamlStorage {
return &DefaultConfig.Confidential.Yaml
}
return getVaultData()
}
func getVaultData() *Mysql {
url := fmt.Sprintf("%s/v1/secret/data/%s",
strings.TrimRight(DefaultConfig.Confidential.Vault.Addr, "/"),
DefaultConfig.Confidential.Vault.DataPath)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err)
}
req.Header.Set("X-Vault-Token", DefaultConfig.Confidential.Vault.Token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(fmt.Errorf("request vault err: %v", err))
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(fmt.Errorf("read vault response body err: %v", err))
}
res := gjson.GetBytes(body, "errors")
if len(res.String()) > 0 {
panic(fmt.Errorf("request vault err: %s", res.String()))
}
res = gjson.GetBytes(body, "data.data")
if len(res.Map()) == 0 {
panic(fmt.Errorf("request vault err: %s", res.String()))
}
result := res.Map()
mysql := &Mysql{
Host: result["host"].String(),
Port: int(result["port"].Int()),
Username: result["username"].String(),
Password: result["password"].String(),
DBName: result["dbname"].String(),
}
if len(mysql.Host) == 0 || mysql.Port <= 0 || len(mysql.Username) == 0 || len(mysql.Password) == 0 {
panic(fmt.Errorf("mysql config err: %s", res.String()))
}
return mysql
}