-
Notifications
You must be signed in to change notification settings - Fork 54
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
🌱 Add support for CA/certificate rotation #1062
Conversation
✅ Deploy Preview for olmv1 ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
This also adds a unit-test for an empty cert dir, and for an expired certificate. And also adds an e2e to make sure secret updates and rotations actually work. |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1062 +/- ##
==========================================
- Coverage 72.85% 72.67% -0.19%
==========================================
Files 31 32 +1
Lines 1864 1965 +101
==========================================
+ Hits 1358 1428 +70
- Misses 371 388 +17
- Partials 135 149 +14
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
4450298
to
03c4c85
Compare
95abebf
to
ab9e281
Compare
Mounted secrets are automatically updated into pods, but... * It doesn't work with `subPath` mountings * When `subPath` is not used, then a bunch of directories are mounted * And one of those directories is a symlink, so `IsDir()` returns false * And a watch is needed to notice the change So, update the certificate volume patch, which requires a change in how we look for certificates in the CA cert directory. Add a watch, so when the certs do change, we update the cert pool. Also look at validity dates of certificates, and error on expired certs. The default cert-manager certificates have 90 days validities. Signed-off-by: Todd Short <tshort@redhat.com>
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.
I'm still reviewing.
For these interested, here are some relevant issues in Go and K8s on root CA reloading:
TLDR: it looks like there is no equivalent of GetClientCertificate
/GetCertificate
, but for root CAs in TLS config in standard library, but it is possible to implement a workaround with VerifyPeerCertificate
. This would require setting InsecureSkipVerify
to true
and re-implementing the standard verification in VerifyPeerCertificate
as far as I understand - see this example from Go docs.
And we really don't want to do that (set |
Signed-off-by: Todd Short <tshort@redhat.com>
Setting As far as I understand in this implementation we are re-creating a client each time and will have to establish a connection each we use it which is not optimal. I don't know how significantly it will affect our performance. One one hand - creating new connections every time is not great, but on the other hand - not sure if we are going to benefit from re-using the connection in these use cases. |
Signed-off-by: Todd Short <tshort@redhat.com>
for { | ||
select { | ||
case <-watcher.Events: | ||
cpw.drainEvents() |
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.
Do you know if any events include only the directory being watched as the event.Name value? If so, I wonder if instead of performing this drainEvents action we could do some event filtering similar to https://github.com/fsnotify/fsnotify/blob/c1467c02fba575afdb5f4201072ab8403bbf00f4/cmd/fsnotify/file.go#L66-L78
I won't block the PR merging on this, but something that could make it so we don't have any "sleep" actions if it is possible
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.
Some filtering might be useful. The only time this path should be updated is when a Secret is updated. The directory is read-only within the pod.
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.
Note: the filter will not work if new files are added, as they will be filtered out. We need to recognize new files, deleted files, updated files, etc.
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.
We need to recognize new files, deleted files, updated files, etc.
If we only react to "directory has been updated" type events wouldn't we catch these events as well?
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.
But it wouldn't catch updates to files within, as that's a change to the file, not the directory.
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.
I thought the reasoning for the drain events operation was because when we receive updates we get mass events on everything when something changed. Maybe I misunderstood, which led me to thinking that if any change happened in the directory (including an individual file), it would trigger an event for the directory as well.
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.
The Watch is on the directory, and that includes the contents. It also depends on how things are mounted. A change to a file within a directory does not necessarily indicate a change to the directory.
The drain is there because the update of a single secret may trigger a number of events (I was seeing 4+, because of how the mounted files were presented), and only one reload of the certs is necessary.
Based on my testing, if there's an update on a file, it only reports the update on that file (i.e. create/update); it doesn't trigger a second update on the directory as well.
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.
I added debug output to the cert watcher test for every event in the unit test:
=== RUN TestCertPoolWatcher
certpoolwatcher_test.go:72: Create cert file at "/tmp/cert-pool4285276756/test1.pem"
certpoolwatcher_test.go:87: Create cert file at "/tmp/cert-pool4285276756/test2.pem"
Event: CREATE "/tmp/cert-pool4285276756/test2.pem"
Event: WRITE "/tmp/cert-pool4285276756/test2.pem"
--- PASS: TestCertPoolWatcher (1.11s)
So, there's an event for the create of the new PEM, and one for the write, but nothing on the directory itself. There are two events, and that would cause two reloads without the drain mechanism in place.
e3e6b03
* Add support for CA/certificate rotation Mounted secrets are automatically updated into pods, but... * It doesn't work with `subPath` mountings * When `subPath` is not used, then a bunch of directories are mounted * And one of those directories is a symlink, so `IsDir()` returns false * And a watch is needed to notice the change So, update the certificate volume patch, which requires a change in how we look for certificates in the CA cert directory. Add a watch, so when the certs do change, we update the cert pool. Also look at validity dates of certificates, and error on expired certs. The default cert-manager certificates have 90 days validities. Signed-off-by: Todd Short <tshort@redhat.com> * fixup! Add support for CA/certificate rotation * fixup! Add support for CA/certificate rotation Signed-off-by: Todd Short <tshort@redhat.com> * fixup! Add support for CA/certificate rotation Signed-off-by: Todd Short <tshort@redhat.com> --------- Signed-off-by: Todd Short <tshort@redhat.com>
* Add support for CA/certificate rotation Mounted secrets are automatically updated into pods, but... * It doesn't work with `subPath` mountings * When `subPath` is not used, then a bunch of directories are mounted * And one of those directories is a symlink, so `IsDir()` returns false * And a watch is needed to notice the change So, update the certificate volume patch, which requires a change in how we look for certificates in the CA cert directory. Add a watch, so when the certs do change, we update the cert pool. Also look at validity dates of certificates, and error on expired certs. The default cert-manager certificates have 90 days validities. Signed-off-by: Todd Short <tshort@redhat.com> * fixup! Add support for CA/certificate rotation * fixup! Add support for CA/certificate rotation Signed-off-by: Todd Short <tshort@redhat.com> * fixup! Add support for CA/certificate rotation Signed-off-by: Todd Short <tshort@redhat.com> --------- Signed-off-by: Todd Short <tshort@redhat.com>
Fixes #915
Mounted secrets are automatically updated into pods, but...
subPath
mountingssubPath
is not used, then a bunch of directories are mountedIsDir()
returns falseSo, update the certificate volume patch, which requires a change in how we look for certificates in the CA cert directory.
Add a watch, so when the certs do change, we update the cert pool.
Also look at validity dates of certificates, and error on expired certs.
The default cert-manager certificates have 90 days validities.
Description
Reviewer Checklist