-
Notifications
You must be signed in to change notification settings - Fork 595
enhance: Reload cert if 401 response from LAPI #4269
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,8 +24,38 @@ var ( | |
| Cert *tls.Certificate | ||
| CaCertPool *x509.CertPool | ||
| lapiClient *ApiClient | ||
|
|
||
| // CertPath and KeyPath store the paths to the client certificate and key files. | ||
| // These are used to reload the certificate when it expires and is renewed on disk. | ||
| CertPath string | ||
| KeyPath string | ||
| ) | ||
|
|
||
| // ReloadCertIfNeeded checks if mTLS is configured and reloads the client certificate | ||
| // from disk, updating the provided transport's TLS config. | ||
| // This is called when a 401 is received, as the cert may have been renewed on disk | ||
| // while the old one expired in memory. | ||
| func ReloadCertIfNeeded(transport http.RoundTripper) error { | ||
| // Only attempt reload if mTLS is configured with cert paths | ||
| if Cert == nil || CertPath == "" || KeyPath == "" { | ||
| return nil | ||
| } | ||
|
|
||
| cert, err := tls.LoadX509KeyPair(CertPath, KeyPath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to reload certificate: %w", err) | ||
| } | ||
|
|
||
| Cert = &cert | ||
|
|
||
| // Update the transport's TLS config if possible | ||
| if httpTransport, ok := transport.(*http.Transport); ok && httpTransport.TLSClientConfig != nil { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might want to breakdown into happy path EG: !ok and == nil and return context errors as it may silently pass here even though we read the cert into memory
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| httpTransport.TLSClientConfig.Certificates = []tls.Certificate{cert} | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| type TokenSave func(ctx context.Context, token string) error | ||
|
|
||
| type ApiClient struct { | ||
|
|
||
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.
@mmetc will kill me for more global vars, but atm they are held globally and scope of PR is a quick fix rather than a re implementation.