-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls.go
85 lines (73 loc) · 2.01 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
package matcher
import (
"crypto/tls"
"github.com/blackstorm/ingress-go/pkg/common"
"github.com/blackstorm/ingress-go/pkg/k8s/api/convert"
"github.com/blackstorm/ingress-go/pkg/store"
"github.com/blackstorm/ingress-go/pkg/watcher"
netv1 "k8s.io/api/networking/v1"
"k8s.io/klog/v2"
)
type TLSMatcher struct {
store *store.CertificateStore
hostTlsStoreKeys map[string]store.CertificateStoreKey
}
func NewTLSMatcher(s *store.CertificateStore) *TLSMatcher {
return &TLSMatcher{
store: s,
hostTlsStoreKeys: make(map[string]store.CertificateStoreKey),
}
}
func (m *TLSMatcher) Match(sni string, wildcard bool) *tls.Certificate {
if key, ok := m.hostTlsStoreKeys[sni]; ok {
cert := m.store.Get(key)
if cert != nil {
return cert
}
}
if wildcard {
return nil
}
return m.Match(common.ToWildcardSni(sni), true)
}
func (m *TLSMatcher) Update(event watcher.Event, updates ...interface{}) {
ingresses := convert.Ingresses(updates...)
switch event {
case watcher.Add:
m.add(ingresses[0])
case watcher.Update:
// delete old
m.delete(ingresses[1])
// add new
m.add(ingresses[0])
case watcher.Delete:
m.delete(ingresses[0])
}
}
// TODO If two ingress or more has same host but diffent tls??
func (m *TLSMatcher) add(ingress *netv1.Ingress) {
for _, tls := range ingress.Spec.TLS {
// check tls secret is defined
if tls.SecretName == "" {
klog.Warningf("tls %s SecretName not defined %s", tls.Hosts, klog.KObj(ingress))
continue
}
key := store.BuildCertificateStoreKey(ingress.Namespace, tls.SecretName)
for _, host := range tls.Hosts {
// klog.Infof("register host %s tls spec %s", host, spec.storeKey)
m.hostTlsStoreKeys[host] = key
}
}
}
func (m *TLSMatcher) delete(ingress *netv1.Ingress) {
for _, tls := range ingress.Spec.TLS {
key := store.BuildCertificateStoreKey(ingress.Namespace, tls.SecretName)
for _, host := range tls.Hosts {
if k, ok := m.hostTlsStoreKeys[host]; ok {
if k == key {
delete(m.hostTlsStoreKeys, host)
}
}
}
}
}