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

Feature request : Implement ability to dynamically reload TLS certificates #856

Open
cdoucy opened this issue Sep 18, 2024 · 4 comments · May be fixed by #866
Open

Feature request : Implement ability to dynamically reload TLS certificates #856

cdoucy opened this issue Sep 18, 2024 · 4 comments · May be fixed by #866

Comments

@cdoucy
Copy link

cdoucy commented Sep 18, 2024

Description

client.SetRootCertificate and client.SetClientRootCertificate allow to configure certificates for TLS communication.
It would be great if it was possible to dynamically reload certificates when they expire to automatically handle certificate rotation.

Use-case

Assume there are multiple HTTPS services in a Kubernetes cluster. These HTTPS services are using Certificates issued by cert-manager.
Therefore, pods who host HTTPS client need to be mounted with the root certificate. When cert-manager rotate the Certificates, the running HTTPS clients need to reload the mounted certificate.

To work around it, it's possible to either panic on TLS errors to restart the pod and reload the cert, or to create a fresh new resty Client for each HTTPS call, but that's not very efficient.

Possible implementations

I see two ways to implement this feature:

  1. Watch for file system events and reload the certificate when the file changes

fsnotify is a cross-platform library that allows to watch file system events. I believe it would be a good fit for the use-case.
However, I'm not sure if fsnotify can be used in resty due to legal reasons. (fsnotify is BSD licensed while resty is MIT)
Implementing a cross-platform module to watch FS system would require some engineering efforts.

  1. Poll periodically certificate file mod time and reload it when it changes

A routine could periodically call os.Stat() on the file to check the certificate file modification time.
This approach would be straightforward to implement but is not as efficient as the previous option.

@cdoucy
Copy link
Author

cdoucy commented Sep 18, 2024

Hello @jeevatkm !
I would happy to start contributing to resty by addressing this issue.

@jeevatkm
Copy link
Member

jeevatkm commented Sep 19, 2024

Hello @cdoucy - Thanks for reaching out and proposing a feature request. I agree; it is a helpful feature for Resty users.

I suggest a change in the implementation. What do you think of the following approach?

Add functionality with user-provided time.Duration and certs file paths as input, and Resty will reload the certificates when the timer hits by checking os.Stat instead of polling.

Please feel free to create a PR.

PS: Resty v2 Client settings modification is not set thread-safe 100%. It should be all right if there are no concurrent certificate modifications; it is just not immune to it yet. The upcoming Resty v3 Client comes with sync.RWMutex to manage settings on client instances, which is 100% thread-safe.

@jeevatkm
Copy link
Member

@cdoucy I edited the above suggestion with input parameter details.

@cdoucy
Copy link
Author

cdoucy commented Sep 20, 2024

Hi @jeevatkm ! Thanks for your feedback!

I suggest a change in the implementation. What do you think of the following approach?

Add functionality with user-provided time.Duration and certs file paths as input, and Resty will reload the certificates when the timer hits by checking os.Stat instead of polling.

Agree, actually that's pretty much what I had in mind as well!

Please feel free to create a PR.

Great, will create a draft PR by Sunday.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants