Skip to content

Commit 1cc414f

Browse files
committed
feat(gm): support GM
1 parent 92ae73a commit 1cc414f

File tree

162 files changed

+34415
-7
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+34415
-7
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/spf13/cobra v1.6.1
1515
github.com/spf13/pflag v1.0.5
1616
github.com/stretchr/testify v1.8.0
17+
github.com/tjfoc/gmsm v1.4.2-0.20220114090716-36b992c51540
1718
golang.org/x/net v0.17.0
1819
golang.org/x/sync v0.4.0
1920
golang.org/x/sys v0.14.0
@@ -54,6 +55,7 @@ require (
5455
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
5556
github.com/pmezard/go-difflib v1.0.0 // indirect
5657
github.com/spf13/afero v1.8.0 // indirect
58+
golang.org/x/crypto v0.14.0 // indirect
5759
golang.org/x/oauth2 v0.11.0 // indirect
5860
golang.org/x/term v0.14.0 // indirect
5961
golang.org/x/text v0.14.0 // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
319319
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
320320
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
321321
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
322+
github.com/tjfoc/gmsm v1.4.2-0.20220114090716-36b992c51540 h1:Q7nxhP4rDahaXbLofX2fRX1dcEoQRvlJA0Hd2hGgh9k=
323+
github.com/tjfoc/gmsm v1.4.2-0.20220114090716-36b992c51540/go.mod h1:j4INPkHWMrhJb38G+J6W4Tw0AbuN8Thu3PbdVYhVcTE=
322324
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
323325
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
324326
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -340,8 +342,11 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
340342
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
341343
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
342344
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
345+
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
343346
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
344347
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
348+
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
349+
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
345350
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
346351
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
347352
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -405,6 +410,7 @@ golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/
405410
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
406411
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
407412
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
413+
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
408414
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
409415
golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
410416
golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=

pkg/config/cloud.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
"github.com/chnsz/golangsdk/openstack"
1010

1111
"github.com/huaweicloud/huaweicloud-csi-driver/pkg/utils"
12+
13+
"github.com/tjfoc/gmsm/gmtls"
14+
"github.com/tjfoc/gmsm/x509"
1215
)
1316

