-
Notifications
You must be signed in to change notification settings - Fork 258
filtering mTLS connections based on the subject name from Caller #4081
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 | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -156,6 +156,28 @@ func getTLSConfig(tlsSettings localtls.TlsSettings, errChan chan<- error) (*tls. | |||||||||||||||||||
return nil, errors.Errorf("invalid tls settings: %+v", tlsSettings) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
// verifyPeerCertificate verifies the client certificate's subject name matches the expected subject name. | ||||||||||||||||||||
func verifyPeerCertificate(rawCerts [][]byte, clientSubjectName string) error { | ||||||||||||||||||||
if len(rawCerts) == 0 { | ||||||||||||||||||||
return errors.New("no client certificate provided") | ||||||||||||||||||||
} | ||||||||||||||||||||
// no client subject name provided, skip verification | ||||||||||||||||||||
if clientSubjectName == "" { | ||||||||||||||||||||
return nil | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
cert, err := x509.ParseCertificate(rawCerts[0]) | ||||||||||||||||||||
if err != nil { | ||||||||||||||||||||
return errors.Errorf("failed to parse certificate: %v", err) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
err = cert.VerifyHostname(clientSubjectName) | ||||||||||||||||||||
if err != nil { | ||||||||||||||||||||
return errors.Errorf("failed to verify client certificate hostname: %v", err) | ||||||||||||||||||||
} | ||||||||||||||||||||
return nil | ||||||||||||||||||||
Comment on lines
+174
to
+178
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. Using
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback 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. this one could be right 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. Yep this function is designed for server certificate validation actually. But its functionality is exactly what I want. It will validate the given strings with SANs /IPaddress of the certs. |
||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
func getTLSConfigFromFile(tlsSettings localtls.TlsSettings) (*tls.Config, error) { | ||||||||||||||||||||
tlsCertRetriever, err := localtls.GetTlsCertificateRetriever(tlsSettings) | ||||||||||||||||||||
if err != nil { | ||||||||||||||||||||
|
@@ -202,8 +224,10 @@ func getTLSConfigFromFile(tlsSettings localtls.TlsSettings) (*tls.Config, error) | |||||||||||||||||||
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert | ||||||||||||||||||||
tlsConfig.ClientCAs = rootCAs | ||||||||||||||||||||
tlsConfig.RootCAs = rootCAs | ||||||||||||||||||||
tlsConfig.VerifyPeerCertificate = func(rawCerts [][]byte, _ [][]*x509.Certificate) error { | ||||||||||||||||||||
return verifyPeerCertificate(rawCerts, tlsSettings.AllowedClientSubjectName) | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
logger.Debugf("TLS configured successfully from file: %+v", tlsSettings) | ||||||||||||||||||||
|
||||||||||||||||||||
return tlsConfig, nil | ||||||||||||||||||||
|
@@ -254,6 +278,9 @@ func getTLSConfigFromKeyVault(tlsSettings localtls.TlsSettings, errChan chan<- e | |||||||||||||||||||
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert | ||||||||||||||||||||
tlsConfig.ClientCAs = rootCAs | ||||||||||||||||||||
tlsConfig.RootCAs = rootCAs | ||||||||||||||||||||
tlsConfig.VerifyPeerCertificate = func(rawCerts [][]byte, _ [][]*x509.Certificate) error { | ||||||||||||||||||||
return verifyPeerCertificate(rawCerts, tlsSettings.AllowedClientSubjectName) | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
logger.Debugf("TLS configured successfully from KV: %+v", tlsSettings) | ||||||||||||||||||||
|
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.
No validation that
rawCerts
slice is not empty before accessingrawCerts[0]
. This could cause a panic if an empty slice is passed.Copilot uses AI. Check for mistakes.
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.
???