-
Notifications
You must be signed in to change notification settings - Fork 0
/
certsexpiration.go
163 lines (147 loc) · 4.69 KB
/
certsexpiration.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
package certsexpiration
import (
"context"
"encoding/json"
"reflect"
"strings"
"time"
"github.com/rancher/kontainer-engine/cluster"
"github.com/rancher/rancher/pkg/controllers/management/clusterprovisioner"
rkecluster "github.com/rancher/rke/cluster"
"github.com/rancher/rke/pki"
v1 "github.com/rancher/types/apis/core/v1"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/util/cert"
)
type Controller struct {
ClusterName string
ClusterLister v3.ClusterLister
ClusterClient v3.ClusterInterface
ClusterStore cluster.PersistentStore
SecretLister v1.SecretLister
}
func Register(ctx context.Context, userContext *config.UserContext) {
c := &Controller{
ClusterName: userContext.ClusterName,
ClusterLister: userContext.Management.Management.Clusters("").Controller().Lister(),
ClusterClient: userContext.Management.Management.Clusters(""),
ClusterStore: clusterprovisioner.NewPersistentStore(userContext.Management.Core.Namespaces(""), userContext.Management.Core),
SecretLister: userContext.Core.Secrets("").Controller().Lister(),
}
userContext.Management.Management.Clusters("").AddHandler(ctx, "certificate-expiration", c.sync)
}
func (c Controller) sync(key string, cluster *v3.Cluster) (runtime.Object, error) {
if key == "" || cluster == nil || cluster.DeletionTimestamp != nil || cluster.Name != c.ClusterName {
return cluster, nil
}
if cluster.Spec.RancherKubernetesEngineConfig == nil {
return cluster, nil
}
certsExpInfo := map[string]v3.CertExpiration{}
cluster, err := c.ClusterLister.Get("", key)
if err != nil {
return cluster, err
}
certBundle, err := c.getClusterCertificateBundle(cluster.Name)
if err != nil {
return cluster, err
}
for certName, certObj := range certBundle {
info, err := getCertExpiration(certObj.CertificatePEM)
if err != nil {
logrus.Debugf("failed to get expiration date for certificate [%s] for cluster [%s]:%v", certName, key, err)
continue
}
certsExpInfo[certName] = info
}
if !reflect.DeepEqual(cluster.Status.CertificatesExpiration, certsExpInfo) {
toUpdate := cluster.DeepCopy()
toUpdate.Status.CertificatesExpiration = certsExpInfo
return c.ClusterClient.Update(toUpdate)
}
return cluster, nil
}
func (c Controller) getClusterCertificateBundle(clusterName string) (map[string]pki.CertificatePKI, error) {
// cluster has a state file ?
currentState, err := getRKECurrentStateFromStore(c.ClusterStore, clusterName)
if err != nil {
return nil, err
}
if currentState != nil {
cleanCertificateBundle(currentState.CertificatesBundle)
return currentState.CertificatesBundle, nil
}
// No state file, let's try get the certs from the user cluster
certs, err := c.getCertsFromUserCluster()
if err != nil {
return nil, err
}
cleanCertificateBundle(certs)
return certs, nil
}
func getRKECurrentStateFromStore(store cluster.PersistentStore, clusterName string) (*rkecluster.State, error) {
cluster, err := store.Get(clusterName)
if err != nil {
return nil, err
}
var fullState rkecluster.FullState
stateStr, ok := cluster.Metadata["fullState"]
if !ok {
return nil, nil
}
err = json.Unmarshal([]byte(stateStr), &fullState)
if err != nil {
return nil, err
}
return &fullState.CurrentState, nil
}
func (c Controller) getCertsFromUserCluster() (map[string]pki.CertificatePKI, error) {
certs := map[string]pki.CertificatePKI{}
secrets, err := c.SecretLister.List("kube-system", labels.Everything())
if err != nil {
return nil, err
}
for _, secret := range secrets {
if strings.HasPrefix(secret.GetName(), "kube-") &&
secret.Type == corev1.SecretTypeOpaque {
cert, ok := secret.Data["Certificate"]
if !ok {
logrus.Debugf("secret [%s] for cluster [%s] doesn't contain a certificate", secret.GetName(), c.ClusterName)
continue
}
certs[secret.GetName()] = pki.CertificatePKI{CertificatePEM: string(cert)}
}
}
return certs, nil
}
func cleanCertificateBundle(certs map[string]pki.CertificatePKI) {
for name := range certs {
if strings.Contains(name, "client") ||
strings.Contains(name, "token") ||
strings.Contains(name, "header") ||
strings.Contains(name, "admin") {
delete(certs, name)
}
}
}
func getCertificateExpDate(c string) (*time.Time, error) {
certs, err := cert.ParseCertsPEM([]byte(c))
if err != nil {
return nil, err
}
return &certs[0].NotAfter, nil
}
func getCertExpiration(c string) (v3.CertExpiration, error) {
date, err := getCertificateExpDate(c)
if err != nil {
return v3.CertExpiration{}, err
}
return v3.CertExpiration{
ExpirationDate: date.Format(time.RFC3339),
}, nil
}