2022-04-07 12:17:36 +00:00
|
|
|
package endpointedge
|
2019-07-25 22:38:07 +00:00
|
|
|
|
|
|
|
import (
|
2023-01-06 19:25:41 +00:00
|
|
|
"bytes"
|
2020-06-25 03:25:51 +00:00
|
|
|
"encoding/base64"
|
2020-08-04 00:44:17 +00:00
|
|
|
"errors"
|
2022-04-07 12:17:36 +00:00
|
|
|
"fmt"
|
2023-01-06 19:25:41 +00:00
|
|
|
"hash/fnv"
|
|
|
|
"io"
|
2019-07-25 22:38:07 +00:00
|
|
|
"net/http"
|
2023-01-06 19:25:41 +00:00
|
|
|
"net/http/httptest"
|
2020-08-04 00:44:17 +00:00
|
|
|
"strconv"
|
2023-01-06 19:25:41 +00:00
|
|
|
"strings"
|
2021-03-01 00:43:47 +00:00
|
|
|
"time"
|
2019-07-25 22:38:07 +00:00
|
|
|
|
|
|
|
httperror "github.com/portainer/libhttp/error"
|
2023-01-06 19:25:41 +00:00
|
|
|
"github.com/portainer/libhttp/request"
|
2019-07-25 22:38:07 +00:00
|
|
|
"github.com/portainer/libhttp/response"
|
2021-02-23 03:21:39 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2023-01-06 19:25:41 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/edge/cache"
|
2019-07-25 22:38:07 +00:00
|
|
|
)
|
|
|
|
|
2020-05-14 02:14:28 +00:00
|
|
|
type stackStatusResponse struct {
|
2021-02-23 03:21:39 +00:00
|
|
|
// EdgeStack Identifier
|
|
|
|
ID portainer.EdgeStackID `example:"1"`
|
|
|
|
// Version of this stack
|
|
|
|
Version int `example:"3"`
|
2020-05-14 02:14:28 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 03:25:51 +00:00
|
|
|
type edgeJobResponse struct {
|
2021-02-23 03:21:39 +00:00
|
|
|
// EdgeJob Identifier
|
|
|
|
ID portainer.EdgeJobID `json:"Id" example:"2"`
|
|
|
|
// Whether to collect logs
|
|
|
|
CollectLogs bool `json:"CollectLogs" example:"true"`
|
|
|
|
// A cron expression to schedule this job
|
|
|
|
CronExpression string `json:"CronExpression" example:"* * * * *"`
|
|
|
|
// Script to run
|
|
|
|
Script string `json:"Script" example:"echo hello"`
|
|
|
|
// Version of this EdgeJob
|
|
|
|
Version int `json:"Version" example:"2"`
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 12:17:36 +00:00
|
|
|
type endpointEdgeStatusInspectResponse struct {
|
2021-09-20 00:14:22 +00:00
|
|
|
// Status represents the environment(endpoint) status
|
2021-02-23 03:21:39 +00:00
|
|
|
Status string `json:"status" example:"REQUIRED"`
|
|
|
|
// The tunnel port
|
|
|
|
Port int `json:"port" example:"8732"`
|
2021-09-20 00:14:22 +00:00
|
|
|
// List of requests for jobs to run on the environment(endpoint)
|
2021-02-23 03:21:39 +00:00
|
|
|
Schedules []edgeJobResponse `json:"schedules"`
|
|
|
|
// The current value of CheckinInterval
|
|
|
|
CheckinInterval int `json:"checkin" example:"5"`
|
|
|
|
//
|
2022-04-07 12:17:36 +00:00
|
|
|
Credentials string `json:"credentials"`
|
2021-09-20 00:14:22 +00:00
|
|
|
// List of stacks to be deployed on the environments(endpoints)
|
2021-02-23 03:21:39 +00:00
|
|
|
Stacks []stackStatusResponse `json:"stacks"`
|
2019-07-25 22:38:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 12:17:36 +00:00
|
|
|
// @id EndpointEdgeStatusInspect
|
2021-09-20 00:14:22 +00:00
|
|
|
// @summary Get environment(endpoint) status
|
2022-04-07 12:17:36 +00:00
|
|
|
// @description environment(endpoint) for edge agent to check status of environment(endpoint)
|
2021-09-20 00:14:22 +00:00
|
|
|
// @description **Access policy**: restricted only to Edge environments(endpoints)
|
2021-02-23 03:21:39 +00:00
|
|
|
// @tags endpoints
|
2021-11-30 02:31:16 +00:00
|
|
|
// @security ApiKeyAuth
|
2021-02-23 03:21:39 +00:00
|
|
|
// @security jwt
|
2021-09-20 00:14:22 +00:00
|
|
|
// @param id path int true "Environment(Endpoint) identifier"
|
2022-04-07 12:17:36 +00:00
|
|
|
// @success 200 {object} endpointEdgeStatusInspectResponse "Success"
|
2021-02-23 03:21:39 +00:00
|
|
|
// @failure 400 "Invalid request"
|
2021-09-20 00:14:22 +00:00
|
|
|
// @failure 403 "Permission denied to access environment(endpoint)"
|
|
|
|
// @failure 404 "Environment(Endpoint) not found"
|
2021-02-23 03:21:39 +00:00
|
|
|
// @failure 500 "Server error"
|
2022-04-07 12:17:36 +00:00
|
|
|
// @router /endpoints/{id}/edge/status [get]
|
|
|
|
func (handler *Handler) endpointEdgeStatusInspect(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
2023-01-06 19:25:41 +00:00
|
|
|
endpointID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
2019-07-25 22:38:07 +00:00
|
|
|
if err != nil {
|
2023-01-06 19:25:41 +00:00
|
|
|
return httperror.BadRequest("Invalid environment identifier route variable", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cachedResp := handler.respondFromCache(w, r, portainer.EndpointID(endpointID))
|
|
|
|
if cachedResp {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := handler.DataStore.Endpoint().Heartbeat(portainer.EndpointID(endpointID)); !ok {
|
|
|
|
return httperror.NotFound("Unable to find an environment with the specified identifier inside the database", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID))
|
|
|
|
if err != nil {
|
|
|
|
if handler.DataStore.IsErrObjectNotFound(err) {
|
|
|
|
return httperror.NotFound("Unable to find an environment with the specified identifier inside the database", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return httperror.InternalServerError("Unable to find an environment with the specified identifier inside the database", err)
|
2019-07-25 22:38:07 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 02:14:28 +00:00
|
|
|
err = handler.requestBouncer.AuthorizedEdgeEndpointOperation(r, endpoint)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.Forbidden("Permission denied to access environment", err)
|
2019-07-25 22:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if endpoint.EdgeID == "" {
|
2020-05-14 02:14:28 +00:00
|
|
|
edgeIdentifier := r.Header.Get(portainer.PortainerAgentEdgeIDHeader)
|
2019-07-25 22:38:07 +00:00
|
|
|
endpoint.EdgeID = edgeIdentifier
|
2022-08-12 01:21:56 +00:00
|
|
|
}
|
2019-07-25 22:38:07 +00:00
|
|
|
|
2022-08-12 01:21:56 +00:00
|
|
|
agentPlatform, agentPlatformErr := parseAgentPlatform(r)
|
|
|
|
if agentPlatformErr != nil {
|
|
|
|
return httperror.BadRequest("agent platform header is not valid", err)
|
2022-04-07 12:17:36 +00:00
|
|
|
}
|
2022-08-12 01:21:56 +00:00
|
|
|
endpoint.Type = agentPlatform
|
2020-08-04 00:44:17 +00:00
|
|
|
|
2022-08-11 04:32:12 +00:00
|
|
|
version := r.Header.Get(portainer.PortainerAgentHeader)
|
|
|
|
endpoint.Agent.Version = version
|
|
|
|
|
2022-04-07 12:17:36 +00:00
|
|
|
endpoint.LastCheckInDate = time.Now().Unix()
|
2020-08-04 00:44:17 +00:00
|
|
|
|
2022-04-07 12:17:36 +00:00
|
|
|
err = handler.DataStore.Endpoint().UpdateEndpoint(endpoint.ID, endpoint)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to Unable to persist environment changes inside the database", err)
|
2021-03-01 00:43:47 +00:00
|
|
|
}
|
2020-08-04 00:44:17 +00:00
|
|
|
|
2022-04-07 12:17:36 +00:00
|
|
|
checkinInterval := endpoint.EdgeCheckinInterval
|
2022-01-17 22:25:29 +00:00
|
|
|
if endpoint.EdgeCheckinInterval == 0 {
|
|
|
|
settings, err := handler.DataStore.Settings().Settings()
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to retrieve settings from the database", err)
|
2022-01-17 22:25:29 +00:00
|
|
|
}
|
2022-04-07 12:17:36 +00:00
|
|
|
checkinInterval = settings.EdgeAgentCheckinInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
tunnel := handler.ReverseTunnelService.GetTunnelDetails(endpoint.ID)
|
|
|
|
|
|
|
|
statusResponse := endpointEdgeStatusInspectResponse{
|
|
|
|
Status: tunnel.Status,
|
|
|
|
Port: tunnel.Port,
|
|
|
|
CheckinInterval: checkinInterval,
|
|
|
|
Credentials: tunnel.Credentials,
|
|
|
|
}
|
2022-01-17 22:25:29 +00:00
|
|
|
|
2022-04-07 12:17:36 +00:00
|
|
|
schedules, handlerErr := handler.buildSchedules(endpoint.ID, tunnel)
|
|
|
|
if handlerErr != nil {
|
|
|
|
return handlerErr
|
2022-01-17 22:25:29 +00:00
|
|
|
}
|
2022-04-07 12:17:36 +00:00
|
|
|
statusResponse.Schedules = schedules
|
2022-01-17 22:25:29 +00:00
|
|
|
|
2022-04-07 12:17:36 +00:00
|
|
|
if tunnel.Status == portainer.EdgeAgentManagementRequired {
|
|
|
|
handler.ReverseTunnelService.SetTunnelStatusToActive(endpoint.ID)
|
|
|
|
}
|
2021-03-01 00:43:47 +00:00
|
|
|
|
2022-04-07 12:17:36 +00:00
|
|
|
edgeStacksStatus, handlerErr := handler.buildEdgeStacks(endpoint.ID)
|
|
|
|
if handlerErr != nil {
|
|
|
|
return handlerErr
|
|
|
|
}
|
|
|
|
statusResponse.Stacks = edgeStacksStatus
|
|
|
|
|
2023-01-06 19:25:41 +00:00
|
|
|
return cacheResponse(w, endpoint.ID, statusResponse)
|
2022-04-07 12:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseAgentPlatform(r *http.Request) (portainer.EndpointType, error) {
|
|
|
|
agentPlatformHeader := r.Header.Get(portainer.HTTPResponseAgentPlatform)
|
|
|
|
if agentPlatformHeader == "" {
|
|
|
|
return 0, errors.New("agent platform header is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
agentPlatformNumber, err := strconv.Atoi(agentPlatformHeader)
|
2021-03-01 00:43:47 +00:00
|
|
|
if err != nil {
|
2022-04-07 12:17:36 +00:00
|
|
|
return 0, err
|
2019-07-25 22:38:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 12:17:36 +00:00
|
|
|
agentPlatform := portainer.AgentPlatform(agentPlatformNumber)
|
2019-07-25 22:38:07 +00:00
|
|
|
|
2022-04-07 12:17:36 +00:00
|
|
|
switch agentPlatform {
|
|
|
|
case portainer.AgentPlatformDocker:
|
|
|
|
return portainer.EdgeAgentOnDockerEnvironment, nil
|
|
|
|
case portainer.AgentPlatformKubernetes:
|
|
|
|
return portainer.EdgeAgentOnKubernetesEnvironment, nil
|
|
|
|
default:
|
|
|
|
return 0, fmt.Errorf("agent platform %v is not valid", agentPlatform)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) buildSchedules(endpointID portainer.EndpointID, tunnel portainer.TunnelDetails) ([]edgeJobResponse, *httperror.HandlerError) {
|
2020-06-25 03:25:51 +00:00
|
|
|
schedules := []edgeJobResponse{}
|
|
|
|
for _, job := range tunnel.Jobs {
|
2022-12-19 21:54:51 +00:00
|
|
|
var collectLogs bool
|
|
|
|
if _, ok := job.GroupLogsCollection[endpointID]; ok {
|
|
|
|
collectLogs = job.GroupLogsCollection[endpointID].CollectLogs
|
|
|
|
} else {
|
|
|
|
collectLogs = job.Endpoints[endpointID].CollectLogs
|
|
|
|
}
|
|
|
|
|
2020-06-25 03:25:51 +00:00
|
|
|
schedule := edgeJobResponse{
|
|
|
|
ID: job.ID,
|
|
|
|
CronExpression: job.CronExpression,
|
2022-12-19 21:54:51 +00:00
|
|
|
CollectLogs: collectLogs,
|
2020-06-25 03:25:51 +00:00
|
|
|
Version: job.Version,
|
|
|
|
}
|
|
|
|
|
2022-03-28 15:02:09 +00:00
|
|
|
file, err := handler.FileService.GetFileContent(job.ScriptPath, "")
|
2020-06-25 03:25:51 +00:00
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return nil, httperror.InternalServerError("Unable to retrieve Edge job script file", err)
|
2020-06-25 03:25:51 +00:00
|
|
|
}
|
|
|
|
schedule.Script = base64.RawStdEncoding.EncodeToString(file)
|
|
|
|
|
|
|
|
schedules = append(schedules, schedule)
|
|
|
|
}
|
2022-04-07 12:17:36 +00:00
|
|
|
return schedules, nil
|
|
|
|
}
|
2020-06-25 03:25:51 +00:00
|
|
|
|
2022-04-07 12:17:36 +00:00
|
|
|
func (handler *Handler) buildEdgeStacks(endpointID portainer.EndpointID) ([]stackStatusResponse, *httperror.HandlerError) {
|
|
|
|
relation, err := handler.DataStore.EndpointRelation().EndpointRelation(endpointID)
|
2020-05-14 02:14:28 +00:00
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return nil, httperror.InternalServerError("Unable to retrieve relation object from the database", err)
|
2020-05-14 02:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
edgeStacksStatus := []stackStatusResponse{}
|
|
|
|
for stackID := range relation.EdgeStacks {
|
2023-01-06 19:25:41 +00:00
|
|
|
version, ok := handler.DataStore.EdgeStack().EdgeStackVersion(stackID)
|
|
|
|
if !ok {
|
2022-09-14 23:42:39 +00:00
|
|
|
return nil, httperror.InternalServerError("Unable to retrieve edge stack from the database", err)
|
2020-05-14 02:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stackStatus := stackStatusResponse{
|
2023-01-06 19:25:41 +00:00
|
|
|
ID: stackID,
|
|
|
|
Version: version,
|
2020-05-14 02:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
edgeStacksStatus = append(edgeStacksStatus, stackStatus)
|
|
|
|
}
|
2023-01-06 19:25:41 +00:00
|
|
|
|
2022-04-07 12:17:36 +00:00
|
|
|
return edgeStacksStatus, nil
|
2019-07-25 22:38:07 +00:00
|
|
|
}
|
2023-01-06 19:25:41 +00:00
|
|
|
|
|
|
|
func cacheResponse(w http.ResponseWriter, endpointID portainer.EndpointID, statusResponse endpointEdgeStatusInspectResponse) *httperror.HandlerError {
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
|
|
|
|
httpErr := response.JSON(rr, statusResponse)
|
|
|
|
if httpErr != nil {
|
|
|
|
return httpErr
|
|
|
|
}
|
|
|
|
|
|
|
|
h := fnv.New32a()
|
|
|
|
h.Write(rr.Body.Bytes())
|
|
|
|
etag := strconv.FormatUint(uint64(h.Sum32()), 16)
|
|
|
|
|
|
|
|
cache.Set(endpointID, []byte(etag))
|
|
|
|
|
|
|
|
resp := rr.Result()
|
|
|
|
|
|
|
|
for k, vs := range resp.Header {
|
|
|
|
for _, v := range vs {
|
|
|
|
w.Header().Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("ETag", etag)
|
|
|
|
io.Copy(w, resp.Body)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (handler *Handler) respondFromCache(w http.ResponseWriter, r *http.Request, endpointID portainer.EndpointID) bool {
|
|
|
|
inmHeader := r.Header.Get("If-None-Match")
|
|
|
|
etags := strings.Split(inmHeader, ",")
|
|
|
|
|
|
|
|
if len(inmHeader) == 0 || etags[0] == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
cachedETag, ok := cache.Get(endpointID)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, etag := range etags {
|
|
|
|
if !bytes.Equal([]byte(etag), cachedETag) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.DataStore.Endpoint().UpdateHeartbeat(endpointID)
|
|
|
|
|
|
|
|
w.Header().Set("ETag", etag)
|
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|