@@ -17,9 +17,11 @@ limitations under the License.
1717package main
1818
1919import (
20+ "crypto/tls"
2021 "fmt"
2122 "net"
2223 "net/http"
24+ "net/url"
2325 "os"
2426 "path/filepath"
2527 "time"
@@ -101,6 +103,8 @@ func main() {
101103 helmCachePurgeInterval string
102104 artifactRetentionTTL time.Duration
103105 artifactRetentionRecords int
106+ storageCertDir string
107+ storageHttpsEnabled bool
104108 )
105109
106110 flag .StringVar (& metricsAddr , "metrics-addr" , envOrDefault ("METRICS_ADDR" , ":8080" ),
@@ -112,6 +116,8 @@ func main() {
112116 "The local storage path." )
113117 flag .StringVar (& storageAddr , "storage-addr" , envOrDefault ("STORAGE_ADDR" , ":9090" ),
114118 "The address the static file server binds to." )
119+ flag .BoolVar (& storageHttpsEnabled , "storage-https-enabled" , false , "The static server serves https." )
120+ flag .StringVar (& storageCertDir , "storage-cert-path" , "" , "The path to static server certificate." )
115121 flag .StringVar (& storageAdvAddr , "storage-adv-addr" , envOrDefault ("STORAGE_ADV_ADDR" , "" ),
116122 "The advertised address of the static file server." )
117123 flag .IntVar (& concurrent , "concurrent" , 2 , "The number of concurrent reconciles per controller." )
@@ -202,6 +208,9 @@ func main() {
202208 if storageAdvAddr == "" {
203209 storageAdvAddr = determineAdvStorageAddr (storageAddr , setupLog )
204210 }
211+
212+ storageAdvAddr = appendScheme (storageAdvAddr , storageHttpsEnabled )
213+
205214 storage := mustInitStorage (storagePath , storageAdvAddr , artifactRetentionTTL , artifactRetentionRecords , setupLog )
206215
207216 if gogitOnly , _ := features .Enabled (features .ForceGoGitImplementation ); ! gogitOnly {
@@ -332,7 +341,7 @@ func main() {
332341 // to handle that.
333342 <- mgr .Elected ()
334343
335- startFileServer (storage .BasePath , storageAddr , setupLog )
344+ startFileServer (storage .BasePath , storageAddr , storageHttpsEnabled , storageCertDir , setupLog )
336345 }()
337346
338347 setupLog .Info ("starting manager" )
@@ -342,13 +351,37 @@ func main() {
342351 }
343352}
344353
345- func startFileServer (path string , address string , l logr.Logger ) {
354+ func getCertificateLoader (certDir string ) func (info * tls.ClientHelloInfo ) (* tls.Certificate , error ) {
355+ return func (_ * tls.ClientHelloInfo ) (* tls.Certificate , error ) {
356+ crt := fmt .Sprintf ("%s/%s" , certDir , "tls.crt" )
357+ key := fmt .Sprintf ("%s/%s" , certDir , "tls.key" )
358+
359+ certificate , err := tls .LoadX509KeyPair (crt , key )
360+ return & certificate , err
361+ }
362+ }
363+
364+ func startFileServer (path string , address string , enableHttpsStorage bool , certDir string , l logr.Logger ) {
346365 l .Info ("starting file server" )
347- fs := http .FileServer (http .Dir (path ))
348- http .Handle ("/" , fs )
349- err := http .ListenAndServe (address , nil )
350- if err != nil {
351- l .Error (err , "file server error" )
366+
367+ server := http.Server {
368+ Addr : address ,
369+ Handler : http .FileServer (http .Dir (path )),
370+ TLSConfig : & tls.Config {
371+ GetCertificate : getCertificateLoader (certDir ),
372+ },
373+ }
374+
375+ if enableHttpsStorage {
376+ err := server .ListenAndServeTLS ("" , "" )
377+ if err != nil {
378+ l .Error (err , "https file server error" )
379+ }
380+ } else {
381+ err := server .ListenAndServe ()
382+ if err != nil {
383+ l .Error (err , "http file server error" )
384+ }
352385 }
353386}
354387
@@ -391,6 +424,19 @@ func determineAdvStorageAddr(storageAddr string, l logr.Logger) string {
391424 return net .JoinHostPort (host , port )
392425}
393426
427+ func appendScheme (storageAdvAddr string , enableHttpsStorage bool ) string {
428+ u , err := url .Parse (storageAdvAddr )
429+ if err != nil {
430+ return storageAdvAddr
431+ }
432+
433+ u .Scheme = "http"
434+ if enableHttpsStorage {
435+ u .Scheme = "https"
436+ }
437+ return u .String ()
438+ }
439+
394440func envOrDefault (envName , defaultValue string ) string {
395441 ret := os .Getenv (envName )
396442 if ret != "" {
0 commit comments