Skip to content

Commit 13677e6

Browse files
authored
changes to support http and https for CNS (#717)
* changes to support http and https for CNS * correct small typo * address feedback * address feedback * rename file * small fix * small fix * fixing issue with cert not found
1 parent e9741df commit 13677e6

36 files changed

+3898
-16
lines changed

cns/common/service.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package common
55

66
import (
77
"errors"
8+
"github.com/Azure/azure-container-networking/server/tls"
89

910
"github.com/Azure/azure-container-networking/cns/logger"
1011
acn "github.com/Azure/azure-container-networking/common"
@@ -37,6 +38,7 @@ type ServiceConfig struct {
3738
ErrChan chan error
3839
Store store.KeyValueStore
3940
ChannelMode string
41+
TlsSettings tls.TlsSettings
4042
}
4143

4244
// NewService creates a new Service object.

cns/configuration/cns_config.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@
1414
"NodeID": "",
1515
"NodeSyncIntervalInSeconds": 30
1616
},
17-
"ChannelMode": "Direct"
17+
"ChannelMode": "Direct",
18+
"UseHTTPS" : false,
19+
"TLSSubjectName" : "",
20+
"TLSCertificatePath" : "",
21+
"TLSEndpoint" : "localhost:10091"
1822
}

cns/configuration/configuration.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ const (
1717
)
1818

1919
type CNSConfig struct {
20-
TelemetrySettings TelemetrySettings
21-
ManagedSettings ManagedSettings
22-
ChannelMode string
20+
TelemetrySettings TelemetrySettings
21+
ManagedSettings ManagedSettings
22+
ChannelMode string
23+
UseHTTPS bool
24+
TLSSubjectName string
25+
TLSCertificatePath string
26+
TLSEndpoint string
2327
}
2428

