Skip to content

Commit

Permalink
Use context-aware logging whenever possible
Browse files Browse the repository at this point in the history
  • Loading branch information
irees committed Dec 19, 2024
1 parent 22c1b5b commit 111045d
Show file tree
Hide file tree
Showing 38 changed files with 224 additions and 207 deletions.
2 changes: 1 addition & 1 deletion cmd/tlserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (cmd *ServerCommand) Run() error {
// Create Finder
dbFinder := dbfinder.NewFinder(db)
if cmd.LoadAdmins {
dbFinder.LoadAdmins()
dbFinder.LoadAdmins(context.Background())
}

// Create RTFinder, GbfsFinder
Expand Down
8 changes: 4 additions & 4 deletions finders/actions/feed_fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,18 @@ func RTFetch(ctx context.Context, target string, feedId string, feedUrl string,
return errors.New("invalid rt data")
}
key := fmt.Sprintf("rtdata:%s:%s", target, urlType)
return cfg.RTFinder.AddData(key, rtdata)
return cfg.RTFinder.AddData(ctx, key, rtdata)
}

func GbfsFetch(ctx context.Context, feedId string, feedUrl string) error {
cfg := model.ForContext(ctx)
log := log.For(ctx)
gfeeds, err := cfg.Finder.FindFeeds(ctx, nil, nil, nil, &model.FeedFilter{OnestopID: &feedId})
if err != nil {
log.Error().Err(err).Msg("gbfs-fetch: error loading source feed")
log.For(ctx).Error().Err(err).Msg("gbfs-fetch: error loading source feed")
return err
}
if len(gfeeds) == 0 {
log.Error().Err(err).Msg("gbfs-fetch: source feed not found")
log.For(ctx).Error().Err(err).Msg("gbfs-fetch: source feed not found")
return errors.New("feed not found")
}

Expand All @@ -155,6 +154,7 @@ func GbfsFetch(ctx context.Context, feedId string, feedUrl string) error {
opts.FeedURL = feedUrl
}
feeds, result, err := gbfs.Fetch(
ctx,
tldb.NewPostgresAdapterFromDBX(cfg.Finder.DBX()),
opts,
)
Expand Down
28 changes: 14 additions & 14 deletions finders/azchecker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (c *Checker) TenantSave(ctx context.Context, req *authz.TenantSaveRequest)
return nil, ErrUnauthorized
}
newName := t.GetName()
log.Trace().Str("tenantName", newName).Int64("id", tenantId).Msg("TenantSave")
log.For(ctx).Trace().Str("tenantName", newName).Int64("id", tenantId).Msg("TenantSave")
_, err := sq.StatementBuilder.
RunWith(c.db).
PlaceholderFormat(sq.Dollar).
Expand All @@ -360,7 +360,7 @@ func (c *Checker) TenantAddPermission(ctx context.Context, req *authz.TenantModi
return nil, ErrUnauthorized
}
tk := req.GetEntityRelation().WithObject(newEntityID(TenantType, tenantId))
log.Trace().Str("tk", tk.String()).Int64("id", tenantId).Msg("TenantAddPermission")
log.For(ctx).Trace().Str("tk", tk.String()).Int64("id", tenantId).Msg("TenantAddPermission")
return &authz.TenantSaveResponse{}, c.fgaClient.SetExclusiveSubjectRelation(ctx, tk, MemberRelation, AdminRelation)
}

Expand All @@ -372,7 +372,7 @@ func (c *Checker) TenantRemovePermission(ctx context.Context, req *authz.TenantM
return nil, ErrUnauthorized
}
tk := req.GetEntityRelation().WithObject(newEntityID(TenantType, tenantId))
log.Trace().Str("tk", tk.String()).Int64("id", tenantId).Msg("TenantRemovePermission")
log.For(ctx).Trace().Str("tk", tk.String()).Int64("id", tenantId).Msg("TenantRemovePermission")
return &authz.TenantSaveResponse{}, c.fgaClient.DeleteTuple(ctx, tk)
}

