Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: store return nil on activity, bookmark, inbox, member, project member/webhook entity non-existence (#308)
spinningbot78920485ee94
diff --git a/server/acl.go b/server/acl.goindex 615b0fc192..9266dae595 100644--- a/server/acl.go+++ b/server/acl.go@@ -44,11 +44,11 @@ func aclMiddleware(l *zap.Logger, s *Server, ce *casbin.Enforcer, next echo.Hand}member, err := s.MemberService.FindMember(ctx, memberFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("User ID is not a member: %d", principalID))- }return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)}+ if member == nil {+ return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("User ID is not a member: %d", principalID))+ }if member.RowStatus == api.Archived {return echo.NewHTTPError(http.StatusUnauthorized, "This user has been deactivated by the admin")}@@ -109,13 +109,10 @@ func isUpdatingSelf(ctx context.Context, c echo.Context, s *Server, curPrincipal}activity, err := s.ActivityService.FindActivity(ctx, activityFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- msg := fmt.Sprintf("Activity ID not found: %d", activityID)- httpErr := echo.NewHTTPError(http.StatusNotFound, msg)- return false, httpErr- }- httpErr := echo.NewHTTPError(http.StatusInternalServerError, defaultErrMsg).SetInternal(err)- return false, httpErr+ return false, echo.NewHTTPError(http.StatusInternalServerError, defaultErrMsg).SetInternal(err)+ }+ if activity == nil {+ return false, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Activity ID not found: %d", activityID))}return activity.CreatorID == curPrincipalID, nil}@@ -132,12 +129,10 @@ func isUpdatingSelf(ctx context.Context, c echo.Context, s *Server, curPrincipal}bookmark, err := s.BookmarkService.FindBookmark(ctx, bookmarkFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- msg := fmt.Sprintf("Bookmark ID not found: %d", bookmarkID)- return false, echo.NewHTTPError(http.StatusNotFound, msg)- }- httpErr := echo.NewHTTPError(http.StatusInternalServerError, defaultErrMsg).SetInternal(err)- return false, httpErr+ return false, echo.NewHTTPError(http.StatusInternalServerError, defaultErrMsg).SetInternal(err)+ }+ if bookmark == nil {+ return false, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Bookmark ID not found: %d", bookmarkID))}return bookmark.CreatorID == curPrincipalID, nil}@@ -154,12 +149,11 @@ func isUpdatingSelf(ctx context.Context, c echo.Context, s *Server, curPrincipal}inbox, err := s.InboxService.FindInbox(ctx, inboxFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- msg := fmt.Sprintf("Inbox ID not found: %d", inboxID)- return false, echo.NewHTTPError(http.StatusNotFound, msg)- }return false, echo.NewHTTPError(http.StatusInternalServerError, defaultErrMsg).SetInternal(err)}+ if inbox == nil {+ return false, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Inbox ID not found: %d", inboxID))+ }return inbox.ReceiverID == curPrincipalID, nil}}diff --git a/server/auth.go b/server/auth.goindex 0442e426f0..b99696b1ca 100644--- a/server/auth.go+++ b/server/auth.go@@ -37,11 +37,11 @@ func (s *Server) registerAuthRoutes(g *echo.Group) {}member, err := s.MemberService.FindMember(ctx, memberFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Member not found: %s", login.Email))- }return echo.NewHTTPError(http.StatusInternalServerError, "Failed to authenticate user").SetInternal(err)}+ if member == nil {+ return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Member not found: %s", login.Email))+ }if member.RowStatus == api.Archived {return echo.NewHTTPError(http.StatusUnauthorized, "This user has been deactivated by the admin")}diff --git a/server/member.go b/server/member.goindex ad4cbb8fbf..dd96f5bcfc 100644--- a/server/member.go+++ b/server/member.go@@ -111,11 +111,11 @@ func (s *Server) registerMemberRoutes(g *echo.Group) {}member, err := s.MemberService.FindMember(ctx, memberFind)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Failed to find member ID: %d", id))- }return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Server error to find member ID: %d", id)).SetInternal(err)}+ if member == nil {+ return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("Failed to find member ID: %d", id))+ }memberPatch := &api.MemberPatch{ID: id,diff --git a/server/principal.go b/server/principal.goindex 6ee92d3ce7..bd991d952b 100644--- a/server/principal.go+++ b/server/principal.go@@ -172,6 +172,9 @@ func (s *Server) composePrincipalRole(ctx context.Context, principal *api.Princiif err != nil {return err}+ if principal.ID > 0 && member == nil {+ return fmt.Errorf("Member not found for ID %v", principal.ID)+ }principal.Role = member.Role}return nildiff --git a/server/project_member.go b/server/project_member.goindex 7d678be6e9..7235ccbe80 100644--- a/server/project_member.go+++ b/server/project_member.go@@ -82,11 +82,11 @@ func (s *Server) registerProjectMemberRoutes(g *echo.Group) {existingProjectMember, err := s.ProjectMemberService.FindProjectMember(ctx, &api.ProjectMemberFind{ID: &id})if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project member ID not found: %d", id))- }return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete project member ID: %v", id)).SetInternal(err)}+ if existingProjectMember == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project member ID not found: %d", id))+ }projectMemberPatch := &api.ProjectMemberPatch{ID: id,@@ -150,11 +150,11 @@ func (s *Server) registerProjectMemberRoutes(g *echo.Group) {projectMember, err := s.ProjectMemberService.FindProjectMember(ctx, &api.ProjectMemberFind{ID: &id})if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project member ID not found: %d", id))- }return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to delete project member ID: %v", id)).SetInternal(err)}+ if projectMember == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project member ID not found: %d", id))+ }projectMemberDelete := &api.ProjectMemberDelete{ID: id,diff --git a/server/project_webhook.go b/server/project_webhook.goindex b6628ba3c1..ceba11f02d 100644--- a/server/project_webhook.go+++ b/server/project_webhook.go@@ -94,11 +94,11 @@ func (s *Server) registerProjectWebhookRoutes(g *echo.Group) {}hook, err := s.ProjectWebhookService.FindProjectWebhook(ctx, find)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project webhook ID not found: %d", id))- }return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch project webhook ID: %v", id)).SetInternal(err)}+ if hook == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project webhook ID not found: %d", id))+ }if err := s.composeProjectWebhookRelationship(ctx, hook); err != nil {return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch webhook relationship").SetInternal(err)@@ -210,11 +210,11 @@ func (s *Server) registerProjectWebhookRoutes(g *echo.Group) {}hook, err := s.ProjectWebhookService.FindProjectWebhook(ctx, find)if err != nil {- if common.ErrorCode(err) == common.NotFound {- return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project webhook ID not found: %d", id))- }return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch project webhook ID: %v", id)).SetInternal(err)}+ if hook == nil {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Project webhook ID not found: %d", id))+ }result := &api.ProjectWebhookTestResult{}err = webhook.Post(diff --git a/store/activity.go b/store/activity.goindex 3f9da01409..d9e4ae7c04 100644--- a/store/activity.go+++ b/store/activity.go@@ -62,7 +62,6 @@ func (s *ActivityService) FindActivityList(ctx context.Context, find *api.Activi}// FindActivity retrieves a single activity based on find.-// Returns ENOTFOUND if no matching record.// Returns ECONFLICT if finding more than 1 matching records.func (s *ActivityService) FindActivity(ctx context.Context, find *api.ActivityFind) (*api.Activity, error) {tx, err := s.db.BeginTx(ctx, nil)@@ -74,8 +73,10 @@ func (s *ActivityService) FindActivity(ctx context.Context, find *api.ActivityFilist, err := findActivityList(ctx, tx, find)if err != nil {return nil, err- } else if len(list) == 0 {- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("activity not found: %+v", find)}+ }++ if len(list) == 0 {+ return nil, nil} else if len(list) > 1 {return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d activities with filter %+v, expect 1. ", len(list), find)}}diff --git a/store/bookmark.go b/store/bookmark.goindex 9df37e6f11..27722890d4 100644--- a/store/bookmark.go+++ b/store/bookmark.go@@ -62,7 +62,6 @@ func (s *BookmarkService) FindBookmarkList(ctx context.Context, find *api.Bookma}// FindBookmark retrieves a single bookmark based on find.-// Returns ENOTFOUND if no matching record.// Returns ECONFLICT if finding more than 1 matching records.func (s *BookmarkService) FindBookmark(ctx context.Context, find *api.BookmarkFind) (*api.Bookmark, error) {tx, err := s.db.BeginTx(ctx, nil)@@ -74,8 +73,10 @@ func (s *BookmarkService) FindBookmark(ctx context.Context, find *api.BookmarkFilist, err := findBookmarkList(ctx, tx, find)if err != nil {return nil, err- } else if len(list) == 0 {- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("bookmark not found: %+v", find)}+ }++ if len(list) == 0 {+ return nil, nil} else if len(list) > 1 {return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d activities with filter %+v, expect 1. ", len(list), find)}}diff --git a/store/inbox.go b/store/inbox.goindex 764172725d..a3d5b40a2a 100644--- a/store/inbox.go+++ b/store/inbox.go@@ -64,7 +64,6 @@ func (s *InboxService) FindInboxList(ctx context.Context, find *api.InboxFind) (}// FindInbox retrieves a single inbox based on find.-// Returns ENOTFOUND if no matching record.// Returns ECONFLICT if finding more than 1 matching records.func (s *InboxService) FindInbox(ctx context.Context, find *api.InboxFind) (*api.Inbox, error) {tx, err := s.db.BeginTx(ctx, nil)@@ -76,8 +75,10 @@ func (s *InboxService) FindInbox(ctx context.Context, find *api.InboxFind) (*apilist, err := findInboxList(ctx, tx, find)if err != nil {return nil, err- } else if len(list) == 0 {- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("inbox not found: %+v", find)}+ }++ if len(list) == 0 {+ return nil, nil} else if len(list) > 1 {return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d inboxes with filter %+v, expect 1", len(list), find)}}diff --git a/store/member.go b/store/member.goindex fa29ec2b51..5d6e7d2b36 100644--- a/store/member.go+++ b/store/member.go@@ -76,7 +76,6 @@ func (s *MemberService) FindMemberList(ctx context.Context, find *api.MemberFind}// FindMember retrieves a single member based on find.-// Returns ENOTFOUND if no matching record.// Returns ECONFLICT if finding more than 1 matching records.func (s *MemberService) FindMember(ctx context.Context, find *api.MemberFind) (*api.Member, error) {if find.PrincipalID != nil {@@ -99,16 +98,16 @@ func (s *MemberService) FindMember(ctx context.Context, find *api.MemberFind) (*list, err := findMemberList(ctx, tx, find)if err != nil {return nil, err- } else if len(list) == 0 {- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("member not found: %+v", find)}+ }++ if len(list) == 0 {+ return nil, nil} else if len(list) > 1 {return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d members with filter %+v, expect 1", len(list), find)}}-if err := s.cache.UpsertCache(api.MemberCache, list[0].PrincipalID, list[0]); err != nil {return nil, err}-return list[0], nil}diff --git a/store/policy.go b/store/policy.goindex 312d0453ea..a8e2edb2ef 100644--- a/store/policy.go+++ b/store/policy.go@@ -28,7 +28,6 @@ func NewPolicyService(logger *zap.Logger, db *DB, cache api.CacheService) *Polic}// FindPolicy finds the policy for an environment.-// Returns ENOTFOUND if no matching record.// Returns ECONFLICT if finding more than 1 matching records.func (s *PolicyService) FindPolicy(ctx context.Context, find *api.PolicyFind) (*api.Policy, error) {// Validate policy type existence.@@ -47,7 +46,9 @@ func (s *PolicyService) FindPolicy(ctx context.Context, find *api.PolicyFind) (*var ret *api.Policyif err != nil {return nil, err- } else if len(list) == 0 {+ }++ if len(list) == 0 {ret = &api.Policy{CreatorID: api.SystemBotID,UpdaterID: api.SystemBotID,diff --git a/store/project_member.go b/store/project_member.goindex 1844f25fb6..b13a683738 100644--- a/store/project_member.go+++ b/store/project_member.go@@ -72,8 +72,10 @@ func (s *ProjectMemberService) FindProjectMember(ctx context.Context, find *api.list, err := findProjectMemberList(ctx, tx, find)if err != nil {return nil, err- } else if len(list) == 0 {- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("project member not found: %+v", find)}+ }++ if len(list) == 0 {+ return nil, nil} else if len(list) > 1 {return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d project members with filter %+v, expect 1", len(list), find)}}diff --git a/store/project_webhook.go b/store/project_webhook.goindex 024bed7bd0..d92f816c05 100644--- a/store/project_webhook.go+++ b/store/project_webhook.go@@ -62,7 +62,6 @@ func (s *ProjectWebhookService) FindProjectWebhookList(ctx context.Context, find}// FindProjectWebhook retrieves a single projectWebhook based on find.-// Returns ENOTFOUND if no matching record.// Returns ECONFLICT if finding more than 1 matching records.func (s *ProjectWebhookService) FindProjectWebhook(ctx context.Context, find *api.ProjectWebhookFind) (*api.ProjectWebhook, error) {tx, err := s.db.BeginTx(ctx, nil)@@ -74,8 +73,10 @@ func (s *ProjectWebhookService) FindProjectWebhook(ctx context.Context, find *aplist, err := findProjectWebhookList(ctx, tx, find)if err != nil {return nil, err- } else if len(list) == 0 {- return nil, &common.Error{Code: common.NotFound, Err: fmt.Errorf("project hook not found: %+v", find)}+ }++ if len(list) == 0 {+ return nil, nil} else if len(list) > 1 {return nil, &common.Error{Code: common.Conflict, Err: fmt.Errorf("found %d project hooks with filter %+v, expect 1", len(list), find)}}