forked from kiali/kiali
/
istio.go
663 lines (578 loc) · 20.3 KB
/
istio.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
package kubernetes
import (
"encoding/json"
"fmt"
"regexp"
"strings"
"sync"
"k8s.io/client-go/rest"
"gopkg.in/yaml.v2"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/log"
)
var (
portNameMatcher = regexp.MustCompile(`^[\-].*`)
portProtocols = [...]string{"grpc", "http", "http2", "https", "mongo", "redis", "tcp", "tls", "udp", "mysql"}
istioConfigmapName = "istio"
)
// Aux method to fetch proper (RESTClient, APIVersion) per API group
func (in *K8SClient) getApiClientVersion(apiGroup string) (*rest.RESTClient, string) {
if apiGroup == ConfigGroupVersion.Group {
return in.istioConfigApi, ApiConfigVersion
} else if apiGroup == NetworkingGroupVersion.Group {
return in.istioNetworkingApi, ApiNetworkingVersion
} else if apiGroup == AuthenticationGroupVersion.Group {
return in.istioAuthenticationApi, ApiAuthenticationVersion
} else if apiGroup == RbacGroupVersion.Group {
return in.istioRbacApi, ApiRbacVersion
} else if apiGroup == MaistraAuthenticationGroupVersion.Group {
return in.maistraAuthenticationApi, ApiMaistraAuthenticationVersion
} else if apiGroup == MaistraRbacGroupVersion.Group {
return in.maistraRbacApi, ApiMaistraRbacVersion
} else if apiGroup == SecurityGroupVersion.Group {
return in.istioSecurityApi, ApiSecurityVersion
}
return nil, ""
}
// CreateIstioObject creates an Istio object
func (in *K8SClient) CreateIstioObject(api, namespace, resourceType, json string) (IstioObject, error) {
var result runtime.Object
var err error
typeMeta := meta_v1.TypeMeta{
Kind: "",
APIVersion: "",
}
typeMeta.Kind = PluralType[resourceType]
byteJson := []byte(json)
var apiClient *rest.RESTClient
apiClient, typeMeta.APIVersion = in.getApiClientVersion(api)
if apiClient == nil {
return nil, fmt.Errorf("%s is not supported in CreateIstioObject operation", api)
}
// MeshPeerAuthentications and ClusterRbacConfigs are cluster scope objects
// Update: Removed the namespace filter as it doesn't work well in all platforms
// https://issues.jboss.org/browse/KIALI-3223
if resourceType == MeshPolicies || resourceType == ClusterRbacConfigs {
result, err = apiClient.Post().Resource(resourceType).Body(byteJson).Do().Get()
} else {
result, err = apiClient.Post().Namespace(namespace).Resource(resourceType).Body(byteJson).Do().Get()
}
if err != nil {
return nil, err
}
istioObject, ok := result.(*GenericIstioObject)
if !ok {
return nil, fmt.Errorf("%s/%s doesn't return an IstioObject object", namespace, resourceType)
}
istioObject.SetTypeMeta(typeMeta)
return istioObject, err
}
// DeleteIstioObject deletes an Istio object from either config api or networking api
func (in *K8SClient) DeleteIstioObject(api, namespace, resourceType, name string) error {
log.Debugf("DeleteIstioObject input: %s / %s / %s / %s", api, namespace, resourceType, name)
var err error
apiClient, _ := in.getApiClientVersion(api)
if apiClient == nil {
return fmt.Errorf("%s is not supported in DeleteIstioObject operation", api)
}
// MeshPeerAuthentications and ClusterRbacConfigs are cluster scope objects
// Update: Removed the namespace filter as it doesn't work well in all platforms
// https://issues.jboss.org/browse/KIALI-3223
if resourceType == MeshPolicies || resourceType == ClusterRbacConfigs {
_, err = apiClient.Delete().Resource(resourceType).Name(name).Do().Get()
} else {
_, err = apiClient.Delete().Namespace(namespace).Resource(resourceType).Name(name).Do().Get()
}
return err
}
// UpdateIstioObject updates an Istio object from either config api or networking api
func (in *K8SClient) UpdateIstioObject(api, namespace, resourceType, name, jsonPatch string) (IstioObject, error) {
log.Debugf("UpdateIstioObject input: %s / %s / %s / %s", api, namespace, resourceType, name)
var result runtime.Object
var err error
typeMeta := meta_v1.TypeMeta{
Kind: "",
APIVersion: "",
}
typeMeta.Kind = PluralType[resourceType]
bytePatch := []byte(jsonPatch)
var apiClient *rest.RESTClient
apiClient, typeMeta.APIVersion = in.getApiClientVersion(api)
if apiClient == nil {
return nil, fmt.Errorf("%s is not supported in UpdateIstioObject operation", api)
}
// MeshPeerAuthentications and ClusterRbacConfigs are cluster scope objects
// Update: Removed the namespace filter as it doesn't work well in all platforms
// https://issues.jboss.org/browse/KIALI-3223
if resourceType == MeshPolicies || resourceType == ClusterRbacConfigs {
result, err = apiClient.Patch(types.MergePatchType).Resource(resourceType).SubResource(name).Body(bytePatch).Do().Get()
} else {
result, err = apiClient.Patch(types.MergePatchType).Namespace(namespace).Resource(resourceType).SubResource(name).Body(bytePatch).Do().Get()
}
if err != nil {
return nil, err
}
istioObject, ok := result.(*GenericIstioObject)
if !ok {
return nil, fmt.Errorf("%s/%s doesn't return an IstioObject object", namespace, name)
}
istioObject.SetTypeMeta(typeMeta)
return istioObject, err
}
func (in *K8SClient) GetIstioObjects(namespace, resourceType, labelSelector string) ([]IstioObject, error) {
var apiClient *rest.RESTClient
var apiGroup, apiVersion string
var ok bool
if apiGroup, ok = ResourceTypesToAPI[resourceType]; ok {
apiClient, apiVersion = in.getApiClientVersion(apiGroup)
} else {
return []IstioObject{}, fmt.Errorf("%s not found in ResourcesTypeToAPI", resourceType)
}
if apiGroup == NetworkingGroupVersion.Group && !in.hasNetworkingResource(resourceType) {
return []IstioObject{}, nil
}
if apiGroup == ConfigGroupVersion.Group && !in.hasConfigResource(resourceType) {
return []IstioObject{}, nil
}
if apiGroup == AuthenticationGroupVersion.Group && !in.hasAuthenticationResource(resourceType) {
return []IstioObject{}, nil
}
if apiGroup == RbacGroupVersion.Group && !in.hasRbacResource(resourceType) {
return []IstioObject{}, nil
}
if apiGroup == SecurityGroupVersion.Group && !in.hasSecurityResource(resourceType) {
return []IstioObject{}, nil
}
var result runtime.Object
var err error
if resourceType == MeshPolicies || resourceType == ClusterRbacConfigs {
result, err = apiClient.Get().Resource(resourceType).Param("labelSelector", labelSelector).Do().Get()
} else {
result, err = apiClient.Get().Namespace(namespace).Resource(resourceType).Param("labelSelector", labelSelector).Do().Get()
}
if err != nil {
return nil, err
}
istioList, ok := result.(*GenericIstioObjectList)
if !ok {
return nil, fmt.Errorf("%s/%s doesn't return a list", namespace, resourceType)
}
typeMeta := meta_v1.TypeMeta{
Kind: PluralType[resourceType],
APIVersion: apiVersion,
}
list := make([]IstioObject, 0)
for _, item := range istioList.GetItems() {
i := item.DeepCopyIstioObject()
i.SetTypeMeta(typeMeta)
list = append(list, i)
}
return list, nil
}
func (in *K8SClient) GetIstioObject(namespace, resourceType, name string) (IstioObject, error) {
var apiClient *rest.RESTClient
var apiGroup, apiVersion string
var ok bool
if apiGroup, ok = ResourceTypesToAPI[resourceType]; ok {
apiClient, apiVersion = in.getApiClientVersion(apiGroup)
} else {
return nil, fmt.Errorf("%s not found in ResourcesTypeToAPI", resourceType)
}
var result runtime.Object
var err error
if resourceType == MeshPolicies || resourceType == ClusterRbacConfigs {
result, err = apiClient.Get().Resource(resourceType).SubResource(name).Do().Get()
} else {
result, err = apiClient.Get().Namespace(namespace).Resource(resourceType).SubResource(name).Do().Get()
}
if err != nil {
return nil, err
}
typeMeta := meta_v1.TypeMeta{
Kind: PluralType[resourceType],
APIVersion: apiVersion,
}
istioObject, ok := result.(*GenericIstioObject)
if !ok {
return nil, fmt.Errorf("%s/%s/%s doesn't return an Istio object", namespace, resourceType, name)
}
io := istioObject.DeepCopyIstioObject()
io.SetTypeMeta(typeMeta)
return io, nil
}
func (in *K8SClient) hasNetworkingResource(resource string) bool {
return in.getNetworkingResources()[resource]
}
func (in *K8SClient) getNetworkingResources() map[string]bool {
if in.networkingResources != nil {
return *in.networkingResources
}
networkingResources := map[string]bool{}
path := fmt.Sprintf("/apis/%s", ApiNetworkingVersion)
resourceListRaw, err := in.k8s.RESTClient().Get().AbsPath(path).Do().Raw()
if err == nil {
resourceList := meta_v1.APIResourceList{}
if errMarshall := json.Unmarshal(resourceListRaw, &resourceList); errMarshall == nil {
for _, resource := range resourceList.APIResources {
networkingResources[resource.Name] = true
}
}
}
in.networkingResources = &networkingResources
return *in.networkingResources
}
func (in *K8SClient) hasConfigResource(resource string) bool {
return in.getConfigResources()[resource]
}
func (in *K8SClient) getConfigResources() map[string]bool {
if in.configResources != nil {
return *in.configResources
}
configResources := map[string]bool{}
path := fmt.Sprintf("/apis/%s", ApiConfigVersion)
resourceListRaw, err := in.k8s.RESTClient().Get().AbsPath(path).Do().Raw()
if err == nil {
resourceList := meta_v1.APIResourceList{}
if errMarshall := json.Unmarshal(resourceListRaw, &resourceList); errMarshall == nil {
for _, resource := range resourceList.APIResources {
configResources[resource.Name] = true
}
}
}
in.configResources = &configResources
return *in.configResources
}
func (in *K8SClient) hasRbacResource(resource string) bool {
return in.getRbacResources()[resource]
}
func (in *K8SClient) getRbacResources() map[string]bool {
if in.rbacResources != nil {
return *in.rbacResources
}
rbacResources := map[string]bool{}
path := fmt.Sprintf("/apis/%s", ApiRbacVersion)
resourceListRaw, err := in.k8s.RESTClient().Get().AbsPath(path).Do().Raw()
if err == nil {
resourceList := meta_v1.APIResourceList{}
if errMarshall := json.Unmarshal(resourceListRaw, &resourceList); errMarshall == nil {
for _, resource := range resourceList.APIResources {
rbacResources[resource.Name] = true
}
}
}
in.rbacResources = &rbacResources
return *in.rbacResources
}
func (in *K8SClient) hasSecurityResource(resource string) bool {
return in.getSecurityResources()[resource]
}
func (in *K8SClient) getSecurityResources() map[string]bool {
if in.securityResources != nil {
return *in.securityResources
}
securityResources := map[string]bool{}
path := fmt.Sprintf("/apis/%s", ApiSecurityVersion)
resourceListRaw, err := in.k8s.RESTClient().Get().AbsPath(path).Do().Raw()
if err == nil {
resourceList := meta_v1.APIResourceList{}
if errMarshall := json.Unmarshal(resourceListRaw, &resourceList); errMarshall == nil {
for _, resource := range resourceList.APIResources {
securityResources[resource.Name] = true
}
}
}
in.securityResources = &securityResources
return *in.securityResources
}
func (in *K8SClient) hasAuthenticationResource(resource string) bool {
return in.getAuthenticationResources()[resource]
}
func (in *K8SClient) getAuthenticationResources() map[string]bool {
if in.authenticationResources != nil {
return *in.authenticationResources
}
authenticationResources := map[string]bool{}
path := fmt.Sprintf("/apis/%s", ApiAuthenticationVersion)
resourceListRaw, err := in.k8s.RESTClient().Get().AbsPath(path).Do().Raw()
if err == nil {
resourceList := meta_v1.APIResourceList{}
if errMarshall := json.Unmarshal(resourceListRaw, &resourceList); errMarshall == nil {
for _, resource := range resourceList.APIResources {
authenticationResources[resource.Name] = true
}
}
}
in.authenticationResources = &authenticationResources
return *in.authenticationResources
}
// GetAuthorizationDetails returns ServiceRoles, ServiceRoleBindings and ClusterRbacDetails
func (in *K8SClient) GetAuthorizationDetails(namespace string) (*RBACDetails, error) {
rb := &RBACDetails{}
errChan := make(chan error, 1)
var wg sync.WaitGroup
wg.Add(4)
go func(errChan chan error) {
defer wg.Done()
if aps, err := in.GetIstioObjects(namespace, AuthorizationPolicies, ""); err == nil {
rb.AuthorizationPolicies = aps
} else {
errChan <- err
}
}(errChan)
go func(errChan chan error) {
defer wg.Done()
if srb, err := in.GetIstioObjects(namespace, ServiceRoleBindings, ""); err == nil {
rb.ServiceRoleBindings = srb
} else {
errChan <- err
}
}(errChan)
go func(errChan chan error) {
defer wg.Done()
if sr, err := in.GetIstioObjects(namespace, ServiceRoles, ""); err == nil {
rb.ServiceRoles = sr
} else {
errChan <- err
}
}(errChan)
go func(errChan chan error) {
defer wg.Done()
// Maistra has migrated ClusterRbacConfigs to ServiceMeshRbacConfigs resources
if !in.IsMaistraApi() {
if crc, err := in.GetIstioObjects("", ClusterRbacConfigs, ""); err == nil {
rb.ClusterRbacConfigs = crc
} else {
errChan <- err
}
} else {
if smrc, err := in.GetIstioObjects(namespace, ServiceMeshRbacConfigs, ""); err == nil {
rb.ServiceMeshRbacConfigs = smrc
} else {
errChan <- err
}
}
}(errChan)
wg.Wait()
close(errChan)
for e := range errChan {
if e != nil { // Check that default value wasn't returned
return rb, e
}
}
return rb, nil
}
func (in *K8SClient) GetIstioConfigMap() (*IstioMeshConfig, error) {
meshConfig := &IstioMeshConfig{}
cfg := config.Get()
istioConfig, err := in.GetConfigMap(cfg.IstioNamespace, istioConfigmapName)
if err != nil {
log.Warningf("GetIstioConfigMap: Cannot retrieve Istio ConfigMap.")
return nil, err
}
meshConfigYaml, ok := istioConfig.Data["mesh"]
log.Tracef("meshConfig: %v", meshConfigYaml)
if !ok {
log.Warningf("GetIstioConfigMap: Cannot find Istio mesh configuration.")
return nil, err
}
err = yaml.Unmarshal([]byte(meshConfigYaml), &meshConfig)
if err != nil {
log.Warningf("GetIstioConfigMap: Cannot read Istio mesh configuration.")
return nil, err
}
return meshConfig, nil
}
func (in *K8SClient) IsMixerDisabled() bool {
if in.isMixerDisabled != nil {
return *in.isMixerDisabled
}
meshConfig, err := in.GetIstioConfigMap()
if err != nil {
log.Warningf("IsMixerDisabled: Cannot read Istio mesh configuration.")
return true
}
log.Debugf("IsMixerDisabled: %t", meshConfig.DisableMixerHttpReports)
// References:
// * https://github.com/istio/api/pull/1112
// * https://github.com/istio/istio/pull/17695
// * https://github.com/istio/istio/issues/15935
in.isMixerDisabled = &meshConfig.DisableMixerHttpReports
return *in.isMixerDisabled
}
// ServiceEntryHostnames returns a list of hostnames defined in the ServiceEntries Specs. Key in the resulting map is the protocol (in lowercase) + hostname
// exported for test
func ServiceEntryHostnames(serviceEntries []IstioObject) map[string][]string {
hostnames := make(map[string][]string)
for _, v := range serviceEntries {
if hostsSpec, found := v.GetSpec()["hosts"]; found {
if hosts, ok := hostsSpec.([]interface{}); ok {
// Seek the protocol
for _, h := range hosts {
if hostname, ok := h.(string); ok {
hostnames[hostname] = make([]string, 0, 1)
}
}
}
}
if portsSpec, found := v.GetSpec()["ports"]; found {
if portsArray, ok := portsSpec.([]interface{}); ok {
for _, portDef := range portsArray {
if ports, ok := portDef.(map[string]interface{}); ok {
if proto, found := ports["protocol"]; found {
if protocol, ok := proto.(string); ok {
protocol = mapPortToVirtualServiceProtocol(protocol)
for host := range hostnames {
hostnames[host] = append(hostnames[host], protocol)
}
}
}
}
}
}
}
}
return hostnames
}
// mapPortToVirtualServiceProtocol transforms Istio's Port-definitions' protocol names to VirtualService's protocol names
func mapPortToVirtualServiceProtocol(proto string) string {
// http: HTTP/HTTP2/GRPC/ TLS-terminated-HTTPS and service entry ports using HTTP/HTTP2/GRPC protocol
// tls: HTTPS/TLS protocols (i.e. with “passthrough” TLS mode) and service entry ports using HTTPS/TLS protocols.
// tcp: everything else
switch proto {
case "HTTP":
fallthrough
case "HTTP2":
fallthrough
case "GRPC":
return "http"
case "HTTPS":
fallthrough
case "TLS":
return "tls"
default:
return "tcp"
}
}
// ValidaPort parses the Istio Port definition and validates the naming scheme
func ValidatePort(portDef interface{}) bool {
return MatchPortNameRule(parsePort(portDef))
}
func parsePort(portDef interface{}) (string, string) {
var name, proto string
if port, ok := portDef.(map[string]interface{}); ok {
if portNameDef, found := port["name"]; found {
if portName, ok := portNameDef.(string); ok {
name = portName
}
}
if protocolDef, found := port["protocol"]; found {
if protocol, ok := protocolDef.(string); ok {
proto = protocol
}
}
}
return name, proto
}
func MatchPortNameRule(portName, protocol string) bool {
protocol = strings.ToLower(protocol)
// Check that portName begins with the protocol
if protocol == "tcp" || protocol == "udp" {
// TCP and UDP protocols do not care about the name
return true
}
if !strings.HasPrefix(portName, protocol) {
return false
}
// If longer than protocol, then it must adhere to <protocol>[-suffix]
// and if there's -, then there must be a suffix ..
if len(portName) > len(protocol) {
restPortName := portName[len(protocol):]
return portNameMatcher.MatchString(restPortName)
}
// Case portName == protocolName
return true
}
func MatchPortNameWithValidProtocols(portName string) bool {
for _, protocol := range portProtocols {
if strings.HasPrefix(portName, protocol) &&
(strings.ToLower(portName) == protocol || portNameMatcher.MatchString(portName[len(protocol):])) {
return true
}
}
return false
}
// GatewayNames extracts the gateway names for easier matching
func GatewayNames(gateways [][]IstioObject) map[string]struct{} {
var empty struct{}
names := make(map[string]struct{})
for _, ns := range gateways {
for _, gw := range ns {
gw := gw
clusterName := gw.GetObjectMeta().ClusterName
if clusterName == "" {
clusterName = config.Get().ExternalServices.Istio.IstioIdentityDomain
}
names[ParseHost(gw.GetObjectMeta().Name, gw.GetObjectMeta().Namespace, clusterName).String()] = empty
}
}
return names
}
func PeerAuthnHasStrictMTLS(peerAuthn IstioObject) bool {
_, mode := PeerAuthnHasMTLSEnabled(peerAuthn)
return mode == "STRICT"
}
func PeerAuthnHasMTLSEnabled(peerAuthn IstioObject) (bool, string) {
// It is no globally enabled when has targets
if peerAuthn.HasMatchLabelsSelector() {
return false, ""
}
// It is globally enabled when mtls is in STRICT mode
if mtls, mtlsPresent := peerAuthn.GetSpec()["mtls"]; mtlsPresent {
if mtlsMap, ok := mtls.(map[string]interface{}); ok {
if modeItf, found := mtlsMap["mode"]; found {
if mode, ok := modeItf.(string); ok {
return mode == "STRICT" || mode == "PERMISSIVE", mode
} else {
return false, ""
}
} else {
return true, "PERMISSIVE"
}
}
}
return false, ""
}
func DestinationRuleHasMeshWideMTLSEnabled(destinationRule IstioObject) (bool, string) {
// Following the suggested procedure to enable mesh-wide mTLS, host might be '*.local':
// https://istio.io/docs/tasks/security/authn-policy/#globally-enabling-istio-mutual-tls
return DestinationRuleHasMTLSEnabledForHost("*.local", destinationRule)
}
func DestinationRuleHasNamespaceWideMTLSEnabled(namespace string, destinationRule IstioObject) (bool, string) {
// Following the suggested procedure to enable namespace-wide mTLS, host might be '*.namespace.svc.cluster.local'
// https://istio.io/docs/tasks/security/authn-policy/#namespace-wide-policy
nsHost := fmt.Sprintf("*.%s.%s", namespace, config.Get().ExternalServices.Istio.IstioIdentityDomain)
return DestinationRuleHasMTLSEnabledForHost(nsHost, destinationRule)
}
func DestinationRuleHasMTLSEnabledForHost(expectedHost string, destinationRule IstioObject) (bool, string) {
host, hostPresent := destinationRule.GetSpec()["host"]
if !hostPresent || host != expectedHost {
return false, ""
}
if trafficPolicy, trafficPresent := destinationRule.GetSpec()["trafficPolicy"]; trafficPresent {
if trafficCasted, ok := trafficPolicy.(map[string]interface{}); ok {
if tls, found := trafficCasted["tls"]; found {
if tlsCasted, ok := tls.(map[string]interface{}); ok {
if mode, found := tlsCasted["mode"]; found {
if modeCasted, ok := mode.(string); ok {
return modeCasted == "ISTIO_MUTUAL", modeCasted
}
}
}
}
}
}
return false, ""
}