2018-06-11 13:13:19 +00:00
|
|
|
package websocket
|
2016-08-03 03:11:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"time"
|
2016-12-25 20:34:02 +00:00
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/errors"
|
|
|
|
|
2018-06-18 09:56:31 +00:00
|
|
|
"github.com/asaskevich/govalidator"
|
2018-05-06 07:15:57 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2018-09-10 10:01:38 +00:00
|
|
|
httperror "github.com/portainer/libhttp/error"
|
|
|
|
"github.com/portainer/libhttp/request"
|
2021-02-23 03:21:39 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2016-08-03 03:11:09 +00:00
|
|
|
)
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
type execStartOperationPayload struct {
|
|
|
|
Tty bool
|
|
|
|
Detach bool
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
// @summary Execute a websocket
|
|
|
|
// @description If the nodeName query parameter is present, the request will be proxied to the underlying agent endpoint.
|
|
|
|
// @description If the nodeName query parameter is not specified, the request will be upgraded to the websocket protocol and
|
|
|
|
// @description an ExecStart operation HTTP request will be created and hijacked.
|
|
|
|
// @description Authentication and access is controlled via the mandatory token query parameter.
|
|
|
|
// @security jwt
|
|
|
|
// @tags websocket
|
|
|
|
// @accept json
|
|
|
|
// @produce json
|
|
|
|
// @param endpointId query int true "endpoint ID of the endpoint where the resource is located"
|
|
|
|
// @param nodeName query string false "node name"
|
|
|
|
// @param token query string true "JWT token used for authentication against this endpoint"
|
|
|
|
// @success 200
|
|
|
|
// @failure 400
|
|
|
|
// @failure 409
|
|
|
|
// @failure 500
|
|
|
|
// @router /websocket/exec [get]
|
2018-06-11 13:13:19 +00:00
|
|
|
func (handler *Handler) websocketExec(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
execID, err := request.RetrieveQueryParameter(r, "id", false)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: id", err}
|
2017-03-12 16:24:15 +00:00
|
|
|
}
|
2018-06-18 09:56:31 +00:00
|
|
|
if !govalidator.IsHexadecimal(execID) {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: id (must be hexadecimal identifier)", err}
|
|
|
|
}
|
2017-03-12 16:24:15 +00:00
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
endpointID, err := request.RetrieveNumericQueryParameter(r, "endpointId", false)
|
2017-03-12 16:24:15 +00:00
|
|
|
if err != nil {
|
2018-06-11 13:13:19 +00:00
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: endpointId", err}
|
2017-03-12 16:24:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID))
|
2020-07-07 21:57:52 +00:00
|
|
|
if err == errors.ErrObjectNotFound {
|
2018-06-11 13:13:19 +00:00
|
|
|
return &httperror.HandlerError{http.StatusNotFound, "Unable to find the endpoint associated to the stack inside the database", err}
|
|
|
|
} else if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find the endpoint associated to the stack inside the database", err}
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 05:41:37 +00:00
|
|
|
err = handler.requestBouncer.AuthorizedEndpointOperation(r, endpoint)
|
2018-06-18 09:56:31 +00:00
|
|
|
if err != nil {
|
2019-05-24 06:04:58 +00:00
|
|
|
return &httperror.HandlerError{http.StatusForbidden, "Permission denied to access endpoint", err}
|
2018-06-18 09:56:31 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 02:04:40 +00:00
|
|
|
params := &webSocketRequestParams{
|
2018-05-06 07:15:57 +00:00
|
|
|
endpoint: endpoint,
|
2019-05-09 02:04:40 +00:00
|
|
|
ID: execID,
|
2018-05-06 07:15:57 +00:00
|
|
|
nodeName: r.FormValue("nodeName"),
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 02:04:40 +00:00
|
|
|
err = handler.handleExecRequest(w, r, params)
|
2018-05-06 07:15:57 +00:00
|
|
|
if err != nil {
|
2018-06-11 13:13:19 +00:00
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "An error occured during websocket exec operation", err}
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
2018-06-11 13:13:19 +00:00
|
|
|
|
|
|
|
return nil
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-09 02:04:40 +00:00
|
|
|
func (handler *Handler) handleExecRequest(w http.ResponseWriter, r *http.Request, params *webSocketRequestParams) error {
|
2018-05-16 07:13:46 +00:00
|
|
|
r.Header.Del("Origin")
|
|
|
|
|
2019-07-25 22:38:07 +00:00
|
|
|
if params.endpoint.Type == portainer.AgentOnDockerEnvironment {
|
|
|
|
return handler.proxyAgentWebsocketRequest(w, r, params)
|
2020-07-05 23:21:03 +00:00
|
|
|
} else if params.endpoint.Type == portainer.EdgeAgentOnDockerEnvironment {
|
2019-07-25 22:38:07 +00:00
|
|
|
return handler.proxyEdgeAgentWebsocketRequest(w, r, params)
|
2016-08-03 03:11:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-06 07:15:57 +00:00
|
|
|
websocketConn, err := handler.connectionUpgrader.Upgrade(w, r, nil)
|
2016-08-03 03:11:09 +00:00
|
|
|
if err != nil {
|
2018-05-06 07:15:57 +00:00
|
|
|
return err
|
2016-08-03 03:11:09 +00:00
|
|
|
}
|
2018-05-06 07:15:57 +00:00
|
|
|
defer websocketConn.Close()
|
2016-08-03 03:11:09 +00:00
|
|
|
|
2019-05-09 02:04:40 +00:00
|
|
|
return hijackExecStartOperation(websocketConn, params.endpoint, params.ID)
|
2018-05-06 07:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func hijackExecStartOperation(websocketConn *websocket.Conn, endpoint *portainer.Endpoint, execID string) error {
|
2018-07-23 14:49:04 +00:00
|
|
|
dial, err := initDial(endpoint)
|
2018-05-06 07:15:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-08-03 03:11:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// When we set up a TCP connection for hijack, there could be long periods
|
|
|
|
// of inactivity (a long running command with no output) that in certain
|
|
|
|
// network setups may cause ECONNTIMEOUT, leaving the client in an unknown
|
|
|
|
// state. Setting TCP KeepAlive on the socket connection will prohibit
|
|
|
|
// ECONNTIMEOUT unless the socket connection truly is broken
|
|
|
|
if tcpConn, ok := dial.(*net.TCPConn); ok {
|
|
|
|
tcpConn.SetKeepAlive(true)
|
|
|
|
tcpConn.SetKeepAlivePeriod(30 * time.Second)
|
|
|
|
}
|
2018-05-06 07:15:57 +00:00
|
|
|
|
|
|
|
httpConn := httputil.NewClientConn(dial, nil)
|
|
|
|
defer httpConn.Close()
|
|
|
|
|
|
|
|
execStartRequest, err := createExecStartRequest(execID)
|
2016-08-03 03:11:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-06 07:15:57 +00:00
|
|
|
err = hijackRequest(websocketConn, httpConn, execStartRequest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-08-03 03:11:09 +00:00
|
|
|
|
2018-05-06 07:15:57 +00:00
|
|
|
func createExecStartRequest(execID string) (*http.Request, error) {
|
|
|
|
execStartOperationPayload := &execStartOperationPayload{
|
|
|
|
Tty: true,
|
|
|
|
Detach: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
encodedBody := bytes.NewBuffer(nil)
|
|
|
|
err := json.NewEncoder(encodedBody).Encode(execStartOperationPayload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-08-03 03:11:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-06 07:15:57 +00:00
|
|
|
request, err := http.NewRequest("POST", "/exec/"+execID+"/start", encodedBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
request.Header.Set("Connection", "Upgrade")
|
|
|
|
request.Header.Set("Upgrade", "tcp")
|
|
|
|
|
|
|
|
return request, nil
|
|
|
|
}
|