-
Notifications
You must be signed in to change notification settings - Fork 3
Support path based matching for authenticators #41
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
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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "regexp" | ||
|
|
||
| "github.com/go-kit/log" | ||
| grpc_middleware_auth "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth" | ||
|
|
@@ -28,9 +29,11 @@ func init() { | |
| } | ||
|
|
||
| type mTLSConfig struct { | ||
| RawCA []byte `json:"ca"` | ||
| CAPath string `json:"caPath"` | ||
| CAs []*x509.Certificate | ||
| RawCA []byte `json:"ca"` | ||
| CAPath string `json:"caPath"` | ||
| PathPatterns []string `json:"pathPatterns"` | ||
| CAs []*x509.Certificate | ||
| pathMatchers []*regexp.Regexp | ||
| } | ||
|
|
||
| type MTLSAuthenticator struct { | ||
|
|
@@ -83,6 +86,15 @@ func newMTLSAuthenticator(c map[string]interface{}, tenant string, registrationR | |
| config.CAs = cas | ||
| } | ||
|
|
||
| // Compile path patterns | ||
| for _, pattern := range config.PathPatterns { | ||
| matcher, err := regexp.Compile(pattern) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to compile mTLS path pattern %q: %v", pattern, err) | ||
|
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. nit: |
||
| } | ||
| config.pathMatchers = append(config.pathMatchers, matcher) | ||
| } | ||
|
|
||
| return MTLSAuthenticator{ | ||
| tenant: tenant, | ||
| logger: logger, | ||
|
|
@@ -93,6 +105,29 @@ func newMTLSAuthenticator(c map[string]interface{}, tenant string, registrationR | |
| func (a MTLSAuthenticator) Middleware() Middleware { | ||
| return func(next http.Handler) http.Handler { | ||
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| // Check if mTLS is required for this path | ||
| if len(a.config.pathMatchers) > 0 { | ||
| pathMatches := false | ||
| for _, matcher := range a.config.pathMatchers { | ||
| if matcher.MatchString(r.URL.Path) { | ||
| pathMatches = true | ||
| break | ||
| } | ||
| } | ||
|
|
||
| // If path doesn't match, skip mTLS enforcement | ||
| if !pathMatches { | ||
| next.ServeHTTP(w, r) | ||
| return | ||
| } | ||
| } | ||
|
Comment on lines
+110
to
+123
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. nit/readability: extract into helper (only used once so feel free to ignore 🤷🏽 ) pathMatches(path string) |
||
|
|
||
| // Path matches or no paths configured, enforce mTLS | ||
|
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. nit: comment could be clearer. |
||
| if r.TLS == nil { | ||
| httperr.PrometheusAPIError(w, "mTLS required but no TLS connection", http.StatusBadRequest) | ||
|
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. Is this not used outside of prom? Why do we use
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. just following the pattern for the rest of the project :) |
||
| return | ||
| } | ||
|
|
||
| caPool := x509.NewCertPool() | ||
| for _, ca := range a.config.CAs { | ||
| caPool.AddCert(ca) | ||
|
|
@@ -157,3 +192,4 @@ func (a MTLSAuthenticator) GRPCMiddleware() grpc.StreamServerInterceptor { | |
| func (a MTLSAuthenticator) Handler() (string, http.Handler) { | ||
| return "", nil | ||
| } | ||
|
|
||
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.
for later, we should document that this is case sensitive.