This repository has been archived by the owner on Dec 2, 2021. It is now read-only.
forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mixer.go
252 lines (215 loc) · 8.33 KB
/
mixer.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
// Copyright 2017 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.
// Mixer filter configuration
package envoy
import (
"net"
"sort"
"strings"
"github.com/golang/glog"
mpb "istio.io/api/mixer/v1"
mccpb "istio.io/api/mixer/v1/config/client"
proxyconfig "istio.io/api/proxy/v1/config"
"istio.io/istio/pilot/model"
"istio.io/istio/pilot/proxy"
)
const (
// MixerCluster is the name of the mixer cluster
MixerCluster = "mixer_server"
// MixerFilter name and its attributes
MixerFilter = "mixer"
// AttrSourceIP is client source IP
AttrSourceIP = "source.ip"
// AttrSourceUID is platform-specific unique identifier for the client instance of the source service
AttrSourceUID = "source.uid"
// AttrDestinationIP is the server source IP
AttrDestinationIP = "destination.ip"
// AttrDestinationUID is platform-specific unique identifier for the server instance of the target service
AttrDestinationUID = "destination.uid"
// AttrDestinationService is name of the target service
AttrDestinationService = "destination.service"
// MixerRequestCount is the quota bucket name
MixerRequestCount = "RequestCount"
// MixerCheck switches Check call on and off
MixerCheck = "mixer_check"
// MixerReport switches Report call on and off
MixerReport = "mixer_report"
// DisableTCPCheckCalls switches Check call on and off for tcp listeners
DisableTCPCheckCalls = "disable_tcp_check_calls"
// MixerForward switches attribute forwarding on and off
MixerForward = "mixer_forward"
)
// FilterMixerConfig definition.
//
// NOTE: all fields marked as DEPRECATED are part of the original v1
// mixerclient configuration. They are deprecated and will be
// eventually removed once proxies are updated.
//
// Going forwards all mixerclient configuration should represeted by
// istio.io/api/mixer/v1/config/client/mixer_filter_config.proto and
// encoded in the `V2` field below.
//
type FilterMixerConfig struct {
// DEPRECATED: MixerAttributes specifies the static list of attributes that are sent with
// each request to Mixer.
MixerAttributes map[string]string `json:"mixer_attributes,omitempty"`
// DEPRECATED: ForwardAttributes specifies the list of attribute keys and values that
// are forwarded as an HTTP header to the server side proxy
ForwardAttributes map[string]string `json:"forward_attributes,omitempty"`
// DEPRECATED: QuotaName specifies the name of the quota bucket to withdraw tokens from;
// an empty name means no quota will be charged.
QuotaName string `json:"quota_name,omitempty"`
// DEPRECATED: If set to true, disables mixer check calls for TCP connections
DisableTCPCheckCalls bool `json:"disable_tcp_check_calls,omitempty"`
// istio.io/api/mixer/v1/config/client configuration protobuf
// encoded as a generic map using canonical JSON encoding.
//
// If `V2` field is not empty, the DEPRECATED fields above should
// be discarded.
V2 map[string]interface{} `json:"v2,omitempty"`
}
func (*FilterMixerConfig) isNetworkFilterConfig() {}
// buildMixerCluster builds an outbound mixer cluster
func buildMixerCluster(mesh *proxyconfig.MeshConfig, role proxy.Node, mixerSAN []string) *Cluster {
mixerCluster := buildCluster(mesh.MixerAddress, MixerCluster, mesh.ConnectTimeout)
mixerCluster.CircuitBreaker = &CircuitBreaker{
Default: DefaultCBPriority{
MaxPendingRequests: 10000,
MaxRequests: 10000,
},
}
mixerCluster.Features = ClusterFeatureHTTP2
// apply auth policies
switch mesh.DefaultConfig.ControlPlaneAuthPolicy {
case proxyconfig.AuthenticationPolicy_NONE:
// do nothing
case proxyconfig.AuthenticationPolicy_MUTUAL_TLS:
// apply SSL context to enable mutual TLS between Envoy proxies between app and mixer
mixerCluster.SSLContext = buildClusterSSLContext(proxy.AuthCertsPath, mixerSAN)
}
return mixerCluster
}
func buildMixerOpaqueConfig(check, forward bool, destinationService string) map[string]string {
keys := map[bool]string{true: "on", false: "off"}
m := map[string]string{
MixerReport: "on",
MixerCheck: keys[check],
MixerForward: keys[forward],
}
if destinationService != "" {
m[AttrDestinationService] = destinationService
}
return m
}
// Mixer filter uses outbound configuration by default (forward attributes,
// but not invoke check calls)
func mixerHTTPRouteConfig(role proxy.Node, instances []*model.ServiceInstance,
config model.IstioConfigStore) *FilterMixerConfig {
filter := &FilterMixerConfig{
MixerAttributes: map[string]string{
AttrDestinationIP: role.IPAddress,
AttrDestinationUID: "kubernetes://" + role.ID,
},
ForwardAttributes: map[string]string{
AttrSourceIP: role.IPAddress,
AttrSourceUID: "kubernetes://" + role.ID,
},
QuotaName: MixerRequestCount,
}
v2 := &mccpb.HttpClientConfig{
MixerAttributes: &mpb.Attributes{
Attributes: map[string]*mpb.Attributes_AttributeValue{
AttrDestinationIP: {Value: &mpb.Attributes_AttributeValue_BytesValue{net.ParseIP(role.IPAddress)}},
AttrDestinationUID: {Value: &mpb.Attributes_AttributeValue_StringValue{"kubernetes://" + role.ID}},
},
},
ForwardAttributes: &mpb.Attributes{
Attributes: map[string]*mpb.Attributes_AttributeValue{
AttrSourceIP: {Value: &mpb.Attributes_AttributeValue_BytesValue{net.ParseIP(role.IPAddress)}},
AttrSourceUID: {Value: &mpb.Attributes_AttributeValue_StringValue{"kubernetes://" + role.ID}},
},
},
ServiceConfigs: map[string]*mccpb.ServiceConfig{},
}
if len(instances) > 0 {
// legacy mixerclient behavior is a comma separated list of
// services. When can this be removed?
var services []string
if instances != nil {
serviceSet := make(map[string]bool, len(instances))
for _, instance := range instances {
serviceSet[instance.Service.Hostname] = true
}
for service := range serviceSet {
services = append(services, service)
}
sort.Strings(services)
}
filter.MixerAttributes[AttrDestinationService] = strings.Join(services, ",")
// first service in the sorted list is the default
v2.DefaultDestinationService = services[0]
}
for _, instance := range instances {
sc := &mccpb.ServiceConfig{
MixerAttributes: &mpb.Attributes{
Attributes: map[string]*mpb.Attributes_AttributeValue{
AttrDestinationService: {
Value: &mpb.Attributes_AttributeValue_StringValue{instance.Service.Hostname},
},
},
},
}
apiSpecs := config.HTTPAPISpecByDestination(instance)
model.SortHTTPAPISpec(apiSpecs)
for _, config := range apiSpecs {
sc.HttpApiSpec = append(sc.HttpApiSpec, config.Spec.(*mccpb.HTTPAPISpec))
}
quotaSpecs := config.QuotaSpecByDestination(instance)
model.SortQuotaSpec(quotaSpecs)
for _, config := range quotaSpecs {
sc.QuotaSpec = append(sc.QuotaSpec, config.Spec.(*mccpb.QuotaSpec))
}
v2.ServiceConfigs[instance.Service.Hostname] = sc
}
if v2JSONMap, err := model.ToJSONMap(v2); err != nil {
glog.Warningf("Could not encode v2 HTTP mixerclient filter for node %q: %v", role, err)
} else {
filter.V2 = v2JSONMap
}
return filter
}
// Mixer TCP filter config for inbound requests.
func mixerTCPConfig(role proxy.Node, check bool, instance *model.ServiceInstance) *FilterMixerConfig {
filter := &FilterMixerConfig{
MixerAttributes: map[string]string{
AttrDestinationIP: role.IPAddress,
AttrDestinationUID: "kubernetes://" + role.ID,
},
}
v2 := &mccpb.TcpClientConfig{
MixerAttributes: &mpb.Attributes{
Attributes: map[string]*mpb.Attributes_AttributeValue{
AttrDestinationIP: {Value: &mpb.Attributes_AttributeValue_StringValue{role.IPAddress}},
AttrDestinationUID: {Value: &mpb.Attributes_AttributeValue_StringValue{"kubernetes://" + role.ID}},
AttrDestinationService: {Value: &mpb.Attributes_AttributeValue_StringValue{instance.Service.Hostname}},
},
},
}
if v2JSONMap, err := model.ToJSONMap(v2); err != nil {
glog.Warningf("Could not encode v2 TCP mixerclient filter for node %q: %v", role, err)
} else {
filter.V2 = v2JSONMap
}
return filter
}