-
Notifications
You must be signed in to change notification settings - Fork 43
feat(starr): add mTLS client certificate support for all starr apps #556
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: main
Are you sure you want to change the base?
Changes from all commits
15cf679
54eb23b
ac7c981
f1dc7f2
4c3c607
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ package unpackerr | |||||
|
|
||||||
| import ( | ||||||
| "crypto/tls" | ||||||
| "crypto/x509" | ||||||
| "fmt" | ||||||
| "net/http" | ||||||
| "os" | ||||||
|
|
@@ -311,12 +312,60 @@ func (u *Unpackerr) validateApp(conf *StarrConfig, app starr.App) error { | |||||
| conf.Protocols = defaultProtocol | ||||||
| } | ||||||
|
|
||||||
| // Configure TLS and HTTP client | ||||||
| tlsConfig, err := u.configureTLS(conf, app) | ||||||
| if err != nil { | ||||||
| return err | ||||||
| } | ||||||
|
|
||||||
| conf.Config.Client = &http.Client{ | ||||||
| Timeout: conf.Timeout.Duration, | ||||||
| Transport: &http.Transport{ | ||||||
| TLSClientConfig: &tls.Config{InsecureSkipVerify: !conf.ValidSSL}, //nolint:gosec | ||||||
| TLSClientConfig: tlsConfig, | ||||||
| }, | ||||||
| } | ||||||
|
|
||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| // configureTLS creates and configures the TLS config for mTLS support. | ||||||
| func (u *Unpackerr) configureTLS(conf *StarrConfig, app starr.App) (*tls.Config, error) { | ||||||
| // Create TLS config - default behavior unchanged | ||||||
| tlsConfig := &tls.Config{InsecureSkipVerify: !conf.ValidSSL} //nolint:gosec | ||||||
|
|
||||||
| // Add mTLS if certificates are configured | ||||||
| if conf.TLSClientCert != "" && conf.TLSClientKey != "" { | ||||||
| certPath := expandHomedir(conf.TLSClientCert) | ||||||
| keyPath := expandHomedir(conf.TLSClientKey) | ||||||
|
|
||||||
| cert, err := tls.LoadX509KeyPair(certPath, keyPath) | ||||||
| if err != nil { | ||||||
| return nil, fmt.Errorf("%s (%s) failed loading TLS client cert from %s and %s: %w", | ||||||
| app, conf.URL, certPath, keyPath, err) | ||||||
| } | ||||||
|
|
||||||
| tlsConfig.Certificates = []tls.Certificate{cert} | ||||||
|
|
||||||
| u.Debugf("%s (%s): Loaded mTLS client certificate", app, conf.URL) | ||||||
| } | ||||||
|
|
||||||
| // Add custom CA if configured | ||||||
| if conf.TLSCACert != "" { | ||||||
| caCert, err := os.ReadFile(expandHomedir(conf.TLSCACert)) | ||||||
| if err != nil { | ||||||
| return nil, fmt.Errorf("%s (%s) failed reading CA cert from %s: %w", | ||||||
| app, conf.URL, expandHomedir(conf.TLSCACert), err) | ||||||
| } | ||||||
|
|
||||||
| caCertPool := x509.NewCertPool() | ||||||
|
Collaborator
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.
Suggested change
Can you just set this directly here? (I'm not sure if .RootCAs is an interface or not.) |
||||||
| if !caCertPool.AppendCertsFromPEM(caCert) { | ||||||
| return nil, fmt.Errorf("%w: %s (%s) from %s", ErrInvalidCA, app, conf.URL, expandHomedir(conf.TLSCACert)) | ||||||
| } | ||||||
|
|
||||||
| tlsConfig.RootCAs = caCertPool | ||||||
|
|
||||||
| u.Debugf("%s (%s): Loaded custom CA certificate", app, conf.URL) | ||||||
| } | ||||||
|
|
||||||
| return tlsConfig, nil | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -29,15 +29,19 @@ type Extract struct { | |||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Shared config items for all starr apps. | ||||||||||||||||||||||||||
| type StarrConfig struct { | ||||||||||||||||||||||||||
| Path string `json:"path" toml:"path" xml:"path" yaml:"path"` | ||||||||||||||||||||||||||
| Paths StringSlice `json:"paths" toml:"paths" xml:"paths" yaml:"paths"` | ||||||||||||||||||||||||||
| Protocols string `json:"protocols" toml:"protocols" xml:"protocols" yaml:"protocols"` | ||||||||||||||||||||||||||
| DeleteOrig bool `json:"delete_orig" toml:"delete_orig" xml:"delete_orig" yaml:"delete_orig"` | ||||||||||||||||||||||||||
| DeleteDelay cnfg.Duration `json:"delete_delay" toml:"delete_delay" xml:"delete_delay" yaml:"delete_delay"` | ||||||||||||||||||||||||||
| Syncthing bool `json:"syncthing" toml:"syncthing" xml:"syncthing" yaml:"syncthing"` | ||||||||||||||||||||||||||
| ValidSSL bool `json:"valid_ssl" toml:"valid_ssl" xml:"valid_ssl" yaml:"valid_ssl"` | ||||||||||||||||||||||||||
| Timeout cnfg.Duration `json:"timeout" toml:"timeout" xml:"timeout" yaml:"timeout"` | ||||||||||||||||||||||||||
| starr.Config | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
Collaborator
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. Extra whitespace you don't need here. |
||||||||||||||||||||||||||
| Path string `json:"path" toml:"path" xml:"path" yaml:"path"` | ||||||||||||||||||||||||||
| Paths StringSlice `json:"paths" toml:"paths" xml:"paths" yaml:"paths"` | ||||||||||||||||||||||||||
| Protocols string `json:"protocols" toml:"protocols" xml:"protocols" yaml:"protocols"` | ||||||||||||||||||||||||||
| DeleteOrig bool `json:"delete_orig" toml:"delete_orig" xml:"delete_orig" yaml:"delete_orig"` | ||||||||||||||||||||||||||
| DeleteDelay cnfg.Duration `json:"delete_delay" toml:"delete_delay" xml:"delete_delay" yaml:"delete_delay"` | ||||||||||||||||||||||||||
| Syncthing bool `json:"syncthing" toml:"syncthing" xml:"syncthing" yaml:"syncthing"` | ||||||||||||||||||||||||||
| ValidSSL bool `json:"valid_ssl" toml:"valid_ssl" xml:"valid_ssl" yaml:"valid_ssl"` | ||||||||||||||||||||||||||
| Timeout cnfg.Duration `json:"timeout" toml:"timeout" xml:"timeout" yaml:"timeout"` | ||||||||||||||||||||||||||
| TLSClientCert string `json:"tls_client_cert" toml:"tls_client_cert" xml:"tls_client_cert" yaml:"tls_client_cert"` //nolint:lll | ||||||||||||||||||||||||||
| TLSClientKey string `json:"tls_client_key" toml:"tls_client_key" xml:"tls_client_key" yaml:"tls_client_key"` | ||||||||||||||||||||||||||
| TLSCACert string `json:"tls_ca_cert" toml:"tls_ca_cert" xml:"tls_ca_cert" yaml:"tls_ca_cert"` | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+42
to
45
Collaborator
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. Can you consider this instead? You'll have to make a few other changes, but I think this will be a bit cleaner in the end.
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // checkQueueChanges checks each item for state changes from the app queues. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
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 usually do an "or" on these two variables when dealing with SSL. In case a user sets one, they'll get an error. Instead of nothing.