Skip to content
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

fix(kv): delete authorization from correct index bucket #16835

Merged
merged 3 commits into from
Feb 12, 2020
Merged
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
7 changes: 6 additions & 1 deletion kv/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,12 @@ func (s *Service) deleteAuthorization(ctx context.Context, tx Tx, id influxdb.ID
}
}

if err := idx.Delete([]byte(authByUserIndexKey(a.UserID, id))); err != nil {
uidx, err := authByUserIndexBucket(tx)
if err != nil {
return &influxdb.Error{Err: err}
}

if err := uidx.Delete([]byte(authByUserIndexKey(a.UserID, id))); err != nil {
return err
}

Expand Down
15 changes: 10 additions & 5 deletions kv/urm.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,16 @@ func (s *Service) findUserResourceMappingsByIndex(ctx context.Context, tx Tx, fi
}

for k, v := cursor.Next(); k != nil && v != nil; k, v = cursor.Next() {
v, err := bkt.Get(v)
nv, err := bkt.Get(v)
if err != nil {
return nil, err
return nil, &influxdb.Error{
Code: influxdb.ENotFound,
Err: err,
}
}

m := &influxdb.UserResourceMapping{}
if err := json.Unmarshal(v, m); err != nil {
if err := json.Unmarshal(nv, m); err != nil {
return nil, CorruptURMError(err)
}

Expand Down Expand Up @@ -531,11 +534,13 @@ func (s *Service) deleteOrgDependentMappings(ctx context.Context, tx Tx, m *infl
return err
}
for _, b := range bs {
if err := s.deleteUserResourceMapping(ctx, tx, influxdb.UserResourceMappingFilter{
filter := influxdb.UserResourceMappingFilter{
ResourceType: influxdb.BucketsResourceType,
ResourceID: b.ID,
UserID: m.UserID,
}); err != nil {
}

if err := s.deleteUserResourceMapping(ctx, tx, filter); err != nil {
if influxdb.ErrorCode(err) == influxdb.ENotFound {
s.log.Info("URM bucket is missing", zap.Stringer("orgID", m.ResourceID))
continue
Expand Down
38 changes: 37 additions & 1 deletion kv/urm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,27 @@ func initUserResourceMappingService(s kv.Store, f influxdbtesting.UserResourceFi
t.Fatalf("error initializing urm service: %v", err)
}

for _, o := range f.Organizations {
if err := svc.CreateOrganization(ctx, o); err != nil {
t.Fatalf("failed to create org %q", err)
}
}

for _, u := range f.Users {
if err := svc.CreateUser(ctx, u); err != nil {
t.Fatalf("failed to create user %q", err)
}
}

for _, b := range f.Buckets {
if err := svc.PutBucket(ctx, b); err != nil {
t.Fatalf("failed to create bucket %q", err)
}
}

for _, m := range f.UserResourceMappings {
if err := svc.CreateUserResourceMapping(ctx, m); err != nil {
t.Fatalf("failed to populate mappings")
t.Fatalf("failed to populate mappings %q", err)
}
}

Expand All @@ -61,6 +79,24 @@ func initUserResourceMappingService(s kv.Store, f influxdbtesting.UserResourceFi
t.Logf("failed to remove user resource mapping: %v", err)
}
}

for _, b := range f.Buckets {
if err := svc.DeleteBucket(ctx, b.ID); err != nil {
t.Logf("failed to delete org", err)
}
}

for _, u := range f.Users {
if err := svc.DeleteUser(ctx, u.ID); err != nil {
t.Fatalf("failed to delete user %q", err)
}
}

for _, o := range f.Organizations {
if err := svc.DeleteOrganization(ctx, o.ID); err != nil {
t.Logf("failed to delete org", err)
}
}
}
}

Expand Down
43 changes: 43 additions & 0 deletions testing/user_resource_mapping_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ var mappingCmpOptions = cmp.Options{

// UserResourceFields includes prepopulated data for mapping tests
type UserResourceFields struct {
Organizations []*platform.Organization
Users []*platform.User
Buckets []*platform.Bucket
UserResourceMappings []*platform.UserResourceMapping
}

Expand Down Expand Up @@ -243,6 +246,46 @@ func DeleteUserResourceMapping(
err: fmt.Errorf("user to resource mapping not found"),
},
},
{
name: "delete user resource mapping for org",
fields: UserResourceFields{
Organizations: []*platform.Organization{
{
ID: MustIDBase16(orgOneID),
Name: "organization1",
},
},
Users: []*platform.User{
{
ID: MustIDBase16(userOneID),
Name: "user1",
},
},
Buckets: []*platform.Bucket{
{
ID: MustIDBase16(bucketOneID),
Name: "bucket1",
OrgID: MustIDBase16(orgOneID),
},
},
UserResourceMappings: []*platform.UserResourceMapping{
{
ResourceID: MustIDBase16(orgOneID),
ResourceType: platform.OrgsResourceType,
MappingType: platform.UserMappingType,
UserID: MustIDBase16(userOneID),
UserType: platform.Member,
},
},
},
args: args{
resourceID: MustIDBase16(orgOneID),
userID: MustIDBase16(userOneID),
},
wants: wants{
mappings: []*platform.UserResourceMapping{},
},
},
}

for _, tt := range tests {
Expand Down