forked from kiali/kiali
-
Notifications
You must be signed in to change notification settings - Fork 0
/
istio_validations.go
628 lines (547 loc) · 26.9 KB
/
istio_validations.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
package business
import (
"context"
"fmt"
"sync"
networking_v1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
security_v1beta "istio.io/client-go/pkg/apis/security/v1beta1"
core_v1 "k8s.io/api/core/v1"
"github.com/kiali/kiali/business/checkers"
"github.com/kiali/kiali/business/references"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/kubernetes"
"github.com/kiali/kiali/log"
"github.com/kiali/kiali/models"
"github.com/kiali/kiali/observability"
"github.com/kiali/kiali/prometheus/internalmetrics"
)
type IstioValidationsService struct {
userClients map[string]kubernetes.ClientInterface
businessLayer *Layer
}
type ObjectChecker interface {
Check() models.IstioValidations
}
type ReferenceChecker interface {
References() models.IstioReferencesMap
}
// GetValidations returns an IstioValidations object with all the checks found when running
// all the enabled checkers. If service is "" then the whole namespace is validated.
// If service is not empty string, then all of its associated Istio objects are validated.
func (in *IstioValidationsService) GetValidations(ctx context.Context, cluster, namespace, service, workload string) (models.IstioValidations, error) {
var end observability.EndFunc
ctx, end = observability.StartSpan(ctx, "GetValidations",
observability.Attribute("package", "business"),
observability.Attribute("cluster", cluster),
observability.Attribute("namespace", namespace),
observability.Attribute("service", service),
observability.Attribute("workload", workload),
)
defer end()
// Check if user has access to the namespace (RBAC) in cache scenarios and/or
// if namespace is accessible from Kiali (Deployment.AccessibleNamespaces)
if namespace != "" {
if _, err := in.businessLayer.Namespace.GetClusterNamespace(ctx, namespace, cluster); err != nil {
return nil, err
}
}
// Ensure the service exists
if service != "" {
_, err := in.businessLayer.Svc.GetService(ctx, cluster, namespace, service)
if err != nil {
if err != nil {
log.Warningf("Error invoking GetService %s", err)
}
return nil, fmt.Errorf("Service [namespace: %s] [name: %s] doesn't exist for Validations.", namespace, service)
}
}
// time this function execution so we can capture how long it takes to fully validate this namespace/service
timer := internalmetrics.GetValidationProcessingTimePrometheusTimer(namespace, service)
defer timer.ObserveDuration()
wg := sync.WaitGroup{}
errChan := make(chan error, 1)
var istioConfigList models.IstioConfigList
var services models.ServiceList
var namespaces models.Namespaces
var workloadsPerNamespace map[string]models.WorkloadList
var mtlsDetails kubernetes.MTLSDetails
var rbacDetails kubernetes.RBACDetails
var registryServices []*kubernetes.RegistryService
istioApiEnabled := config.Get().ExternalServices.Istio.IstioAPIEnabled
wg.Add(3) // We need to add these here to make sure we don't execute wg.Wait() before scheduler has started goroutines
if service != "" {
wg.Add(1)
}
if istioApiEnabled {
wg.Add(1)
}
// We fetch without target service as some validations will require full-namespace details
go in.fetchIstioConfigList(ctx, &istioConfigList, &mtlsDetails, &rbacDetails, cluster, namespace, errChan, &wg)
if workload != "" {
// load only requested workload
go in.fetchWorkload(ctx, &workloadsPerNamespace, cluster, workload, namespace, errChan, &wg)
} else {
go in.fetchAllWorkloads(ctx, &workloadsPerNamespace, cluster, &namespaces, errChan, &wg)
}
go in.fetchNonLocalmTLSConfigs(&mtlsDetails, cluster, errChan, &wg)
if service != "" {
go in.fetchServices(ctx, &services, cluster, namespace, errChan, &wg)
}
if istioApiEnabled {
// @TODO registry services for remote cluster
go in.fetchRegistryServices(®istryServices, errChan, &wg)
}
wg.Wait()
close(errChan)
for e := range errChan {
if e != nil { // Check that default value wasn't returned
return nil, e
}
}
objectCheckers := in.getAllObjectCheckers(istioConfigList, workloadsPerNamespace, mtlsDetails, rbacDetails, namespaces, registryServices, cluster)
// Get group validations for same kind istio objects
validations := runObjectCheckers(objectCheckers)
if service != "" {
// in.businessLayer.Svc.GetServiceList(criteria) on fetchServices performs the validations on the service
// No need to re-fetch deployments+pods for this
validations.MergeValidations(services.Validations)
validations = validations.FilterBySingleType("service", service)
} else if workload != "" {
workloadList := workloadsPerNamespace[namespace]
validations.MergeValidations(workloadList.Validations)
validations = validations.FilterBySingleType("workload", workload)
}
return validations, nil
}
func (in *IstioValidationsService) getAllObjectCheckers(istioConfigList models.IstioConfigList, workloadsPerNamespace map[string]models.WorkloadList, mtlsDetails kubernetes.MTLSDetails, rbacDetails kubernetes.RBACDetails, namespaces []models.Namespace, registryServices []*kubernetes.RegistryService, cluster string) []ObjectChecker {
return []ObjectChecker{
checkers.NoServiceChecker{Namespaces: namespaces, IstioConfigList: &istioConfigList, WorkloadsPerNamespace: workloadsPerNamespace, AuthorizationDetails: &rbacDetails, RegistryServices: registryServices, PolicyAllowAny: in.isPolicyAllowAny(), Cluster: cluster},
checkers.VirtualServiceChecker{Namespaces: namespaces, VirtualServices: istioConfigList.VirtualServices, DestinationRules: istioConfigList.DestinationRules, Cluster: cluster},
checkers.DestinationRulesChecker{Namespaces: namespaces, DestinationRules: istioConfigList.DestinationRules, MTLSDetails: mtlsDetails, ServiceEntries: istioConfigList.ServiceEntries, Cluster: cluster},
checkers.GatewayChecker{Gateways: istioConfigList.Gateways, WorkloadsPerNamespace: workloadsPerNamespace, IsGatewayToNamespace: in.isGatewayToNamespace(), Cluster: cluster},
checkers.PeerAuthenticationChecker{PeerAuthentications: mtlsDetails.PeerAuthentications, MTLSDetails: mtlsDetails, WorkloadsPerNamespace: workloadsPerNamespace, Cluster: cluster},
checkers.ServiceEntryChecker{ServiceEntries: istioConfigList.ServiceEntries, Namespaces: namespaces, WorkloadEntries: istioConfigList.WorkloadEntries, Cluster: cluster},
checkers.AuthorizationPolicyChecker{AuthorizationPolicies: rbacDetails.AuthorizationPolicies, Namespaces: namespaces, ServiceEntries: istioConfigList.ServiceEntries, WorkloadsPerNamespace: workloadsPerNamespace, MtlsDetails: mtlsDetails, VirtualServices: istioConfigList.VirtualServices, RegistryServices: registryServices, PolicyAllowAny: in.isPolicyAllowAny(), Cluster: cluster},
checkers.SidecarChecker{Sidecars: istioConfigList.Sidecars, Namespaces: namespaces, WorkloadsPerNamespace: workloadsPerNamespace, ServiceEntries: istioConfigList.ServiceEntries, RegistryServices: registryServices, Cluster: cluster},
checkers.RequestAuthenticationChecker{RequestAuthentications: istioConfigList.RequestAuthentications, WorkloadsPerNamespace: workloadsPerNamespace, Cluster: cluster},
checkers.WorkloadChecker{AuthorizationPolicies: rbacDetails.AuthorizationPolicies, WorkloadsPerNamespace: workloadsPerNamespace, Cluster: cluster},
checkers.K8sGatewayChecker{K8sGateways: istioConfigList.K8sGateways, Cluster: cluster, GatewayClasses: in.businessLayer.IstioConfig.GatewayAPIClasses()},
checkers.WasmPluginChecker{WasmPlugins: istioConfigList.WasmPlugins, Namespaces: namespaces},
checkers.TelemetryChecker{Telemetries: istioConfigList.Telemetries, Namespaces: namespaces},
checkers.K8sHTTPRouteChecker{K8sHTTPRoutes: istioConfigList.K8sHTTPRoutes, K8sGateways: istioConfigList.K8sGateways, Namespaces: namespaces, RegistryServices: registryServices, Cluster: cluster},
}
}
// GetIstioObjectValidations validates a single Istio object of the given type with the given name found in the given namespace.
func (in *IstioValidationsService) GetIstioObjectValidations(ctx context.Context, cluster, namespace string, objectType string, object string) (models.IstioValidations, models.IstioReferencesMap, error) {
var end observability.EndFunc
ctx, end = observability.StartSpan(ctx, "GetIstioObjectValidations",
observability.Attribute("package", "business"),
observability.Attribute("cluster", "cluster"),
observability.Attribute("namespace", namespace),
observability.Attribute("objectType", objectType),
observability.Attribute("object", object),
)
defer end()
var istioConfigList models.IstioConfigList
var namespaces models.Namespaces
var workloadsPerNamespace map[string]models.WorkloadList
var mtlsDetails kubernetes.MTLSDetails
var rbacDetails kubernetes.RBACDetails
var registryServices []*kubernetes.RegistryService
var err error
var objectCheckers []ObjectChecker
var referenceChecker ReferenceChecker
istioReferences := models.IstioReferencesMap{}
istioApiEnabled := config.Get().ExternalServices.Istio.IstioAPIEnabled
// Check if user has access to the namespace (RBAC) in cache scenarios and/or
// if namespace is accessible from Kiali (Deployment.AccessibleNamespaces)
if _, err = in.businessLayer.Namespace.GetClusterNamespace(ctx, namespace, cluster); err != nil {
return nil, istioReferences, err
}
// time this function execution so we can capture how long it takes to fully validate this istio object
timer := internalmetrics.GetSingleValidationProcessingTimePrometheusTimer(namespace, objectType, object)
defer timer.ObserveDuration()
wg := sync.WaitGroup{}
errChan := make(chan error, 1)
// Get all the Istio objects from a Namespace and all gateways from every namespace
wg.Add(3)
if istioApiEnabled {
wg.Add(1)
}
go in.fetchIstioConfigList(ctx, &istioConfigList, &mtlsDetails, &rbacDetails, cluster, namespace, errChan, &wg)
go in.fetchAllWorkloads(ctx, &workloadsPerNamespace, cluster, &namespaces, errChan, &wg)
go in.fetchNonLocalmTLSConfigs(&mtlsDetails, cluster, errChan, &wg)
if istioApiEnabled {
go in.fetchRegistryServices(®istryServices, errChan, &wg)
}
wg.Wait()
noServiceChecker := checkers.NoServiceChecker{Namespaces: namespaces, IstioConfigList: &istioConfigList, WorkloadsPerNamespace: workloadsPerNamespace, AuthorizationDetails: &rbacDetails, RegistryServices: registryServices, PolicyAllowAny: in.isPolicyAllowAny()}
switch objectType {
case kubernetes.Gateways:
objectCheckers = []ObjectChecker{
checkers.GatewayChecker{Gateways: istioConfigList.Gateways, WorkloadsPerNamespace: workloadsPerNamespace, IsGatewayToNamespace: in.isGatewayToNamespace()},
}
referenceChecker = references.GatewayReferences{Gateways: istioConfigList.Gateways, VirtualServices: istioConfigList.VirtualServices, WorkloadsPerNamespace: workloadsPerNamespace}
case kubernetes.VirtualServices:
virtualServiceChecker := checkers.VirtualServiceChecker{Namespaces: namespaces, VirtualServices: istioConfigList.VirtualServices, DestinationRules: istioConfigList.DestinationRules}
objectCheckers = []ObjectChecker{noServiceChecker, virtualServiceChecker}
referenceChecker = references.VirtualServiceReferences{Namespace: namespace, Namespaces: namespaces, VirtualServices: istioConfigList.VirtualServices, DestinationRules: istioConfigList.DestinationRules, AuthorizationPolicies: rbacDetails.AuthorizationPolicies}
case kubernetes.DestinationRules:
destinationRulesChecker := checkers.DestinationRulesChecker{Namespaces: namespaces, DestinationRules: istioConfigList.DestinationRules, MTLSDetails: mtlsDetails, ServiceEntries: istioConfigList.ServiceEntries}
objectCheckers = []ObjectChecker{noServiceChecker, destinationRulesChecker}
referenceChecker = references.DestinationRuleReferences{Namespace: namespace, Namespaces: namespaces, DestinationRules: istioConfigList.DestinationRules, VirtualServices: istioConfigList.VirtualServices, WorkloadsPerNamespace: workloadsPerNamespace, ServiceEntries: istioConfigList.ServiceEntries, RegistryServices: registryServices}
case kubernetes.ServiceEntries:
serviceEntryChecker := checkers.ServiceEntryChecker{ServiceEntries: istioConfigList.ServiceEntries, Namespaces: namespaces, WorkloadEntries: istioConfigList.WorkloadEntries}
objectCheckers = []ObjectChecker{serviceEntryChecker}
referenceChecker = references.ServiceEntryReferences{AuthorizationPolicies: rbacDetails.AuthorizationPolicies, Namespace: namespace, Namespaces: namespaces, DestinationRules: istioConfigList.DestinationRules, ServiceEntries: istioConfigList.ServiceEntries, Sidecars: istioConfigList.Sidecars, RegistryServices: registryServices}
case kubernetes.Sidecars:
sidecarsChecker := checkers.SidecarChecker{
Sidecars: istioConfigList.Sidecars, Namespaces: namespaces,
WorkloadsPerNamespace: workloadsPerNamespace, ServiceEntries: istioConfigList.ServiceEntries, RegistryServices: registryServices,
}
objectCheckers = []ObjectChecker{sidecarsChecker}
referenceChecker = references.SidecarReferences{Sidecars: istioConfigList.Sidecars, Namespace: namespace, Namespaces: namespaces, ServiceEntries: istioConfigList.ServiceEntries, RegistryServices: registryServices, WorkloadsPerNamespace: workloadsPerNamespace}
case kubernetes.AuthorizationPolicies:
authPoliciesChecker := checkers.AuthorizationPolicyChecker{
AuthorizationPolicies: rbacDetails.AuthorizationPolicies,
Namespaces: namespaces, ServiceEntries: istioConfigList.ServiceEntries,
WorkloadsPerNamespace: workloadsPerNamespace, MtlsDetails: mtlsDetails, VirtualServices: istioConfigList.VirtualServices, RegistryServices: registryServices, PolicyAllowAny: in.isPolicyAllowAny(),
}
objectCheckers = []ObjectChecker{authPoliciesChecker}
referenceChecker = references.AuthorizationPolicyReferences{AuthorizationPolicies: rbacDetails.AuthorizationPolicies, Namespace: namespace, Namespaces: namespaces, VirtualServices: istioConfigList.VirtualServices, ServiceEntries: istioConfigList.ServiceEntries, RegistryServices: registryServices, WorkloadsPerNamespace: workloadsPerNamespace}
case kubernetes.PeerAuthentications:
// Validations on PeerAuthentications
peerAuthnChecker := checkers.PeerAuthenticationChecker{PeerAuthentications: mtlsDetails.PeerAuthentications, MTLSDetails: mtlsDetails, WorkloadsPerNamespace: workloadsPerNamespace}
objectCheckers = []ObjectChecker{peerAuthnChecker}
referenceChecker = references.PeerAuthReferences{MTLSDetails: mtlsDetails, WorkloadsPerNamespace: workloadsPerNamespace}
case kubernetes.WorkloadEntries:
// Validation on WorkloadEntries are not yet in place
case kubernetes.WorkloadGroups:
// Validation on WorkloadGroups are not yet in place
case kubernetes.RequestAuthentications:
// Validation on RequestAuthentications are not yet in place
requestAuthnChecker := checkers.RequestAuthenticationChecker{RequestAuthentications: istioConfigList.RequestAuthentications, WorkloadsPerNamespace: workloadsPerNamespace}
objectCheckers = []ObjectChecker{requestAuthnChecker}
case kubernetes.EnvoyFilters:
// Validation on EnvoyFilters are not yet in place
case kubernetes.WasmPlugins:
// Validation on WasmPlugins is not expected
case kubernetes.Telemetries:
// Validation on Telemetries is not expected
case kubernetes.K8sGateways:
// Validations on K8sGateways
objectCheckers = []ObjectChecker{
checkers.K8sGatewayChecker{K8sGateways: istioConfigList.K8sGateways, GatewayClasses: in.businessLayer.IstioConfig.GatewayAPIClasses()},
}
referenceChecker = references.K8sGatewayReferences{K8sGateways: istioConfigList.K8sGateways, K8sHTTPRoutes: istioConfigList.K8sHTTPRoutes}
case kubernetes.K8sHTTPRoutes:
httpRouteChecker := checkers.K8sHTTPRouteChecker{K8sHTTPRoutes: istioConfigList.K8sHTTPRoutes, K8sGateways: istioConfigList.K8sGateways, Namespaces: namespaces, RegistryServices: registryServices}
objectCheckers = []ObjectChecker{noServiceChecker, httpRouteChecker}
referenceChecker = references.K8sHTTPRouteReferences{K8sHTTPRoutes: istioConfigList.K8sHTTPRoutes, Namespaces: namespaces}
default:
err = fmt.Errorf("object type not found: %v", objectType)
}
close(errChan)
for e := range errChan {
if e != nil { // Check that default value wasn't returned
return nil, istioReferences, err
}
}
if referenceChecker != nil {
istioReferences = runObjectReferenceChecker(referenceChecker)
}
if objectCheckers == nil {
return models.IstioValidations{}, istioReferences, err
}
return runObjectCheckers(objectCheckers).FilterByKey(models.ObjectTypeSingular[objectType], object), istioReferences, nil
}
func runObjectCheckers(objectCheckers []ObjectChecker) models.IstioValidations {
objectTypeValidations := models.IstioValidations{}
// Run checks for each IstioObject type
for _, objectChecker := range objectCheckers {
objectTypeValidations.MergeValidations(runObjectChecker(objectChecker))
}
objectTypeValidations.StripIgnoredChecks()
return objectTypeValidations
}
func runObjectChecker(objectChecker ObjectChecker) models.IstioValidations {
// tracking the time it takes to execute the Check
promtimer := internalmetrics.GetCheckerProcessingTimePrometheusTimer(fmt.Sprintf("%T", objectChecker))
defer promtimer.ObserveDuration()
return objectChecker.Check()
}
func runObjectReferenceChecker(referenceChecker ReferenceChecker) models.IstioReferencesMap {
// tracking the time it takes to execute the Check
promtimer := internalmetrics.GetCheckerProcessingTimePrometheusTimer(fmt.Sprintf("%T", referenceChecker))
defer promtimer.ObserveDuration()
return referenceChecker.References()
}
func (in *IstioValidationsService) fetchServices(ctx context.Context, rValue *models.ServiceList, cluster, namespace string, errChan chan error, wg *sync.WaitGroup) {
defer wg.Done()
if len(errChan) == 0 {
var services *models.ServiceList
var err error
criteria := ServiceCriteria{
IncludeHealth: false,
Namespace: namespace,
Cluster: cluster,
}
services, err = in.businessLayer.Svc.GetServiceList(ctx, criteria)
if err != nil {
select {
case errChan <- err:
default:
}
} else {
*rValue = *services
}
}
}
func (in *IstioValidationsService) fetchAllWorkloads(ctx context.Context, rValue *map[string]models.WorkloadList, cluster string, namespaces *models.Namespaces, errChan chan error, wg *sync.WaitGroup) {
defer wg.Done()
if len(errChan) == 0 {
nss, err := in.businessLayer.Namespace.GetClusterNamespaces(ctx, cluster)
if err != nil {
errChan <- err
return
}
*namespaces = nss
allWorkloads := map[string]models.WorkloadList{}
for _, ns := range nss {
criteria := WorkloadCriteria{Cluster: cluster, Namespace: ns.Name, IncludeIstioResources: false, IncludeHealth: false}
workloadList, err := in.businessLayer.Workload.GetWorkloadList(ctx, criteria)
if err != nil {
select {
case errChan <- err:
default:
}
} else {
allWorkloads[ns.Name] = workloadList
}
}
*rValue = allWorkloads
}
}
func (in *IstioValidationsService) fetchWorkload(ctx context.Context, rValue *map[string]models.WorkloadList, cluster, workload, namespace string, errChan chan error, wg *sync.WaitGroup) {
defer wg.Done()
if len(errChan) == 0 {
allWorkloads := map[string]models.WorkloadList{}
criteria := WorkloadCriteria{Cluster: cluster, WorkloadName: workload, Namespace: namespace, IncludeIstioResources: true, IncludeHealth: false}
workloadList, err := in.businessLayer.Workload.GetWorkloadList(ctx, criteria)
if err != nil {
select {
case errChan <- err:
default:
}
} else {
allWorkloads[namespace] = workloadList
}
*rValue = allWorkloads
}
}
func (in *IstioValidationsService) fetchIstioConfigList(ctx context.Context, rValue *models.IstioConfigList, mtlsDetails *kubernetes.MTLSDetails, rbacDetails *kubernetes.RBACDetails, cluster, namespace string, errChan chan error, wg *sync.WaitGroup) {
defer wg.Done()
if len(errChan) > 0 {
return
}
criteria := IstioConfigCriteria{
AllNamespaces: true,
Cluster: cluster,
IncludeGateways: true,
IncludeDestinationRules: true,
IncludeServiceEntries: true,
IncludeVirtualServices: true,
IncludeSidecars: true,
IncludeRequestAuthentications: true,
IncludeWorkloadEntries: true,
IncludeAuthorizationPolicies: true,
IncludePeerAuthentications: true,
IncludeK8sHTTPRoutes: true,
IncludeK8sGateways: true,
}
istioConfigMap, err := in.businessLayer.IstioConfig.GetIstioConfigMap(ctx, criteria)
if err != nil {
errChan <- err
return
}
istioConfigList := istioConfigMap[criteria.Cluster]
// Filter VS
filteredVSs := in.filterVSExportToNamespaces(namespace, istioConfigList.VirtualServices)
rValue.VirtualServices = append(rValue.VirtualServices, filteredVSs...)
// Filter DR
filteredDRs := in.filterDRExportToNamespaces(namespace, kubernetes.FilterAutogeneratedDestinationRules(istioConfigList.DestinationRules))
rValue.DestinationRules = append(rValue.DestinationRules, filteredDRs...)
mtlsDetails.DestinationRules = append(mtlsDetails.DestinationRules, filteredDRs...)
// Filter SE
filteredSEs := in.filterSEExportToNamespaces(namespace, istioConfigList.ServiceEntries)
rValue.ServiceEntries = append(rValue.ServiceEntries, filteredSEs...)
// All Gateways
rValue.Gateways = append(rValue.Gateways, kubernetes.FilterAutogeneratedGateways(istioConfigList.Gateways)...)
// All K8sGateways
rValue.K8sGateways = append(rValue.K8sGateways, istioConfigList.K8sGateways...)
// All K8sHTTPRoutes
rValue.K8sHTTPRoutes = append(rValue.K8sHTTPRoutes, istioConfigList.K8sHTTPRoutes...)
// All Sidecars
rValue.Sidecars = append(rValue.Sidecars, istioConfigList.Sidecars...)
// All RequestAuthentications
rValue.RequestAuthentications = append(rValue.RequestAuthentications, istioConfigList.RequestAuthentications...)
// All WorkloadEntries
rValue.WorkloadEntries = append(rValue.WorkloadEntries, istioConfigList.WorkloadEntries...)
in.filterPeerAuths(namespace, mtlsDetails, istioConfigList.PeerAuthentications)
in.filterAuthPolicies(namespace, rbacDetails, istioConfigList.AuthorizationPolicies)
}
func (in *IstioValidationsService) filterPeerAuths(namespace string, mtlsDetails *kubernetes.MTLSDetails, peerAuths []*security_v1beta.PeerAuthentication) {
rootNs := config.Get().ExternalServices.Istio.RootNamespace
for _, pa := range peerAuths {
if pa.Namespace == rootNs {
mtlsDetails.MeshPeerAuthentications = append(mtlsDetails.MeshPeerAuthentications, pa)
}
if pa.Namespace == namespace || namespace == "" {
mtlsDetails.PeerAuthentications = append(mtlsDetails.PeerAuthentications, pa)
}
}
}
func (in *IstioValidationsService) filterAuthPolicies(namespace string, rbacDetails *kubernetes.RBACDetails, authPolicies []*security_v1beta.AuthorizationPolicy) {
for _, ap := range authPolicies {
if ap.Namespace == namespace || namespace == "" {
rbacDetails.AuthorizationPolicies = append(rbacDetails.AuthorizationPolicies, ap)
}
}
}
func (in *IstioValidationsService) filterVSExportToNamespaces(namespace string, vs []*networking_v1beta1.VirtualService) []*networking_v1beta1.VirtualService {
if namespace == "" {
return kubernetes.FilterAutogeneratedVirtualServices(vs)
}
var result []*networking_v1beta1.VirtualService
for _, v := range vs {
if kubernetes.IsAutogenerated(v.Name) {
continue
}
if len(v.Spec.ExportTo) > 0 {
for _, exportToNs := range v.Spec.ExportTo {
// take only namespaces where it is exported to, or if it is exported to all namespaces, or export to own namespace
if checkExportTo(exportToNs, namespace, v.Namespace) {
result = append(result, v)
}
}
} else {
// no exportTo field, means object exported to all namespaces
result = append(result, v)
}
}
return result
}
func (in *IstioValidationsService) filterDRExportToNamespaces(namespace string, dr []*networking_v1beta1.DestinationRule) []*networking_v1beta1.DestinationRule {
if namespace == "" {
return dr
}
var result []*networking_v1beta1.DestinationRule
for _, d := range dr {
if len(d.Spec.ExportTo) > 0 {
for _, exportToNs := range d.Spec.ExportTo {
// take only namespaces where it is exported to, or if it is exported to all namespaces, or export to own namespace
if checkExportTo(exportToNs, namespace, d.Namespace) {
result = append(result, d)
}
}
} else {
// no exportTo field, means object exported to all namespaces
result = append(result, d)
}
}
return result
}
func (in *IstioValidationsService) filterSEExportToNamespaces(namespace string, se []*networking_v1beta1.ServiceEntry) []*networking_v1beta1.ServiceEntry {
if namespace == "" {
return se
}
var result []*networking_v1beta1.ServiceEntry
for _, s := range se {
if len(s.Spec.ExportTo) > 0 {
for _, exportToNs := range s.Spec.ExportTo {
// take only namespaces where it is exported to, or if it is exported to all namespaces, or export to own namespace
if checkExportTo(exportToNs, namespace, s.Namespace) {
result = append(result, s)
}
}
} else {
// no exportTo field, means object exported to all namespaces
result = append(result, s)
}
}
return result
}
func (in *IstioValidationsService) fetchNonLocalmTLSConfigs(mtlsDetails *kubernetes.MTLSDetails, cluster string, errChan chan error, wg *sync.WaitGroup) {
defer wg.Done()
if len(errChan) > 0 {
return
}
kubeCache := kialiCache.GetKubeCaches()[cluster]
if kubeCache == nil {
return
}
userClient := in.userClients[cluster]
if userClient == nil {
return
}
cfg := config.Get()
var istioConfig *core_v1.ConfigMap
var err error
if IsNamespaceCached(cfg.IstioNamespace) {
istioConfig, err = kialiCache.GetConfigMap(cfg.IstioNamespace, cfg.ExternalServices.Istio.ConfigMapName)
} else {
istioConfig, err = userClient.GetConfigMap(cfg.IstioNamespace, cfg.ExternalServices.Istio.ConfigMapName)
}
if err != nil {
errChan <- err
return
}
icm, err := kubernetes.GetIstioConfigMap(istioConfig)
if err != nil {
errChan <- err
} else {
mtlsDetails.EnabledAutoMtls = icm.GetEnableAutoMtls()
}
}
func (in *IstioValidationsService) fetchRegistryServices(rValue *[]*kubernetes.RegistryService, errChan chan error, wg *sync.WaitGroup) {
defer wg.Done()
criteria := RegistryCriteria{AllNamespaces: true}
registryServices, err := in.businessLayer.RegistryStatus.GetRegistryServices(criteria)
if err != nil {
select {
case errChan <- err:
default:
}
} else {
*rValue = registryServices
}
}
func (in *IstioValidationsService) isGatewayToNamespace() bool {
mesh, err := in.businessLayer.Mesh.GetMesh(context.TODO())
if err != nil {
log.Errorf("Error getting mesh config: %s", err)
return false
}
// TODO: Multi-primary support
for _, controlPlane := range mesh.ControlPlanes {
if controlPlane.Cluster.IsKialiHome {
return controlPlane.Config.IsGatewayToNamespace
}
}
return false
}
func (in *IstioValidationsService) isPolicyAllowAny() bool {
allowAny := false
if in.businessLayer != nil {
if otp, err := in.businessLayer.Mesh.OutboundTrafficPolicy(); err == nil {
if otp.Mode == "" || otp.Mode == AllowAny {
return true
}
}
}
return allowAny
}
func checkExportTo(exportToNs string, namespace string, ownNs string) bool {
// check if namespaces where it is exported to, or if it is exported to all namespaces, or export to own namespace
return exportToNs == "*" || exportToNs == namespace || (exportToNs == "." && ownNs == namespace)
}