mirror of
https://github.com/portainer/portainer.git
synced 2025-07-23 15:29:42 +02:00
feat(server): use https by default (#5315) [EE-332]
This commit is contained in:
parent
3257cb1e28
commit
11d555bbd6
32 changed files with 1160 additions and 319 deletions
|
@ -2,6 +2,7 @@ package http
|
|||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
@ -31,6 +32,7 @@ import (
|
|||
"github.com/portainer/portainer/api/http/handler/resourcecontrols"
|
||||
"github.com/portainer/portainer/api/http/handler/roles"
|
||||
"github.com/portainer/portainer/api/http/handler/settings"
|
||||
sslhandler "github.com/portainer/portainer/api/http/handler/ssl"
|
||||
"github.com/portainer/portainer/api/http/handler/stacks"
|
||||
"github.com/portainer/portainer/api/http/handler/status"
|
||||
"github.com/portainer/portainer/api/http/handler/tags"
|
||||
|
@ -46,13 +48,16 @@ import (
|
|||
"github.com/portainer/portainer/api/http/proxy/factory/kubernetes"
|
||||
"github.com/portainer/portainer/api/http/security"
|
||||
"github.com/portainer/portainer/api/internal/authorization"
|
||||
"github.com/portainer/portainer/api/internal/ssl"
|
||||
"github.com/portainer/portainer/api/kubernetes/cli"
|
||||
)
|
||||
|
||||
// Server implements the portainer.Server interface
|
||||
type Server struct {
|
||||
AuthorizationService *authorization.Service
|
||||
AuthorizationService *authorization.Service
|
||||
BindAddress string
|
||||
BindAddressHTTPS string
|
||||
HTTPEnabled bool
|
||||
AssetsPath string
|
||||
Status *portainer.Status
|
||||
ReverseTunnelService portainer.ReverseTunnelService
|
||||
|
@ -70,9 +75,7 @@ type Server struct {
|
|||
ProxyManager *proxy.Manager
|
||||
KubernetesTokenCacheManager *kubernetes.TokenCacheManager
|
||||
Handler *handler.Handler
|
||||
SSL bool
|
||||
SSLCert string
|
||||
SSLKey string
|
||||
SSLService *ssl.Service
|
||||
DockerClientFactory *docker.ClientFactory
|
||||
KubernetesClientFactory *cli.ClientFactory
|
||||
KubernetesDeployer portainer.KubernetesDeployer
|
||||
|
@ -175,6 +178,9 @@ func (server *Server) Start() error {
|
|||
settingsHandler.LDAPService = server.LDAPService
|
||||
settingsHandler.SnapshotService = server.SnapshotService
|
||||
|
||||
var sslHandler = sslhandler.NewHandler(requestBouncer)
|
||||
sslHandler.SSLService = server.SSLService
|
||||
|
||||
var stackHandler = stacks.NewHandler(requestBouncer)
|
||||
stackHandler.DataStore = server.DataStore
|
||||
stackHandler.DockerClientFactory = server.DockerClientFactory
|
||||
|
@ -236,6 +242,7 @@ func (server *Server) Start() error {
|
|||
RegistryHandler: registryHandler,
|
||||
ResourceControlHandler: resourceControlHandler,
|
||||
SettingsHandler: settingsHandler,
|
||||
SSLHandler: sslHandler,
|
||||
StatusHandler: statusHandler,
|
||||
StackHandler: stackHandler,
|
||||
TagHandler: tagHandler,
|
||||
|
@ -248,31 +255,48 @@ func (server *Server) Start() error {
|
|||
WebhookHandler: webhookHandler,
|
||||
}
|
||||
|
||||
httpServer := &http.Server{
|
||||
Addr: server.BindAddress,
|
||||
Handler: server.Handler,
|
||||
}
|
||||
httpServer.Handler = offlineGate.WaitingMiddleware(time.Minute, httpServer.Handler)
|
||||
handler := offlineGate.WaitingMiddleware(time.Minute, server.Handler)
|
||||
|
||||
if server.SSL {
|
||||
httpServer.TLSConfig = crypto.CreateServerTLSConfiguration()
|
||||
return httpServer.ListenAndServeTLS(server.SSLCert, server.SSLKey)
|
||||
if server.HTTPEnabled {
|
||||
go func() {
|
||||
log.Printf("[INFO] [http,server] [message: starting HTTP server on port %s]", server.BindAddress)
|
||||
httpServer := &http.Server{
|
||||
Addr: server.BindAddress,
|
||||
Handler: handler,
|
||||
}
|
||||
|
||||
go shutdown(server.ShutdownCtx, httpServer)
|
||||
err := httpServer.ListenAndServe()
|
||||
if err != nil && err != http.ErrServerClosed {
|
||||
log.Printf("[ERROR] [message: http server failed] [error: %s]", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
go server.shutdown(httpServer)
|
||||
log.Printf("[INFO] [http,server] [message: starting HTTPS server on port %s]", server.BindAddressHTTPS)
|
||||
httpsServer := &http.Server{
|
||||
Addr: server.BindAddressHTTPS,
|
||||
Handler: handler,
|
||||
}
|
||||
|
||||
return httpServer.ListenAndServe()
|
||||
httpsServer.TLSConfig = crypto.CreateServerTLSConfiguration()
|
||||
httpsServer.TLSConfig.GetCertificate = func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
|
||||
return server.SSLService.GetRawCertificate(), nil
|
||||
}
|
||||
|
||||
go shutdown(server.ShutdownCtx, httpsServer)
|
||||
return httpsServer.ListenAndServeTLS("", "")
|
||||
}
|
||||
|
||||
func (server *Server) shutdown(httpServer *http.Server) {
|
||||
<-server.ShutdownCtx.Done()
|
||||
func shutdown(shutdownCtx context.Context, httpServer *http.Server) {
|
||||
<-shutdownCtx.Done()
|
||||
|
||||
log.Println("[DEBUG] Shutting down http server")
|
||||
log.Println("[DEBUG] [http,server] [message: shutting down http server]")
|
||||
shutdownTimeout, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := httpServer.Shutdown(shutdownTimeout)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed shutdown http server: %s \n", err)
|
||||
fmt.Printf("[ERROR] [http,server] [message: failed shutdown http server] [error: %s]", err)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue