-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathingress-tls-rule-domain-match.yaml
116 lines (107 loc) · 2.38 KB
/
ingress-tls-rule-domain-match.yaml
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
apiVersion: kubevious.io/v1alpha1
kind: ClusterRule
metadata:
name: ingress-tls-rule-domain-match
spec:
summary: |
Validate Ingress TLS and rule domain match.
categories:
- k8s
- ingress
- ingress-extension
- tls
- domain
target: |
Union(
Api('networking.k8s.io')
.Kind('Ingress'),
Api('extensions')
.Kind('Ingress')
)
rule: |
if (!config.spec?.tls) {
return;
}
let hasNoHostRule = false;
let ruleHostDict = {};
let ruleDomains = [];
for(const rule of config.spec?.rules ?? [])
{
const ruleHost = rule.host;
if (!ruleHost) {
hasNoHostRule = true;
} else {
registerRuleDomain(ruleHost);
}
}
if (item.annotations["nginx.ingress.kubernetes.io/from-to-www-redirect"] == "true")
{
for(const ruleDomain of ruleDomains)
{
if (ruleDomain.subDomain == "www")
{
registerRuleDomain(ruleDomain.domainBase);
}
}
}
for(const tls of config.spec?.tls ?? [])
{
for(const tlsHost of tls.hosts ?? [])
{
if (!matchesHost(tlsHost)) {
error(`Could not match TLS host ${tlsHost} to any of the rules.`);
}
}
}
// ** UTILS **
function registerRuleDomain(domain)
{
if (!ruleHostDict[domain])
{
ruleHostDict[domain] = true;
ruleDomains.push(parseDomain(domain));
}
}
function parseDomain(domain)
{
let parts = domain.split('.');
const sub = _.head(parts);
parts = _.drop(parts);
return {
domain: domain,
isWildcard: (sub == '*'),
subDomain: sub,
domainBase: parts.join('.')
}
}
function matchesHost(domain)
{
if (hasNoHostRule) {
return true;
}
if (ruleHostDict[domain]) {
return true;
}
let tlsDomain = parseDomain(domain);
if (tlsDomain.isWildcard)
{
for(const ruleDomain of ruleDomains)
{
if (ruleDomain.domainBase == tlsDomain.domainBase)
{
return true;
}
}
}
else
{
for(const ruleDomain of ruleDomains)
{
if (ruleDomain.isWildcard && (ruleDomain.domainBase == tlsDomain.domainBase))
{
return true;
}
}
}
return false;
}