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

feat: add mTLS to CNS #2751

Merged
merged 14 commits into from
Jun 8, 2024
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
1 change: 1 addition & 0 deletions cns/configuration/cns_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"TLSPort": "10091",
"TLSSubjectName": "",
"UseHTTPS": false,
"UseMTLS": false,
"WireserverIP": "168.63.129.16",
"KeyVaultSettings": {
"URL": "",
Expand Down
1 change: 1 addition & 0 deletions cns/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type CNSConfig struct {
TLSSubjectName string
TelemetrySettings TelemetrySettings
UseHTTPS bool
UseMTLS bool
jackieluc marked this conversation as resolved.
Show resolved Hide resolved
WatchPods bool `json:"-"`
WireserverIP string
}
Expand Down
1 change: 1 addition & 0 deletions cns/configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func TestReadConfigFromFile(t *testing.T) {
PopulateHomeAzCacheRetryIntervalSecs: 60,
},
UseHTTPS: true,
UseMTLS: true,
WireserverIP: "168.63.129.16",
},
wantErr: false,
Expand Down
1 change: 1 addition & 0 deletions cns/configuration/testdata/good.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"TelemetryBatchSizeBytes": 16384
},
"UseHTTPS": true,
"UseMTLS": true,
"WireserverIP": "168.63.129.16",
"AZRSettings": {
"PopulateHomeAzCacheRetryIntervalSecs": 60
Expand Down
55 changes: 55 additions & 0 deletions cns/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cns
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -190,6 +191,18 @@ func getTLSConfigFromFile(tlsSettings localtls.TlsSettings) (*tls.Config, error)
},
}

if tlsSettings.UseMTLS {
paulyufan2 marked this conversation as resolved.
Show resolved Hide resolved
rootCAs, err := mtlsRootCAsFromCertificate(&tlsCert)
if err != nil {
return nil, errors.Wrap(err, "failed to get root CAs for configuring mTLS")
}
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
tlsConfig.ClientCAs = rootCAs
tlsConfig.RootCAs = rootCAs
}

logger.Debugf("TLS configured successfully from file: %+v", tlsSettings)

return tlsConfig, nil
}

Expand Down Expand Up @@ -224,9 +237,51 @@ func getTLSConfigFromKeyVault(tlsSettings localtls.TlsSettings, errChan chan<- e
},
}

if tlsSettings.UseMTLS {
tlsCert := cr.GetCertificate()
rootCAs, err := mtlsRootCAsFromCertificate(tlsCert)
if err != nil {
return nil, errors.Wrap(err, "failed to get root CAs for configuring mTLS")
}
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert
tlsConfig.ClientCAs = rootCAs
tlsConfig.RootCAs = rootCAs
}

logger.Debugf("TLS configured successfully from KV: %+v", tlsSettings)

return &tlsConfig, nil
}

// Given a TLS cert, return the root CAs
func mtlsRootCAsFromCertificate(tlsCert *tls.Certificate) (*x509.CertPool, error) {
switch {
case tlsCert == nil || len(tlsCert.Certificate) == 0:
return nil, errors.New("no certificate provided")
case len(tlsCert.Certificate) == 1:
certs := x509.NewCertPool()
cert, err := x509.ParseCertificate(tlsCert.Certificate[0])
if err != nil {
return nil, errors.Wrap(err, "parsing self signed cert")
}
certs.AddCert(cert)

return certs, nil
default:
certs := x509.NewCertPool()
// given a fullchain cert, we skip leaf cert at index 0 because
// we only want intermediate and root certs in the cert pool for mTLS
for _, certBytes := range tlsCert.Certificate[1:] {
cert, err := x509.ParseCertificate(certBytes)
if err != nil {
return nil, errors.Wrap(err, "parsing root certs")
}
certs.AddCert(cert)
}
return certs, nil
}
}

func (service *Service) StartListener(config *common.ServiceConfig) error {
log.Debugf("[Azure CNS] Going to start listener: %+v", config)

Expand Down
1 change: 1 addition & 0 deletions cns/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ func main() {
KeyVaultCertificateName: cnsconfig.KeyVaultSettings.CertificateName,
MSIResourceID: cnsconfig.MSISettings.ResourceID,
KeyVaultCertificateRefreshInterval: time.Duration(cnsconfig.KeyVaultSettings.RefreshIntervalInHrs) * time.Hour,
UseMTLS: cnsconfig.UseMTLS,
jackieluc marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Loading
Loading