Skip to content

Commit

Permalink
fix: org filtering for both kv and tenant need to match behaviors.
Browse files Browse the repository at this point in the history
  • Loading branch information
lyondhill committed Apr 16, 2020
1 parent 94ae519 commit 874f520
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 4 deletions.
13 changes: 13 additions & 0 deletions authorizer/org.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/influxdata/influxdb/v2"
icontext "github.com/influxdata/influxdb/v2/context"
)

var _ influxdb.OrganizationService = (*OrgService)(nil)
Expand Down Expand Up @@ -43,6 +44,18 @@ func (s *OrgService) FindOrganization(ctx context.Context, filter influxdb.Organ

// FindOrganizations retrieves all organizations that match the provided filter and then filters the list down to only the resources that are authorized.
func (s *OrgService) FindOrganizations(ctx context.Context, filter influxdb.OrganizationFilter, opt ...influxdb.FindOptions) ([]*influxdb.Organization, int, error) {
if filter.Name == nil && filter.ID == nil && filter.UserID == nil {
// if the user doesnt have permission to look up all orgs we need to add this users id to the filter to save lookup time
auth, err := icontext.GetAuthorizer(ctx)
if err != nil {
return nil, 0, err
}
if _, _, err := AuthorizeReadGlobal(ctx, influxdb.OrgsResourceType); err != nil {
userid := auth.GetUserID()
filter.UserID = &userid
}
}

// TODO: we'll likely want to push this operation into the database eventually since fetching the whole list of data
// will likely be expensive.
os, _, err := s.s.FindOrganizations(ctx, filter, opt...)
Expand Down
9 changes: 9 additions & 0 deletions http/org_service.go
Expand Up @@ -257,6 +257,15 @@ func (h *OrgHandler) handleGetOrgs(w http.ResponseWriter, r *http.Request) {
filter.ID = id
}

if userID := qp.Get("userID"); userID != "" {
id, err := influxdb.IDFromString(userID)
if err != nil {
h.API.Err(w, err)
return
}
filter.UserID = id
}

orgs, _, err := h.OrgSVC.FindOrganizations(r.Context(), filter)
if err != nil {
h.API.Err(w, err)
Expand Down
5 changes: 5 additions & 0 deletions http/swagger.yml
Expand Up @@ -3751,6 +3751,11 @@ paths:
schema:
type: string
description: Filter organizations to a specific organization ID.
- in: query
name: userID
schema:
type: string
description: Filter organizations to a specific user ID.
responses:
'200':
description: A list of organizations
Expand Down
23 changes: 23 additions & 0 deletions kv/org.go
Expand Up @@ -236,6 +236,29 @@ func (s *Service) FindOrganizations(ctx context.Context, filter influxdb.Organiz
}

os := []*influxdb.Organization{}

if filter.UserID != nil {
// find urms for orgs with this user
urms, _, err := s.FindUserResourceMappings(ctx, influxdb.UserResourceMappingFilter{
UserID: *filter.UserID,
ResourceType: influxdb.OrgsResourceType,
}, opt...)

if err != nil {
return nil, 0, err
}
// find orgs by the urm's resource ids.
for _, urm := range urms {
o, err := s.FindOrganizationByID(ctx, urm.ResourceID)
if err == nil {
// if there is an error then this is a crufty urm and we should just move on
os = append(os, o)
}
}

return os, len(os), nil
}

filterFn := filterOrganizationsFn(filter)
err := s.kv.View(ctx, func(tx Tx) error {
return forEachOrganization(ctx, tx, func(o *influxdb.Organization) bool {
Expand Down
5 changes: 3 additions & 2 deletions organization.go
Expand Up @@ -71,8 +71,9 @@ var ErrInvalidOrgFilter = &Error{

// OrganizationFilter represents a set of filter that restrict the returned results.
type OrganizationFilter struct {
Name *string
ID *ID
Name *string
ID *ID
UserID *ID
}

func ErrInternalOrgServiceError(op string, err error) *Error {
Expand Down
7 changes: 7 additions & 0 deletions tenant/http_server_org.go
Expand Up @@ -142,6 +142,13 @@ func (h *OrgHandler) handleGetOrgs(w http.ResponseWriter, r *http.Request) {
}
}

if id := qp.Get("userID"); id != "" {
i, err := influxdb.IDFromString(id)
if err == nil {
filter.UserID = i
}
}

orgs, _, err := h.orgSvc.FindOrganizations(r.Context(), filter)
if err != nil {
h.api.Err(w, err)
Expand Down
15 changes: 13 additions & 2 deletions tenant/middleware_org_auth.go
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/authorizer"
icontext "github.com/influxdata/influxdb/v2/context"
)

var _ influxdb.OrganizationService = (*AuthedOrgService)(nil)
Expand Down Expand Up @@ -46,8 +47,18 @@ func (s *AuthedOrgService) FindOrganization(ctx context.Context, filter influxdb

// FindOrganizations retrieves all organizations that match the provided filter and then filters the list down to only the resources that are authorized.
func (s *AuthedOrgService) FindOrganizations(ctx context.Context, filter influxdb.OrganizationFilter, opt ...influxdb.FindOptions) ([]*influxdb.Organization, int, error) {
// TODO: we'll likely want to push this operation into the database eventually since fetching the whole list of data
// will likely be expensive.
if filter.Name == nil && filter.ID == nil && filter.UserID == nil {
// if the user doesnt have permission to look up all orgs we need to add this users id to the filter to save lookup time
auth, err := icontext.GetAuthorizer(ctx)
if err != nil {
return nil, 0, err
}
if _, _, err := authorizer.AuthorizeReadGlobal(ctx, influxdb.OrgsResourceType); err != nil {
userid := auth.GetUserID()
filter.UserID = &userid
}
}

os, _, err := s.s.FindOrganizations(ctx, filter, opt...)
if err != nil {
return nil, 0, err
Expand Down
22 changes: 22 additions & 0 deletions tenant/service_org.go
Expand Up @@ -67,6 +67,28 @@ func (s *Service) FindOrganizations(ctx context.Context, filter influxdb.Organiz
}

var orgs []*influxdb.Organization

if filter.UserID != nil {
// find urms for orgs with this user
urms, _, err := s.FindUserResourceMappings(ctx, influxdb.UserResourceMappingFilter{
UserID: *filter.UserID,
ResourceType: influxdb.OrgsResourceType,
}, opt...)
if err != nil {
return nil, 0, err
}
// find orgs by the urm's resource ids.
for _, urm := range urms {
o, err := s.FindOrganizationByID(ctx, urm.ResourceID)
if err == nil {
// if there is an error then this is a crufty urm and we should just move on
orgs = append(orgs, o)
}
}

return orgs, len(orgs), nil
}

err := s.store.View(ctx, func(tx kv.Tx) error {
os, err := s.store.ListOrgs(ctx, tx, opt...)
if err != nil {
Expand Down

0 comments on commit 874f520

Please sign in to comment.