-
Notifications
You must be signed in to change notification settings - Fork 474
/
tls.go
241 lines (198 loc) · 6.19 KB
/
tls.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
package business
import (
"sync"
"github.com/kiali/kiali/config"
"github.com/kiali/kiali/kubernetes"
"github.com/kiali/kiali/log"
"github.com/kiali/kiali/models"
)
type TLSService struct {
k8s kubernetes.ClientInterface
businessLayer *Layer
enabledAutoMtls *bool
}
const (
MTLSEnabled = "MTLS_ENABLED"
MTLSPartiallyEnabled = "MTLS_PARTIALLY_ENABLED"
MTLSNotEnabled = "MTLS_NOT_ENABLED"
MTLSDisabled = "MTLS_DISABLED"
)
func (in *TLSService) MeshWidemTLSStatus(namespaces []string) (models.MTLSStatus, error) {
var drp = true
var paErr, drErr error
pap, paErr := in.hasMeshPeerAuthnEnabled()
if paErr != nil {
return models.MTLSStatus{}, paErr
}
if !in.hasAutoMTLSEnabled() {
drp, drErr = in.hasDestinationRuleEnabled(namespaces)
if drErr != nil {
return models.MTLSStatus{}, drErr
}
}
finalStatus := MTLSNotEnabled
if drp && pap {
finalStatus = MTLSEnabled
} else if drp || pap {
if !in.hasAutoMTLSEnabled() {
finalStatus = MTLSPartiallyEnabled
}
}
return models.MTLSStatus{
Status: finalStatus,
}, nil
}
func (in *TLSService) hasMeshPeerAuthnEnabled() (bool, error) {
var mps []kubernetes.IstioObject
var err error
if !in.k8s.IsMaistraApi() {
if mps, err = in.k8s.GetIstioObjects(config.Get().IstioNamespace, kubernetes.PeerAuthentications, ""); err != nil {
return false, nil
}
} else {
// ServiceMeshPolicies are namespace scoped.
// And Maistra will only consider resources under control-plane namespace
// https://github.com/Maistra/istio/pull/39/files#diff-e3109392080297ee093b7189648289e1R40
// see https://github.com/Maistra/istio/blob/maistra-1.0/pilot/pkg/model/config.go#L958
// see https://github.com/Maistra/istio/blob/maistra-1.0/pilot/pkg/model/config.go#L990
// note - Maistra does not allow Istio multi-namespace deployment, use the single Istio namespace.
controlPlaneNs := config.Get().IstioNamespace
if mps, err = in.k8s.GetIstioObjects(controlPlaneNs, kubernetes.ServiceMeshPolicies, ""); err != nil {
// This query can return false if user can't access to controlPlaneNs
// On this case we log internally the error but we return a false with nil
log.Warningf("GetServiceMeshPolicies failed during a TLS validation. Probably user can't access to %s namespace. Error: %s", controlPlaneNs, err)
return false, nil
}
}
for _, mp := range mps {
if strictMode := kubernetes.PeerAuthnHasStrictMTLS(mp); strictMode {
return true, nil
}
}
return false, nil
}
func (in *TLSService) hasDestinationRuleEnabled(namespaces []string) (bool, error) {
drs, err := in.getAllDestinationRules(namespaces)
if err != nil {
return false, err
}
for _, dr := range drs {
if enabled, _ := kubernetes.DestinationRuleHasMeshWideMTLSEnabled(dr); enabled {
return true, nil
}
}
return false, nil
}
func (in *TLSService) getAllDestinationRules(namespaces []string) ([]kubernetes.IstioObject, error) {
drChan := make(chan []kubernetes.IstioObject, len(namespaces))
errChan := make(chan error, 1)
wg := sync.WaitGroup{}
wg.Add(len(namespaces))
for _, namespace := range namespaces {
go func(ns string) {
defer wg.Done()
var drs []kubernetes.IstioObject
var err error
// Check if namespace is cached
// Namespace access is checked in the upper call
if kialiCache != nil && kialiCache.CheckIstioResource(kubernetes.DestinationRuleType) && kialiCache.CheckNamespace(ns) {
drs, err = kialiCache.GetIstioResources(kubernetes.DestinationRuleType, ns)
} else {
drs, err = in.k8s.GetIstioObjects(ns, kubernetes.DestinationRules, "")
}
if err != nil {
errChan <- err
return
}
drChan <- drs
}(namespace)
}
wg.Wait()
close(errChan)
close(drChan)
for err := range errChan {
if err != nil {
return nil, err
}
}
allDestinationRules := make([]kubernetes.IstioObject, 0)
for drs := range drChan {
allDestinationRules = append(allDestinationRules, drs...)
}
return allDestinationRules, nil
}
func (in TLSService) NamespaceWidemTLSStatus(namespace string) (models.MTLSStatus, error) {
var plMode, drMode string
var pErr, dErr error
plMode, pErr = in.hasPeerAuthnNamespacemTLSDefinition(namespace)
if pErr != nil {
return models.MTLSStatus{}, pErr
}
drMode, dErr = in.hasDesinationRuleEnablingNamespacemTLS(namespace)
if dErr != nil {
return models.MTLSStatus{}, dErr
}
return models.MTLSStatus{
Status: in.finalStatus(drMode, plMode),
}, nil
}
func (in TLSService) hasPeerAuthnNamespacemTLSDefinition(namespace string) (string, error) {
// PeerAuthn at istio control plane level, are considered mesh-wide objects
if namespace == config.Get().IstioNamespace {
return "", nil
}
ps, err := in.k8s.GetIstioObjects(namespace, kubernetes.PeerAuthentications, "")
if err != nil {
return "", err
}
for _, p := range ps {
if _, mode := kubernetes.PeerAuthnHasMTLSEnabled(p); mode != "" {
return mode, nil
}
}
return "", nil
}
func (in TLSService) hasDesinationRuleEnablingNamespacemTLS(namespace string) (string, error) {
nss, nssErr := in.businessLayer.Namespace.GetNamespaces()
if nssErr != nil {
return "", nssErr
}
nsNames := make([]string, 0)
for _, ns := range nss {
nsNames = append(nsNames, ns.Name)
}
drs, nssErr := in.getAllDestinationRules(nsNames)
if nssErr != nil {
return "", nssErr
}
for _, dr := range drs {
if _, mode := kubernetes.DestinationRuleHasNamespaceWideMTLSEnabled(namespace, dr); mode != "" {
return mode, nil
}
}
return "", nil
}
func (in TLSService) finalStatus(drStatus string, paStatus string) string {
finalStatus := MTLSPartiallyEnabled
autoMtls := in.hasAutoMTLSEnabled()
mtlsEnabled := drStatus == "ISTIO_MUTUAL" || drStatus == "MUTUAL" || (drStatus == "" && autoMtls)
mtlsDisabled := drStatus == "DISABLE" || (drStatus == "" && autoMtls)
if paStatus == "STRICT" && mtlsEnabled {
finalStatus = MTLSEnabled
} else if paStatus == "DISABLE" && mtlsDisabled {
finalStatus = MTLSDisabled
} else if paStatus == "" && drStatus == "" {
finalStatus = MTLSNotEnabled
}
return finalStatus
}
func (in TLSService) hasAutoMTLSEnabled() bool {
if in.enabledAutoMtls != nil {
return *in.enabledAutoMtls
}
mc, err := in.k8s.GetIstioConfigMap()
if err != nil {
return true
}
return mc.GetEnableAutoMtls()
}