...

Source file src/k8s.io/kubernetes/pkg/controller/certificates/signer/ca_provider.go

Documentation: k8s.io/kubernetes/pkg/controller/certificates/signer

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package signer
    18  
    19  import (
    20  	"bytes"
    21  	"crypto"
    22  	"fmt"
    23  	"sync/atomic"
    24  
    25  	"k8s.io/apiserver/pkg/server/dynamiccertificates"
    26  	"k8s.io/client-go/util/cert"
    27  	"k8s.io/client-go/util/keyutil"
    28  	"k8s.io/kubernetes/pkg/controller/certificates/authority"
    29  )
    30  
    31  func newCAProvider(caFile, caKeyFile string) (*caProvider, error) {
    32  	caLoader, err := dynamiccertificates.NewDynamicServingContentFromFiles("csr-controller", caFile, caKeyFile)
    33  	if err != nil {
    34  		return nil, fmt.Errorf("error reading CA cert file %q: %v", caFile, err)
    35  	}
    36  
    37  	ret := &caProvider{
    38  		caLoader: caLoader,
    39  	}
    40  	if err := ret.setCA(); err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	return ret, nil
    45  }
    46  
    47  type caProvider struct {
    48  	caValue  atomic.Value
    49  	caLoader *dynamiccertificates.DynamicCertKeyPairContent
    50  }
    51  
    52  // setCA unconditionally stores the current cert/key content
    53  func (p *caProvider) setCA() error {
    54  	certPEM, keyPEM := p.caLoader.CurrentCertKeyContent()
    55  
    56  	certs, err := cert.ParseCertsPEM(certPEM)
    57  	if err != nil {
    58  		return fmt.Errorf("error reading CA cert file %q: %v", p.caLoader.Name(), err)
    59  	}
    60  	if len(certs) != 1 {
    61  		return fmt.Errorf("error reading CA cert file %q: expected 1 certificate, found %d", p.caLoader.Name(), len(certs))
    62  	}
    63  
    64  	key, err := keyutil.ParsePrivateKeyPEM(keyPEM)
    65  	if err != nil {
    66  		return fmt.Errorf("error reading CA key file %q: %v", p.caLoader.Name(), err)
    67  	}
    68  	priv, ok := key.(crypto.Signer)
    69  	if !ok {
    70  		return fmt.Errorf("error reading CA key file %q: key did not implement crypto.Signer", p.caLoader.Name())
    71  	}
    72  
    73  	ca := &authority.CertificateAuthority{
    74  		RawCert: certPEM,
    75  		RawKey:  keyPEM,
    76  
    77  		Certificate: certs[0],
    78  		PrivateKey:  priv,
    79  	}
    80  	p.caValue.Store(ca)
    81  
    82  	return nil
    83  }
    84  
    85  // currentCA provides the current value of the CA.
    86  // It always check for a stale value.  This is cheap because it's all an in memory cache of small slices.
    87  func (p *caProvider) currentCA() (*authority.CertificateAuthority, error) {
    88  	certPEM, keyPEM := p.caLoader.CurrentCertKeyContent()
    89  	currCA := p.caValue.Load().(*authority.CertificateAuthority)
    90  	if bytes.Equal(currCA.RawCert, certPEM) && bytes.Equal(currCA.RawKey, keyPEM) {
    91  		return currCA, nil
    92  	}
    93  
    94  	// the bytes weren't equal, so we have to set and then load
    95  	if err := p.setCA(); err != nil {
    96  		return currCA, err
    97  	}
    98  	return p.caValue.Load().(*authority.CertificateAuthority), nil
    99  }
   100  

View as plain text