From 626c689d92d0bac50622f1228943c8d0b8aad141 Mon Sep 17 00:00:00 2001 From: Gianmaria Del Monte <39946305+gmgigi96@users.noreply.github.com> Date: Mon, 17 Oct 2022 14:09:14 +0200 Subject: [PATCH] Use custom reva logger in ocs (#3361) --- changelog/unreleased/fix-logger-ocs.md | 3 ++ .../handlers/apps/sharing/shares/pending.go | 2 +- .../handlers/apps/sharing/shares/shares.go | 51 ++++++++++--------- 3 files changed, 31 insertions(+), 25 deletions(-) create mode 100644 changelog/unreleased/fix-logger-ocs.md diff --git a/changelog/unreleased/fix-logger-ocs.md b/changelog/unreleased/fix-logger-ocs.md new file mode 100644 index 00000000000..90d2653cfb7 --- /dev/null +++ b/changelog/unreleased/fix-logger-ocs.md @@ -0,0 +1,3 @@ +Bugfix: Use custom reva logger in ocs + +https://github.com/cs3org/reva/pull/3361 \ No newline at end of file diff --git a/internal/http/services/owncloud/ocs/handlers/apps/sharing/shares/pending.go b/internal/http/services/owncloud/ocs/handlers/apps/sharing/shares/pending.go index 86bb93de564..c6646edb0d3 100644 --- a/internal/http/services/owncloud/ocs/handlers/apps/sharing/shares/pending.go +++ b/internal/http/services/owncloud/ocs/handlers/apps/sharing/shares/pending.go @@ -87,7 +87,7 @@ func (h *Handler) updateReceivedShare(w http.ResponseWriter, r *http.Request, sh info, status, err := h.getResourceInfoByID(ctx, client, rs.Share.ResourceId) if err != nil || status.Code != rpc.Code_CODE_OK { - h.logProblems(status, err, "could not stat, skipping") + h.logProblems(status, err, "could not stat, skipping", logger) } data, err := conversions.CS3Share2ShareData(r.Context(), rs.Share) diff --git a/internal/http/services/owncloud/ocs/handlers/apps/sharing/shares/shares.go b/internal/http/services/owncloud/ocs/handlers/apps/sharing/shares/shares.go index a2850ddd378..a44f7628216 100644 --- a/internal/http/services/owncloud/ocs/handlers/apps/sharing/shares/shares.go +++ b/internal/http/services/owncloud/ocs/handlers/apps/sharing/shares/shares.go @@ -39,7 +39,7 @@ import ( link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1" provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" "github.com/go-chi/chi/v5" - "github.com/rs/zerolog/log" + "github.com/rs/zerolog" "github.com/ReneKroon/ttlcache/v2" "github.com/cs3org/reva/internal/http/services/owncloud/ocdav" @@ -175,17 +175,17 @@ func (h *Handler) CreateShare(w http.ResponseWriter, r *http.Request) { Ref: &ref, } - sublog := appctx.GetLogger(ctx).With().Interface("ref", ref).Logger() + log := appctx.GetLogger(ctx).With().Interface("ref", ref).Logger() statRes, err := client.Stat(ctx, &statReq) if err != nil { - sublog.Debug().Err(err).Str("createShare", "shares").Msg("error on stat call") + log.Debug().Err(err).Str("createShare", "shares").Msg("error on stat call") response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "missing resource information", fmt.Errorf("error getting resource information")) return } if statRes.Status.Code != rpc.Code_CODE_OK { - ocdav.HandleErrorStatus(&sublog, w, statRes.Status) + ocdav.HandleErrorStatus(&log, w, statRes.Status) return } @@ -299,15 +299,15 @@ func (h *Handler) GetShare(w http.ResponseWriter, r *http.Request) { var resourceID *provider.ResourceId shareID := chi.URLParam(r, "shareid") ctx := r.Context() - logger := appctx.GetLogger(r.Context()) - logger.Debug().Str("shareID", shareID).Msg("get share by id") + log := appctx.GetLogger(r.Context()) + log.Debug().Str("shareID", shareID).Msg("get share by id") client, err := pool.GetGatewayServiceClient(pool.Endpoint(h.gatewayAddr)) if err != nil { response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "error getting grpc gateway client", err) return } - logger.Debug().Str("shareID", shareID).Msg("get public share by id") + log.Debug().Str("shareID", shareID).Msg("get public share by id") psRes, err := client.GetPublicShare(r.Context(), &link.GetPublicShareRequest{ Ref: &link.PublicShareReference{ Spec: &link.PublicShareReference_Id{ @@ -342,7 +342,7 @@ func (h *Handler) GetShare(w http.ResponseWriter, r *http.Request) { if share == nil { // check if we have a user share - logger.Debug().Str("shareID", shareID).Msg("get user share by id") + log.Debug().Str("shareID", shareID).Msg("get user share by id") uRes, err := client.GetShare(r.Context(), &collaboration.GetShareRequest{ Ref: &collaboration.ShareReference{ Spec: &collaboration.ShareReference_Id{ @@ -363,7 +363,7 @@ func (h *Handler) GetShare(w http.ResponseWriter, r *http.Request) { } if uRes.Status.Code != rpc.Code_CODE_OK && uRes.Status.Code != rpc.Code_CODE_NOT_FOUND { - logger.Error().Err(err).Msgf("grpc get user share request failed, code: %v", uRes.Status.Code) + log.Error().Err(err).Msgf("grpc get user share request failed, code: %v", uRes.Status.Code) response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "grpc get user share request failed", err) return } @@ -380,7 +380,7 @@ func (h *Handler) GetShare(w http.ResponseWriter, r *http.Request) { } if share == nil { - logger.Debug().Str("shareID", shareID).Msg("no share found with this id") + log.Debug().Str("shareID", shareID).Msg("no share found with this id") response.WriteOCSError(w, r, response.MetaNotFound.StatusCode, "share not found", nil) return } @@ -422,6 +422,7 @@ func (h *Handler) UpdateShare(w http.ResponseWriter, r *http.Request) { func (h *Handler) updateShare(w http.ResponseWriter, r *http.Request, shareID string) { ctx := r.Context() + log := appctx.GetLogger(ctx) pval := r.FormValue("permissions") if pval == "" { @@ -649,7 +650,7 @@ func (h *Handler) listSharesWithMe(w http.ResponseWriter, r *http.Request) { Ref: &provider.Reference{Path: path.Join(h.homeNamespace, h.sharePrefix)}, }) if err != nil || lcRes.Status.Code != rpc.Code_CODE_OK { - h.logProblems(lcRes.GetStatus(), err, "could not list container, continuing without share jail path info") + h.logProblems(lcRes.GetStatus(), err, "could not list container, continuing without share jail path info", log) } else { shareJailInfos = lcRes.Infos } @@ -676,7 +677,7 @@ func (h *Handler) listSharesWithMe(w http.ResponseWriter, r *http.Request) { var status *rpc.Status info, status, err = h.getResourceInfoByID(ctx, client, rs.Share.ResourceId) if err != nil || status.Code != rpc.Code_CODE_OK { - h.logProblems(status, err, "could not stat, skipping") + h.logProblems(status, err, "could not stat, skipping", log) continue } } @@ -753,6 +754,8 @@ func findMatch(shareJailInfos []*provider.ResourceInfo, id *provider.ResourceId) func (h *Handler) listSharesWithOthers(w http.ResponseWriter, r *http.Request) { shares := make([]*conversions.ShareData, 0) + log := appctx.GetLogger(r.Context()) + filters := []*collaboration.Filter{} linkFilters := []*link.ListPublicSharesRequest_Filter{} var e error @@ -800,19 +803,19 @@ func (h *Handler) listSharesWithOthers(w http.ResponseWriter, r *http.Request) { if listPublicShares { publicShares, status, err := h.listPublicShares(r, linkFilters) - h.logProblems(status, err, "could not listPublicShares") + h.logProblems(status, err, "could not listPublicShares", log) shares = append(shares, publicShares...) } if listUserShares { userShares, status, err := h.listUserShares(r, filters) - h.logProblems(status, err, "could not listUserShares") + h.logProblems(status, err, "could not listUserShares", log) shares = append(shares, userShares...) } response.WriteOCSSuccess(w, r, shares) } -func (h *Handler) logProblems(s *rpc.Status, e error, msg string) { +func (h *Handler) logProblems(s *rpc.Status, e error, msg string, log *zerolog.Logger) { if e != nil { // errors need to be taken care of log.Error().Err(e).Msg(msg) @@ -911,17 +914,17 @@ func (h *Handler) addFileInfo(ctx context.Context, s *conversions.ShareData, inf // mustGetIdentifiers always returns a struct with identifiers, if the user or group could not be found they will all be empty func (h *Handler) mustGetIdentifiers(ctx context.Context, client gateway.GatewayAPIClient, id string, isGroup bool) *userIdentifiers { - sublog := appctx.GetLogger(ctx).With().Str("id", id).Logger() + log := appctx.GetLogger(ctx).With().Str("id", id).Logger() if id == "" { return &userIdentifiers{} } if idIf, err := h.userIdentifierCache.Get(id); err == nil { - sublog.Debug().Msg("cache hit") + log.Debug().Msg("cache hit") return idIf.(*userIdentifiers) } - sublog.Debug().Msg("cache miss") + log.Debug().Msg("cache miss") var ui *userIdentifiers if isGroup { @@ -932,18 +935,18 @@ func (h *Handler) mustGetIdentifiers(ctx context.Context, client gateway.Gateway SkipFetchingMembers: true, }) if err != nil { - sublog.Err(err).Msg("could not look up group") + log.Err(err).Msg("could not look up group") return &userIdentifiers{} } if res.GetStatus().GetCode() != rpc.Code_CODE_OK { - sublog.Err(err). + log.Err(err). Int32("code", int32(res.GetStatus().GetCode())). Str("message", res.GetStatus().GetMessage()). Msg("get group call failed") return &userIdentifiers{} } if res.Group == nil { - sublog.Debug(). + log.Debug(). Int32("code", int32(res.GetStatus().GetCode())). Str("message", res.GetStatus().GetMessage()). Msg("group not found") @@ -962,18 +965,18 @@ func (h *Handler) mustGetIdentifiers(ctx context.Context, client gateway.Gateway SkipFetchingUserGroups: true, }) if err != nil { - sublog.Err(err).Msg("could not look up user") + log.Err(err).Msg("could not look up user") return &userIdentifiers{} } if res.GetStatus().GetCode() != rpc.Code_CODE_OK { - sublog.Err(err). + log.Err(err). Int32("code", int32(res.GetStatus().GetCode())). Str("message", res.GetStatus().GetMessage()). Msg("get user call failed") return &userIdentifiers{} } if res.User == nil { - sublog.Debug(). + log.Debug(). Int32("code", int32(res.GetStatus().GetCode())). Str("message", res.GetStatus().GetMessage()). Msg("user not found")