This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixer.go
364 lines (299 loc) · 13 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// 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 v1
import (
"crypto/sha256"
"encoding/base64"
"net"
meshconfig "istio.io/api/mesh/v1alpha1"
mpb "istio.io/api/mixer/v1"
mccpb "istio.io/api/mixer/v1/config/client"
"istio.io/istio/pilot/pkg/model"
"istio.io/istio/pkg/log"
)
const (
// MixerCheckClusterName is the name of the mixer cluster used for policy checks
MixerCheckClusterName = "mixer_check_server"
// MixerReportClusterName is the name of the mixer cluster used for telemetry
MixerReportClusterName = "mixer_report_server"
// MixerFilter name and its attributes
MixerFilter = "mixer"
// AttrSourcePrefix all source attributes start with this prefix
AttrSourcePrefix = "source"
// 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"
// AttrDestinationPrefix all destination attributes start with this prefix
AttrDestinationPrefix = "destination"
// 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"
// AttrDestinationLabels is Labels associated with the destination
AttrDestinationLabels = "destination.labels"
// AttrDestinationService is name of the target service
AttrDestinationService = "destination.service"
// AttrIPSuffix represents IP address suffix.
AttrIPSuffix = "ip"
// AttrUIDSuffix is the uid suffix of with source or destination.
AttrUIDSuffix = "uid"
// AttrLabelsSuffix is the suffix for labels associated with source or destination.
AttrLabelsSuffix = "labels"
// keyConfigMixer is a key in the opaque_config. It is base64(json.Marshal(ServiceConfig)).
keyConfigMixer = "mixer"
// keyConfigMixerSha is the sha of keyConfigMixer. It is used for equality check.
// MixerClient uses it to avoid decoding and processing keyConfigMixer on every request.
keyConfigMixerSha = "mixer_sha"
// 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"
// 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"`
}
// IsNetworkFilterConfig marks FilterMixerConfig as an implementation of NetworkFilterConfig
func (*FilterMixerConfig) IsNetworkFilterConfig() {}
// buildMixerCluster builds an outbound mixer cluster of a given name
func buildMixerCluster(mesh *meshconfig.MeshConfig, mixerSAN []string, server, clusterName string) *Cluster {
cluster := buildCluster(server, clusterName, mesh.ConnectTimeout)
cluster.CircuitBreaker = &CircuitBreaker{
Default: DefaultCBPriority{
MaxPendingRequests: 10000,
MaxRequests: 10000,
},
}
cluster.MakeHTTP2()
// apply auth policies
switch mesh.DefaultConfig.ControlPlaneAuthPolicy {
case meshconfig.AuthenticationPolicy_NONE:
// do nothing
case meshconfig.AuthenticationPolicy_MUTUAL_TLS:
// apply SSL context to enable mutual TLS between Envoy proxies between app and mixer
cluster.SSLContext = buildClusterSSLContext(model.AuthCertsPath, mixerSAN)
}
return cluster
}
// BuildMixerClusters builds an outbound mixer cluster with configured check/report clusters
func BuildMixerClusters(mesh *meshconfig.MeshConfig, role model.Proxy, mixerSAN []string) []*Cluster {
mixerClusters := make([]*Cluster, 0)
if mesh.MixerCheckServer != "" {
mixerClusters = append(mixerClusters, buildMixerCluster(mesh, mixerSAN, mesh.MixerCheckServer, MixerCheckClusterName))
}
if mesh.MixerReportServer != "" {
mixerClusters = append(mixerClusters, buildMixerCluster(mesh, mixerSAN, mesh.MixerReportServer, MixerReportClusterName))
}
return mixerClusters
}
// BuildMixerConfig build per route mixer config to be deployed at the `model.Proxy` workload
// with destination of Service `dest` and `destName` as the service name
func BuildMixerConfig(source model.Proxy, destName string, dest *model.Service, instances []*model.ServiceInstance, config model.IstioConfigStore,
disableCheck bool, disableReport bool) map[string]string {
sc := ServiceConfig(destName, &model.ServiceInstance{Service: dest}, config, disableCheck, disableReport)
var labels map[string]string
// Note: instances are all running on mode.Node named 'role'
// So instance labels are the workload / Node labels.
if len(instances) > 0 {
labels = instances[0].Labels
}
AddStandardNodeAttributes(sc.MixerAttributes.Attributes, AttrSourcePrefix, source.IPAddress, source.ID, labels)
AddStandardNodeAttributes(sc.MixerAttributes.Attributes, AttrDestinationPrefix, "", destName, nil)
oc := map[string]string{
AttrDestinationService: destName,
}
if cfg, err := model.ToJSON(sc); err == nil {
ba := []byte(cfg)
oc[keyConfigMixer] = base64.StdEncoding.EncodeToString(ba)
h := sha256.New()
h.Write(ba) //nolint: errcheck
oc[keyConfigMixerSha] = base64.StdEncoding.EncodeToString(h.Sum(nil))
} else {
log.Warnf("Unable to convert %#v to json: %v", sc, err)
}
return oc
}
// BuildMixerOpaqueConfig builds a mixer opaque config.
func BuildMixerOpaqueConfig(check, forward bool, destinationService model.Hostname) 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.String()
}
return m
}
// BuildHTTPMixerFilterConfig builds a mixer HTTP filter config. Mixer filter uses outbound configuration by default
// (forward attributes, but not invoke check calls) ServiceInstances belong to the Node.
func BuildHTTPMixerFilterConfig(mesh *meshconfig.MeshConfig, role model.Proxy, nodeInstances []*model.ServiceInstance, outboundRoute bool, config model.IstioConfigStore) *FilterMixerConfig { // nolint: lll
transport := &mccpb.TransportConfig{
CheckCluster: MixerCheckClusterName,
ReportCluster: MixerReportClusterName,
}
v2 := &mccpb.HttpClientConfig{
MixerAttributes: &mpb.Attributes{
Attributes: map[string]*mpb.Attributes_AttributeValue{},
},
ServiceConfigs: map[string]*mccpb.ServiceConfig{},
Transport: transport,
}
filter := &FilterMixerConfig{}
var labels map[string]string
// Note: instances are all running on mode.Node named 'role'
// So instance labels are the workload / Node labels.
if len(nodeInstances) > 0 {
labels = nodeInstances[0].Labels
v2.DefaultDestinationService = nodeInstances[0].Service.Hostname.String()
}
if !outboundRoute {
// for outboundRoutes there are no default MixerAttributes
// specific MixerAttributes are in per route configuration.
AddStandardNodeAttributes(v2.MixerAttributes.Attributes, AttrDestinationPrefix, role.IPAddress, role.ID, labels)
}
if role.Type == model.Sidecar && !outboundRoute {
// Don't forward mixer attributes to the app from inbound sidecar routes
} else {
v2.ForwardAttributes = &mpb.Attributes{
Attributes: map[string]*mpb.Attributes_AttributeValue{},
}
AddStandardNodeAttributes(v2.ForwardAttributes.Attributes, AttrSourcePrefix, role.IPAddress, role.ID, labels)
}
for _, instance := range nodeInstances {
v2.ServiceConfigs[instance.Service.Hostname.String()] = ServiceConfig(instance.Service.Hostname.String(), instance, config,
outboundRoute || mesh.DisablePolicyChecks, outboundRoute)
}
if v2JSONMap, err := model.ToJSONMap(v2); err != nil {
log.Warnf("Could not encode v2 HTTP mixerclient filter for node %q: %v", role, err)
} else {
filter.V2 = v2JSONMap
}
return filter
}
// AddStandardNodeAttributes add standard node attributes with the given prefix
func AddStandardNodeAttributes(attr map[string]*mpb.Attributes_AttributeValue, prefix string, IPAddress string, ID string, labels map[string]string) {
if len(IPAddress) > 0 {
attr[prefix+"."+AttrIPSuffix] = &mpb.Attributes_AttributeValue{
Value: &mpb.Attributes_AttributeValue_BytesValue{net.ParseIP(IPAddress)},
}
}
attr[prefix+"."+AttrUIDSuffix] = &mpb.Attributes_AttributeValue{
Value: &mpb.Attributes_AttributeValue_StringValue{"kubernetes://" + ID},
}
if len(labels) > 0 {
attr[prefix+"."+AttrLabelsSuffix] = &mpb.Attributes_AttributeValue{
Value: &mpb.Attributes_AttributeValue_StringMapValue{
StringMapValue: &mpb.Attributes_StringMap{Entries: labels},
},
}
}
}
// StandardNodeAttributes populates and returns a map of attributes with the provided parameters.
func StandardNodeAttributes(prefix string, IPAddress string, ID string, labels map[string]string) map[string]*mpb.Attributes_AttributeValue {
attrs := make(map[string]*mpb.Attributes_AttributeValue)
AddStandardNodeAttributes(attrs, prefix, IPAddress, ID, labels)
return attrs
}
// ServiceConfig generates a ServiceConfig for a given instance
func ServiceConfig(serviceName string, dest *model.ServiceInstance, config model.IstioConfigStore, disableCheck, disableReport bool) *mccpb.ServiceConfig {
sc := &mccpb.ServiceConfig{
MixerAttributes: &mpb.Attributes{
Attributes: map[string]*mpb.Attributes_AttributeValue{
AttrDestinationService: {
Value: &mpb.Attributes_AttributeValue_StringValue{StringValue: serviceName},
},
},
},
DisableCheckCalls: disableCheck,
DisableReportCalls: disableReport,
}
if len(dest.Labels) > 0 {
sc.MixerAttributes.Attributes[AttrDestinationLabels] = &mpb.Attributes_AttributeValue{
Value: &mpb.Attributes_AttributeValue_StringMapValue{
StringMapValue: &mpb.Attributes_StringMap{Entries: dest.Labels},
},
}
}
apiSpecs := config.HTTPAPISpecByDestination(dest)
model.SortHTTPAPISpec(apiSpecs)
for _, config := range apiSpecs {
sc.HttpApiSpec = append(sc.HttpApiSpec, config.Spec.(*mccpb.HTTPAPISpec))
}
quotaSpecs := config.QuotaSpecByDestination(dest)
model.SortQuotaSpec(quotaSpecs)
for _, config := range quotaSpecs {
sc.QuotaSpec = append(sc.QuotaSpec, config.Spec.(*mccpb.QuotaSpec))
}
return sc
}
// BuildTCPMixerFilterConfig builds a TCP filter config for inbound requests.
func BuildTCPMixerFilterConfig(mesh *meshconfig.MeshConfig, role model.Proxy, instance *model.ServiceInstance) *FilterMixerConfig {
filter := &FilterMixerConfig{
MixerAttributes: map[string]string{
AttrDestinationIP: role.IPAddress,
AttrDestinationUID: "kubernetes://" + role.ID,
},
}
attrs := StandardNodeAttributes(AttrDestinationPrefix, role.IPAddress, role.ID, nil)
attrs[AttrDestinationService] = &mpb.Attributes_AttributeValue{Value: &mpb.Attributes_AttributeValue_StringValue{instance.Service.Hostname.String()}}
v2 := &mccpb.TcpClientConfig{
MixerAttributes: &mpb.Attributes{
Attributes: attrs,
},
Transport: &mccpb.TransportConfig{
CheckCluster: MixerCheckClusterName,
ReportCluster: MixerReportClusterName,
},
DisableCheckCalls: mesh.DisablePolicyChecks,
}
if v2JSONMap, err := model.ToJSONMap(v2); err != nil {
log.Warnf("Could not encode v2 TCP mixerclient filter for node %q: %v", role, err)
} else {
filter.V2 = v2JSONMap
}
return filter
}