Skip to content

Commit 0f35d66

Browse files
Merge pull request #430 from perdasilva/cert_rotation_fix_backport
OCPBUGS-5938: backport cert rotation fix
2 parents 10727ea + 35ec3f1 commit 0f35d66

File tree

10 files changed

+579
-61
lines changed

10 files changed

+579
-61
lines changed

staging/operator-lifecycle-manager/pkg/controller/install/certresources.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func (i *StrategyDeploymentInstaller) getCertResources() []certResource {
160160
}
161161

162162
func (i *StrategyDeploymentInstaller) certResourcesForDeployment(deploymentName string) []certResource {
163-
result := []certResource{}
163+
var result []certResource
164164
for _, desc := range i.getCertResources() {
165165
if desc.getDeploymentName() == deploymentName {
166166
result = append(result, desc)
@@ -185,13 +185,12 @@ func (i *StrategyDeploymentInstaller) installCertRequirements(strategy Strategy)
185185
}
186186

187187
// Create the CA
188-
expiration := time.Now().Add(DefaultCertValidFor)
189-
ca, err := certs.GenerateCA(expiration, Organization)
188+
i.certificateExpirationTime = CalculateCertExpiration(time.Now())
189+
ca, err := certs.GenerateCA(i.certificateExpirationTime, Organization)
190190
if err != nil {
191191
logger.Debug("failed to generate CA")
192192
return nil, err
193193
}
194-
rotateAt := expiration.Add(-1 * DefaultCertMinFresh)
195194

196195
for n, sddSpec := range strategyDetailsDeployment.DeploymentSpecs {
197196
certResources := i.certResourcesForDeployment(sddSpec.Name)
@@ -202,7 +201,7 @@ func (i *StrategyDeploymentInstaller) installCertRequirements(strategy Strategy)
202201
}
203202

204203
// Update the deployment for each certResource
205-
newDepSpec, caPEM, err := i.installCertRequirementsForDeployment(sddSpec.Name, ca, rotateAt, sddSpec.Spec, getServicePorts(certResources))
204+
newDepSpec, caPEM, err := i.installCertRequirementsForDeployment(sddSpec.Name, ca, i.certificateExpirationTime, sddSpec.Spec, getServicePorts(certResources))
206205
if err != nil {
207206
return nil, err
208207
}
@@ -214,6 +213,14 @@ func (i *StrategyDeploymentInstaller) installCertRequirements(strategy Strategy)
214213
return strategyDetailsDeployment, nil
215214
}
216215

216+
func (i *StrategyDeploymentInstaller) CertsRotateAt() time.Time {
217+
return CalculateCertRotatesAt(i.certificateExpirationTime)
218+
}
219+
220+
func (i *StrategyDeploymentInstaller) CertsRotated() bool {
221+
return i.certificatesRotated
222+
}
223+
217224
func ShouldRotateCerts(csv *v1alpha1.ClusterServiceVersion) bool {
218225
now := metav1.Now()
219226
if !csv.Status.CertsRotateAt.IsZero() && csv.Status.CertsRotateAt.Before(&now) {
@@ -223,7 +230,15 @@ func ShouldRotateCerts(csv *v1alpha1.ClusterServiceVersion) bool {
223230
return false
224231
}
225232

226-
func (i *StrategyDeploymentInstaller) installCertRequirementsForDeployment(deploymentName string, ca *certs.KeyPair, rotateAt time.Time, depSpec appsv1.DeploymentSpec, ports []corev1.ServicePort) (*appsv1.DeploymentSpec, []byte, error) {
233+
func CalculateCertExpiration(startingFrom time.Time) time.Time {
234+
return startingFrom.Add(DefaultCertValidFor)
235+
}
236+
237+
func CalculateCertRotatesAt(certExpirationTime time.Time) time.Time {
238+
return certExpirationTime.Add(-1 * DefaultCertMinFresh)
239+
}
240+
241+
func (i *StrategyDeploymentInstaller) installCertRequirementsForDeployment(deploymentName string, ca *certs.KeyPair, expiration time.Time, depSpec appsv1.DeploymentSpec, ports []corev1.ServicePort) (*appsv1.DeploymentSpec, []byte, error) {
227242
logger := log.WithFields(log.Fields{})
228243

229244
// Create a service for the deployment
@@ -263,7 +278,7 @@ func (i *StrategyDeploymentInstaller) installCertRequirementsForDeployment(deplo
263278
fmt.Sprintf("%s.%s", service.GetName(), i.owner.GetNamespace()),
264279
fmt.Sprintf("%s.%s.svc", service.GetName(), i.owner.GetNamespace()),
265280
}
266-
servingPair, err := certGenerator.Generate(rotateAt, Organization, ca, hosts)
281+
servingPair, err := certGenerator.Generate(expiration, Organization, ca, hosts)
267282
if err != nil {
268283
logger.Warnf("could not generate signed certs for hosts %v", hosts)
269284
return nil, nil, err
@@ -311,9 +326,12 @@ func (i *StrategyDeploymentInstaller) installCertRequirementsForDeployment(deplo
311326
secret = existingSecret
312327
caPEM = existingCAPEM
313328
caHash = certs.PEMSHA256(caPEM)
314-
} else if _, err := i.strategyClient.GetOpClient().UpdateSecret(secret); err != nil {
315-
logger.Warnf("could not update secret %s", secret.GetName())
316-
return nil, nil, err
329+
} else {
330+
if _, err := i.strategyClient.GetOpClient().UpdateSecret(secret); err != nil {
331+
logger.Warnf("could not update secret %s", secret.GetName())
332+
return nil, nil, err
333+
}
334+
i.certificatesRotated = true
317335
}
318336

319337
} else if k8serrors.IsNotFound(err) {
@@ -331,6 +349,7 @@ func (i *StrategyDeploymentInstaller) installCertRequirementsForDeployment(deplo
331349
return nil, nil, err
332350
}
333351
}
352+
i.certificatesRotated = true
334353
} else {
335354
return nil, nil, err
336355
}

staging/operator-lifecycle-manager/pkg/controller/install/deployment.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package install
33
import (
44
"fmt"
55
"hash/fnv"
6+
"time"
67

78
log "github.com/sirupsen/logrus"
89
appsv1 "k8s.io/api/apps/v1"
@@ -22,13 +23,15 @@ import (
2223
const DeploymentSpecHashLabelKey = "olm.deployment-spec-hash"
2324

2425
type StrategyDeploymentInstaller struct {
25-
strategyClient wrappers.InstallStrategyDeploymentInterface
26-
owner ownerutil.Owner
27-
previousStrategy Strategy
28-
templateAnnotations map[string]string
29-
initializers DeploymentInitializerFuncChain
30-
apiServiceDescriptions []certResource
31-
webhookDescriptions []certResource
26+
strategyClient wrappers.InstallStrategyDeploymentInterface
27+
owner ownerutil.Owner
28+
previousStrategy Strategy
29+
templateAnnotations map[string]string
30+
initializers DeploymentInitializerFuncChain
31+
apiServiceDescriptions []certResource
32+
webhookDescriptions []certResource
33+
certificateExpirationTime time.Time
34+
certificatesRotated bool
3235
}
3336

3437
var _ Strategy = &v1alpha1.StrategyDetailsDeployment{}
@@ -77,13 +80,15 @@ func NewStrategyDeploymentInstaller(strategyClient wrappers.InstallStrategyDeplo
7780
}
7881

7982
return &StrategyDeploymentInstaller{
80-
strategyClient: strategyClient,
81-
owner: owner,
82-
previousStrategy: previousStrategy,
83-
templateAnnotations: templateAnnotations,
84-
initializers: initializers,
85-
apiServiceDescriptions: apiDescs,
86-
webhookDescriptions: webhookDescs,
83+
strategyClient: strategyClient,
84+
owner: owner,
85+
previousStrategy: previousStrategy,
86+
templateAnnotations: templateAnnotations,
87+
initializers: initializers,
88+
apiServiceDescriptions: apiDescs,
89+
webhookDescriptions: webhookDescs,
90+
certificatesRotated: false,
91+
certificateExpirationTime: time.Time{},
8792
}
8893
}
8994

staging/operator-lifecycle-manager/pkg/controller/install/resolver.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package install
55

66
import (
77
"fmt"
8+
"time"
89

910
"github.com/operator-framework/api/pkg/operators/v1alpha1"
1011
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/wrappers"
@@ -20,6 +21,8 @@ type Strategy interface {
2021
type StrategyInstaller interface {
2122
Install(strategy Strategy) error
2223
CheckInstalled(strategy Strategy) (bool, error)
24+
CertsRotateAt() time.Time
25+
CertsRotated() bool
2326
}
2427

2528
type StrategyResolverInterface interface {
@@ -68,3 +71,11 @@ func (i *NullStrategyInstaller) Install(s Strategy) error {
6871
func (i *NullStrategyInstaller) CheckInstalled(s Strategy) (bool, error) {
6972
return true, nil
7073
}
74+
75+
func (i *NullStrategyInstaller) CertsRotateAt() time.Time {
76+
return time.Time{}
77+
}
78+
79+
func (i *NullStrategyInstaller) CertsRotated() bool {
80+
return false
81+
}

staging/operator-lifecycle-manager/pkg/controller/operators/olm/operator.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,11 +1607,9 @@ func (a *Operator) transitionCSVState(in v1alpha1.ClusterServiceVersion) (out *v
16071607
return
16081608
}
16091609

1610-
if out.HasCAResources() {
1610+
if installer.CertsRotated() {
16111611
now := metav1.Now()
1612-
expiration := now.Add(install.DefaultCertValidFor)
1613-
rotateAt := expiration.Add(-1 * install.DefaultCertMinFresh)
1614-
rotateTime := metav1.NewTime(rotateAt)
1612+
rotateTime := metav1.NewTime(installer.CertsRotateAt())
16151613
out.Status.CertsLastUpdated = &now
16161614
out.Status.CertsRotateAt = &rotateTime
16171615
}

0 commit comments

Comments
 (0)