Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

renew certs within Kubernetes secrets before they expire #112

Merged
merged 7 commits into from
Nov 17, 2017
Merged

Conversation

xh3b4sd
Copy link
Contributor

@xh3b4sd xh3b4sd commented Nov 16, 2017

No description provided.

@xh3b4sd xh3b4sd self-assigned this Nov 16, 2017
@taylorbot taylorbot deployed to lycan November 16, 2017 21:36 Active
@taylorbot taylorbot deployed to lycan November 16, 2017 22:30 Active
Copy link
Contributor Author

@xh3b4sd xh3b4sd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully tested on lycan. Works like a charm. 🎉

@@ -25,6 +26,9 @@ func (r *Resource) GetDesiredState(ctx context.Context, obj interface{}) (interf
secret := &apiv1.Secret{
ObjectMeta: apismetav1.ObjectMeta{
Name: key.SecretName(customObject),
Annotations: map[string]string{
UpdateTimestampAnnotation: r.currentTimeFactory().In(time.UTC).Format(UpdateTimestampLayout),
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The secrets that contain the certificates have now an annotation for the timestamp they where they were last updated. This timestamp is used to calculate whether the certificate tracked by the secret should be renewed.

type VaultCrt struct {
ExpirationThreshold string
Namespace string
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is trying to make it right in the first place, plus boyscouting. ExpirationThreshold is a new option and Namespace already existed but was only hard coded in the resource implementation.

@@ -77,48 +75,3 @@ func (r *Resource) newCreateChange(ctx context.Context, obj, currentState, desir

return secretToCreate, nil
}

func (r *Resource) ensureVaultRole(customObject certificatetpr.CustomObject) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now used for creation and deletion, so I moved it to the resource.go to have the code in a more common place.

@@ -104,14 +105,18 @@ func Test_Resource_VaultCrt_newCreateChange(t *testing.T) {
var err error
var newResource *Resource
{
resourceConfig := DefaultConfig()
c := DefaultConfig()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boyscouting.


newResource, err = New(resourceConfig)
c.ExpirationThreshold = 24 * time.Hour
c.Namespace = "default"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new flags are required.

VaultRole vaultrole.Interface

Namespace string
CurrentTimeFactory func() time.Time
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to add this to keep the code testable.

return false, microerror.Mask(err)
}

if t.Add(TTL).Add(-threshold).Before(r.currentTimeFactory()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t is the time of the last update. t plus the certificate TTL is the point in time where the certificate expires. We want to renew certificates before they expire. Thus we substitute a threshold from the point in time of the expiration and compare this value against the current time.

resource:
vaultCrt:
expirationThreshold: '24h'
namespace: 'default'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new settings are properly set in the config map. Note that the 24 hours is a magic number I just made up. IMO it should be good enough to let the cert-operator know 24 hours before the certificates expire that it should actually renew them in the Kubernetes secrets.

UpdateTimestampAnnotation = "giantswarm.io/update-timestamp"
// UpdateTimestampLayout is the time layout used to format and parse the
// update timestamps tracked in the annotations of the Kubernetes secrets.
UpdateTimestampLayout = "2006-01-02T15:04:05.000000Z"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is Layout the right term here? I was confused here. I think the better term is Format

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL. Thanks! <3

}

renew, err := r.shouldCertBeRenewed(currentSecret, TTL, r.expirationThreshold)
if IsMissingAnnotation(err) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the annotation is missing, could we add it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We add it with new clusters. I think there will be a transition period like in the kvm-operator with the version bundle versions tracked in the deployments.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking was we could avoid the transition period. No strong opinion.

@xh3b4sd xh3b4sd merged commit 2968c32 into master Nov 17, 2017
@xh3b4sd xh3b4sd deleted the expire branch November 17, 2017 10:21
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants