44package common
55
66import (
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.
1820type 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.
42105func (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 )
0 commit comments