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: several fix (#6016)
Danny Xu3360ed29fa72
diff --git a/backend/runner/taskrun/scheduler.go b/backend/runner/taskrun/scheduler.goindex 1b62e212cd..4c32c63f05 100644--- a/backend/runner/taskrun/scheduler.go+++ b/backend/runner/taskrun/scheduler.go@@ -1165,31 +1165,33 @@ func (s *Scheduler) CanPrincipalBeAssignee(ctx context.Context, principalID int,// ChangeIssueStatus changes the status of an issue.func (s *Scheduler) ChangeIssueStatus(ctx context.Context, issue *store.IssueMessage, newStatus api.IssueStatus, updaterID int, comment string) error {- tasks, err := s.store.ListTasks(ctx, &api.TaskFind{PipelineID: issue.PipelineUID})- if err != nil {- return err- }- switch newStatus {- case api.IssueOpen:- case api.IssueDone:- // Returns error if any of the tasks is not DONE.- for _, task := range tasks {- if task.Status != api.TaskDone {- return &common.Error{Code: common.Conflict, Err: errors.Errorf("failed to resolve issue: %v, task %v has not finished", issue.Title, task.Name)}- }+ if issue.PipelineUID != nil {+ tasks, err := s.store.ListTasks(ctx, &api.TaskFind{PipelineID: issue.PipelineUID})+ if err != nil {+ return err}- case api.IssueCanceled:- // If we want to cancel the issue, we find the current running tasks, mark each of them CANCELED.- // We keep PENDING and FAILED tasks as is since the issue maybe reopened later, and it's better to- // keep those tasks in the same state before the issue was canceled.- for _, task := range tasks {- if task.Status == api.TaskRunning {- if err := s.PatchTaskStatus(ctx, task, &api.TaskStatusPatch{- ID: task.ID,- UpdaterID: updaterID,- Status: api.TaskCanceled,- }); err != nil {- return errors.Wrapf(err, "failed to cancel issue: %v, failed to cancel task: %v", issue.Title, task.Name)+ switch newStatus {+ case api.IssueOpen:+ case api.IssueDone:+ // Returns error if any of the tasks is not DONE.+ for _, task := range tasks {+ if task.Status != api.TaskDone {+ return &common.Error{Code: common.Conflict, Err: errors.Errorf("failed to resolve issue: %v, task %v has not finished", issue.Title, task.Name)}+ }+ }+ case api.IssueCanceled:+ // If we want to cancel the issue, we find the current running tasks, mark each of them CANCELED.+ // We keep PENDING and FAILED tasks as is since the issue maybe reopened later, and it's better to+ // keep those tasks in the same state before the issue was canceled.+ for _, task := range tasks {+ if task.Status == api.TaskRunning {+ if err := s.PatchTaskStatus(ctx, task, &api.TaskStatusPatch{+ ID: task.ID,+ UpdaterID: updaterID,+ Status: api.TaskCanceled,+ }); err != nil {+ return errors.Wrapf(err, "failed to cancel issue: %v, failed to cancel task: %v", issue.Title, task.Name)+ }}}}diff --git a/backend/server/acl.go b/backend/server/acl.goindex e2e9cc0347..c85305d0b3 100644--- a/backend/server/acl.go+++ b/backend/server/acl.go@@ -166,7 +166,7 @@ func aclMiddleware(s *Server, pathPrefix string, ce *casbin.Enforcer, next echo.}c.Request().Body = io.NopCloser(bytes.NewBuffer(bodyBytes))- aclErr = enforceWorkspaceDeveloperIssueRouteACL(s.licenseService.GetEffectivePlan(), path, method, string(bodyBytes), c.QueryParams(), principalID, getRetrieveIssueProjectID(ctx, s.store), projectRolesFinder)+ aclErr = enforceWorkspaceDeveloperIssueRouteACL(s.licenseService.GetEffectivePlan(), path, method, c.QueryParams(), principalID, getRetrieveIssueProjectID(ctx, s.store), projectRolesFinder)}if aclErr != nil {return aclErr@@ -360,9 +360,8 @@ func enforceWorkspaceDeveloperSheetRouteACL(plan api.PlanType, path string, meth}var issueStatusRegex = regexp.MustCompile(`^/issue/(?P<issueID>\d+)/status$`)-var issueRouteRegex = regexp.MustCompile(`^/issue/(?P<issueID>\d+)$`)-func enforceWorkspaceDeveloperIssueRouteACL(plan api.PlanType, path string, method string, body 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 {+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.@@ -375,23 +374,6 @@ func enforceWorkspaceDeveloperIssueRouteACL(plan api.PlanType, path string, methif principalID != userID {return echo.NewHTTPError(http.StatusUnauthorized, "not allowed to list other users' issues")}- } else if matches := issueRouteRegex.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 len(projectRoles) == 0 {- return echo.NewHTTPError(http.StatusUnauthorized, "not allowed to retrieve issue that is in the project that the user is not a member of")- }}case http.MethodPatch:// Workspace developer can only operating the issues if the user is the member of the project that the issue belongs to.@@ -413,21 +395,6 @@ func enforceWorkspaceDeveloperIssueRouteACL(plan api.PlanType, path string, methreturn echo.NewHTTPError(http.StatusUnauthorized, "not allowed to operate the issue")}}- case http.MethodPost:- if path == "/issue" {- // Workspace developer can only create issue under the project that the user is the member of.- var issueCreate api.IssueCreate- if err := jsonapi.UnmarshalPayload(strings.NewReader(body), &issueCreate); err != nil {- return echo.NewHTTPError(http.StatusBadRequest, "Malformed create issue request").SetInternal(err)- }- projectRoles, err := projectRolesFinder(issueCreate.ProjectID, principalID)- if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to process authorize request.").SetInternal(err)- }- if !api.ProjectPermission(api.ProjectPermissionChangeDatabase, plan, projectRoles) {- return echo.NewHTTPError(http.StatusUnauthorized, fmt.Sprintf("not allowed to create issues under the project %d", issueCreate.ProjectID))- }- }}return nil}diff --git a/backend/server/acl_issue_test.go b/backend/server/acl_issue_test.goindex d3c5c4d431..ce547483af 100644--- a/backend/server/acl_issue_test.go+++ b/backend/server/acl_issue_test.go@@ -18,7 +18,6 @@ func TestWorkspaceDeveloperIssueRouteACL_RetrieveIssue(t *testing.T) {method stringqueryParams url.ValuesprincipalID int- body stringerrMsg string}@@ -54,7 +53,7 @@ func TestWorkspaceDeveloperIssueRouteACL_RetrieveIssue(t *testing.T) {for _, tc := range tests {t.Run(tc.desc, func(t *testing.T) {- err := enforceWorkspaceDeveloperIssueRouteACL(tc.plan, tc.path, tc.method, tc.body, tc.queryParams, tc.principalID, testWorkspaceDeveloperIssueRouteMockGetIssueProjectID, getProjectRolesFinderForTest(testWorkspaceDeveloperIssueRouteHelper.projectMembers))+ 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)@@ -74,7 +73,6 @@ func TestWorkspaceDeveloperIssueRouteACL_OperateIssue(t *testing.T) {plan api.PlanTypepath stringmethod string- body stringqueryParams url.ValuesprincipalID interrMsg string@@ -85,7 +83,6 @@ func TestWorkspaceDeveloperIssueRouteACL_OperateIssue(t *testing.T) {desc: "Operating the issue created by other user in other projects",plan: api.ENTERPRISE,path: "/issue/403/status",- body: "",queryParams: url.Values{},method: "PATCH",principalID: 202,@@ -95,7 +92,6 @@ func TestWorkspaceDeveloperIssueRouteACL_OperateIssue(t *testing.T) {desc: "Operating the issue I created",plan: api.ENTERPRISE,path: "/issue/401/status",- body: "",queryParams: url.Values{},method: "PATCH",principalID: 202,@@ -105,7 +101,6 @@ func TestWorkspaceDeveloperIssueRouteACL_OperateIssue(t *testing.T) {desc: "Operating the issue created by other user in projects I am a member of",plan: api.ENTERPRISE,path: "/issue/402/status",- body: "",queryParams: url.Values{},method: "PATCH",principalID: 202,@@ -115,7 +110,6 @@ func TestWorkspaceDeveloperIssueRouteACL_OperateIssue(t *testing.T) {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",- body: "",queryParams: url.Values{},method: "PATCH",principalID: 204,@@ -126,7 +120,7 @@ func TestWorkspaceDeveloperIssueRouteACL_OperateIssue(t *testing.T) {for _, tc := range tests {tc := tct.Run(tc.desc, func(t *testing.T) {- err := enforceWorkspaceDeveloperIssueRouteACL(tc.plan, tc.path, tc.method, tc.body, tc.queryParams, tc.principalID, testWorkspaceDeveloperIssueRouteMockGetIssueProjectID, getProjectRolesFinderForTest(testWorkspaceDeveloperIssueRouteHelper.projectMembers))+ 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)@@ -146,48 +140,26 @@ func TestWorkspaceDeveloperIssueRouteACL_CreateIssue(t *testing.T) {plan api.PlanTypepath stringmethod string- body stringqueryParams url.ValuesprincipalID interrMsg string}tests := []test{- {- desc: "Create issue under project I am not a member of",- plan: api.ENTERPRISE,- path: "/issue",- body: `{"data":{"type":"issue","attributes":{"title":"test","description":"test","projectId":103}}}`,- queryParams: url.Values{},- method: "POST",- principalID: 202,- errMsg: "not allowed to create issues under the project 103",- },{desc: "Create issue under project I am a member of",plan: api.ENTERPRISE,path: "/issue",- body: `{"data":{"type":"issue","attributes":{"title":"test","description":"test","projectId":102}}}`,queryParams: url.Values{},method: "POST",principalID: 202,errMsg: "",},- {- desc: "Create issue under project I am a member of but I am neither a project owner nor a developer",- plan: api.ENTERPRISE,- path: "/issue",- body: `{"data":{"type":"issue","attributes":{"title":"test","description":"test","projectId":102}}}`,- queryParams: url.Values{},- method: "POST",- principalID: 204,- errMsg: "not allowed to create issues under the project 102",- },}for _, tc := range tests {t.Run(tc.desc, func(t *testing.T) {- err := enforceWorkspaceDeveloperIssueRouteACL(tc.plan, tc.path, tc.method, tc.body, tc.queryParams, tc.principalID, testWorkspaceDeveloperIssueRouteMockGetIssueProjectID, getProjectRolesFinderForTest(testWorkspaceDeveloperIssueRouteHelper.projectMembers))+ 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)diff --git a/backend/server/sql.go b/backend/server/sql.goindex f02f60b3bd..152baa6855 100644--- a/backend/server/sql.go+++ b/backend/server/sql.go@@ -3,6 +3,7 @@ package serverimport ("context""database/sql"+ "encoding/base64""encoding/json""fmt""net/http"@@ -294,11 +295,11 @@ func (s *Server) registerSQLRoutes(g *echo.Group) {// TODO(d): perfect matching condition expression.var usedExpression stringfor _, database := range databases {- databaseResourceURL := fmt.Sprintf("environments/%s/instances/%s/databases/%s", environment.ResourceID, instance.ResourceID, database.DatabaseName)+ databaseResourceURL := fmt.Sprintf("instances/%s/databases/%s", instance.ResourceID, database.DatabaseName)attributes := map[string]any{"request.time": time.Now(),"resource.database": databaseResourceURL,- "request.statement": exec.Statement,+ "request.statement": base64.StdEncoding.EncodeToString([]byte(exec.Statement)),"request.row_limit": exec.Limit,"request.export_format": exec.ExportFormat,}@@ -1152,6 +1153,7 @@ func getDatabasesFromQuery(engine db.Type, databaseName, statement string) ([]streturn nil, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Malformed sql execute request, specify database %q but access database %q", databaseName, name))}}+ return []string{databaseName}, nil}return databases, nil}