Skip to content

Commit 4adafe5

Browse files
committed
Handle invalid auto cert provider ValidityDuration
This commit will handle a scenario of invalid auto cert provider ValidityDuration user input and throw an error. In case, ValidityDuration is not defined by user it will default to 365days for auto cert provider Signed-off-by: ArkaSaha30 <[email protected]>
1 parent 4861f78 commit 4adafe5

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

internal/controller/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ func createCMCertificateConfig(ec *ecv1alpha1.EtcdCluster) (*certInterface.Confi
615615
return config, nil
616616
}
617617

618-
func createAutoCertificateConfig(ec *ecv1alpha1.EtcdCluster) *certInterface.Config {
618+
func createAutoCertificateConfig(ec *ecv1alpha1.EtcdCluster) (*certInterface.Config, error) {
619619
autoConfig := ec.Spec.TLS.ProviderCfg.AutoCfg
620620

621621
// Set default duration to 365 days for auto provider if not provided
@@ -643,7 +643,7 @@ func createAutoCertificateConfig(ec *ecv1alpha1.EtcdCluster) *certInterface.Conf
643643
ValidityDuration: duration,
644644
AltNames: altNames,
645645
}
646-
return config
646+
return config, nil
647647
}
648648

649649
func createCertificate(ec *ecv1alpha1.EtcdCluster, ctx context.Context, c client.Client, certName string) error {

pkg/certificate/auto/provider.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ import (
2828
)
2929

3030
const (
31-
DefaultValidity = 365 * 24 * time.Hour
31+
// oneYearInHours is used to convert validity duration to years for transport.SelfCert.
32+
// transport.SelfCert expects the validity parameter to be in years (uint).
33+
oneYearInHours = 365 * 24 * time.Hour
3234
)
3335

3436
type Provider struct {
@@ -218,9 +220,14 @@ func checkKeyPair(cert *x509.Certificate, privateKey crypto.PrivateKey) error {
218220
// https://github.com/etcd-io/etcd/blob/b87bc1c3a275d7d4904f4d201b963a2de2264f0d/client/pkg/transport/listener.go#L275
219221
func (ac *Provider) createNewSecret(ctx context.Context, secretKey client.ObjectKey,
220222
cfg *interfaces.Config) error {
221-
validity := DefaultValidity
223+
validity := interfaces.DefaultAutoValidity
222224
if cfg.ValidityDuration != 0 {
223-
validity = cfg.ValidityDuration * time.Hour
225+
validity = cfg.ValidityDuration
226+
}
227+
228+
// Validate validity duration: minimum is 1 year as required by etcd
229+
if validity < oneYearInHours {
230+
return fmt.Errorf("validity duration must be at least 1 year (365 days), got: %v", validity)
224231
}
225232

226233
tmpDir, err := os.MkdirTemp("", fmt.Sprintf("etcd-auto-cert-%s-*", secretKey.Name))
@@ -252,7 +259,15 @@ func (ac *Provider) createNewSecret(ctx context.Context, secretKey client.Object
252259

253260
log.Printf("calling SelfCert with hosts: %v", hosts)
254261

255-
tlsInfo, selfCertErr := transport.SelfCert(zap.NewNop(), tmpDir, hosts, uint(validity/DefaultValidity))
262+
// Convert validity duration to years for transport.SelfCert
263+
// transport.SelfCert expects the validity parameter to be in years (uint)
264+
validityYears := uint(validity / oneYearInHours)
265+
if validityYears == 0 {
266+
// This should not happen due to the earlier check, but adding as a safeguard
267+
return fmt.Errorf("validity duration converts to 0 years, must be at least 1 year")
268+
}
269+
270+
tlsInfo, selfCertErr := transport.SelfCert(zap.NewNop(), tmpDir, hosts, validityYears)
256271
if selfCertErr != nil {
257272
return fmt.Errorf("certificate creation via transport.SelfCert failed: %w", selfCertErr)
258273
}

pkg/certificate/interfaces/interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ const (
4747
MaxRetries = 36
4848
RetryInterval = 5 * time.Second
4949

50+
// DefaultAutoValidity is the default validity duration for auto-generated certificates (365 days)
51+
DefaultAutoValidity = 365 * 24 * time.Hour
52+
5053
// DefaultCertManagerValidity is the default validity duration for cert-manager certificates (90 days)
5154
DefaultCertManagerValidity = 90 * 24 * time.Hour
5255
)

test/e2e/auto_provider_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
const (
3030
autoCertificateName = "sample-cert"
3131
autoCertificateNamespace = "default"
32-
autoCertificateValidity = auto.DefaultValidity
32+
autoCertificateValidity = interfaces.DefaultAutoValidity
3333
)
3434

3535
func TestAutoProvider(t *testing.T) {

0 commit comments

Comments
 (0)