Skip to content

Commit caef5be

Browse files
authored
feat(store): organization delete keeps the row and frees its name (#1973)
1 parent 58578a2 commit caef5be

4 files changed

Lines changed: 153 additions & 33 deletions

File tree

‎internal/store/postgres/organization_repository.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ func (r OrganizationRepository) SetState(ctx context.Context, id string, state o
509509
}
510510

511511
func (r OrganizationRepository) Delete(ctx context.Context, id string) error {
512-
query, params, err := dialect.Delete(TABLE_ORGANIZATIONS).Where(
512+
query, params, err := softDelete(TABLE_ORGANIZATIONS).Where(
513513
goqu.Ex{
514514
"id": id,
515515
},

‎internal/store/postgres/organization_repository_test.go‎

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -438,40 +438,30 @@ func (s *OrganizationRepositoryTestSuite) TestUpdateBySlug() {
438438
}
439439

440440
func (s *OrganizationRepositoryTestSuite) TestDelete() {
441-
type testCase struct {
442-
Description string
443-
OrganizationToDelete organization.Organization
444-
ErrString string
445-
}
441+
s.Run("should return error if organization not found", func() {
442+
s.Assert().ErrorIs(s.repository.Delete(s.ctx, uuid.NewString()), organization.ErrNotExist)
443+
})
446444

447-
var testCases = []testCase{
448-
{
449-
Description: "should delete a organization",
450-
OrganizationToDelete: organization.Organization{
451-
ID: s.orgs[0].ID,
452-
},
453-
},
454-
{
455-
Description: "should return error if organization not found",
456-
OrganizationToDelete: organization.Organization{
457-
ID: uuid.NewString(),
458-
},
459-
ErrString: organization.ErrNotExist.Error(),
460-
},
461-
}
445+
s.Run("should keep the row, hide it from reads, refuse a second delete and free its name", func() {
446+
deleted := s.orgs[0]
462447

463-
for _, tc := range testCases {
464-
s.Run(tc.Description, func() {
465-
err := s.repository.Delete(s.ctx, tc.OrganizationToDelete.ID)
466-
if tc.ErrString != "" {
467-
if err.Error() != tc.ErrString {
468-
s.T().Fatalf("got error %s, expected was %s", err.Error(), tc.ErrString)
469-
}
470-
} else {
471-
s.Assert().NoError(err)
472-
}
473-
})
474-
}
448+
s.Require().NoError(s.repository.Delete(s.ctx, deleted.ID))
449+
450+
s.Assert().Equal("1", scalarSQL(s.T(), s.ctx, s.client,
451+
"SELECT count(*) FROM organizations WHERE id = $1 AND deleted_at IS NOT NULL", deleted.ID))
452+
s.Assert().Equal("1", scalarSQL(s.T(), s.ctx, s.client,
453+
"SELECT count(*) FROM audit_records WHERE event = 'organization.delete' AND resource_id = $1", deleted.ID))
454+
455+
_, err := s.repository.GetByID(s.ctx, deleted.ID)
456+
s.Assert().ErrorIs(err, organization.ErrNotExist)
457+
458+
s.Assert().ErrorIs(s.repository.Delete(s.ctx, deleted.ID), organization.ErrNotExist)
459+
460+
again, err := s.repository.Create(s.ctx, organization.Organization{Name: deleted.Name, Metadata: metadata.Metadata{}})
461+
s.Require().NoError(err)
462+
s.Assert().NotEqual(deleted.ID, again.ID)
463+
s.Assert().Equal(deleted.Name, again.Name)
464+
})
475465
}
476466

477467
func (s *OrganizationRepositoryTestSuite) TestSetState() {

‎test/e2e/regression/api_test.go‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3214,6 +3214,76 @@ func (s *APIRegressionTestSuite) TestOrganizationRoleDeleteInUse() {
32143214
s.Require().NoError(err)
32153215
}
32163216

3217+
func (s *APIRegressionTestSuite) TestOrganizationDeleteHidesTheOrgAndRemovesWhatItOwned() {
3218+
ctxOrgAdminAuth := testbench.ContextWithAuth(context.Background(), s.adminCookie)
3219+
3220+
createOrgResp, err := s.testBench.Client.CreateOrganization(ctxOrgAdminAuth, connect.NewRequest(&frontierv1beta1.CreateOrganizationRequest{
3221+
Body: &frontierv1beta1.OrganizationRequestBody{
3222+
Title: "org soft delete",
3223+
Name: "org-soft-delete",
3224+
},
3225+
}))
3226+
s.Require().NoError(err)
3227+
orgID := createOrgResp.Msg.GetOrganization().GetId()
3228+
3229+
createDomainResp, err := s.testBench.Client.CreateOrganizationDomain(ctxOrgAdminAuth, connect.NewRequest(&frontierv1beta1.CreateOrganizationDomainRequest{
3230+
OrgId: orgID,
3231+
Domain: "org-soft-delete.raystack.io",
3232+
}))
3233+
s.Require().NoError(err)
3234+
domainID := createDomainResp.Msg.GetDomain().GetId()
3235+
3236+
policiesResp, err := s.testBench.Client.ListPolicies(ctxOrgAdminAuth, connect.NewRequest(&frontierv1beta1.ListPoliciesRequest{OrgId: orgID}))
3237+
s.Require().NoError(err)
3238+
policiesOnOrgBeforeDelete := policiesResp.Msg.GetPolicies()
3239+
s.Require().NotEmpty(policiesOnOrgBeforeDelete)
3240+
3241+
_, err = s.testBench.Client.DeleteOrganization(ctxOrgAdminAuth, connect.NewRequest(&frontierv1beta1.DeleteOrganizationRequest{Id: orgID}))
3242+
s.Require().NoError(err)
3243+
3244+
s.Run("no live policy names the org as its resource", func() {
3245+
for _, policyBeforeDelete := range policiesOnOrgBeforeDelete {
3246+
_, err := s.testBench.Client.GetPolicy(ctxOrgAdminAuth, connect.NewRequest(&frontierv1beta1.GetPolicyRequest{Id: policyBeforeDelete.GetId()}))
3247+
s.Require().Error(err)
3248+
s.Assert().Equal(connect.CodeNotFound, connect.CodeOf(err))
3249+
}
3250+
})
3251+
3252+
s.Run("the org is hidden from the superuser listing", func() {
3253+
listResp, err := s.testBench.AdminClient.ListAllOrganizations(ctxOrgAdminAuth, connect.NewRequest(&frontierv1beta1.ListAllOrganizationsRequest{}))
3254+
s.Require().NoError(err)
3255+
for _, org := range listResp.Msg.GetOrganizations() {
3256+
s.Assert().NotEqual(orgID, org.GetId())
3257+
}
3258+
})
3259+
3260+
s.Run("the org's domain is gone with it", func() {
3261+
_, err := s.testBench.Client.GetOrganizationDomain(ctxOrgAdminAuth, connect.NewRequest(&frontierv1beta1.GetOrganizationDomainRequest{
3262+
OrgId: orgID,
3263+
Id: domainID,
3264+
}))
3265+
s.Require().Error(err)
3266+
s.Assert().Equal(connect.CodeNotFound, connect.CodeOf(err))
3267+
})
3268+
3269+
s.Run("a second delete is refused", func() {
3270+
_, err := s.testBench.Client.DeleteOrganization(ctxOrgAdminAuth, connect.NewRequest(&frontierv1beta1.DeleteOrganizationRequest{Id: orgID}))
3271+
s.Require().Error(err)
3272+
s.Assert().Contains([]connect.Code{connect.CodeNotFound, connect.CodePermissionDenied}, connect.CodeOf(err))
3273+
})
3274+
3275+
s.Run("a new org can take the name", func() {
3276+
recreateResp, err := s.testBench.Client.CreateOrganization(ctxOrgAdminAuth, connect.NewRequest(&frontierv1beta1.CreateOrganizationRequest{
3277+
Body: &frontierv1beta1.OrganizationRequestBody{
3278+
Title: "org soft delete again",
3279+
Name: "org-soft-delete",
3280+
},
3281+
}))
3282+
s.Require().NoError(err)
3283+
s.Assert().NotEqual(orgID, recreateResp.Msg.GetOrganization().GetId())
3284+
})
3285+
}
3286+
32173287
func TestEndToEndAPIRegressionTestSuite(t *testing.T) {
32183288
suite.Run(t, new(APIRegressionTestSuite))
32193289
}

‎test/e2e/regression/pat_test.go‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ func (s *PATRegressionTestSuite) checkPermission(ctx context.Context, namespace,
155155
return resp.Msg.GetStatus()
156156
}
157157

158+
func (s *PATRegressionTestSuite) patHasPermissionInSpiceDB(ctx context.Context, patID, namespace, id, permission string) bool {
159+
resp, err := s.testBench.AdminClient.CheckFederatedResourcePermission(ctx, connect.NewRequest(&frontierv1beta1.CheckFederatedResourcePermissionRequest{
160+
Resource: schema.JoinNamespaceAndResourceID(namespace, id),
161+
Permission: permission,
162+
Subject: schema.JoinNamespaceAndResourceID(schema.PATPrincipal, patID),
163+
}))
164+
s.Require().NoError(err)
165+
return resp.Msg.GetStatus()
166+
}
167+
158168
func (s *PATRegressionTestSuite) TestPATScope_OrgViewer_ProjectViewer() {
159169
ctxAdmin := testbench.ContextWithAuth(context.Background(), s.adminCookie)
160170
orgID, proj1ID, proj2ID := s.createOrgAndProjects(ctxAdmin, "org-pat-ov-pv", "pat-ov-pv-p1", "pat-ov-pv-p2")
@@ -771,6 +781,56 @@ func (s *PATRegressionTestSuite) TestPATCRUD_CreateErrors() {
771781
})
772782
}
773783

784+
func (s *PATRegressionTestSuite) TestPATOfDeletedOrgStopsAuthenticating() {
785+
ctxAdmin := testbench.ContextWithAuth(context.Background(), s.adminCookie)
786+
orgID, proj1ID, _ := s.createOrgAndProjects(ctxAdmin, "org-pat-deleted-org", "pat-deleted-org-p1", "")
787+
788+
patID, patToken := s.createPAT(ctxAdmin, orgID, "pat-of-deleted-org", []*frontierv1beta1.PATScope{
789+
{RoleId: s.roleID(schema.RoleOrganizationViewer), ResourceType: schema.OrganizationNamespace},
790+
{RoleId: s.roleID(schema.RoleProjectViewer), ResourceType: schema.ProjectNamespace, ResourceIds: []string{proj1ID}},
791+
})
792+
patCtx := getPATCtx(patToken)
793+
s.Require().True(s.checkPermission(patCtx, schema.OrganizationNamespace, orgID, schema.GetPermission))
794+
s.Require().True(s.patHasPermissionInSpiceDB(ctxAdmin, patID, schema.OrganizationNamespace, orgID, schema.GetPermission))
795+
s.Require().True(s.patHasPermissionInSpiceDB(ctxAdmin, patID, schema.ProjectNamespace, proj1ID, schema.GetPermission))
796+
797+
policiesResp, err := s.testBench.Client.ListPolicies(ctxAdmin, connect.NewRequest(&frontierv1beta1.ListPoliciesRequest{UserId: patID}))
798+
s.Require().NoError(err)
799+
patPoliciesBeforeDelete := policiesResp.Msg.GetPolicies()
800+
s.Require().Len(patPoliciesBeforeDelete, 2)
801+
802+
_, err = s.testBench.Client.DeleteOrganization(ctxAdmin, connect.NewRequest(&frontierv1beta1.DeleteOrganizationRequest{Id: orgID}))
803+
s.Require().NoError(err)
804+
805+
s.Run("the token is rejected", func() {
806+
_, err := s.testBench.Client.GetCurrentUser(patCtx, connect.NewRequest(&frontierv1beta1.GetCurrentUserRequest{}))
807+
s.Require().Error(err)
808+
s.Assert().Equal(connect.CodeUnauthenticated, connect.CodeOf(err))
809+
})
810+
811+
s.Run("the token is hidden from its owner", func() {
812+
_, err := s.testBench.Client.GetCurrentUserPAT(ctxAdmin, connect.NewRequest(&frontierv1beta1.GetCurrentUserPATRequest{Id: patID}))
813+
s.Require().Error(err)
814+
s.Assert().Equal(connect.CodeNotFound, connect.CodeOf(err))
815+
})
816+
817+
s.Run("no live policy is held by the token", func() {
818+
policiesResp, err := s.testBench.Client.ListPolicies(ctxAdmin, connect.NewRequest(&frontierv1beta1.ListPoliciesRequest{UserId: patID}))
819+
s.Require().NoError(err)
820+
s.Assert().Empty(policiesResp.Msg.GetPolicies())
821+
for _, policyBeforeDelete := range patPoliciesBeforeDelete {
822+
_, err := s.testBench.Client.GetPolicy(ctxAdmin, connect.NewRequest(&frontierv1beta1.GetPolicyRequest{Id: policyBeforeDelete.GetId()}))
823+
s.Require().Error(err)
824+
s.Assert().Equal(connect.CodeNotFound, connect.CodeOf(err))
825+
}
826+
})
827+
828+
s.Run("the token has no access left in SpiceDB", func() {
829+
s.Assert().False(s.patHasPermissionInSpiceDB(ctxAdmin, patID, schema.OrganizationNamespace, orgID, schema.GetPermission))
830+
s.Assert().False(s.patHasPermissionInSpiceDB(ctxAdmin, patID, schema.ProjectNamespace, proj1ID, schema.GetPermission))
831+
})
832+
}
833+
774834
func TestEndToEndPATRegressionTestSuite(t *testing.T) {
775835
suite.Run(t, new(PATRegressionTestSuite))
776836
}

0 commit comments

Comments
 (0)