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: drop duplicate function NewCACertAndKey #76769

Merged
merged 1 commit into from
Apr 22, 2019
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
4 changes: 2 additions & 2 deletions cmd/kubeadm/app/phases/certs/certlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (k *KubeadmCert) CreateAsCA(ic *kubeadmapi.InitConfiguration) (*x509.Certif
if err != nil {
return nil, nil, errors.Wrapf(err, "couldn't get configuration for %q CA certificate", k.Name)
}
caCert, caKey, err := NewCACertAndKey(cfg)
caCert, caKey, err := pkiutil.NewCertificateAuthority(cfg)
if err != nil {
return nil, nil, errors.Wrapf(err, "couldn't generate %q CA certificate", k.Name)
}
Expand Down Expand Up @@ -141,7 +141,7 @@ func (t CertificateTree) CreateTree(ic *kubeadmapi.InitConfiguration) error {
// CA key exists; just use that to create new certificates.
} else {
// CACert doesn't already exist, create a new cert and key.
caCert, caKey, err = NewCACertAndKey(cfg)
caCert, caKey, err = pkiutil.NewCertificateAuthority(cfg)
if err != nil {
return err
}
Expand Down
13 changes: 1 addition & 12 deletions cmd/kubeadm/app/phases/certs/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,6 @@ func NewServiceAccountSigningKey() (*rsa.PrivateKey, error) {
return saSigningKey, nil
}

// NewCACertAndKey will generate a self signed CA.
func NewCACertAndKey(certSpec *certutil.Config) (*x509.Certificate, *rsa.PrivateKey, error) {

caCert, caKey, err := pkiutil.NewCertificateAuthority(certSpec)
if err != nil {
return nil, nil, errors.Wrap(err, "failure while generating CA certificate and key")
Copy link
Member

Choose a reason for hiding this comment

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

IMO, it's provider a better error message, using only pkiutil.NewCertificateAuthority will return unable to create private key or unable to create self-signed certificate.
I'm not against the change, but will leave the lgtm to other reviews.

Copy link
Author

Choose a reason for hiding this comment

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

If the sole purpose of this function is to give a better error message then the messages returned by pkiutil.NewCertificateAuthority could be made clearer, e.g. unable to create private key while generating CA certificate and unable to create self-signed CA certificate. What do you think?

Also additional error wrapping goes against the rationale explained in kubernetes/kubeadm#1266

Copy link
Contributor

Choose a reason for hiding this comment

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

You both have a point here. The func was introduced in its form by Fabrizio, so it's probably best to let him decide on this one.

/assign @fabriziopandini

}

return caCert, caKey, nil
}

// CreateCACertAndKeyFiles generates and writes out a given certificate authority.
// The certSpec should be one of the variables from this package.
func CreateCACertAndKeyFiles(certSpec *KubeadmCert, cfg *kubeadmapi.InitConfiguration) error {
Expand All @@ -114,7 +103,7 @@ func CreateCACertAndKeyFiles(certSpec *KubeadmCert, cfg *kubeadmapi.InitConfigur
return err
}

caCert, caKey, err := NewCACertAndKey(certConfig)
caCert, caKey, err := pkiutil.NewCertificateAuthority(certConfig)
if err != nil {
return err
}
Expand Down
10 changes: 0 additions & 10 deletions cmd/kubeadm/app/phases/certs/certs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,16 +370,6 @@ func TestWriteKeyFilesIfNotExist(t *testing.T) {
}
}

func TestNewCACertAndKey(t *testing.T) {
certCfg := &certutil.Config{CommonName: "kubernetes"}
caCert, _, err := NewCACertAndKey(certCfg)
if err != nil {
t.Fatalf("failed call NewCACertAndKey: %v", err)
}

certstestutil.AssertCertificateIsCa(t, caCert)
}

func TestSharedCertificateExists(t *testing.T) {
caCert, caKey := certstestutil.CreateCACert(t)
_, key, _ := certstestutil.CreateTestCert(t, caCert, caKey, certutil.AltNames{})
Expand Down
1 change: 0 additions & 1 deletion cmd/kubeadm/app/phases/certs/renewal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ go_test(
],
embed = [":go_default_library"],
deps = [
"//cmd/kubeadm/app/phases/certs:go_default_library",
"//cmd/kubeadm/app/util/certs:go_default_library",
"//cmd/kubeadm/app/util/pkiutil:go_default_library",
"//cmd/kubeadm/test:go_default_library",
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubeadm/app/phases/certs/renewal/filerenewal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
"testing"

certutil "k8s.io/client-go/util/cert"
"k8s.io/kubernetes/cmd/kubeadm/app/phases/certs"
"k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
)

func TestFileRenew(t *testing.T) {
caCertCfg := &certutil.Config{CommonName: "kubernetes"}
caCert, caKey, err := certs.NewCACertAndKey(caCertCfg)
caCert, caKey, err := pkiutil.NewCertificateAuthority(caCertCfg)
if err != nil {
t.Fatalf("couldn't create CA: %v", err)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/kubeadm/app/phases/certs/renewal/renewal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ import (
fakecerts "k8s.io/client-go/kubernetes/typed/certificates/v1beta1/fake"
k8stesting "k8s.io/client-go/testing"
certutil "k8s.io/client-go/util/cert"
"k8s.io/kubernetes/cmd/kubeadm/app/phases/certs"
certtestutil "k8s.io/kubernetes/cmd/kubeadm/app/util/certs"
"k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
testutil "k8s.io/kubernetes/cmd/kubeadm/test"
)

func TestRenewImplementations(t *testing.T) {
caCertCfg := &certutil.Config{CommonName: "kubernetes"}
caCert, caKey, err := certs.NewCACertAndKey(caCertCfg)
caCert, caKey, err := pkiutil.NewCertificateAuthority(caCertCfg)
if err != nil {
t.Fatalf("couldn't create CA: %v", err)
}
Expand Down Expand Up @@ -198,7 +197,7 @@ func TestRenewExistingCert(t *testing.T) {
}

caCertCfg := &certutil.Config{CommonName: "kubernetes"}
caCert, caKey, err := certs.NewCACertAndKey(caCertCfg)
caCert, caKey, err := pkiutil.NewCertificateAuthority(caCertCfg)
if err != nil {
t.Fatalf("couldn't create CA: %v", err)
}
Expand Down
7 changes: 0 additions & 7 deletions cmd/kubeadm/app/util/certs/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ func SetupCertificateAuthorithy(t *testing.T) (*x509.Certificate, *rsa.PrivateKe
return caCert, caKey
}

// AssertCertificateIsCa is a utility function for kubeadm testing that asserts if a given certificate is a CA
func AssertCertificateIsCa(t *testing.T, cert *x509.Certificate) {
if !cert.IsCA {
t.Error("cert is not a valida CA")
}
}

// AssertCertificateIsSignedByCa is a utility function for kubeadm testing that asserts if a given certificate is signed
// by the expected CA
func AssertCertificateIsSignedByCa(t *testing.T, cert *x509.Certificate, signingCa *x509.Certificate) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubeadm/app/util/pkiutil/pki_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ const (
func NewCertificateAuthority(config *certutil.Config) (*x509.Certificate, *rsa.PrivateKey, error) {
key, err := NewPrivateKey()
if err != nil {
return nil, nil, errors.Wrap(err, "unable to create private key")
return nil, nil, errors.Wrap(err, "unable to create private key while generating CA certificate")
}

cert, err := certutil.NewSelfSignedCACert(*config, key)
if err != nil {
return nil, nil, errors.Wrap(err, "unable to create self-signed certificate")
return nil, nil, errors.Wrap(err, "unable to create self-signed CA certificate")
}

return cert, key, nil
Expand Down
17 changes: 7 additions & 10 deletions cmd/kubeadm/app/util/pkiutil/pki_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,17 @@ func TestNewCertificateAuthority(t *testing.T) {
cert, key, err := NewCertificateAuthority(&certutil.Config{CommonName: "kubernetes"})

if cert == nil {
t.Errorf(
"failed NewCertificateAuthority, cert == nil",
)
t.Error("failed NewCertificateAuthority, cert == nil")
} else if !cert.IsCA {
t.Error("cert is not a valida CA")
}

if key == nil {
t.Errorf(
"failed NewCertificateAuthority, key == nil",
)
t.Error("failed NewCertificateAuthority, key == nil")
}

if err != nil {
t.Errorf(
"failed NewCertificateAuthority with an error: %v",
err,
)
t.Errorf("failed NewCertificateAuthority with an error: %+v", err)
}
}

Expand Down