1417
const (
@@ -26,6 +29,7 @@ type CloudCredentials struct {
2629
SecretKey string `gcfg:"secret-key"`
2730
ProjectID string `gcfg:"project-id"`
2831
Idc bool `gcfg:"idc"`
32+
GMSupport bool `gcfg:"gm-support"`
2933
}
3034

3135
Vpc struct {
@@ -82,8 +86,8 @@ func newServiceClient(cc *CloudCredentials, catalogName, region string) (*golang
8286
if !ok {
8387
return nil, fmt.Errorf("service type %s is invalid or not supportted", catalogName)
8488
}
85-
8689
client := cc.CloudClient
90+
8791
// update ProjectID and region in ProviderClient
8892
clone := new(golangsdk.ProviderClient)
8993
*clone = *client
@@ -137,27 +141,45 @@ func (c *CloudCredentials) newCloudClient() error {
137141
return err
138142
}
139143

140-
transport := &http.Transport{
144+
defaultTransport := &http.Transport{
141145
Proxy: http.ProxyFromEnvironment,
142146
TLSClientConfig: &tls.Config{
143147
MinVersion: tls.VersionTLS12,
144148
InsecureSkipVerify: c.Global.Insecure,
145149
},
146150
}
147151

152+
wrappers := []utils.WrapperFunc{
153+
utils.NewLogRoundTripper(),
154+
}
155+
if c.Global.GMSupport {
156+
gmCfg := &gmtls.Config{
157+
GMSupport: &gmtls.GMSupport{WorkMode: gmtls.ModeAutoSwitch},
158+
InsecureSkipVerify: true,
159+
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
160+
for _, v := range rawCerts {
161+
_, err = x509.ParseCertificate(v)
162+
if err != nil {
163+
return err
164+
}
165+
}
166+
return nil
167+
},
168+
}
169+
wrappers = append(wrappers, utils.NewGMRoundTripper(gmCfg))
170+
}
171+
148172
client.HTTPClient = http.Client{
149-
Transport: &utils.LogRoundTripper{
150-
Rt: transport,
151-
},
173+
Transport: utils.Wrappers(wrappers...)(defaultTransport),
152174
}
153175

154-
err = openstack.Authenticate(client, ao)
155-
if err != nil {
176+
if err := openstack.Authenticate(client, ao); err != nil {
156177
return err
157178
}
158179

159180
c.CloudClient = client
160181
c.CloudClient.UserAgent.Prepend(UserAgent)
182+
161183
return nil
162184
}
163185

pkg/utils/gm_round_tripper.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package utils
2+
3+
import (
4+
"context"
5+
"crypto/tls"
6+
"net"
7+
"net/http"
8+
"time"
9+
10+
"github.com/tjfoc/gmsm/gmtls"
11+
)
12+
13+
func NewGMRoundTripper(cfg *gmtls.Config) WrapperFunc {
14+
return func(inner http.RoundTripper) http.RoundTripper {
15+
return &http.Transport{
16+
Proxy: http.ProxyFromEnvironment,
17+
TLSClientConfig: &tls.Config{
18+
InsecureSkipVerify: cfg.InsecureSkipVerify,
19+
},
20+
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
21+
dialer := &net.Dialer{}
22+
conn, err := gmtls.DialWithDialer(dialer, network, addr, cfg)
23+
if err != nil {
24+
return nil, err
25+
}
26+
return conn, nil
27+
},
28+
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
29+
dialer := &net.Dialer{
30+
Timeout: 10 * time.Second,
31+
KeepAlive: 60 * time.Second,
32+
}
33+
conn, err := gmtls.DialWithDialer(dialer, network, addr, cfg)
34+
if err != nil {
35+
return nil, err
36+
}
37+
return conn, nil
38+
},
39+
TLSHandshakeTimeout: 10 * time.Second,
40+
IdleConnTimeout: 30 * time.Second,
41+
}
42+
}
43+
}

pkg/utils/gm_round_tripper_test.go

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package utils
2+
3+
import (
4+
"net/http"
5+
"testing"
6+
7+
"github.com/tjfoc/gmsm/gmtls"
8+
"github.com/tjfoc/gmsm/x509"
9+
)
10+
11+
func TestNewGMRoundTripper(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
description string
15+
cfg *gmtls.Config
16+
request *http.Request
17+
expected bool
18+
}{
19+
{
20+
name: "test1",
21+
description: "support both GM and no-GM encryption algorithm",
22+
cfg: &gmtls.Config{
23+
InsecureSkipVerify: true,
24+
},
25+
request: httpNewRequest("GET", "https://sm2test.ovssl.cn", nil, t),
26+
expected: false,
27+
},
28+
{
29+
name: "test2",
30+
description: "support both GM and no-GM encryption algorithm",
31+
cfg: &gmtls.Config{
32+
GMSupport: &gmtls.GMSupport{WorkMode: gmtls.ModeAutoSwitch},
33+
InsecureSkipVerify: true,
34+
},
35+
request: httpNewRequest("GET", "https://sm2test.ovssl.cn", nil, t),
36+
expected: false,
37+
},
38+
{
39+
name: "test3",
40+
description: "support both GM and no-GM encryption algorithm",
41+
cfg: &gmtls.Config{
42+
GMSupport: &gmtls.GMSupport{WorkMode: gmtls.ModeGMSSLOnly},
43+
InsecureSkipVerify: true,
44+
},
45+
request: httpNewRequest("GET", "https://sm2test.ovssl.cn", nil, t),
46+
expected: false,
47+
},
48+
{
49+
name: "test4",
50+
description: "support both GM and no-GM encryption algorithm",
51+
cfg: &gmtls.Config{
52+
GMSupport: &gmtls.GMSupport{WorkMode: gmtls.ModeAutoSwitch},
53+
InsecureSkipVerify: true,
54+
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
55+
for _, v := range rawCerts {
56+
_, err := x509.ParseCertificate(v)
57+
if err != nil {
58+
return err
59+
}
60+
}
61+
return nil
62+
},
63+
},
64+
request: httpNewRequest("GET", "https://sm2test.ovssl.cn", nil, t),
65+
expected: false,
66+
},
67+
{
68+
name: "test5",
69+
description: "support both GM and no-GM encryption algorithm",
70+
cfg: &gmtls.Config{
71+
GMSupport: &gmtls.GMSupport{WorkMode: gmtls.ModeGMSSLOnly},
72+
InsecureSkipVerify: true,
73+
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
74+
for _, v := range rawCerts {
75+
_, err := x509.ParseCertificate(v)
76+
if err != nil {
77+
return err
78+
}
79+
}
80+
return nil
81+
},
82+
},
83+
request: httpNewRequest("GET", "https://sm2test.ovssl.cn", nil, t),
84+
expected: false,
85+
},
86+
87+
{
88+
name: "test6",
89+
description: "don not support GM",
90+
cfg: &gmtls.Config{
91+
InsecureSkipVerify: true,
92+
},
93+
request: httpNewRequest("GET", "https://baidu.com", nil, t),
94+
expected: true,
95+
},
96+
{
97+
name: "test7",
98+
description: "don not support GM",
99+
cfg: &gmtls.Config{
100+
GMSupport: &gmtls.GMSupport{WorkMode: gmtls.ModeAutoSwitch},
101+
InsecureSkipVerify: true,
102+
},
103+
request: httpNewRequest("GET", "https://baidu.com", nil, t),
104+
expected: true,
105+
},
106+
{
107+
name: "test8",
108+
description: "don not support GM",
109+
cfg: &gmtls.Config{
110+
GMSupport: &gmtls.GMSupport{WorkMode: gmtls.ModeGMSSLOnly},
111+
InsecureSkipVerify: true,
112+
},
113+
request: httpNewRequest("GET", "https://baidu.com", nil, t),
114+
expected: true,
115+
},
116+
{
117+
name: "test9",
118+
description: "don not support GM",
119+
cfg: &gmtls.Config{
120+
GMSupport: &gmtls.GMSupport{WorkMode: gmtls.ModeAutoSwitch},
121+
InsecureSkipVerify: true,
122+
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
123+
for _, v := range rawCerts {
124+
_, err := x509.ParseCertificate(v)
125+
if err != nil {
126+
return err
127+
}
128+
}
129+
return nil
130+
},
131+
},
132+
request: httpNewRequest("GET", "https://baidu.com", nil, t),
133+
expected: true,
134+
},
135+
{
136+
name: "test10",
137+
description: "don not support GM",
138+
cfg: &gmtls.Config{
139+
GMSupport: &gmtls.GMSupport{WorkMode: gmtls.ModeGMSSLOnly},
140+
InsecureSkipVerify: true,
141+
VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
142+
for _, v := range rawCerts {
143+
_, err := x509.ParseCertificate(v)
144+
if err != nil {
145+
return err
146+
}
147+
}
148+
return nil
149+
},
150+
},
151+
request: httpNewRequest("GET", "https://baidu.com", nil, t),
152+
expected: true,
153+
},
154+
}
155+
156+
for _, testCase := range tests {
157+
t.Run(testCase.name, func(t *testing.T) {
158+
wrapper := NewGMRoundTripper(testCase.cfg)
159+
wrappedTransport := wrapper(http.DefaultTransport)
160+
client := &http.Client{Transport: wrappedTransport}
161+
162+
resp, err := client.Do(testCase.request)
163+
164+
if testCase.expected && err == nil {
165+
t.Errorf("expected error but got none")
166+
}
167+
if !testCase.expected && err != nil {
168+
t.Errorf("expected: %v, but got err: %v", testCase.expected, err)
169+
}
170+
171+
if resp != nil && resp.Body != nil {
172+
resp.Body.Close()
173+
}
174+
})
175+
}
176+
}

