Skip to content

Commit 2d93a34

Browse files
authored
fix(membership): revoke a removed member's PATs in the org (#2048)
1 parent 885fc0f commit 2d93a34

7 files changed

Lines changed: 437 additions & 8 deletions

File tree

‎core/membership/mocks/user_pat_service.go‎

Lines changed: 49 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎core/membership/org.go‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,19 @@ func (s *Service) SetPATAllProjectsRole(ctx context.Context, orgID, patID, roleI
205205
// relations. Returns ErrNotMember if the principal has no policies on this org,
206206
// and ErrLastOwnerRole when removing the org's last owner.
207207
func (s *Service) RemoveOrganizationMember(ctx context.Context, orgID, principalID, principalType string) error {
208-
return s.removeOrganizationMember(ctx, orgID, principalID, principalType, false)
208+
removeErr := s.removeOrganizationMember(ctx, orgID, principalID, principalType, false)
209+
if removeErr != nil && !errors.Is(removeErr, ErrNotMember) {
210+
return removeErr
211+
}
212+
if patErr := s.revokeMemberPATs(ctx, orgID, principalID, principalType); patErr != nil {
213+
return patErr
214+
}
215+
return removeErr
209216
}
210217

211218
// RemoveOrganizationMemberOnDelete is RemoveOrganizationMember for deleting a
212219
// user. It also works on a disabled org, because the user has to leave every
213-
// org before the account can go.
220+
// org before the account can go. It leaves PATs to the user delete.
214221
func (s *Service) RemoveOrganizationMemberOnDelete(ctx context.Context, orgID, principalID, principalType string) error {
215222
return s.removeOrganizationMember(ctx, orgID, principalID, principalType, true)
216223
}
@@ -392,6 +399,23 @@ func (s *Service) removeCustomResourceAccess(ctx context.Context, principalID, p
392399
return nil
393400
}
394401

402+
func (s *Service) revokeMemberPATs(ctx context.Context, orgID, principalID, principalType string) error {
403+
if principalType != schema.UserPrincipal {
404+
return nil
405+
}
406+
if s.userPATService == nil {
407+
s.log.ErrorContext(ctx, "user PAT service not set: PATs left behind",
408+
"org_id", orgID,
409+
"principal_id", principalID,
410+
)
411+
return nil
412+
}
413+
if err := s.userPATService.OnOrgMemberRemoved(ctx, orgID, principalID); err != nil {
414+
return fmt.Errorf("revoke member PATs: %w", err)
415+
}
416+
return nil
417+
}
418+
395419
// validateMinOwnerConstraint ensures the org always has at least one owner after a role change.
396420
// Returns the resolved owner role ID for reuse by callers.
397421
func (s *Service) validateMinOwnerConstraint(ctx context.Context, orgID, newRoleID string, existing []policy.Policy) (string, error) {

‎core/membership/org_test.go‎

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,12 +744,14 @@ func TestService_RemoveOrganizationMember(t *testing.T) {
744744
projSvc *mocks.ProjectService
745745
grpSvc *mocks.GroupService
746746
resSvc *mocks.ResourceService
747+
patSvc *mocks.UserPATService
747748
auditRepo *mocks.AuditRecordRepository
748749
}
749750

750751
tests := []struct {
751752
name string
752753
principalType string
754+
noPATService bool
753755
setup func(d testDeps)
754756
wantErr error
755757
wantErrContain string
@@ -775,13 +777,23 @@ func TestService_RemoveOrganizationMember(t *testing.T) {
775777
wantErr: organization.ErrDisabled,
776778
},
777779
{
778-
name: "should return ErrNotMember if principal has no org policies",
780+
name: "should revoke leftover PATs and return ErrNotMember if principal has no org policies",
779781
setup: func(d testDeps) {
780782
d.orgSvc.EXPECT().Get(ctx, orgID).Return(enabledOrg, nil)
781783
d.policySvc.EXPECT().List(ctx, policy.Filter{OrgID: orgID, PrincipalID: userID, PrincipalType: schema.UserPrincipal}).Return([]policy.Policy{}, nil)
784+
d.patSvc.EXPECT().OnOrgMemberRemoved(ctx, orgID, userID).Return(nil)
782785
},
783786
wantErr: membership.ErrNotMember,
784787
},
788+
{
789+
name: "should return error if revoking leftover PATs fails for a principal with no org policies",
790+
setup: func(d testDeps) {
791+
d.orgSvc.EXPECT().Get(ctx, orgID).Return(enabledOrg, nil)
792+
d.policySvc.EXPECT().List(ctx, policy.Filter{OrgID: orgID, PrincipalID: userID, PrincipalType: schema.UserPrincipal}).Return([]policy.Policy{}, nil)
793+
d.patSvc.EXPECT().OnOrgMemberRemoved(ctx, orgID, userID).Return(errors.New("pat boom"))
794+
},
795+
wantErrContain: "revoke member PATs",
796+
},
785797
{
786798
name: "should return ErrLastOwnerRole when removing the last owner",
787799
setup: func(d testDeps) {
@@ -827,6 +839,7 @@ func TestService_RemoveOrganizationMember(t *testing.T) {
827839
d.policySvc.EXPECT().Delete(ctx, "org-p1").Return(nil)
828840
d.resSvc.EXPECT().RemovePrincipalAccess(ctx, userID, schema.UserPrincipal, orgID, []string{}).Return(nil)
829841
d.auditRepo.EXPECT().Create(ctx, mock.Anything).Return(auditrecord.AuditRecord{}, nil)
842+
d.patSvc.EXPECT().OnOrgMemberRemoved(ctx, orgID, userID).Return(nil)
830843
},
831844
wantErr: nil,
832845
},
@@ -851,6 +864,7 @@ func TestService_RemoveOrganizationMember(t *testing.T) {
851864
d.resSvc.EXPECT().RemovePrincipalAccess(ctx, userID, schema.UserPrincipal, orgID, []string{projectID}).Return(nil)
852865
d.relSvc.EXPECT().Delete(ctx, relation.Relation{Object: grpObj, Subject: userSub, RelationName: schema.MemberRelationName}).Return(relation.ErrNotExist)
853866
d.auditRepo.EXPECT().Create(ctx, mock.Anything).Return(auditrecord.AuditRecord{}, nil)
867+
d.patSvc.EXPECT().OnOrgMemberRemoved(ctx, orgID, userID).Return(nil)
854868
},
855869
wantErr: nil,
856870
},
@@ -872,6 +886,7 @@ func TestService_RemoveOrganizationMember(t *testing.T) {
872886
d.policySvc.EXPECT().Delete(ctx, "org-p1").Return(nil)
873887
d.resSvc.EXPECT().RemovePrincipalAccess(ctx, userID, schema.UserPrincipal, orgID, []string{}).Return(nil)
874888
d.auditRepo.EXPECT().Create(ctx, mock.Anything).Return(auditrecord.AuditRecord{}, nil)
889+
d.patSvc.EXPECT().OnOrgMemberRemoved(ctx, orgID, userID).Return(nil)
875890
},
876891
wantErr: nil,
877892
},
@@ -925,6 +940,7 @@ func TestService_RemoveOrganizationMember(t *testing.T) {
925940
d.policySvc.EXPECT().Delete(ctx, "org-p1").Return(nil)
926941
d.resSvc.EXPECT().RemovePrincipalAccess(ctx, userID, schema.UserPrincipal, orgID, []string{projectID, secondProjectID}).Return(nil)
927942
d.auditRepo.EXPECT().Create(ctx, mock.Anything).Return(auditrecord.AuditRecord{}, nil)
943+
d.patSvc.EXPECT().OnOrgMemberRemoved(ctx, orgID, userID).Return(nil)
928944
},
929945
wantErr: nil,
930946
},
@@ -962,6 +978,59 @@ func TestService_RemoveOrganizationMember(t *testing.T) {
962978
},
963979
wantErr: membership.ErrLastOwnerRole,
964980
},
981+
{
982+
name: "should return error if PAT revocation fails after the member is removed",
983+
setup: func(d testDeps) {
984+
d.orgSvc.EXPECT().Get(ctx, orgID).Return(enabledOrg, nil)
985+
d.policySvc.EXPECT().List(ctx, policy.Filter{OrgID: orgID, PrincipalID: userID, PrincipalType: schema.UserPrincipal}).Return([]policy.Policy{{ID: "org-p1", RoleID: viewerRoleID}}, nil)
986+
d.roleSvc.EXPECT().Get(ctx, schema.RoleOrganizationOwner).Return(role.Role{ID: ownerRoleID, Name: schema.RoleOrganizationOwner}, nil)
987+
d.projSvc.EXPECT().List(ctx, project.Filter{OrgID: orgID}).Return([]project.Project{}, nil)
988+
d.grpSvc.EXPECT().List(ctx, group.Filter{OrganizationID: orgID}).Return([]group.Group{}, nil)
989+
d.policySvc.EXPECT().List(ctx, policy.Filter{PrincipalID: userID, PrincipalType: schema.UserPrincipal}).Return([]policy.Policy{
990+
{ID: "org-p1", ResourceType: schema.OrganizationNamespace, ResourceID: orgID},
991+
}, nil)
992+
d.policySvc.EXPECT().Delete(ctx, "org-p1").Return(nil)
993+
d.resSvc.EXPECT().RemovePrincipalAccess(ctx, userID, schema.UserPrincipal, orgID, []string{}).Return(nil)
994+
d.auditRepo.EXPECT().Create(ctx, mock.Anything).Return(auditrecord.AuditRecord{}, nil)
995+
d.patSvc.EXPECT().OnOrgMemberRemoved(ctx, orgID, userID).Return(errors.New("pat boom"))
996+
},
997+
wantErrContain: "revoke member PATs",
998+
},
999+
{
1000+
name: "should not revoke PATs when the removed principal is not a user",
1001+
principalType: schema.PATPrincipal,
1002+
setup: func(d testDeps) {
1003+
d.orgSvc.EXPECT().Get(ctx, orgID).Return(enabledOrg, nil)
1004+
d.policySvc.EXPECT().List(ctx, policy.Filter{OrgID: orgID, PrincipalID: userID, PrincipalType: schema.PATPrincipal}).Return([]policy.Policy{{ID: "org-p1", RoleID: viewerRoleID}}, nil)
1005+
d.projSvc.EXPECT().List(ctx, project.Filter{OrgID: orgID}).Return([]project.Project{}, nil)
1006+
d.grpSvc.EXPECT().List(ctx, group.Filter{OrganizationID: orgID}).Return([]group.Group{}, nil)
1007+
d.policySvc.EXPECT().List(ctx, policy.Filter{PrincipalID: userID, PrincipalType: schema.PATPrincipal}).Return([]policy.Policy{
1008+
{ID: "org-p1", ResourceType: schema.OrganizationNamespace, ResourceID: orgID},
1009+
}, nil)
1010+
d.policySvc.EXPECT().Delete(ctx, "org-p1").Return(nil)
1011+
d.resSvc.EXPECT().RemovePrincipalAccess(ctx, userID, schema.PATPrincipal, orgID, []string{}).Return(nil)
1012+
d.auditRepo.EXPECT().Create(ctx, mock.Anything).Return(auditrecord.AuditRecord{}, nil)
1013+
},
1014+
wantErr: nil,
1015+
},
1016+
{
1017+
name: "should still remove the member when the PAT service is not set",
1018+
noPATService: true,
1019+
setup: func(d testDeps) {
1020+
d.orgSvc.EXPECT().Get(ctx, orgID).Return(enabledOrg, nil)
1021+
d.policySvc.EXPECT().List(ctx, policy.Filter{OrgID: orgID, PrincipalID: userID, PrincipalType: schema.UserPrincipal}).Return([]policy.Policy{{ID: "org-p1", RoleID: viewerRoleID}}, nil)
1022+
d.roleSvc.EXPECT().Get(ctx, schema.RoleOrganizationOwner).Return(role.Role{ID: ownerRoleID, Name: schema.RoleOrganizationOwner}, nil)
1023+
d.projSvc.EXPECT().List(ctx, project.Filter{OrgID: orgID}).Return([]project.Project{}, nil)
1024+
d.grpSvc.EXPECT().List(ctx, group.Filter{OrganizationID: orgID}).Return([]group.Group{}, nil)
1025+
d.policySvc.EXPECT().List(ctx, policy.Filter{PrincipalID: userID, PrincipalType: schema.UserPrincipal}).Return([]policy.Policy{
1026+
{ID: "org-p1", ResourceType: schema.OrganizationNamespace, ResourceID: orgID},
1027+
}, nil)
1028+
d.policySvc.EXPECT().Delete(ctx, "org-p1").Return(nil)
1029+
d.resSvc.EXPECT().RemovePrincipalAccess(ctx, userID, schema.UserPrincipal, orgID, []string{}).Return(nil)
1030+
d.auditRepo.EXPECT().Create(ctx, mock.Anything).Return(auditrecord.AuditRecord{}, nil)
1031+
},
1032+
wantErr: nil,
1033+
},
9651034
}
9661035

