Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: project owner should not have privilege transfer DB from other project that he is not owner (#5833)
h3n4laf137bb7220b
diff --git a/backend/server/acl.go b/backend/server/acl.goindex ee00a86ea6..41cbcf48c5 100644--- a/backend/server/acl.go+++ b/backend/server/acl.go@@ -152,7 +152,7 @@ func aclMiddleware(s *Server, pathPrefix string, ce *casbin.Enforcer, next echo.}c.Request().Body = io.NopCloser(bytes.NewBuffer(bodyBytes))- aclErr = enforceWorkspaceDeveloperDatabaseRouteACL(path, method, string(bodyBytes), principalID, getRetrieveDatabaseProjectID(ctx, s.store), projectRolesFinder)+ 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.@@ -235,7 +235,7 @@ func enforceWorkspaceDeveloperProjectRouteACL(plan api.PlanType, path string, mevar databaseGeneralRouteRegex = regexp.MustCompile(`^/database/(?P<databaseID>\d+)$`)-func enforceWorkspaceDeveloperDatabaseRouteACL(path string, method string, body string, principalID int, projectIDOfDatabase func(databaseID int) (int, error), projectRolesFinder func(projectID int, principalID int) (map[common.ProjectRole]bool, error)) *echo.HTTPError {+func enforceWorkspaceDeveloperDatabaseRouteACL(plan api.PlanType, path string, method string, body string, principalID int, projectIDOfDatabase func(databaseID int) (int, error), projectRolesFinder func(projectID int, principalID int) (map[common.ProjectRole]bool, error)) *echo.HTTPError {switch method {case http.MethodGet:// For /database route, server should list the databases that the user has access to.@@ -275,9 +275,16 @@ func enforceWorkspaceDeveloperDatabaseRouteACL(path string, method string, bodyif err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Cannot find project member ids for project %d", oldProjectID)).SetInternal(err)}- if len(oldProjectRoles) == 0 {- return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("user is not a member of project owns the database %d", databaseIDInt))+ if plan == api.ENTERPRISE {+ if _, ok := oldProjectRoles[common.ProjectOwner]; !ok {+ return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("user is not project owner of project owns the database %d", databaseIDInt))+ }+ } else {+ if len(oldProjectRoles) == 0 {+ return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("user is not a project member of project owns the database %d", databaseIDInt))+ }}+// Workspace developer can only modify the database belongs to the project which he is a member of.var databasePatch api.DatabasePatchif err := jsonapi.UnmarshalPayload(strings.NewReader(body), &databasePatch); err != nil {@@ -289,8 +296,14 @@ func enforceWorkspaceDeveloperDatabaseRouteACL(path string, method string, bodyif err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Cannot find project member ids for project %d", *databasePatch.ProjectID)).SetInternal(err)}- if len(newProjectRoles) == 0 {- return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("user is not a member of project %d", *databasePatch.ProjectID))+ if plan == api.ENTERPRISE {+ if _, ok := newProjectRoles[common.ProjectOwner]; !ok {+ return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("user is not project owner of project want owns the database %d", databaseIDInt))+ }+ } else {+ if len(newProjectRoles) == 0 {+ return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("user is not a project member of project want to owns the database %d", databaseIDInt))+ }}}}diff --git a/backend/server/acl_database_test.go b/backend/server/acl_database_test.goindex 8f088754ed..c5cb31fb5e 100644--- a/backend/server/acl_database_test.go+++ b/backend/server/acl_database_test.go@@ -3,6 +3,8 @@ package serverimport ("testing"+ api "github.com/bytebase/bytebase/backend/legacyapi"+"github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"@@ -16,9 +18,11 @@ func TestEnforceWorkspaceDeveloperDatabaseRouteACL(t *testing.T) {method stringprincipalID interrMsg string+ plan api.PlanType}tests := []test{+ // Retrieve the database which is unassigned to any project.{desc: "Retrieve the database which is unassigned to any project",path: "/database/301",@@ -26,6 +30,7 @@ func TestEnforceWorkspaceDeveloperDatabaseRouteACL(t *testing.T) {body: "",principalID: 201,errMsg: "user is not a member of project owns this database",+ plan: api.ENTERPRISE,},{desc: "Retrieve the database which is assigned to a project, but the user is not a member of the project",@@ -34,6 +39,7 @@ func TestEnforceWorkspaceDeveloperDatabaseRouteACL(t *testing.T) {body: "",principalID: 201,errMsg: "user is not a member of project owns this database",+ plan: api.ENTERPRISE,},{desc: "Retrieve the database which is assigned to a project, and the user is a member of the project",@@ -42,36 +48,69 @@ func TestEnforceWorkspaceDeveloperDatabaseRouteACL(t *testing.T) {body: "",principalID: 201,errMsg: "",+ plan: api.ENTERPRISE,},++ // Transfer the database.{- desc: "Transfer the database to the project which the user is not a member of",- path: "/database/303",- body: `{"data":{"type":"databasePatch","attributes":{"projectId":101}}}`,+ desc: "Enterprise Plan, workspace developer of project developer cannot transfer the database out",+ path: "/database/304",+ body: `{"data":{"type":"databasePatch","attributes":{"projectId":104}}}`,method: "PATCH",principalID: 201,- errMsg: "user is not a member of project 101",+ errMsg: "user is not project owner of project owns the database 304",+ plan: api.ENTERPRISE,},{- desc: "Transfer out the database under the project which the user is not a member of",- path: "/database/302",- body: `{"data":{"type":"databasePatch","attributes":{"projectId":102}}}`,+ desc: "Free & Pro Plan, workspace developer of project developer can transfer the database out",+ path: "/database/304",+ body: `{"data":{"type":"databasePatch","attributes":{"projectId":104}}}`,method: "PATCH",principalID: 201,- errMsg: "user is not a member of project owns the database 302",+ errMsg: "",+ plan: api.FREE,},{- desc: "Transfer out the database under the project which the user is not a member of",- path: "/database/304",+ desc: "Enterprise Plan, workspace developer of project owner cannot transfer the database out to the project if he is not the owner",+ path: "/database/303",+ body: `{"data":{"type":"databasePatch","attributes":{"projectId":103}}}`,+ method: "PATCH",+ principalID: 201,+ errMsg: "user is not project owner of project want owns the database 303",+ plan: api.ENTERPRISE,+ },+ {+ desc: "Free & Pro Plan, workspace developer of project owner can transfer the database out to the project if he is not the owner",+ path: "/database/303",body: `{"data":{"type":"databasePatch","attributes":{"projectId":103}}}`,method: "PATCH",principalID: 201,errMsg: "",+ plan: api.TEAM,+ },+ {+ desc: "Transfer the database to the project which the user is not a member of",+ path: "/database/303",+ body: `{"data":{"type":"databasePatch","attributes":{"projectId":101}}}`,+ method: "PATCH",+ principalID: 201,+ errMsg: "user is not a project member of project want to owns the database 303",+ plan: api.FREE,+ },+ {+ desc: "Transfer out the database under the project which the user is not a member of",+ path: "/database/302",+ body: `{"data":{"type":"databasePatch","attributes":{"projectId":102}}}`,+ method: "PATCH",+ principalID: 201,+ errMsg: "user is not a project member of project owns the database 302",+ plan: api.FREE,},}for _, tc := range tests {t.Run(tc.desc, func(t *testing.T) {- err := enforceWorkspaceDeveloperDatabaseRouteACL(tc.path, tc.method, tc.body, tc.principalID, testWorkspaceDeveloperDatabaseRouteMockGetDatabaseProjectID, getProjectRolesFinderForTest(testWorkspaceDeveloperDatabaseRouteHelper.projectMembers))+ err := enforceWorkspaceDeveloperDatabaseRouteACL(tc.plan, tc.path, tc.method, tc.body, tc.principalID, testWorkspaceDeveloperDatabaseRouteMockGetDatabaseProjectID, getProjectRolesFinderForTest(testWorkspaceDeveloperDatabaseRouteHelper.projectMembers))if err != nil {if tc.errMsg == "" {t.Errorf("expect no error, got %s", err.Message)@@ -94,11 +133,14 @@ var testWorkspaceDeveloperDatabaseRouteHelper = struct {1: {},101: {},102: {- common.ProjectDeveloper: {201},+ common.ProjectOwner: {201},},103: {common.ProjectDeveloper: {201},},+ 104: {+ common.ProjectOwner: {201},+ },},projectOwnsDatabase: map[int]map[int]bool{// Default Project owns database 301.@@ -117,6 +159,7 @@ var testWorkspaceDeveloperDatabaseRouteHelper = struct {103: {304: true,},+ 104: {},},}