Skip to content

Commit

Permalink
feat: karmadactl add ca-cert-path and ca-key-path opts
Browse files Browse the repository at this point in the history
Signed-off-by: guozheng-shen <[email protected]>
  • Loading branch information
guozheng-shen committed Jul 10, 2024
1 parent c4a81bf commit 09f9600
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
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, caCertPath, caKeyPath string, etcdServerCertCfg, etcdClientCertCfg, karmadaCertCfg, apiserverCertCfg, frontProxyClientCertCfg *CertsConfig) error {
caCert, caKey, err := getCertAndKey(caCertPath, caKeyPath)
if err != nil {
return err
}

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

func getCertAndKey(caCertPath, caKeyPath string) (caCert *x509.Certificate, caKey *crypto.Signer, err error) {
if caKeyPath != "" && caCertPath != "" {
certificate, err := tls.LoadX509KeyPair(caCertPath, caKeyPath)
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
9 changes: 7 additions & 2 deletions pkg/karmadactl/cmdinit/cert/cert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ 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) {
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 {
fmt.Println(err)
}
if err := GenCerts(TestCertsTmp, TestCaCertPath, TestCaKeyPath, etcdServerCertConfig, etcdClientCertCfg, karmadaCertCfg, apiserverCertCfg, frontProxyClientCertCfg); err != nil {
fmt.Println(err)
}
}
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.CaCertPath, "ca-cert-path", "", "", "The path of CA cert, if not set, will gen new, the option must be specified together with ca-key-path")
flags.StringVarP(&opts.CaKeyPath, "ca-key-path", "", "", "The path of CA key, if not set, will gen new, the option must be specified together with ca-cert-path")
// Kubernetes
flags.StringVarP(&opts.Namespace, "namespace", "n", "karmada-system", "Kubernetes namespace")
flags.StringVar(&opts.StorageClassesName, "storage-classes-name", "", "Kubernetes StorageClasses Name")
Expand Down
4 changes: 3 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
CaCertPath string
CaKeyPath string
}

func (i *CommandInitOption) validateLocalEtcd(parentCommand string) error {
Expand Down Expand Up @@ -353,7 +355,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.CaCertPath, i.CaKeyPath, etcdServerCertConfig, etcdClientCertCfg, karmadaCertCfg, apiserverCertCfg, frontProxyClientCertCfg); err != nil {
return err
}
return nil
Expand Down

0 comments on commit 09f9600

Please sign in to comment.