-
Notifications
You must be signed in to change notification settings - Fork 476
/
status.go
229 lines (196 loc) · 6.94 KB
/
status.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
package mtls
import (
networking_v1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
security_v1beta "istio.io/client-go/pkg/apis/security/v1beta1"
"k8s.io/apimachinery/pkg/labels"
"github.com/kiali/kiali/kubernetes"
)
const (
MTLSEnabled = "MTLS_ENABLED"
MTLSPartiallyEnabled = "MTLS_PARTIALLY_ENABLED"
MTLSNotEnabled = "MTLS_NOT_ENABLED"
MTLSDisabled = "MTLS_DISABLED"
)
type MtlsStatus struct {
PeerAuthentications []*security_v1beta.PeerAuthentication
DestinationRules []*networking_v1beta1.DestinationRule
MatchingLabels labels.Labels
AutoMtlsEnabled bool
AllowPermissive bool
RegistryServices []*kubernetes.RegistryService
}
type TlsStatus struct {
DestinationRuleStatus string
PeerAuthenticationStatus string
OverallStatus string
}
type NameNamespace struct {
Name string
Namespace string
}
func (m MtlsStatus) hasPeerAuthnNamespacemTLSDefinition() string {
for _, p := range m.PeerAuthentications {
if _, mode := kubernetes.PeerAuthnHasMTLSEnabled(p); mode != "" {
return mode
}
}
return ""
}
func (m MtlsStatus) hasDesinationRuleEnablingNamespacemTLS(namespace string) string {
for _, dr := range m.DestinationRules {
if _, mode := kubernetes.DestinationRuleHasNamespaceWideMTLSEnabled(namespace, dr); mode != "" {
return mode
}
}
return ""
}
// Returns the mTLS status at workload level (matching the m.MatchingLabels)
func (m MtlsStatus) WorkloadMtlsStatus(namespace string) string {
for _, pa := range m.PeerAuthentications {
var selectorLabels map[string]string
if pa.Spec.Selector != nil {
selectorLabels = pa.Spec.Selector.MatchLabels
} else {
continue
}
selector := labels.Set(selectorLabels).AsSelector()
match := selector.Matches(m.MatchingLabels)
if !match {
continue
}
_, mode := kubernetes.PeerAuthnMTLSMode(pa)
if mode == "STRICT" {
return MTLSEnabled
} else if mode == "DISABLE" {
return MTLSDisabled
} else if mode == "PERMISSIVE" {
if len(m.DestinationRules) == 0 {
return MTLSNotEnabled
} else {
// Filter DR that applies to the Services matching with the selector
// Fetch hosts from DRs and its mtls mode [details, ISTIO_STATUS]
// Filter Svc and extract its workloads selectors
filteredRSvcs := kubernetes.FilterRegistryServicesBySelector(selector, namespace, m.RegistryServices)
nameNamespaces := []NameNamespace{}
for _, rSvc := range filteredRSvcs {
nameNamespaces = append(nameNamespaces, NameNamespace{rSvc.IstioService.Attributes.Name, rSvc.IstioService.Attributes.Namespace})
}
for _, nameNamespace := range nameNamespaces {
filteredDrs := kubernetes.FilterDestinationRulesByService(m.DestinationRules, nameNamespace.Namespace, nameNamespace.Name)
for _, dr := range filteredDrs {
enabled, mode := kubernetes.DestinationRuleHasMTLSEnabled(dr)
if enabled || mode == "MUTUAL" {
return MTLSEnabled
} else if mode == "DISABLE" {
return MTLSDisabled
}
}
}
return MTLSNotEnabled
}
}
}
return MTLSNotEnabled
}
func (m MtlsStatus) NamespaceMtlsStatus(namespace string) TlsStatus {
drStatus := m.hasDesinationRuleEnablingNamespacemTLS(namespace)
paStatus := m.hasPeerAuthnNamespacemTLSDefinition()
return m.finalStatus(drStatus, paStatus)
}
func (m MtlsStatus) finalStatus(drStatus, paStatus string) TlsStatus {
finalStatus := MTLSPartiallyEnabled
mtlsEnabled := drStatus == "ISTIO_MUTUAL" || drStatus == "MUTUAL" || (drStatus == "" && m.AutoMtlsEnabled)
mtlsDisabled := drStatus == "DISABLE" || (drStatus == "" && m.AutoMtlsEnabled)
if (paStatus == "STRICT" || (paStatus == "PERMISSIVE" && m.AllowPermissive)) && mtlsEnabled {
finalStatus = MTLSEnabled
} else if paStatus == "DISABLE" && mtlsDisabled {
finalStatus = MTLSDisabled
} else if paStatus == "" && drStatus == "" {
finalStatus = MTLSNotEnabled
}
return TlsStatus{
DestinationRuleStatus: drStatus,
PeerAuthenticationStatus: paStatus,
OverallStatus: finalStatus,
}
}
func (m MtlsStatus) MeshMtlsStatus() TlsStatus {
drStatus := m.hasDestinationRuleMeshTLSDefinition()
paStatus := m.hasPeerAuthnMeshTLSDefinition()
return TlsStatus{
DestinationRuleStatus: drStatus,
PeerAuthenticationStatus: paStatus,
OverallStatus: m.OverallMtlsStatus(TlsStatus{}, m.finalStatus(drStatus, paStatus)),
}
}
func (m MtlsStatus) hasPeerAuthnMeshTLSDefinition() string {
for _, mp := range m.PeerAuthentications {
if _, mode := kubernetes.PeerAuthnHasMTLSEnabled(mp); mode != "" {
return mode
}
}
return ""
}
func (m MtlsStatus) hasDestinationRuleMeshTLSDefinition() string {
for _, dr := range m.DestinationRules {
if _, mode := kubernetes.DestinationRuleHasMTLSEnabledForHost("*.local", dr); mode != "" {
return mode
}
}
return ""
}
func (m MtlsStatus) OverallMtlsStatus(nsStatus, meshStatus TlsStatus) string {
var status = MTLSPartiallyEnabled
if nsStatus.hasDefinedTls() {
status = nsStatus.OverallStatus
} else if nsStatus.hasPartialTlsConfig() {
status = m.inheritedOverallStatus(nsStatus, meshStatus)
} else if meshStatus.hasDefinedTls() {
status = meshStatus.OverallStatus
} else if meshStatus.hasNoConfig() {
status = MTLSNotEnabled
} else if meshStatus.hasPartialDisabledConfig() {
status = MTLSDisabled
} else if meshStatus.hasHalfTlsConfigDefined(m.AutoMtlsEnabled, m.AllowPermissive) {
status = MTLSEnabled
} else if !m.AutoMtlsEnabled && meshStatus.hasPartialTlsConfig() {
status = MTLSPartiallyEnabled
}
return status
}
func (m MtlsStatus) inheritedOverallStatus(nsStatus, meshStatus TlsStatus) string {
var partialDRStatus, partialPAStatus = nsStatus.DestinationRuleStatus, nsStatus.PeerAuthenticationStatus
if nsStatus.DestinationRuleStatus == "" {
partialDRStatus = meshStatus.DestinationRuleStatus
}
if nsStatus.PeerAuthenticationStatus == "" {
partialPAStatus = meshStatus.PeerAuthenticationStatus
}
return m.OverallMtlsStatus(TlsStatus{},
m.finalStatus(partialDRStatus, partialPAStatus),
)
}
func (t TlsStatus) hasDefinedTls() bool {
return t.OverallStatus == MTLSEnabled || t.OverallStatus == MTLSDisabled
}
func (t TlsStatus) hasPartialTlsConfig() bool {
return t.OverallStatus == MTLSPartiallyEnabled
}
func (t TlsStatus) hasHalfTlsConfigDefined(autoMtls, allowPermissive bool) bool {
defined := false
if autoMtls {
defined = t.PeerAuthenticationStatus == "STRICT" && t.DestinationRuleStatus == "" ||
(t.DestinationRuleStatus == "ISTIO_MUTUAL" || t.DestinationRuleStatus == "MUTUAL") && t.PeerAuthenticationStatus == ""
if !defined && allowPermissive {
defined = t.PeerAuthenticationStatus == "PERMISSIVE" && t.DestinationRuleStatus == ""
}
}
return defined
}
func (t TlsStatus) hasNoConfig() bool {
return t.PeerAuthenticationStatus == "" && t.DestinationRuleStatus == ""
}
func (t TlsStatus) hasPartialDisabledConfig() bool {
return t.PeerAuthenticationStatus == "DISABLE" && t.DestinationRuleStatus == "" ||
t.DestinationRuleStatus == "DISABLE" && t.PeerAuthenticationStatus == ""
}