-
Notifications
You must be signed in to change notification settings - Fork 172
ROX-9129: Determine secret expiration by parsing certificates stored in secrets #471
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
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6ad77ed
determine secret expiration by parsing certificates stored in secrets
1ba1e47
fix style
e1c021d
fix style
9395f0e
fix style
ac5d574
use fixed time instead of now
ab76c5c
remove references to scanner
49a9ef7
test public method
a47f850
improve naming of fields and methods
328ddc5
add comment explaining cert renewal time criteria
61be725
fix style
08214f4
adapt to certificate set instead of secrets
95c75f9
fix style
8a00438
fix style
223a2ea
update testutilsMTLS import
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package localscanner | ||
|
|
||
| import ( | ||
| "crypto/x509" | ||
| "math/rand" | ||
| "time" | ||
|
|
||
| "github.com/cloudflare/cfssl/helpers" | ||
| "github.com/pkg/errors" | ||
| "github.com/stackrox/rox/generated/storage" | ||
| ) | ||
|
|
||
| var ( | ||
| // ErrEmptyCertificate indicates that the certificate stored in a secret is empty. | ||
| ErrEmptyCertificate = errors.New("empty certificate") | ||
| ) | ||
|
|
||
| // GetCertsRenewalTime computes the time when the service certificates should be refreshed. | ||
| // If different services have different expiration times then the earliest time is returned. | ||
| func GetCertsRenewalTime(certificates *storage.TypedServiceCertificateSet) (time.Time, error) { | ||
| var ( | ||
| renewalTime time.Time | ||
| renewalTimeInitialized bool | ||
| ) | ||
| for _, certificate := range certificates.GetServiceCerts() { | ||
| certRenewalTime, err := getCertificateRenewalTime(certificate) | ||
| if err != nil { | ||
| return renewalTime, err | ||
| } | ||
| if !renewalTimeInitialized || certRenewalTime.Before(renewalTime) { | ||
| renewalTimeInitialized = true | ||
| renewalTime = certRenewalTime | ||
| } | ||
| } | ||
| return renewalTime, nil | ||
| } | ||
|
|
||
| func getCertificateRenewalTime(certificate *storage.TypedServiceCertificate) (time.Time, error) { | ||
| certBytes := certificate.GetCert().GetCertPem() | ||
| var ( | ||
| cert *x509.Certificate | ||
| err error | ||
| ) | ||
| if len(certBytes) == 0 { | ||
| err = ErrEmptyCertificate | ||
| } else { | ||
| cert, err = helpers.ParseCertificatePEM(certBytes) | ||
| } | ||
| if err != nil { | ||
| var zeroTime time.Time | ||
| return zeroTime, err | ||
| } | ||
|
|
||
| return calculateRenewalTime(cert), nil | ||
| } | ||
|
|
||
| // In order to ensure certificates are rotated before expiration, this returns a renewal time no later than | ||
| // half its expiration date. | ||
| func calculateRenewalTime(cert *x509.Certificate) time.Time { | ||
| certValidityDurationSecs := cert.NotAfter.Sub(cert.NotBefore).Seconds() | ||
| durationBeforeRenewalAttempt := time.Second * | ||
juanrh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| (time.Duration(certValidityDurationSecs/2) - time.Duration(rand.Intn(int(certValidityDurationSecs/10)))) | ||
| certRenewalTime := cert.NotBefore.Add(durationBeforeRenewalAttempt) | ||
| return certRenewalTime | ||
| } | ||
82 changes: 82 additions & 0 deletions
82
sensor/kubernetes/localscanner/certificate_expiration_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package localscanner | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stackrox/rox/generated/storage" | ||
| "github.com/stackrox/rox/pkg/mtls" | ||
| testutilsMTLS "github.com/stackrox/rox/pkg/mtls/testutils" | ||
| "github.com/stackrox/rox/pkg/testutils/envisolator" | ||
| "github.com/stretchr/testify/suite" | ||
| ) | ||
|
|
||
| var ( | ||
| // should be the same as the expiration corresponding to `mtls.WithValidityExpiringInHours()`. | ||
| afterOffset = 3 * time.Hour | ||
| ) | ||
|
|
||
| func TestGetSecretRenewalTime(t *testing.T) { | ||
| suite.Run(t, new(getSecretRenewalTimeSuite)) | ||
| } | ||
|
|
||
| type getSecretRenewalTimeSuite struct { | ||
| suite.Suite | ||
| envIsolator *envisolator.EnvIsolator | ||
| } | ||
|
|
||
| func (s *getSecretRenewalTimeSuite) SetupSuite() { | ||
| s.envIsolator = envisolator.NewEnvIsolator(s.T()) | ||
| } | ||
|
|
||
| func (s *getSecretRenewalTimeSuite) SetupTest() { | ||
| err := testutilsMTLS.LoadTestMTLSCerts(s.envIsolator) | ||
| s.Require().NoError(err) | ||
| } | ||
|
|
||
| func (s *getSecretRenewalTimeSuite) TearDownTest() { | ||
| s.envIsolator.RestoreAll() | ||
| } | ||
|
|
||
| func (s *getSecretRenewalTimeSuite) TestGetSecretsCertRenewalTime() { | ||
| certPEMHours, err := issueCertificatePEM(mtls.WithValidityExpiringInHours()) | ||
| s.Require().NoError(err) | ||
| certPEMDays, err := issueCertificatePEM(mtls.WithValidityExpiringInDays()) | ||
| s.Require().NoError(err) | ||
| certificates := &storage.TypedServiceCertificateSet{ | ||
| CaPem: make([]byte, 0), | ||
| ServiceCerts: []*storage.TypedServiceCertificate{ | ||
| { | ||
| ServiceType: storage.ServiceType_SCANNER_SERVICE, | ||
| Cert: &storage.ServiceCertificate{ | ||
| CertPem: certPEMHours, | ||
| }, | ||
| }, | ||
| { | ||
| ServiceType: storage.ServiceType_SCANNER_DB_SERVICE, | ||
| Cert: &storage.ServiceCertificate{ | ||
| CertPem: certPEMDays, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| certRenewalTime, err := GetCertsRenewalTime(certificates) | ||
|
|
||
| s.Require().NoError(err) | ||
| certDuration := time.Until(certRenewalTime) | ||
| s.LessOrEqual(certDuration, afterOffset/2) | ||
| } | ||
|
|
||
| func issueCertificatePEM(issueOption mtls.IssueCertOption) ([]byte, error) { | ||
| ca, err := mtls.CAForSigning() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| subject := mtls.NewSubject("clusterId", storage.ServiceType_SCANNER_SERVICE) | ||
| cert, err := ca.IssueCertForSubject(subject, issueOption) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return cert.CertPEM, err | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, doesn't this code always return the first certificate found instead of returning the shorter expiration date?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renewalTimeInitializedis set to true the first time therenewalTimeis set, so subsequent updates torenewalTimeare only done ifsecretRenewalTime.Before(renewalTime). In any case I've added a test forGetSecretsCertRenewalTimethat also checks this