Skip to content

OCPBUGS-5938: backport cert rotation fix #430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (i *StrategyDeploymentInstaller) getCertResources() []certResource {
}

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

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

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

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

func (i *StrategyDeploymentInstaller) CertsRotateAt() time.Time {
return CalculateCertRotatesAt(i.certificateExpirationTime)
}

func (i *StrategyDeploymentInstaller) CertsRotated() bool {
return i.certificatesRotated
}

func ShouldRotateCerts(csv *v1alpha1.ClusterServiceVersion) bool {
now := metav1.Now()
if !csv.Status.CertsRotateAt.IsZero() && csv.Status.CertsRotateAt.Before(&now) {
Expand All @@ -223,7 +230,15 @@ func ShouldRotateCerts(csv *v1alpha1.ClusterServiceVersion) bool {
return false
}

func (i *StrategyDeploymentInstaller) installCertRequirementsForDeployment(deploymentName string, ca *certs.KeyPair, rotateAt time.Time, depSpec appsv1.DeploymentSpec, ports []corev1.ServicePort) (*appsv1.DeploymentSpec, []byte, error) {
func CalculateCertExpiration(startingFrom time.Time) time.Time {
return startingFrom.Add(DefaultCertValidFor)
}

func CalculateCertRotatesAt(certExpirationTime time.Time) time.Time {
return certExpirationTime.Add(-1 * DefaultCertMinFresh)
}

func (i *StrategyDeploymentInstaller) installCertRequirementsForDeployment(deploymentName string, ca *certs.KeyPair, expiration time.Time, depSpec appsv1.DeploymentSpec, ports []corev1.ServicePort) (*appsv1.DeploymentSpec, []byte, error) {
logger := log.WithFields(log.Fields{})

// Create a service for the deployment
Expand Down Expand Up @@ -263,7 +278,7 @@ func (i *StrategyDeploymentInstaller) installCertRequirementsForDeployment(deplo
fmt.Sprintf("%s.%s", service.GetName(), i.owner.GetNamespace()),
fmt.Sprintf("%s.%s.svc", service.GetName(), i.owner.GetNamespace()),
}
servingPair, err := certGenerator.Generate(rotateAt, Organization, ca, hosts)
servingPair, err := certGenerator.Generate(expiration, Organization, ca, hosts)
if err != nil {
logger.Warnf("could not generate signed certs for hosts %v", hosts)
return nil, nil, err
Expand Down Expand Up @@ -311,9 +326,12 @@ func (i *StrategyDeploymentInstaller) installCertRequirementsForDeployment(deplo
secret = existingSecret
caPEM = existingCAPEM
caHash = certs.PEMSHA256(caPEM)
} else if _, err := i.strategyClient.GetOpClient().UpdateSecret(secret); err != nil {
logger.Warnf("could not update secret %s", secret.GetName())
return nil, nil, err
} else {
if _, err := i.strategyClient.GetOpClient().UpdateSecret(secret); err != nil {
logger.Warnf("could not update secret %s", secret.GetName())
return nil, nil, err
}
i.certificatesRotated = true
}

} else if k8serrors.IsNotFound(err) {
Expand All @@ -331,6 +349,7 @@ func (i *StrategyDeploymentInstaller) installCertRequirementsForDeployment(deplo
return nil, nil, err
}
}
i.certificatesRotated = true
} else {
return nil, nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package install
import (
"fmt"
"hash/fnv"
"time"

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

type StrategyDeploymentInstaller struct {
strategyClient wrappers.InstallStrategyDeploymentInterface
owner ownerutil.Owner
previousStrategy Strategy
templateAnnotations map[string]string
initializers DeploymentInitializerFuncChain
apiServiceDescriptions []certResource
webhookDescriptions []certResource
strategyClient wrappers.InstallStrategyDeploymentInterface
owner ownerutil.Owner
previousStrategy Strategy
templateAnnotations map[string]string
initializers DeploymentInitializerFuncChain
apiServiceDescriptions []certResource
webhookDescriptions []certResource
certificateExpirationTime time.Time
certificatesRotated bool
}

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

return &StrategyDeploymentInstaller{
strategyClient: strategyClient,
owner: owner,
previousStrategy: previousStrategy,
templateAnnotations: templateAnnotations,
initializers: initializers,
apiServiceDescriptions: apiDescs,
webhookDescriptions: webhookDescs,
strategyClient: strategyClient,
owner: owner,
previousStrategy: previousStrategy,
templateAnnotations: templateAnnotations,
initializers: initializers,
apiServiceDescriptions: apiDescs,
webhookDescriptions: webhookDescs,
certificatesRotated: false,
certificateExpirationTime: time.Time{},
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package install

import (
"fmt"
"time"

"github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/wrappers"
Expand All @@ -20,6 +21,8 @@ type Strategy interface {
type StrategyInstaller interface {
Install(strategy Strategy) error
CheckInstalled(strategy Strategy) (bool, error)
CertsRotateAt() time.Time
CertsRotated() bool
}

type StrategyResolverInterface interface {
Expand Down Expand Up @@ -68,3 +71,11 @@ func (i *NullStrategyInstaller) Install(s Strategy) error {
func (i *NullStrategyInstaller) CheckInstalled(s Strategy) (bool, error) {
return true, nil
}

func (i *NullStrategyInstaller) CertsRotateAt() time.Time {
return time.Time{}
}

func (i *NullStrategyInstaller) CertsRotated() bool {
return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -1607,11 +1607,9 @@ func (a *Operator) transitionCSVState(in v1alpha1.ClusterServiceVersion) (out *v
return
}

if out.HasCAResources() {
if installer.CertsRotated() {
now := metav1.Now()
expiration := now.Add(install.DefaultCertValidFor)
rotateAt := expiration.Add(-1 * install.DefaultCertMinFresh)
rotateTime := metav1.NewTime(rotateAt)
rotateTime := metav1.NewTime(installer.CertsRotateAt())
out.Status.CertsLastUpdated = &now
out.Status.CertsRotateAt = &rotateTime
}
Expand Down
Loading