Skip to content

Commit da947ef

Browse files
Merge pull request #2277 from vrutkovs/backport-130047
OCPBUGS-54208: UPSTREAM: 130047: adjusting loopback certificate validity in kube-apiserver
2 parents 9c64611 + ba07fe1 commit da947ef

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

staging/src/k8s.io/apiserver/pkg/server/options/serving_with_loopback.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package options
1818

1919
import (
2020
"fmt"
21+
"time"
2122

2223
"github.com/google/uuid"
2324

@@ -49,9 +50,18 @@ func (s *SecureServingOptionsWithLoopback) ApplyTo(secureServingInfo **server.Se
4950
return nil
5051
}
5152

53+
// Set a validity period of approximately 3 years for the loopback certificate
54+
// to avoid kube-apiserver disruptions due to certificate expiration.
55+
// When this certificate expires, restarting kube-apiserver will automatically
56+
// regenerate a new certificate with fresh validity dates.
57+
maxAge := (3*365 + 1) * 24 * time.Hour
58+
5259
// create self-signed cert+key with the fake server.LoopbackClientServerNameOverride and
5360
// let the server return it when the loopback client connects.
54-
certPem, keyPem, err := certutil.GenerateSelfSignedCertKey(server.LoopbackClientServerNameOverride, nil, nil)
61+
certPem, keyPem, err := certutil.GenerateSelfSignedCertKeyWithOptions(certutil.SelfSignedCertKeyOptions{
62+
Host: server.LoopbackClientServerNameOverride,
63+
MaxAge: maxAge,
64+
})
5565
if err != nil {
5666
return fmt.Errorf("failed to generate self-signed certificate for loopback connection: %v", err)
5767
}

staging/src/k8s.io/client-go/util/cert/cert.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,37 @@ func NewSelfSignedCACert(cfg Config, key crypto.Signer) (*x509.Certificate, erro
9090
return x509.ParseCertificate(certDERBytes)
9191
}
9292

93+
// SelfSignedCertKeyOptions contains configuration parameters for generating self-signed certificates.
94+
type SelfSignedCertKeyOptions struct {
95+
// Host is required, and identifies the host of the serving certificate. Can be a DNS name or IP address.
96+
Host string
97+
// AlternateIPs is optional, and identifies additional IPs the serving certificate should be valid for.
98+
AlternateIPs []net.IP
99+
// AlternateDNS is optional, and identifies additional DNS names the serving certificate should be valid for.
100+
AlternateDNS []string
101+
102+
// MaxAge controls the duration of the issued certificate.
103+
// Defaults to 1 year if unset.
104+
// Ignored if FixtureDirectory is set.
105+
MaxAge time.Duration
106+
107+
// FixtureDirectory is intended for use in tests.
108+
// If non-empty, it is a directory path which can contain pre-generated certs. The format is:
109+
// <host>_<ip>-<ip>_<alternateDNS>-<alternateDNS>.crt
110+
// <host>_<ip>-<ip>_<alternateDNS>-<alternateDNS>.key
111+
// Certs/keys not existing in that directory are created with a duration of 100 years.
112+
FixtureDirectory string
113+
}
114+
93115
// GenerateSelfSignedCertKey creates a self-signed certificate and key for the given host.
94116
// Host may be an IP or a DNS name
95117
// You may also specify additional subject alt names (either ip or dns names) for the certificate.
96118
func GenerateSelfSignedCertKey(host string, alternateIPs []net.IP, alternateDNS []string) ([]byte, []byte, error) {
97-
return GenerateSelfSignedCertKeyWithFixtures(host, alternateIPs, alternateDNS, "")
119+
return GenerateSelfSignedCertKeyWithOptions(SelfSignedCertKeyOptions{
120+
Host: host,
121+
AlternateIPs: alternateIPs,
122+
AlternateDNS: alternateDNS,
123+
})
98124
}
99125

100126
// GenerateSelfSignedCertKeyWithFixtures creates a self-signed certificate and key for the given host.
@@ -106,8 +132,26 @@ func GenerateSelfSignedCertKey(host string, alternateIPs []net.IP, alternateDNS
106132
// <host>_<ip>-<ip>_<alternateDNS>-<alternateDNS>.key
107133
// Certs/keys not existing in that directory are created.
108134
func GenerateSelfSignedCertKeyWithFixtures(host string, alternateIPs []net.IP, alternateDNS []string, fixtureDirectory string) ([]byte, []byte, error) {
135+
return GenerateSelfSignedCertKeyWithOptions(SelfSignedCertKeyOptions{
136+
Host: host,
137+
AlternateIPs: alternateIPs,
138+
AlternateDNS: alternateDNS,
139+
FixtureDirectory: fixtureDirectory,
140+
})
141+
}
142+
143+
// GenerateSelfSignedCertKeyWithOptions generates a self-signed certificate and key based on the provided options.
144+
func GenerateSelfSignedCertKeyWithOptions(opts SelfSignedCertKeyOptions) ([]byte, []byte, error) {
145+
host := opts.Host
146+
alternateIPs := opts.AlternateIPs
147+
alternateDNS := opts.AlternateDNS
148+
fixtureDirectory := opts.FixtureDirectory
149+
maxAge := opts.MaxAge
150+
if maxAge == 0 {
151+
maxAge = 365 * 24 * time.Hour
152+
}
153+
109154
validFrom := time.Now().Add(-time.Hour) // valid an hour earlier to avoid flakes due to clock skew
110-
maxAge := time.Hour * 24 * 365 // one year self-signed certs
111155

112156
baseName := fmt.Sprintf("%s_%s_%s", host, strings.Join(ipsToStrings(alternateIPs), "-"), strings.Join(alternateDNS, "-"))
113157
certFixturePath := filepath.Join(fixtureDirectory, baseName+".crt")

0 commit comments

Comments
 (0)