forked from go-chassis/go-chassis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls.go
executable file
·192 lines (164 loc) · 5.02 KB
/
tls.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package tls
import (
"crypto/tls"
"errors"
"fmt"
"strconv"
"strings"
"github.com/ServiceComb/go-chassis/core/common"
"github.com/ServiceComb/go-chassis/core/config"
secCommon "github.com/ServiceComb/go-chassis/security/common"
"github.com/ServiceComb/go-chassis/third_party/forked/kubernetes/pkg/util/sets"
)
var errSSLConfigNotExist = errors.New("No SSL config")
var useDefaultSslTag = sets.NewString("registry.Consumer.", "configcenter.Consumer.", "monitor.Consumer.")
func hasDefaultSslTag(tag string) bool {
if len(tag) == 0 {
return false
}
if useDefaultSslTag.Has(tag) {
return true
}
return false
}
func getDefaultSslConfigMap() map[string]string {
cipherSuits := []string{}
for k := range secCommon.TLSCipherSuiteMap {
cipherSuits = append(cipherSuits, k)
}
cipherSuitesKey := strings.Join(cipherSuits, ",")
defaultSslConfigMap := map[string]string{
common.SslCipherPluginKey: "default",
common.SslVerifyPeerKey: "false",
common.SslCipherSuitsKey: cipherSuitesKey,
common.SslProtocolKey: "TLSv1.2",
common.SslCaFileKey: "",
common.SslCertFileKey: "",
common.SslKeyFileKey: "",
common.SslCertPwdFileKey: "",
}
return defaultSslConfigMap
}
func getSSLConfigMap(tag string) map[string]string {
sslConfigMap := config.GlobalDefinition.Ssl
defaultSslConfigMap := getDefaultSslConfigMap()
result := make(map[string]string)
sslSet := false
if tag != "" {
tag = tag + `.`
}
for k, v := range defaultSslConfigMap {
// 使用默认配置
result[k] = v
// 若配置了全局配置项,则覆盖默认配置
if r, exist := sslConfigMap[k]; exist && r != "" {
result[k] = r
sslSet = true
}
// 若配置了指定交互方的配置项,则覆盖全局配置
keyWithTag := tag + k
if v, exist := sslConfigMap[keyWithTag]; exist && v != "" {
result[k] = v
sslSet = true
}
}
// 未设置ssl 且不提供内部默认ss配置 返回空字典
if !sslSet && !hasDefaultSslTag(tag) {
return make(map[string]string)
}
return result
}
func parseSSLConfig(sslConfigMap map[string]string) (*secCommon.SSLConfig, error) {
sslConfig := &secCommon.SSLConfig{}
var err error
sslConfig.CipherPlugin = sslConfigMap[common.SslCipherPluginKey]
sslConfig.VerifyPeer, err = strconv.ParseBool(sslConfigMap[common.SslVerifyPeerKey])
if err != nil {
return nil, err
}
sslConfig.CipherSuites, err = secCommon.ParseSSLCipherSuites(sslConfigMap[common.SslCipherSuitsKey])
if err != nil {
return nil, err
}
if len(sslConfig.CipherSuites) == 0 {
return nil, fmt.Errorf("No valid cipher")
}
sslConfig.MinVersion, err = secCommon.ParseSSLProtocol(sslConfigMap[common.SslProtocolKey])
if err != nil {
return nil, err
}
sslConfig.MaxVersion = secCommon.TLSVersionMap["TLSv1.2"]
sslConfig.CAFile = sslConfigMap[common.SslCaFileKey]
sslConfig.CertFile = sslConfigMap[common.SslCertFileKey]
sslConfig.KeyFile = sslConfigMap[common.SslKeyFileKey]
sslConfig.CertPWDFile = sslConfigMap[common.SslCertPwdFileKey]
return sslConfig, nil
}
// GetSSLConfigByService get ssl configurations based on service
func GetSSLConfigByService(svcName, protocol, svcType string) (*secCommon.SSLConfig, error) {
tag, err := generateSSLTag(svcName, protocol, svcType)
if err != nil {
return nil, err
}
sslConfigMap := getSSLConfigMap(tag)
if len(sslConfigMap) == 0 {
return nil, errSSLConfigNotExist
}
sslConfig, err := parseSSLConfig(sslConfigMap)
if err != nil {
return nil, err
}
return sslConfig, nil
}
// GetDefaultSSLConfig get default ssl configurations
func GetDefaultSSLConfig() *secCommon.SSLConfig {
sslConfigMap := getDefaultSslConfigMap()
sslConfig, _ := parseSSLConfig(sslConfigMap)
return sslConfig
}
// generateSSLTag generate ssl tag
func generateSSLTag(svcName, protocol, svcType string) (string, error) {
var tag string
if svcName != "" {
tag = tag + "." + svcName
}
if protocol != "" {
tag = tag + "." + protocol
}
if tag == "" {
return "", errors.New("Service name and protocol can't be empty both")
}
switch svcType {
case common.Consumer, common.Provider:
tag = tag + "." + svcType
default:
return "", fmt.Errorf("Service type not support: %s, must be: %s|%s",
svcType, common.Provider, common.Consumer)
}
return tag[1:], nil
}
// GetTLSConfigByService get tls configurations based on service
func GetTLSConfigByService(svcName, protocol, svcType string) (*tls.Config, *secCommon.SSLConfig, error) {
sslConfig, err := GetSSLConfigByService(svcName, protocol, svcType)
if err != nil {
return nil, nil, err
}
var tlsConfig *tls.Config
switch svcType {
case common.Provider:
tlsConfig, err = secCommon.GetServerTLSConfig(sslConfig)
case common.Consumer:
tlsConfig, err = secCommon.GetClientTLSConfig(sslConfig)
default:
err = fmt.Errorf("service type not support: %s, must be: %s|%s",
svcType, common.Provider, common.Consumer)
}
if err != nil {
return nil, sslConfig, err
}
return tlsConfig, sslConfig, nil
}
// IsSSLConfigNotExist check the status of ssl configurations
func IsSSLConfigNotExist(e error) bool {
return e == errSSLConfigNotExist
}