-
Notifications
You must be signed in to change notification settings - Fork 887
/
certificates.go
306 lines (262 loc) · 10.1 KB
/
certificates.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
Copyright 2016 The Kubernetes 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 certificates
import (
"context"
"fmt"
"io"
"github.com/spf13/cobra"
certificatesv1 "k8s.io/api/certificates/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/cli-runtime/pkg/resource"
v1 "k8s.io/client-go/kubernetes/typed/certificates/v1"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/scheme"
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
// NewCmdCertificate returns `certificate` Cobra command
func NewCmdCertificate(restClientGetter genericclioptions.RESTClientGetter, ioStreams genericiooptions.IOStreams) *cobra.Command {
cmd := &cobra.Command{
Use: "certificate SUBCOMMAND",
DisableFlagsInUseLine: true,
Short: i18n.T("Modify certificate resources"),
Long: i18n.T("Modify certificate resources."),
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
cmd.AddCommand(NewCmdCertificateApprove(restClientGetter, ioStreams))
cmd.AddCommand(NewCmdCertificateDeny(restClientGetter, ioStreams))
return cmd
}
// CertificateOptions declares the arguments accepted by the certificate command
type CertificateOptions struct {
resource.FilenameOptions
PrintFlags *genericclioptions.PrintFlags
PrintObj printers.ResourcePrinterFunc
csrNames []string
outputStyle string
certificatesV1Client v1.CertificatesV1Interface
builder *resource.Builder
genericiooptions.IOStreams
}
// NewCertificateOptions creates CertificateOptions struct for `certificate` command
func NewCertificateOptions(ioStreams genericiooptions.IOStreams, operation string) *CertificateOptions {
return &CertificateOptions{
PrintFlags: genericclioptions.NewPrintFlags(operation).WithTypeSetter(scheme.Scheme),
IOStreams: ioStreams,
}
}
// Complete loads data from the command environment
func (o *CertificateOptions) Complete(restClientGetter genericclioptions.RESTClientGetter, cmd *cobra.Command, args []string) error {
o.csrNames = args
o.outputStyle = cmdutil.GetFlagString(cmd, "output")
printer, err := o.PrintFlags.ToPrinter()
if err != nil {
return err
}
o.PrintObj = func(obj runtime.Object, out io.Writer) error {
return printer.PrintObj(obj, out)
}
o.builder = resource.NewBuilder(restClientGetter)
clientConfig, err := restClientGetter.ToRESTConfig()
if err != nil {
return err
}
o.certificatesV1Client, err = v1.NewForConfig(clientConfig)
if err != nil {
return err
}
return nil
}
// Validate checks if the provided `certificate` arguments are valid
func (o *CertificateOptions) Validate() error {
if len(o.csrNames) < 1 && cmdutil.IsFilenameSliceEmpty(o.Filenames, o.Kustomize) {
return fmt.Errorf("one or more CSRs must be specified as <name> or -f <filename>")
}
return nil
}
// NewCmdCertificateApprove returns the `certificate approve` Cobra command
func NewCmdCertificateApprove(restClientGetter genericclioptions.RESTClientGetter, ioStreams genericiooptions.IOStreams) *cobra.Command {
o := NewCertificateOptions(ioStreams, "approved")
cmd := &cobra.Command{
Use: "approve (-f FILENAME | NAME)",
DisableFlagsInUseLine: true,
Short: i18n.T("Approve a certificate signing request"),
Long: templates.LongDesc(i18n.T(`
Approve a certificate signing request.
kubectl certificate approve allows a cluster admin to approve a certificate
signing request (CSR). This action tells a certificate signing controller to
issue a certificate to the requester with the attributes requested in the CSR.
SECURITY NOTICE: Depending on the requested attributes, the issued certificate
can potentially grant a requester access to cluster resources or to authenticate
as a requested identity. Before approving a CSR, ensure you understand what the
signed certificate can do.
`)),
Example: templates.Examples(i18n.T(`
# Approve CSR 'csr-sqgzp'
kubectl certificate approve csr-sqgzp
`)),
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.Complete(restClientGetter, cmd, args))
cmdutil.CheckErr(o.Validate())
cmdutil.CheckErr(o.RunCertificateApprove(cmdutil.GetFlagBool(cmd, "force")))
},
}
o.PrintFlags.AddFlags(cmd)
cmd.Flags().Bool("force", false, "Update the CSR even if it is already approved.")
cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, "identifying the resource to update")
return cmd
}
// RunCertificateApprove approves a certificate signing request
func (o *CertificateOptions) RunCertificateApprove(force bool) error {
return o.modifyCertificateCondition(
o.builder,
force,
addConditionIfNeeded(string(certificatesv1.CertificateDenied), string(certificatesv1.CertificateApproved), "KubectlApprove", "This CSR was approved by kubectl certificate approve."),
)
}
// NewCmdCertificateDeny returns the `certificate deny` Cobra command
func NewCmdCertificateDeny(restClientGetter genericclioptions.RESTClientGetter, ioStreams genericiooptions.IOStreams) *cobra.Command {
o := NewCertificateOptions(ioStreams, "denied")
cmd := &cobra.Command{
Use: "deny (-f FILENAME | NAME)",
DisableFlagsInUseLine: true,
Short: i18n.T("Deny a certificate signing request"),
Long: templates.LongDesc(i18n.T(`
Deny a certificate signing request.
kubectl certificate deny allows a cluster admin to deny a certificate
signing request (CSR). This action tells a certificate signing controller to
not to issue a certificate to the requester.
`)),
Example: templates.Examples(i18n.T(`
# Deny CSR 'csr-sqgzp'
kubectl certificate deny csr-sqgzp
`)),
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.Complete(restClientGetter, cmd, args))
cmdutil.CheckErr(o.Validate())
cmdutil.CheckErr(o.RunCertificateDeny(cmdutil.GetFlagBool(cmd, "force")))
},
}
o.PrintFlags.AddFlags(cmd)
cmd.Flags().Bool("force", false, "Update the CSR even if it is already denied.")
cmdutil.AddFilenameOptionFlags(cmd, &o.FilenameOptions, "identifying the resource to update")
return cmd
}
// RunCertificateDeny denies a certificate signing request
func (o *CertificateOptions) RunCertificateDeny(force bool) error {
return o.modifyCertificateCondition(
o.builder,
force,
addConditionIfNeeded(string(certificatesv1.CertificateApproved), string(certificatesv1.CertificateDenied), "KubectlDeny", "This CSR was denied by kubectl certificate deny."),
)
}
func (o *CertificateOptions) modifyCertificateCondition(builder *resource.Builder, force bool, modify func(csr runtime.Object) (runtime.Object, bool, error)) error {
var found int
r := builder.
Unstructured().
ContinueOnError().
FilenameParam(false, &o.FilenameOptions).
ResourceNames("certificatesigningrequests", o.csrNames...).
RequireObject(true).
Flatten().
Latest().
Do()
err := r.Visit(func(info *resource.Info, err error) error {
if err != nil {
return err
}
for i := 0; ; i++ {
obj, ok := info.Object.(*unstructured.Unstructured)
if !ok {
return fmt.Errorf("expected *unstructured.Unstructured, got %T", obj)
}
if want, got := certificatesv1.Kind("CertificateSigningRequest"), obj.GetObjectKind().GroupVersionKind().GroupKind(); want != got {
return fmt.Errorf("can only handle %s objects, got %s", want.String(), got.String())
}
var csr runtime.Object
// get a typed object
csr, err = o.certificatesV1Client.CertificateSigningRequests().Get(context.TODO(), obj.GetName(), metav1.GetOptions{})
if apierrors.IsNotFound(err) {
return fmt.Errorf("could not find v1 version of %s: %v", obj.GetName(), err)
}
if err != nil {
return err
}
modifiedCSR, hasCondition, err := modify(csr)
if err != nil {
return err
}
if !hasCondition || force {
if mCSR, ok := modifiedCSR.(*certificatesv1.CertificateSigningRequest); ok {
_, err = o.certificatesV1Client.CertificateSigningRequests().UpdateApproval(context.TODO(), mCSR.Name, mCSR, metav1.UpdateOptions{})
} else {
return fmt.Errorf("can only handle certificates.k8s.io CertificateSigningRequest objects, got %T", mCSR)
}
if apierrors.IsConflict(err) && i < 10 {
if err := info.Get(); err != nil {
return err
}
continue
}
if err != nil {
return err
}
}
break
}
found++
return o.PrintObj(info.Object, o.Out)
})
if found == 0 && err == nil {
fmt.Fprintf(o.Out, "No resources found\n")
}
return err
}
func addConditionIfNeeded(mustNotHaveConditionType, conditionType, reason, message string) func(runtime.Object) (runtime.Object, bool, error) {
return func(obj runtime.Object) (runtime.Object, bool, error) {
if csr, ok := obj.(*certificatesv1.CertificateSigningRequest); ok {
var alreadyHasCondition bool
for _, c := range csr.Status.Conditions {
if string(c.Type) == mustNotHaveConditionType {
return nil, false, fmt.Errorf("certificate signing request %q is already %s", csr.Name, c.Type)
}
if string(c.Type) == conditionType {
alreadyHasCondition = true
}
}
if alreadyHasCondition {
return csr, true, nil
}
csr.Status.Conditions = append(csr.Status.Conditions, certificatesv1.CertificateSigningRequestCondition{
Type: certificatesv1.RequestConditionType(conditionType),
Status: corev1.ConditionTrue,
Reason: reason,
Message: message,
LastUpdateTime: metav1.Now(),
})
return csr, false, nil
} else {
return csr, false, nil
}
}
}