Expand All @@ -388,7 +388,7 @@ func (c *Checker) TenantCreateGroup(ctx context.Context, req *authz.TenantCreate
} else if !check.Actions.CanCreateOrg {
return nil, ErrUnauthorized
}
log.Trace().Str("groupName", groupName).Int64("id", tenantId).Msg("TenantCreateGroup")
log.For(ctx).Trace().Str("groupName", groupName).Int64("id", tenantId).Msg("TenantCreateGroup")
groupId := int64(0)
err := sq.StatementBuilder.
RunWith(c.db).
Expand Down Expand Up @@ -495,7 +495,7 @@ func (c *Checker) GroupSave(ctx context.Context, req *authz.GroupSaveRequest) (*
} else if !check.Actions.CanEdit {
return nil, ErrUnauthorized
}
log.Trace().Str("groupName", newName).Int64("id", groupId).Msg("GroupSave")
log.For(ctx).Trace().Str("groupName", newName).Int64("id", groupId).Msg("GroupSave")
_, err := sq.StatementBuilder.
RunWith(c.db).
PlaceholderFormat(sq.Dollar).
Expand All @@ -515,7 +515,7 @@ func (c *Checker) GroupAddPermission(ctx context.Context, req *authz.GroupModify
return nil, ErrUnauthorized
}
tk := req.GetEntityRelation().WithObject(newEntityID(GroupType, groupId))
log.Trace().Str("tk", tk.String()).Int64("id", groupId).Msg("GroupAddPermission")
log.For(ctx).Trace().Str("tk", tk.String()).Int64("id", groupId).Msg("GroupAddPermission")
return &authz.GroupSaveResponse{}, c.fgaClient.SetExclusiveSubjectRelation(ctx, tk, ViewerRelation, EditorRelation, ManagerRelation)
}

Expand All @@ -527,7 +527,7 @@ func (c *Checker) GroupRemovePermission(ctx context.Context, req *authz.GroupMod
return nil, ErrUnauthorized
}
tk := req.GetEntityRelation().WithObject(newEntityID(GroupType, groupId))
log.Trace().Str("tk", tk.String()).Int64("id", groupId).Msg("GroupRemovePermission")
log.For(ctx).Trace().Str("tk", tk.String()).Int64("id", groupId).Msg("GroupRemovePermission")
return &authz.GroupSaveResponse{}, c.fgaClient.DeleteTuple(ctx, tk)
}

Expand All @@ -540,7 +540,7 @@ func (c *Checker) GroupSetTenant(ctx context.Context, req *authz.GroupSetTenantR
return nil, ErrUnauthorized
}
tk := authz.NewTupleKey().WithSubjectID(TenantType, newTenantId).WithObjectID(GroupType, groupId).WithRelation(ParentRelation)
log.Trace().Str("tk", tk.String()).Int64("id", groupId).Msg("GroupSetTenant")
log.For(ctx).Trace().Str("tk", tk.String()).Int64("id", groupId).Msg("GroupSetTenant")
return &authz.GroupSetTenantResponse{}, c.fgaClient.SetExclusiveRelation(ctx, tk)
}

Expand Down Expand Up @@ -611,7 +611,7 @@ func (c *Checker) FeedSetGroup(ctx context.Context, req *authz.FeedSetGroupReque
return nil, ErrUnauthorized
}
tk := authz.NewTupleKey().WithSubjectID(GroupType, newGroup).WithObjectID(FeedType, feedId).WithRelation(ParentRelation)
log.Trace().Str("tk", tk.String()).Int64("id", feedId).Msg("FeedSetGroup")
log.For(ctx).Trace().Str("tk", tk.String()).Int64("id", feedId).Msg("FeedSetGroup")
return &authz.FeedSaveResponse{}, c.fgaClient.SetExclusiveRelation(ctx, tk)
}

