mirror of
https://github.com/portainer/portainer.git
synced 2025-07-24 15:59:41 +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
29
api/http/handler/ssl/handler.go
Normal file
29
api/http/handler/ssl/handler.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package ssl
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
httperror "github.com/portainer/libhttp/error"
|
||||
"github.com/portainer/portainer/api/http/security"
|
||||
"github.com/portainer/portainer/api/internal/ssl"
|
||||
)
|
||||
|
||||
// Handler is the HTTP handler used to handle MOTD operations.
|
||||
type Handler struct {
|
||||
*mux.Router
|
||||
SSLService *ssl.Service
|
||||
}
|
||||
|
||||
// NewHandler returns a new Handler
|
||||
func NewHandler(bouncer *security.RequestBouncer) *Handler {
|
||||
h := &Handler{
|
||||
Router: mux.NewRouter(),
|
||||
}
|
||||
h.Handle("/ssl",
|
||||
bouncer.AdminAccess(httperror.LoggerHandler(h.sslInspect))).Methods(http.MethodGet)
|
||||
h.Handle("/ssl",
|
||||
bouncer.AdminAccess(httperror.LoggerHandler(h.sslUpdate))).Methods(http.MethodPut)
|
||||
|
||||
return h
|
||||
}
|
29
api/http/handler/ssl/ssl_inspect.go
Normal file
29
api/http/handler/ssl/ssl_inspect.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package ssl
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
httperror "github.com/portainer/libhttp/error"
|
||||
"github.com/portainer/libhttp/response"
|
||||
)
|
||||
|
||||
// @id SSLInspect
|
||||
// @summary Inspect the ssl settings
|
||||
// @description Retrieve the ssl settings.
|
||||
// @description **Access policy**: administrator
|
||||
// @tags ssl
|
||||
// @security jwt
|
||||
// @produce json
|
||||
// @success 200 {object} portainer.SSLSettings "Success"
|
||||
// @failure 400 "Invalid request"
|
||||
// @failure 403 "Permission denied to access settings"
|
||||
// @failure 500 "Server error"
|
||||
// @router /ssl [get]
|
||||
func (handler *Handler) sslInspect(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
||||
settings, err := handler.SSLService.GetSSLSettings()
|
||||
if err != nil {
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Failed to fetch certificate info", err}
|
||||
}
|
||||
|
||||
return response.JSON(w, settings)
|
||||
}
|
62
api/http/handler/ssl/ssl_update.go
Normal file
62
api/http/handler/ssl/ssl_update.go
Normal file
|
@ -0,0 +1,62 @@
|
|||
package ssl
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
httperror "github.com/portainer/libhttp/error"
|
||||
"github.com/portainer/libhttp/request"
|
||||
"github.com/portainer/libhttp/response"
|
||||
)
|
||||
|
||||
type sslUpdatePayload struct {
|
||||
Cert *string
|
||||
Key *string
|
||||
HTTPEnabled *bool
|
||||
}
|
||||
|
||||
func (payload *sslUpdatePayload) Validate(r *http.Request) error {
|
||||
if (payload.Cert == nil || payload.Key == nil) && payload.Cert != payload.Key {
|
||||
return errors.New("both certificate and key files should be provided")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// @id SSLUpdate
|
||||
// @summary Update the ssl settings
|
||||
// @description Update the ssl settings.
|
||||
// @description **Access policy**: administrator
|
||||
// @tags ssl
|
||||
// @security jwt
|
||||
// @accept json
|
||||
// @produce json
|
||||
// @param body body sslUpdatePayload true "SSL Settings"
|
||||
// @success 204 "Success"
|
||||
// @failure 400 "Invalid request"
|
||||
// @failure 403 "Permission denied to access settings"
|
||||
// @failure 500 "Server error"
|
||||
// @router /ssl [put]
|
||||
func (handler *Handler) sslUpdate(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
||||
var payload sslUpdatePayload
|
||||
err := request.DecodeAndValidateJSONPayload(r, &payload)
|
||||
if err != nil {
|
||||
return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err}
|
||||
}
|
||||
|
||||
if payload.Cert != nil {
|
||||
err = handler.SSLService.SetCertificates([]byte(*payload.Cert), []byte(*payload.Key))
|
||||
if err != nil {
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Failed to save certificate", err}
|
||||
}
|
||||
}
|
||||
|
||||
if payload.HTTPEnabled != nil {
|
||||
err = handler.SSLService.SetHTTPEnabled(*payload.HTTPEnabled)
|
||||
if err != nil {
|
||||
return &httperror.HandlerError{http.StatusInternalServerError, "Failed to force https", err}
|
||||
}
|
||||
}
|
||||
|
||||
return response.Empty(w)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue