k3s/vendor/k8s.io/kubernetes/pkg/controller/certificates/signer/signer.go

278 lines
9.2 KiB
Go
Raw Normal View History

2019-12-12 01:27:03 +00:00
/*
Copyright 2019 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 signer implements a CA signer that uses keys stored on local disk.
package signer
import (
2020-03-26 21:07:15 +00:00
"context"
"crypto/x509"
2019-12-12 01:27:03 +00:00
"encoding/pem"
"fmt"
"time"
2020-08-10 17:43:49 +00:00
capi "k8s.io/api/certificates/v1"
capiv1beta1 "k8s.io/api/certificates/v1beta1"
v1 "k8s.io/api/core/v1"
2020-03-26 21:07:15 +00:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2020-08-10 17:43:49 +00:00
"k8s.io/apimachinery/pkg/util/sets"
2020-03-26 21:07:15 +00:00
"k8s.io/apiserver/pkg/server/dynamiccertificates"
2020-08-10 17:43:49 +00:00
certificatesinformers "k8s.io/client-go/informers/certificates/v1"
2019-12-12 01:27:03 +00:00
clientset "k8s.io/client-go/kubernetes"
2020-08-10 17:43:49 +00:00
capihelper "k8s.io/kubernetes/pkg/apis/certificates"
2019-12-12 01:27:03 +00:00
"k8s.io/kubernetes/pkg/controller/certificates"
"k8s.io/kubernetes/pkg/controller/certificates/authority"
)
2020-03-26 21:07:15 +00:00
type CSRSigningController struct {
certificateController *certificates.CertificateController
dynamicCertReloader dynamiccertificates.ControllerRunner
}
2020-08-10 17:43:49 +00:00
func NewKubeletServingCSRSigningController(
client clientset.Interface,
csrInformer certificatesinformers.CertificateSigningRequestInformer,
caFile, caKeyFile string,
certTTL time.Duration,
) (*CSRSigningController, error) {
return NewCSRSigningController("csrsigning-kubelet-serving", capi.KubeletServingSignerName, client, csrInformer, caFile, caKeyFile, certTTL)
}
func NewKubeletClientCSRSigningController(
client clientset.Interface,
csrInformer certificatesinformers.CertificateSigningRequestInformer,
caFile, caKeyFile string,
certTTL time.Duration,
) (*CSRSigningController, error) {
return NewCSRSigningController("csrsigning-kubelet-client", capi.KubeAPIServerClientKubeletSignerName, client, csrInformer, caFile, caKeyFile, certTTL)
}
func NewKubeAPIServerClientCSRSigningController(
client clientset.Interface,
csrInformer certificatesinformers.CertificateSigningRequestInformer,
caFile, caKeyFile string,
certTTL time.Duration,
) (*CSRSigningController, error) {
return NewCSRSigningController("csrsigning-kube-apiserver-client", capi.KubeAPIServerClientSignerName, client, csrInformer, caFile, caKeyFile, certTTL)
}
func NewLegacyUnknownCSRSigningController(
client clientset.Interface,
csrInformer certificatesinformers.CertificateSigningRequestInformer,
caFile, caKeyFile string,
certTTL time.Duration,
) (*CSRSigningController, error) {
return NewCSRSigningController("csrsigning-legacy-unknown", capiv1beta1.LegacyUnknownSignerName, client, csrInformer, caFile, caKeyFile, certTTL)
}
2019-12-12 01:27:03 +00:00
func NewCSRSigningController(
2020-08-10 17:43:49 +00:00
controllerName string,
signerName string,
2019-12-12 01:27:03 +00:00
client clientset.Interface,
csrInformer certificatesinformers.CertificateSigningRequestInformer,
caFile, caKeyFile string,
certTTL time.Duration,
2020-03-26 21:07:15 +00:00
) (*CSRSigningController, error) {
2020-08-10 17:43:49 +00:00
signer, err := newSigner(signerName, caFile, caKeyFile, client, certTTL)
2019-12-12 01:27:03 +00:00
if err != nil {
return nil, err
}
2020-03-26 21:07:15 +00:00
return &CSRSigningController{
certificateController: certificates.NewCertificateController(
2020-08-10 17:43:49 +00:00
controllerName,
2020-03-26 21:07:15 +00:00
client,
csrInformer,
signer.handle,
),
dynamicCertReloader: signer.caProvider.caLoader,
}, nil
}
// Run the main goroutine responsible for watching and syncing jobs.
func (c *CSRSigningController) Run(workers int, stopCh <-chan struct{}) {
go c.dynamicCertReloader.Run(workers, stopCh)
c.certificateController.Run(workers, stopCh)
2019-12-12 01:27:03 +00:00
}
2020-08-10 17:43:49 +00:00
type isRequestForSignerFunc func(req *x509.CertificateRequest, usages []capi.KeyUsage, signerName string) (bool, error)
2019-12-12 01:27:03 +00:00
type signer struct {
2020-03-26 21:07:15 +00:00
caProvider *caProvider
2019-12-12 01:27:03 +00:00
client clientset.Interface
certTTL time.Duration
2020-08-10 17:43:49 +00:00
signerName string
isRequestForSignerFn isRequestForSignerFunc
2019-12-12 01:27:03 +00:00
}
2020-08-10 17:43:49 +00:00
func newSigner(signerName, caFile, caKeyFile string, client clientset.Interface, certificateDuration time.Duration) (*signer, error) {
isRequestForSignerFn, err := getCSRVerificationFuncForSignerName(signerName)
if err != nil {
return nil, err
}
2020-03-26 21:07:15 +00:00
caProvider, err := newCAProvider(caFile, caKeyFile)
2019-12-12 01:27:03 +00:00
if err != nil {
2020-03-26 21:07:15 +00:00
return nil, err
2019-12-12 01:27:03 +00:00
}
2020-03-26 21:07:15 +00:00
ret := &signer{
2020-08-10 17:43:49 +00:00
caProvider: caProvider,
client: client,
certTTL: certificateDuration,
signerName: signerName,
isRequestForSignerFn: isRequestForSignerFn,
2019-12-12 01:27:03 +00:00
}
2020-03-26 21:07:15 +00:00
return ret, nil
}
func (s *signer) handle(csr *capi.CertificateSigningRequest) error {
2020-08-10 17:43:49 +00:00
// Ignore unapproved or failed requests
if !certificates.IsCertificateRequestApproved(csr) || certificates.HasTrueCondition(csr, capi.CertificateFailed) {
2020-03-26 21:07:15 +00:00
return nil
2019-12-12 01:27:03 +00:00
}
2020-08-10 17:43:49 +00:00
// Fast-path to avoid any additional processing if the CSRs signerName does not match
if csr.Spec.SignerName != s.signerName {
2020-03-26 21:07:15 +00:00
return nil
2019-12-12 01:27:03 +00:00
}
2020-03-26 21:07:15 +00:00
x509cr, err := capihelper.ParseCSR(csr.Spec.Request)
2019-12-12 01:27:03 +00:00
if err != nil {
2020-03-26 21:07:15 +00:00
return fmt.Errorf("unable to parse csr %q: %v", csr.Name, err)
2019-12-12 01:27:03 +00:00
}
2020-08-10 17:43:49 +00:00
if recognized, err := s.isRequestForSignerFn(x509cr, csr.Spec.Usages, csr.Spec.SignerName); err != nil {
csr.Status.Conditions = append(csr.Status.Conditions, capi.CertificateSigningRequestCondition{
Type: capi.CertificateFailed,
Status: v1.ConditionTrue,
Reason: "SignerValidationFailure",
Message: err.Error(),
LastUpdateTime: metav1.Now(),
})
_, err = s.client.CertificatesV1().CertificateSigningRequests().UpdateStatus(context.TODO(), csr, metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("error adding failure condition for csr: %v", err)
}
return nil
} else if !recognized {
// Ignore requests for kubernetes.io signerNames we don't recognize
2019-12-12 01:27:03 +00:00
return nil
}
2020-03-26 21:07:15 +00:00
cert, err := s.sign(x509cr, csr.Spec.Usages)
2019-12-12 01:27:03 +00:00
if err != nil {
return fmt.Errorf("error auto signing csr: %v", err)
}
2020-03-26 21:07:15 +00:00
csr.Status.Certificate = cert
2020-08-10 17:43:49 +00:00
_, err = s.client.CertificatesV1().CertificateSigningRequests().UpdateStatus(context.TODO(), csr, metav1.UpdateOptions{})
2019-12-12 01:27:03 +00:00
if err != nil {
return fmt.Errorf("error updating signature for csr: %v", err)
}
return nil
}
2020-03-26 21:07:15 +00:00
func (s *signer) sign(x509cr *x509.CertificateRequest, usages []capi.KeyUsage) ([]byte, error) {
currCA, err := s.caProvider.currentCA()
2019-12-12 01:27:03 +00:00
if err != nil {
2020-03-26 21:07:15 +00:00
return nil, err
2019-12-12 01:27:03 +00:00
}
2020-03-26 21:07:15 +00:00
der, err := currCA.Sign(x509cr.Raw, authority.PermissiveSigningPolicy{
2019-12-12 01:27:03 +00:00
TTL: s.certTTL,
2020-03-26 21:07:15 +00:00
Usages: usages,
2019-12-12 01:27:03 +00:00
})
if err != nil {
return nil, err
}
2020-03-26 21:07:15 +00:00
return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}), nil
}
2020-08-10 17:43:49 +00:00
// 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) {
2020-03-26 21:07:15 +00:00
switch signerName {
case capi.KubeletServingSignerName:
2020-08-10 17:43:49 +00:00
return isKubeletServing, nil
2020-03-26 21:07:15 +00:00
case capi.KubeAPIServerClientKubeletSignerName:
2020-08-10 17:43:49 +00:00
return isKubeletClient, nil
2020-03-26 21:07:15 +00:00
case capi.KubeAPIServerClientSignerName:
2020-08-10 17:43:49 +00:00
return isKubeAPIServerClient, nil
case capiv1beta1.LegacyUnknownSignerName:
return isLegacyUnknown, nil
2020-03-26 21:07:15 +00:00
default:
2020-08-10 17:43:49 +00:00
// TODO type this error so that a different reporting loop (one without a signing cert), can mark
// CSRs with unknown kube signers as terminal if we wish. This largely depends on how tightly we want to control
// our signerNames.
return nil, fmt.Errorf("unrecognized signerName: %q", signerName)
}
}
func isKubeletServing(req *x509.CertificateRequest, usages []capi.KeyUsage, signerName string) (bool, error) {
if signerName != capi.KubeletServingSignerName {
return false, nil
}
return true, capihelper.ValidateKubeletServingCSR(req, usagesToSet(usages))
}
func isKubeletClient(req *x509.CertificateRequest, usages []capi.KeyUsage, signerName string) (bool, error) {
if signerName != capi.KubeAPIServerClientKubeletSignerName {
return false, nil
2020-03-26 21:07:15 +00:00
}
2020-08-10 17:43:49 +00:00
return true, capihelper.ValidateKubeletClientCSR(req, usagesToSet(usages))
2020-03-26 21:07:15 +00:00
}
2020-08-10 17:43:49 +00:00
func isKubeAPIServerClient(req *x509.CertificateRequest, usages []capi.KeyUsage, signerName string) (bool, error) {
if signerName != capi.KubeAPIServerClientSignerName {
return false, nil
}
return true, validAPIServerClientUsages(usages)
}
func isLegacyUnknown(req *x509.CertificateRequest, usages []capi.KeyUsage, signerName string) (bool, error) {
if signerName != capiv1beta1.LegacyUnknownSignerName {
return false, nil
}
// No restrictions are applied to the legacy-unknown signerName to
// maintain backward compatibility in v1.
return true, nil
}
func validAPIServerClientUsages(usages []capi.KeyUsage) error {
2020-03-26 21:07:15 +00:00
hasClientAuth := false
for _, u := range usages {
switch u {
// these usages are optional
case capi.UsageDigitalSignature, capi.UsageKeyEncipherment:
case capi.UsageClientAuth:
hasClientAuth = true
default:
2020-08-10 17:43:49 +00:00
return fmt.Errorf("invalid usage for client certificate: %s", u)
2020-03-26 21:07:15 +00:00
}
}
2020-08-10 17:43:49 +00:00
if !hasClientAuth {
return fmt.Errorf("missing required usage for client certificate: %s", capi.UsageClientAuth)
}
return nil
}
func usagesToSet(usages []capi.KeyUsage) sets.String {
result := sets.NewString()
for _, usage := range usages {
result.Insert(string(usage))
}
return result
2019-12-12 01:27:03 +00:00
}