Skip to content

Commit

Permalink
Merge pull request #106927 from neolit123/automated-cherry-pick-of-#1…
Browse files Browse the repository at this point in the history
…06854-origin-release-1.20

Automated cherry pick of #106854: kubeadm: avoid requiring a CA key during kubeconfig
  • Loading branch information
k8s-ci-robot committed Dec 10, 2021
2 parents cd5fe52 + bfa3af3 commit 05b7435
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cmd/kubeadm/app/phases/certs/renewal/readwriter.go
Expand Up @@ -28,7 +28,7 @@ import (
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
certutil "k8s.io/client-go/util/cert"
"k8s.io/client-go/util/keyutil"
certsphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/certs"

pkiutil "k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
)

Expand Down Expand Up @@ -140,11 +140,15 @@ func (rw *kubeConfigReadWriter) Read() (*x509.Certificate, error) {
// For local CA renewal, the local CA on disk could have changed, thus a reload is needed.
// For CSR renewal we assume the same CA on disk is mounted for usage with KCM's
// '--cluster-signing-cert-file' flag.
caCert, _, err := certsphase.LoadCertificateAuthority(rw.certificateDir, rw.baseName)
certificatePath, _ := pkiutil.PathsForCertAndKey(rw.certificateDir, rw.baseName)
caCerts, err := certutil.CertsFromFile(certificatePath)
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "failed to load existing certificate %s", rw.baseName)
}
if len(caCerts) != 1 {
return nil, errors.Errorf("wanted exactly one certificate, got %d", len(caCerts))
}
rw.caCert = caCert
rw.caCert = caCerts[0]

// get current context
if _, ok := kubeConfig.Contexts[kubeConfig.CurrentContext]; !ok {
Expand Down
5 changes: 5 additions & 0 deletions cmd/kubeadm/app/phases/certs/renewal/readwriter_test.go
Expand Up @@ -125,6 +125,11 @@ func TestKubeconfigReadWriter(t *testing.T) {
t.Fatalf("couldn't write new embedded certificate: %v", err)
}

// Make sure that CA key is not present during Read() as it is not needed.
// This covers testing when the CA is external and not present on the host.
_, caKeyPath := pkiutil.PathsForCertAndKey(dirPKI, caName)
os.Remove(caKeyPath)

// Reads back the new certificate embedded in a kubeconfig writer
readCert, err = kubeconfigReadWriter.Read()
if err != nil {
Expand Down

0 comments on commit 05b7435

Please sign in to comment.