Skip to content

[WIP] Concept for whoami server and improved admin check #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions actions/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,24 @@ func fetchCheckFeed(ctx context.Context, feedId string) (*model.Feed, error) {
feed := feeds[0]

// Check feed permissions
if whoami := cfg.Whoami; whoami == nil {
// Not ok
} else if me, err := whoami.Me(ctx, &authz.MeRequest{}); err != nil {
// Not ok
} else if me.IsGlobalAdmin {
// OK
return feed, nil
}

if checker := cfg.Checker; checker == nil {
// pass
// Not ok
} else if check, err := checker.FeedPermissions(ctx, &authz.FeedRequest{Id: int64(feed.ID)}); err != nil {
return nil, err
} else if !check.Actions.CanCreateFeedVersion {
return nil, errors.New("unauthorized")
// Not ok
} else if check.Actions.CanCreateFeedVersion {
// OK
return feed, nil
}
return feed, nil

// Failed
return nil, authz.ErrUnauthorized
}
1 change: 0 additions & 1 deletion actions/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func TestStaticFetchWorker(t *testing.T) {
// Setup job
feedUrl := ts.URL + "/" + tc.serveFile
testconfig.ConfigTxRollback(t, testconfig.Options{}, func(cfg model.Config) {
cfg.Checker = nil // disable checker for this test
ctx := model.WithConfig(context.Background(), cfg)
// Run job
if result, err := StaticFetch(ctx, tc.feedId, nil, feedUrl); err != nil && !tc.expectError {
Expand Down
7 changes: 6 additions & 1 deletion cmd/tlserver/server_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/interline-io/transitland-lib/tldb"
"github.com/interline-io/transitland-mw/auth/ancheck"
"github.com/interline-io/transitland-mw/auth/authn"
"github.com/interline-io/transitland-mw/auth/azcheck"
"github.com/interline-io/transitland-server/finders/dbfinder"
"github.com/interline-io/transitland-server/finders/gbfsfinder"
"github.com/interline-io/transitland-server/finders/rtfinder"
Expand Down Expand Up @@ -127,11 +128,15 @@ func (cmd *Command) Run() error {
gbfsFinder = gbfsfinder.NewFinder(nil)
}

// Create default Whoami checker
whoami := &azcheck.Whoami{}

// Setup config
cfg := model.Config{
Finder: dbFinder,
RTFinder: rtFinder,
GbfsFinder: gbfsFinder,
Whoami: whoami,
Secrets: cmd.secrets,
Storage: cmd.Storage,
RTStorage: cmd.RTStorage,
Expand All @@ -152,7 +157,7 @@ func (cmd *Command) Run() error {
root.Use(model.AddConfig(cfg))

// This server only supports admin access
root.Use(ancheck.AdminDefaultMiddleware("admin"))
root.Use(ancheck.AdminDefaultMiddleware(azcheck.GLOBALADMIN_ROLE))

// Add logging middleware - must be after auth
root.Use(log.LoggingMiddleware(cmd.LongQueryDuration, func(ctx context.Context) string {
Expand Down
1 change: 1 addition & 0 deletions finders/dbfinder/agency_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func AgencySelect(limit *int, after *model.Cursor, ids []int, active bool, permF
Join("feed_states fsp on fsp.feed_id = current_feeds.id").
Where(sq.Or{
sq.Expr("fsp.public = true"),
sq.Eq{"true": permFilter.IsGlobalAdmin()},
sq.Eq{"fsp.feed_id": permFilter.GetAllowedFeeds()},
sq.Eq{"feed_versions.id": permFilter.GetAllowedFeedVersions()},
})
Expand Down
24 changes: 19 additions & 5 deletions finders/dbfinder/entity_mutations.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,26 @@ func checkFeedEdit(ctx context.Context, fvid int) error {
return errors.New("invalid feed version id")
}
cfg := model.ForContext(ctx)
if checker := cfg.Checker; checker == nil {

// Check feed permissions
if whoami := cfg.Whoami; whoami == nil {
// Not ok
} else if me, err := whoami.Me(ctx, &authz.MeRequest{}); err != nil {
// Not ok
} else if me.IsGlobalAdmin {
// OK
return nil
}

if checker := cfg.Checker; checker == nil {
// Not ok
} else if check, err := checker.FeedVersionPermissions(ctx, &authz.FeedVersionRequest{Id: int64(fvid)}); err != nil {
return err
} else if !check.Actions.CanEdit {
return authz.ErrUnauthorized
// Not ok
} else if check.Actions.CanEdit {
// OK
return nil
}
return nil

// Failed
return authz.ErrUnauthorized
}
1 change: 1 addition & 0 deletions finders/dbfinder/feed_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func FeedSelect(limit *int, after *model.Cursor, ids []int, permFilter *model.Pe
Join("feed_states fsp on fsp.feed_id = current_feeds.id").
Where(sq.Or{
sq.Expr("fsp.public = true"),
sq.Eq{"true": permFilter.IsGlobalAdmin()},
sq.Eq{"fsp.feed_id": permFilter.GetAllowedFeeds()},
})

Expand Down
1 change: 1 addition & 0 deletions finders/dbfinder/feed_version_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func FeedVersionSelect(limit *int, after *model.Cursor, ids []int, permFilter *m
Join("feed_states fsp on fsp.feed_id = current_feeds.id").
Where(sq.Or{
sq.Expr("fsp.public = true"),
sq.Eq{"true": permFilter.IsGlobalAdmin()},
sq.Eq{"fsp.feed_id": permFilter.GetAllowedFeeds()},
sq.Eq{"feed_versions.id": permFilter.GetAllowedFeedVersions()},
})
Expand Down
36 changes: 19 additions & 17 deletions finders/dbfinder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ func (f *Finder) LoadAdmins() error {
}

func (f *Finder) PermFilter(ctx context.Context) *model.PermFilter {
permFilter, _ := checkActive(ctx)
permFilter, err := checkActive(ctx)
if err != nil {
log.Error().Err(err).Msg("failed to get permissions filter for context")
}
return permFilter
}

Expand Down Expand Up @@ -1843,36 +1846,35 @@ func convertEnts[A any, B any](vals [][]A, fn func(a A) B) [][]B {
return ret
}

type canCheckGlobalAdmin interface {
CheckGlobalAdmin(context.Context) (bool, error)
}

func checkActive(ctx context.Context) (*model.PermFilter, error) {
checker := model.ForContext(ctx).Checker
active := &model.PermFilter{}
if checker == nil {

// Check if we are a global admin
whoami := model.ForContext(ctx).Whoami
if whoami == nil {
return active, nil
}

// TODO: Make this part of actual checker interface
if c, ok := checker.(canCheckGlobalAdmin); ok {
if a, err := c.CheckGlobalAdmin(ctx); err != nil {
return nil, err
} else if a {
return nil, nil
}
me, err := whoami.Me(ctx, &authz.MeRequest{})
if err != nil {
return active, err
}
active.GlobalAdmin = me.IsGlobalAdmin

// Check for additional permissions
checker := model.ForContext(ctx).Checker
if checker == nil {
return active, nil
}
okFeeds, err := checker.FeedList(ctx, &authz.FeedListRequest{})
if err != nil {
return nil, err
return active, err
}
for _, feed := range okFeeds.Feeds {
active.AllowedFeeds = append(active.AllowedFeeds, int(feed.Id))
}
okFvids, err := checker.FeedVersionList(ctx, &authz.FeedVersionListRequest{})
if err != nil {
return nil, err
return active, err
}
for _, fv := range okFvids.FeedVersions {
active.AllowedFeedVersions = append(active.AllowedFeedVersions, int(fv.Id))
Expand Down
1 change: 1 addition & 0 deletions finders/dbfinder/operator_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func OperatorSelect(limit *int, after *model.Cursor, ids []int, permFilter *mode
Join("feed_states fsp on fsp.feed_id = coif.feed_id").
Where(sq.Or{
sq.Expr("fsp.public = true"),
sq.Eq{"true": permFilter.IsGlobalAdmin()},
sq.Eq{"fsp.feed_id": permFilter.GetAllowedFeeds()},
})

Expand Down
1 change: 1 addition & 0 deletions finders/dbfinder/pathway_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func PathwaySelect(limit *int, after *model.Cursor, ids []int, permFilter *model
Join("feed_states fsp on fsp.feed_id = current_feeds.id").
Where(sq.Or{
sq.Expr("fsp.public = true"),
sq.Eq{"true": permFilter.IsGlobalAdmin()},
sq.Eq{"fsp.feed_id": permFilter.GetAllowedFeeds()},
sq.Eq{"feed_versions.id": permFilter.GetAllowedFeedVersions()},
})
Expand Down
1 change: 1 addition & 0 deletions finders/dbfinder/place_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func PlaceSelect(limit *int, after *model.Cursor, ids []int, level *model.PlaceA
q = q.
Where(sq.Or{
sq.Expr("feed_states.public = true"),
sq.Eq{"true": permFilter.IsGlobalAdmin()},
sq.Eq{"feed_states.feed_id": permFilter.GetAllowedFeeds()},
sq.Eq{"feed_states.feed_version_id": permFilter.GetAllowedFeedVersions()},
})
Expand Down
1 change: 1 addition & 0 deletions finders/dbfinder/route_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func RouteSelect(limit *int, after *model.Cursor, ids []int, active bool, permFi
Join("feed_states fsp on fsp.feed_id = current_feeds.id").
Where(sq.Or{
sq.Expr("fsp.public = true"),
sq.Eq{"true": permFilter.IsGlobalAdmin()},
sq.Eq{"fsp.feed_id": permFilter.GetAllowedFeeds()},
sq.Eq{"feed_versions.id": permFilter.GetAllowedFeedVersions()},
})
Expand Down
1 change: 1 addition & 0 deletions finders/dbfinder/stop_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func StopSelect(limit *int, after *model.Cursor, ids []int, active bool, permFil
Join("feed_states fsp on fsp.feed_id = current_feeds.id").
Where(sq.Or{
sq.Expr("fsp.public = true"),
sq.Eq{"true": permFilter.IsGlobalAdmin()},
sq.Eq{"fsp.feed_id": permFilter.GetAllowedFeeds()},
sq.Eq{"feed_versions.id": permFilter.GetAllowedFeedVersions()},
})
Expand Down
1 change: 1 addition & 0 deletions finders/dbfinder/trip_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func TripSelect(limit *int, after *model.Cursor, ids []int, active bool, permFil
Join("feed_states fsp on fsp.feed_id = current_feeds.id").
Where(sq.Or{
sq.Expr("fsp.public = true"),
sq.Eq{"true": permFilter.IsGlobalAdmin()},
sq.Eq{"fsp.feed_id": permFilter.GetAllowedFeeds()},
sq.Eq{"feed_versions.id": permFilter.GetAllowedFeedVersions()},
})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,6 @@ require (
)

// replace github.com/interline-io/transitland-lib => /Users/irees/src/interline-io/transitland-lib
// replace github.com/interline-io/transitland-mw => /Users/irees/src/interline-io/transitland-mw
replace github.com/interline-io/transitland-mw => /Users/irees/src/interline-io/transitland-mw
// replace github.com/interline-io/log => /Users/irees/src/interline-io/log
// replace github.com/interline-io/transitland-dbutil => /Users/irees/src/interline-io/transitland-dbutil
8 changes: 7 additions & 1 deletion internal/testconfig/testconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,21 @@ func newTestConfig(t testing.TB, db sqlx.Ext, opts Options) model.Config {

// Setup Checker
var checker model.Checker
var whoami model.Whoami
if opts.FGAEndpoint != "" {
checkerCfg := azcheck.CheckerConfig{
FGAEndpoint: opts.FGAEndpoint,
FGALoadModelFile: opts.FGAModelFile,
FGALoadTestData: opts.FGAModelTuples,
}
checker, err = azcheck.NewCheckerFromConfig(checkerCfg, db)
ch, err := azcheck.NewCheckerFromConfig(checkerCfg, db)
if err != nil {
t.Fatal(err)
}
checker = ch
whoami = ch
} else {
whoami = &azcheck.Whoami{}
}

// Setup DB
Expand Down Expand Up @@ -144,6 +149,7 @@ func newTestConfig(t testing.TB, db sqlx.Ext, opts Options) model.Config {
Finder: dbf,
RTFinder: rtf,
GbfsFinder: gbf,
Whoami: whoami,
Checker: checker,
JobQueue: jobQueue,
Clock: cl,
Expand Down
1 change: 1 addition & 0 deletions model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Config struct {
Finder Finder
RTFinder RTFinder
GbfsFinder GbfsFinder
Whoami Whoami
Checker Checker
JobQueue jobs.JobQueue
Clock clock.Clock
Expand Down
8 changes: 8 additions & 0 deletions model/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type PermFilter struct {
AllowedFeeds []int
AllowedFeedVersions []int
GlobalAdmin bool
}

func (pf *PermFilter) GetAllowedFeeds() []int {
Expand All @@ -29,6 +30,13 @@ func (pf *PermFilter) GetAllowedFeedVersions() []int {
return pf.AllowedFeedVersions
}

func (pf *PermFilter) IsGlobalAdmin() bool {
if pf == nil {
return false
}
return pf.GlobalAdmin
}

type Cursor struct {
FeedVersionID int
ID int
Expand Down
4 changes: 4 additions & 0 deletions model/finders.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,7 @@ type GbfsFinder interface {
type Checker interface {
authz.CheckerServer
}

type Whoami interface {
authz.WhoamiServer
}
6 changes: 3 additions & 3 deletions server/gql/query_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func (r *queryResolver) Me(ctx context.Context) (*model.Me, error) {
cfg := model.ForContext(ctx)
me := model.Me{}
me.ExternalData = tt.NewMap(map[string]any{})
if checker := cfg.Checker; checker != nil {
// Use checker if available
cm, err := checker.Me(ctx, &authz.MeRequest{})
if whoami := cfg.Whoami; whoami != nil {
// Use whoami checker if available
cm, err := whoami.Me(ctx, &authz.MeRequest{})
if err != nil {
return nil, err
}
Expand Down
Loading