1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-08-02 20:35:25 +02:00

feat(logging): replace all the loggers with zerolog EE-4186 (#7663)

This commit is contained in:
andres-portainer 2022-09-16 13:18:44 -03:00 committed by GitHub
parent 53025178ef
commit 36e7981ab7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
109 changed files with 1101 additions and 662 deletions

View file

@ -2,16 +2,17 @@ package factory
import (
"fmt"
"log"
"net"
"net/http"
"github.com/pkg/errors"
portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/crypto"
"github.com/portainer/portainer/api/http/proxy/factory/agent"
"github.com/portainer/portainer/api/internal/endpointutils"
"github.com/portainer/portainer/api/internal/url"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
// ProxyServer provide an extended proxy with a local server to forward requests
@ -24,7 +25,7 @@ type ProxyServer struct {
func (factory *ProxyFactory) NewAgentProxy(endpoint *portainer.Endpoint) (*ProxyServer, error) {
urlString := endpoint.URL
if endpointutils.IsEdgeEndpoint((endpoint)) {
if endpointutils.IsEdgeEndpoint(endpoint) {
tunnel, err := factory.reverseTunnelService.GetActiveTunnel(endpoint)
if err != nil {
return nil, errors.Wrap(err, "failed starting tunnel")
@ -77,15 +78,16 @@ func (proxy *ProxyServer) start() error {
}
proxy.Port = listener.Addr().(*net.TCPAddr).Port
go func() {
proxyHost := fmt.Sprintf("127.0.0.1:%d", proxy.Port)
log.Printf("Starting Proxy server on %s...\n", proxyHost)
log.Debug().Str("host", proxyHost).Msg("starting proxy server")
err := proxy.server.Serve(listener)
log.Printf("Exiting Proxy server %s\n", proxyHost)
log.Debug().Str("host", proxyHost).Msg("exiting proxy server")
if err != nil && err != http.ErrServerClosed {
log.Printf("Proxy server %s exited with an error: %s\n", proxyHost, err)
log.Debug().Str("host", proxyHost).Err(err).Msg("proxy server exited with an error")
}
}()

View file

@ -1,12 +1,13 @@
package azure
import (
"log"
"net/http"
portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/http/security"
"github.com/portainer/portainer/api/internal/authorization"
"github.com/rs/zerolog/log"
)
func (transport *Transport) createAzureRequestContext(request *http.Request) (*azureRequestContext, error) {
@ -65,7 +66,11 @@ func (transport *Transport) createPrivateResourceControl(
err := transport.dataStore.ResourceControl().Create(resourceControl)
if err != nil {
log.Printf("[ERROR] [http,proxy,azure,transport] [message: unable to persist resource control] [resource: %s] [err: %s]", resourceIdentifier, err)
log.Error().
Str("resource", resourceIdentifier).
Err(err).
Msg("unable to persist resource control")
return nil, err
}
@ -76,6 +81,7 @@ func (transport *Transport) userCanDeleteContainerGroup(request *http.Request, c
if context.isAdmin {
return true
}
resourceIdentifier := request.URL.Path
resourceControl := transport.findResourceControl(resourceIdentifier, context)
return authorization.UserCanAccessResource(context.userID, context.userTeamIDs, resourceControl)
@ -100,7 +106,7 @@ func (transport *Transport) decorateContainerGroup(containerGroup map[string]int
containerGroup = decorateObject(containerGroup, resourceControl)
}
} else {
log.Printf("[WARN] [http,proxy,azure,decorate] [message: unable to find resource id property in container group]")
log.Warn().Msg("unable to find resource id property in container group")
}
return containerGroup
@ -137,7 +143,7 @@ func (transport *Transport) removeResourceControl(containerGroup map[string]inte
return err
}
} else {
log.Printf("[WARN] [http,proxy,azure] [message: missign ID in container group]")
log.Debug().Msg("missing ID in container group")
}
return nil

View file

@ -3,7 +3,6 @@ package factory
import (
"fmt"
"io"
"log"
"net/http"
"strings"
@ -12,6 +11,8 @@ import (
"github.com/portainer/portainer/api/crypto"
"github.com/portainer/portainer/api/http/proxy/factory/docker"
"github.com/portainer/portainer/api/internal/url"
"github.com/rs/zerolog/log"
)
func (factory *ProxyFactory) newDockerProxy(endpoint *portainer.Endpoint) (http.Handler, error) {
@ -107,6 +108,6 @@ func (proxy *dockerLocalProxy) ServeHTTP(w http.ResponseWriter, r *http.Request)
w.WriteHeader(res.StatusCode)
if _, err := io.Copy(w, res.Body); err != nil {
log.Printf("proxy error: %s\n", err)
log.Debug().Err(err).Msg("proxy error")
}
}

View file

@ -1,16 +1,15 @@
package docker
import (
"log"
"net/http"
"strings"
"github.com/portainer/portainer/api/internal/stackutils"
portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/http/proxy/factory/utils"
"github.com/portainer/portainer/api/internal/authorization"
"github.com/portainer/portainer/api/internal/stackutils"
portainer "github.com/portainer/portainer/api"
"github.com/rs/zerolog/log"
)
const (
@ -79,7 +78,11 @@ func (transport *Transport) newResourceControlFromPortainerLabels(labelsObject m
for _, name := range teamNames {
team, err := transport.dataStore.Team().TeamByName(name)
if err != nil {
log.Printf("[WARN] [http,proxy,docker] [message: unknown team name in access control label, ignoring access control rule for this team] [name: %s] [resource_id: %s]", name, resourceID)
log.Warn().
Str("name", name).
Str("resource_id", resourceID).
Msg("unknown team name in access control label, ignoring access control rule for this team")
continue
}
@ -89,7 +92,11 @@ func (transport *Transport) newResourceControlFromPortainerLabels(labelsObject m
for _, name := range userNames {
user, err := transport.dataStore.User().UserByUsername(name)
if err != nil {
log.Printf("[WARN] [http,proxy,docker] [message: unknown user name in access control label, ignoring access control rule for this user] [name: %s] [resource_id: %s]", name, resourceID)
log.Warn().
Str("name", name).
Str("resource_id", resourceID).
Msg("unknown user name in access control label, ignoring access control rule for this user")
continue
}
@ -114,7 +121,11 @@ func (transport *Transport) createPrivateResourceControl(resourceIdentifier stri
err := transport.dataStore.ResourceControl().Create(resourceControl)
if err != nil {
log.Printf("[ERROR] [http,proxy,docker,transport] [message: unable to persist resource control] [resource: %s] [err: %s]", resourceIdentifier, err)
log.Error().
Str("resource", resourceIdentifier).
Err(err).
Msg("unable to persist resource control")
return nil, err
}
@ -148,7 +159,10 @@ func (transport *Transport) getInheritedResourceControlFromServiceOrStack(resour
func (transport *Transport) applyAccessControlOnResource(parameters *resourceOperationParameters, responseObject map[string]interface{}, response *http.Response, executor *operationExecutor) error {
if responseObject[parameters.resourceIdentifierAttribute] == nil {
log.Printf("[WARN] [message: unable to find resource identifier property in resource object] [identifier_attribute: %s]", parameters.resourceIdentifierAttribute)
log.Warn().
Str("identifier_attribute", parameters.resourceIdentifierAttribute).
Msg("unable to find resource identifier property in resource object")
return nil
}
@ -195,7 +209,10 @@ func (transport *Transport) decorateResourceList(parameters *resourceOperationPa
resourceObject := resource.(map[string]interface{})
if resourceObject[parameters.resourceIdentifierAttribute] == nil {
log.Printf("[WARN] [http,proxy,docker,decorate] [message: unable to find resource identifier property in resource list element] [identifier_attribute: %s]", parameters.resourceIdentifierAttribute)
log.Warn().
Str("identifier_attribute", parameters.resourceIdentifierAttribute).
Msg("unable to find resource identifier property in resource list element")
continue
}
@ -232,7 +249,10 @@ func (transport *Transport) filterResourceList(parameters *resourceOperationPara
for _, resource := range resourceData {
resourceObject := resource.(map[string]interface{})
if resourceObject[parameters.resourceIdentifierAttribute] == nil {
log.Printf("[WARN] [http,proxy,docker,filter] [message: unable to find resource identifier property in resource list element] [identifier_attribute: %s]", parameters.resourceIdentifierAttribute)
log.Warn().
Str("identifier_attribute", parameters.resourceIdentifierAttribute).
Msg("unable to find resource identifier property in resource list element")
continue
}

View file

@ -5,11 +5,12 @@ import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"mime"
"net/http"
"github.com/portainer/portainer/api/archive"
"github.com/rs/zerolog/log"
)
const OneMegabyte = 1024768
@ -78,7 +79,7 @@ func buildOperation(request *http.Request) error {
defer f.Close()
log.Printf("[INFO] [http,proxy,docker] [message: upload the file to build image] [filename: %s] [size: %d]", hdr.Filename, hdr.Size)
log.Info().Str("filename", hdr.Filename).Int64("size", hdr.Size).Msg("upload the file to build image")
content, err := ioutil.ReadAll(f)
if err != nil {

View file

@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"path"
"regexp"
@ -21,6 +20,8 @@ import (
"github.com/portainer/portainer/api/http/proxy/factory/utils"
"github.com/portainer/portainer/api/http/security"
"github.com/portainer/portainer/api/internal/authorization"
"github.com/rs/zerolog/log"
)
var apiVersionRe = regexp.MustCompile(`(/v[0-9]\.[0-9]*)?`)
@ -422,6 +423,7 @@ func (transport *Transport) proxyImageRequest(request *http.Request) (*http.Resp
func (transport *Transport) replaceRegistryAuthenticationHeader(request *http.Request) (*http.Response, error) {
transport.decorateRegistryAuthenticationHeader(request)
return transport.decorateGenericResourceCreationOperation(request, serviceObjectIdentifier, portainer.ServiceResourceControl)
}
@ -601,7 +603,8 @@ func (transport *Transport) decorateGenericResourceCreationResponse(response *ht
}
if responseObject[resourceIdentifierAttribute] == nil {
log.Printf("[ERROR] [proxy,docker]")
log.Error().Msg("missing identifier in Docker resource creation response")
return errors.New("missing identifier in Docker resource creation response")
}
@ -754,6 +757,7 @@ func (transport *Transport) createOperationContext(request *http.Request) (*rest
for _, membership := range teamMemberships {
userTeamIDs = append(userTeamIDs, membership.TeamID)
}
operationContext.userTeamIDs = userTeamIDs
}

View file

@ -6,18 +6,18 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"path"
"regexp"
"strconv"
"strings"
portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/dataservices"
"github.com/portainer/portainer/api/http/security"
"github.com/portainer/portainer/api/kubernetes/cli"
portainer "github.com/portainer/portainer/api"
"github.com/rs/zerolog/log"
)
type baseTransport struct {
@ -132,7 +132,10 @@ func (transport *baseTransport) getRoundTripToken(request *http.Request, tokenMa
} else {
token, err = tokenManager.GetUserServiceAccountToken(int(tokenData.ID), transport.endpoint.ID)
if err != nil {
log.Printf("Failed retrieving service account token: %v", err)
log.Debug().
Err(err).
Msg("failed retrieving service account token")
return "", err
}
}

View file

@ -3,10 +3,12 @@ package utils
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"github.com/rs/zerolog/log"
)
// GetResponseAsJSONObject returns the response content as a generic JSON object
@ -34,10 +36,17 @@ func GetResponseAsJSONArray(response *http.Response) ([]interface{}, error) {
if responseObject["message"] != nil {
return nil, errors.New(responseObject["message"].(string))
}
log.Printf("[ERROR] [http,proxy,response] [message: invalid response format, expecting JSON array] [response: %+v]", responseObject)
log.Error().
Str("response", fmt.Sprintf("%+v", responseObject)).
Msg("invalid response format, expecting JSON array")
return nil, errors.New("unable to parse response: expected JSON array, got JSON object")
default:
log.Printf("[ERROR] [http,proxy,response] [message: invalid response format, expecting JSON array] [response: %+v]", responseObject)
log.Error().
Str("response", fmt.Sprintf("%+v", responseObject)).
Msg("invalid response format, expecting JSON array")
return nil, errors.New("unable to parse response: expected JSON array")
}
}
@ -50,6 +59,7 @@ type errorResponse struct {
func WriteAccessDeniedResponse() (*http.Response, error) {
response := &http.Response{}
err := RewriteResponse(response, errorResponse{Message: "access denied to resource"}, http.StatusForbidden)
return response, err
}