Skip to content

Commit

Permalink
fix: auth requests use org and user names if present (influxdata#22272)
Browse files Browse the repository at this point in the history
* fix: auth requests use org and user names if present

* chore: update CHANGELOG
  • Loading branch information
williamhbaker authored Aug 23, 2021
1 parent 467040a commit 46ade4d
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ This release adds an embedded SQLite database for storing metadata required by t
1. [#22228](https://github.com/influxdata/influxdb/pull/22228): influxdb2 packages should depend on curl
1. [#22211](https://github.com/influxdata/influxdb/pull/22211): Prevent scheduling an inactivated tasks after updating it
1. [#22235](https://github.com/influxdata/influxdb/pull/22235): Avoid compaction queue stats flutter
1. [#22272](https://github.com/influxdata/influxdb/pull/22272): Requests to `/api/v2/authorizations` filter correctly on `org` and `user` parameters

## v2.0.7 [2021-06-04]

Expand Down
22 changes: 11 additions & 11 deletions authorization/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,17 +391,9 @@ func (h *AuthHandler) handleGetAuthorizations(w http.ResponseWriter, r *http.Req
return
}

opts := influxdb.FindOptions{}
as, _, err := h.authSvc.FindAuthorizations(ctx, req.filter, opts)

if err != nil {
h.api.Err(w, r, err)
return
}

f := req.filter
// If the user or org name was provided, look up the ID first
if f.User != nil {
// Look up user ID and org ID if they were not provided, but names were
if f.UserID == nil && f.User != nil {
u, err := h.tenantService.FindUser(ctx, influxdb.UserFilter{Name: f.User})
if err != nil {
h.api.Err(w, r, err)
Expand All @@ -410,7 +402,7 @@ func (h *AuthHandler) handleGetAuthorizations(w http.ResponseWriter, r *http.Req
f.UserID = &u.ID
}

if f.Org != nil {
if f.OrgID == nil && f.Org != nil {
o, err := h.tenantService.FindOrganization(ctx, influxdb.OrganizationFilter{Name: f.Org})
if err != nil {
h.api.Err(w, r, err)
Expand All @@ -419,6 +411,14 @@ func (h *AuthHandler) handleGetAuthorizations(w http.ResponseWriter, r *http.Req
f.OrgID = &o.ID
}

opts := influxdb.FindOptions{}
as, _, err := h.authSvc.FindAuthorizations(ctx, f, opts)

if err != nil {
h.api.Err(w, r, err)
return
}

auths := make([]*authResponse, 0, len(as))
for _, a := range as {
ps, err := h.newPermissionsResponse(ctx, a.Permissions)
Expand Down
50 changes: 50 additions & 0 deletions authorization/http_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/influxdb/v2/kit/platform/errors"
"github.com/stretchr/testify/require"

"github.com/go-chi/chi"
"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -394,6 +395,55 @@ func TestService_handleGetAuthorization(t *testing.T) {
}
}

func TestGetAuthorizationsWithNames(t *testing.T) {
t.Parallel()

testUserName := "user"
testUserID := itesting.MustIDBase16("6c7574652c206f6e")
testOrgName := "org"
testOrgID := itesting.MustIDBase16("9d70616e656d2076")

ts := &tenantService{
FindUserFn: func(ctx context.Context, f influxdb.UserFilter) (*influxdb.User, error) {
require.Equal(t, &testUserName, f.Name)

return &influxdb.User{
ID: testUserID,
Name: testUserName,
}, nil
},

FindOrganizationF: func(ctx context.Context, f influxdb.OrganizationFilter) (*influxdb.Organization, error) {
require.Equal(t, &testOrgName, f.Name)

return &influxdb.Organization{
ID: testOrgID,
Name: testOrgName,
}, nil
},
}

as := &mock.AuthorizationService{
FindAuthorizationsFn: func(ctx context.Context, f influxdb.AuthorizationFilter, opts ...influxdb.FindOptions) ([]*influxdb.Authorization, int, error) {
require.Equal(t, &testOrgID, f.OrgID)
require.Equal(t, &testUserID, f.UserID)

return []*influxdb.Authorization{}, 0, nil
},
}

h := NewHTTPAuthHandler(zaptest.NewLogger(t), as, ts)

w := httptest.NewRecorder()
r := httptest.NewRequest("get", "http://any.url", nil)
qp := r.URL.Query()
qp.Add("user", testUserName)
qp.Add("org", testOrgName)
r.URL.RawQuery = qp.Encode()

h.handleGetAuthorizations(w, r)
}

func TestService_handleGetAuthorizations(t *testing.T) {
type fields struct {
AuthorizationService influxdb.AuthorizationService
Expand Down

0 comments on commit 46ade4d

Please sign in to comment.