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(backend): move common helpers to owning packages (#21501)
p0ny7e1061fd6c74
diff --git a/backend/api/v1/acl.go b/backend/api/v1/acl.goindex e98882c3cc..07e3e7f0d2 100644--- a/backend/api/v1/acl.go+++ b/backend/api/v1/acl.go@@ -49,14 +49,14 @@ func (in *ACLInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {// Keyed on the code because inside doACLCheck only a permission// verdict answers PermissionDenied.if connect.CodeOf(err) == connect.CodePermissionDenied {- common.SetPermissionDenied(ctx)+ setPermissionDenied(ctx)}return nil, err}// ACL is the last interceptor on both chains, so admission here is the// call reaching its handler. It is set here, not in doACLCheck, because// the skipped-authentication return admits too.- common.SetHandlerReached(ctx)+ setHandlerReached(ctx)return next(ctx, req)}}diff --git a/backend/api/v1/audit.go b/backend/api/v1/audit.goindex 128e340c35..893b2257b7 100644--- a/backend/api/v1/audit.go+++ b/backend/api/v1/audit.go@@ -58,7 +58,7 @@ func NewAuditInterceptor(store *store.Store, secret string, profile *config.Proffunc (in *AuditInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {var serviceData *anypb.Any- ctx = common.WithSetServiceData(ctx, func(a *anypb.Any) {+ ctx = withSetServiceData(ctx, func(a *anypb.Any) {serviceData = a})@@ -66,13 +66,13 @@ func (in *AuditInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc// against. Needed for allow_without_credential methods (Login/Signup/// ExchangeToken) where the workspace is resolved inside the handler.var handlerAuditWorkspaceID string- ctx = common.WithSetAuditWorkspaceID(ctx, func(workspaceID string) {+ ctx = withSetAuditWorkspaceID(ctx, func(workspaceID string) {handlerAuditWorkspaceID = workspaceID})var permissionDenied, handlerReached bool- ctx = common.WithSetPermissionDenied(ctx, func() { permissionDenied = true })- ctx = common.WithSetHandlerReached(ctx, func() { handlerReached = true })+ ctx = withSetPermissionDenied(ctx, func() { permissionDenied = true })+ ctx = withSetHandlerReached(ctx, func() { handlerReached = true })startTime := time.Now()response, rerr := next(ctx, req)@@ -179,10 +179,10 @@ type auditEntry struct {request anyresponse anymethod string- // serviceData is populated by handlers via common.WithSetServiceData.+ // serviceData is populated by handlers via withSetServiceData.serviceData *anypb.Any// handlerAuditWorkspaceID is populated by handlers via- // common.SetAuditWorkspaceID. Used as the validated audit parent for+ // setAuditWorkspaceID. Used as the validated audit parent for// allow_without_credential methods where authContext.Resources is empty// because no workspace is in the context.handlerAuditWorkspaceID stringdiff --git a/backend/api/v1/audit_context.go b/backend/api/v1/audit_context.gonew file mode 100644index 0000000000..305d42be0d--- /dev/null+++ b/backend/api/v1/audit_context.go@@ -0,0 +1,91 @@+package v1++import (+ "context"++ "connectrpc.com/connect"+ "google.golang.org/protobuf/types/known/anypb"+)++type auditContextKey int++const (+ serviceDataKey auditContextKey = iota+ auditWorkspaceIDKey+ permissionDeniedKey+ handlerReachedKey+)++func withSetServiceData(ctx context.Context, setServiceData func(a *anypb.Any)) context.Context {+ return context.WithValue(ctx, serviceDataKey, setServiceData)+}++func getSetServiceDataFromContext(ctx context.Context) (func(a *anypb.Any), bool) {+ setServiceData, ok := ctx.Value(serviceDataKey).(func(*anypb.Any))+ return setServiceData, ok+}++// withSetAuditWorkspaceID registers a callback handlers can use to tell the+// audit interceptor which workspace a request should be audited against. This+// is needed for methods that run with allow_without_credential=true (e.g.+// Login/Signup/ExchangeToken): the workspace is unknown when the interceptor+// chain starts, but the handler learns it before returning.+func withSetAuditWorkspaceID(ctx context.Context, setAuditWorkspaceID func(workspaceID string)) context.Context {+ return context.WithValue(ctx, auditWorkspaceIDKey, setAuditWorkspaceID)+}++// setAuditWorkspaceID records the workspace that the current request should be+// audited against, if the audit interceptor registered a setter on the context.+// Safe to call even when auditing is disabled for the current method.+func setAuditWorkspaceID(ctx context.Context, workspaceID string) {+ if workspaceID == "" {+ return+ }+ setter, ok := ctx.Value(auditWorkspaceIDKey).(func(string))+ if !ok {+ return+ }+ setter(workspaceID)+}++// withSetPermissionDenied registers a callback that a permission check inside+// the audit interceptor uses to report that it refused the request. The check+// runs inside the interceptor, so a value it puts on the context cannot travel+// back out; withSetAuditWorkspaceID has the same shape for the same reason.+func withSetPermissionDenied(ctx context.Context, setPermissionDenied func()) context.Context {+ return context.WithValue(ctx, permissionDeniedKey, setPermissionDenied)+}++// setPermissionDenied reports that a permission check refused the current+// request. Mark only a verdict about the caller's permission, not an outage, a+// license gate, a workflow state or an ownership rule.+func setPermissionDenied(ctx context.Context) {+ if setter, ok := ctx.Value(permissionDeniedKey).(func()); ok {+ setter()+ }+}++// permissionDeniedError is the refusal a handler answers with when its own+// permission check turns the caller away. It marks the request, so the audit+// interceptor streams the refusal and stamps it WARNING. A license gate, a+// workflow state or an ownership rule answers connect.NewError directly: none+// of them is a verdict about a permission the caller holds.+func permissionDeniedError(ctx context.Context, err error) *connect.Error {+ setPermissionDenied(ctx)+ return connect.NewError(connect.CodePermissionDenied, err)+}++// withSetHandlerReached registers a callback the ACL interceptor uses to report+// that it admitted the request to its handler.+func withSetHandlerReached(ctx context.Context, setHandlerReached func()) context.Context {+ return context.WithValue(ctx, handlerReachedKey, setHandlerReached)+}++// setHandlerReached reports that the current request passed every interceptor+// check and is about to run its handler. The audit interceptor stores a row+// only for such a call.+func setHandlerReached(ctx context.Context) {+ if setter, ok := ctx.Value(handlerReachedKey).(func()); ok {+ setter()+ }+}diff --git a/backend/common/context_test.go b/backend/api/v1/audit_context_test.gosimilarity index 74%rename from backend/common/context_test.gorename to backend/api/v1/audit_context_test.goindex c0d0a41859..14461f7b11 100644--- a/backend/common/context_test.go+++ b/backend/api/v1/audit_context_test.go@@ -1,5 +1,4 @@-//nolint:revive-package common+package v1import ("context"@@ -14,8 +13,8 @@ func TestPermissionDeniedErrorMarksTheRequest(t *testing.T) {t.Parallel()marked := false- ctx := WithSetPermissionDenied(context.Background(), func() { marked = true })- err := PermissionDeniedError(ctx, errors.New("user does not have permission"))+ ctx := withSetPermissionDenied(context.Background(), func() { marked = true })+ err := permissionDeniedError(ctx, errors.New("user does not have permission"))require.Equal(t, connect.CodePermissionDenied, connect.CodeOf(err))require.ErrorContains(t, err, "user does not have permission")@@ -24,5 +23,5 @@ func TestPermissionDeniedErrorMarksTheRequest(t *testing.T) {// A handler built outside the interceptor chain, and every test that calls// one, reaches this with no setter registered.require.Equal(t, connect.CodePermissionDenied,- connect.CodeOf(PermissionDeniedError(context.Background(), errors.New("no setter"))))+ connect.CodeOf(permissionDeniedError(context.Background(), errors.New("no setter"))))}diff --git a/backend/api/v1/audit_test.go b/backend/api/v1/audit_test.goindex b730773a1b..05191cd7b1 100644--- a/backend/api/v1/audit_test.go+++ b/backend/api/v1/audit_test.go@@ -284,7 +284,7 @@ func newRecordingAuditInterceptor() (*AuditInterceptor, *recordingAuditLogWriter// runs the audit interceptor around a handler with no ACL between them.func admitted(handler connect.UnaryFunc) connect.UnaryFunc {return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {- common.SetHandlerReached(ctx)+ setHandlerReached(ctx)return handler(ctx, req)}}@@ -535,7 +535,7 @@ func TestAuditSinks(t *testing.T) {return func(ctx context.Context, _ connect.AnyRequest) (connect.AnyResponse, error) {switch outcome {case outcomeRefusedInHandler:- common.SetPermissionDenied(ctx)+ setPermissionDenied(ctx)return nil, connect.NewError(connect.CodePermissionDenied, errors.New("permission denied"))case outcomeFailedInHandler:return nil, connect.NewError(connect.CodeFailedPrecondition, errors.New("setting is locked"))diff --git a/backend/api/v1/auth_service.go b/backend/api/v1/auth_service.goindex 04794237e9..47b5e6a996 100644--- a/backend/api/v1/auth_service.go+++ b/backend/api/v1/auth_service.go@@ -159,7 +159,7 @@ func (s *AuthService) resolveAuthenticationWorkspaceID(ctx context.Context, work}}if workspaceID != "" {- common.SetAuditWorkspaceID(ctx, workspaceID)+ setAuditWorkspaceID(ctx, workspaceID)}return workspaceID, nil}@@ -265,7 +265,7 @@ func (s *AuthService) Login(ctx context.Context, req *connect.Request[v1pb.Loginreturn nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to provision workspace"))}}- common.SetAuditWorkspaceID(ctx, workspaceID)+ setAuditWorkspaceID(ctx, workspaceID)// 4. Post-auth checks (deleted, domain, license). The fetched restriction// is reused by needResetPassword below to spare a duplicate settings read.@@ -377,7 +377,7 @@ func (s *AuthService) Signup(ctx context.Context, req *connect.Request[v1pb.Signreturn nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to resolve workspace"))}// Announce it on every exit path so denied signups still produce audit entries.- common.SetAuditWorkspaceID(ctx, workspaceID)+ setAuditWorkspaceID(ctx, workspaceID)restriction, err := getAccountRestriction(ctx, s.store, s.licenseService, s.profile.SaaS, workspaceID)if err != nil {@@ -949,7 +949,7 @@ func (s *AuthService) SwitchWorkspace(ctx context.Context, req *connect.Request[return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to find workspace"))}if ws == nil {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("not a member of workspace %q", workspaceID))+ return nil, permissionDeniedError(ctx, errors.Errorf("not a member of workspace %q", workspaceID))}// Validate the target workspace's sign-in policies.@@ -1197,7 +1197,7 @@ func (s *AuthService) ExchangeToken(ctx context.Context, req *connect.Request[v1// Announce the workspace as soon as we know it (from the WI record) so// that a deactivated-WI attempt — which compliance wants to see — still// lands in the audit log.- common.SetAuditWorkspaceID(ctx, wi.Workspace)+ setAuditWorkspaceID(ctx, wi.Workspace)if wi.MemberDeleted {return nil, connect.NewError(connect.CodeUnauthenticated,errors.New("workload identity has been deactivated"))diff --git a/backend/api/v1/auth_service_email_code.go b/backend/api/v1/auth_service_email_code.goindex 10f6d28279..780d99db9f 100644--- a/backend/api/v1/auth_service_email_code.go+++ b/backend/api/v1/auth_service_email_code.go@@ -112,7 +112,7 @@ func (s *AuthService) ResetPassword(ctx context.Context, req *connect.Request[v1if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to resolve workspace"))}- common.SetAuditWorkspaceID(ctx, workspaceID)+ setAuditWorkspaceID(ctx, workspaceID)restriction, err := getAccountRestriction(ctx, s.store, s.licenseService, s.profile.SaaS, workspaceID)if err != nil {return nil, err@@ -329,7 +329,7 @@ func (s *AuthService) parseAndSetAuditWorkspace(ctx context.Context, email strin})if accountErr == nil && account != nil && account.Type == storepb.PrincipalType_END_USER && !account.MemberDeleted &&workspaceErr == nil && workspace != nil {- common.SetAuditWorkspaceID(ctx, workspace.ResourceID)+ setAuditWorkspaceID(ctx, workspace.ResourceID)}}return workspaceID, nildiff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.goindex cf70d50448..db5c47de78 100644--- a/backend/api/v1/database_service.go+++ b/backend/api/v1/database_service.go@@ -203,7 +203,7 @@ func (s *DatabaseService) BatchGetDatabases(ctx context.Context, req *connect.Re// Same code and message as a missing database: a different one would// tell the caller a database exists in a project they cannot see. The// mark records the refusal the caller is not told about.- common.SetPermissionDenied(ctx)+ setPermissionDenied(ctx)return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("database %q not found", name))}database, err := s.convertToDatabase(ctx, databaseMessage)@@ -301,7 +301,7 @@ func (s *DatabaseService) ListDatabases(ctx context.Context, req *connect.Requesreturn nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to check permission with error: %v", err))}if !ok {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q in %q", permission.InstancesGet, req.Msg.Parent))+ return nil, permissionDeniedError(ctx, errors.Errorf("user does not have permission %q in %q", permission.InstancesGet, req.Msg.Parent))}find.InstanceID = &instanceID} else if projectID, err := common.GetProjectID(req.Msg.Parent); err == nil {@@ -320,7 +320,7 @@ func (s *DatabaseService) ListDatabases(ctx context.Context, req *connect.Requesreturn nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to check permission with error: %v", err))}if !ok {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q in %q", permission.ProjectsGet, req.Msg.Parent))+ return nil, permissionDeniedError(ctx, errors.Errorf("user does not have permission %q in %q", permission.ProjectsGet, req.Msg.Parent))}find.ProjectID = &projectID} else if _, err := common.GetWorkspaceID(req.Msg.Parent); err == nil {@@ -329,7 +329,7 @@ func (s *DatabaseService) ListDatabases(ctx context.Context, req *connect.Requesreturn nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to check permission with error: %v", err.Error()))}if !ok {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.DatabasesList))+ return nil, permissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.DatabasesList))}} else if instanceID, err := common.GetInstanceID(req.Msg.Parent); err == nil {if _, err := s.getInstanceForDatabaseResource(ctx, nil, instanceID); err != nil {@@ -340,7 +340,7 @@ func (s *DatabaseService) ListDatabases(ctx context.Context, req *connect.Requesreturn nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to check permission with error: %v", err.Error()))}if !ok {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.InstancesGet))+ return nil, permissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.InstancesGet))}find.InstanceID = &instanceID} else {diff --git a/backend/api/v1/group_service.go b/backend/api/v1/group_service.goindex ccf9e15327..77d3b9c686 100644--- a/backend/api/v1/group_service.go+++ b/backend/api/v1/group_service.go@@ -184,7 +184,7 @@ func (s *GroupService) UpdateGroup(ctx context.Context, req *connect.Request[v1preturn nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to check permission"))}if !ok {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.GroupsCreate))+ return nil, permissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.GroupsCreate))}return s.CreateGroup(ctx, connect.NewRequest(&v1pb.CreateGroupRequest{Group: req.Msg.Group,@@ -286,7 +286,7 @@ func (s *GroupService) checkPermission(ctx context.Context, group *store.GroupMereturn connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to check permission"))}if !ok {- return common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission))+ return permissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission))}return nil}@@ -373,7 +373,7 @@ func (s *GroupService) checkGroupPermission(ctx context.Context, req connect.Anyreturn connect.NewError(connect.CodeInternal, errors.Errorf("failed to check permission with error: %v", err.Error()))}if !ok {- err := common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.GroupsGet))+ err := permissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.GroupsGet))if detail, detailErr := connect.NewErrorDetail(&v1pb.PermissionDeniedDetail{Method: req.Spec().Procedure,RequiredPermissions: []string{string(permission.GroupsGet)},diff --git a/backend/api/v1/issue_review.go b/backend/api/v1/issue_review.goindex 9efb4fcafd..fd145f67b9 100644--- a/backend/api/v1/issue_review.go+++ b/backend/api/v1/issue_review.go@@ -131,7 +131,7 @@ func mapReviewError(ctx context.Context, err error, action review.Action) error// two refusals with this code are a project self-approval setting and// an issue-ownership rule, which the design leaves unmarked.if workflowErr.Reason == review.ReasonApproverRoleRequired {- return common.PermissionDeniedError(ctx, workflowErr)+ return permissionDeniedError(ctx, workflowErr)}return connect.NewError(connect.CodePermissionDenied, workflowErr)case review.ErrorConflict:diff --git a/backend/api/v1/issue_review_test.go b/backend/api/v1/issue_review_test.goindex b827e83751..d0bef6bf2f 100644--- a/backend/api/v1/issue_review_test.go+++ b/backend/api/v1/issue_review_test.go@@ -8,7 +8,6 @@ import ("github.com/pkg/errors""github.com/stretchr/testify/require"- "github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/component/review")@@ -41,7 +40,7 @@ func TestMapReviewErrorMarksOnlyTheApproverRoleVerdict(t *testing.T) {t.Run(test.name, func(t *testing.T) {t.Parallel()marked := false- ctx := common.WithSetPermissionDenied(context.Background(), func() { marked = true })+ ctx := withSetPermissionDenied(context.Background(), func() { marked = true })err := mapReviewError(ctx, test.err, review.ActionApprove)diff --git a/backend/api/v1/issue_service.go b/backend/api/v1/issue_service.goindex 490179e814..00b98430ff 100644--- a/backend/api/v1/issue_service.go+++ b/backend/api/v1/issue_service.go@@ -394,7 +394,7 @@ func (s *IssueService) SearchIssues(ctx context.Context, req *connect.Request[v1return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to check permission %q", permission.IssuesGet))}if !ok {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q in %q", permission.IssuesGet, req.Msg.Parent))+ return nil, permissionDeniedError(ctx, errors.Errorf("user does not have permission %q in %q", permission.IssuesGet, req.Msg.Parent))}projectIDs = append(projectIDs, projectID)}@@ -914,7 +914,7 @@ func (s *IssueService) UpdateIssue(ctx context.Context, req *connect.Request[v1preturn nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to check permission"))}if !ok {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.IssuesCreate))+ return nil, permissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.IssuesCreate))}// Extract project ID from the issue name (format: projects/{project}/issues/{issue})@@ -1392,7 +1392,7 @@ func (s *IssueService) UpdateIssueComment(ctx context.Context, req *connect.Requreturn nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to check permission"))}if !ok {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.IssueCommentsCreate))+ return nil, permissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.IssueCommentsCreate))}// Creation applies the complete resource regardless of the update mask.return s.CreateIssueComment(ctx, connect.NewRequest(&v1pb.CreateIssueCommentRequest{diff --git a/backend/api/v1/mcp_gate.go b/backend/api/v1/mcp_gate.goindex 64a7633265..f7999dc107 100644--- a/backend/api/v1/mcp_gate.go+++ b/backend/api/v1/mcp_gate.go@@ -312,7 +312,7 @@ func mcpSettingsForCurrentWorkspace(ctx context.Context, reader mcpSettingsReade// row). An admin tightening the ceiling binds the next request of a session// already open; work already admitted finishes.//-// A policy denial is marked (common.SetPermissionDenied), so the audit+// A policy denial is marked (setPermissionDenied), so the audit// interceptor streams it whatever the method's audit annotation says. That is// why redaction covers every refused request, not only the audited RPCs.//@@ -362,7 +362,7 @@ func (in *internalMCPGateInterceptor) WrapUnary(next connect.UnaryFunc) connect.// The audit interceptor wraps this one and reads the mark when the// request comes back out. Only a verdict about the caller is marked —// an unreadable ceiling and a broken chain are not policy denials.- common.SetPermissionDenied(ctx)+ setPermissionDenied(ctx)}return nil, err}diff --git a/backend/api/v1/mcp_gate_test.go b/backend/api/v1/mcp_gate_test.goindex c11155aee2..4bdb33629a 100644--- a/backend/api/v1/mcp_gate_test.go+++ b/backend/api/v1/mcp_gate_test.go@@ -851,7 +851,7 @@ func invokeMCPGate(t *testing.T, stores mcpSettingsReader, authCtx *common.AuthCctx = context.WithValue(ctx, common.AuthContextKey, authCtx)}ctx = context.WithValue(ctx, common.WorkspaceIDContextKey, auditTestWorkspace)- ctx = common.WithSetPermissionDenied(ctx, func() { out.auditMarked = true })+ ctx = withSetPermissionDenied(ctx, func() { out.auditMarked = true })_, out.err = NewInternalMCPGateInterceptor(stores).WrapUnary(next)(ctx,&specRequest{AnyRequest: req, procedure: procedure})return outdiff --git a/backend/api/v1/org_policy_service.go b/backend/api/v1/org_policy_service.goindex ccc935e179..677539c571 100644--- a/backend/api/v1/org_policy_service.go+++ b/backend/api/v1/org_policy_service.go@@ -475,7 +475,7 @@ func (s *OrgPolicyService) checkPolicyPermission(ctx context.Context, req connecreturn connect.NewError(connect.CodeInternal, errors.Errorf("failed to check permission with error: %v", err.Error()))}if !ok {- err := common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q", perm))+ err := permissionDeniedError(ctx, errors.Errorf("user does not have permission %q", perm))if detail, detailErr := connect.NewErrorDetail(&v1pb.PermissionDeniedDetail{Method: req.Spec().Procedure,RequiredPermissions: []string{string(perm)},diff --git a/backend/api/v1/plan_service.go b/backend/api/v1/plan_service.goindex 1dac991dbf..d04be593bb 100644--- a/backend/api/v1/plan_service.go+++ b/backend/api/v1/plan_service.go@@ -264,7 +264,7 @@ func (s *PlanService) UpdatePlan(ctx context.Context, request *connect.Request[vreturn nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to check permission"))}if !ok {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.PlansCreate))+ return nil, permissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.PlansCreate))}return s.CreatePlan(ctx, connect.NewRequest(&v1pb.CreatePlanRequest{Parent: common.FormatProject(projectID),@@ -293,7 +293,7 @@ func (s *PlanService) UpdatePlan(ctx context.Context, request *connect.Request[vreturn nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to check permission"))}if !ok {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("permission denied to update plan"))+ return nil, permissionDeniedError(ctx, errors.Errorf("permission denied to update plan"))}var title *stringdiff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.goindex cf9d7ff20c..6c192b6813 100644--- a/backend/api/v1/project_service.go+++ b/backend/api/v1/project_service.go@@ -639,7 +639,7 @@ func (s *ProjectService) SetIamPolicy(ctx context.Context, req *connect.Request[return nil, connect.NewError(connect.CodeInternal, err)}- if setServiceData, ok := common.GetSetServiceDataFromContext(ctx); ok {+ if setServiceData, ok := getSetServiceDataFromContext(ctx); ok {deltas := findIamPolicyDeltas(replaced.Policy, iamPolicyMessage.Policy)p, err := convertToProtoAny(deltas)if err != nil {diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.goindex 21c9f295df..100021d26f 100644--- a/backend/api/v1/rollout_service.go+++ b/backend/api/v1/rollout_service.go@@ -285,7 +285,7 @@ func (s *RolloutService) CreateRollout(ctx context.Context, req *connect.Request}if !hasPermission {- return nil, common.PermissionDeniedError(ctx, errors.New("permission denied to create rollout"))+ return nil, permissionDeniedError(ctx, errors.New("permission denied to create rollout"))}if err := rejectMCPOriginatedIssuelessRollout(ctx, project, issue, "create a rollout"); err != nil {@@ -864,7 +864,7 @@ func (s *RolloutService) BatchRunTasks(ctx context.Context, req *connect.Requestreturn nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to check if the user can run tasks"))}if !ok {- return nil, common.PermissionDeniedError(ctx, errors.New("Not allowed to run tasks"))+ return nil, permissionDeniedError(ctx, errors.New("Not allowed to run tasks"))}if err := rejectMCPOriginatedIssuelessRollout(ctx, project, issueN, "run tasks"); err != nil {@@ -1026,7 +1026,7 @@ func (s *RolloutService) BatchSkipTasks(ctx context.Context, req *connect.Requesreturn nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to check if the user can skip tasks"))}if !ok {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("not allowed to skip tasks in environment %q", environment))+ return nil, permissionDeniedError(ctx, errors.Errorf("not allowed to skip tasks in environment %q", environment))}}@@ -1140,7 +1140,7 @@ func (s *RolloutService) BatchCancelTaskRuns(ctx context.Context, req *connect.Rreturn nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to check if the user can run tasks"))}if !ok {- return nil, common.PermissionDeniedError(ctx, errors.New("Not allowed to cancel tasks"))+ return nil, permissionDeniedError(ctx, errors.New("Not allowed to cancel tasks"))}// Confine the lookup to the plan and environment just authorized: the stage indiff --git a/backend/api/v1/saved_query_service.go b/backend/api/v1/saved_query_service.goindex 2bda0a0426..9d274e7f29 100644--- a/backend/api/v1/saved_query_service.go+++ b/backend/api/v1/saved_query_service.go@@ -637,7 +637,7 @@ func (s *SavedQueryService) checkProjectWide(ctx context.Context, user *store.Usreturn connect.NewError(connect.CodeInternal, errors.Errorf("failed to check permission with error: %v", err))}if !ok {- return common.PermissionDeniedError(ctx, errors.Errorf("permission denied: %s", p))+ return permissionDeniedError(ctx, errors.Errorf("permission denied: %s", p))}return nil}@@ -1004,7 +1004,7 @@ func (s *SavedQueryService) hasSavedQueryPermission(ctx context.Context, user *sif !ok {// Callers answer NotFound to hide the saved query's existence, so the// mark is the only record that a permission check refused the caller.- common.SetPermissionDenied(ctx)+ setPermissionDenied(ctx)}return ok, nil}diff --git a/backend/api/v1/setting_service.go b/backend/api/v1/setting_service.goindex 4ff829a620..f281a4fd83 100644--- a/backend/api/v1/setting_service.go+++ b/backend/api/v1/setting_service.go@@ -173,7 +173,7 @@ func (s *SettingService) UpdateSetting(ctx context.Context, request *connect.Reqreturn nil, connect.NewError(connect.CodeNotFound, errors.Errorf("setting %s not found", settingName))}// audit log.- if setServiceData, ok := common.GetSetServiceDataFromContext(ctx); ok && existedSetting != nil {+ if setServiceData, ok := getSetServiceDataFromContext(ctx); ok && existedSetting != nil {v1pbSetting, err := convertToSettingMessage(existedSetting)if err != nil {slog.Warn("audit: failed to convert to v1.Setting", log.BBError(err))@@ -582,7 +582,7 @@ func (s *SettingService) updateAppIMSetting(ctx context.Context, request *connec// Re-capture the audit before-image from the locked row the merge ran// against, overwriting UpdateSetting's earlier pre-lock snapshot.- if setServiceData, ok := common.GetSetServiceDataFromContext(ctx); ok && lockedBefore != nil {+ if setServiceData, ok := getSetServiceDataFromContext(ctx); ok && lockedBefore != nil {v1pbSetting, err := convertToSettingMessage(&store.SettingMessage{Name: storepb.SettingName_APP_IM,Workspace: workspaceID,@@ -805,7 +805,7 @@ func (s *SettingService) updateWorkspaceProfileSetting(ctx context.Context, requ// Re-capture the audit before-image from the locked row the merge ran// against, overwriting UpdateSetting's earlier pre-lock snapshot.- if setServiceData, ok := common.GetSetServiceDataFromContext(ctx); ok && lockedBefore != nil {+ if setServiceData, ok := getSetServiceDataFromContext(ctx); ok && lockedBefore != nil {v1pbSetting, err := convertToSettingMessage(&store.SettingMessage{Name: storepb.SettingName_WORKSPACE_PROFILE,Workspace: workspaceID,@@ -1108,7 +1108,7 @@ func (s *SettingService) checkSettingPermission(ctx context.Context, req connectreturn connect.NewError(connect.CodeInternal, errors.Errorf("failed to check permission with error: %v", err.Error()))}if !ok {- err := common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q", perm))+ err := permissionDeniedError(ctx, errors.Errorf("user does not have permission %q", perm))if detail, detailErr := connect.NewErrorDetail(&v1pb.PermissionDeniedDetail{Method: req.Spec().Procedure,RequiredPermissions: []string{string(perm)},diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.goindex 6d436fbf2c..8d168a93cf 100644--- a/backend/api/v1/sql_service.go+++ b/backend/api/v1/sql_service.go@@ -350,7 +350,7 @@ func (s *SQLService) Query(ctx context.Context, req *connect.Request[v1pb.QueryR// one point that can see the request's argument. Query is audited// and this runs in its handler, so the row is stored either way;// the mark stamps it WARNING.- common.SetPermissionDenied(ctx)+ setPermissionDenied(ctx)return nil, err}}@@ -1445,7 +1445,7 @@ func (s *SQLService) accessCheckWithGrantedTargets(// inside its response rather than as an RPC error, so connect.CodeOf// cannot see this verdict. The mark is the only way it reaches the// audit interceptor.- common.SetPermissionDenied(ctx)+ setPermissionDenied(ctx)return &queryError{err: connect.NewError(connect.CodePermissionDenied,@@ -1539,7 +1539,7 @@ func (s *SQLService) accessCheckWithGrantedTargets(return err}if len(deniedResources) > 0 {- common.SetPermissionDenied(ctx)+ setPermissionDenied(ctx)return &queryError{err: connect.NewError(connect.CodePermissionDenied,@@ -1607,7 +1607,7 @@ func (s *SQLService) accessCheckWithGrantedTargets(}}if len(deniedResources) > 0 {- common.SetPermissionDenied(ctx)+ setPermissionDenied(ctx)return &queryError{err: connect.NewError(connect.CodePermissionDenied,diff --git a/backend/api/v1/user_service.go b/backend/api/v1/user_service.goindex 07b8d943a0..c555b2cbc6 100644--- a/backend/api/v1/user_service.go+++ b/backend/api/v1/user_service.go@@ -179,7 +179,7 @@ func (s *UserService) ListUsers(ctx context.Context, request *connect.Request[v1return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to check user permission"))}if !hasPermission {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.ProjectsGet))+ return nil, permissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.ProjectsGet))}} else if s.profile.SaaS {find.Workspace = common.GetWorkspaceIDFromContext(ctx)@@ -342,7 +342,7 @@ func (s *UserService) UpdateUser(ctx context.Context, request *connect.Request[vreturn nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to check permission with error: %v", err.Error()))}if !ok {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.UsersCreate))+ return nil, permissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.UsersCreate))}return s.CreateUser(ctx, connect.NewRequest(&v1pb.CreateUserRequest{User: request.Msg.User,@@ -365,7 +365,7 @@ func (s *UserService) UpdateUser(ctx context.Context, request *connect.Request[vreturn nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to check permission with error: %v", err.Error()))}if !ok {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.UsersUpdate))+ return nil, permissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.UsersUpdate))}}@@ -451,7 +451,7 @@ func (s *UserService) DeleteUser(ctx context.Context, request *connect.Request[vreturn nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to check permission with error: %v", err.Error()))}if !ok {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.UsersDelete))+ return nil, permissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.UsersDelete))}email, err := common.GetUserEmail(request.Msg.Name)@@ -539,7 +539,7 @@ func (s *UserService) UndeleteUser(ctx context.Context, request *connect.Requestreturn nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to check permission with error: %v", err.Error()))}if !ok {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.UsersUndelete))+ return nil, permissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.UsersUndelete))}email, err := common.GetUserEmail(request.Msg.Name)@@ -591,7 +591,7 @@ func (s *UserService) UpdateEmail(ctx context.Context, request *connect.Request[return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to check permission with error: %v", err.Error()))}if !ok {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.UsersUpdateEmail))+ return nil, permissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.UsersUpdateEmail))}// Get user by email from the name field@@ -916,7 +916,7 @@ func (s *UserService) DisableMFA(ctx context.Context, request *connect.Request[vreturn nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to check permission with error: %v", err.Error()))}if !hasPermission {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.UsersUpdate))+ return nil, permissionDeniedError(ctx, errors.Errorf("user does not have permission %q", permission.UsersUpdate))}}diff --git a/backend/api/v1/workspace_service.go b/backend/api/v1/workspace_service.goindex 8c3c9a5dee..d545cb30c8 100644--- a/backend/api/v1/workspace_service.go+++ b/backend/api/v1/workspace_service.go@@ -208,7 +208,7 @@ func (s *WorkspaceService) RotateDirectorySyncToken(ctx context.Context, req *coreturn nil, connect.NewError(connect.CodeInvalidArgument, errors.Wrapf(err, "invalid workspace name %q", req.Msg.GetName()))}if requested != workspaceID {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("workspace mismatch"))+ return nil, permissionDeniedError(ctx, errors.Errorf("workspace mismatch"))}// The update path this replaces gated on the same feature. Without it an@@ -276,7 +276,7 @@ func (s *WorkspaceService) DeleteWorkspace(ctx context.Context, req *connect.Reqreturn nil, connect.NewError(connect.CodeInvalidArgument, err)}if contextWorkspaceID := common.GetWorkspaceIDFromContext(ctx); workspaceID != contextWorkspaceID {- return nil, common.PermissionDeniedError(ctx, errors.Errorf("cannot delete workspace %q from workspace %q", workspaceID, contextWorkspaceID))+ return nil, permissionDeniedError(ctx, errors.Errorf("cannot delete workspace %q from workspace %q", workspaceID, contextWorkspaceID))}// Cancel active Stripe subscription if any.@@ -536,7 +536,7 @@ func (s *WorkspaceService) SetIamPolicy(ctx context.Context, req *connect.Requesdeltas := findIamPolicyDeltas(replaced.Policy, policy.Policy)- if setServiceData, ok := common.GetSetServiceDataFromContext(ctx); ok {+ if setServiceData, ok := getSetServiceDataFromContext(ctx); ok {p, err := convertToProtoAny(deltas)if err != nil {slog.Warn("audit: failed to convert to anypb.Any")diff --git a/backend/common/context.go b/backend/common/context.goindex 3a356ef62c..4d4105d09c 100644--- a/backend/common/context.go+++ b/backend/common/context.go@@ -4,10 +4,6 @@ package commonimport ("context"- "connectrpc.com/connect"-- "google.golang.org/protobuf/types/known/anypb"-v1pb "github.com/bytebase/bytebase/backend/generated-go/v1")@@ -18,87 +14,9 @@ const (// UserContextKey is the key name used to store user message in the context.UserContextKey ContextKey = iotaAuthContextKey- ServiceDataKeyWorkspaceIDContextKey- AuditWorkspaceIDKey- PermissionDeniedKey- HandlerReachedKey)-func WithSetServiceData(ctx context.Context, setServiceData func(a *anypb.Any)) context.Context {- return context.WithValue(ctx, ServiceDataKey, setServiceData)-}--func GetSetServiceDataFromContext(ctx context.Context) (func(a *anypb.Any), bool) {- setServiceData, ok := ctx.Value(ServiceDataKey).(func(*anypb.Any))- return setServiceData, ok-}--// WithSetAuditWorkspaceID registers a callback handlers can use to tell the-// audit interceptor which workspace a request should be audited against. This-// is needed for methods that run with allow_without_credential=true (e.g.-// Login/Signup/ExchangeToken): the workspace is unknown when the interceptor-// chain starts, but the handler learns it before returning.-func WithSetAuditWorkspaceID(ctx context.Context, setAuditWorkspaceID func(workspaceID string)) context.Context {- return context.WithValue(ctx, AuditWorkspaceIDKey, setAuditWorkspaceID)-}--// SetAuditWorkspaceID records the workspace that the current request should be-// audited against, if the audit interceptor registered a setter on the context.-// Safe to call even when auditing is disabled for the current method.-func SetAuditWorkspaceID(ctx context.Context, workspaceID string) {- if workspaceID == "" {- return- }- setter, ok := ctx.Value(AuditWorkspaceIDKey).(func(string))- if !ok {- return- }- setter(workspaceID)-}--// WithSetPermissionDenied registers a callback that a permission check inside-// the audit interceptor uses to report that it refused the request. The check-// runs inside the interceptor, so a value it puts on the context cannot travel-// back out; WithSetAuditWorkspaceID has the same shape for the same reason.-func WithSetPermissionDenied(ctx context.Context, setPermissionDenied func()) context.Context {- return context.WithValue(ctx, PermissionDeniedKey, setPermissionDenied)-}--// SetPermissionDenied reports that a permission check refused the current-// request. Mark only a verdict about the caller's permission, not an outage, a-// license gate, a workflow state or an ownership rule.-func SetPermissionDenied(ctx context.Context) {- if setter, ok := ctx.Value(PermissionDeniedKey).(func()); ok {- setter()- }-}--// PermissionDeniedError is the refusal a handler answers with when its own-// permission check turns the caller away. It marks the request, so the audit-// interceptor streams the refusal and stamps it WARNING. A license gate, a-// workflow state or an ownership rule answers connect.NewError directly: none-// of them is a verdict about a permission the caller holds.-func PermissionDeniedError(ctx context.Context, err error) *connect.Error {- SetPermissionDenied(ctx)- return connect.NewError(connect.CodePermissionDenied, err)-}--// WithSetHandlerReached registers a callback the ACL interceptor uses to report-// that it admitted the request to its handler.-func WithSetHandlerReached(ctx context.Context, setHandlerReached func()) context.Context {- return context.WithValue(ctx, HandlerReachedKey, setHandlerReached)-}--// SetHandlerReached reports that the current request passed every interceptor-// check and is about to run its handler. The audit interceptor stores a row-// only for such a call.-func SetHandlerReached(ctx context.Context) {- if setter, ok := ctx.Value(HandlerReachedKey).(func()); ok {- setter()- }-}-// GetWorkspaceIDFromContext returns the workspace ID from the request context.func GetWorkspaceIDFromContext(ctx context.Context) string {if v, ok := ctx.Value(WorkspaceIDContextKey).(string); ok {diff --git a/backend/common/util.go b/backend/common/util.goindex 003fdc86a8..d4de41f25b 100644--- a/backend/common/util.go+++ b/backend/common/util.go@@ -3,7 +3,6 @@ package commonimport ("crypto/rand"- "encoding/base64""fmt""math""math/big"@@ -106,38 +105,6 @@ func TruncateString(str string, limit int) (string, bool) {return str, false}-// TruncateStringWithDescription tries to truncate the string and append "... (view details in Bytebase)" if truncated.-func TruncateStringWithDescription(str string) string {- const limit = 450- if truncatedStr, truncated := TruncateString(str, limit); truncated {- return fmt.Sprintf("%s... (view details in Bytebase)", truncatedStr)- }- return str-}--// Obfuscate obfuscates a string with a seed string.-func Obfuscate(src, seed string) string {- srcBytes, seedBytes := []byte(src), []byte(seed)- obfuscated := make([]byte, len(srcBytes))- for i, b := range srcBytes {- obfuscated[i] = b ^ seedBytes[i%len(seedBytes)]- }- return base64.StdEncoding.EncodeToString(obfuscated)-}--// Unobfuscate unobfuscates a string with a seed string.-func Unobfuscate(dst, seed string) (string, error) {- obfuscated, err := base64.StdEncoding.DecodeString(dst)- if err != nil {- return "", err- }- unobfuscated, seedBytes := make([]byte, len(obfuscated)), []byte(seed)- for i, b := range obfuscated {- unobfuscated[i] = b ^ seedBytes[i%len(seedBytes)]- }- return string(unobfuscated), nil-}-// NormalizeExternalURL will format the external url.func NormalizeExternalURL(externalURL string) (string, error) {r := strings.TrimSpace(externalURL)diff --git a/backend/common/util_test.go b/backend/common/util_test.goindex 2366732da3..6372844cdc 100644--- a/backend/common/util_test.go+++ b/backend/common/util_test.go@@ -126,47 +126,6 @@ func TestTruncateString(t *testing.T) {}}-func TestObfuscate(t *testing.T) {- tests := []struct {- src string- seed string- dst string- }{- {- src: "",- seed: "01234567890123456789012345678901", // 32 bytes.- dst: "",- },- {- src: "hello",- seed: "01234567890123456789012345678901", // 32 bytes.- dst: "WFReX1s=",- },- {- src: "你好!",- seed: "ENuef1JjSvQ6VPfgrB33T2mkshhwRRjp", // 32 bytes.- dst: "ofPVgMOMaw==",- },- {- src: "Bytebase is a database tool for developers. Bytebase 是个数据库 DevOps 工具。",- seed: "01234567890123456789012345678901", // 32 bytes.- dst: "ckhGVlZURVIYUEMRUxNQVEJWWlhDVBJHW1paF15WQhFUVERWWFpGUkpKHhFwSkBQVFZLXBDXqpzQjZzRrYnWvJ7UiKAUcVNBd0lDEdeEkdCzgNu5sg==",- },- {- src: `{ "type": "service_account", "project_id": "spanner-test-371702", "private_key_id": "klsdjfklasjdfas\nklsdjaflkajefjlaksdjf\nlsajdfklsjaldkfjkasldjf\nD PRIVATE KEY-----\n, "client_email": "[email protected]", "client_id": "102052620181224568340", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/test-768%40spanner-test-371702.iam.gserviceaccount.com" }`,- seed: `aGgQpKjg7fuwNV6B31sIRQ1qm4Ttqw9s`, // 32 bytes.- dst: `GmdHcVI/ExdSRE9XbCVTMEVYECwNMFISAkE6AFNbGVNBZRcjHyEPBEM5HBNsbBZgQEESJzw0Q1wZUScAXEQOQlZ3VXNca0pHFRYHHjg3QidsWhYwDThVU1cUdh8dBF0ZBywLMAMhDgFWFSkZJTpFJllQFSU5MFsUC144FRoEXRkHGwk9AyoAA1ENGQQkN1omWFcZIjMiXRUHUggaNVdpISgRJgU1ayEibktYWmN7aiwfEVNpcDJdGAhaICsUGlgaDWVdcVI/DxRDS0JBdhZFMlJfHSwgfEUUHkB5R0ZGDkNTaQ4wHWUNFFIUAx4tM1chUF4GJyZ/Uh4AFnhUUVcbEA0uAj8EFAMDFVxVVX9mBHIGA0V7YmAJQF8GYEFHTwpHUWVLcVBrSAZCEh0oOyRfYAkRUSEmJUECVxt7FRIUVgYPMxR/FyQFAFsDWxQhOxktHF4SPCY5A14MQSAcU1sZU0FlEz4bLgQ4QhQcVXR2FCpHRQM6aH4eHgxBIBxDWV4cDiALNBE7AxQZBRoaYSJZKVZfUWVycRFTDEEgHC4HSxwXLgM0AhQSUgdfKhQrJEIdRkMfa2hxExkZQCQHS1gWBBYwSTYfJA0LUgcFHj14VS1eHhwoJyVZQ0JCZVsSEksHEmVLcVBrSARbDxAZOglOdwMILCo3I0UuGEY4VktXGxsVMxciSmRFEEARWxAhOVEuVlADICF/Uh4AGyYbExhNXBd2SDwVPwsDVhIUWDZjBnscRRY6JnwGR1URYEQCB1gdDyIVfAQuGRMaVUJGeWYEbFpQHmc1IlQDG103ERAUWhwUKRN/EyQHRRcb`,- },- }- for _, test := range tests {- obfuscated := Obfuscate(test.src, test.seed)- require.Equal(t, test.dst, obfuscated)- ubobfuscated, err := Unobfuscate(obfuscated, test.seed)- require.NoError(t, err)- require.Equal(t, test.src, ubobfuscated)- }-}-func TestNormalizeExternalURL(t *testing.T) {tests := []struct {url stringdiff --git a/backend/component/webhook/manager.go b/backend/component/webhook/manager.goindex af60aae322..9d09b172f7 100644--- a/backend/component/webhook/manager.go+++ b/backend/component/webhook/manager.go@@ -254,7 +254,7 @@ func (m *Manager) postWebhookList(ctx context.Context, webhookCtx *webhook.ContewebhookCtx.CreatedTS = time.Now().Unix()webhookCtx.DirectMessage = hook.Payload.GetDirectMessage()go func(webhookCtx *webhook.Context, hook *store.ProjectWebhookMessage) {- if err := common.Retry(ctx, func() error {+ if err := webhook.Retry(ctx, func() error {return webhook.Post(hook.Payload.GetType(), *webhookCtx)}); err != nil {// The external webhook endpoint might be invalid which is out of our code control, so we just emit a warningdiff --git a/backend/plugin/webhook/dingtalk/dingtalk.go b/backend/plugin/webhook/dingtalk/dingtalk.goindex 050f93c7d8..4b6245f40f 100644--- a/backend/plugin/webhook/dingtalk/dingtalk.go+++ b/backend/plugin/webhook/dingtalk/dingtalk.go@@ -16,7 +16,6 @@ import ("github.com/pkg/errors""go.uber.org/multierr"- "github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/webhook"@@ -88,7 +87,7 @@ func sendDirectMessage(webhookCtx webhook.Context) bool {ctx := context.Background()sent := map[string]bool{}- if err := common.Retry(ctx, func() error {+ if err := webhook.Retry(ctx, func() error {var errs errorvar userPhones, userIDs []stringfor _, u := range webhookCtx.MentionEndUsers {diff --git a/backend/plugin/webhook/feishu/feishu.go b/backend/plugin/webhook/feishu/feishu.goindex f82a97f626..eb32f763dc 100644--- a/backend/plugin/webhook/feishu/feishu.go+++ b/backend/plugin/webhook/feishu/feishu.go@@ -13,7 +13,6 @@ import ("github.com/pkg/errors""go.uber.org/multierr"- "github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/webhook"@@ -136,7 +135,7 @@ func postDirectMessage(webhookCtx webhook.Context) bool {sent := map[string]bool{}- if err := common.Retry(ctx, func() error {+ if err := webhook.Retry(ctx, func() error {var errs errorvar emails []stringdiff --git a/backend/plugin/webhook/format.go b/backend/plugin/webhook/format.gonew file mode 100644index 0000000000..0b7c7122e3--- /dev/null+++ b/backend/plugin/webhook/format.go@@ -0,0 +1,16 @@+package webhook++import (+ "fmt"++ "github.com/bytebase/bytebase/backend/common"+)++// TruncateStringWithDescription tries to truncate the string and append "... (view details in Bytebase)" if truncated.+func TruncateStringWithDescription(str string) string {+ const limit = 450+ if truncatedStr, truncated := common.TruncateString(str, limit); truncated {+ return fmt.Sprintf("%s... (view details in Bytebase)", truncatedStr)+ }+ return str+}diff --git a/backend/plugin/webhook/googlechat/googlechat.go b/backend/plugin/webhook/googlechat/googlechat.goindex f90054157a..d25d9537c7 100644--- a/backend/plugin/webhook/googlechat/googlechat.go+++ b/backend/plugin/webhook/googlechat/googlechat.go@@ -12,7 +12,6 @@ import ("github.com/pkg/errors"- "github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/webhook")@@ -107,7 +106,7 @@ func BuildMessage(ctx webhook.Context) MessagePayload {widgets = append(widgets, textWidgetHTML(fmt.Sprintf("<b>%s</b>", escapeText(ctx.Issue.Name))))}if ctx.Issue.Description != "" {- widgets = append(widgets, textWidgetHTML(fmt.Sprintf("<b>%s</b>", escapeText(common.TruncateStringWithDescription(ctx.Issue.Description)))))+ widgets = append(widgets, textWidgetHTML(fmt.Sprintf("<b>%s</b>", escapeText(webhook.TruncateStringWithDescription(ctx.Issue.Description)))))}} else if ctx.Rollout != nil && ctx.Rollout.Title != "" {widgets = append(widgets, textWidgetHTML(fmt.Sprintf("<b>%s</b>", escapeText(ctx.Rollout.Title))))diff --git a/backend/plugin/webhook/lark/lark.go b/backend/plugin/webhook/lark/lark.goindex 5b3b8d87bf..bb59c633a7 100644--- a/backend/plugin/webhook/lark/lark.go+++ b/backend/plugin/webhook/lark/lark.go@@ -13,7 +13,6 @@ import ("github.com/pkg/errors""go.uber.org/multierr"- "github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/webhook"@@ -136,7 +135,7 @@ func postDirectMessage(webhookCtx webhook.Context) bool {sent := map[string]bool{}- if err := common.Retry(ctx, func() error {+ if err := webhook.Retry(ctx, func() error {var errs errorvar emails []stringdiff --git a/backend/common/retry.go b/backend/plugin/webhook/retry.gosimilarity index 95%rename from backend/common/retry.gorename to backend/plugin/webhook/retry.goindex 4a4f0f0d8c..4c50afaf89 100644--- a/backend/common/retry.go+++ b/backend/plugin/webhook/retry.go@@ -1,5 +1,4 @@-//nolint:revive-package common+package webhookimport ("context"diff --git a/backend/plugin/webhook/slack/slack.go b/backend/plugin/webhook/slack/slack.goindex e88a187f0b..8556d7a860 100644--- a/backend/plugin/webhook/slack/slack.go+++ b/backend/plugin/webhook/slack/slack.go@@ -13,7 +13,6 @@ import ("github.com/pkg/errors""go.uber.org/multierr"- "github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/webhook"@@ -147,7 +146,7 @@ func BuildMessage(ctx webhook.Context) MessagePayload {if ctx.Issue.Description != "" {blocks = append(blocks, Block{Type: "section",- Text: &BlockMarkdown{Type: "mrkdwn", Text: escapeMrkdwn(common.TruncateStringWithDescription(ctx.Issue.Description))},+ Text: &BlockMarkdown{Type: "mrkdwn", Text: escapeMrkdwn(webhook.TruncateStringWithDescription(ctx.Issue.Description))},})}} else if ctx.Rollout != nil && ctx.Rollout.Title != "" {@@ -259,7 +258,7 @@ func postDirectMessage(webhookCtx webhook.Context) bool {}p := newProvider(t)sent := map[string]bool{}- if err := common.Retry(ctx, func() error {+ if err := webhook.Retry(ctx, func() error {var errs errorfor _, u := range webhookCtx.MentionEndUsers {if sent[u.Email] {diff --git a/backend/plugin/webhook/teams/teams.go b/backend/plugin/webhook/teams/teams.goindex c975e0f5a9..db7750fa57 100644--- a/backend/plugin/webhook/teams/teams.go+++ b/backend/plugin/webhook/teams/teams.go@@ -20,7 +20,6 @@ import ("github.com/pkg/errors""go.uber.org/multierr"- "github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/webhook"@@ -157,7 +156,7 @@ func postDirectMessage(webhookCtx webhook.Context) bool {sent := map[string]bool{}- if err := common.Retry(ctx, func() error {+ if err := webhook.Retry(ctx, func() error {var errs errorvar emails []stringdiff --git a/backend/plugin/webhook/webhook.go b/backend/plugin/webhook/webhook.goindex b8d8be5f57..b57ba37b82 100644--- a/backend/plugin/webhook/webhook.go+++ b/backend/plugin/webhook/webhook.go@@ -10,7 +10,6 @@ import (storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/store")@@ -134,7 +133,7 @@ func (c *Context) GetMetaList() []Meta {})m = append(m, Meta{Name: "Issue Description",- Value: common.TruncateStringWithDescription(c.Issue.Description),+ Value: TruncateStringWithDescription(c.Issue.Description),})} else if c.Rollout != nil {if c.Rollout.Title != "" {@@ -178,7 +177,7 @@ func (c *Context) GetMetaListZh() []Meta {})m = append(m, Meta{Name: "工单描述",- Value: common.TruncateStringWithDescription(c.Issue.Description),+ Value: TruncateStringWithDescription(c.Issue.Description),})} else if c.Rollout != nil {if c.Rollout.Title != "" {diff --git a/backend/plugin/webhook/wecom/wecom.go b/backend/plugin/webhook/wecom/wecom.goindex ea03d314c2..4e22bc3157 100644--- a/backend/plugin/webhook/wecom/wecom.go+++ b/backend/plugin/webhook/wecom/wecom.go@@ -14,7 +14,6 @@ import ("github.com/pkg/errors""go.uber.org/multierr"- "github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/webhook"@@ -187,7 +186,7 @@ func (*Receiver) sendDirectMessage(webhookCtx webhook.Context) bool {return errs}- if err := common.Retry(ctx, fn); err != nil {+ if err := webhook.Retry(ctx, fn); err != nil {slog.Warn("failed to send direct message to wecom users", log.BBError(err))return false}diff --git a/backend/store/access_grant.go b/backend/store/access_grant.goindex 052bf33da5..76243f702a 100644--- a/backend/store/access_grant.go+++ b/backend/store/access_grant.go@@ -14,8 +14,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// AccessGrantMessage is the message for access_grant.diff --git a/backend/store/account_filter.go b/backend/store/account_filter.goindex f5617c7c73..b382315a1e 100644--- a/backend/store/account_filter.go+++ b/backend/store/account_filter.go@@ -9,8 +9,8 @@ import ("github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+ "github.com/bytebase/bytebase/backend/store/qb")// AccountListFilter contains the filter query for service accounts and workload identities.diff --git a/backend/store/audit_log.go b/backend/store/audit_log.goindex 215a0bd4fd..efb9e6f346 100644--- a/backend/store/audit_log.go+++ b/backend/store/audit_log.go@@ -12,8 +12,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")type AuditLog struct {diff --git a/backend/store/audit_log_test.go b/backend/store/audit_log_test.goindex 4d6f4ed510..4383f32c86 100644--- a/backend/store/audit_log_test.go+++ b/backend/store/audit_log_test.go@@ -7,7 +7,7 @@ import ("github.com/stretchr/testify/assert""github.com/stretchr/testify/require"- "github.com/bytebase/bytebase/backend/common/qb"+ "github.com/bytebase/bytebase/backend/store/qb")func TestApplyRetentionFilter(t *testing.T) {diff --git a/backend/store/changelog.go b/backend/store/changelog.goindex fb87aade44..654e36ef66 100644--- a/backend/store/changelog.go+++ b/backend/store/changelog.go@@ -11,8 +11,8 @@ import ("github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")type ChangelogStatus stringdiff --git a/backend/store/credential.go b/backend/store/credential.gonew file mode 100644index 0000000000..99fb8c7de8--- /dev/null+++ b/backend/store/credential.go@@ -0,0 +1,24 @@+package store++import "encoding/base64"++func obfuscate(src, seed string) string {+ srcBytes, seedBytes := []byte(src), []byte(seed)+ obfuscated := make([]byte, len(srcBytes))+ for i, b := range srcBytes {+ obfuscated[i] = b ^ seedBytes[i%len(seedBytes)]+ }+ return base64.StdEncoding.EncodeToString(obfuscated)+}++func unobfuscate(dst, seed string) (string, error) {+ obfuscated, err := base64.StdEncoding.DecodeString(dst)+ if err != nil {+ return "", err+ }+ unobfuscated, seedBytes := make([]byte, len(obfuscated)), []byte(seed)+ for i, b := range obfuscated {+ unobfuscated[i] = b ^ seedBytes[i%len(seedBytes)]+ }+ return string(unobfuscated), nil+}diff --git a/backend/store/credential_test.go b/backend/store/credential_test.gonew file mode 100644index 0000000000..bf120632d4--- /dev/null+++ b/backend/store/credential_test.go@@ -0,0 +1,48 @@+package store++import (+ "testing"++ "github.com/stretchr/testify/require"+)++func TestObfuscate(t *testing.T) {+ tests := []struct {+ src string+ seed string+ dst string+ }{+ {+ src: "",+ seed: "01234567890123456789012345678901", // 32 bytes.+ dst: "",+ },+ {+ src: "hello",+ seed: "01234567890123456789012345678901", // 32 bytes.+ dst: "WFReX1s=",+ },+ {+ src: "你好!",+ seed: "ENuef1JjSvQ6VPfgrB33T2mkshhwRRjp", // 32 bytes.+ dst: "ofPVgMOMaw==",+ },+ {+ src: "Bytebase is a database tool for developers. Bytebase 是个数据库 DevOps 工具。",+ seed: "01234567890123456789012345678901", // 32 bytes.+ dst: "ckhGVlZURVIYUEMRUxNQVEJWWlhDVBJHW1paF15WQhFUVERWWFpGUkpKHhFwSkBQVFZLXBDXqpzQjZzRrYnWvJ7UiKAUcVNBd0lDEdeEkdCzgNu5sg==",+ },+ {+ src: `{ "type": "service_account", "project_id": "spanner-test-371702", "private_key_id": "klsdjfklasjdfas\nklsdjaflkajefjlaksdjf\nlsajdfklsjaldkfjkasldjf\nD PRIVATE KEY-----\n, "client_email": "[email protected]", "client_id": "102052620181224568340", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/test-768%40spanner-test-371702.iam.gserviceaccount.com" }`,+ seed: `aGgQpKjg7fuwNV6B31sIRQ1qm4Ttqw9s`, // 32 bytes.+ dst: `GmdHcVI/ExdSRE9XbCVTMEVYECwNMFISAkE6AFNbGVNBZRcjHyEPBEM5HBNsbBZgQEESJzw0Q1wZUScAXEQOQlZ3VXNca0pHFRYHHjg3QidsWhYwDThVU1cUdh8dBF0ZBywLMAMhDgFWFSkZJTpFJllQFSU5MFsUC144FRoEXRkHGwk9AyoAA1ENGQQkN1omWFcZIjMiXRUHUggaNVdpISgRJgU1ayEibktYWmN7aiwfEVNpcDJdGAhaICsUGlgaDWVdcVI/DxRDS0JBdhZFMlJfHSwgfEUUHkB5R0ZGDkNTaQ4wHWUNFFIUAx4tM1chUF4GJyZ/Uh4AFnhUUVcbEA0uAj8EFAMDFVxVVX9mBHIGA0V7YmAJQF8GYEFHTwpHUWVLcVBrSAZCEh0oOyRfYAkRUSEmJUECVxt7FRIUVgYPMxR/FyQFAFsDWxQhOxktHF4SPCY5A14MQSAcU1sZU0FlEz4bLgQ4QhQcVXR2FCpHRQM6aH4eHgxBIBxDWV4cDiALNBE7AxQZBRoaYSJZKVZfUWVycRFTDEEgHC4HSxwXLgM0AhQSUgdfKhQrJEIdRkMfa2hxExkZQCQHS1gWBBYwSTYfJA0LUgcFHj14VS1eHhwoJyVZQ0JCZVsSEksHEmVLcVBrSARbDxAZOglOdwMILCo3I0UuGEY4VktXGxsVMxciSmRFEEARWxAhOVEuVlADICF/Uh4AGyYbExhNXBd2SDwVPwsDVhIUWDZjBnscRRY6JnwGR1URYEQCB1gdDyIVfAQuGRMaVUJGeWYEbFpQHmc1IlQDG103ERAUWhwUKRN/EyQHRRcb`,+ },+ }+ for _, test := range tests {+ obfuscated := obfuscate(test.src, test.seed)+ require.Equal(t, test.dst, obfuscated)+ ubobfuscated, err := unobfuscate(obfuscated, test.seed)+ require.NoError(t, err)+ require.Equal(t, test.src, ubobfuscated)+ }+}diff --git a/backend/store/database.go b/backend/store/database.goindex 82c75c5cda..991392dc60 100644--- a/backend/store/database.go+++ b/backend/store/database.go@@ -14,8 +14,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// DatabaseMessage is the message for database.diff --git a/backend/store/database_group.go b/backend/store/database_group.goindex 425430303d..5d4800acf8 100644--- a/backend/store/database_group.go+++ b/backend/store/database_group.go@@ -8,7 +8,7 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"+ "github.com/bytebase/bytebase/backend/store/qb")// DatabaseGroupMessage is the message for database groups.diff --git a/backend/store/db_connection.go b/backend/store/db_connection.goindex 88d52c97c0..4a94737c3c 100644--- a/backend/store/db_connection.go+++ b/backend/store/db_connection.go@@ -15,8 +15,8 @@ import ("github.com/pkg/errors""go.uber.org/multierr"- "github.com/bytebase/bytebase/backend/common/qb""github.com/bytebase/bytebase/backend/store/dbauth"+ "github.com/bytebase/bytebase/backend/store/qb")// DBConnectionManager manages database connections with support for dynamic updates.diff --git a/backend/store/db_schema.go b/backend/store/db_schema.goindex f04aedf816..382da79728 100644--- a/backend/store/db_schema.go+++ b/backend/store/db_schema.go@@ -9,9 +9,9 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/store/model"+ "github.com/bytebase/bytebase/backend/store/qb")// UpdateDBSchemaMessage is the message for updating db schema.diff --git a/backend/store/email_verification_code.go b/backend/store/email_verification_code.goindex 20e66460db..891f5b55de 100644--- a/backend/store/email_verification_code.go+++ b/backend/store/email_verification_code.go@@ -7,8 +7,8 @@ import ("github.com/pkg/errors"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// EmailVerificationCodeMessage represents a row in the email_verification_code table:diff --git a/backend/store/environment.go b/backend/store/environment.goindex 892b4b2338..0580482205 100644--- a/backend/store/environment.go+++ b/backend/store/environment.go@@ -5,8 +5,8 @@ import ("github.com/pkg/errors"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")func (s *Store) GetEnvironmentByID(ctx context.Context, workspaceID string, id string) (*storepb.EnvironmentSetting_Environment, error) {diff --git a/backend/store/filter.go b/backend/store/filter.goindex 3a40255a7e..bb92fdd8cc 100644--- a/backend/store/filter.go+++ b/backend/store/filter.go@@ -8,7 +8,7 @@ import (celoperators "github.com/google/cel-go/common/operators""github.com/pkg/errors"- "github.com/bytebase/bytebase/backend/common/qb"+ "github.com/bytebase/bytebase/backend/store/qb")func getVariableAndValueFromExpr(expr celast.Expr) (string, any) {diff --git a/backend/store/filter_test.go b/backend/store/filter_test.goindex 06478ae2d1..af99f826a0 100644--- a/backend/store/filter_test.go+++ b/backend/store/filter_test.go@@ -5,7 +5,7 @@ import ("github.com/stretchr/testify/require"- "github.com/bytebase/bytebase/backend/common/qb"+ "github.com/bytebase/bytebase/backend/store/qb")func TestEscapeLikePattern(t *testing.T) {diff --git a/backend/store/group.go b/backend/store/group.goindex c0a0db5b19..66ad0363c7 100644--- a/backend/store/group.go+++ b/backend/store/group.go@@ -13,8 +13,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// FindGroupMessage is the message for finding groups.diff --git a/backend/store/id.go b/backend/store/id.goindex f402ee42bb..41537283fd 100644--- a/backend/store/id.go+++ b/backend/store/id.go@@ -7,7 +7,7 @@ import ("github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"+ "github.com/bytebase/bytebase/backend/store/qb")// idMinValue is the minimum ID value for per-project auto-increment.diff --git a/backend/store/idp.go b/backend/store/idp.goindex 9ddbef812b..958c442854 100644--- a/backend/store/idp.go+++ b/backend/store/idp.go@@ -8,8 +8,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// IdentityProviderMessage is the message for identity provider.diff --git a/backend/store/instance.go b/backend/store/instance.goindex 8e769d0e36..986381dafb 100644--- a/backend/store/instance.go+++ b/backend/store/instance.go@@ -15,9 +15,9 @@ import ("google.golang.org/protobuf/proto""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+ "github.com/bytebase/bytebase/backend/store/qb")// InstanceMessage is the message for instance.@@ -600,55 +600,55 @@ func (s *Store) obfuscateInstance(ctx context.Context, instance *storepb.Instancredacted := proto.CloneOf(instance)for _, ds := range redacted.GetDataSources() {- ds.ObfuscatedPassword = common.Obfuscate(ds.GetPassword(), secret)+ ds.ObfuscatedPassword = obfuscate(ds.GetPassword(), secret)ds.Password = ""- ds.ObfuscatedSslCa = common.Obfuscate(ds.GetSslCa(), secret)+ ds.ObfuscatedSslCa = obfuscate(ds.GetSslCa(), secret)ds.SslCa = ""- ds.ObfuscatedSslCaPath = common.Obfuscate(ds.GetSslCaPath(), secret)+ ds.ObfuscatedSslCaPath = obfuscate(ds.GetSslCaPath(), secret)ds.SslCaPath = ""- ds.ObfuscatedSslCert = common.Obfuscate(ds.GetSslCert(), secret)+ ds.ObfuscatedSslCert = obfuscate(ds.GetSslCert(), secret)ds.SslCert = ""- ds.ObfuscatedSslCertPath = common.Obfuscate(ds.GetSslCertPath(), secret)+ ds.ObfuscatedSslCertPath = obfuscate(ds.GetSslCertPath(), secret)ds.SslCertPath = ""- ds.ObfuscatedSslKey = common.Obfuscate(ds.GetSslKey(), secret)+ ds.ObfuscatedSslKey = obfuscate(ds.GetSslKey(), secret)ds.SslKey = ""- ds.ObfuscatedSslKeyPath = common.Obfuscate(ds.GetSslKeyPath(), secret)+ ds.ObfuscatedSslKeyPath = obfuscate(ds.GetSslKeyPath(), secret)ds.SslKeyPath = ""- ds.ObfuscatedSshPassword = common.Obfuscate(ds.GetSshPassword(), secret)+ ds.ObfuscatedSshPassword = obfuscate(ds.GetSshPassword(), secret)ds.SshPassword = ""- ds.ObfuscatedSshPrivateKey = common.Obfuscate(ds.GetSshPrivateKey(), secret)+ ds.ObfuscatedSshPrivateKey = obfuscate(ds.GetSshPrivateKey(), secret)ds.SshPrivateKey = ""- ds.ObfuscatedAuthenticationPrivateKey = common.Obfuscate(ds.GetAuthenticationPrivateKey(), secret)+ ds.ObfuscatedAuthenticationPrivateKey = obfuscate(ds.GetAuthenticationPrivateKey(), secret)ds.AuthenticationPrivateKey = ""- ds.ObfuscatedAuthenticationPrivateKeyPassphrase = common.Obfuscate(ds.GetAuthenticationPrivateKeyPassphrase(), secret)+ ds.ObfuscatedAuthenticationPrivateKeyPassphrase = obfuscate(ds.GetAuthenticationPrivateKeyPassphrase(), secret)ds.AuthenticationPrivateKeyPassphrase = ""- ds.ObfuscatedMasterPassword = common.Obfuscate(ds.GetMasterPassword(), secret)+ ds.ObfuscatedMasterPassword = obfuscate(ds.GetMasterPassword(), secret)ds.MasterPassword = ""if azureCredential := ds.GetAzureCredential(); azureCredential != nil {- azureCredential.ObfuscatedClientSecret = common.Obfuscate(azureCredential.ClientSecret, secret)+ azureCredential.ObfuscatedClientSecret = obfuscate(azureCredential.ClientSecret, secret)azureCredential.ClientSecret = ""}if awsCredential := ds.GetAwsCredential(); awsCredential != nil {- awsCredential.ObfuscatedAccessKeyId = common.Obfuscate(awsCredential.AccessKeyId, secret)+ awsCredential.ObfuscatedAccessKeyId = obfuscate(awsCredential.AccessKeyId, secret)awsCredential.AccessKeyId = ""- awsCredential.ObfuscatedSecretAccessKey = common.Obfuscate(awsCredential.SecretAccessKey, secret)+ awsCredential.ObfuscatedSecretAccessKey = obfuscate(awsCredential.SecretAccessKey, secret)awsCredential.SecretAccessKey = ""- awsCredential.ObfuscatedSessionToken = common.Obfuscate(awsCredential.SessionToken, secret)+ awsCredential.ObfuscatedSessionToken = obfuscate(awsCredential.SessionToken, secret)awsCredential.SessionToken = ""}if gcpCredential := ds.GetGcpCredential(); gcpCredential != nil {- gcpCredential.ObfuscatedContent = common.Obfuscate(gcpCredential.Content, secret)+ gcpCredential.ObfuscatedContent = obfuscate(gcpCredential.Content, secret)gcpCredential.Content = ""}if externalSecret := ds.GetExternalSecret(); externalSecret != nil {- externalSecret.ObfuscatedVaultSslCa = common.Obfuscate(externalSecret.GetVaultSslCa(), secret)+ externalSecret.ObfuscatedVaultSslCa = obfuscate(externalSecret.GetVaultSslCa(), secret)externalSecret.VaultSslCa = ""- externalSecret.ObfuscatedVaultSslCert = common.Obfuscate(externalSecret.GetVaultSslCert(), secret)+ externalSecret.ObfuscatedVaultSslCert = obfuscate(externalSecret.GetVaultSslCert(), secret)externalSecret.VaultSslCert = ""- externalSecret.ObfuscatedVaultSslKey = common.Obfuscate(externalSecret.GetVaultSslKey(), secret)+ externalSecret.ObfuscatedVaultSslKey = obfuscate(externalSecret.GetVaultSslKey(), secret)externalSecret.VaultSslKey = ""}}@@ -664,77 +664,77 @@ func (s *Store) deobfuscateInstances(ctx context.Context, instances []*InstanceMfor _, instance := range instances {for _, ds := range instance.Metadata.GetDataSources() {- password, err := common.Unobfuscate(ds.GetObfuscatedPassword(), secret)+ password, err := unobfuscate(ds.GetObfuscatedPassword(), secret)if err != nil {return err}ds.Password = password- sslCa, err := common.Unobfuscate(ds.GetObfuscatedSslCa(), secret)+ sslCa, err := unobfuscate(ds.GetObfuscatedSslCa(), secret)if err != nil {return err}ds.SslCa = sslCa- sslCaPath, err := common.Unobfuscate(ds.GetObfuscatedSslCaPath(), secret)+ sslCaPath, err := unobfuscate(ds.GetObfuscatedSslCaPath(), secret)if err != nil {return err}ds.SslCaPath = sslCaPath- sslCert, err := common.Unobfuscate(ds.GetObfuscatedSslCert(), secret)+ sslCert, err := unobfuscate(ds.GetObfuscatedSslCert(), secret)if err != nil {return err}ds.SslCert = sslCert- sslCertPath, err := common.Unobfuscate(ds.GetObfuscatedSslCertPath(), secret)+ sslCertPath, err := unobfuscate(ds.GetObfuscatedSslCertPath(), secret)if err != nil {return err}ds.SslCertPath = sslCertPath- sslKey, err := common.Unobfuscate(ds.GetObfuscatedSslKey(), secret)+ sslKey, err := unobfuscate(ds.GetObfuscatedSslKey(), secret)if err != nil {return err}ds.SslKey = sslKey- sslKeyPath, err := common.Unobfuscate(ds.GetObfuscatedSslKeyPath(), secret)+ sslKeyPath, err := unobfuscate(ds.GetObfuscatedSslKeyPath(), secret)if err != nil {return err}ds.SslKeyPath = sslKeyPath- sshPassword, err := common.Unobfuscate(ds.GetObfuscatedSshPassword(), secret)+ sshPassword, err := unobfuscate(ds.GetObfuscatedSshPassword(), secret)if err != nil {return err}ds.SshPassword = sshPassword- sshPrivateKey, err := common.Unobfuscate(ds.GetObfuscatedSshPrivateKey(), secret)+ sshPrivateKey, err := unobfuscate(ds.GetObfuscatedSshPrivateKey(), secret)if err != nil {return err}ds.SshPrivateKey = sshPrivateKey- authenticationPrivateKey, err := common.Unobfuscate(ds.GetObfuscatedAuthenticationPrivateKey(), secret)+ authenticationPrivateKey, err := unobfuscate(ds.GetObfuscatedAuthenticationPrivateKey(), secret)if err != nil {return err}ds.AuthenticationPrivateKey = authenticationPrivateKey- authenticationPrivateKeyPassphrase, err := common.Unobfuscate(ds.GetObfuscatedAuthenticationPrivateKeyPassphrase(), secret)+ authenticationPrivateKeyPassphrase, err := unobfuscate(ds.GetObfuscatedAuthenticationPrivateKeyPassphrase(), secret)if err != nil {return err}ds.AuthenticationPrivateKeyPassphrase = authenticationPrivateKeyPassphrase- masterPassword, err := common.Unobfuscate(ds.GetObfuscatedMasterPassword(), secret)+ masterPassword, err := unobfuscate(ds.GetObfuscatedMasterPassword(), secret)if err != nil {return err}ds.MasterPassword = masterPasswordif azureCredential := ds.GetAzureCredential(); azureCredential != nil {- clientSecret, err := common.Unobfuscate(azureCredential.ObfuscatedClientSecret, secret)+ clientSecret, err := unobfuscate(azureCredential.ObfuscatedClientSecret, secret)if err != nil {return err}@@ -742,19 +742,19 @@ func (s *Store) deobfuscateInstances(ctx context.Context, instances []*InstanceM}if awsCredential := ds.GetAwsCredential(); awsCredential != nil {- accessKeyID, err := common.Unobfuscate(awsCredential.ObfuscatedAccessKeyId, secret)+ accessKeyID, err := unobfuscate(awsCredential.ObfuscatedAccessKeyId, secret)if err != nil {return err}awsCredential.AccessKeyId = accessKeyID- secretAccessKey, err := common.Unobfuscate(awsCredential.ObfuscatedSecretAccessKey, secret)+ secretAccessKey, err := unobfuscate(awsCredential.ObfuscatedSecretAccessKey, secret)if err != nil {return err}awsCredential.SecretAccessKey = secretAccessKey- sessionToken, err := common.Unobfuscate(awsCredential.ObfuscatedSessionToken, secret)+ sessionToken, err := unobfuscate(awsCredential.ObfuscatedSessionToken, secret)if err != nil {return err}@@ -762,7 +762,7 @@ func (s *Store) deobfuscateInstances(ctx context.Context, instances []*InstanceM}if gcpCredential := ds.GetGcpCredential(); gcpCredential != nil {- content, err := common.Unobfuscate(gcpCredential.ObfuscatedContent, secret)+ content, err := unobfuscate(gcpCredential.ObfuscatedContent, secret)if err != nil {return err}@@ -770,19 +770,19 @@ func (s *Store) deobfuscateInstances(ctx context.Context, instances []*InstanceM}if externalSecret := ds.GetExternalSecret(); externalSecret != nil {- sslCa, err := common.Unobfuscate(externalSecret.GetObfuscatedVaultSslCa(), secret)+ sslCa, err := unobfuscate(externalSecret.GetObfuscatedVaultSslCa(), secret)if err != nil {return err}externalSecret.VaultSslCa = sslCa- sslCert, err := common.Unobfuscate(externalSecret.GetObfuscatedVaultSslCert(), secret)+ sslCert, err := unobfuscate(externalSecret.GetObfuscatedVaultSslCert(), secret)if err != nil {return err}externalSecret.VaultSslCert = sslCert- sslKey, err := common.Unobfuscate(externalSecret.GetObfuscatedVaultSslKey(), secret)+ sslKey, err := unobfuscate(externalSecret.GetObfuscatedVaultSslKey(), secret)if err != nil {return err}diff --git a/backend/store/issue.go b/backend/store/issue.goindex 9f6a6d1213..98dbeb063d 100644--- a/backend/store/issue.go+++ b/backend/store/issue.go@@ -13,8 +13,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")var getSegmenter func() *gse.Segmenterdiff --git a/backend/store/issue_comment.go b/backend/store/issue_comment.goindex 3cb39af11a..efaeb8410b 100644--- a/backend/store/issue_comment.go+++ b/backend/store/issue_comment.go@@ -12,8 +12,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// ThreadState is the resolvable state carried by a thread's root comment: adiff --git a/backend/store/login_attempt.go b/backend/store/login_attempt.goindex 38f66992f0..98c26dfb32 100644--- a/backend/store/login_attempt.go+++ b/backend/store/login_attempt.go@@ -7,8 +7,8 @@ import ("github.com/pkg/errors"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// maxIdentityBytes caps the identity column. The longest legal identity todaydiff --git a/backend/store/oauth2_authorization_code.go b/backend/store/oauth2_authorization_code.goindex fdc34054bc..49d18b853f 100644--- a/backend/store/oauth2_authorization_code.go+++ b/backend/store/oauth2_authorization_code.go@@ -9,8 +9,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")type OAuth2AuthorizationCodeMessage struct {diff --git a/backend/store/oauth2_client.go b/backend/store/oauth2_client.goindex 79bf663496..a6da72d7f9 100644--- a/backend/store/oauth2_client.go+++ b/backend/store/oauth2_client.go@@ -9,8 +9,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")type OAuth2ClientMessage struct {diff --git a/backend/store/oauth2_refresh_token.go b/backend/store/oauth2_refresh_token.goindex 4d5f7a1475..ed9a5e4579 100644--- a/backend/store/oauth2_refresh_token.go+++ b/backend/store/oauth2_refresh_token.go@@ -9,8 +9,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")type OAuth2RefreshTokenMessage struct {diff --git a/backend/store/plan.go b/backend/store/plan.goindex 8071190a99..c836f80dae 100644--- a/backend/store/plan.go+++ b/backend/store/plan.go@@ -13,9 +13,9 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+ "github.com/bytebase/bytebase/backend/store/qb")// ErrPlanHasRollout indicates that a plan update was rejected because rollout already started.diff --git a/backend/store/plan_check_run.go b/backend/store/plan_check_run.goindex ca25b35a29..a63ed3120b 100644--- a/backend/store/plan_check_run.go+++ b/backend/store/plan_check_run.go@@ -9,8 +9,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// PlanCheckRunStatus is the status of a plan check run.diff --git a/backend/store/policy.go b/backend/store/policy.goindex 6122be3fd8..165c4a8ffa 100644--- a/backend/store/policy.go+++ b/backend/store/policy.go@@ -14,8 +14,8 @@ import ("github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")type IamPolicyMessage struct {diff --git a/backend/store/principal.go b/backend/store/principal.goindex 12b8f3ad96..1f5a881576 100644--- a/backend/store/principal.go+++ b/backend/store/principal.go@@ -13,8 +13,8 @@ import ("google.golang.org/protobuf/types/known/timestamppb""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// FindUserMessage is the message for finding users.diff --git a/backend/store/project.go b/backend/store/project.goindex 925a40b286..40ff79fed5 100644--- a/backend/store/project.go+++ b/backend/store/project.go@@ -15,9 +15,9 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+ "github.com/bytebase/bytebase/backend/store/qb")// ProjectMessage is the message for project.diff --git a/backend/store/project_webhook.go b/backend/store/project_webhook.goindex 1ccb0a85d3..2513bc1b16 100644--- a/backend/store/project_webhook.go+++ b/backend/store/project_webhook.go@@ -8,8 +8,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// ProjectWebhookMessage is the store model for an project webhook.diff --git a/backend/common/qb/qb.go b/backend/store/qb/qb.gosimilarity index 95%rename from backend/common/qb/qb.gorename to backend/store/qb/qb.goindex 2035fea70e..6f861c6294 100644--- a/backend/common/qb/qb.go+++ b/backend/store/qb/qb.go@@ -107,7 +107,7 @@ func (q *Query) toRawSQL() (string, []any, error) {// Return the first error encounteredreturn "", nil, part.Errs[0]}- sqlBuilder.WriteString(part.Text)+ _, _ = sqlBuilder.WriteString(part.Text)params = append(params, part.Params...)}@@ -168,7 +168,7 @@ func (q *Query) ToSQL() (string, []any, error) {// Return the first error encounteredreturn "", nil, part.Errs[0]}- sqlBuilder.WriteString(part.Text)+ _, _ = sqlBuilder.WriteString(part.Text)params = append(params, part.Params...)}@@ -190,11 +190,11 @@ func (q *Query) ToSQL() (string, []any, error) {// Build final SQL with $1, $2, etc.var builder strings.Builderfor i := range params {- builder.WriteString(parts[i])- builder.WriteString("$")- builder.WriteString(strconv.Itoa(i + 1))+ _, _ = builder.WriteString(parts[i])+ _, _ = builder.WriteString("$")+ _, _ = builder.WriteString(strconv.Itoa(i + 1))}- builder.WriteString(parts[len(parts)-1])+ _, _ = builder.WriteString(parts[len(parts)-1])// Replace delimiter back to ?finalSQL := strings.ReplaceAll(builder.String(), delimiter, "?")diff --git a/backend/common/qb/qb_test.go b/backend/store/qb/qb_test.gosimilarity index 100%rename from backend/common/qb/qb_test.gorename to backend/store/qb/qb_test.godiff --git a/backend/store/query_history.go b/backend/store/query_history.goindex 4731add5de..52b31591b4 100644--- a/backend/store/query_history.go+++ b/backend/store/query_history.go@@ -13,8 +13,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")type QueryHistoryType stringdiff --git a/backend/store/release.go b/backend/store/release.goindex bfbbf4d455..2ee5645c6a 100644--- a/backend/store/release.go+++ b/backend/store/release.go@@ -10,8 +10,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")type ReleaseMessage struct {diff --git a/backend/store/replica_heartbeat.go b/backend/store/replica_heartbeat.goindex b0ea48c450..5f333d3702 100644--- a/backend/store/replica_heartbeat.go+++ b/backend/store/replica_heartbeat.go@@ -6,7 +6,7 @@ import ("github.com/pkg/errors"- "github.com/bytebase/bytebase/backend/common/qb"+ "github.com/bytebase/bytebase/backend/store/qb")// UpsertReplicaHeartbeat updates or inserts a replica heartbeat.diff --git a/backend/store/review_config.go b/backend/store/review_config.goindex 8909e5bef5..f5b2ba6275 100644--- a/backend/store/review_config.go+++ b/backend/store/review_config.go@@ -8,8 +8,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// ReviewConfigMessage is the API message for sql review.diff --git a/backend/store/review_run.go b/backend/store/review_run.goindex 73454fc963..8ba003779b 100644--- a/backend/store/review_run.go+++ b/backend/store/review_run.go@@ -9,8 +9,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// Reviewer type values stored in review_run.type. The column has no CHECK ondiff --git a/backend/store/revision.go b/backend/store/revision.goindex 374ae6eebb..b6d1649ee8 100644--- a/backend/store/revision.go+++ b/backend/store/revision.go@@ -9,8 +9,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")type RevisionMessage struct {diff --git a/backend/store/role.go b/backend/store/role.goindex 672f2f3540..7807e8c28d 100644--- a/backend/store/role.go+++ b/backend/store/role.go@@ -8,8 +8,8 @@ import ("github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/permission"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// RoleMessage is the message for roles.diff --git a/backend/store/rollout_filter.go b/backend/store/rollout_filter.goindex 7bd11c83c0..a57f642740 100644--- a/backend/store/rollout_filter.go+++ b/backend/store/rollout_filter.go@@ -8,9 +8,9 @@ import ("github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+ "github.com/bytebase/bytebase/backend/store/qb")// GetListRolloutFilter parses a CEL filter expression into a query builder query for listing rollouts.diff --git a/backend/store/runner_queries.go b/backend/store/runner_queries.goindex dc0c346c16..80aed833f1 100644--- a/backend/store/runner_queries.go+++ b/backend/store/runner_queries.go@@ -13,8 +13,8 @@ import ("github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// GetProjectByResourceID gets a project by its globally unique resource ID without workspace filter.diff --git a/backend/store/saved_query.go b/backend/store/saved_query.goindex 1fb6823c2d..8e8ce14f0b 100644--- a/backend/store/saved_query.go+++ b/backend/store/saved_query.go@@ -17,8 +17,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// SavedQueryMessage is the message for a saved query.diff --git a/backend/store/service_account.go b/backend/store/service_account.goindex 6097fa5742..339371ae07 100644--- a/backend/store/service_account.go+++ b/backend/store/service_account.go@@ -7,7 +7,7 @@ import ("github.com/pkg/errors"- "github.com/bytebase/bytebase/backend/common/qb"+ "github.com/bytebase/bytebase/backend/store/qb")// ServiceAccountMessage is the message for a service account.diff --git a/backend/store/setting.go b/backend/store/setting.goindex 89d9494996..dc4794b468 100644--- a/backend/store/setting.go+++ b/backend/store/setting.go@@ -12,8 +12,8 @@ import ("google.golang.org/protobuf/proto""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// SettingMessage is the message of setting.diff --git a/backend/store/sheet.go b/backend/store/sheet.goindex fdd4ba510b..1d8bdc018f 100644--- a/backend/store/sheet.go+++ b/backend/store/sheet.go@@ -12,7 +12,7 @@ import ("github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"+ "github.com/bytebase/bytebase/backend/store/qb")// SheetMessage is the message for a sheet.diff --git a/backend/store/stats.go b/backend/store/stats.goindex c51edf48d9..bceb3d03dd 100644--- a/backend/store/stats.go+++ b/backend/store/stats.go@@ -5,7 +5,7 @@ import ("github.com/pkg/errors"- "github.com/bytebase/bytebase/backend/common/qb"+ "github.com/bytebase/bytebase/backend/store/qb")// CountWorkspaces counts all workspaces.diff --git a/backend/store/sync_history.go b/backend/store/sync_history.goindex 6c1b6dd7fd..ffc7e64e6d 100644--- a/backend/store/sync_history.go+++ b/backend/store/sync_history.go@@ -11,7 +11,7 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"+ "github.com/bytebase/bytebase/backend/store/qb")type SyncHistory struct {diff --git a/backend/store/task.go b/backend/store/task.goindex 3bb7170971..51573b1f34 100644--- a/backend/store/task.go+++ b/backend/store/task.go@@ -9,9 +9,9 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/store/model"+ "github.com/bytebase/bytebase/backend/store/qb")// TaskMessage is the message for tasks.diff --git a/backend/store/task_run.go b/backend/store/task_run.goindex 7e52c2ce60..70cb8397c3 100644--- a/backend/store/task_run.go+++ b/backend/store/task_run.go@@ -10,8 +10,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// TaskRunMessage is message for task run.diff --git a/backend/store/task_run_log.go b/backend/store/task_run_log.goindex ba6defc6f2..aef59e3a4c 100644--- a/backend/store/task_run_log.go+++ b/backend/store/task_run_log.go@@ -10,8 +10,8 @@ import ("github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")type TaskRunLog struct {diff --git a/backend/store/web_refresh_token.go b/backend/store/web_refresh_token.goindex 1a4ae04c96..2f83014071 100644--- a/backend/store/web_refresh_token.go+++ b/backend/store/web_refresh_token.go@@ -7,7 +7,7 @@ import ("github.com/pkg/errors"- "github.com/bytebase/bytebase/backend/common/qb"+ "github.com/bytebase/bytebase/backend/store/qb")type WebRefreshTokenMessage struct {diff --git a/backend/store/workload_identity.go b/backend/store/workload_identity.goindex eed49dd0ca..5ae5a1df71 100644--- a/backend/store/workload_identity.go+++ b/backend/store/workload_identity.go@@ -9,8 +9,8 @@ import ("google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// WorkloadIdentityMessage is the message for a workload identity.diff --git a/backend/store/workspace.go b/backend/store/workspace.goindex d12321643c..9d903fa407 100644--- a/backend/store/workspace.go+++ b/backend/store/workspace.go@@ -15,8 +15,8 @@ import ("github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"- "github.com/bytebase/bytebase/backend/common/qb"storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store/qb")// WorkspaceMessage is the message for a workspace.diff --git a/backend/tests/webhook_test.go b/backend/tests/webhook_test.goindex 9e8de9c0a8..be7143e6d0 100644--- a/backend/tests/webhook_test.go+++ b/backend/tests/webhook_test.go@@ -147,7 +147,7 @@ func TestWebhookIntegration(t *testing.T) {//// Body MUST be the literal "ok" — the Slack plugin's postMessage// (backend/plugin/webhook/slack/slack.go:247-249) treats any other body as- // a delivery failure and triggers common.Retry (up to 3 attempts, 5s+ // a delivery failure and triggers webhook.Retry (up to 3 attempts, 5s// exponential backoff). Without an "ok" response, every webhook event// would be delivered 1-3 times depending on test timing, causing// nondeterministic counts in the requireWebhookCount assertions acrossdiff --git a/docs/backend-common-audit.md b/docs/backend-common-audit.mdnew file mode 100644index 0000000000..31a45f8fb8--- /dev/null+++ b/docs/backend-common-audit.md@@ -0,0 +1,117 @@+# Backend common package audit++Audit scope: `backend/common`, including its subpackages, and their in-repository+Go callers. The first migration implements `store/qb`, webhook retry and notification+truncation, private store credential helpers, and private v1 audit callbacks.+The remaining entries are proposals.++The root package mixes unrelated ownership: resource names, identity, request+context, audit transport, policy expressions, SQL execution, and generic helpers.+At the start of the audit, 387 non-test Go files across 76 backend package+directories imported it. These are+source inventory counts, not a build-tag-aware dependency analysis.++Prefer existing owners and private functions. Introduce a shared leaf package+only when callers span owners. Keep focused `common` subpackages where they+already provide a useful shared interface; moving every directory would create+churn without improving ownership. Do not move this collection into `backend/utils`,+which already depends on the store and contains unrelated domain behavior.++## Straightforward moves++All paths below are relative to `backend/`. Proposed destinations are not+necessarily existing files or packages.++| Current code | Proposed home | Evidence and constraint |+| --- | --- | --- |+| `common/qb` | `store/qb` | All 48 non-test importing files are in `store`. Keep its tests with it. |+| `common/retry.go` | `plugin/webhook`, exported for adapters and `component/webhook` | Every production caller delivers webhooks. The three-attempt, five-second initial interval and five-minute timeout are delivery policy. |+| `TruncateStringWithDescription` | `plugin/webhook` | Its 450-character limit and Bytebase footer are webhook presentation policy. |+| `Obfuscate`, `Unobfuscate` | Private helpers in `store` | All production uses are persisted instance credential encoding in `store/instance.go`. Preserve the stored encoding exactly. |+| `HasPrefixes` | Private helper in `server` | Only `server_frontend_routes.go` calls it. |+| Live position conversions in `common/position.go` | `plugin/parser/base/position.go`; TiDB-only conversion local to `plugin/parser/tidb` | Parser base already owns position mapping. Advisor implementations consume the ANTLR line conversion. |+| Transaction types and default mode in `common/engine.go` | `plugin/parser/base/transaction_mode.go` | This file already parses directives and converts isolation levels; drivers consume the result. Avoid putting types in `plugin/db` and making parser base depend on drivers. |+| `ConvertToParserEngine` | Private helper in `api/v1` initially | Only v1 calls it, and it returns a Connect error. A parser-owned version should return a transport-independent error. |+| Audit callback keys, setters, getters, `PermissionDeniedError` | Private helpers in `api/v1` | These coordinate v1 interceptors and handlers; no production callers outside v1 were found. |+| `GetQueryExportFactors` and its traversal | Private helpers in `component/review` | The only production caller is review evaluation. It should consume the shared IAM expression definitions described below. |+| `SanitizeUTF8Message` and reflection traversal | Private helpers in `plugin/db/oracle` | Only Oracle metadata sync calls it. Keep string sanitization shared because v1 uses it too. |+| `GetPostgresSocketDir` | `resources/postgres` | Callers are embedded PostgreSQL setup, server startup, and the self-hosted sample manager. |+| `ReleaseMode` | `component/config` | `Profile.Mode` is the central configuration field; the package currently imports common only for this type. |+| Build-tagged `IsDev` | Local build-tagged files in `plugin/db/cosmosdb` | Cosmos DB is its only production consumer. Keep build-time mode distinct from runtime `Profile.Mode`. |++## Shared modules that need deliberate extraction++| Current code | Proposed home | Design requirement |+| --- | --- | --- |+| Resource parsers, formatters, and prefixes in `resource_name.go` | `common/resourcename` | Keep a single canonical resource-name implementation used by store, APIs, parsers, and runners. Preserve workspace/project scope and escaping. ADR-0002 requires complete project-scoped names. |+| Principal email conventions, account suffixes, password/email/phone validation, directory-sync token hashing | A leaf `component/iam/identity` package | Used across APIs, identity providers, recovery, and store. Must not import the parent IAM manager or store. Separate files by concept, rather than another util file. |+| `AuthContext`, `DelegatedGrant`, authorization resource types | `api/auth` | They describe transport authentication and authorization. These can live with the existing auth implementation, unlike the cross-component workspace context. |+| Workspace/user context keys and workspace accessor | A leaf `common/requestcontext` package | Review and parser-context components also consume request identity. Keep it independent of `api/auth`, store, and Connect. Use typed accessors and private keys as a separate interface cleanup. |+| `audit.go` | `component/audit` | Shared by v1, MCP, and OAuth2. Preserve the narrow writer interface, detached bounded write, denial severity, stdout behavior, and caller-IP semantics. Keep HTTP metadata helpers with the audit module initially. |+| Approval CEL definitions/validation and `risk.go` | `component/review` | This is review policy. Keep validation and runtime evaluation on the same definitions; map validation errors to Connect at the v1 caller. |+| IAM CEL definitions, member validation, `EvalBindingCondition` | A leaf `component/iam/condition` package | Store and utils call the evaluator. Moving it into the parent IAM package creates a cycle because IAM imports store and utils. Preserve partial-evaluation behavior. |+| Masking CEL definitions/validation | Initially private files in `api/v1` | All current external consumers are v1. The existing `component/masker` implements value masking, which is a different responsibility from policy-expression evaluation. |+| Database-group CEL definitions/validation | A leaf `component/databasegroup` package | Shared by v1 and the group evaluation in `utils`; move that evaluation into this owner too. Keep the expression core independent of store. |+| `cel_filter.go` | `common/filter` | Both store and API handlers use it. Preserve bounded parsing and errors that omit filter contents. It is list-filter syntax, separate from policy CEL. |+| `EngineSupport*` predicates | Feature owners where possible | SQL-review support belongs with advisor; SDL export with schema; completion with parser; single-consumer gates can stay private to their API or runner. Keep genuinely shared product capability rules in a small leaf `common/engine` if extraction into owners would invert dependencies. Do not infer product support solely from plugin registration. |+| `BackupDatabaseNameOfEngine` | `plugin/parser/base/backup.go` | Backup SQL, advisors, schema, and runners share this naming convention; parser base already owns backup primitives. Confirm the full import graph during implementation. |+| `EnvironmentOrderMap` | A leaf `component/environment` package | Shared by API plan/rollout construction and task execution; store is not the owner of ordering behavior. |+| Default-project ID rules and reserved review-config tag | `store` initially | Store creates the default project and persists the tag; API consumers already depend on store. Preserve legacy default-project recognition. |+| `error.go` | `common/errcode` initially | Codes cross store, drivers, enterprise, APIs, and runners. Retain numeric values and error matching; prune unused codes separately after inspecting wire/persisted uses. |++Do not move all of `cel.go` and `cel_attributes.go` to a generic CEL package.+Group variables with their policy dialect; a common implementation language does+not imply shared ownership. Split their tests along the same lines.++## Remaining util.go surface++| Code | Proposed home |+| --- | --- |+| `RoundRows`, `AddRows` | `plugin/parser/base`, alongside row-estimate handling; retain saturation tests. |+| `RandomString` | The shared identity leaf, alongside its credential consumers; retain cryptographic randomness. |+| `NormalizeExternalURL` | `component/config`, shared by startup and API configuration validation. |+| `TruncateString`, `SanitizeUTF8String` | A focused `common/text` package; preserve rune-count and invalid-byte behavior. |+| `ProtojsonUnmarshaler` | `common/protoutil`; it is used beyond store. Preserve `DiscardUnknown` for persisted protobuf JSON compatibility. |+| `Uniq` | A focused collection helper if retained; preserve order. `slices.Compact` is not an equivalent replacement. |+| `IsNil` | Inspect its v1 and T-SQL callers for typed nil checks before retaining a shared reflection helper. |+| `MaximumCommands`, default SQL result size and result-size message | `plugin/db`, which owns the execution interface and is already consumed by the relevant drivers. |+| `MaximumAdvicePerStatus`, `MaximumLintExplainSize` | `plugin/advisor`, alongside review and EXPLAIN-budget enforcement. |+| `MaxSheetSize`, `MaxSheetCheckSize` | A leaf `component/sheet/limits` package; the current sheet component and driver dependencies need to remain separate. Revisit whether display, parsing, and prior-backup limits truly share one policy in a separate behavior change. |++`common/permission` is already cohesive. Moving it under `component/iam/permission`+would clarify ownership, but it must remain a leaf because store imports it.+Keep `common/log`, `common/stacktrace`, `common/testcontainer`, and `common/yamltest`+for now: their names and interfaces communicate a specific shared purpose.+The testcontainer package deliberately depends on the store and migrator; it is+test infrastructure, not a production foundation.++## Delete before relocating++Repository-wide Go searches found no production callers for+`ConvertANTLRPositionToPosition`, `ConvertANTLRTokenToExclusiveEndPosition`,+`TrimSuffixAndGetInstanceDatabaseID`, `GetProjectIDPlanIDPlanCheckRunID`,+`FormatRevision`, `FormatProjectRevision`, or `FormatSpec`.+Some have tests only. The default test/prod environment constants also have no+production callers. Treat these as deletion candidates, with a final full-repo+reference check before removing them. Do not confuse internally called exported+helpers such as `GetNameParentTokens` with dead code.++## Migration order and validation++1. Remove verified dead helpers and perform the straightforward owner-local moves.+2. Extract resource names and split request context, then audit and identity.+3. Split policy expressions by owner, preserving validation/evaluation semantics.+4. Split engine behavior and the remaining util surface; consider whether the root+ common package can then disappear without introducing unnecessary tiny modules.++Move tests with each implementation and update callers directly rather than+adding permanent forwarding aliases. Validate import cycles after each batch.+Keep error mapping or interface redesign separate from mechanical relocation+where practical. Each code batch requires the repository's Go formatting, lint,+affected tests, and server-build gates. Update documented testcontainer imports+only if that package is moved later.++The initial audit used source and caller inspection. The first migration passes+tests for common, store, query builder, v1, webhook adapters, and the webhook+manager, plus the server build. Repository-wide lint reports existing findings+on code already present at HEAD. Remaining proposed package graphs are not yet compiler-verified.diff --git a/docs/design/audit-what-produces-a-row.md b/docs/design/audit-what-produces-a-row.mdindex 0e5b81266b..79d51beea2 100644--- a/docs/design/audit-what-produces-a-row.md+++ b/docs/design/audit-what-produces-a-row.md@@ -149,7 +149,7 @@ option on its response field.interceptor answers permission-denied, at the MCP ceiling gate, at theread-only SQL clamp, and at the custom-auth handler sites that make the checkthemselves. Handler sites mark by building the refusal with-`common.PermissionDeniedError`, so the mark cannot be forgotten separately+`permissionDeniedError` in `backend/api/v1`, so the mark cannot be forgotten separatelyfrom the error; the two out-of-band doors mark through their shared writer.## 4. Interceptor orderdiff --git a/docs/plans/2026-01-07-task-run-heartbeat-design.md b/docs/plans/2026-01-07-task-run-heartbeat-design.mdindex 264225bb9f..1ec529a732 100644--- a/docs/plans/2026-01-07-task-run-heartbeat-design.md+++ b/docs/plans/2026-01-07-task-run-heartbeat-design.md@@ -93,7 +93,7 @@ import ("github.com/pkg/errors"- "github.com/bytebase/bytebase/backend/common/qb"+ "github.com/bytebase/bytebase/backend/store/qb")// UpsertReplicaHeartbeat updates or inserts a replica heartbeat.