mosn

Форк
0
/
tls_extend_test.go 
204 строки · 4.7 Кб
1
//go:build MOSNTest
2
// +build MOSNTest
3

4
package tls
5

6
import (
7
	"fmt"
8
	"regexp"
9
	"testing"
10

11
	"mosn.io/api"
12
	"mosn.io/mosn/pkg/protocol"
13
	. "mosn.io/mosn/test/framework"
14
	"mosn.io/mosn/test/lib"
15
	"mosn.io/mosn/test/lib/extends"
16
	"mosn.io/mosn/test/lib/http"
17
)
18

19
func TestTLSExtendMode(t *testing.T) {
20
	Scenario(t, "protocol http1 with tls extend", func() {
21
		lib.InitMosn(CreateTlsConfig(protocol.HTTP1, protocol.HTTP1), lib.CreateConfig(`{
22
			"protocol":"Http1",
23
			"config": {
24
				"address": "127.0.0.1:8080"
25
			}
26
		}`))
27
		Case("http1 with tls extend", func() {
28
			client := lib.CreateClient("Http1", &http.HttpClientConfig{
29
				TargetAddr: "127.0.0.1:2045",
30
				Verify: &http.VerifyConfig{
31
					ExpectedStatusCode: 200,
32
				},
33
			})
34
			Verify(client.SyncCall(), Equal, true)
35
		})
36
	})
37

38
	Scenario(t, "protocol http2 with tls extend", func() {
39
		lib.InitMosn(CreateTlsConfig(protocol.HTTP2, protocol.HTTP2), lib.CreateConfig(`{
40
			"protocol":"Http2",
41
			"config": {
42
				"address": "127.0.0.1:8080"
43
			}
44
		}`))
45
		Case("http2 with tls extend", func() {
46
			client := lib.CreateClient("Http2", &http.HttpClientConfig{
47
				TargetAddr: "127.0.0.1:2045",
48
				Verify: &http.VerifyConfig{
49
					ExpectedStatusCode: 200,
50
				},
51
			})
52
			Verify(client.SyncCall(), Equal, true)
53
		})
54
	})
55

56
	Scenario(t, "protocol http1 to http2 with tls extend", func() {
57
		lib.InitMosn(CreateTlsConfig(protocol.HTTP1, protocol.HTTP2), lib.CreateConfig(`{
58
			"protocol":"Http1",
59
			"config": {
60
				"address": "127.0.0.1:8080"
61
			}
62
		}`))
63
		Case("http1 to http2 with tls extend", func() {
64
			client := lib.CreateClient("Http1", &http.HttpClientConfig{
65
				TargetAddr: "127.0.0.1:2045",
66
				Verify: &http.VerifyConfig{
67
					ExpectedStatusCode: 200,
68
				},
69
			})
70
			Verify(client.SyncCall(), Equal, true)
71
		})
72
	})
73

74
	Scenario(t, "protocol http2 to http1 with tls extend", func() {
75
		lib.InitMosn(CreateTlsConfig(protocol.HTTP2, protocol.HTTP1), lib.CreateConfig(`{
76
			"protocol":"Http2",
77
			"config": {
78
				"address": "127.0.0.1:8080"
79
			}
80
		}`))
81
		Case("http2 with tls extend", func() {
82
			client := lib.CreateClient("Http2", &http.HttpClientConfig{
83
				TargetAddr: "127.0.0.1:2045",
84
				Verify: &http.VerifyConfig{
85
					ExpectedStatusCode: 200,
86
				},
87
			})
88
			Verify(client.SyncCall(), Equal, true)
89
		})
90
	})
91

92
}
93

94
var trimStreamFilters = regexp.MustCompile(`"stream_filters":\[.*\]`)
95

96
func CreateTlsConfig(original api.ProtocolName, trans api.ProtocolName) string {
97
	if original == trans {
98
		newTmpl := trimStreamFilters.ReplaceAllString(ConfigTlsExtendConfigTmpl, `"stream_filters":[]`)
99
		return fmt.Sprintf(newTmpl, original, original)
100
	}
101
	filterin, filtermid := extends.GetTransFilter(original, trans)
102
	return fmt.Sprintf(ConfigTlsExtendConfigTmpl, original, filterin, trans, filtermid)
103
}
104

105
const ConfigTlsExtendConfigTmpl = `{
106
	"servers":[
107
		{
108
			"default_log_path":"stdout",
109
			"default_log_level": "ERROR",
110
			"routers": [
111
				{
112
					"router_config_name":"router_to_upstream",
113
					"virtual_hosts":[{
114
						"name":"server_hosts",
115
						"domains": ["*"],
116
						"routers": [
117
							{
118
								"match":{"prefix":"/"},
119
								"route":{"cluster_name":"upstream"}
120
							}
121
						]
122
					}]
123
				},
124
				{
125
					"router_config_name":"router_to_mesh",
126
					"virtual_hosts":[{
127
						"name":"server_hosts2",
128
						"domains": ["*"],
129
						"routers": [
130
							{
131
								"match":{"prefix":"/"},
132
								"route":{"cluster_name":"mesh"}
133
							}
134
						]
135
					}]
136
				}
137
			],
138
			"listeners":[
139
				{
140
					"address":"127.0.0.1:2045",
141
					"bind_port": true,
142
					"filter_chains": [{
143
						"filters": [
144
							{
145
								"type": "proxy",
146
								"config": {
147
									"downstream_protocol": "%s",
148
									"upstream_protocol": "Auto",
149
									"router_config_name":"router_to_mesh"
150
								}
151
							}
152
						]
153
					}],
154
					"stream_filters":[{"type": "transcoder","config": {"type": "%s"}}]
155
				},
156
				{
157
					"address":"127.0.0.1:2046",
158
					"bind_port": true,
159
					"filter_chains": [{
160
						"tls_context":{
161
							"status": true,
162
							"type": "mosn-integrate-test-tls"
163
						},
164
						"filters": [
165
							{
166
								"type": "proxy",
167
								"config": {
168
									"downstream_protocol": "%s",
169
									"upstream_protocol": "Auto",
170
									"router_config_name":"router_to_upstream"
171
								}
172
							}
173
						]
174
					}],
175
					"stream_filters":[{"type": "transcoder","config": {"type": "%s"}}]
176
				}
177
			]
178
		}
179
	],
180
	"cluster_manager":{
181
		"clusters":[
182
			{
183
				"name": "mesh",
184
				"type": "SIMPLE",
185
				"lb_type": "LB_ROUNDROBIN",
186
				"tls_context": {
187
					"status": true,
188
					"type": "mosn-integrate-test-tls"
189
				},
190
				"hosts":[
191
					{"address":"127.0.0.1:2046"}
192
				]
193
			},
194
			{
195
				"name": "upstream",
196
				"type": "SIMPLE",
197
				"lb_type": "LB_ROUNDROBIN",
198
				"hosts":[
199
					{"address":"127.0.0.1:8080"}
200
				]
201
			}
202
		]
203
	}
204
}`
205

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.