2529
type TelemetrySettings struct {

cns/service.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,23 @@ func (service *Service) Initialize(config *common.ServiceConfig) error {
6363
if err != nil {
6464
return err
6565
}
66-
6766
// Create the listener.
6867
listener, err := acn.NewListener(u)
6968
if err != nil {
7069
return err
7170
}
72-
71+
if config.TlsSettings.TLSEndpoint != "" {
72+
// Start the listener and HTTP and HTTPS server.
73+
if err = listener.StartTLS(config.ErrChan, config.TlsSettings); err != nil {
74+
return err
75+
}
76+
}
7377
// Start the listener.
74-
err = listener.Start(config.ErrChan)
75-
if err != nil {
78+
// continue to listen on the normal endpoint for http traffic, this will be supported
79+
// for sometime until partners migrate fully to https
80+
if err = listener.Start(config.ErrChan); err != nil {
7681
return err
7782
}
78-
7983
config.Listener = listener
8084
}
8185

cns/service/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"encoding/json"
1010
"fmt"
11+
localtls "github.com/Azure/azure-container-networking/server/tls"
1112
"net/http"
1213
"os"
1314
"os/signal"
@@ -424,6 +425,14 @@ func main() {
424425

425426
// Start CNS.
426427
if httpRestService != nil {
428+
if cnsconfig.UseHTTPS {
429+
config.TlsSettings = localtls.TlsSettings{
430+
TLSSubjectName: cnsconfig.TLSSubjectName,
431+
TLSCertificatePath: cnsconfig.TLSCertificatePath,
432+
TLSEndpoint: cnsconfig.TLSEndpoint,
433+
}
434+
}
435+
427436
err = httpRestService.Start(&config)
428437
if err != nil {
429438
logger.Errorf("Failed to start CNS, err:%v.\n", err)

common/listener.go

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
package common
55

66
import (
7+
"crypto/tls"
78
"encoding/json"
89
"fmt"
10+
localtls "github.com/Azure/azure-container-networking/server/tls"
911
"net"
1012
"net/http"
1113
"net/url"
@@ -16,13 +18,14 @@ import (
1618

1719
// Listener represents an HTTP listener.
1820
type Listener struct {
19-
URL *url.URL
20-
protocol string
21-
localAddress string
22-
endpoints []string
23-
active bool
24-
l net.Listener
25-
mux *http.ServeMux
21+
URL *url.URL
22+
protocol string
23+
localAddress string
24+
endpoints []string
25+
active bool
26+
l net.Listener
27+
securelistener net.Listener
28+
mux *http.ServeMux
2629
}
2730

2831
// NewListener creates a new Listener.
@@ -38,6 +41,66 @@ func NewListener(u *url.URL) (*Listener, error) {
3841
return &listener, nil
3942
}
4043

44+
func GetTlsConfig(tlsSettings localtls.TlsSettings) (*tls.Config, error) {
45+
tlsCertRetriever, err := localtls.GetTlsCertificateRetriever(tlsSettings)
46+
if err != nil {
47+
return nil, fmt.Errorf("Failed to get certificate retriever %+v", err)
48+
}
49+
leafCertificate, err := tlsCertRetriever.GetCertificate()
50+
if err != nil {
51+
return nil, fmt.Errorf("Failed to get certificate %+v", err)
52+
}
53+
if leafCertificate == nil {
54+
return nil, fmt.Errorf("Certificate retrival returned empty %+v", err)
55+
}
56+
privateKey, err := tlsCertRetriever.GetPrivateKey()
57+
if err != nil {
58+
return nil, fmt.Errorf("Failed to get certificate private key %+v", err)
59+
}
60+
tlsCert := tls.Certificate{
61+
Certificate: [][]byte{leafCertificate.Raw},
62+
PrivateKey: privateKey,
63+
Leaf: leafCertificate,
64+
}
65+
tlsConfig := &tls.Config{
66+
MaxVersion: tls.VersionTLS12,
67+
MinVersion: tls.VersionTLS12,
68+
Certificates: []tls.Certificate{
69+
tlsCert,
70+
},
71+
}
72+
return tlsConfig, nil
73+
}
74+
75+
// Start creates the listener socket and starts the HTTPS server.
76+
func (listener *Listener) StartTLS(errChan chan error, tlsSettings localtls.TlsSettings) error {
77+
tlsConfig, err := GetTlsConfig(tlsSettings)
78+
if err != nil {
79+
log.Printf("[Listener] Failed to compose Tls Configuration with errror: %+v", err)
80+
return err
81+
}
82+
server := http.Server{
83+
TLSConfig: tlsConfig,
84+
Handler: listener.mux,
85+
}
86+
87+
// listen on a seperate endpoint for secure tls connections
88+
listener.securelistener, err = net.Listen(listener.protocol, tlsSettings.TLSEndpoint)
89+
if err != nil {
90+
log.Printf("[Listener] Failed to listen on TlsEndpoint: %+v", err)
91+
return err
92+
}
93+
log.Printf("[Listener] Started listening on tls endpoint %s.", tlsSettings.TLSEndpoint)
94+
95+
// Launch goroutine for servicing https requests
96+
go func() {
97+
errChan <- server.ServeTLS(listener.securelistener, "", "")
98+
}()
99+
100+
listener.active = true
101+
return nil
102+
}
103+
41104
// Start creates the listener socket and starts the HTTP server.
42105
func (listener *Listener) Start(errChan chan error) error {
43106
var err error
@@ -75,6 +138,11 @@ func (listener *Listener) Stop() {
75138
// Stop servicing requests.
76139
listener.l.Close()
77140

141+
if listener.securelistener != nil {
142+
// Stop servicing requests on secure listener
143+
listener.securelistener.Close()
144+
}
145+
78146
// Delete the unix socket.
79147
if listener.protocol == "unix" {
80148
os.Remove(listener.localAddress)

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ require (
2727
github.com/spf13/pflag v1.0.5
2828
github.com/spf13/viper v1.3.2
2929
go.opencensus.io v0.22.2 // indirect
30+
golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975
3031
golang.org/x/net v0.0.0-20191112182307-2180aed22343 // indirect
3132
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e
3233
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
@@ -36,4 +37,5 @@ require (
3637
k8s.io/apimachinery v0.18.2
3738
k8s.io/client-go v0.18.2
3839
sigs.k8s.io/controller-runtime v0.6.0
40+
software.sslmate.com/src/go-pkcs12 v0.0.0-20201102150903-66718f75db0e
3941
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,3 +630,5 @@ sigs.k8s.io/structured-merge-diff/v4 v4.0.1/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK
630630
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
631631
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
632632
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
633+
software.sslmate.com/src/go-pkcs12 v0.0.0-20201102150903-66718f75db0e h1:GP6k9CR+zxSHXs+agkNmd7ucZ/YuPY8vRRaKMGIFuWE=
634+
software.sslmate.com/src/go-pkcs12 v0.0.0-20201102150903-66718f75db0e/go.mod h1:/xvNRWUqm0+/ZMiF4EX00vrSCMsE4/NHb+Pt3freEeQ=

0 commit comments

Comments
 (0)