@@ -111,72 +111,79 @@ type genCertArgs struct {
111111 isCA * bool
112112}
113113
114- // signedCertificate generate certificate signed by root certificate
115- func (gca genCertArgs ) signedCertificate (cfg * cgutilcert.Config ) (string , string ) {
116- parentKey , err := TryLoadKeyFromDisk (gca .rootKey )
114+ // signedCertificate generates a certificate signed by the root certificate
115+ func (gca genCertArgs ) signedCertificate (cfg cgutilcert.Config ) (string , string ) {
116+ // Load CA private key
117+ caKey , err := TryLoadKeyFromDisk (gca .rootKey )
117118 if err != nil {
118119 return "" , fmt .Sprintf ("failed to load root key: %v" , err )
119120 }
120- parentCert , _ , err := TryLoadCertChainFromDisk (gca .rootCert )
121+ // Load CA certificate
122+ caCert , _ , err := TryLoadCertChainFromDisk (gca .rootCert )
121123 if err != nil {
122124 return "" , fmt .Sprintf ("failed to load root certificate: %v" , err )
123125 }
124126
125- if gca . policy == policyIfNotPresent {
126- if _ , err := TryLoadKeyFromDisk ( gca . outKey ); err != nil {
127- klog . V ( 4 ). InfoS ( "Failed to load out key, new it" )
128-
129- goto NEW
127+ // Function to generate and write new certificate and key
128+ generateAndWrite := func () ( string , string ) {
129+ newKey , err := rsa . GenerateKey ( cryptorand . Reader , rsaKeySize )
130+ if err != nil {
131+ return "" , fmt . Sprintf ( "generate rsa key error: %v" , err )
130132 }
131-
132- existCert , intermediates , err := TryLoadCertChainFromDisk (gca .outCert )
133+ newCert , err := NewSignedCert (cfg , gca .date , newKey , caCert , caKey , ptr .Deref (gca .isCA , false ))
133134 if err != nil {
134- klog .V (4 ).InfoS ("Failed to load out cert, new it" )
135+ return "" , fmt .Sprintf ("failed to generate certificate: %v" , err )
136+ }
137+ if err := WriteKey (gca .outKey , newKey , gca .policy ); err != nil {
138+ return "" , fmt .Sprintf ("failed to write key: %v" , err )
139+ }
140+ if err := WriteCert (gca .outCert , newCert , gca .policy ); err != nil {
141+ return "" , fmt .Sprintf ("failed to write certificate: %v" , err )
142+ }
143+ return StdoutSuccess , ""
144+ }
135145
136- goto NEW
146+ switch gca .policy {
147+ case policyIfNotPresent :
148+ // Check if key exists
149+ if _ , err := TryLoadKeyFromDisk (gca .outKey ); err != nil {
150+ klog .V (4 ).InfoS ("Failed to load out key, create it" )
151+ return generateAndWrite ()
152+ }
153+ // Check if certificate exists
154+ existCert , existIntermediates , err := TryLoadCertChainFromDisk (gca .outCert )
155+ if err != nil {
156+ klog .V (4 ).InfoS ("Failed to load out cert, create it" )
157+ return generateAndWrite ()
137158 }
138- // check if the existing key and cert match the root key and cert
159+ // Validate certificate period
139160 if err := ValidateCertPeriod (existCert , 0 ); err != nil {
140161 return "" , fmt .Sprintf ("failed to ValidateCertPeriod: %v" , err )
141162 }
142- if err := VerifyCertChain (existCert , intermediates , parentCert ); err != nil {
163+ // Validate certificate chain
164+ if err := VerifyCertChain (existCert , existIntermediates , caCert ); err != nil {
143165 return "" , fmt .Sprintf ("failed to VerifyCertChain: %v" , err )
144166 }
167+ // Validate certificate SAN and other config
145168 if err := validateCertificateWithConfig (existCert , gca .outCert , cfg ); err != nil {
146169 return "" , fmt .Sprintf ("failed to validateCertificateWithConfig: %v" , err )
147170 }
148-
171+ // Existing certificate and key are valid, skip generation
149172 return StdoutSkip , ""
173+ default :
174+ // Otherwise, always generate new certificate and key
175+ return generateAndWrite ()
150176 }
151- NEW:
152- newKey , err := rsa .GenerateKey (cryptorand .Reader , rsaKeySize )
153- if err != nil {
154- return "" , fmt .Sprintf ("generate rsa key error: %v" , err )
155- }
156- newCert , err := NewSignedCert (* cfg , gca .date , newKey , parentCert , parentKey , ptr .Deref (gca .isCA , false ))
157- if err != nil {
158- return "" , fmt .Sprintf ("failed to generate certificate: %v" , err )
159- }
160-
161- // write key and cert to file
162- if err := WriteKey (gca .outKey , newKey , gca .policy ); err != nil {
163- return "" , fmt .Sprintf ("failed to write key: %v" , err )
164- }
165- if err := WriteCert (gca .outCert , newCert , gca .policy ); err != nil {
166- return "" , fmt .Sprintf ("failed to write certificate: %v" , err )
167- }
168-
169- return StdoutSuccess , ""
170177}
171178
172179// selfSignedCertificate generate Self-signed certificate
173- func (gca genCertArgs ) selfSignedCertificate (cfg * cgutilcert.Config ) (string , string ) {
180+ func (gca genCertArgs ) selfSignedCertificate (cfg cgutilcert.Config ) (string , string ) {
174181 newKey , err := rsa .GenerateKey (cryptorand .Reader , rsaKeySize )
175182 if err != nil {
176183 return "" , fmt .Sprintf ("generate rsa key error: %v" , err )
177184 }
178185
179- newCert , err := NewSelfSignedCACert (* cfg , gca .date , newKey )
186+ newCert , err := NewSelfSignedCACert (cfg , gca .date , newKey )
180187 if err != nil {
181188 return "" , fmt .Sprintf ("failed to generate self-signed certificate: %v" , err )
182189 }
@@ -239,9 +246,9 @@ func ModuleGenCert(ctx context.Context, options ExecOptions) (string, string) {
239246
240247 switch {
241248 case gca .rootKey == "" || gca .rootCert == "" :
242- return gca .selfSignedCertificate (cfg )
249+ return gca .selfSignedCertificate (* cfg )
243250 default :
244- return gca .signedCertificate (cfg )
251+ return gca .signedCertificate (* cfg )
245252 }
246253}
247254
@@ -503,7 +510,7 @@ func VerifyCertChain(cert *x509.Certificate, intermediates []*x509.Certificate,
503510
504511// validateCertificateWithConfig makes sure that a given certificate is valid at
505512// least for the SANs defined in the configuration.
506- func validateCertificateWithConfig (cert * x509.Certificate , baseName string , cfg * cgutilcert.Config ) error {
513+ func validateCertificateWithConfig (cert * x509.Certificate , baseName string , cfg cgutilcert.Config ) error {
507514 for _ , dnsName := range cfg .AltNames .DNSNames {
508515 if err := cert .VerifyHostname (dnsName ); err != nil {
509516 return errors .Wrapf (err , "certificate %s is invalid" , baseName )
0 commit comments