Skip to content
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

Provide the client certificate without relying on golang matching it #152

Merged
merged 1 commit into from
Oct 14, 2024
Merged
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
18 changes: 16 additions & 2 deletions pkg/fftls/fftls.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,34 @@ func NewTLSConfig(ctx context.Context, config *Config, tlsType TLSType) (*tls.Co

tlsConfig.RootCAs = rootCAs

var configuredCert *tls.Certificate
// For mTLS we need both the cert and key
if config.CertFile != "" && config.KeyFile != "" {
// Read the key pair to create certificate
cert, err := tls.LoadX509KeyPair(config.CertFile, config.KeyFile)
if err != nil {
return nil, i18n.WrapError(ctx, err, i18n.MsgInvalidKeyPairFiles)
}
tlsConfig.Certificates = []tls.Certificate{cert}
configuredCert = &cert
} else if config.Cert != "" && config.Key != "" {
cert, err := tls.X509KeyPair([]byte(config.Cert), []byte(config.Key))
if err != nil {
return nil, i18n.WrapError(ctx, err, i18n.MsgInvalidKeyPairFiles)
}
tlsConfig.Certificates = []tls.Certificate{cert}
configuredCert = &cert
}

if configuredCert != nil {
// Rather than letting Golang pick a certificate it thinks matches from the list of one,
// we directly supply it the one we have in all cases.
tlsConfig.GetClientCertificate = func(_ *tls.CertificateRequestInfo) (*tls.Certificate, error) {
log.L(ctx).Debugf("Supplying client certificate")
return configuredCert, nil
}
tlsConfig.GetCertificate = func(_ *tls.ClientHelloInfo) (*tls.Certificate, error) {
log.L(ctx).Debugf("Supplying server certificate")
return configuredCert, nil
}
}

if tlsType == ServerType {
Expand Down
Loading