Skip to content

Commit 1fbb22e

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request kubernetes#39702 from mikedanese/kubelet-csr
Automatic merge from submit-queue (batch tested with PRs 39684, 39577, 38989, 39534, 39702) kubelet: request client auth certificates from certificate API. This fixes kubeadm and --experiment-kubelet-bootstrap. cc @liggitt
2 parents f74a556 + d2032fd commit 1fbb22e

4 files changed

Lines changed: 111 additions & 4 deletions

File tree

pkg/controller/certificates/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ filegroup(
5656

5757
go_test(
5858
name = "go_default_test",
59-
srcs = ["cfssl_signer_test.go"],
59+
srcs = [
60+
"cfssl_signer_test.go",
61+
"groupapprove_test.go",
62+
],
6063
data = [
6164
"testdata/ca.crt",
6265
"testdata/ca.key",

pkg/controller/certificates/groupapprove.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ func (cc *groupApprover) AutoApprove(csr *certificates.CertificateSigningRequest
7676
if len(x509cr.DNSNames)+len(x509cr.EmailAddresses)+len(x509cr.IPAddresses) != 0 {
7777
return csr, nil
7878
}
79+
if !hasExactUsages(csr, kubeletClientUsages) {
80+
return csr, nil
81+
}
7982

8083
csr.Status.Conditions = append(csr.Status.Conditions, certificates.CertificateSigningRequestCondition{
8184
Type: certificates.CertificateApproved,
@@ -84,3 +87,28 @@ func (cc *groupApprover) AutoApprove(csr *certificates.CertificateSigningRequest
8487
})
8588
return cc.client.UpdateApproval(csr)
8689
}
90+
91+
var kubeletClientUsages = []certificates.KeyUsage{
92+
certificates.UsageKeyEncipherment,
93+
certificates.UsageDigitalSignature,
94+
certificates.UsageClientAuth,
95+
}
96+
97+
func hasExactUsages(csr *certificates.CertificateSigningRequest, usages []certificates.KeyUsage) bool {
98+
if len(usages) != len(csr.Spec.Usages) {
99+
return false
100+
}
101+
102+
usageMap := map[certificates.KeyUsage]struct{}{}
103+
for _, u := range usages {
104+
usageMap[u] = struct{}{}
105+
}
106+
107+
for _, u := range csr.Spec.Usages {
108+
if _, ok := usageMap[u]; !ok {
109+
return false
110+
}
111+
}
112+
113+
return true
114+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package certificates
18+
19+
import (
20+
"testing"
21+
22+
certificates "k8s.io/kubernetes/pkg/apis/certificates/v1alpha1"
23+
)
24+
25+
func TestHasKubeletUsages(t *testing.T) {
26+
cases := []struct {
27+
usages []certificates.KeyUsage
28+
expected bool
29+
}{
30+
{
31+
usages: nil,
32+
expected: false,
33+
},
34+
{
35+
usages: []certificates.KeyUsage{},
36+
expected: false,
37+
},
38+
{
39+
usages: []certificates.KeyUsage{
40+
certificates.UsageKeyEncipherment,
41+
certificates.UsageDigitalSignature,
42+
},
43+
expected: false,
44+
},
45+
{
46+
usages: []certificates.KeyUsage{
47+
certificates.UsageKeyEncipherment,
48+
certificates.UsageDigitalSignature,
49+
certificates.UsageServerAuth,
50+
},
51+
expected: false,
52+
},
53+
{
54+
usages: []certificates.KeyUsage{
55+
certificates.UsageKeyEncipherment,
56+
certificates.UsageDigitalSignature,
57+
certificates.UsageClientAuth,
58+
},
59+
expected: true,
60+
},
61+
}
62+
for _, c := range cases {
63+
if hasExactUsages(&certificates.CertificateSigningRequest{
64+
Spec: certificates.CertificateSigningRequestSpec{
65+
Usages: c.usages,
66+
},
67+
}, kubeletClientUsages) != c.expected {
68+
t.Errorf("unexpected result of hasKubeletUsages(%v), expecting: %v", c.usages, c.expected)
69+
}
70+
}
71+
}

pkg/kubelet/util/csr/csr.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,14 @@ func RequestNodeCertificate(client unversionedcertificates.CertificateSigningReq
5454
TypeMeta: metav1.TypeMeta{Kind: "CertificateSigningRequest"},
5555
ObjectMeta: v1.ObjectMeta{GenerateName: "csr-"},
5656

57-
// TODO: For now, this is a request for a certificate with allowed usage of "TLS Web Client Authentication".
58-
// Need to figure out whether/how to surface the allowed usage in the spec.
59-
Spec: certificates.CertificateSigningRequestSpec{Request: csr},
57+
Spec: certificates.CertificateSigningRequestSpec{
58+
Request: csr,
59+
Usages: []certificates.KeyUsage{
60+
certificates.UsageDigitalSignature,
61+
certificates.UsageKeyEncipherment,
62+
certificates.UsageClientAuth,
63+
},
64+
},
6065
})
6166
if err != nil {
6267
return nil, fmt.Errorf("cannot create certificate signing request: %v", err)

0 commit comments

Comments
 (0)