Expand Down Expand Up @@ -698,7 +698,7 @@ func (c *Checker) FeedVersionAddPermission(ctx context.Context, req *authz.FeedV
return nil, ErrUnauthorized
}
tk := req.GetEntityRelation().WithObject(newEntityID(FeedVersionType, fvid))
log.Trace().Str("tk", tk.String()).Int64("id", fvid).Msg("FeedVersionAddPermission")
log.For(ctx).Trace().Str("tk", tk.String()).Int64("id", fvid).Msg("FeedVersionAddPermission")
return &authz.FeedVersionSaveResponse{}, c.fgaClient.SetExclusiveSubjectRelation(ctx, tk, ViewerRelation, EditorRelation, ManagerRelation)
}

Expand All @@ -710,7 +710,7 @@ func (c *Checker) FeedVersionRemovePermission(ctx context.Context, req *authz.Fe
return nil, ErrUnauthorized
}
tk := req.GetEntityRelation().WithObject(newEntityID(FeedVersionType, fvid))
log.Trace().Str("tk", tk.String()).Int64("id", fvid).Msg("FeedVersionRemovePermission")
log.For(ctx).Trace().Str("tk", tk.String()).Int64("id", fvid).Msg("FeedVersionRemovePermission")
return &authz.FeedVersionSaveResponse{}, c.fgaClient.DeleteTuple(ctx, tk)
}

Expand Down Expand Up @@ -791,12 +791,12 @@ func (c *Checker) checkAction(ctx context.Context, checkAction Action, obj Entit
}
userName := checkUser.ID()
if c.checkGlobalAdmin(checkUser) {
log.Debug().Str("check_user", userName).Str("obj", obj.String()).Str("check_action", checkAction.String()).Msg("global admin action")
log.For(ctx).Debug().Str("check_user", userName).Str("obj", obj.String()).Str("check_action", checkAction.String()).Msg("global admin action")
return true, nil
}
checkTk := authz.NewTupleKey().WithUser(userName).WithObject(obj.Type, obj.Name).WithAction(checkAction)
ret, err := c.fgaClient.Check(ctx, checkTk, ctxtk...)
log.Trace().Str("tk", checkTk.String()).Bool("result", ret).Err(err).Msg("checkAction")
log.For(ctx).Trace().Str("tk", checkTk.String()).Bool("result", ret).Err(err).Msg("checkAction")
return ret, err
}

