mirror of https://github.com/portainer/portainer
feat(edge) EE-947 provide a way to re-associate an Edge endpoint with a new Edge key (#5413)
Co-authored-by: Simon Meng <simon.meng@portainer.io>pull/5417/head
parent
f763dcb386
commit
6b37235eb4
|
@ -1,14 +1,17 @@
|
||||||
package endpoints
|
package endpoints
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"fmt"
|
||||||
|
|
||||||
httperror "github.com/portainer/libhttp/error"
|
httperror "github.com/portainer/libhttp/error"
|
||||||
"github.com/portainer/libhttp/request"
|
"github.com/portainer/libhttp/request"
|
||||||
"github.com/portainer/libhttp/response"
|
"github.com/portainer/libhttp/response"
|
||||||
portainer "github.com/portainer/portainer/api"
|
portainer "github.com/portainer/portainer/api"
|
||||||
bolterrors "github.com/portainer/portainer/api/bolt/errors"
|
bolterrors "github.com/portainer/portainer/api/bolt/errors"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// @id EndpointAssociationDelete
|
// @id EndpointAssociationDelete
|
||||||
|
@ -45,6 +48,11 @@ func (handler *Handler) endpointAssociationDelete(w http.ResponseWriter, r *http
|
||||||
endpoint.Snapshots = []portainer.DockerSnapshot{}
|
endpoint.Snapshots = []portainer.DockerSnapshot{}
|
||||||
endpoint.Kubernetes.Snapshots = []portainer.KubernetesSnapshot{}
|
endpoint.Kubernetes.Snapshots = []portainer.KubernetesSnapshot{}
|
||||||
|
|
||||||
|
endpoint.EdgeKey, err = handler.updateEdgeKey(endpoint.EdgeKey)
|
||||||
|
if err != nil {
|
||||||
|
return &httperror.HandlerError{http.StatusInternalServerError, "Invalid EdgeKey", err}
|
||||||
|
}
|
||||||
|
|
||||||
err = handler.DataStore.Endpoint().UpdateEndpoint(portainer.EndpointID(endpointID), endpoint)
|
err = handler.DataStore.Endpoint().UpdateEndpoint(portainer.EndpointID(endpointID), endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &httperror.HandlerError{http.StatusInternalServerError, "Failed persisting endpoint in database", err}
|
return &httperror.HandlerError{http.StatusInternalServerError, "Failed persisting endpoint in database", err}
|
||||||
|
@ -54,3 +62,27 @@ func (handler *Handler) endpointAssociationDelete(w http.ResponseWriter, r *http
|
||||||
|
|
||||||
return response.JSON(w, endpoint)
|
return response.JSON(w, endpoint)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (handler *Handler) updateEdgeKey(edgeKey string) (string, error) {
|
||||||
|
oldEdgeKeyByte, err := base64.RawStdEncoding.DecodeString(edgeKey)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
oldEdgeKeyStr := string(oldEdgeKeyByte)
|
||||||
|
|
||||||
|
httpPort := getPort(handler.BindAddress)
|
||||||
|
httpsPort := getPort(handler.BindAddressHTTPS)
|
||||||
|
|
||||||
|
// replace "http://" with "https://" and replace ":9000" with ":9443", in the case of default values
|
||||||
|
// oldEdgeKeyStr example: http://10.116.1.178:9000|10.116.1.178:8000|46:99:4a:8d:a6:de:6a:bd:d8:e2:1c:99:81:60:54:55|52
|
||||||
|
r := regexp.MustCompile(fmt.Sprintf("^(http://)([^|]+)(:%s)(|.*)", httpPort))
|
||||||
|
newEdgeKeyStr := r.ReplaceAllString(oldEdgeKeyStr, fmt.Sprintf("https://$2:%s$4", httpsPort))
|
||||||
|
|
||||||
|
return base64.RawStdEncoding.EncodeToString([]byte(newEdgeKeyStr)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPort(url string) string {
|
||||||
|
items := strings.Split(url, ":")
|
||||||
|
return items[len(items) - 1]
|
||||||
|
}
|
||||||
|
|
|
@ -32,6 +32,8 @@ type Handler struct {
|
||||||
K8sClientFactory *cli.ClientFactory
|
K8sClientFactory *cli.ClientFactory
|
||||||
ComposeStackManager portainer.ComposeStackManager
|
ComposeStackManager portainer.ComposeStackManager
|
||||||
AuthorizationService *authorization.Service
|
AuthorizationService *authorization.Service
|
||||||
|
BindAddress string
|
||||||
|
BindAddressHTTPS string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHandler creates a handler to manage endpoint operations.
|
// NewHandler creates a handler to manage endpoint operations.
|
||||||
|
|
|
@ -143,6 +143,8 @@ func (server *Server) Start() error {
|
||||||
endpointHandler.ReverseTunnelService = server.ReverseTunnelService
|
endpointHandler.ReverseTunnelService = server.ReverseTunnelService
|
||||||
endpointHandler.ComposeStackManager = server.ComposeStackManager
|
endpointHandler.ComposeStackManager = server.ComposeStackManager
|
||||||
endpointHandler.AuthorizationService = server.AuthorizationService
|
endpointHandler.AuthorizationService = server.AuthorizationService
|
||||||
|
endpointHandler.BindAddress = server.BindAddress
|
||||||
|
endpointHandler.BindAddressHTTPS = server.BindAddressHTTPS
|
||||||
|
|
||||||
var endpointEdgeHandler = endpointedge.NewHandler(requestBouncer)
|
var endpointEdgeHandler = endpointedge.NewHandler(requestBouncer)
|
||||||
endpointEdgeHandler.DataStore = server.DataStore
|
endpointEdgeHandler.DataStore = server.DataStore
|
||||||
|
|
Loading…
Reference in New Issue