Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
Use custom reva logger in ocs (cs3org#3361)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgigi96 authored and vascoguita committed Oct 26, 2022
1 parent 3cf7b3c commit 626c689
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
3 changes: 3 additions & 0 deletions changelog/unreleased/fix-logger-ocs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Bugfix: Use custom reva logger in ocs

https://github.com/cs3org/reva/pull/3361
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit 626c689

Please sign in to comment.