forked from kubermatic/machine-controller
/
controller.go
279 lines (236 loc) · 8.9 KB
/
controller.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
Copyright 2020 The Machine Controller Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package nodecsrapprover
import (
"context"
"crypto/x509"
"encoding/pem"
"fmt"
"strings"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"go.uber.org/zap"
"github.com/magnusr/kubermatic-machine-controller/pkg/apis/cluster/v1alpha1"
certificatesv1 "k8s.io/api/certificates/v1"
corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
certificatesv1client "k8s.io/client-go/kubernetes/typed/certificates/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
const (
// ControllerName is name of the NodeCSRApprover controller.
ControllerName = "node-csr-approver-controller"
nodeUser = "system:node"
nodeUserPrefix = nodeUser + ":"
nodeGroup = "system:nodes"
authenticatedGroup = "system:authenticated"
)
var (
allowedUsages = []certificatesv1.KeyUsage{
certificatesv1.UsageDigitalSignature,
certificatesv1.UsageKeyEncipherment,
certificatesv1.UsageServerAuth,
}
)
type reconciler struct {
client.Client
log *zap.SugaredLogger
// Have to use the typed client because csr approval is a subresource
// the dynamic client does not approve
certClient certificatesv1client.CertificateSigningRequestInterface
}
func Add(mgr manager.Manager, log *zap.SugaredLogger) error {
certClient, err := certificatesv1client.NewForConfig(mgr.GetConfig())
if err != nil {
return fmt.Errorf("failed to create certificate client: %w", err)
}
rec := &reconciler{
Client: mgr.GetClient(),
log: log.Named(ControllerName),
certClient: certClient.CertificateSigningRequests(),
}
watchType := &certificatesv1.CertificateSigningRequest{}
cntrl, err := controller.New(ControllerName, mgr, controller.Options{
Reconciler: rec,
LogConstructor: func(request *reconcile.Request) logr.Logger {
// we log ourselves
return zapr.NewLogger(zap.NewNop())
},
})
if err != nil {
return fmt.Errorf("failed to construct controller: %w", err)
}
return cntrl.Watch(source.Kind(mgr.GetCache(), watchType), &handler.EnqueueRequestForObject{})
}
func (r *reconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
log := r.log.With("csr", request.NamespacedName)
log.Debug("Reconciling")
// Get the CSR object
csr := &certificatesv1.CertificateSigningRequest{}
if err := r.Get(ctx, request.NamespacedName, csr); err != nil {
if kerrors.IsNotFound(err) {
return reconcile.Result{}, nil
}
log.Errorw("Failed to get CertificateSigningRequest", zap.Error(err))
return reconcile.Result{}, err
}
err := r.reconcile(ctx, log, csr)
if err != nil {
log.Errorw("Reconciling failed", zap.Error(err))
}
return reconcile.Result{}, err
}
func (r *reconciler) reconcile(ctx context.Context, log *zap.SugaredLogger, csr *certificatesv1.CertificateSigningRequest) error {
// If CSR is approved, skip it
for _, condition := range csr.Status.Conditions {
if condition.Type == certificatesv1.CertificateApproved {
log.Debug("CSR already approved, skipping reconciling")
return nil
}
}
// Validate the CSR object and get the node name
nodeName, err := r.validateCSRObject(csr)
if err != nil {
log.Debugw("Skipping reconciling CSR because object is invalid", zap.Error(err))
return nil
}
// Get machine name for the appropriate node
machine, found, err := r.getMachineForNode(ctx, nodeName)
if err != nil {
return fmt.Errorf("failed to get machine for node '%s': %w", nodeName, err)
}
if !found {
return fmt.Errorf("no machine found for given node '%s'", nodeName)
}
// Parse the certificate request
csrBlock, rest := pem.Decode(csr.Spec.Request)
if csrBlock == nil {
return fmt.Errorf("no certificate request found for the given CSR")
}
if len(rest) != 0 {
return fmt.Errorf("found more than one PEM encoded block in the result")
}
certRequest, err := x509.ParseCertificateRequest(csrBlock.Bytes)
if err != nil {
return err
}
// Validate the certificate request
if err := r.validateX509CSR(csr, certRequest, machine); err != nil {
return fmt.Errorf("error validating the x509 certificate request: %w", err)
}
// Approve CSR
nodeLog := log.With("node", nodeName)
nodeLog.Debug("Approving CSR")
approvalCondition := certificatesv1.CertificateSigningRequestCondition{
Type: certificatesv1.CertificateApproved,
Reason: "machine-controller NodeCSRApprover controller approved node serving cert",
Status: corev1.ConditionTrue,
}
csr.Status.Conditions = append(csr.Status.Conditions, approvalCondition)
if _, err := r.certClient.UpdateApproval(ctx, csr.Name, csr, metav1.UpdateOptions{}); err != nil {
return fmt.Errorf("failed to approve CSR %q: %w", csr.Name, err)
}
nodeLog.Info("Successfully approved CSR")
return nil
}
// validateCSRObject valides the CSR object and returns name of the node that requested the certificate.
func (r *reconciler) validateCSRObject(csr *certificatesv1.CertificateSigningRequest) (string, error) {
// Get and validate the node name.
if !strings.HasPrefix(csr.Spec.Username, nodeUserPrefix) {
return "", fmt.Errorf("username must have the '%s' prefix", nodeUserPrefix)
}
nodeName := strings.TrimPrefix(csr.Spec.Username, nodeUserPrefix)
if len(nodeName) == 0 {
return "", fmt.Errorf("node name is empty")
}
// Ensure system:nodes and system:authenticated are in groups.
if len(csr.Spec.Groups) < 2 {
return "", fmt.Errorf("there are less than 2 groups")
}
if !sets.NewString(csr.Spec.Groups...).HasAll(nodeGroup, authenticatedGroup) {
return "", fmt.Errorf("'%s' and/or '%s' are not in its groups", nodeGroup, authenticatedGroup)
}
// Check that present usages matching allowed usages
for _, usage := range csr.Spec.Usages {
if !isUsageInUsageList(usage, allowedUsages) {
return "", fmt.Errorf("usage %v is not in the list of allowed usages (%v)", usage, allowedUsages)
}
}
return nodeName, nil
}
// validateX509CSR validates the certificate request by comparing CN with username,
// and organization with groups.
func (r *reconciler) validateX509CSR(csr *certificatesv1.CertificateSigningRequest, certReq *x509.CertificateRequest, machine v1alpha1.Machine) error {
// Validate Subject CommonName.
if certReq.Subject.CommonName != csr.Spec.Username {
return fmt.Errorf("commonName '%s' is different then CSR username '%s'", certReq.Subject.CommonName, csr.Spec.Username)
}
// Validate Subject Organization.
if len(certReq.Subject.Organization) != 1 {
return fmt.Errorf("expected only one organization but got %d instead", len(certReq.Subject.Organization))
}
if certReq.Subject.Organization[0] != nodeGroup {
return fmt.Errorf("organization '%s' doesn't match node group '%s'", certReq.Subject.Organization[0], nodeGroup)
}
machineAddressSet := sets.NewString(machine.Status.NodeRef.Name)
for _, addr := range machine.Status.Addresses {
machineAddressSet.Insert(addr.Address)
}
// Validate SAN DNS names.
for _, dns := range certReq.DNSNames {
if len(dns) == 0 {
continue
}
if !machineAddressSet.Has(dns) {
return fmt.Errorf("dns name '%s' cannot be associated with node '%s'", dns, machine.Status.NodeRef.Name)
}
}
// Validate SAN IP addresses
for _, ip := range certReq.IPAddresses {
if len(ip) == 0 {
continue
}
if !machineAddressSet.Has(ip.String()) {
return fmt.Errorf("ip address '%v' cannot be associated with node '%s'", ip, machine.Status.NodeRef.Name)
}
}
return nil
}
func (r *reconciler) getMachineForNode(ctx context.Context, nodeName string) (v1alpha1.Machine, bool, error) {
// List all Machines in all namespaces.
machines := &v1alpha1.MachineList{}
if err := r.Client.List(ctx, machines); err != nil {
return v1alpha1.Machine{}, false, fmt.Errorf("failed to list all machine objects: %w", err)
}
for _, machine := range machines.Items {
if machine.Status.NodeRef != nil && machine.Status.NodeRef.Name == nodeName {
return machine, true, nil
}
}
return v1alpha1.Machine{}, false, fmt.Errorf("failed to get machine for given node name '%s'", nodeName)
}
func isUsageInUsageList(usage certificatesv1.KeyUsage, usageList []certificatesv1.KeyUsage) bool {
for _, usageListItem := range usageList {
if usage == usageListItem {
return true
}
}
return false
}