-
Notifications
You must be signed in to change notification settings - Fork 38.7k
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
Add client cert authentication #6190
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
|
@@ -23,6 +23,7 @@ import ( | |
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"encoding/pem" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"math/big" | ||
|
@@ -97,3 +98,65 @@ func GenerateSelfSignedCert(host, certPath, keyPath string) error { | |
|
||
return nil | ||
} | ||
|
||
// CertPoolFromFile returns an x509.CertPool containing the certificates in the given PEM-encoded file. | ||
// Returns an error if the file could not be read, a certificate could not be parsed, or if the file does not contain any certificates | ||
func CertPoolFromFile(filename string) (*x509.CertPool, error) { | ||
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. Right now only this function is used outside the util package. Why not restrict the visibility of the helper functions until it's necessary for outside code to call them directly? |
||
certs, err := certificatesFromFile(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
pool := x509.NewCertPool() | ||
for _, cert := range certs { | ||
pool.AddCert(cert) | ||
} | ||
return pool, nil | ||
} | ||
|
||
// certificatesFromFile returns the x509.Certificates contained in the given PEM-encoded file. | ||
// Returns an error if the file could not be read, a certificate could not be parsed, or if the file does not contain any certificates | ||
func certificatesFromFile(file string) ([]*x509.Certificate, error) { | ||
if len(file) == 0 { | ||
return nil, errors.New("error reading certificates from an empty filename") | ||
} | ||
pemBlock, err := ioutil.ReadFile(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
certs, err := certsFromPEM(pemBlock) | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading %s: %s", file, err) | ||
} | ||
return certs, nil | ||
} | ||
|
||
// certsFromPEM returns the x509.Certificates contained in the given PEM-encoded byte array | ||
// Returns an error if a certificate could not be parsed, or if the data does not contain any certificates | ||
func certsFromPEM(pemCerts []byte) ([]*x509.Certificate, error) { | ||
ok := false | ||
certs := []*x509.Certificate{} | ||
for len(pemCerts) > 0 { | ||
var block *pem.Block | ||
block, pemCerts = pem.Decode(pemCerts) | ||
if block == nil { | ||
break | ||
} | ||
// Only use PEM "CERTIFICATE" blocks without extra headers | ||
if block.Type != "CERTIFICATE" || len(block.Headers) != 0 { | ||
continue | ||
} | ||
|
||
cert, err := x509.ParseCertificate(block.Bytes) | ||
if err != nil { | ||
return certs, err | ||
} | ||
|
||
certs = append(certs, cert) | ||
ok = true | ||
} | ||
|
||
if !ok { | ||
return certs, errors.New("could not read any certificates") | ||
} | ||
return certs, nil | ||
} |
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.
What if I am accessing the apiserver via my browser and I have installed a client cert from a CA which is not in the clientCAs pool? Then I automatically get rejected. That seems wrong.
If we tried to expand the clientCAs pool to include our cluster's CA plus all known trustworthy CAs, then it would mean that anyone with a client cert from any of those CAs could authenticate to our cluster, which is also not good.
So, I think the set of allowed client CAs needs to be broader than the set of automatically-authenticating client CAs.
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.
If there is a way to set the TLSConfig to allow connections with any cert, and do all the checking in the Authenticator, then I think that is the way to go for now?
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.
You won't get rejected.
Setting
ClientAuth=RequestClientCert
means a client cert is requested, but not required or validated at the connection level.Setting
ClientCAs
tells clients what CAs are valid to present client certificates for. If your browser doesn't have any client certificates for that CA, it won't send any. LeavingClientCAs
blank means some browsers will try to send any client cert they have.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.
That's exactly what this does. See "This allows certificates to be validated by authenticators, while still allowing other auth types" comment :)
Requests without client certs, with invalid client certs, or with valid client certs are all allowed at the transport layer.
Requests without a client cert will skip the client cert Authenticator.
Requests with an "invalid" client cert will fail the client cert Authenticator, but could still get an identity from the token Authenticator.
Requests with a valid client cert will get an identity from the client cert Authenticator.