Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kubeadm: aggregate all the errors when the shared certs are validated #106042

Merged
merged 1 commit into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/kubeadm/app/cmd/phases/join/preflight.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func checkIfReadyForAdditionalControlPlane(initConfiguration *kubeadmapi.Cluster
}

if !hasCertificateKey {
// checks if the certificates that must be equal across controlplane instances are provided
// checks if the certificates are provided and are still valid, not expired yet.
if ret, err := certs.SharedCertificateExists(initConfiguration); !ret {
return err
}
Expand Down
21 changes: 12 additions & 9 deletions cmd/kubeadm/app/phases/certs/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/pkg/errors"

utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/util/keyutil"
"k8s.io/klog/v2"

Expand Down Expand Up @@ -304,30 +305,32 @@ type certKeyLocation struct {
uxName string
}

// SharedCertificateExists verifies if the shared certificates - the certificates that must be
// equal across control-plane nodes: ca.key, ca.crt, sa.key, sa.pub + etcd/ca.key, etcd/ca.crt if local/stacked etcd
// Missing keys are non-fatal and produce warnings.
// SharedCertificateExists verifies if the shared certificates exist and are still valid - the certificates must be
// equal across control-plane nodes: ca.key, ca.crt, sa.key, sa.pub, front-proxy-ca.key, front-proxy-ca.crt and etcd/ca.key, etcd/ca.crt if local/stacked etcd
// Missing private keys of CA are non-fatal and produce warnings.
func SharedCertificateExists(cfg *kubeadmapi.ClusterConfiguration) (bool, error) {

var errs []error
if err := validateCACertAndKey(certKeyLocation{cfg.CertificatesDir, kubeadmconstants.CACertAndKeyBaseName, "", "CA"}); err != nil {
return false, err
errs = append(errs, err)
}

if err := validatePrivatePublicKey(certKeyLocation{cfg.CertificatesDir, "", kubeadmconstants.ServiceAccountKeyBaseName, "service account"}); err != nil {
return false, err
errs = append(errs, err)
}

if err := validateCACertAndKey(certKeyLocation{cfg.CertificatesDir, kubeadmconstants.FrontProxyCACertAndKeyBaseName, "", "front-proxy CA"}); err != nil {
return false, err
errs = append(errs, err)
}

// in case of local/stacked etcd
if cfg.Etcd.External == nil {
if err := validateCACertAndKey(certKeyLocation{cfg.CertificatesDir, kubeadmconstants.EtcdCACertAndKeyBaseName, "", "etcd CA"}); err != nil {
return false, err
errs = append(errs, err)
}
}

if len(errs) != 0 {
return false, utilerrors.NewAggregate(errs)
Copy link
Member

@neolit123 neolit123 Nov 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is worth updating the unit tests to check how many errors were returned exactly:
https://github.com/kubernetes/kubernetes/blob/e363bbdddb17100e6cc41a36c83caac49ea4c3c7/cmd/kubeadm/app/phases/certs/certs_test.go#L378

expectedError bool -> expectedErrors int

len(err) != len(expectedErrors) { unit test failed, print err }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just found that testcase "missing etcd/ca.crt" is not wrote correctly, it is just a copy of "missing front-proxy.crt". 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
return true, nil
}

Expand Down
41 changes: 27 additions & 14 deletions cmd/kubeadm/app/phases/certs/certs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"

utilerrors "k8s.io/apimachinery/pkg/util/errors"
certutil "k8s.io/client-go/util/cert"
"k8s.io/client-go/util/keyutil"

Expand Down Expand Up @@ -373,9 +374,9 @@ func TestSharedCertificateExists(t *testing.T) {
publicKey := key.Public()

var tests = []struct {
name string
files certstestutil.PKIFiles
expectedError bool
name string
files certstestutil.PKIFiles
expectedErrors int
}{
{
name: "success",
Expand All @@ -401,7 +402,7 @@ func TestSharedCertificateExists(t *testing.T) {
"etcd/ca.crt": caCert,
"etcd/ca.key": caKey,
},
expectedError: true,
expectedErrors: 1,
},
{
name: "missing ca.key",
Expand All @@ -414,7 +415,6 @@ func TestSharedCertificateExists(t *testing.T) {
"etcd/ca.crt": caCert,
"etcd/ca.key": caKey,
},
expectedError: false,
},
{
name: "missing sa.key",
Expand All @@ -427,7 +427,7 @@ func TestSharedCertificateExists(t *testing.T) {
"etcd/ca.crt": caCert,
"etcd/ca.key": caKey,
},
expectedError: true,
expectedErrors: 1,
},
{
name: "missing front-proxy.crt",
Expand All @@ -440,20 +440,32 @@ func TestSharedCertificateExists(t *testing.T) {
"etcd/ca.crt": caCert,
"etcd/ca.key": caKey,
},
expectedError: true,
expectedErrors: 1,
},
{
name: "missing etcd/ca.crt",
files: certstestutil.PKIFiles{
"ca.crt": caCert,
"ca.key": caKey,
"front-proxy-ca.crt": caCert,
"front-proxy-ca.key": caKey,
"sa.pub": publicKey,
"sa.key": key,
"etcd/ca.crt": caCert,
"etcd/ca.key": caKey,
},
expectedError: true,
expectedErrors: 1,
},
{
name: "missing multiple certs (ca.crt and etcd/ca.crt)",
files: certstestutil.PKIFiles{
"ca.key": caKey,
"front-proxy-ca.crt": caCert,
"front-proxy-ca.key": caKey,
"sa.pub": publicKey,
"sa.key": key,
"etcd/ca.key": caKey,
},
expectedErrors: 2,
},
}

Expand All @@ -472,12 +484,13 @@ func TestSharedCertificateExists(t *testing.T) {

// executes create func
ret, err := SharedCertificateExists(cfg)

switch {
case !test.expectedError && err != nil:
t.Errorf("error SharedCertificateExists failed when not expected to fail: %v", err)
case test.expectedError && err == nil:
t.Errorf("error SharedCertificateExists didn't failed when expected")
case err != nil:
if agg, ok := err.(utilerrors.Aggregate); ok && len(agg.Errors()) != test.expectedErrors {
t.Errorf("SharedCertificateExists didn't fail with the expected number of errors, expected: %v, got: %v", test.expectedErrors, len(agg.Errors()))
}
case err == nil && test.expectedErrors != 0:
t.Errorf("error SharedCertificateExists didn't fail when expected")
case ret != (err == nil):
t.Errorf("error SharedCertificateExists returned %v when expected to return %v", ret, err == nil)
}
Expand Down