Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: fix issue routes (#6027)
Danny Xuc2f770f571fa
diff --git a/backend/server/acl.go b/backend/server/acl.goindex c85305d0b3..96895a4f32 100644--- a/backend/server/acl.go+++ b/backend/server/acl.go@@ -6,7 +6,6 @@ import ("fmt""io""net/http"- "net/url""regexp""strconv""strings"@@ -153,20 +152,6 @@ func aclMiddleware(s *Server, pathPrefix string, ce *casbin.Enforcer, next echo.c.Request().Body = io.NopCloser(bytes.NewBuffer(bodyBytes))aclErr = enforceWorkspaceDeveloperDatabaseRouteACL(s.licenseService.GetEffectivePlan(), path, method, string(bodyBytes), principalID, getRetrieveDatabaseProjectID(ctx, s.store), projectRolesFinder)- } else if strings.HasPrefix(path, "/issue") {- // We need to copy the body because it will be consumed by the next middleware.- // And TeeReader require us the write must complete before the read completes.- // The body under the /issue route is a JSON object, and always not too large, so using ioutil.ReadAll is fine here.- bodyBytes, err := io.ReadAll(c.Request().Body)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to read request body.").SetInternal(err)- }- if err := c.Request().Body.Close(); err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to close request body.").SetInternal(err)- }- c.Request().Body = io.NopCloser(bytes.NewBuffer(bodyBytes))-- aclErr = enforceWorkspaceDeveloperIssueRouteACL(s.licenseService.GetEffectivePlan(), path, method, c.QueryParams(), principalID, getRetrieveIssueProjectID(ctx, s.store), projectRolesFinder)}if aclErr != nil {return aclErr@@ -359,61 +344,6 @@ func enforceWorkspaceDeveloperSheetRouteACL(plan api.PlanType, path string, methreturn nil}-var issueStatusRegex = regexp.MustCompile(`^/issue/(?P<issueID>\d+)/status$`)--func enforceWorkspaceDeveloperIssueRouteACL(plan api.PlanType, path string, method string, queryParams url.Values, principalID int, getIssueProjectID func(issueID int) (int, error), projectRolesFinder func(projectID int, principalID int) (map[common.ProjectRole]bool, error)) *echo.HTTPError {- switch method {- case http.MethodGet:- // For /issue route, require the caller principal to be the same as the user in the query.- // Only /issue and /project route will bring parameter user in the query.- if userStr := queryParams.Get("user"); userStr != "" {- userID, err := strconv.Atoi(userStr)- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, "Invalid user ID").SetInternal(err)- }- if principalID != userID {- return echo.NewHTTPError(http.StatusUnauthorized, "not allowed to list other users' issues")- }- }- case http.MethodPatch:- // Workspace developer can only operating the issues if the user is the member of the project that the issue belongs to.- if matches := issueStatusRegex.FindStringSubmatch(path); len(matches) > 0 {- issueIDStr := matches[1]- issueID, err := strconv.Atoi(issueIDStr)- if err != nil {- return echo.NewHTTPError(http.StatusBadRequest, "Invalid issue ID").SetInternal(err)- }- projectID, err := getIssueProjectID(issueID)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)- }- projectRoles, err := projectRolesFinder(projectID, principalID)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)- }- if !api.ProjectPermission(api.ProjectPermissionOrganizeSheet, plan, projectRoles) {- return echo.NewHTTPError(http.StatusUnauthorized, "not allowed to operate the issue")- }- }- }- return nil-}--func getRetrieveIssueProjectID(ctx context.Context, s *store.Store) func(issueID int) (int, error) {- return func(issueID int) (int, error) {- issue, err := s.GetIssueV2(ctx, &store.FindIssueMessage{- UID: &issueID,- })- if err != nil {- return 0, err- }- if issue == nil {- return 0, errors.Errorf("cannot find issue %d", issueID)- }- return issue.Project.UID, nil- }-}-func getRetrieveDatabaseProjectID(ctx context.Context, s *store.Store) func(databaseID int) (int, error) {return func(databaseID int) (int, error) {db, err := s.GetDatabaseV2(ctx, &store.FindDatabaseMessage{diff --git a/backend/server/acl_issue_test.go b/backend/server/acl_issue_test.godeleted file mode 100644index ce547483af..0000000000--- a/backend/server/acl_issue_test.go+++ /dev/null@@ -1,209 +0,0 @@-package server--import (- "net/url"- "testing"-- "github.com/pkg/errors"-- "github.com/bytebase/bytebase/backend/common"- api "github.com/bytebase/bytebase/backend/legacyapi"-)--func TestWorkspaceDeveloperIssueRouteACL_RetrieveIssue(t *testing.T) {- type test struct {- desc string- plan api.PlanType- path string- method string- queryParams url.Values- principalID int- errMsg string- }-- tests := []test{- {- desc: "Retrieve other users' issue by project owner",- plan: api.ENTERPRISE,- path: "/issue?user=201",- queryParams: url.Values{"user": []string{"201"}},- method: "GET",- principalID: 202,- errMsg: "not allowed to list other users' issues",- },- {- desc: "Retrieve other users' issue by custom role",- plan: api.ENTERPRISE,- path: "/issue?user=201",- queryParams: url.Values{"user": []string{"201"}},- method: "GET",- principalID: 204,- errMsg: "not allowed to list other users' issues",- },- {- desc: "Retrieve my issue",- plan: api.ENTERPRISE,- path: "/issue?user=202",- queryParams: url.Values{"user": []string{"202"}},- method: "GET",- principalID: 202,- errMsg: "",- },- }-- for _, tc := range tests {- t.Run(tc.desc, func(t *testing.T) {- err := enforceWorkspaceDeveloperIssueRouteACL(tc.plan, tc.path, tc.method, tc.queryParams, tc.principalID, testWorkspaceDeveloperIssueRouteMockGetIssueProjectID, getProjectRolesFinderForTest(testWorkspaceDeveloperIssueRouteHelper.projectMembers))- if err != nil {- if tc.errMsg == "" {- t.Errorf("expect no error, got %s", err.Message)- } else if tc.errMsg != err.Message {- t.Errorf("expect error %s, got %s", tc.errMsg, err.Message)- }- } else if tc.errMsg != "" {- t.Errorf("expect error %s, got no error", tc.errMsg)- }- })- }-}--func TestWorkspaceDeveloperIssueRouteACL_OperateIssue(t *testing.T) {- type test struct {- desc string- plan api.PlanType- path string- method string- queryParams url.Values- principalID int- errMsg string- }-- tests := []test{- {- desc: "Operating the issue created by other user in other projects",- plan: api.ENTERPRISE,- path: "/issue/403/status",- queryParams: url.Values{},- method: "PATCH",- principalID: 202,- errMsg: "not allowed to operate the issue",- },- {- desc: "Operating the issue I created",- plan: api.ENTERPRISE,- path: "/issue/401/status",- queryParams: url.Values{},- method: "PATCH",- principalID: 202,- errMsg: "",- },- {- desc: "Operating the issue created by other user in projects I am a member of",- plan: api.ENTERPRISE,- path: "/issue/402/status",- queryParams: url.Values{},- method: "PATCH",- principalID: 202,- errMsg: "",- },- {- desc: "Operating the issue created by other user in projects I am a member of but I'm neither a project owner nor a developer",- plan: api.ENTERPRISE,- path: "/issue/402/status",- queryParams: url.Values{},- method: "PATCH",- principalID: 204,- errMsg: "not allowed to operate the issue",- },- }-- for _, tc := range tests {- tc := tc- t.Run(tc.desc, func(t *testing.T) {- err := enforceWorkspaceDeveloperIssueRouteACL(tc.plan, tc.path, tc.method, tc.queryParams, tc.principalID, testWorkspaceDeveloperIssueRouteMockGetIssueProjectID, getProjectRolesFinderForTest(testWorkspaceDeveloperIssueRouteHelper.projectMembers))- if err != nil {- if tc.errMsg == "" {- t.Errorf("expect no error, got %s", err.Message)- } else if tc.errMsg != err.Message {- t.Errorf("expect error %s, got %s", tc.errMsg, err.Message)- }- } else if tc.errMsg != "" {- t.Errorf("expect error %s, got no error", tc.errMsg)- }- })- }-}--func TestWorkspaceDeveloperIssueRouteACL_CreateIssue(t *testing.T) {- type test struct {- desc string- plan api.PlanType- path string- method string- queryParams url.Values- principalID int- errMsg string- }-- tests := []test{- {- desc: "Create issue under project I am a member of",- plan: api.ENTERPRISE,- path: "/issue",- queryParams: url.Values{},- method: "POST",- principalID: 202,- errMsg: "",- },- }-- for _, tc := range tests {- t.Run(tc.desc, func(t *testing.T) {- err := enforceWorkspaceDeveloperIssueRouteACL(tc.plan, tc.path, tc.method, tc.queryParams, tc.principalID, testWorkspaceDeveloperIssueRouteMockGetIssueProjectID, getProjectRolesFinderForTest(testWorkspaceDeveloperIssueRouteHelper.projectMembers))- if err != nil {- if tc.errMsg == "" {- t.Errorf("expect no error, got %s", err.Message)- } else if tc.errMsg != err.Message {- t.Errorf("expect error %s, got %s", tc.errMsg, err.Message)- }- } else if tc.errMsg != "" {- t.Errorf("expect error %s, got no error", tc.errMsg)- }- })- }-}--var testWorkspaceDeveloperIssueRouteHelper = struct {- projectMembers map[int]map[common.ProjectRole][]int- // issueIDToProjectID is a map from issue ID to the project ID.- issueIDToProjectID map[int]int-}{- projectMembers: map[int]map[common.ProjectRole][]int{- // Project 102 contains members 202 and 203.- 102: {- common.ProjectOwner: {202},- common.ProjectDeveloper: {202, 203},- common.ProjectRole("CustomRole"): {204},- },- // Project 103 contains member 204.- 103: {- common.ProjectOwner: {204},- },- },- issueIDToProjectID: map[int]int{- // Issue 401 belongs to project 102.- 401: 102,- // Issue 402 belongs to project 102.- 402: 102,- // Issue 403 belongs to project 103.- 403: 103,- },-}--func testWorkspaceDeveloperIssueRouteMockGetIssueProjectID(issueID int) (int, error) {- projectID, ok := testWorkspaceDeveloperIssueRouteHelper.issueIDToProjectID[issueID]- if !ok {- return 0, errors.Errorf("issue %d does not belong to any project", issueID)- }- return projectID, nil-}