1/*
2Copyright 2020 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package v1
18
19import (
20	"fmt"
21
22	v1 "k8s.io/api/core/v1"
23	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24)
25
26// +genclient
27// +genclient:nonNamespaced
28// +genclient:method=UpdateApproval,verb=update,subresource=approval,input=k8s.io/api/certificates/v1.CertificateSigningRequest,result=k8s.io/api/certificates/v1.CertificateSigningRequest
29// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
30
31// CertificateSigningRequest objects provide a mechanism to obtain x509 certificates
32// by submitting a certificate signing request, and having it asynchronously approved and issued.
33//
34// Kubelets use this API to obtain:
35//  1. client certificates to authenticate to kube-apiserver (with the "kubernetes.io/kube-apiserver-client-kubelet" signerName).
36//  2. serving certificates for TLS endpoints kube-apiserver can connect to securely (with the "kubernetes.io/kubelet-serving" signerName).
37//
38// This API can be used to request client certificates to authenticate to kube-apiserver
39// (with the "kubernetes.io/kube-apiserver-client" signerName),
40// or to obtain certificates from custom non-Kubernetes signers.
41type CertificateSigningRequest struct {
42	metav1.TypeMeta `json:",inline"`
43	// +optional
44	metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
45
46	// spec contains the certificate request, and is immutable after creation.
47	// Only the request, signerName, expirationSeconds, and usages fields can be set on creation.
48	// Other fields are derived by Kubernetes and cannot be modified by users.
49	Spec CertificateSigningRequestSpec `json:"spec" protobuf:"bytes,2,opt,name=spec"`
50
51	// status contains information about whether the request is approved or denied,
52	// and the certificate issued by the signer, or the failure condition indicating signer failure.
53	// +optional
54	Status CertificateSigningRequestStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
55}
56
57// CertificateSigningRequestSpec contains the certificate request.
58type CertificateSigningRequestSpec struct {
59	// request contains an x509 certificate signing request encoded in a "CERTIFICATE REQUEST" PEM block.
60	// When serialized as JSON or YAML, the data is additionally base64-encoded.
61	// +listType=atomic
62	Request []byte `json:"request" protobuf:"bytes,1,opt,name=request"`
63
64	// signerName indicates the requested signer, and is a qualified name.
65	//
66	// List/watch requests for CertificateSigningRequests can filter on this field using a "spec.signerName=NAME" fieldSelector.
67	//
68	// Well-known Kubernetes signers are:
69	//  1. "kubernetes.io/kube-apiserver-client": issues client certificates that can be used to authenticate to kube-apiserver.
70	//   Requests for this signer are never auto-approved by kube-controller-manager, can be issued by the "csrsigning" controller in kube-controller-manager.
71	//  2. "kubernetes.io/kube-apiserver-client-kubelet": issues client certificates that kubelets use to authenticate to kube-apiserver.
72	//   Requests for this signer can be auto-approved by the "csrapproving" controller in kube-controller-manager, and can be issued by the "csrsigning" controller in kube-controller-manager.
73	//  3. "kubernetes.io/kubelet-serving" issues serving certificates that kubelets use to serve TLS endpoints, which kube-apiserver can connect to securely.
74	//   Requests for this signer are never auto-approved by kube-controller-manager, and can be issued by the "csrsigning" controller in kube-controller-manager.
75	//
76	// More details are available at https://k8s.io/docs/reference/access-authn-authz/certificate-signing-requests/#kubernetes-signers
77	//
78	// Custom signerNames can also be specified. The signer defines:
79	//  1. Trust distribution: how trust (CA bundles) are distributed.
80	//  2. Permitted subjects: and behavior when a disallowed subject is requested.
81	//  3. Required, permitted, or forbidden x509 extensions in the request (including whether subjectAltNames are allowed, which types, restrictions on allowed values) and behavior when a disallowed extension is requested.
82	//  4. Required, permitted, or forbidden key usages / extended key usages.
83	//  5. Expiration/certificate lifetime: whether it is fixed by the signer, configurable by the admin.
84	//  6. Whether or not requests for CA certificates are allowed.
85	SignerName string `json:"signerName" protobuf:"bytes,7,opt,name=signerName"`
86
87	// expirationSeconds is the requested duration of validity of the issued
88	// certificate. The certificate signer may issue a certificate with a different
89	// validity duration so a client must check the delta between the notBefore and
90	// and notAfter fields in the issued certificate to determine the actual duration.
91	//
92	// The v1.22+ in-tree implementations of the well-known Kubernetes signers will
93	// honor this field as long as the requested duration is not greater than the
94	// maximum duration they will honor per the --cluster-signing-duration CLI
95	// flag to the Kubernetes controller manager.
96	//
97	// Certificate signers may not honor this field for various reasons:
98	//
99	//   1. Old signer that is unaware of the field (such as the in-tree
100	//      implementations prior to v1.22)
101	//   2. Signer whose configured maximum is shorter than the requested duration
102	//   3. Signer whose configured minimum is longer than the requested duration
103	//
104	// The minimum valid value for expirationSeconds is 600, i.e. 10 minutes.
105	//
106	// As of v1.22, this field is beta and is controlled via the CSRDuration feature gate.
107	//
108	// +optional
109	ExpirationSeconds *int32 `json:"expirationSeconds,omitempty" protobuf:"varint,8,opt,name=expirationSeconds"`
110
111	// usages specifies a set of key usages requested in the issued certificate.
112	//
113	// Requests for TLS client certificates typically request: "digital signature", "key encipherment", "client auth".
114	//
115	// Requests for TLS serving certificates typically request: "key encipherment", "digital signature", "server auth".
116	//
117	// Valid values are:
118	//  "signing", "digital signature", "content commitment",
119	//  "key encipherment", "key agreement", "data encipherment",
120	//  "cert sign", "crl sign", "encipher only", "decipher only", "any",
121	//  "server auth", "client auth",
122	//  "code signing", "email protection", "s/mime",
123	//  "ipsec end system", "ipsec tunnel", "ipsec user",
124	//  "timestamping", "ocsp signing", "microsoft sgc", "netscape sgc"
125	// +listType=atomic
126	Usages []KeyUsage `json:"usages,omitempty" protobuf:"bytes,5,opt,name=usages"`
127
128	// username contains the name of the user that created the CertificateSigningRequest.
129	// Populated by the API server on creation and immutable.
130	// +optional
131	Username string `json:"username,omitempty" protobuf:"bytes,2,opt,name=username"`
132	// uid contains the uid of the user that created the CertificateSigningRequest.
133	// Populated by the API server on creation and immutable.
134	// +optional
135	UID string `json:"uid,omitempty" protobuf:"bytes,3,opt,name=uid"`
136	// groups contains group membership of the user that created the CertificateSigningRequest.
137	// Populated by the API server on creation and immutable.
138	// +listType=atomic
139	// +optional
140	Groups []string `json:"groups,omitempty" protobuf:"bytes,4,rep,name=groups"`
141	// extra contains extra attributes of the user that created the CertificateSigningRequest.
142	// Populated by the API server on creation and immutable.
143	// +optional
144	Extra map[string]ExtraValue `json:"extra,omitempty" protobuf:"bytes,6,rep,name=extra"`
145}
146
147// Built in signerName values that are honored by kube-controller-manager.
148const (
149	// "kubernetes.io/kube-apiserver-client" signer issues client certificates that can be used to authenticate to kube-apiserver.
150	// Never auto-approved by kube-controller-manager.
151	// Can be issued by the "csrsigning" controller in kube-controller-manager.
152	KubeAPIServerClientSignerName = "kubernetes.io/kube-apiserver-client"
153
154	// "kubernetes.io/kube-apiserver-client-kubelet" issues client certificates that kubelets use to authenticate to kube-apiserver.
155	// Can be auto-approved by the "csrapproving" controller in kube-controller-manager.
156	// Can be issued by the "csrsigning" controller in kube-controller-manager.
157	KubeAPIServerClientKubeletSignerName = "kubernetes.io/kube-apiserver-client-kubelet"
158
159	// "kubernetes.io/kubelet-serving" issues serving certificates that kubelets use to serve TLS endpoints,
160	// which kube-apiserver can connect to securely.
161	// Never auto-approved by kube-controller-manager.
162	// Can be issued by the "csrsigning" controller in kube-controller-manager.
163	KubeletServingSignerName = "kubernetes.io/kubelet-serving"
164)
165
166// ExtraValue masks the value so protobuf can generate
167// +protobuf.nullable=true
168// +protobuf.options.(gogoproto.goproto_stringer)=false
169type ExtraValue []string
170
171func (t ExtraValue) String() string {
172	return fmt.Sprintf("%v", []string(t))
173}
174
175// CertificateSigningRequestStatus contains conditions used to indicate
176// approved/denied/failed status of the request, and the issued certificate.
177type CertificateSigningRequestStatus struct {
178	// conditions applied to the request. Known conditions are "Approved", "Denied", and "Failed".
179	// +listType=map
180	// +listMapKey=type
181	// +optional
182	Conditions []CertificateSigningRequestCondition `json:"conditions,omitempty" protobuf:"bytes,1,rep,name=conditions"`
183
184	// certificate is populated with an issued certificate by the signer after an Approved condition is present.
185	// This field is set via the /status subresource. Once populated, this field is immutable.
186	//
187	// If the certificate signing request is denied, a condition of type "Denied" is added and this field remains empty.
188	// If the signer cannot issue the certificate, a condition of type "Failed" is added and this field remains empty.
189	//
190	// Validation requirements:
191	//  1. certificate must contain one or more PEM blocks.
192	//  2. All PEM blocks must have the "CERTIFICATE" label, contain no headers, and the encoded data
193	//   must be a BER-encoded ASN.1 Certificate structure as described in section 4 of RFC5280.
194	//  3. Non-PEM content may appear before or after the "CERTIFICATE" PEM blocks and is unvalidated,
195	//   to allow for explanatory text as described in section 5.2 of RFC7468.
196	//
197	// If more than one PEM block is present, and the definition of the requested spec.signerName
198	// does not indicate otherwise, the first block is the issued certificate,
199	// and subsequent blocks should be treated as intermediate certificates and presented in TLS handshakes.
200	//
201	// The certificate is encoded in PEM format.
202	//
203	// When serialized as JSON or YAML, the data is additionally base64-encoded, so it consists of:
204	//
205	//     base64(
206	//     -----BEGIN CERTIFICATE-----
207	//     ...
208	//     -----END CERTIFICATE-----
209	//     )
210	//
211	// +listType=atomic
212	// +optional
213	Certificate []byte `json:"certificate,omitempty" protobuf:"bytes,2,opt,name=certificate"`
214}
215
216// RequestConditionType is the type of a CertificateSigningRequestCondition
217type RequestConditionType string
218
219// Well-known condition types for certificate requests.
220const (
221	// Approved indicates the request was approved and should be issued by the signer.
222	CertificateApproved RequestConditionType = "Approved"
223	// Denied indicates the request was denied and should not be issued by the signer.
224	CertificateDenied RequestConditionType = "Denied"
225	// Failed indicates the signer failed to issue the certificate.
226	CertificateFailed RequestConditionType = "Failed"
227)
228
229// CertificateSigningRequestCondition describes a condition of a CertificateSigningRequest object
230type CertificateSigningRequestCondition struct {
231	// type of the condition. Known conditions are "Approved", "Denied", and "Failed".
232	//
233	// An "Approved" condition is added via the /approval subresource,
234	// indicating the request was approved and should be issued by the signer.
235	//
236	// A "Denied" condition is added via the /approval subresource,
237	// indicating the request was denied and should not be issued by the signer.
238	//
239	// A "Failed" condition is added via the /status subresource,
240	// indicating the signer failed to issue the certificate.
241	//
242	// Approved and Denied conditions are mutually exclusive.
243	// Approved, Denied, and Failed conditions cannot be removed once added.
244	//
245	// Only one condition of a given type is allowed.
246	Type RequestConditionType `json:"type" protobuf:"bytes,1,opt,name=type,casttype=RequestConditionType"`
247	// status of the condition, one of True, False, Unknown.
248	// Approved, Denied, and Failed conditions may not be "False" or "Unknown".
249	Status v1.ConditionStatus `json:"status" protobuf:"bytes,6,opt,name=status,casttype=k8s.io/api/core/v1.ConditionStatus"`
250	// reason indicates a brief reason for the request state
251	// +optional
252	Reason string `json:"reason,omitempty" protobuf:"bytes,2,opt,name=reason"`
253	// message contains a human readable message with details about the request state
254	// +optional
255	Message string `json:"message,omitempty" protobuf:"bytes,3,opt,name=message"`
256	// lastUpdateTime is the time of the last update to this condition
257	// +optional
258	LastUpdateTime metav1.Time `json:"lastUpdateTime,omitempty" protobuf:"bytes,4,opt,name=lastUpdateTime"`
259	// lastTransitionTime is the time the condition last transitioned from one status to another.
260	// If unset, when a new condition type is added or an existing condition's status is changed,
261	// the server defaults this to the current time.
262	// +optional
263	LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty" protobuf:"bytes,5,opt,name=lastTransitionTime"`
264}
265
266// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
267
268// CertificateSigningRequestList is a collection of CertificateSigningRequest objects
269type CertificateSigningRequestList struct {
270	metav1.TypeMeta `json:",inline"`
271	// +optional
272	metav1.ListMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
273
274	// items is a collection of CertificateSigningRequest objects
275	Items []CertificateSigningRequest `json:"items" protobuf:"bytes,2,rep,name=items"`
276}
277
278// KeyUsage specifies valid usage contexts for keys.
279// See: https://tools.ietf.org/html/rfc5280#section-4.2.1.3
280//      https://tools.ietf.org/html/rfc5280#section-4.2.1.12
281type KeyUsage string
282
283// Valid key usages
284const (
285	UsageSigning           KeyUsage = "signing"
286	UsageDigitalSignature  KeyUsage = "digital signature"
287	UsageContentCommitment KeyUsage = "content commitment"
288	UsageKeyEncipherment   KeyUsage = "key encipherment"
289	UsageKeyAgreement      KeyUsage = "key agreement"
290	UsageDataEncipherment  KeyUsage = "data encipherment"
291	UsageCertSign          KeyUsage = "cert sign"
292	UsageCRLSign           KeyUsage = "crl sign"
293	UsageEncipherOnly      KeyUsage = "encipher only"
294	UsageDecipherOnly      KeyUsage = "decipher only"
295	UsageAny               KeyUsage = "any"
296	UsageServerAuth        KeyUsage = "server auth"
297	UsageClientAuth        KeyUsage = "client auth"
298	UsageCodeSigning       KeyUsage = "code signing"
299	UsageEmailProtection   KeyUsage = "email protection"
300	UsageSMIME             KeyUsage = "s/mime"
301	UsageIPsecEndSystem    KeyUsage = "ipsec end system"
302	UsageIPsecTunnel       KeyUsage = "ipsec tunnel"
303	UsageIPsecUser         KeyUsage = "ipsec user"
304	UsageTimestamping      KeyUsage = "timestamping"
305	UsageOCSPSigning       KeyUsage = "ocsp signing"
306	UsageMicrosoftSGC      KeyUsage = "microsoft sgc"
307	UsageNetscapeSGC       KeyUsage = "netscape sgc"
308)
309