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>
This commit is contained in:
@@ -38,8 +38,8 @@ type CSRSigningControllerConfiguration struct {
|
||||
// legacyUnknownSignerConfiguration holds the certificate and key used to issue certificates for the kubernetes.io/legacy-unknown
|
||||
LegacyUnknownSignerConfiguration CSRSigningConfiguration
|
||||
|
||||
// clusterSigningDuration is the length of duration signed certificates
|
||||
// will be given.
|
||||
// clusterSigningDuration is the max length of duration signed certificates will be given.
|
||||
// Individual CSRs may request shorter certs by setting spec.expirationSeconds.
|
||||
ClusterSigningDuration metav1.Duration
|
||||
}
|
||||
|
||||
|
@@ -30,11 +30,14 @@ import (
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/apiserver/pkg/server/dynamiccertificates"
|
||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||
certificatesinformers "k8s.io/client-go/informers/certificates/v1"
|
||||
clientset "k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/util/certificate/csr"
|
||||
capihelper "k8s.io/kubernetes/pkg/apis/certificates"
|
||||
"k8s.io/kubernetes/pkg/controller/certificates"
|
||||
"k8s.io/kubernetes/pkg/controller/certificates/authority"
|
||||
"k8s.io/kubernetes/pkg/features"
|
||||
)
|
||||
|
||||
type CSRSigningController struct {
|
||||
@@ -115,7 +118,7 @@ type signer struct {
|
||||
caProvider *caProvider
|
||||
|
||||
client clientset.Interface
|
||||
certTTL time.Duration
|
||||
certTTL time.Duration // max TTL; individual requests may request shorter certs by setting spec.expirationSeconds
|
||||
|
||||
signerName string
|
||||
isRequestForSignerFn isRequestForSignerFunc
|
||||
@@ -173,7 +176,7 @@ func (s *signer) handle(csr *capi.CertificateSigningRequest) error {
|
||||
// Ignore requests for kubernetes.io signerNames we don't recognize
|
||||
return nil
|
||||
}
|
||||
cert, err := s.sign(x509cr, csr.Spec.Usages, nil)
|
||||
cert, err := s.sign(x509cr, csr.Spec.Usages, csr.Spec.ExpirationSeconds, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error auto signing csr: %v", err)
|
||||
}
|
||||
@@ -185,15 +188,15 @@ func (s *signer) handle(csr *capi.CertificateSigningRequest) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *signer) sign(x509cr *x509.CertificateRequest, usages []capi.KeyUsage, now func() time.Time) ([]byte, error) {
|
||||
func (s *signer) sign(x509cr *x509.CertificateRequest, usages []capi.KeyUsage, expirationSeconds *int32, now func() time.Time) ([]byte, error) {
|
||||
currCA, err := s.caProvider.currentCA()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
der, err := currCA.Sign(x509cr.Raw, authority.PermissiveSigningPolicy{
|
||||
TTL: s.certTTL,
|
||||
TTL: s.duration(expirationSeconds),
|
||||
Usages: usages,
|
||||
Backdate: 5 * time.Minute, // this must always be less than the minimum TTL requested by a user
|
||||
Backdate: 5 * time.Minute, // this must always be less than the minimum TTL requested by a user (see sanity check requestedDuration below)
|
||||
Short: 8 * time.Hour, // 5 minutes of backdating is roughly 1% of 8 hours
|
||||
Now: now,
|
||||
})
|
||||
@@ -203,6 +206,30 @@ func (s *signer) sign(x509cr *x509.CertificateRequest, usages []capi.KeyUsage, n
|
||||
return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}), nil
|
||||
}
|
||||
|
||||
func (s *signer) duration(expirationSeconds *int32) time.Duration {
|
||||
if !utilfeature.DefaultFeatureGate.Enabled(features.CSRDuration) {
|
||||
return s.certTTL
|
||||
}
|
||||
|
||||
if expirationSeconds == nil {
|
||||
return s.certTTL
|
||||
}
|
||||
|
||||
// honor requested duration is if it is less than the default TTL
|
||||
// use 10 min (2x hard coded backdate above) as a sanity check lower bound
|
||||
const min = 10 * time.Minute
|
||||
switch requestedDuration := csr.ExpirationSecondsToDuration(*expirationSeconds); {
|
||||
case requestedDuration > s.certTTL:
|
||||
return s.certTTL
|
||||
|
||||
case requestedDuration < min:
|
||||
return min
|
||||
|
||||
default:
|
||||
return requestedDuration
|
||||
}
|
||||
}
|
||||
|
||||
// getCSRVerificationFuncForSignerName is a function that provides reliable mapping of signer names to verification so that
|
||||
// we don't have accidents with wiring at some later date.
|
||||
func getCSRVerificationFuncForSignerName(signerName string) (isRequestForSignerFunc, error) {
|
||||
|
@@ -32,11 +32,15 @@ import (
|
||||
capi "k8s.io/api/certificates/v1"
|
||||
"k8s.io/apimachinery/pkg/util/clock"
|
||||
"k8s.io/apimachinery/pkg/util/diff"
|
||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||
"k8s.io/client-go/kubernetes/fake"
|
||||
testclient "k8s.io/client-go/testing"
|
||||
"k8s.io/client-go/util/cert"
|
||||
"k8s.io/client-go/util/certificate/csr"
|
||||
featuregatetesting "k8s.io/component-base/featuregate/testing"
|
||||
capihelper "k8s.io/kubernetes/pkg/apis/certificates/v1"
|
||||
"k8s.io/kubernetes/pkg/controller/certificates"
|
||||
"k8s.io/kubernetes/pkg/features"
|
||||
)
|
||||
|
||||
func TestSigner(t *testing.T) {
|
||||
@@ -61,7 +65,11 @@ func TestSigner(t *testing.T) {
|
||||
capi.UsageKeyEncipherment,
|
||||
capi.UsageServerAuth,
|
||||
capi.UsageClientAuth,
|
||||
}, fakeClock.Now)
|
||||
},
|
||||
// requesting a duration that is greater than TTL is ignored
|
||||
csr.DurationToExpirationSeconds(3*time.Hour),
|
||||
fakeClock.Now,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to sign CSR: %v", err)
|
||||
}
|
||||
@@ -344,3 +352,91 @@ func makeTestCSR(b csrBuilder) *capi.CertificateSigningRequest {
|
||||
}
|
||||
return csr
|
||||
}
|
||||
|
||||
func Test_signer_duration(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
certTTL time.Duration
|
||||
expirationSeconds *int32
|
||||
wantGateEnabled time.Duration
|
||||
wantGateDisabled time.Duration
|
||||
}{
|
||||
{
|
||||
name: "can request shorter duration than TTL",
|
||||
certTTL: time.Hour,
|
||||
expirationSeconds: csr.DurationToExpirationSeconds(30 * time.Minute),
|
||||
wantGateEnabled: 30 * time.Minute,
|
||||
wantGateDisabled: time.Hour,
|
||||
},
|
||||
{
|
||||
name: "cannot request longer duration than TTL",
|
||||
certTTL: time.Hour,
|
||||
expirationSeconds: csr.DurationToExpirationSeconds(3 * time.Hour),
|
||||
wantGateEnabled: time.Hour,
|
||||
wantGateDisabled: time.Hour,
|
||||
},
|
||||
{
|
||||
name: "cannot request negative duration",
|
||||
certTTL: time.Hour,
|
||||
expirationSeconds: csr.DurationToExpirationSeconds(-time.Minute),
|
||||
wantGateEnabled: 10 * time.Minute,
|
||||
wantGateDisabled: time.Hour,
|
||||
},
|
||||
{
|
||||
name: "cannot request duration less than 10 mins",
|
||||
certTTL: time.Hour,
|
||||
expirationSeconds: csr.DurationToExpirationSeconds(10*time.Minute - time.Second),
|
||||
wantGateEnabled: 10 * time.Minute,
|
||||
wantGateDisabled: time.Hour,
|
||||
},
|
||||
{
|
||||
name: "can request duration of exactly 10 mins",
|
||||
certTTL: time.Hour,
|
||||
expirationSeconds: csr.DurationToExpirationSeconds(10 * time.Minute),
|
||||
wantGateEnabled: 10 * time.Minute,
|
||||
wantGateDisabled: time.Hour,
|
||||
},
|
||||
{
|
||||
name: "can request duration equal to the default",
|
||||
certTTL: time.Hour,
|
||||
expirationSeconds: csr.DurationToExpirationSeconds(time.Hour),
|
||||
wantGateEnabled: time.Hour,
|
||||
wantGateDisabled: time.Hour,
|
||||
},
|
||||
{
|
||||
name: "can choose not to request a duration to get the default",
|
||||
certTTL: time.Hour,
|
||||
expirationSeconds: nil,
|
||||
wantGateEnabled: time.Hour,
|
||||
wantGateDisabled: time.Hour,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
|
||||
f := func(t *testing.T, want time.Duration) {
|
||||
s := &signer{
|
||||
certTTL: tt.certTTL,
|
||||
}
|
||||
if got := s.duration(tt.expirationSeconds); got != want {
|
||||
t.Errorf("duration() = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// regular tests
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel() // these are safe to run in parallel but not the feature gate disabled tests
|
||||
|
||||
f(t, tt.wantGateEnabled)
|
||||
})
|
||||
|
||||
// same tests with the feature gate disabled
|
||||
t.Run("feature gate disabled - "+tt.name, func(t *testing.T) {
|
||||
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSRDuration, false)()
|
||||
f(t, tt.wantGateDisabled)
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user