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

Keep the last CA when creating a new one #287

Merged
merged 3 commits into from
Aug 26, 2021
Merged

Conversation

tvoran
Copy link
Member

@tvoran tvoran commented Aug 23, 2021

Keeps the last CA in the K8s API caBundle when generating a new CA. Should cover the case when follower replicas haven't received a new leaf cert from the new CA, and are still listening with a leaf cert from the previous CA.

Keeps the last CA in the K8s API caBundle when generating a new
CA. Should help cover when follower replicas haven't received a new
leaf cert from the new CA.
@tvoran tvoran requested review from tomhjp and benashz August 23, 2021 23:51
Copy link
Contributor

@tomhjp tomhjp left a comment

Choose a reason for hiding this comment

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

Just some nits, looks great!

// appendCert returns a new CA bundle:
// [0] last CA cert from oldCABundle
// [1] newCACert
func appendCert(oldCABundle, newCACert []byte) ([]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this name could be a bit better to capture that it's smarter than a simple append. Maybe prependPreviousCA, prependLastCA, or includePreviousCA, possibly with the argument order swapped? As of course the ordering is important too, because we always want to keep the CAs in age order.

Copy link
Member Author

Choose a reason for hiding this comment

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

prependLastCA sounds good to me, added in 5e2c53e

if next == nil {
break
}
if next.Type == "CERTIFICATE" {
Copy link
Contributor

Choose a reason for hiding this comment

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

What might we throw away in the else clause, we would never expect anything right? Perhaps worth logging a warning to surface anything unexpected?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's just what's in the header of the pem block I believe, so it could be CERTIFICATE, PRIVATE KEY, etc. But now, we wouldn't expect anything besides CERTIFICATE. Added a warning log in 4344a67.

@@ -217,6 +235,40 @@ func (s *GenSource) getBundleFromSecret() (Bundle, error) {
return bundle, nil
}

func (s *GenSource) getExistingCA(ctx context.Context) []byte {
var caBundle []byte
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: My personal preference is for less mutability where practical, and I think every case in this switch could just immediately return once it finds a result, rather than needing to track a variable throughout the function.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm good with that, changed in 5e2c53e.


for name, tc := range tests {
t.Run(name, func(t *testing.T) {
result, err := appendCert(tc.oldCAs, newBundle.CACert)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it worth adding a test case where you go through an additional generation of appendCert? I was thinking yes because of the fact it consumes its own output (eventually), and so it would be nice to ensure by induction that it can continue forever, but after thinking about it a bit more I'm not sure if it really adds much to the coverage. Up to you if you think it has value 🙂

Copy link
Member Author

Choose a reason for hiding this comment

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

Doesn't hurt to check, added in 5e2c53e.

tvoran added 2 commits August 25, 2021 16:45
- appendCert(oldCAs, newCA) -> prependLastCA(newCA, oldCAs)
- immediately return in getExistingCA() switch
- test prependLastCA() with its own output
- simpler returns in getExistingCA()
- add logging to decodeCerts()
@tvoran tvoran merged commit b1ce2fd into master Aug 26, 2021
@tvoran tvoran deleted the VAULT-2403/keep-old-ca branch August 26, 2021 05:03
RemcoBuddelmeijer pushed a commit to RemcoBuddelmeijer/vault-k8s that referenced this pull request Feb 22, 2022
Keeps the last CA in the K8s API caBundle when generating a new
CA. Should help cover when follower replicas haven't received a new
leaf cert from the new CA.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants