csr: add expirationSeconds field to control cert lifetime

This change updates the CSR API to add a new, optional field called
expirationSeconds.  This field is a request to the signer for the
maximum duration the client wishes the cert to have.  The signer is
free to ignore this request based on its own internal policy.  The
signers built-in to KCM will honor this field if it is not set to a
value greater than --cluster-signing-duration.  The minimum allowed
value for this field is 600 seconds (ten minutes).

This change will help enforce safer durations for certificates in
the Kube ecosystem and will help related projects such as
cert-manager with their migration to the Kube CSR API.

Future enhancements may update the Kubelet to take advantage of this
field when it is configured in a way that can tolerate shorter
certificate lifespans with regular rotation.

Signed-off-by: Monis Khan <mok@vmware.com>

Kubernetes-commit: cd91e59f7c351fce47c064a5162c2cb79075159c
This commit is contained in:
Monis Khan 2021-06-25 22:08:10 -04:00 committed by Kubernetes Publisher
parent 08564af8a9
commit 82734c256a
1 changed files with 16 additions and 9 deletions

View File

@ -33,7 +33,6 @@ import (
"unicode"
"github.com/fatih/camelcase"
"k8s.io/apimachinery/pkg/runtime"
appsv1 "k8s.io/api/apps/v1"
autoscalingv1 "k8s.io/api/autoscaling/v1"
@ -60,6 +59,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/duration"
@ -72,6 +72,7 @@ import (
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/reference"
utilcsr "k8s.io/client-go/util/certificate/csr"
"k8s.io/klog/v2"
"k8s.io/kubectl/pkg/scheme"
"k8s.io/kubectl/pkg/util/certificate"
@ -3690,12 +3691,13 @@ type CertificateSigningRequestDescriber struct {
func (p *CertificateSigningRequestDescriber) Describe(namespace, name string, describerSettings DescriberSettings) (string, error) {
var (
crBytes []byte
metadata metav1.ObjectMeta
status string
signerName string
username string
events *corev1.EventList
crBytes []byte
metadata metav1.ObjectMeta
status string
signerName string
expirationSeconds *int32
username string
events *corev1.EventList
)
if csr, err := p.client.CertificatesV1().CertificateSigningRequests().Get(context.TODO(), name, metav1.GetOptions{}); err == nil {
@ -3707,6 +3709,7 @@ func (p *CertificateSigningRequestDescriber) Describe(namespace, name string, de
}
status = extractCSRStatus(conditionTypes, csr.Status.Certificate)
signerName = csr.Spec.SignerName
expirationSeconds = csr.Spec.ExpirationSeconds
username = csr.Spec.Username
if describerSettings.ShowEvents {
events, _ = searchEvents(p.client.CoreV1(), csr, describerSettings.ChunkSize)
@ -3722,6 +3725,7 @@ func (p *CertificateSigningRequestDescriber) Describe(namespace, name string, de
if csr.Spec.SignerName != nil {
signerName = *csr.Spec.SignerName
}
expirationSeconds = csr.Spec.ExpirationSeconds
username = csr.Spec.Username
if describerSettings.ShowEvents {
events, _ = searchEvents(p.client.CoreV1(), csr, describerSettings.ChunkSize)
@ -3735,10 +3739,10 @@ func (p *CertificateSigningRequestDescriber) Describe(namespace, name string, de
return "", fmt.Errorf("Error parsing CSR: %v", err)
}
return describeCertificateSigningRequest(metadata, signerName, username, cr, status, events)
return describeCertificateSigningRequest(metadata, signerName, expirationSeconds, username, cr, status, events)
}
func describeCertificateSigningRequest(csr metav1.ObjectMeta, signerName string, username string, cr *x509.CertificateRequest, status string, events *corev1.EventList) (string, error) {
func describeCertificateSigningRequest(csr metav1.ObjectMeta, signerName string, expirationSeconds *int32, username string, cr *x509.CertificateRequest, status string, events *corev1.EventList) (string, error) {
printListHelper := func(w PrefixWriter, prefix, name string, values []string) {
if len(values) == 0 {
return
@ -3758,6 +3762,9 @@ func describeCertificateSigningRequest(csr metav1.ObjectMeta, signerName string,
if len(signerName) > 0 {
w.Write(LEVEL_0, "Signer:\t%s\n", signerName)
}
if expirationSeconds != nil {
w.Write(LEVEL_0, "Requested Duration:\t%s\n", duration.HumanDuration(utilcsr.ExpirationSecondsToDuration(*expirationSeconds)))
}
w.Write(LEVEL_0, "Status:\t%s\n", status)
w.Write(LEVEL_0, "Subject:\n")