-
Notifications
You must be signed in to change notification settings - Fork 7
/
get_config.go
106 lines (92 loc) · 2.23 KB
/
get_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
package aliacm
import (
"context"
"encoding/base64"
"errors"
"net/http"
"strings"
"github.com/xiaojiaoyu100/cast"
"github.com/xiaojiaoyu100/lizard/convert"
)
// GetConfigRequest 获取配置参数
type GetConfigRequest struct {
Tenant string `url:"tenant"`
DataID string `url:"dataId"`
Group string `url:"group"`
}
type GetConfigResponse struct {
Content []byte
DecryptContent []byte
}
// GetConfig 获取配置
func (d *Diamond) GetConfig(args *GetConfigRequest) (*GetConfigResponse, error) {
if len(args.Group) == 0 {
args.Group = DefaultGroup
}
if len(args.Tenant) == 0 {
args.Tenant = d.option.tenant
}
ip, err := d.QueryIP()
if err != nil {
return nil, err
}
header := make(http.Header)
if err := d.withSignature(args.Tenant, args.Group)(header); err != nil {
return nil, err
}
request := d.c.NewRequest().
WithTimeout(apiTimeout).
WithPath(acmConfig.String(ip)).
WithQueryParam(args).
WithHeader(header).
Get()
response, err := d.c.Do(context.TODO(), request)
if err != nil {
return nil, err
}
if !response.Success() {
return nil, errors.New(response.String())
}
config, err := d.getConfig(response, args.DataID)
if err != nil {
return nil, err
}
return config, nil
}
// getConfig 适配配置kms加密
func (d *Diamond) getConfig(response *cast.Response, dataID string) (*GetConfigResponse, error) {
config := &GetConfigResponse{
Content: response.Body(),
DecryptContent: response.Body(),
}
if d.kmsClient == nil {
return config, nil
}
body := convert.ByteToString(response.Body())
switch {
case strings.HasPrefix(dataID, "cipher-kms-aes-128-"):
dataKey, err := d.kmsDecrypt(response.Header().Get("Encrypted-Data-Key"))
if err != nil {
return nil, err
}
bodyByte, err := base64.StdEncoding.DecodeString(body)
if err != nil {
return nil, err
}
dataKeyByte, err := base64.StdEncoding.DecodeString(dataKey)
if err != nil {
return nil, err
}
config.DecryptContent, err = aesDecrypt(bodyByte, dataKeyByte)
if err != nil {
return nil, err
}
case strings.HasPrefix(dataID, "cipher-"):
configStr, err := d.kmsDecrypt(body)
if err != nil {
return nil, err
}
config.DecryptContent = convert.StringToByte(configStr)
}
return config, nil
}