9671036
for _, tt := range tests {
@@ -974,6 +1043,7 @@ func TestService_RemoveOrganizationMember(t *testing.T) {
9741043
projSvc: mocks.NewProjectService(t),
9751044
grpSvc: mocks.NewGroupService(t),
9761045
resSvc: mocks.NewResourceService(t),
1046+
patSvc: mocks.NewUserPATService(t),
9771047
auditRepo: mocks.NewAuditRecordRepository(t),
9781048
}
9791049
if tt.setup != nil {
@@ -982,6 +1052,9 @@ func TestService_RemoveOrganizationMember(t *testing.T) {
9821052

9831053
svc := membership.NewService(slog.New(slog.NewTextHandler(io.Discard, nil)), d.policySvc, d.relSvc, d.roleSvc, d.orgSvc, mocks.NewUserService(t), d.projSvc, d.grpSvc, mocks.NewServiceuserService(t), d.auditRepo)
9841054
svc.SetResourceService(d.resSvc)
1055+
if !tt.noPATService {
1056+
svc.SetUserPATService(d.patSvc)
1057+
}
9851058

9861059
principalType := tt.principalType
9871060
if principalType == "" {
@@ -1096,6 +1169,7 @@ func TestService_RemoveOrganizationMemberOnDelete(t *testing.T) {
10961169

10971170
svc := membership.NewService(slog.New(slog.NewTextHandler(io.Discard, nil)), d.policySvc, mocks.NewRelationService(t), d.roleSvc, d.orgSvc, mocks.NewUserService(t), d.projSvc, d.grpSvc, mocks.NewServiceuserService(t), d.auditRepo)
10981171
svc.SetResourceService(d.resSvc)
1172+
svc.SetUserPATService(mocks.NewUserPATService(t))
10991173

11001174
err := svc.RemoveOrganizationMemberOnDelete(ctx, orgID, userID, schema.UserPrincipal)
11011175
if tt.wantErr != nil {

‎core/membership/service.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type ServiceuserService interface {
6363

6464
type UserPATService interface {
6565
GetByID(ctx context.Context, id string) (patmodels.PAT, error)
66+
OnOrgMemberRemoved(ctx context.Context, orgID, userID string) error
6667
}
6768

6869
type ResourceService interface {

‎core/userpat/service.go‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ var supportedPATResourceTypes = []string{
3434
schema.ProjectNamespace,
3535
}
3636

37+
const patRemovalPageSize = 1000
38+
3739
type OrganizationService interface {
3840
GetRaw(ctx context.Context, id string) (organization.Organization, error)
3941
}
@@ -198,14 +200,42 @@ func (s *Service) DeleteAllByOrg(ctx context.Context, orgID string) error {
198200
return fmt.Errorf("listing PATs for org: %w", err)
199201
}
200202
for _, pat := range pats {
201-
if err := s.revokeOrgPAT(ctx, pat); err != nil {
203+
if err := s.revokePAT(ctx, pat, nil); err != nil {
204+
return fmt.Errorf("deleting PAT[%s]: %w", pat.ID, err)
205+
}
206+
}
207+
return nil
208+
}
209+
210+
func (s *Service) OnOrgMemberRemoved(ctx context.Context, orgID, userID string) error {
211+
pats, err := s.listAllByUserInOrg(ctx, userID, orgID)
212+
if err != nil {
213+
return fmt.Errorf("listing PATs for user in org: %w", err)
214+
}
215+
for _, pat := range pats {
216+
if err := s.revokePAT(ctx, pat, map[string]any{"reason": "member_removed"}); err != nil {
202217
return fmt.Errorf("deleting PAT[%s]: %w", pat.ID, err)
203218
}
204219
}
205220
return nil
206221
}
207222

208-
func (s *Service) revokeOrgPAT(ctx context.Context, pat patmodels.PAT) error {
223+
func (s *Service) listAllByUserInOrg(ctx context.Context, userID, orgID string) ([]patmodels.PAT, error) {
224+
var pats []patmodels.PAT
225+
for offset := 0; ; offset += patRemovalPageSize {
226+
query := pkgUtils.NewRQLQuery("", offset, patRemovalPageSize, []rql.Filter{}, []rql.Sort{}, []string{})
227+
page, err := s.repo.List(ctx, userID, orgID, query)
228+
if err != nil {
229+
return nil, err
230+
}
231+
pats = append(pats, page.PATs...)
232+
if len(page.PATs) < patRemovalPageSize {
233+
return pats, nil
234+
}
235+
}
236+
}
237+
238+
func (s *Service) revokePAT(ctx context.Context, pat patmodels.PAT, targetMetadata map[string]any) error {
209239
softDeleted := false
210240
if pat.DeletedAt == nil {
211241
err := s.repo.Delete(ctx, pat.ID)
@@ -223,7 +253,7 @@ func (s *Service) revokeOrgPAT(ctx context.Context, pat patmodels.PAT) error {
223253
}
224254

225255
if softDeleted {
226-
if err := s.createAuditRecord(ctx, pkgAuditRecord.PATRevokedEvent, pat, time.Now().UTC(), nil); err != nil {
256+
if err := s.createAuditRecord(ctx, pkgAuditRecord.PATRevokedEvent, pat, time.Now().UTC(), targetMetadata); err != nil {
227257
s.logger.ErrorContext(ctx, "failed to create audit record for PAT revocation", "pat_id", pat.ID, "error", err)
228258
}
229259
}

0 commit comments

Comments
 (0)