pkg/utils/logroundtripper.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ var (
3737
serverGRPCEndpointCallCounter uint64
3838
)
3939

40+
func NewLogRoundTripper() WrapperFunc {
41+
return func(rt http.RoundTripper) http.RoundTripper {
42+
return &LogRoundTripper{Rt: rt}
43+
}
44+
}
45+
4046
// LogRoundTripper satisfies the http.RoundTripper interface and is used to
4147
// customize the default http client RoundTripper to allow for logging.
4248
type LogRoundTripper struct {

pkg/utils/transport.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package utils
2+
3+
import (
4+
"net/http"
5+
)
6+
7+
type WrapperFunc func(rt http.RoundTripper) http.RoundTripper
8+
9+
func Wrappers(fns ...WrapperFunc) WrapperFunc {
10+
if len(fns) == 0 {
11+
return nil
12+
}
13+
// optimize the common case of wrapping a possibly nil tr wrapper
14+
// with an additional wrapper
15+
if len(fns) == 2 && fns[0] == nil {
16+
return fns[1]
17+
}
18+
return func(rt http.RoundTripper) http.RoundTripper {
19+
base := rt
20+
for _, fn := range fns {
21+
if fn != nil {
22+
base = fn(base)
23+
}
24+
}
25+
return base
26+
}
27+
}

0 commit comments

Comments
 (0)