feat(http): add debug logs in

This commit is contained in:
deviantony
2022-04-30 19:45:46 +00:00
parent 24c61034c1
commit 97d2a3bdf3
4 changed files with 41 additions and 0 deletions
+1
View File
@@ -4,6 +4,7 @@ go 1.17
require (
github.com/Microsoft/go-winio v0.5.1
github.com/Sirupsen/logrus v1.0.6
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535
github.com/aws/aws-sdk-go-v2 v1.11.1
github.com/aws/aws-sdk-go-v2/credentials v1.6.2
+2
View File
@@ -91,6 +91,8 @@ github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbt
github.com/PuerkitoBio/urlesc v0.0.0-20160726150825-5bd2802263f2/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d/go.mod h1:HI8ITrYtUY+O+ZhtlqUnD8+KwNPOyugEhfP9fdUIaEQ=
github.com/Sirupsen/logrus v1.0.6 h1:HCAGQRk48dRVPA5Y+Yh0qdCSTzPOyU1tBJ7Q9YzotII=
github.com/Sirupsen/logrus v1.0.6/go.mod h1:rmk17hk6i8ZSAJkSDa7nOxamrG+SP4P0mm+DAvExv4U=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
@@ -12,6 +12,7 @@ import (
"github.com/portainer/libhttp/response"
portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/http/middlewares"
"github.com/sirupsen/logrus"
)
type stackStatusResponse struct {
@@ -64,16 +65,22 @@ type endpointEdgeStatusInspectResponse struct {
// @failure 500 "Server error"
// @router /endpoints/{id}/edge/status [get]
func (handler *Handler) endpointEdgeStatusInspect(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
logrus.Debug("[http,handler,endpointedge] [message: received request to get endpoint edge status]")
endpoint, err := middlewares.FetchEndpoint(r)
if err != nil {
return httperror.BadRequest("Unable to find an environment on request context", err)
}
logrus.Debugf("[http,handler,endpointedge] [message: retrieved endpoint information from context] [endpoint: %s] [endpoint_id: %s]", endpoint.Name, endpoint.ID)
err = handler.requestBouncer.AuthorizedEdgeEndpointOperation(r, endpoint)
if err != nil {
return &httperror.HandlerError{http.StatusForbidden, "Permission denied to access environment", err}
}
logrus.Debugf("[http,handler,endpointedge] [message: authorized endpoint operation] [endpoint: %s] [endpoint_id: %s]", endpoint.Name, endpoint.ID)
if endpoint.EdgeID == "" {
edgeIdentifier := r.Header.Get(portainer.PortainerAgentEdgeIDHeader)
endpoint.EdgeID = edgeIdentifier
@@ -92,6 +99,8 @@ func (handler *Handler) endpointEdgeStatusInspect(w http.ResponseWriter, r *http
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to Unable to persist environment changes inside the database", err}
}
logrus.Debugf("[http,handler,endpointedge] [message: updated endpoint information in database] [edge_id: %s] [checkin_time: %s]", endpoint.EdgeID, endpoint.LastCheckInDate)
checkinInterval := endpoint.EdgeCheckinInterval
if endpoint.EdgeCheckinInterval == 0 {
settings, err := handler.DataStore.Settings().Settings()
@@ -110,12 +119,16 @@ func (handler *Handler) endpointEdgeStatusInspect(w http.ResponseWriter, r *http
Credentials: tunnel.Credentials,
}
logrus.Debugf("[http,handler,endpointedge] [message: retrieved tunnel information from mem] [status: %s]", tunnel.Status)
schedules, handlerErr := handler.buildSchedules(endpoint.ID, tunnel)
if handlerErr != nil {
return handlerErr
}
statusResponse.Schedules = schedules
logrus.Debugf("[http,handler,endpointedge] [message: built schedules] [schedule_count: %d]", len(schedules))
if tunnel.Status == portainer.EdgeAgentManagementRequired {
handler.ReverseTunnelService.SetTunnelStatusToActive(endpoint.ID)
}
@@ -126,6 +139,8 @@ func (handler *Handler) endpointEdgeStatusInspect(w http.ResponseWriter, r *http
}
statusResponse.Stacks = edgeStacksStatus
logrus.Debugf("[http,handler,endpointedge] [message: built stacks] [stack_count: %d]", len(edgeStacksStatus))
return response.JSON(w, statusResponse)
}
@@ -18,6 +18,7 @@ import (
"github.com/portainer/portainer/api/crypto"
"github.com/portainer/portainer/api/http/client"
"github.com/portainer/portainer/api/internal/edge"
"github.com/sirupsen/logrus"
)
type endpointCreatePayload struct {
@@ -181,32 +182,44 @@ func (payload *endpointCreatePayload) Validate(r *http.Request) error {
// @failure 500 "Server error"
// @router /endpoints [post]
func (handler *Handler) endpointCreate(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
logrus.Debug("[http,handler,endpoints] [message: received request to create endpoint]")
payload := &endpointCreatePayload{}
err := payload.Validate(r)
if err != nil {
return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err}
}
logrus.Debug("[http,handler,endpoints] [message: successfully validated payload]")
endpoint, endpointCreationError := handler.createEndpoint(payload)
if endpointCreationError != nil {
return endpointCreationError
}
logrus.Debugf("[http,handler,endpoints] [message: successfully created endpoint] [endpoint: %s] [endpoint_id: %s]", endpoint.Name, endpoint.ID)
endpointGroup, err := handler.DataStore.EndpointGroup().EndpointGroup(endpoint.GroupID)
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an environment group inside the database", err}
}
logrus.Debugf("[http,handler,endpoints] [message: fetched endpoint group] [group: %s]", endpointGroup.Name)
edgeGroups, err := handler.DataStore.EdgeGroup().EdgeGroups()
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve edge groups from the database", err}
}
logrus.Debug("[http,handler,endpoints] [message: successfully fetched edge groups]")
edgeStacks, err := handler.DataStore.EdgeStack().EdgeStacks()
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve edge stacks from the database", err}
}
logrus.Debug("[http,handler,endpoints] [message: successfully fetched edge stacks]")
relationObject := &portainer.EndpointRelation{
EndpointID: endpoint.ID,
EdgeStacks: map[portainer.EdgeStackID]bool{},
@@ -217,6 +230,8 @@ func (handler *Handler) endpointCreate(w http.ResponseWriter, r *http.Request) *
for _, stackID := range relatedEdgeStacks {
relationObject.EdgeStacks[stackID] = true
}
logrus.Debugf("[http,handler,endpoints] [message: computed edge stacks relationships] [stack_count: %d]", len(relatedEdgeStacks))
}
err = handler.DataStore.EndpointRelation().Create(relationObject)
@@ -224,6 +239,8 @@ func (handler *Handler) endpointCreate(w http.ResponseWriter, r *http.Request) *
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the relation object inside the database", err}
}
logrus.Debug("[http,handler,endpoints] [message: created database relation object]")
return response.JSON(w, endpoint)
}
@@ -308,6 +325,8 @@ func (handler *Handler) createEdgeAgentEndpoint(payload *endpointCreatePayload)
edgeKey := handler.ReverseTunnelService.GenerateEdgeKey(payload.URL, portainerHost, endpointID)
logrus.Debugf("[http,handler,endpoints] [message: successfully generated Edge key] [key: %s]", edgeKey)
endpoint := &portainer.Endpoint{
ID: portainer.EndpointID(endpointID),
Name: payload.Name,
@@ -334,6 +353,8 @@ func (handler *Handler) createEdgeAgentEndpoint(payload *endpointCreatePayload)
return nil, &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve the settings from the database", err}
}
logrus.Debug("[http,handler,endpoints] [message: successfully fetched settings]")
if settings.EnforceEdgeID {
edgeID, err := uuid.NewV4()
if err != nil {
@@ -348,6 +369,8 @@ func (handler *Handler) createEdgeAgentEndpoint(payload *endpointCreatePayload)
return nil, &httperror.HandlerError{http.StatusInternalServerError, "An error occured while trying to create the environment", err}
}
logrus.Debug("[http,handler,endpoints] [message: successfully updated endpoint authorizations]")
return endpoint, nil
}