@@ -18,6 +18,8 @@ package cert
1818
1919import (
2020 "crypto/sha256"
21+ "crypto/x509"
22+ "encoding/pem"
2123 "fmt"
2224 "io"
2325 "net"
@@ -29,7 +31,9 @@ import (
2931 certutil "k8s.io/client-go/util/cert"
3032 "k8s.io/klog/v2"
3133
34+ initopt "github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/options"
3235 "github.com/karmada-io/karmada/pkg/karmadactl/cmdinit/utils"
36+ globalopt "github.com/karmada-io/karmada/pkg/karmadactl/options"
3337 "github.com/karmada-io/karmada/pkg/util/names"
3438)
3539
@@ -189,3 +193,63 @@ func compareCertFilesInDirs(dir1, dir2, filename string) (bool, error) {
189193 file2 := filepath .Join (dir2 , filename )
190194 return compareFiles (file1 , file2 )
191195}
196+
197+ // helper: read certificate from dir/name.{crt}
198+ func readCertFromPath (t * testing.T , dir , name string ) * x509.Certificate {
199+ t .Helper ()
200+ b , err := os .ReadFile (filepath .Join (dir , fmt .Sprintf ("%s.crt" , name )))
201+ if err != nil {
202+ t .Fatalf ("failed reading cert %s: %v" , name , err )
203+ }
204+ blk , _ := pem .Decode (b )
205+ if blk == nil {
206+ t .Fatalf ("failed decoding PEM for %s" , name )
207+ }
208+ crt , err := x509 .ParseCertificate (blk .Bytes )
209+ if err != nil {
210+ t .Fatalf ("failed parsing x509 for %s: %v" , name , err )
211+ }
212+ return crt
213+ }
214+
215+ // TestNewGenCerts_CASelection verifies certificates are signed by the expected CA
216+ // according to their names: etcd-* by etcd-ca, front-proxy-client by front-proxy-ca,
217+ // others by main CA.
218+ func TestNewGenCerts_CASelection (t * testing.T ) {
219+ dir := t .TempDir ()
220+ notAfter := time .Now ().Add (Duration365d ).UTC ()
221+
222+ cfg := map [string ]* CertsConfig {
223+ // main CA signer
224+ initopt .KarmadaAPIServerCertAndKeyName : NewCertConfig (initopt .KarmadaAPIServerCN , nil , certutil.AltNames {DNSNames : []string {"localhost" }, IPs : []net.IP {utils .StringToNetIP ("127.0.0.1" )}}, & notAfter ),
225+ // front-proxy CA signer
226+ initopt .FrontProxyClientCertAndKeyName : NewCertConfig (initopt .KarmadaFrontProxyClientCN , nil , certutil.AltNames {}, & notAfter ),
227+ // etcd CA signer
228+ initopt .KarmadaAPIServerEtcdClientCertAndKeyName : NewCertConfig (initopt .KarmadaAPIServerEtcdClientCN , nil , certutil.AltNames {}, & notAfter ),
229+ }
230+
231+ if err := NewGenCerts (dir , "" , "" , cfg ); err != nil {
232+ t .Fatalf ("NewGenCerts error: %v" , err )
233+ }
234+
235+ // load CA certs
236+ ca := readCertFromPath (t , dir , globalopt .CaCertAndKeyName )
237+ etcdCA := readCertFromPath (t , dir , initopt .EtcdCaCertAndKeyName )
238+ fpCA := readCertFromPath (t , dir , initopt .FrontProxyCaCertAndKeyName )
239+
240+ cases := []struct {
241+ name string
242+ expected string
243+ }{
244+ {initopt .KarmadaAPIServerCertAndKeyName , ca .Subject .CommonName },
245+ {initopt .FrontProxyClientCertAndKeyName , fpCA .Subject .CommonName },
246+ {initopt .KarmadaAPIServerEtcdClientCertAndKeyName , etcdCA .Subject .CommonName },
247+ }
248+
249+ for _ , tc := range cases {
250+ crt := readCertFromPath (t , dir , tc .name )
251+ if got := crt .Issuer .CommonName ; got != tc .expected {
252+ t .Fatalf ("%s issuer CN = %q, want %q" , tc .name , got , tc .expected )
253+ }
254+ }
255+ }
0 commit comments