Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions authentication/mtls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"`
Copy link
Copy Markdown

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.

CAs []*x509.Certificate
pathMatchers []*regexp.Regexp
}

type MTLSAuthenticator struct {
Expand Down Expand Up @@ -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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: %w for error wrapping, should give detailed regex error to user when it fails to compile (i think) i'm rusty on regex stdlib

}
config.pathMatchers = append(config.pathMatchers, matcher)
}

return MTLSAuthenticator{
tenant: tenant,
logger: logger,
Expand All @@ -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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not used outside of prom? Why do we use PrometehusAPIError

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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)
Expand Down Expand Up @@ -157,3 +192,4 @@ func (a MTLSAuthenticator) GRPCMiddleware() grpc.StreamServerInterceptor {
func (a MTLSAuthenticator) Handler() (string, http.Handler) {
return "", nil
}

Loading