Skip to content
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

Replace time.Before with time.Sub #213

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
17 changes: 14 additions & 3 deletions pkg/resource/tls_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ func (s *TLSSecret) IsRotationRequired(duration time.Duration, cronStr string) (

// validate expiry. If expiry is before the next cron, then rotate the certificate
validUpto := annotations[CertValidUpto]

return s.CertExpired(time.Now(), cronStr, validUpto)
}

func (s *TLSSecret) CertExpired(now time.Time, cronStr string, validUpto string) (bool, string) {
expiryTime, err := time.Parse(time.RFC3339, validUpto)
if err != nil {
return true, "Failed to verify expiry date, rotating certificate"
Expand All @@ -156,14 +161,20 @@ func (s *TLSSecret) IsRotationRequired(duration time.Duration, cronStr string) (
return true, "Failed to verify expiry date due to invalid cron, rotating certificate"
}

nextRun := cronSchedule.Next(time.Now())
nextRun := cronSchedule.Next(now)
nextToNextRun := cronSchedule.Next(nextRun)

if expiryTime.Before(nextRun) {
// if cert is expiring before next run or within (next run + 1 hour)
if expiryTime.Sub(nextRun) < 1*time.Hour {
return true, "Certificate about to expire, rotating certificate"
}

return false, ""
// if cert is expiring before next-to-next run or within (next-to-next run + 1 hour)
if expiryTime.Sub(nextToNextRun) < 1*time.Hour {
return true, "Certificate about to expire, rotating certificate"
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why we need to check the next to next run + 1 hour, as we are already checking next run + 1 hour, the certificate expiring after next run + 1 hour will be rotated in the next run.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@prafull01 your comment does make sense. I don't exactly remember the scenario since it was more than a month ago (should have documented the issue properly). I will remove this and update the unit test in next patch set.

Copy link
Contributor Author

@junaid-ali junaid-ali Mar 10, 2022

Choose a reason for hiding this comment

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

@prafull01 Apologies for addressing your comment late. I have just pushed a new patchset.


return false, ""
}

// Ready checks if secret contains required data
Expand Down
89 changes: 89 additions & 0 deletions pkg/resource/tls_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,92 @@ func secretObj(name, namespace string, data map[string][]byte, annotations map[s
Data: data,
}
}

func TestCertExpired(t *testing.T) {
type args struct {
now time.Time
cronStr string
validUpto string
}
tests := []struct {
name string
secret *resource.TLSSecret
args args
rotate bool
reason string
}{
{
name: "not-expiring-soon",
secret: &resource.TLSSecret{},
args: args{
now: time.Date(2021, time.November, 10, 0, 0, 0, 0, time.UTC),
cronStr: "0 0 */23 * *",
validUpto: time.Date(2021, time.December, 02, 0, 0, 0, 0, time.UTC).Format(time.RFC3339),
},
rotate: false,
reason: "",
},
{
name: "invalid-expiration",
secret: &resource.TLSSecret{},
args: args{
now: time.Now(),
cronStr: "0 0 */23 * *",
validUpto: "invalid-expiration-date",
},
rotate: true,
reason: "Failed to verify expiry date, rotating certificate",
},
{
name: "expire-before-next-run",
secret: &resource.TLSSecret{},
args: args{
now: time.Date(2021, time.November, 10, 0, 0, 0, 0, time.UTC),
cronStr: "0 0 */23 * *",
validUpto: time.Date(2021, time.November, 22, 0, 0, 0, 0, time.UTC).Format(time.RFC3339),
},
rotate: true,
reason: "Certificate about to expire, rotating certificate",
},
{
name: "expire-before-next-to-next-run",
secret: &resource.TLSSecret{},
args: args{
now: time.Date(2021, time.November, 10, 0, 0, 0, 0, time.UTC),
cronStr: "0 0 */23 * *",
validUpto: time.Date(2021, time.November, 27, 0, 0, 0, 0, time.UTC).Format(time.RFC3339),
},
rotate: true,
reason: "Certificate about to expire, rotating certificate",
},
{
name: "expire-equals-next-to-next-run",
secret: &resource.TLSSecret{},
args: args{
now: time.Date(2021, time.November, 10, 0, 0, 0, 0, time.UTC),
cronStr: "0 0 */23 * *",
validUpto: time.Date(2021, time.December, 1, 0, 0, 0, 0, time.UTC).Format(time.RFC3339),
},
rotate: true,
reason: "Certificate about to expire, rotating certificate",
},
{
name: "not-expiring-before-next-to-next-run",
secret: &resource.TLSSecret{},
args: args{
now: time.Date(2021, time.November, 10, 0, 0, 0, 0, time.UTC),
cronStr: "0 0 */23 * *",
validUpto: time.Date(2021, time.December, 2, 0, 0, 0, 0, time.UTC).Format(time.RFC3339),
},
rotate: false,
reason: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
needsRotation, msg := tt.secret.CertExpired(tt.args.now, tt.args.cronStr, tt.args.validUpto)
assert.Equal(t, tt.rotate, needsRotation)
assert.Equal(t, tt.reason, msg)
})
}
}