Expand Down Expand Up @@ -853,7 +853,7 @@ func getEntities[T hasId](ctx context.Context, db sqlx.Ext, ids []int64, table s
var t []T
q := sq.StatementBuilder.Select(cols...).From(table).Where(sq.Eq{"id": ids})
if err := dbutil.Select(ctx, db, q, &t); err != nil {
log.Trace().Err(err)
log.For(ctx).Trace().Err(err)
return nil, err
}
if err := checkIds(t, ids); err != nil {
Expand Down
73 changes: 37 additions & 36 deletions finders/azchecker/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package azchecker

import (
"context"
"encoding/json"
"io"
"io/ioutil"
Expand All @@ -21,15 +22,15 @@ func NewServer(checker authz.CheckerServer) (http.Handler, error) {

router.Get("/users", func(w http.ResponseWriter, r *http.Request) {
ret, err := checker.UserList(r.Context(), &authz.UserListRequest{Q: r.URL.Query().Get("q")})
handleJson(w, ret, err)
handleJson(r.Context(), w, ret, err)
})
router.Get("/users/{user_id}", func(w http.ResponseWriter, r *http.Request) {
ret, err := checker.User(r.Context(), &authz.UserRequest{Id: chi.URLParam(r, "user_id")})
handleJson(w, ret, err)
handleJson(r.Context(), w, ret, err)
})
router.Get("/me", func(w http.ResponseWriter, r *http.Request) {
ret, err := checker.Me(r.Context(), &authz.MeRequest{})
handleJson(w, ret, err)
handleJson(r.Context(), w, ret, err)
})

/////////////////
Expand All @@ -38,48 +39,48 @@ func NewServer(checker authz.CheckerServer) (http.Handler, error) {

router.Get("/tenants", func(w http.ResponseWriter, r *http.Request) {
ret, err := checker.TenantList(r.Context(), &authz.TenantListRequest{})
handleJson(w, ret, err)
handleJson(r.Context(), w, ret, err)
})
router.Get("/tenants/{tenant_id}", func(w http.ResponseWriter, r *http.Request) {
ret, err := checker.TenantPermissions(r.Context(), &authz.TenantRequest{Id: checkId(r, "tenant_id")})
handleJson(w, ret, err)
handleJson(r.Context(), w, ret, err)
})
router.Post("/tenants/{tenant_id}", func(w http.ResponseWriter, r *http.Request) {
check := authz.Tenant{}
if err := parseJson(r.Body, &check); err != nil {
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
return
}
check.Id = checkId(r, "tenant_id")
_, err := checker.TenantSave(r.Context(), &authz.TenantSaveRequest{Tenant: &check})
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
})
router.Post("/tenants/{tenant_id}/groups", func(w http.ResponseWriter, r *http.Request) {
check := authz.Group{}
if err := parseJson(r.Body, &check); err != nil {
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
return
}
_, err := checker.TenantCreateGroup(r.Context(), &authz.TenantCreateGroupRequest{Id: checkId(r, "tenant_id"), Group: &check})
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
})
router.Post("/tenants/{tenant_id}/permissions", func(w http.ResponseWriter, r *http.Request) {
check := &authz.EntityRelation{}
if err := parseJson(r.Body, check); err != nil {
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
}
entId := checkId(r, "tenant_id")
_, err := checker.TenantAddPermission(r.Context(), &authz.TenantModifyPermissionRequest{Id: entId, EntityRelation: check})
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
})
router.Delete("/tenants/{tenant_id}/permissions", func(w http.ResponseWriter, r *http.Request) {
check := &authz.EntityRelation{}
if err := parseJson(r.Body, check); err != nil {
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
}
entId := checkId(r, "tenant_id")
_, err := checker.TenantRemovePermission(r.Context(), &authz.TenantModifyPermissionRequest{Id: entId, EntityRelation: check})
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
})

/////////////////
Expand All @@ -88,49 +89,49 @@ func NewServer(checker authz.CheckerServer) (http.Handler, error) {

router.Get("/groups", func(w http.ResponseWriter, r *http.Request) {
ret, err := checker.GroupList(r.Context(), &authz.GroupListRequest{})
handleJson(w, ret, err)
handleJson(r.Context(), w, ret, err)
})
router.Post("/groups/{group_id}", func(w http.ResponseWriter, r *http.Request) {
check := authz.Group{}
if err := parseJson(r.Body, &check); err != nil {
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
return
}
check.Id = checkId(r, "group_id")
_, err := checker.GroupSave(r.Context(), &authz.GroupSaveRequest{Group: &check})
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
})
router.Get("/groups/{group_id}", func(w http.ResponseWriter, r *http.Request) {
ret, err := checker.GroupPermissions(r.Context(), &authz.GroupRequest{Id: checkId(r, "group_id")})
handleJson(w, ret, err)
handleJson(r.Context(), w, ret, err)
})
router.Post("/groups/{group_id}/permissions", func(w http.ResponseWriter, r *http.Request) {
check := &authz.EntityRelation{}
if err := parseJson(r.Body, check); err != nil {
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
}
entId := checkId(r, "group_id")
_, err := checker.GroupAddPermission(r.Context(), &authz.GroupModifyPermissionRequest{Id: entId, EntityRelation: check})
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
})
router.Delete("/groups/{group_id}/permissions", func(w http.ResponseWriter, r *http.Request) {
check := &authz.EntityRelation{}
if err := parseJson(r.Body, check); err != nil {
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
}
entId := checkId(r, "group_id")
_, err := checker.GroupRemovePermission(r.Context(), &authz.GroupModifyPermissionRequest{Id: entId, EntityRelation: check})
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
})
router.Post("/groups/{group_id}/tenant", func(w http.ResponseWriter, r *http.Request) {
check := authz.GroupSetTenantRequest{}
if err := parseJson(r.Body, &check); err != nil {
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
return
}
check.Id = checkId(r, "group_id")
_, err := checker.GroupSetTenant(r.Context(), &check)
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
})

/////////////////
Expand All @@ -139,21 +140,21 @@ func NewServer(checker authz.CheckerServer) (http.Handler, error) {

router.Get("/feeds", func(w http.ResponseWriter, r *http.Request) {
ret, err := checker.FeedList(r.Context(), &authz.FeedListRequest{})
handleJson(w, ret, err)
handleJson(r.Context(), w, ret, err)
})
router.Get("/feeds/{feed_id}", func(w http.ResponseWriter, r *http.Request) {
ret, err := checker.FeedPermissions(r.Context(), &authz.FeedRequest{Id: checkId(r, "feed_id")})
handleJson(w, ret, err)
handleJson(r.Context(), w, ret, err)
})
router.Post("/feeds/{feed_id}/group", func(w http.ResponseWriter, r *http.Request) {
check := authz.FeedSetGroupRequest{}
if err := parseJson(r.Body, &check); err != nil {
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
return
}
check.Id = checkId(r, "feed_id")
_, err := checker.FeedSetGroup(r.Context(), &check)
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
})

/////////////////
Expand All @@ -162,29 +163,29 @@ func NewServer(checker authz.CheckerServer) (http.Handler, error) {

router.Get("/feed_versions", func(w http.ResponseWriter, r *http.Request) {
ret, err := checker.FeedVersionList(r.Context(), &authz.FeedVersionListRequest{})
handleJson(w, ret, err)
handleJson(r.Context(), w, ret, err)
})
router.Get("/feed_versions/{feed_version_id}", func(w http.ResponseWriter, r *http.Request) {
ret, err := checker.FeedVersionPermissions(r.Context(), &authz.FeedVersionRequest{Id: checkId(r, "feed_version_id")})
handleJson(w, ret, err)
handleJson(r.Context(), w, ret, err)
})
router.Post("/feed_versions/{feed_version_id}/permissions", func(w http.ResponseWriter, r *http.Request) {
check := &authz.EntityRelation{}
if err := parseJson(r.Body, check); err != nil {
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
}
entId := checkId(r, "feed_version_id")
_, err := checker.FeedVersionAddPermission(r.Context(), &authz.FeedVersionModifyPermissionRequest{Id: entId, EntityRelation: check})
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
})
router.Delete("/feed_versions/{feed_version_id}/permissions", func(w http.ResponseWriter, r *http.Request) {
check := &authz.EntityRelation{}
if err := parseJson(r.Body, check); err != nil {
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
}
entId := checkId(r, "feed_version_id")
_, err := checker.FeedVersionRemovePermission(r.Context(), &authz.FeedVersionModifyPermissionRequest{Id: entId, EntityRelation: check})
handleJson(w, nil, err)
handleJson(r.Context(), w, nil, err)
})

return router, nil
Expand All @@ -198,13 +199,13 @@ func makeJsonError(msg string) string {
return string(jj)
}

func handleJson(w http.ResponseWriter, ret any, err error) {
func handleJson(ctx context.Context, w http.ResponseWriter, ret any, err error) {
if err == ErrUnauthorized {
log.Error().Err(err).Msg("unauthorized")
log.For(ctx).Error().Err(err).Msg("unauthorized")
http.Error(w, makeJsonError(http.StatusText(http.StatusUnauthorized)), http.StatusUnauthorized)
return
} else if err != nil {
log.Error().Err(err).Msg("admin api error")
log.For(ctx).Error().Err(err).Msg("admin api error")
http.Error(w, makeJsonError(http.StatusText(http.StatusInternalServerError)), http.StatusInternalServerError)
return
}
Expand Down
Loading

0 comments on commit 111045d

Please sign in to comment.