-
Notifications
You must be signed in to change notification settings - Fork 608
fix: use certmanager if availiable otherwise certfile #5076
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
4 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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
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
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
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
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,68 @@ | ||
| package frontline | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "fmt" | ||
|
|
||
| "github.com/unkeyed/unkey/pkg/logger" | ||
| pkgtls "github.com/unkeyed/unkey/pkg/tls" | ||
| "github.com/unkeyed/unkey/svc/frontline/services/certmanager" | ||
| ) | ||
|
|
||
| // buildTlsConfig creates a TLS configuration for the frontline server. | ||
| // | ||
| // The function supports three modes: | ||
| // - Disabled: TLS is explicitly disabled via config | ||
| // - Dynamic: Certificates are fetched from Vault via the cert manager (production) | ||
| // - Static: Certificates are loaded from filesystem (development) | ||
| // | ||
| // Dynamic certificates are preferred when Vault is configured because they support | ||
| // per-domain certificates without server restarts. Static files are a fallback for | ||
| // development environments or when Vault is unavailable. | ||
| // | ||
| // Returns nil TLS config when disabled, or an error if TLS is required but no | ||
| // certificate source is configured. | ||
| func buildTlsConfig(cfg Config, certManager certmanager.Service) (*tls.Config, error) { | ||
|
|
||
| tlsDisabled := cfg.TLS != nil && cfg.TLS.Disabled | ||
|
|
||
| if tlsDisabled { | ||
| logger.Warn("TLS explicitly disabled via config") | ||
|
|
||
| return nil, nil | ||
| } | ||
|
|
||
| if cfg.TLS != nil && cfg.TLS.CertFile != "" && cfg.TLS.KeyFile != "" { | ||
| // Dev mode: static file-based certificate | ||
| logger.Info("TLS configured with static certificate files", | ||
| "certFile", cfg.TLS.CertFile, | ||
| "keyFile", cfg.TLS.KeyFile) | ||
| return pkgtls.NewFromFiles(cfg.TLS.CertFile, cfg.TLS.KeyFile) | ||
| } | ||
|
|
||
| if certManager != nil { | ||
| // Production mode: dynamic certificates from database/vault | ||
|
|
||
| logger.Info("TLS configured with dynamic certificate manager") | ||
|
|
||
| //nolint:exhaustruct | ||
| return &tls.Config{ | ||
| GetCertificate: func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) { | ||
| return certManager.GetCertificate(context.Background(), hello.ServerName) | ||
| }, | ||
| MinVersion: tls.VersionTLS12, | ||
| // Enable session resumption for faster subsequent connections | ||
| // Session tickets allow clients to skip the full TLS handshake | ||
| SessionTicketsDisabled: false, | ||
| // Let Go's TLS implementation choose optimal cipher suites | ||
| // This prefers TLS 1.3 when available (1-RTT vs 2-RTT for TLS 1.2) | ||
| PreferServerCipherSuites: false, | ||
| }, nil | ||
| } | ||
|
|
||
| return nil, fmt.Errorf("TLS is required but no certificate source configured: " + | ||
| "either enable Vault for dynamic certificates, provide [tls] cert_file and key_file, " + | ||
| "or explicitly disable TLS with [tls] disabled = true") | ||
|
|
||
| } |
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.
Stale doc comment references
TLSFiles.The comment on line 94 still references
[config.TLSFiles]but the type is nowconfig.TLS.📝 Proposed fix
🤖 Prompt for AI Agents