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

feat: karmadactl add ca-cert-path and ca-key-path opts #5127

Merged
merged 1 commit into from
Jul 12, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions pkg/karmadactl/cmdinit/cert/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
Expand Down Expand Up @@ -261,11 +262,12 @@ func NewCertConfig(cn string, org []string, altNames certutil.AltNames, notAfter
}

// GenCerts Create CA certificate and sign etcd karmada certificate.
func GenCerts(pkiPath string, etcdServerCertCfg, etcdClientCertCfg, karmadaCertCfg, apiserverCertCfg, frontProxyClientCertCfg *CertsConfig) error {
caCert, caKey, err := NewCACertAndKey("karmada")
func GenCerts(pkiPath, caCertFile, caKeyFile string, etcdServerCertCfg, etcdClientCertCfg, karmadaCertCfg, apiserverCertCfg, frontProxyClientCertCfg *CertsConfig) error {
caCert, caKey, err := getCACertAndKey(caCertFile, caKeyFile)
if err != nil {
return err
}

if err = WriteCertAndKey(pkiPath, globaloptions.CaCertAndKeyName, caCert, caKey); err != nil {
return err
}
RainbowMango marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -309,6 +311,27 @@ func GenCerts(pkiPath string, etcdServerCertCfg, etcdClientCertCfg, karmadaCertC
return genEtcdCerts(pkiPath, etcdServerCertCfg, etcdClientCertCfg)
}

func getCACertAndKey(caCertFile, caKeyFile string) (caCert *x509.Certificate, caKey *crypto.Signer, err error) {
if caKeyFile != "" && caCertFile != "" {
certificate, err := tls.LoadX509KeyPair(caCertFile, caKeyFile)
if err != nil {
return nil, nil, err
}
caCert, err = x509.ParseCertificate(certificate.Certificate[0])
if err != nil {
return nil, nil, err
}
key := certificate.PrivateKey.(crypto.Signer)
caKey = &key
} else {
caCert, caKey, err = NewCACertAndKey("karmada")
if err != nil {
return nil, nil, err
}
}
return caCert, caKey, nil
}

func genEtcdCerts(pkiPath string, etcdServerCertCfg, etcdClientCertCfg *CertsConfig) error {
etcdCaCert, etcdCaKey, err := NewCACertAndKey("etcd-ca")
if err != nil {
Expand Down
11 changes: 8 additions & 3 deletions pkg/karmadactl/cmdinit/cert/cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ import (
)

const (
TestCertsTmp = "./test-certs-tmp"
TestCertsTmp = "./test-certs-tmp"
TestCaCertPath = "./test-certs-tmp/ca.crt"
TestCaKeyPath = "./test-certs-tmp/ca.key"
)

func TestGenCerts(_ *testing.T) {
func TestGenCerts(t *testing.T) {
defer os.RemoveAll(TestCertsTmp)

notAfter := time.Now().Add(Duration365d * 10).UTC()
Expand Down Expand Up @@ -101,7 +103,10 @@ func TestGenCerts(_ *testing.T) {
apiserverCertCfg := NewCertConfig("karmada-apiserver", []string{""}, karmadaAltNames, &notAfter)
frontProxyClientCertCfg := NewCertConfig("front-proxy-client", []string{}, certutil.AltNames{}, &notAfter)

if err := GenCerts(TestCertsTmp, etcdServerCertConfig, etcdClientCertCfg, karmadaCertCfg, apiserverCertCfg, frontProxyClientCertCfg); err != nil {
if err := GenCerts(TestCertsTmp, "", "", etcdServerCertConfig, etcdClientCertCfg, karmadaCertCfg, apiserverCertCfg, frontProxyClientCertCfg); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

It would be even better if you could supplement the test coverage for this case GenCerts(TestCertsTmp, caCertPath, caKeyPath, etcdServerCertConfig, etcdClientCertCfg, karmadaCertCfg, apiserverCertCfg, frontProxyClientCertCfg) ٩(๑❛ᴗ❛๑)۶ .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

t.Fatal(err)
}
if err := GenCerts(TestCertsTmp, TestCaCertPath, TestCaKeyPath, etcdServerCertConfig, etcdClientCertCfg, karmadaCertCfg, apiserverCertCfg, frontProxyClientCertCfg); err != nil {
fmt.Println(err)
}
}
RainbowMango marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions pkg/karmadactl/cmdinit/cmdinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func NewCmdInit(parentCommand string) *cobra.Command {
flags.StringVar(&opts.ExternalIP, "cert-external-ip", "", "the external IP of Karmada certificate (e.g 192.168.1.2,172.16.1.2)")
flags.StringVar(&opts.ExternalDNS, "cert-external-dns", "", "the external DNS of Karmada certificate (e.g localhost,localhost.com)")
flags.DurationVar(&opts.CertValidity, "cert-validity-period", cert.Duration365d, "the validity period of Karmada certificate (e.g 8760h0m0s, that is 365 days)")
flags.StringVarP(&opts.CaCertFile, "ca-cert-file", "", "", "The root CA certificate file which will be used to issue new certificates for Karmada components. If not set, a new self-signed root CA certificate will be generated. This must be used together with --ca-key-file.")
flags.StringVarP(&opts.CaKeyFile, "ca-key-file", "", "", "The root CA private key file which will be used to issue new certificates for Karmada components. If not set, a new self-signed root CA key will be generated. This must be used together with --ca-cert-file.")
// Kubernetes
flags.StringVarP(&opts.Namespace, "namespace", "n", "karmada-system", "Kubernetes namespace")
flags.StringVar(&opts.StorageClassesName, "storage-classes-name", "", "Kubernetes StorageClasses Name")
Expand Down
7 changes: 6 additions & 1 deletion pkg/karmadactl/cmdinit/kubernetes/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ type CommandInitOption struct {
KarmadaAPIServerIP []net.IP
HostClusterDomain string
WaitComponentReadyTimeout int
CaCertFile string
CaKeyFile string
}

func (i *CommandInitOption) validateLocalEtcd(parentCommand string) error {
Expand Down Expand Up @@ -225,6 +227,9 @@ func (i *CommandInitOption) Validate(parentCommand string) error {
return fmt.Errorf("karmada apiserver advertise address is not valid")
}
}
if (i.CaCertFile != "") != (i.CaKeyFile != "") {
return fmt.Errorf("ca-cert-file and ca-key-file must be used together")
}

switch i.ImagePullPolicy {
case string(corev1.PullAlways), string(corev1.PullIfNotPresent), string(corev1.PullNever):
Expand Down Expand Up @@ -353,7 +358,7 @@ func (i *CommandInitOption) genCerts() error {
apiserverCertCfg := cert.NewCertConfig("karmada-apiserver", []string{""}, karmadaAltNames, &notAfter)

frontProxyClientCertCfg := cert.NewCertConfig("front-proxy-client", []string{}, certutil.AltNames{}, &notAfter)
if err = cert.GenCerts(i.KarmadaPkiPath, etcdServerCertConfig, etcdClientCertCfg, karmadaCertCfg, apiserverCertCfg, frontProxyClientCertCfg); err != nil {
if err = cert.GenCerts(i.KarmadaPkiPath, i.CaCertFile, i.CaKeyFile, etcdServerCertConfig, etcdClientCertCfg, karmadaCertCfg, apiserverCertCfg, frontProxyClientCertCfg); err != nil {
return err
}
return nil
Expand Down