forked from istio/istio
/
gateway_test.go
344 lines (317 loc) · 10.4 KB
/
gateway_test.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Copyright Istio 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 client_test
import (
"context"
"encoding/base64"
"fmt"
"net"
"testing"
corev2 "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
listener "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
route "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
hcm "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3"
discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v2"
"github.com/envoyproxy/go-control-plane/pkg/cache/types"
"github.com/envoyproxy/go-control-plane/pkg/cache/v2"
xds "github.com/envoyproxy/go-control-plane/pkg/server/v2"
"github.com/envoyproxy/go-control-plane/pkg/wellknown"
"google.golang.org/grpc"
meshconfig "istio.io/api/mesh/v1alpha1"
mixerpb "istio.io/api/mixer/v1"
"istio.io/istio/mixer/test/client/env"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pilot/pkg/networking"
"istio.io/istio/pilot/pkg/networking/plugin"
"istio.io/istio/pilot/pkg/networking/plugin/mixer"
pilotutil "istio.io/istio/pilot/pkg/networking/util"
"istio.io/istio/pkg/config/host"
"istio.io/istio/pkg/config/labels"
)
const (
envoyConf = `
admin:
access_log_path: {{.AccessLogPath}}
address:
socket_address:
address: 127.0.0.1
port_value: {{.Ports.AdminPort}}
node:
id: id
cluster: unknown
metadata:
# these two must come together and they need to be set
NODE_UID: pod.ns
NODE_NAMESPACE: ns
dynamic_resources:
lds_config: { ads: {} }
ads_config:
api_type: GRPC
grpc_services:
envoy_grpc:
cluster_name: xds
static_resources:
clusters:
- name: xds
http2_protocol_options: {}
connect_timeout: 5s
type: STATIC
hosts:
- socket_address:
address: 127.0.0.1
port_value: {{.Ports.DiscoveryPort}}
- name: "outbound|||svc.ns3"
connect_timeout: 5s
type: STATIC
hosts:
- socket_address:
address: 127.0.0.1
port_value: {{.Ports.BackendPort}}
- name: "outbound|9091||mixer_server"
http2_protocol_options: {}
connect_timeout: 5s
type: STATIC
hosts:
- socket_address:
address: 127.0.0.1
port_value: {{.Ports.MixerPort}}
`
checkAttributesOkOutbound = `
{
"connection.mtls": false,
"origin.ip": "[127 0 0 1]",
"context.protocol": "http",
"context.reporter.kind": "outbound",
"context.reporter.uid": "kubernetes://pod.ns",
"destination.service.host": "svc.ns3",
"destination.service.name": "svc",
"destination.service.namespace": "ns3",
"destination.service.uid": "istio://ns3/services/svc",
"source.uid": "%s",
"source.namespace": "ns",
"request.headers": {
":method": "GET",
":path": "/echo",
":authority": "*",
"x-forwarded-proto": "http",
"x-request-id": "*"
},
"request.host": "*",
"request.path": "/echo",
"request.time": "*",
"request.useragent": "Go-http-client/1.1",
"request.method": "GET",
"request.scheme": "http",
"request.url_path": "/echo"
}
`
reportAttributesOkOutbound = `
{
"connection.mtls": false,
"origin.ip": "[127 0 0 1]",
"context.protocol": "http",
"context.proxy_error_code": "-",
"context.reporter.kind": "outbound",
"context.reporter.uid": "kubernetes://pod.ns",
"destination.ip": "[127 0 0 1]",
"destination.port": "*",
"destination.service.host": "svc.ns3",
"destination.service.name": "svc",
"destination.service.namespace": "ns3",
"destination.service.uid": "istio://ns3/services/svc",
"source.uid": "%s",
"source.namespace": "ns",
"check.cache_hit": false,
"quota.cache_hit": false,
"request.headers": {
":method": "GET",
":path": "/echo",
":authority": "*",
"x-forwarded-proto": "http",
"x-istio-attributes": "-",
"x-request-id": "*"
},
"request.host": "*",
"request.path": "/echo",
"request.time": "*",
"request.useragent": "Go-http-client/1.1",
"request.method": "GET",
"request.scheme": "http",
"request.size": 0,
"request.total_size": "*",
"request.url_path": "/echo",
"response.time": "*",
"response.size": 0,
"response.duration": "*",
"response.code": 200,
"response.headers": {
"date": "*",
"content-length": "0",
":status": "200",
"server": "envoy"
},
"response.total_size": "*"
}`
)
func TestGateway(t *testing.T) {
s := env.NewTestSetup(env.GatewayTest, t)
s.EnvoyTemplate = envoyConf
grpcServer := grpc.NewServer()
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", s.Ports().DiscoveryPort))
if err != nil {
t.Fatal(err)
}
snapshots := cache.NewSnapshotCache(true, mock{}, nil)
_ = snapshots.SetSnapshot(id, makeSnapshot(s, t))
server := xds.NewServer(context.Background(), snapshots, nil)
discovery.RegisterAggregatedDiscoveryServiceServer(grpcServer, server)
go func() {
_ = grpcServer.Serve(lis)
}()
defer grpcServer.Stop()
s.SetMixerSourceUID("pod.ns")
if err := s.SetUp(); err != nil {
t.Fatalf("Failed to setup test: %v", err)
}
defer s.TearDown()
s.WaitEnvoyReady()
// verify that bootstrap source.uid is present
if _, _, err := env.HTTPGet(fmt.Sprintf("http://localhost:%d/echo", s.Ports().ClientProxyPort)); err != nil {
t.Errorf("Failed in request: %v", err)
}
sourceUID := "kubernetes://pod.ns"
s.VerifyCheck("http-outbound", fmt.Sprintf(checkAttributesOkOutbound, sourceUID))
s.VerifyReport("http", fmt.Sprintf(reportAttributesOkOutbound, sourceUID))
// Issues a GET echo request with 0 size body, forward some random source.uid
attrs := mixerpb.Attributes{
Attributes: map[string]*mixerpb.Attributes_AttributeValue{
"source.uid": {Value: &mixerpb.Attributes_AttributeValue_StringValue{
StringValue: "in-mesh-app",
}},
},
}
out, _ := attrs.Marshal()
headers := map[string]string{
"x-istio-attributes": base64.StdEncoding.EncodeToString(out),
}
if _, _, err := env.HTTPGetWithHeaders(fmt.Sprintf("http://localhost:%d/echo", s.Ports().ClientProxyPort), headers); err != nil {
t.Errorf("Failed in request: %v", err)
}
// verify that sourceUID is not overridden by forwarded attributes.
s.VerifyCheck("http-outbound", fmt.Sprintf(checkAttributesOkOutbound, sourceUID))
s.VerifyReport("http", fmt.Sprintf(reportAttributesOkOutbound, sourceUID))
}
type mock struct{}
func (mock) ID(*corev2.Node) string {
return id
}
func (mock) GetProxyServiceInstances(_ *model.Proxy) ([]*model.ServiceInstance, error) {
return nil, nil
}
func (mock) GetProxyWorkloadLabels(proxy *model.Proxy) (labels.Collection, error) {
return nil, nil
}
func (mock) GetService(_ host.Name) (*model.Service, error) { return nil, nil }
func (mock) InstancesByPort(_ *model.Service, _ int, _ labels.Collection) ([]*model.ServiceInstance, error) {
return nil, nil
}
func (mock) Services() ([]*model.Service, error) { return nil, nil }
func (mock) GetIstioServiceAccounts(_ *model.Service, ports []int) []string { return nil }
const (
id = "id"
)
var (
svc = model.Service{
Hostname: "svc.ns3",
Attributes: model.ServiceAttributes{
Name: "svc",
Namespace: "ns3",
UID: "istio://ns3/services/svc",
},
}
pushContext = model.PushContext{
ServiceByHostnameAndNamespace: map[host.Name]map[string]*model.Service{
host.Name("svc.ns3"): {
"ns3": &svc,
},
},
Mesh: &meshconfig.MeshConfig{
MixerCheckServer: "mixer_server:9091",
MixerReportServer: "mixer_server:9091",
},
ServiceDiscovery: mock{},
}
clientParams = plugin.InputParams{
ListenerProtocol: networking.ListenerProtocolHTTP,
Node: &model.Proxy{
ID: "pod.ns",
Type: model.Router,
Metadata: &model.NodeMetadata{},
},
Service: &svc,
Push: &pushContext,
}
)
func makeRoute(cluster string) *route.RouteConfiguration {
return &route.RouteConfiguration{
Name: cluster,
VirtualHosts: []*route.VirtualHost{{
Name: cluster,
Domains: []string{"*"},
Routes: []*route.Route{{
Match: &route.RouteMatch{PathSpecifier: &route.RouteMatch_Prefix{Prefix: "/"}},
Action: &route.Route_Route{Route: &route.RouteAction{
ClusterSpecifier: &route.RouteAction_Cluster{Cluster: cluster},
}},
}},
}},
}
}
func makeListener(port uint16, route string) (*listener.Listener, *hcm.HttpConnectionManager) {
return &listener.Listener{
Name: route,
Address: &core.Address{Address: &core.Address_SocketAddress{SocketAddress: &core.SocketAddress{
Address: "127.0.0.1",
PortSpecifier: &core.SocketAddress_PortValue{PortValue: uint32(port)}}}},
}, &hcm.HttpConnectionManager{
CodecType: hcm.HttpConnectionManager_AUTO,
StatPrefix: route,
RouteSpecifier: &hcm.HttpConnectionManager_Rds{
Rds: &hcm.Rds{RouteConfigName: route, ConfigSource: &core.ConfigSource{
ConfigSourceSpecifier: &core.ConfigSource_Ads{Ads: &core.AggregatedConfigSource{}},
}},
},
HttpFilters: []*hcm.HttpFilter{{Name: wellknown.Router}},
}
}
func makeSnapshot(s *env.TestSetup, t *testing.T) cache.Snapshot {
clientListener, clientManager := makeListener(s.Ports().ClientProxyPort, "outbound|||svc.ns3")
clientRoute := makeRoute("outbound|||svc.ns3")
p := mixer.NewPlugin()
clientMutable := networking.MutableObjects{Listener: clientListener, FilterChains: []networking.FilterChain{{}}}
if err := p.OnOutboundListener(&clientParams, &clientMutable); err != nil {
t.Error(err)
}
clientManager.HttpFilters = append(clientMutable.FilterChains[0].HTTP, clientManager.HttpFilters...)
clientListener.FilterChains = []*listener.FilterChain{{Filters: []*listener.Filter{{
Name: "http",
ConfigType: &listener.Filter_TypedConfig{TypedConfig: pilotutil.MessageToAny(clientManager)},
}}}}
p.OnOutboundRouteConfiguration(&clientParams, clientRoute)
snapshot := cache.Snapshot{}
snapshot.Resources[types.Route] = cache.NewResources("http", []types.Resource{env.CastRouteToV2(clientRoute)})
snapshot.Resources[types.Listener] = cache.NewResources("http", []types.Resource{env.CastListenerToV2(clientListener)})
return snapshot
}