Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
test(api): take api/auth, oauth2, lsp and mcp off Postgres; api/v1 to 9 s (#21359)
Danny Xub86e038226e3
diff --git a/backend/api/auth/internal_interceptor.go b/backend/api/auth/internal_interceptor.goindex d00909b16f..393f3a41d8 100644--- a/backend/api/auth/internal_interceptor.go+++ b/backend/api/auth/internal_interceptor.go@@ -2,6 +2,7 @@ package authimport ("context"+ "net/http""time""connectrpc.com/connect"@@ -45,29 +46,10 @@ func NewInternalMCPAuthInterceptor(store *store.Store, secret string, profile *c// WrapUnary implements the ConnectRPC interceptor interface for unary RPCs.func (in *InternalMCPAuthInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {- tokenStr, err := GetTokenFromHeaders(req.Header())- if err != nil {- return nil, connect.NewError(connect.CodeUnauthenticated, err)- }- cred, err := VerifyInternalMCPToken(tokenStr, in.secret)- if err != nil {- return nil, connect.NewError(connect.CodeUnauthenticated, err)- }-- authContext, err := getAuthContext(req.Spec().Procedure)+ cred, authContext, err := authenticateDelegated(req.Header(), req.Spec().Procedure, in.secret)if err != nil {return nil, err}- // The credential's grant state travels verbatim into the AuthContext —- // this is the contract P1b's enforcement keys on (see- // common.DelegatedGrant for the empty-state semantics). Grant state- // only: the principal is re-resolved below, and RBAC downstream.- authContext.DelegatedGrant = &common.DelegatedGrant{- Scope: cred.Scope,- Resource: cred.Resource,- ClientID: cred.ClientID,- CorrelationID: cred.CorrelationID,- }ctx = context.WithValue(ctx, common.AuthContextKey, authContext)user, err := resolvePrincipal(ctx, in.store, in.profile, cred.Principal, cred.WorkspaceID)@@ -82,6 +64,34 @@ func (in *InternalMCPAuthInterceptor) WrapUnary(next connect.UnaryFunc) connect.}}+// authenticateDelegated verifies the delegated credential a request carries and+// builds the AuthContext for its procedure. The credential's grant state+// travels verbatim into the AuthContext — this is the contract P1b's+// enforcement keys on (see common.DelegatedGrant for the empty-state+// semantics). Grant state only: the principal is re-resolved by the caller on+// every request, and RBAC downstream.+func authenticateDelegated(header http.Header, procedure, secret string) (*DelegatedMCPCredential, *common.AuthContext, error) {+ tokenStr, err := GetTokenFromHeaders(header)+ if err != nil {+ return nil, nil, connect.NewError(connect.CodeUnauthenticated, err)+ }+ cred, err := VerifyInternalMCPToken(tokenStr, secret)+ if err != nil {+ return nil, nil, connect.NewError(connect.CodeUnauthenticated, err)+ }+ authContext, err := getAuthContext(procedure)+ if err != nil {+ return nil, nil, err+ }+ authContext.DelegatedGrant = &common.DelegatedGrant{+ Scope: cred.Scope,+ Resource: cred.Resource,+ ClientID: cred.ClientID,+ CorrelationID: cred.CorrelationID,+ }+ return cred, authContext, nil+}+// WrapStreamingClient implements the ConnectRPC interceptor interface for streaming clients.func (*InternalMCPAuthInterceptor) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc {return func(ctx context.Context, spec connect.Spec) connect.StreamingClientConn {diff --git a/backend/api/auth/internal_interceptor_livestate_test.go b/backend/api/auth/internal_interceptor_livestate_test.godeleted file mode 100644index 13efd44b16..0000000000--- a/backend/api/auth/internal_interceptor_livestate_test.go+++ /dev/null@@ -1,213 +0,0 @@-package auth--import (- "context"- "fmt"- "testing"- "time"-- "github.com/bytebase/bytebase/backend/common/testcontainer"-- "connectrpc.com/connect"- "github.com/stretchr/testify/require"- "google.golang.org/protobuf/types/known/emptypb"-- "github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/component/config"- "github.com/bytebase/bytebase/backend/store"-- _ "github.com/bytebase/bytebase/backend/plugin/db/pg"-)--const (- liveWorkspace = "ws-live"- liveUserEmail = "[email protected]"- liveClientID = "client-A"- liveResource = "https://bb.example.com/mcp"-)--// procedureRequest overrides a request's Spec so the interceptor sees a real-// registered procedure — getAuthContext resolves it through the proto registry,-// which connect.NewRequest alone leaves empty.-type procedureRequest struct {- connect.AnyRequest- procedure string-}--func (r *procedureRequest) Spec() connect.Spec {- return connect.Spec{Procedure: r.procedure}-}--func newProcedureRequest(t *testing.T, bearer string) *procedureRequest {- t.Helper()- req := connect.NewRequest(&emptypb.Empty{})- req.Header().Set("Authorization", "Bearer "+bearer)- return &procedureRequest{AnyRequest: req, procedure: "/bytebase.v1.SQLService/Query"}-}--// newLiveStore boots a migrated store with one workspace and one member, so-// resolvePrincipal has real state to re-resolve against.-func newLiveStore(t *testing.T) *store.Store {- ctx := context.Background()- db, st, _ := testcontainer.NewMetadataDB(t)-- _, err := db.ExecContext(ctx, fmt.Sprintf(`- INSERT INTO workspace (resource_id) VALUES ('%s');- INSERT INTO principal (name, email, password_hash) VALUES ('live', '%s', 'unused');- `, liveWorkspace, liveUserEmail))- require.NoError(t, err)-- _, err = st.PatchWorkspaceIamPolicy(ctx, &store.PatchIamPolicyMessage{- Workspace: liveWorkspace,- Member: common.FormatUserEmail(liveUserEmail),- Roles: []string{"roles/workspaceMember"},- })- require.NoError(t, err)- return st-}--// TestInternalInterceptorPopulatesDelegatedGrant pins the AuthContext contract-// P1b keys on: every internal-chain request carries the verified credential's-// grant state — scope, resource, client, correlation — verbatim, and the two-// empty-scope states stay distinguishable (see common.DelegatedGrant). The-// public chain carries none.-func TestInternalInterceptorPopulatesDelegatedGrant(t *testing.T) {- st := newLiveStore(t)- in := NewInternalMCPAuthInterceptor(st, testSecret, &config.Profile{})-- capture := func(t *testing.T, interceptor connect.Interceptor, bearer string) *common.AuthContext {- t.Helper()- var captured *common.AuthContext- next := func(ctx context.Context, _ connect.AnyRequest) (connect.AnyResponse, error) {- captured, _ = common.GetAuthContextFromContext(ctx)- return nil, nil- }- _, err := interceptor.WrapUnary(next)(context.Background(), newProcedureRequest(t, bearer))- require.NoError(t, err)- require.NotNil(t, captured)- return captured- }-- rows := []struct {- name string- cred DelegatedMCPCredential- }{- {- name: "a consented grant travels verbatim",- cred: DelegatedMCPCredential{- Principal: liveUserEmail,- WorkspaceID: liveWorkspace,- ClientID: liveClientID,- CorrelationID: "corr-1",- Scope: "mcp:read-only",- Resource: liveResource,- },- },- {- name: "legacy pre-grant session: scope and resource both empty",- cred: DelegatedMCPCredential{- Principal: liveUserEmail,- WorkspaceID: liveWorkspace,- CorrelationID: "corr-2",- },- },- {- // A grant that recorded no scope: a scope-less consent (permanent- // population) or a PR-3-era mint during a rolling upgrade- // (transient, and its grant DID record a scope). The populated- // resource is what keeps it distinguishable from the genuinely- // pre-grant row above — collapsing the two could widen a consented- // read-only session to full legacy semantics.- name: "grant-backed token: resource present, scope empty",- cred: DelegatedMCPCredential{- Principal: liveUserEmail,- WorkspaceID: liveWorkspace,- ClientID: liveClientID,- CorrelationID: "corr-3",- Resource: liveResource,- },- },- }- for _, row := range rows {- t.Run(row.name, func(t *testing.T) {- token, err := GenerateInternalMCPToken(row.cred, testSecret)- require.NoError(t, err)-- captured := capture(t, in, token)- require.NotNil(t, captured.DelegatedGrant,- "every internal-chain request must carry its delegated grant state")- require.Equal(t, row.cred.Scope, captured.DelegatedGrant.Scope)- require.Equal(t, row.cred.Resource, captured.DelegatedGrant.Resource)- require.Equal(t, row.cred.ClientID, captured.DelegatedGrant.ClientID)- require.Equal(t, row.cred.CorrelationID, captured.DelegatedGrant.CorrelationID)- })- }-- t.Run("the public chain carries no delegated grant", func(t *testing.T) {- webToken, err := GenerateAccessToken(liveUserEmail, liveWorkspace, testSecret, time.Hour)- require.NoError(t, err)-- pub := New(st, testSecret, nil, nil, &config.Profile{})- captured := capture(t, pub, webToken)- require.Nil(t, captured.DelegatedGrant,- "a public-chain request must leave the delegated grant zero-valued")- })-}--// TestInternalChainMembershipRevocationTakesEffectNextRequest pins the-// property the delegated credential rests on: it carries identity and grant-// state only, so authorization-relevant state — here workspace membership — is-// re-resolved against the store on EVERY internal request. Revoking membership-// must bite on the very next request with the SAME still-valid credential: no-// re-consent, no token expiry involved. If a refactor ever starts trusting the-// credential for authorization state, this goes red.-func TestInternalChainMembershipRevocationTakesEffectNextRequest(t *testing.T) {- ctx := context.Background()- st := newLiveStore(t)- in := NewInternalMCPAuthInterceptor(st, testSecret, &config.Profile{})-- token, err := GenerateInternalMCPToken(DelegatedMCPCredential{- Principal: liveUserEmail,- WorkspaceID: liveWorkspace,- ClientID: liveClientID,- CorrelationID: "corr-live",- Scope: "mcp:read-only",- Resource: liveResource,- }, testSecret)- require.NoError(t, err)-- call := func() (reached bool, err error) {- next := func(_ context.Context, _ connect.AnyRequest) (connect.AnyResponse, error) {- reached = true- return nil, nil- }- _, err = in.WrapUnary(next)(context.Background(), newProcedureRequest(t, token))- return reached, err- }-- reached, err := call()- require.NoError(t, err)- require.True(t, reached)-- setRoles := func(roles []string) {- _, err := st.PatchWorkspaceIamPolicy(ctx, &store.PatchIamPolicyMessage{- Workspace: liveWorkspace,- Member: common.FormatUserEmail(liveUserEmail),- Roles: roles,- })- require.NoError(t, err)- }-- setRoles(nil) // revoke the membership entirely- reached, err = call()- require.Error(t, err, "the same credential must stop working the moment membership is revoked")- require.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))- require.False(t, reached, "no handler may run for a revoked member")-- // Live in both directions, not a one-way latch: restoring membership- // restores service with the unchanged credential.- setRoles([]string{"roles/workspaceMember"})- reached, err = call()- require.NoError(t, err)- require.True(t, reached)-}diff --git a/backend/api/auth/internal_interceptor_test.go b/backend/api/auth/internal_interceptor_test.goindex d0d5e751a5..591086fa12 100644--- a/backend/api/auth/internal_interceptor_test.go+++ b/backend/api/auth/internal_interceptor_test.go@@ -2,6 +2,7 @@ package authimport ("context"+ "net/http""testing""time"@@ -61,3 +62,80 @@ func TestInternalInterceptorRejectsStreaming(t *testing.T) {err := in.WrapStreamingHandler(nil)(context.Background(), nil)require.Error(t, err)}++// TestAuthenticateDelegatedCarriesGrantVerbatim pins the AuthContext contract+// P1b keys on: every internal-chain request carries the verified credential's+// grant state — scope, resource, client, correlation — verbatim, and the two+// empty-scope states stay distinguishable (see common.DelegatedGrant). The+// principal is deliberately not part of this step; it is re-resolved against+// the store on every request, which backend/tests pins on a live server+// (TestMCPMembershipRevocationBitesLiveSession).+func TestAuthenticateDelegatedCarriesGrantVerbatim(t *testing.T) {+ rows := []struct {+ name string+ cred DelegatedMCPCredential+ }{+ {+ name: "a consented grant travels verbatim",+ cred: DelegatedMCPCredential{+ Principal: "[email protected]",+ WorkspaceID: "ws-live",+ ClientID: "client-A",+ CorrelationID: "corr-1",+ Scope: "mcp:read-only",+ Resource: testResource,+ },+ },+ {+ name: "legacy pre-grant session: scope and resource both empty",+ cred: DelegatedMCPCredential{+ Principal: "[email protected]",+ WorkspaceID: "ws-live",+ CorrelationID: "corr-2",+ },+ },+ {+ // A grant that recorded no scope: a scope-less consent (permanent+ // population) or a PR-3-era mint during a rolling upgrade+ // (transient, and its grant DID record a scope). The populated+ // resource is what keeps it distinguishable from the genuinely+ // pre-grant row above — collapsing the two could widen a consented+ // read-only session to full legacy semantics.+ name: "grant-backed token: resource present, scope empty",+ cred: DelegatedMCPCredential{+ Principal: "[email protected]",+ WorkspaceID: "ws-live",+ ClientID: "client-A",+ CorrelationID: "corr-3",+ Resource: testResource,+ },+ },+ }+ for _, row := range rows {+ t.Run(row.name, func(t *testing.T) {+ token, err := GenerateInternalMCPToken(row.cred, testSecret)+ require.NoError(t, err)+ header := http.Header{}+ header.Set("Authorization", "Bearer "+token)++ cred, authContext, err := authenticateDelegated(header, "/bytebase.v1.SQLService/Query", testSecret)+ require.NoError(t, err)+ require.Equal(t, row.cred, *cred)+ require.NotNil(t, authContext.DelegatedGrant,+ "every internal-chain request must carry its delegated grant state")+ require.Equal(t, row.cred.Scope, authContext.DelegatedGrant.Scope)+ require.Equal(t, row.cred.Resource, authContext.DelegatedGrant.Resource)+ require.Equal(t, row.cred.ClientID, authContext.DelegatedGrant.ClientID)+ require.Equal(t, row.cred.CorrelationID, authContext.DelegatedGrant.CorrelationID)+ })+ }++ t.Run("the public chain carries no delegated grant", func(t *testing.T) {+ // The public interceptor builds its AuthContext from the method alone+ // and never touches the grant; only the internal chain sets it.+ authContext, err := getAuthContext("/bytebase.v1.SQLService/Query")+ require.NoError(t, err)+ require.Nil(t, authContext.DelegatedGrant,+ "a public-chain request must leave the delegated grant zero-valued")+ })+}diff --git a/backend/api/auth/main_test.go b/backend/api/auth/main_test.godeleted file mode 100644index 07c589aaa8..0000000000--- a/backend/api/auth/main_test.go+++ /dev/null@@ -1,9 +0,0 @@-package auth--import (- "testing"-- "github.com/bytebase/bytebase/backend/common/testcontainer"-)--func TestMain(m *testing.M) { testcontainer.Main(m) }diff --git a/backend/api/lsp/main_test.go b/backend/api/lsp/main_test.godeleted file mode 100644index 7213fc0eee..0000000000--- a/backend/api/lsp/main_test.go+++ /dev/null@@ -1,9 +0,0 @@-package lsp--import (- "testing"-- "github.com/bytebase/bytebase/backend/common/testcontainer"-)--func TestMain(m *testing.M) { testcontainer.Main(m) }diff --git a/backend/api/lsp/project_instance_metadata_test.go b/backend/api/lsp/project_instance_metadata_test.godeleted file mode 100644index 4ed93c8fea..0000000000--- a/backend/api/lsp/project_instance_metadata_test.go+++ /dev/null@@ -1,59 +0,0 @@-package lsp--import (- "context"- "testing"-- "github.com/bytebase/bytebase/backend/common/testcontainer"-- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/common"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/store"-)--func TestMetadataProjectInstanceScope(t *testing.T) {- ctx := context.WithValue(context.Background(), common.WorkspaceIDContextKey, "default")- db, stores, _ := testcontainer.NewMetadataDB(t)- _, err := db.ExecContext(ctx, `- INSERT INTO workspace (resource_id) VALUES ('default');- INSERT INTO project (resource_id, workspace, name) VALUES- ('project-a', 'default', 'Project A'),- ('project-b', 'default', 'Project B');- `)- require.NoError(t, err)-- projectID := "project-a"- instanceID := "project-instance"- _, err = stores.CreateInstance(ctx, &store.InstanceMessage{- ResourceID: instanceID,- Workspace: "default",- ProjectID: &projectID,- Metadata: &storepb.Instance{DataSources: []*storepb.DataSource{{- Id: "admin",- Type: storepb.DataSourceType_ADMIN,- }}},- })- require.NoError(t, err)-- h := &Handler{store: stores}- canonicalName := common.FormatProjectInstance(projectID, instanceID)- h.setMetadata(SetMetadataCommandArguments{InstanceID: canonicalName})- require.Equal(t, canonicalName, h.metadata.InstanceID)- require.Equal(t, instanceID, h.getInstanceID())-- instance, err := h.getMetadataInstance(ctx, canonicalName)- require.NoError(t, err)- require.Equal(t, instanceID, instance.ResourceID)-- _, err = h.getMetadataInstance(ctx, common.FormatInstance(instanceID))- require.Error(t, err)- _, err = h.getMetadataInstance(ctx, common.FormatProjectInstance("project-b", instanceID))- require.Error(t, err)-- _, err = db.ExecContext(ctx, "UPDATE project SET deleted = TRUE WHERE resource_id = $1", projectID)- require.NoError(t, err)- _, err = h.getMetadataInstance(ctx, canonicalName)- require.Error(t, err)-}diff --git a/backend/api/mcp/connection_audit_test.go b/backend/api/mcp/connection_audit_test.goindex fbbb158eb9..04be24db9f 100644--- a/backend/api/mcp/connection_audit_test.go+++ b/backend/api/mcp/connection_audit_test.go@@ -145,6 +145,18 @@ func TestMCPConnectionDenialEmission(t *testing.T) {"the interceptor audits what an admitted session then does; this gate only records refusals")})+ t.Run("a read-only ceiling admits the session", func(t *testing.T) {+ // READ_ONLY opens a session: what a read-only session may then do is+ // refused per method by the gate on the internal chain and per+ // statement by the SQL clamp, not at this door.+ st := newTestServerStore()+ st.capability = storepb.MCPSetting_READ_ONLY+ srv := newServer(t, st)++ require.Equal(t, http.StatusOK, connect(t, srv, mcpToken(t, secret, tokenOptions{})))+ require.Empty(t, st.auditRows)+ })+t.Run("the row carries the whole grant, not just its presence", func(t *testing.T) {const resource = "https://bb.example.com/mcp"st := newTestServerStore()diff --git a/backend/api/mcp/main_test.go b/backend/api/mcp/main_test.godeleted file mode 100644index 9e05e0b2c7..0000000000--- a/backend/api/mcp/main_test.go+++ /dev/null@@ -1,9 +0,0 @@-package mcp--import (- "testing"-- "github.com/bytebase/bytebase/backend/common/testcontainer"-)--func TestMain(m *testing.M) { testcontainer.Main(m) }diff --git a/backend/api/mcp/server_killswitch_test.go b/backend/api/mcp/server_killswitch_test.godeleted file mode 100644index f467f89ec9..0000000000--- a/backend/api/mcp/server_killswitch_test.go+++ /dev/null@@ -1,311 +0,0 @@-package mcp--import (- "context"- "net/http"- "net/http/httptest"- "strings"- "testing"- "time"-- "github.com/bytebase/bytebase/backend/common/testcontainer"-- "github.com/golang-jwt/jwt/v5"- "github.com/labstack/echo/v5"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/api/auth"- "github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/component/config"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/store"-- _ "github.com/bytebase/bytebase/backend/plugin/db/pg"-)--// TestMCPKillSwitchEndToEnd drives the /mcp auth middleware against a real store:-// a workspace with DISABLED is rejected server-side with 403, while an-// explicit READ_WRITE ceiling is allowed. This is the server-side enforcement-// the kill-switch promises — the token authenticates fine; policy, not auth,-// blocks the disabled workspace.-func TestMCPKillSwitchEndToEnd(t *testing.T) {- ctx := context.Background()- db, s, _ := testcontainer.NewMetadataDB(t)-- _, err := db.ExecContext(ctx, `- INSERT INTO workspace (resource_id) VALUES ('ws-disabled'), ('ws-open');- `)- require.NoError(t, err)-- // Both workspaces have the metadata row required after migration.- _, err = s.UpsertSetting(ctx, &store.SettingMessage{- Name: storepb.SettingName_MCP,- Workspace: "ws-disabled",- Value: &storepb.MCPSetting{Capability: storepb.MCPSetting_DISABLED},- })- require.NoError(t, err)- _, err = s.UpsertSetting(ctx, &store.SettingMessage{- Name: storepb.SettingName_MCP,- Workspace: "ws-open",- Value: &storepb.MCPSetting{Capability: storepb.MCPSetting_READ_WRITE},- })- require.NoError(t, err)-- secret := "test-secret-key"- profile := &config.Profile{Mode: common.ReleaseModeDev, ExternalURL: "https://bb.example.com"}- srv, err := NewServer(s, profile, secret, nil)- require.NoError(t, err)-- statusFor := func(workspaceID string) int {- e := echo.New()- req := httptest.NewRequest(http.MethodPost, "/mcp", strings.NewReader(`{}`))- req.Header.Set("Content-Type", "application/json")- req.Header.Set("Authorization", "Bearer "+tokenForWorkspace(t, secret, workspaceID))- rec := httptest.NewRecorder()- c := e.NewContext(req, rec)- handler := srv.authMiddleware(func(c *echo.Context) error {- return c.String(http.StatusOK, "success")- })- if err := handler(c); err != nil {- echo.DefaultHTTPErrorHandler(true)(c, err)- }- return rec.Code- }-- require.Equal(t, http.StatusForbidden, statusFor("ws-disabled"),- "DISABLED workspace must be rejected server-side despite a valid token")- require.Equal(t, http.StatusOK, statusFor("ws-open"),- "the explicit READ_WRITE ceiling must be allowed")-}--// TestMCPKillSwitchBypassesSettingCache pins that the /mcp gate reads the-// stored policy fresh instead of through the store's setting cache. The cache-// has no TTL and only in-process writes refresh it, so with a cache-enabled-// store (the non-HA production shape) a cached READ_WRITE value would keep-// admitting MCP forever after the ceiling is flipped by an out-of-band admin-// path (direct SQL, another process) — the kill switch must observe the stored-// truth on the next request.-func TestMCPKillSwitchBypassesSettingCache(t *testing.T) {- ctx := context.Background()- db, s, _ := testcontainer.NewMetadataDBWithCache(t, true)-- _, err := db.ExecContext(ctx, `- INSERT INTO workspace (resource_id) VALUES ('ws-cached');- `)- require.NoError(t, err)-- // The explicit default row has to exist for the out-of-band flip below to- // have something to write into.- _, err = s.UpsertSetting(ctx, &store.SettingMessage{- Name: storepb.SettingName_MCP,- Workspace: "ws-cached",- Value: &storepb.MCPSetting{Capability: storepb.MCPSetting_READ_WRITE},- })- require.NoError(t, err)-- secret := "test-secret-key"- profile := &config.Profile{Mode: common.ReleaseModeDev, ExternalURL: "https://bb.example.com"}- srv, err := NewServer(s, profile, secret, nil)- require.NoError(t, err)-- statusFor := func() int {- e := echo.New()- req := httptest.NewRequest(http.MethodPost, "/mcp", strings.NewReader(`{}`))- req.Header.Set("Content-Type", "application/json")- req.Header.Set("Authorization", "Bearer "+tokenForWorkspace(t, secret, "ws-cached"))- rec := httptest.NewRecorder()- c := e.NewContext(req, rec)- handler := srv.authMiddleware(func(c *echo.Context) error {- return c.String(http.StatusOK, "success")- })- if err := handler(c); err != nil {- echo.DefaultHTTPErrorHandler(true)(c, err)- }- return rec.Code- }-- // First request: READ_WRITE ceiling -> allowed. This also primes the setting cache.- require.Equal(t, http.StatusOK, statusFor())-- // Flip the ceiling behind the store's back, as an emergency SQL toggle would.- _, err = db.ExecContext(ctx, `- UPDATE setting SET value = jsonb_set(value, '{capability}', '"DISABLED"')- WHERE workspace = 'ws-cached' AND name = 'MCP';- `)- require.NoError(t, err)-- require.Equal(t, http.StatusForbidden, statusFor(),- "a ceiling written out-of-band must be enforced on the next request, not served stale from the setting cache")-}--func tokenForWorkspace(t *testing.T, secret, workspaceID string) string {- t.Helper()- claims := jwt.MapClaims{- "iss": "bytebase",- "sub": "[email protected]",- "aud": auth.OAuth2AccessTokenAudience,- "workspace_id": workspaceID,- "exp": time.Now().Add(time.Hour).Unix(),- "iat": time.Now().Unix(),- }- token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)- token.Header["kid"] = "v1"- tokenStr, err := token.SignedString([]byte(secret))- require.NoError(t, err)- return tokenStr-}--// TestMCPCeilingStoredValueFailsClosed drives the /mcp connection gate against-// the ceiling values a workspace row can actually hold, including the ones no-// Bytebase write produces.-//-// The typo row is the one that mattered: protojson discards a field whose enum-// NAME it does not recognize, so `{"capability":"READ_WRTIE"}` would parse as-// unset and resolve to READ_WRITE — a mistyped kill switch that silently did-// nothing. An unknown enum NUMBER never had that problem, because protojson-// keeps it and no mode serves it.-//-// The read-only row is the cutover pin, now on its other side: READ_ONLY opens-// a session, because a read-only session can no longer write — the ceiling gate-// refuses the methods the mode does not cover and the SQL clamp refuses a-// statement that writes. The rows around it are what must NOT have moved with-// it: a capability this build does not recognize still refuses, and so does a-// value no Bytebase write produces.-func TestMCPCeilingStoredValueFailsClosed(t *testing.T) {- ctx := context.Background()- db, _, pgURL := testcontainer.NewMetadataDB(t)-- rows := []struct {- workspace string- value string- want int- why string- }{- {"ws-typo", `{"capability":"READ_WRTIE"}`, http.StatusForbidden,- "an unknown enum name resolves to UNSPECIFIED, which no mode permits"},- {"ws-reserved", `{"capability":2}`, http.StatusForbidden,- "the reserved number was METADATA_ONLY; no mode serves it"},- {"ws-explicit-unset", `{"capability":"CAPABILITY_UNSPECIFIED"}`, http.StatusForbidden,- "no Bytebase write produces this key with this value, so a row that has it was hand-edited"},- {"ws-readonly", `{"capability":"READ_ONLY"}`, http.StatusOK,- "READ_ONLY admits a session now that the SQL clamp holds it to reads"},- {"ws-other-unknown-field", `{"capability":"READ_WRITE","someFieldFromANewerRelease":"x"}`, http.StatusOK,- "one unknown field must not disable MCP when the required ceiling is valid"},- {"ws-open", `{"capability":"READ_WRITE"}`, http.StatusOK,- "the explicit default admits MCP"},- {"ws-missing-capability", `{}`, http.StatusForbidden,- "an existing row with an unset capability is invalid"},- {"ws-wrong-type", `{"capability":true}`, http.StatusServiceUnavailable,- "a payload the store protocol cannot decode is a metadata read failure"},- {"ws-ignoring", `{"capability":"READ_ONLY","ignoreMaskingExemptions":true}`, http.StatusOK,- "ignoring masking exemptions is not a reason to refuse the connection; it changes what the session reads"},- }-- for _, row := range rows {- _, err := db.ExecContext(ctx, `INSERT INTO workspace (resource_id) VALUES ($1)`, row.workspace)- require.NoError(t, err)- _, err = db.ExecContext(ctx,- `INSERT INTO setting (name, workspace, value) VALUES ('MCP', $1, $2)`,- row.workspace, row.value)- require.NoError(t, err)- }-- for _, ws := range []struct{ workspace, profile string }{- {workspace: "ws-absent"},- {workspace: "ws-legacy", profile: `{"mcpCapability":"DISABLED"}`},- } {- _, err := db.ExecContext(ctx, `INSERT INTO workspace (resource_id) VALUES ($1)`, ws.workspace)- require.NoError(t, err)- if ws.profile != "" {- _, err = db.ExecContext(ctx,- `INSERT INTO setting (name, workspace, value) VALUES ('WORKSPACE_PROFILE', $1, $2)`,- ws.workspace, ws.profile)- require.NoError(t, err)- }- }-- s, err := store.New(ctx, pgURL, false)- require.NoError(t, err)- t.Cleanup(func() { require.NoError(t, s.Close()) })-- secret := "test-secret-key"- profile := &config.Profile{Mode: common.ReleaseModeDev, ExternalURL: "https://bb.example.com"}- srv, err := NewServer(s, profile, secret, nil)- require.NoError(t, err)-- // The connection's STATUS alone cannot separate "the ceiling parsed as- // DISABLED" from "the ceiling could not be read": both are policy and both- // answer 403. Its wording does, and TestMCPConnectionDenialEmission pins- // that. The resolver is asserted directly here, on every workspace at once.- //- // That doubles as the composite-primary-key check the setting table needs.- // It is keyed (workspace, name), and GetMCPSettingsUncached is a new- // reader of it: a future edit that dropped the workspace predicate would- // return whichever row the scan reached first and apply one tenant's MCP- // ceiling to every other tenant — a kill switch answering for the wrong- // workspace. Every workspace below sits in this one database and must still- // resolve to its own stored value; the ws-readonly row must resolve as- // itself rather than as a fail-closed error, which is what separates "the- // ceiling says read-only" from "the ceiling could not be read".- for workspace, want := range map[string]storepb.MCPSetting_Capability{- "ws-readonly": storepb.MCPSetting_READ_ONLY,- "ws-open": storepb.MCPSetting_READ_WRITE,- "ws-other-unknown-field": storepb.MCPSetting_READ_WRITE,- "ws-ignoring": storepb.MCPSetting_READ_ONLY,- "ws-typo": storepb.MCPSetting_CAPABILITY_UNSPECIFIED,- "ws-reserved": storepb.MCPSetting_Capability(2),- "ws-explicit-unset": storepb.MCPSetting_CAPABILITY_UNSPECIFIED,- "ws-missing-capability": storepb.MCPSetting_CAPABILITY_UNSPECIFIED,- } {- got, err := s.GetMCPSettingsUncached(ctx, workspace)- require.NoError(t, err, "%s stores a ceiling this build understands", workspace)- require.Equal(t, want, got.Capability,- "%s must resolve its OWN stored ceiling, not a neighbour's", workspace)- }- // The toggle comes off the same row read as the ceiling, which is what lets- // one resolution per request hold for both. Asserting it here rather than in- // a test of its own is how that stays provable: these rows are read once- // each, so a reader that resolved the two fields separately would have to- // show it.- for workspace, want := range map[string]bool{- "ws-ignoring": true,- "ws-readonly": false,- "ws-open": false,- "ws-other-unknown-field": false,- } {- settings, err := s.GetMCPSettingsUncached(ctx, workspace)- require.NoError(t, err)- require.Equal(t, want, settings.IgnoreMaskingExemptions,- "%s must resolve its OWN toggle, and an unset one is false", workspace)- }-- for _, workspace := range []string{"ws-absent", "ws-legacy"} {- got, err := s.GetMCPSettingsUncached(ctx, workspace)- require.NoError(t, err)- require.Equal(t, storepb.MCPSetting_READ_WRITE, got.Capability,- "unexpected capability for %s", workspace)- require.False(t, got.IgnoreMaskingExemptions)- }-- got, err := s.GetMCPSettingsUncached(ctx, "ws-wrong-type")- require.Error(t, err, "an invalid MCP payload must remain a store error")- require.Nil(t, got, "an error must not carry a guessed settings result")-- for _, row := range rows {- t.Run(row.workspace, func(t *testing.T) {- e := echo.New()- req := httptest.NewRequest(http.MethodPost, "/mcp", strings.NewReader(`{}`))- req.Header.Set("Content-Type", "application/json")- req.Header.Set("Authorization", "Bearer "+tokenForWorkspace(t, secret, row.workspace))- rec := httptest.NewRecorder()- c := e.NewContext(req, rec)- handler := srv.authMiddleware(func(c *echo.Context) error {- return c.String(http.StatusOK, "success")- })- if err := handler(c); err != nil {- echo.DefaultHTTPErrorHandler(true)(c, err)- }- require.Equal(t, row.want, rec.Code, row.why)- })- }-}diff --git a/backend/api/mcp/server_test.go b/backend/api/mcp/server_test.goindex 36bd570ad6..70b693a92b 100644--- a/backend/api/mcp/server_test.go+++ b/backend/api/mcp/server_test.go@@ -139,8 +139,7 @@ func TestNewServerRequiresStore(t *testing.T) {// telling a client to retry a mistyped stored value promises something no// retry delivers.//-// The stored-value cases are covered against a real database in-// TestMCPCeilingStoredValueFailsClosed; this covers the arm above them.+// This covers the arm above the stored-value cases.func TestMCPReadFailureRefusesTheConnectionAsUnavailable(t *testing.T) {secret := "test-secret-key"profile := &config.Profile{Mode: common.ReleaseModeDev, ExternalURL: "https://bb.example.com"}@@ -576,3 +575,22 @@ func generateTokenWithWrongAudience(t *testing.T, secret string) string {require.NoError(t, err)return tokenStr}++// tokenForWorkspace mints a legacy-audience MCP token for a workspace, the+// shape the boundary still admits while such tokens live.+func tokenForWorkspace(t *testing.T, secret, workspaceID string) string {+ t.Helper()+ claims := jwt.MapClaims{+ "iss": "bytebase",+ "sub": "[email protected]",+ "aud": auth.OAuth2AccessTokenAudience,+ "workspace_id": workspaceID,+ "exp": time.Now().Add(time.Hour).Unix(),+ "iat": time.Now().Unix(),+ }+ token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)+ token.Header["kid"] = "v1"+ tokenStr, err := token.SignedString([]byte(secret))+ require.NoError(t, err)+ return tokenStr+}diff --git a/backend/api/mcp/store_test.go b/backend/api/mcp/store_test.goindex 19a953d38a..c8a57a7e24 100644--- a/backend/api/mcp/store_test.go+++ b/backend/api/mcp/store_test.go@@ -22,6 +22,9 @@ type testServerStore struct {// writeCtxHasDeadline shows the detached write is still bounded: dropping// the request's cancellation also drops its deadline.writeCtxHasDeadline bool+ // deletedRefreshGrants records the (user, client) pairs the reauthorize+ // tool asked to revoke.+ deletedRefreshGrants [][2]string}func newTestServerStore() *testServerStore {@@ -47,7 +50,8 @@ func (s *testServerStore) GetMCPSettingsUncached(context.Context, string) (*storreturn &storepb.MCPSetting{Capability: s.capability}, nil}-func (*testServerStore) DeleteOAuth2RefreshTokensByUserAndClient(context.Context, string, string) error {+func (s *testServerStore) DeleteOAuth2RefreshTokensByUserAndClient(_ context.Context, userEmail, clientID string) error {+ s.deletedRefreshGrants = append(s.deletedRefreshGrants, [2]string{userEmail, clientID})return nil}diff --git a/backend/api/mcp/tool_reauthorize_test.go b/backend/api/mcp/tool_reauthorize_test.goindex 93060bacbf..4191ae4738 100644--- a/backend/api/mcp/tool_reauthorize_test.go+++ b/backend/api/mcp/tool_reauthorize_test.go@@ -8,52 +8,24 @@ import ("testing""time"- "github.com/bytebase/bytebase/backend/common/testcontainer"-"github.com/labstack/echo/v5""github.com/modelcontextprotocol/go-sdk/mcp""github.com/stretchr/testify/require"- "github.com/bytebase/bytebase/backend/api/auth""github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/component/config"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/store"-- _ "github.com/bytebase/bytebase/backend/plugin/db/pg")-// newReauthorizeTestServer boots a store-backed MCP server with one OAuth2-// client, one principal, and one live refresh grant — the state the reauthorize-// tool operates on.-func newReauthorizeTestServer(ctx context.Context, t *testing.T) (*Server, *store.Store, string) {+// newReauthorizeTestServer builds an MCP server over the in-memory store, which+// records the refresh grants the reauthorize tool asks it to revoke. That the+// real store deletes them and the token endpoint stops refreshing is pinned+// in backend/tests TestOAuth2GrantLifecycle.+func newReauthorizeTestServer(t *testing.T) (*Server, *testServerStore) {t.Helper()- db, st, _ := testcontainer.NewMetadataDB(t)-- _, err := db.ExecContext(ctx, `- INSERT INTO workspace (resource_id) VALUES ('ws-test');- INSERT INTO setting (name, workspace, value)- VALUES ('MCP', 'ws-test', '{"capability":"READ_WRITE"}');- INSERT INTO principal (name, email, password_hash) VALUES ('demo', '[email protected]', 'unused');- INSERT INTO oauth2_client (client_id, workspace, client_secret_hash, config)- VALUES ('client-A', NULL, 'unused-hash', '{"clientName":"test","redirectUris":["http://localhost/cb"],"grantTypes":["authorization_code","refresh_token"],"tokenEndpointAuthMethod":"none"}'::jsonb);- `)- require.NoError(t, err)-- refreshToken := "refresh-token"- _, err = st.CreateOAuth2RefreshToken(ctx, &store.OAuth2RefreshTokenMessage{- TokenHash: auth.HashToken(refreshToken),- ClientID: "client-A",- UserEmail: "[email protected]",- Workspace: "ws-test",- Config: &storepb.OAuth2RefreshTokenConfig{},- ExpiresAt: time.Now().Add(time.Hour),- })- require.NoError(t, err)-- s, err := NewServer(st, &config.Profile{Mode: common.ReleaseModeDev, ExternalURL: "https://bb.example.com"}, revalidationSecret, nil)+ st := newTestServerStore()+ s, err := newServerWithStore(st, &config.Profile{Mode: common.ReleaseModeDev, ExternalURL: "https://bb.example.com"}, revalidationSecret, nil)require.NoError(t, err)- return s, st, refreshToken+ return s, st}// probeAuthMiddleware reports the status the /mcp boundary gives a bearer.@@ -75,10 +47,9 @@ func probeAuthMiddleware(t *testing.T, s *Server, bearer string) int {func TestReauthorizeRejectsCurrentAccessToken(t *testing.T) {ctx := context.Background()- s, st, refreshToken := newReauthorizeTestServer(ctx, t)- const secret = revalidationSecret+ s, st := newReauthorizeTestServer(t)- accessToken := generateOAuth2MCPToken(t, secret, "client-A", "ws-test")+ accessToken := generateOAuth2MCPToken(t, revalidationSecret, "client-A", "ws-test")reauthorizeCtx := withAccessToken(ctx, accessToken)reauthorizeCtx = withUserEmail(reauthorizeCtx, "[email protected]")reauthorizeCtx = withOAuth2ClientID(reauthorizeCtx, "client-A")@@ -86,10 +57,8 @@ func TestReauthorizeRejectsCurrentAccessToken(t *testing.T) {_, _, err := s.handleReauthorize(reauthorizeCtx, nil, ReauthorizeInput{})require.NoError(t, err)- stored, err := st.GetOAuth2RefreshToken(ctx, "client-A", auth.HashToken(refreshToken))- require.NoError(t, err)- require.Nil(t, stored)-+ require.Equal(t, [][2]string{{"[email protected]", "client-A"}}, st.deletedRefreshGrants,+ "the caller's refresh grants for this client are revoked, and only those")require.Equal(t, http.StatusUnauthorized, probeAuthMiddleware(t, s, accessToken))}@@ -104,7 +73,7 @@ func TestReauthorizeRejectsCurrentAccessToken(t *testing.T) {// grants but never producing the OAuth challenge the tool promises.func TestReauthorizeRejectsRefreshedAccessToken(t *testing.T) {ctx := context.Background()- s, _, _ := newReauthorizeTestServer(ctx, t)+ s, _ := newReauthorizeTestServer(t)e := echo.New()s.RegisterRoutes(e)diff --git a/backend/api/oauth2/authorize_test.go b/backend/api/oauth2/authorize_test.gonew file mode 100644index 0000000000..fe0d1d65e3--- /dev/null+++ b/backend/api/oauth2/authorize_test.go@@ -0,0 +1,19 @@+package oauth2++import (+ "testing"++ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/backend/component/config"+ "github.com/bytebase/bytebase/backend/store"+)++func TestConsentWorkspaceRequiresSessionClaim(t *testing.T) {+ s := &Service{profile: &config.Profile{}}++ workspaceID, failure := s.consentWorkspace(&sessionClaims{}, &store.OAuth2ClientMessage{})++ require.Empty(t, workspaceID)+ require.Equal(t, &oauth2Failure{code: "access_denied", description: "session is missing workspace claim"}, failure)+}diff --git a/backend/api/oauth2/consent_audit.go b/backend/api/oauth2/consent_audit.goindex c648916881..bdb602dc17 100644--- a/backend/api/oauth2/consent_audit.go+++ b/backend/api/oauth2/consent_audit.go@@ -79,7 +79,19 @@ func (s *Service) refuseConsentByCeiling(c *echo.Context, attempt consentAttempt// The row is written here for the same reason the connection gate writes its// own: this route is echo, so no interceptor sees it.func (s *Service) refuseConsent(c *echo.Context, attempt consentAttempt, verdict auth.MCPCeilingVerdict) error {- row := &storepb.AuditLog{+ row := consentRefusalRow(attempt, verdict, common.RequestMetadataFromHTTP(c.Request()))+ common.RecordOutOfBandAudit(c.Request().Context(), s.store,+ s.profile.RuntimeEnableAuditLogStdout.Load(), attempt.user.workspaceID, row)+ return c.HTML(http.StatusForbidden, consentRefusedHTML(verdict, attempt.redirectURI, attempt.state))+}++// consentRefusalRow is the audit row a refused consent leaves: a+// PermissionDenied under the consenting user's workspace, carrying the MCP+// provenance this flow has — the client asking, and the resource and scope it+// asked for. No correlation ID: that is minted at /mcp, and this attempt never+// got there.+func consentRefusalRow(attempt consentAttempt, verdict auth.MCPCeilingVerdict, requestMetadata *storepb.RequestMetadata) *storepb.AuditLog {+ return &storepb.AuditLog{Parent: common.FormatWorkspace(attempt.user.workspaceID),Method: common.AuditMethodMCPConsentApprove,Resource: common.FormatWorkspace(attempt.user.workspaceID),@@ -89,19 +101,13 @@ func (s *Service) refuseConsent(c *echo.Context, attempt consentAttempt, verdictCode: int32(codes.PermissionDenied),Message: verdict.Refusal(),},- RequestMetadata: common.RequestMetadataFromHTTP(c.Request()),- // The MCP provenance this flow has: the client asking, and the- // resource and scope it asked for. No correlation ID — that is minted- // at /mcp, and this attempt never got there.+ RequestMetadata: requestMetadata,McpDelegation: &storepb.MCPDelegation{Scope: attempt.params.scope,Resource: attempt.params.resource,ClientId: attempt.clientID,},}- common.RecordOutOfBandAudit(c.Request().Context(), s.store,- s.profile.RuntimeEnableAuditLogStdout.Load(), attempt.user.workspaceID, row)- return c.HTML(http.StatusForbidden, consentRefusedHTML(verdict, attempt.redirectURI, attempt.state))}// consentRefusedHTML renders the refusal. Inline styles only: the global CSPdiff --git a/backend/api/oauth2/consent_audit_test.go b/backend/api/oauth2/consent_audit_test.gonew file mode 100644index 0000000000..9768c788ff--- /dev/null+++ b/backend/api/oauth2/consent_audit_test.go@@ -0,0 +1,75 @@+package oauth2++import (+ "strings"+ "testing"++ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/backend/api/auth"+ "github.com/bytebase/bytebase/backend/common"+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+)++// TestConsentRefusal pins what a consent refused by the MCP ceiling leaves+// behind: the audit row an operator finds with `mcp == true`, and the page the+// user sees. The verdict itself is auth.ClassifyMCPCeiling's, pinned in+// api/auth; that a refused consent writes no authorization code and lands this+// row on a live server is TestMCPPolicyDenialsReachTheAuditPage in+// backend/tests.+func TestConsentRefusal(t *testing.T) {+ attempt := consentAttempt{+ user: consentingUser{email: "[email protected]", workspaceID: "ws-disabled"},+ clientID: "client-A",+ params: grantParams{resource: "https://bb.example.com/mcp", scope: "mcp:read-only"},+ redirectURI: "http://localhost/cb",+ state: "state-1",+ }+ for _, tc := range []struct {+ name string+ capability storepb.MCPSetting_Capability+ message string+ page []string+ }{+ {+ name: "MCP turned off",+ capability: storepb.MCPSetting_DISABLED,+ message: "turned MCP access off",+ page: []string{"<title>MCP access is turned off</title>", "turned MCP access off for this workspace", "in the workspace settings"},+ },+ {+ name: "a ceiling this build does not serve",+ capability: storepb.MCPSetting_Capability(2),+ message: "not one this build serves",+ page: []string{"not one this build serves", "not one this version supports"},+ },+ } {+ t.Run(tc.name, func(t *testing.T) {+ verdict := auth.ClassifyMCPCeiling(&storepb.MCPSetting{Capability: tc.capability}, nil)+ require.True(t, verdict.IsPolicy())++ row := consentRefusalRow(attempt, verdict, &storepb.RequestMetadata{CallerIp: "10.0.1.50"})+ require.Equal(t, common.AuditMethodMCPConsentApprove, row.Method)+ require.Equal(t, "workspaces/ws-disabled", row.Parent)+ require.Equal(t, "users/[email protected]", row.User)+ require.EqualValues(t, 7, row.GetStatus().GetCode(), "PermissionDenied")+ require.Contains(t, row.GetStatus().GetMessage(), tc.message)+ require.Equal(t, "10.0.1.50", row.GetRequestMetadata().GetCallerIp())+ // The MCP marker, so `mcp == true` finds this alongside the+ // connection denial the same user would have hit next.+ require.Equal(t, "client-A", row.GetMcpDelegation().GetClientId())+ require.Equal(t, "mcp:read-only", row.GetMcpDelegation().GetScope())+ require.Equal(t, "https://bb.example.com/mcp", row.GetMcpDelegation().GetResource())+ require.Empty(t, row.GetMcpDelegation().GetCorrelationId(),+ "a consent never reached the /mcp boundary that mints one")++ page := consentRefusedHTML(verdict, attempt.redirectURI, attempt.state)+ for _, want := range tc.page {+ require.Contains(t, page, want)+ }+ require.NotContains(t, page, "code=", "no authorization code may be issued")+ require.Contains(t, page, "error=access_denied", "the way back tells the client the policy refused it")+ require.True(t, strings.Contains(page, "Nothing was connected."))+ })+ }+}diff --git a/backend/api/oauth2/consent_ceiling_test.go b/backend/api/oauth2/consent_ceiling_test.godeleted file mode 100644index 91affa1a13..0000000000--- a/backend/api/oauth2/consent_ceiling_test.go+++ /dev/null@@ -1,380 +0,0 @@-package oauth2--import (- "context"- "crypto/sha256"- "encoding/base64"- "net/http"- "net/http/httptest"- "net/url"- "strings"- "testing"- "time"-- "github.com/bytebase/bytebase/backend/common/testcontainer"-- "github.com/labstack/echo/v5"- "github.com/pkg/errors"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/api/auth"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/store"-- _ "github.com/bytebase/bytebase/backend/plugin/db/pg"-)--// TestConsentCeiling pins the ceiling check this flow did not have.-//-// Before it, the authorization flow knew nothing about the MCP ceiling: a user-// in a workspace with MCP turned off approved the consent, was issued a real-// authorization code, exchanged it for a real token, and only learned at /mcp-// that the door refuses them. The credential outlived the misunderstanding,-// and nothing recorded that anyone had tried.-//-// Two halves, and they are separable on purpose. The refusal is the-// enforcement and does not depend on the row; the row is what an operator-// sees and does not change the verdict. Removing the write leaves every-// refusal assertion below green and fails only the row assertions.-func TestConsentCeiling(t *testing.T) {- ctx := context.Background()- db, st, _ := testcontainer.NewMetadataDB(t)-- _, err := db.ExecContext(ctx, `- INSERT INTO workspace (resource_id) VALUES- ('ws-disabled'), ('ws-readonly'), ('ws-readwrite'), ('ws-typo'), ('ws-reserved');- INSERT INTO principal (name, email, password_hash) VALUES ('demo', '[email protected]', 'unused');- INSERT INTO oauth2_client (client_id, workspace, client_secret_hash, config)- VALUES ('client-A', NULL, 'unused-hash', '{"clientName":"test","redirectUris":["http://localhost/cb"],"grantTypes":["authorization_code","refresh_token"],"tokenEndpointAuthMethod":"none"}'::jsonb);- INSERT INTO setting (name, workspace, value) VALUES- ('MCP', 'ws-disabled', '{"capability":"DISABLED"}'),- ('MCP', 'ws-readonly', '{"capability":"READ_ONLY"}'),- ('MCP', 'ws-readwrite', '{"capability":"READ_WRITE"}'),- ('MCP', 'ws-typo', '{"capability":"READ_ONLYY"}'),- ('MCP', 'ws-reserved', '{"capability":2}');- `)- require.NoError(t, err)-- s := newTestService(st, "https://bb.example.com")-- auditRows := func(t *testing.T, workspace string) []*store.AuditLog {- t.Helper()- logs, err := st.SearchAuditLogs(ctx, &store.AuditLogFind{Workspace: workspace})- require.NoError(t, err)- return logs- }-- t.Run("a disabled workspace refuses the consent and records the attempt", func(t *testing.T) {- rec := consentTo(t, s, "ws-disabled")-- require.Equal(t, http.StatusForbidden, rec.Code)- body := rec.Body.String()- require.Contains(t, body, "MCP access is turned off")- require.Contains(t, body, "turned MCP access off for this workspace")- require.Contains(t, body, "<title>MCP access is turned off</title>")- require.Contains(t, body, "in the workspace settings", "the page names what to ask an admin for")- require.NotContains(t, body, "code=", "no authorization code may be issued")-- // Against the table, not only the page: a refusal that rendered but- // still wrote a code would leave a usable credential behind.- var codes int- require.NoError(t, db.QueryRowContext(ctx,- `SELECT count(*) FROM oauth2_authorization_code`).Scan(&codes))- require.Zero(t, codes, "a refused consent must persist no authorization code")-- rows := auditRows(t, "ws-disabled")- require.Len(t, rows, 1)- row := rows[0].Payload- require.Equal(t, "/bytebase.mcp.Consent/Approve", row.GetMethod())- require.Equal(t, "workspaces/ws-disabled", row.GetParent())- require.Equal(t, "users/[email protected]", row.GetUser())- require.EqualValues(t, 7, row.GetStatus().GetCode(), "PermissionDenied")- require.Contains(t, row.GetStatus().GetMessage(), "turned MCP access off")-- // The MCP marker, so `mcp == true` finds this alongside the- // connection denial the same user would have hit next.- require.NotNil(t, row.GetMcpDelegation())- require.Equal(t, testClientID, row.GetMcpDelegation().GetClientId())- require.Equal(t, "mcp:read-only", row.GetMcpDelegation().GetScope())- require.Equal(t, testResource, row.GetMcpDelegation().GetResource())- require.Empty(t, row.GetMcpDelegation().GetCorrelationId(),- "a consent never reached the /mcp boundary that mints one")- })-- t.Run("an unknown capability is refused as unserved", func(t *testing.T) {- rec := consentTo(t, s, "ws-typo")-- require.Equal(t, http.StatusForbidden, rec.Code)- require.Contains(t, rec.Body.String(), "not one this build serves")- require.Contains(t, rec.Body.String(), "not one this version supports")- require.NotContains(t, rec.Body.String(), "MCP access is turned off")-- rows := auditRows(t, "ws-typo")- require.Len(t, rows, 1)- require.Contains(t, rows[0].Payload.GetStatus().GetMessage(), "not one this build serves")- })-- t.Run("a ceiling this build does not serve is refused too", func(t *testing.T) {- rec := consentTo(t, s, "ws-reserved")-- require.Equal(t, http.StatusForbidden, rec.Code)- require.Contains(t, rec.Body.String(), "not one this build serves")- require.Contains(t, rec.Body.String(), "not one this version supports")- require.NotContains(t, rec.Body.String(), "MCP access is turned off")- require.Len(t, auditRows(t, "ws-reserved"), 1)- })-- t.Run("a serving ceiling consents and records nothing", func(t *testing.T) {- for _, workspace := range []string{"ws-readonly", "ws-readwrite"} {- t.Run(workspace, func(t *testing.T) {- rec := consentTo(t, s, workspace)-- require.Equal(t, http.StatusOK, rec.Code)- redirect := redirectTargetOf(t, rec.Body.String())- require.Empty(t, redirect.Query().Get("error"))- require.NotEmpty(t, redirect.Query().Get("code"))-- require.Empty(t, auditRows(t, workspace),- "this gate records refusals; a granted consent is the token endpoint's story")- })- }- })-}--// TestConsentCeilingRefusalIsNotAWideningPath pins that the refusal cannot be-// walked around by asking for a different scope: the ceiling decides whether-// any grant is issued at all, before the requested mode is considered.-func TestConsentCeilingRefusalIsNotAWideningPath(t *testing.T) {- ctx := context.Background()- db, st, _ := testcontainer.NewMetadataDB(t)-- _, err := db.ExecContext(ctx, `- INSERT INTO workspace (resource_id) VALUES ('ws-disabled');- INSERT INTO principal (name, email, password_hash) VALUES ('demo', '[email protected]', 'unused');- INSERT INTO oauth2_client (client_id, workspace, client_secret_hash, config)- VALUES ('client-A', NULL, 'unused-hash', '{"clientName":"test","redirectUris":["http://localhost/cb"],"grantTypes":["authorization_code","refresh_token"],"tokenEndpointAuthMethod":"none"}'::jsonb);- INSERT INTO setting (name, workspace, value) VALUES ('MCP', 'ws-disabled', '{"capability":"DISABLED"}');- `)- require.NoError(t, err)-- s := newTestService(st, "https://bb.example.com")-- for _, scope := range []string{"mcp:read-only", "mcp:read-write", ""} {- rec := consentTo(t, s, "ws-disabled", url.Values{"scope": {scope}})- require.Equal(t, http.StatusForbidden, rec.Code, "scope %q", scope)- }- logs, err := st.SearchAuditLogs(ctx, &store.AuditLogFind{Workspace: "ws-disabled"})- require.NoError(t, err)- require.Len(t, logs, 3, "every attempt is recorded, not just the first")-}--// consentTo posts an approved consent form as a user whose session is in the-// given workspace, and returns the raw response so a caller can read either the-// refusal page or the meta-refresh carrying the code.-func consentTo(t *testing.T, s *Service, workspaceID string, extra ...url.Values) *httptest.ResponseRecorder {- t.Helper()-- challenge := sha256.Sum256([]byte(testCodeVerifier))- form := url.Values{- "client_id": {testClientID},- "redirect_uri": {testRedirectURI},- "state": {"state-1"},- "code_challenge": {base64.RawURLEncoding.EncodeToString(challenge[:])},- "code_challenge_method": {"S256"},- "action": {"allow"},- "resource": {testResource},- "scope": {"mcp:read-only"},- }- for _, values := range extra {- for name, value := range values {- form[name] = value- }- }-- sessionToken, err := auth.GenerateAccessToken(testUserEmail, workspaceID, testSecret, time.Hour)- require.NoError(t, err)-- req := httptest.NewRequest(http.MethodPost, "/api/oauth2/authorize", strings.NewReader(form.Encode()))- req.Header.Set("Content-Type", "application/x-www-form-urlencoded")- req.Header.Set("Authorization", "Bearer "+sessionToken)- rec := httptest.NewRecorder()-- e := echo.New()- s.RegisterRoutes(e)- e.ServeHTTP(rec, req)- return rec-}--// TestConsentCeilingOutageIsNotARefusal pins the arm a real store cannot be-// asked to take: the ceiling read itself fails.-//-// It is the one exit that returns a nil error while having written a response,-// which is the exact shape this flow already got wrong once — a caller reading-// the nil as "may proceed" mints an authorization code underneath a 200 the-// client reads as an error. And it must not read as a refusal: telling a user-// their admin disabled MCP during a database blip sends them to an admin with-// nothing to fix.-func TestConsentCeilingOutageIsNotARefusal(t *testing.T) {- ctx := context.Background()- db, st, _ := testcontainer.NewMetadataDB(t)-- _, err := db.ExecContext(ctx, `- INSERT INTO workspace (resource_id) VALUES ('ws-outage');- INSERT INTO principal (name, email, password_hash) VALUES ('demo', '[email protected]', 'unused');- INSERT INTO oauth2_client (client_id, workspace, client_secret_hash, config)- VALUES ('client-A', NULL, 'unused-hash', '{"clientName":"test","redirectUris":["http://localhost/cb"],"grantTypes":["authorization_code","refresh_token"],"tokenEndpointAuthMethod":"none"}'::jsonb);- `)- require.NoError(t, err)-- s := newTestService(st, "https://bb.example.com")- s.mcpCeiling = failingCeilingReader{err: errors.New("connection refused")}-- rec := consentTo(t, s, "ws-outage")-- // The client is told to retry, in its own vocabulary, and gets no code.- require.Equal(t, http.StatusOK, rec.Code, "an error redirect is a 200 carrying the error")- redirect := redirectTargetOf(t, rec.Body.String())- require.Equal(t, "temporarily_unavailable", redirect.Query().Get("error"))- require.Empty(t, redirect.Query().Get("code"), "an outage must not mint a credential either")-- var codes int- require.NoError(t, db.QueryRowContext(ctx, `SELECT count(*) FROM oauth2_authorization_code`).Scan(&codes))- require.Zero(t, codes)-- logs, err := st.SearchAuditLogs(ctx, &store.AuditLogFind{Workspace: "ws-outage"})- require.NoError(t, err)- require.Empty(t, logs, "an outage recorded as a policy denial is a decision nobody made")-}--// failingCeilingReader is a ceiling read that fails, which is what a real store-// cannot be made to do for one call.-type failingCeilingReader struct{ err error }--func (f failingCeilingReader) GetMCPSettingsUncached(context.Context, string) (*storepb.MCPSetting, error) {- return nil, f.err-}--// TestTokenIssuanceRechecksTheCeiling pins the half a consent-time check alone-// would miss.-//-// The consent decision is captured once and reused at every later issuance: an-// authorization code outlives the consent by ten minutes and a refresh token by-// thirty days, so a grant consented while MCP was on keeps minting access-// tokens long after an admin turns MCP off. The workspace membership check on-// this endpoint exists for the same reason, and says so in its own comment.-func TestTokenIssuanceRechecksTheCeiling(t *testing.T) {- ctx := context.Background()- db, st, _ := testcontainer.NewMetadataDB(t)-- _, err := db.ExecContext(ctx, `- INSERT INTO workspace (resource_id) VALUES ('ws-test');- INSERT INTO principal (name, email, password_hash) VALUES ('demo', '[email protected]', 'unused');- INSERT INTO oauth2_client (client_id, workspace, client_secret_hash, config)- VALUES ('client-A', NULL, 'unused-hash', '{"clientName":"test","redirectUris":["http://localhost/cb"],"grantTypes":["authorization_code","refresh_token"],"tokenEndpointAuthMethod":"none"}'::jsonb);- INSERT INTO setting (name, workspace, value) VALUES ('MCP', 'ws-test', '{"capability":"READ_WRITE"}');- `)- require.NoError(t, err)-- s := newTestService(st, "https://bb.example.com")-- // Consent while MCP is on, so the grant is genuine and only the ceiling- // moves underneath it.- code := consentOK(t, s, url.Values{"resource": {testResource}, "scope": {"mcp:read-only"}})- tokens := tokenOK(t, s, url.Values{- "grant_type": {"authorization_code"},- "code": {code},- "redirect_uri": {testRedirectURI},- "code_verifier": {testCodeVerifier},- "client_id": {testClientID},- })- require.NotEmpty(t, tokens.RefreshToken)-- _, err = db.ExecContext(ctx,- `UPDATE setting SET value = '{"capability":"DISABLED"}' WHERE workspace = 'ws-test' AND name = 'MCP'`)- require.NoError(t, err)-- refresh := func() *httptest.ResponseRecorder {- return postToken(t, s, url.Values{- "grant_type": {"refresh_token"},- "refresh_token": {tokens.RefreshToken},- "client_id": {testClientID},- })- }-- // The refusal must read as reversible, not as a dead grant. invalid_grant- // is what legacyGrantFailure uses to make a compliant client discard the- // credential and rerun the OAuth flow — exactly what this path retains the- // credential to avoid — so a ceiling refusal must not borrow it.- t.Run("a refresh stops re-issuing, retryably", func(t *testing.T) {- rec := refresh()- require.Equal(t, http.StatusServiceUnavailable, rec.Code, rec.Body.String())- require.Equal(t, "temporarily_unavailable", errorCode(t, rec))- require.NotEqual(t, "invalid_grant", errorCode(t, rec),- "a compliant client discards the grant on invalid_grant, and this one must keep it")- require.Contains(t, errorDescription(t, rec), "turned MCP access off")- })-- // The refusal must not burn the credential it refuses. The ceiling is a- // toggle, and its own message tells the user raising it restores service —- // so an hour of MCP being off must not cost every client its refresh token- // and force a fresh interactive consent. The consume is single-use and- // irreversible, so this only holds while the check runs before it.- t.Run("the refused refresh survives the policy being reversed", func(t *testing.T) {- var left int- require.NoError(t, db.QueryRowContext(ctx,- `SELECT count(*) FROM oauth2_refresh_token`).Scan(&left))- require.Equal(t, 1, left, "a refused refresh must not consume the token")-- _, err := db.ExecContext(ctx,- `UPDATE setting SET value = '{"capability":"READ_WRITE"}' WHERE workspace = 'ws-test' AND name = 'MCP'`)- require.NoError(t, err)-- rec := refresh()- require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())-- // Put it back so the subtest below still meets a refusing ceiling.- _, err = db.ExecContext(ctx,- `UPDATE setting SET value = '{"capability":"DISABLED"}' WHERE workspace = 'ws-test' AND name = 'MCP'`)- require.NoError(t, err)- })-- t.Run("an unexchanged code stops exchanging, and is not burned either", func(t *testing.T) {- // A code minted before the ceiling moved. The consent that produced it- // was allowed; the exchange is not.- s.mcpCeiling = servingCeilingReader{}- fresh := consentOK(t, s, url.Values{"resource": {testResource}, "scope": {"mcp:read-only"}})- s.mcpCeiling = st-- exchange := func() *httptest.ResponseRecorder {- return postToken(t, s, url.Values{- "grant_type": {"authorization_code"},- "code": {fresh},- "redirect_uri": {testRedirectURI},- "code_verifier": {testCodeVerifier},- "client_id": {testClientID},- })- }-- rec := exchange()- require.Equal(t, http.StatusServiceUnavailable, rec.Code, rec.Body.String())- require.Equal(t, "temporarily_unavailable", errorCode(t, rec))-- var left int- require.NoError(t, db.QueryRowContext(ctx,- `SELECT count(*) FROM oauth2_authorization_code`).Scan(&left))- require.Equal(t, 1, left, "a refused exchange must not consume the code")-- _, err := db.ExecContext(ctx,- `UPDATE setting SET value = '{"capability":"READ_WRITE"}' WHERE workspace = 'ws-test' AND name = 'MCP'`)- require.NoError(t, err)- require.Equal(t, http.StatusOK, exchange().Code, "the same code exchanges once the ceiling is raised")- })-}--// servingCeilingReader lets a consent through so the test can mint a grant that-// the live ceiling would now refuse.-type servingCeilingReader struct{}--func (servingCeilingReader) GetMCPSettingsUncached(context.Context, string) (*storepb.MCPSetting, error) {- return &storepb.MCPSetting{Capability: storepb.MCPSetting_READ_WRITE}, nil-}diff --git a/backend/api/oauth2/grant_test.go b/backend/api/oauth2/grant_test.godeleted file mode 100644index 254eb52914..0000000000--- a/backend/api/oauth2/grant_test.go+++ /dev/null@@ -1,652 +0,0 @@-package oauth2--import (- "context"- "crypto/sha256"- "encoding/base64"- "encoding/json"- "html"- "net/http"- "net/http/httptest"- "net/url"- "regexp"- "strings"- "testing"- "time"-- "github.com/bytebase/bytebase/backend/common/testcontainer"-- "github.com/labstack/echo/v5"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/api/auth"- "github.com/bytebase/bytebase/backend/api/mcp"- "github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/component/config"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/store"-- _ "github.com/bytebase/bytebase/backend/plugin/db/pg"-)--const (- testSecret = "test-secret"- testUserEmail = "[email protected]"- testWorkspace = "ws-test"- testClientID = "client-A"- testRedirectURI = "http://localhost/cb"- // 43-128 chars per RFC 7636.- testCodeVerifier = "kVerifier_kVerifier_kVerifier_kVerifier_kVer"- testResource = "https://bb.example.com/mcp"-)--func TestConsentWorkspaceRequiresSessionClaim(t *testing.T) {- s := &Service{profile: &config.Profile{}}-- workspaceID, failure := s.consentWorkspace(&sessionClaims{}, &store.OAuth2ClientMessage{})-- require.Empty(t, workspaceID)- require.Equal(t, &oauth2Failure{code: "access_denied", description: "session is missing workspace claim"}, failure)-}--// TestResourceScopeGrantLifecycle drives the whole consent → token → refresh-// path against a real store. The pure rule space lives in resource_test.go;-// what this pins is the wiring, which is where a resource/scope binding-// silently disappears: the code row must carry what was consented, the token-// endpoint must compare against it, and every refresh must re-issue the same-// grant rather than whatever the client asks for.-func TestResourceScopeGrantLifecycle(t *testing.T) {- ctx := context.Background()- db, st, _ := testcontainer.NewMetadataDB(t)-- _, err := db.ExecContext(ctx, `- INSERT INTO workspace (resource_id) VALUES ('ws-test');- INSERT INTO principal (name, email, password_hash) VALUES ('demo', '[email protected]', 'unused');- INSERT INTO oauth2_client (client_id, workspace, client_secret_hash, config)- VALUES ('client-A', NULL, 'unused-hash', '{"clientName":"test","redirectUris":["http://localhost/cb"],"grantTypes":["authorization_code","refresh_token"],"tokenEndpointAuthMethod":"none"}'::jsonb);- INSERT INTO setting (name, workspace, value) VALUES ('MCP', 'ws-test', '{"capability":"READ_WRITE"}');- `)- require.NoError(t, err)-- configured := newTestService(st, "https://bb.example.com")-- t.Run("authorization starts without an existing browser session", func(t *testing.T) {- entryService := newTestService(st, "")- entryService.profile.SaaS = true- challenge := sha256.Sum256([]byte(testCodeVerifier))- query := url.Values{- "response_type": {"code"},- "client_id": {testClientID},- "redirect_uri": {testRedirectURI},- "state": {"test-state"},- "code_challenge": {base64.RawURLEncoding.EncodeToString(challenge[:])},- "code_challenge_method": {"S256"},- "resource": {testResource},- }- req := httptest.NewRequest(http.MethodGet, "/api/oauth2/authorize?"+query.Encode(), nil)- rec := httptest.NewRecorder()- e := echo.New()- c := e.NewContext(req, rec)- require.NoError(t, entryService.handleAuthorizeGet(c))- response, err := echo.UnwrapResponse(c.Response())- require.NoError(t, err)- require.Equal(t, http.StatusFound, response.Status)-- location, err := url.Parse(response.Header().Get("Location"))- require.NoError(t, err)- require.Equal(t, "/oauth2/consent", location.Path)- require.Equal(t, testClientID, location.Query().Get("client_id"))- require.Equal(t, testResource, location.Query().Get("resource"))- })-- t.Run("configured external URL: matching resource and known scope are consented", func(t *testing.T) {- code := consentOK(t, configured, url.Values{"resource": {testResource}, "scope": {"mcp:read-only"}})-- got, err := st.GetOAuth2AuthorizationCode(ctx, testClientID, code)- require.NoError(t, err)- require.NotNil(t, got)- require.Equal(t, testResource, got.Config.Resource)- require.Equal(t, "mcp:read-only", got.Config.Scope)- })-- t.Run("resource is stored canonicalized, not as sent", func(t *testing.T) {- code := consentOK(t, configured, url.Values{"resource": {"HTTPS://BB.Example.com/mcp/"}})-- got, err := st.GetOAuth2AuthorizationCode(ctx, testClientID, code)- require.NoError(t, err)- require.Equal(t, testResource, got.Config.Resource,- "the token endpoint compares against this value, so it must already be canonical")- })-- t.Run("a bare origin is consented as the canonical MCP resource", func(t *testing.T) {- // Accepting the bare origin (our unsuffixed RFC 9728 document publishes- // it) must not put a second spelling in the grant: PR 3 binds the token- // audience to the stored value, and two spellings would mean two- // audiences to honor at /mcp for as long as any grant lives.- code := consentOK(t, configured, url.Values{"resource": {"https://bb.example.com"}})-- got, err := st.GetOAuth2AuthorizationCode(ctx, testClientID, code)- require.NoError(t, err)- require.Equal(t, testResource, got.Config.Resource)- })-- t.Run("no resource requested: the grant is bound to this server's MCP resource", func(t *testing.T) {- // Since PR 3 every new grant is resource-bound, because its tokens carry- // the resource as their audience: an unbound grant would mint tokens no- // audience check accepts once the legacy window closes, sending the- // client into a re-auth loop. A client that never sends `resource`- // (mandatory for MCP clients per RFC 8707, but not every DCR client is- // one) is defaulted to the only resource this server would accept anyway.- code := consentOK(t, configured, url.Values{})-- got, err := st.GetOAuth2AuthorizationCode(ctx, testClientID, code)- require.NoError(t, err)- require.Equal(t, testResource, got.Config.Resource)- require.Empty(t, got.Config.Scope, "only the resource is defaulted; scope stays exactly as requested")- })-- rejections := []struct {- name string- params url.Values- wantCode string- }{- {"resource on another host", url.Values{"resource": {"https://evil.example.com/mcp"}}, "invalid_target"},- {"resource parameter repeated", url.Values{"resource": {testResource, testResource}}, "invalid_target"},- {"unknown scope", url.Values{"scope": {"mcp:admin"}}, "invalid_scope"},- {"scope parameter repeated", url.Values{"scope": {"mcp:read-only", "mcp:read-write"}}, "invalid_scope"},- }- for _, tc := range rejections {- t.Run("consent rejected: "+tc.name, func(t *testing.T) {- redirect := consent(t, configured, tc.params)- require.Equal(t, tc.wantCode, redirect.Query().Get("error"))- })- }-- t.Run("default port resource is consented as the canonical resource", func(t *testing.T) {- code := consentOK(t, configured, url.Values{"resource": {"https://bb.example.com:443/mcp"}})- got, err := st.GetOAuth2AuthorizationCode(ctx, testClientID, code)- require.NoError(t, err)- require.Equal(t, testResource, got.Config.Resource)- })-- t.Run("no configured external URL: a resource request gets the actionable setup error", func(t *testing.T) {- _, err := st.UpsertSetting(ctx, &store.SettingMessage{- Name: storepb.SettingName_WORKSPACE_PROFILE,- Workspace: testWorkspace,- Value: &storepb.WorkspaceProfileSetting{},- })- require.NoError(t, err)-- unconfigured := newTestService(st, "")- redirect := consent(t, unconfigured, url.Values{"resource": {testResource}})- require.Equal(t, "server_error", redirect.Query().Get("error"))- description := redirect.Query().Get("error_description")- require.Contains(t, description, "external URL isn't setup yet")- })-- t.Run("no configured external URL: consent without a resource gets the setup error too", func(t *testing.T) {- // PR 2 let a resource-less request through on an unconfigured instance;- // PR 3 binds every grant, and the binding needs the trusted URL. Failing- // consent with the actionable setup pointer beats minting tokens that- // can only ever 401 at /mcp.- unconfigured := newTestService(st, "")- redirect := consent(t, unconfigured, url.Values{})- require.Equal(t, "server_error", redirect.Query().Get("error"))- require.Contains(t, redirect.Query().Get("error_description"), "external URL isn't setup yet")- })-- t.Run("the workspace setting is trusted too, not only the flag", func(t *testing.T) {- // Self-hosted instances configure the external URL in Settings rather- // than on the command line, so the DB tier has to validate resources- // exactly like the flag does.- _, err := st.UpsertSetting(ctx, &store.SettingMessage{- Name: storepb.SettingName_WORKSPACE_PROFILE,- Workspace: testWorkspace,- Value: &storepb.WorkspaceProfileSetting{ExternalUrl: "https://bb.example.com"},- })- require.NoError(t, err)- t.Cleanup(func() {- _, err := st.UpsertSetting(ctx, &store.SettingMessage{- Name: storepb.SettingName_WORKSPACE_PROFILE,- Workspace: testWorkspace,- Value: &storepb.WorkspaceProfileSetting{},- })- require.NoError(t, err)- })-- fromSetting := newTestService(st, "")- code := consentOK(t, fromSetting, url.Values{"resource": {testResource}})- got, err := st.GetOAuth2AuthorizationCode(ctx, testClientID, code)- require.NoError(t, err)- require.Equal(t, testResource, got.Config.Resource)-- redirect := consent(t, fromSetting, url.Values{"resource": {"https://evil.example.com/mcp"}})- require.Equal(t, "invalid_target", redirect.Query().Get("error"))- })-- t.Run("token exchange and refresh carry the grant forward unchanged", func(t *testing.T) {- code := consentOK(t, configured, url.Values{"resource": {testResource}, "scope": {"mcp:read-write"}})-- // Exchange, echoing the resource back the way RFC 8707 clients do.- first := tokenOK(t, configured, url.Values{- "grant_type": {"authorization_code"},- "code": {code},- "redirect_uri": {testRedirectURI},- "code_verifier": {testCodeVerifier},- "client_id": {testClientID},- "resource": {testResource},- })- require.Equal(t, "mcp:read-write", first.Scope, "the consented scope must be echoed (RFC 6749 §5.1)")- require.NotEmpty(t, first.RefreshToken)-- stored, err := st.GetOAuth2RefreshToken(ctx, testClientID, auth.HashToken(first.RefreshToken))- require.NoError(t, err)- require.NotNil(t, stored)- require.Equal(t, testResource, stored.Config.GetResource())- require.Equal(t, "mcp:read-write", stored.Config.GetScope())-- // A refresh that names a wider scope is refused, and refusal happens- // before consumption so the client can retry correctly.- widened := postToken(t, configured, url.Values{- "grant_type": {"refresh_token"},- "refresh_token": {first.RefreshToken},- "client_id": {testClientID},- "scope": {"mcp:read-only"},- })- require.Equal(t, http.StatusBadRequest, widened.Code)- require.Equal(t, "invalid_scope", errorCode(t, widened))-- mismatched := postToken(t, configured, url.Values{- "grant_type": {"refresh_token"},- "refresh_token": {first.RefreshToken},- "client_id": {testClientID},- "resource": {"https://evil.example.com/mcp"},- })- require.Equal(t, http.StatusBadRequest, mismatched.Code)- require.Equal(t, "invalid_target", errorCode(t, mismatched))-- // The rejected attempts left the grant usable, and the honest refresh- // re-issues the same resource and scope.- second := tokenOK(t, configured, url.Values{- "grant_type": {"refresh_token"},- "refresh_token": {first.RefreshToken},- "client_id": {testClientID},- })- require.Equal(t, "mcp:read-write", second.Scope)-- rotated, err := st.GetOAuth2RefreshToken(ctx, testClientID, auth.HashToken(second.RefreshToken))- require.NoError(t, err)- require.NotNil(t, rotated)- require.Equal(t, testResource, rotated.Config.GetResource())- require.Equal(t, "mcp:read-write", rotated.Config.GetScope())- })-- t.Run("a bare-origin grant refreshes as the canonical resource, named either way", func(t *testing.T) {- // Consent with the bare origin, then drive the whole exchange naming the- // bare origin at every step. The stored value stays the canonical MCP- // URI throughout, and the same normalization applies at the token end so- // the client is never told its own spelling is wrong.- code := consentOK(t, configured, url.Values{"resource": {"https://bb.example.com/"}})-- first := tokenOK(t, configured, url.Values{- "grant_type": {"authorization_code"},- "code": {code},- "redirect_uri": {testRedirectURI},- "code_verifier": {testCodeVerifier},- "client_id": {testClientID},- "resource": {"https://bb.example.com"},- })- stored, err := st.GetOAuth2RefreshToken(ctx, testClientID, auth.HashToken(first.RefreshToken))- require.NoError(t, err)- require.NotNil(t, stored)- require.Equal(t, testResource, stored.Config.GetResource())-- second := tokenOK(t, configured, url.Values{- "grant_type": {"refresh_token"},- "refresh_token": {first.RefreshToken},- "client_id": {testClientID},- "resource": {"https://bb.example.com"},- })- rotated, err := st.GetOAuth2RefreshToken(ctx, testClientID, auth.HashToken(second.RefreshToken))- require.NoError(t, err)- require.NotNil(t, rotated)- require.Equal(t, testResource, rotated.Config.GetResource(),- "the canonical form must survive rotation, not drift back to what the client sent")- })-- t.Run("legacy unbound grants are refused at the token endpoint with re-auth guidance", func(t *testing.T) {- // Rows created before 3.22.1 carry no resource in Config. PR 2 kept them- // redeemable as a deliberate interim; PR 3 retires them: tokens are now- // audience-bound to the grant's stored resource, so an unbound grant has- // nothing valid to mint. invalid_grant is what makes RFC-compliant- // clients discard the grant and rerun the OAuth flow, and the- // description points at the reauthorize tool as the in-band recovery.- challenge := sha256.Sum256([]byte(testCodeVerifier))- const legacyCode = "bb_code_legacy_unbound"- _, err := st.CreateOAuth2AuthorizationCode(ctx, &store.OAuth2AuthorizationCodeMessage{- Code: legacyCode,- ClientID: testClientID,- UserEmail: testUserEmail,- Workspace: testWorkspace,- Config: &storepb.OAuth2AuthorizationCodeConfig{- RedirectUri: testRedirectURI,- CodeChallenge: base64.RawURLEncoding.EncodeToString(challenge[:]),- CodeChallengeMethod: "S256",- },- ExpiresAt: time.Now().Add(10 * time.Minute),- })- require.NoError(t, err)-- exchanged := postToken(t, configured, url.Values{- "grant_type": {"authorization_code"},- "code": {legacyCode},- "redirect_uri": {testRedirectURI},- "code_verifier": {testCodeVerifier},- "client_id": {testClientID},- })- require.Equal(t, http.StatusBadRequest, exchanged.Code)- require.Equal(t, "invalid_grant", errorCode(t, exchanged))- require.Contains(t, errorDescription(t, exchanged), "re-authorize")- require.Contains(t, errorDescription(t, exchanged), "reauthorize tool")-- const legacyRefresh = "bb_refresh_legacy_unbound"- _, err = st.CreateOAuth2RefreshToken(ctx, &store.OAuth2RefreshTokenMessage{- TokenHash: auth.HashToken(legacyRefresh),- ClientID: testClientID,- UserEmail: testUserEmail,- Workspace: testWorkspace,- Config: &storepb.OAuth2RefreshTokenConfig{},- ExpiresAt: time.Now().Add(24 * time.Hour),- })- require.NoError(t, err)-- refreshed := postToken(t, configured, url.Values{- "grant_type": {"refresh_token"},- "refresh_token": {legacyRefresh},- "client_id": {testClientID},- })- require.Equal(t, http.StatusBadRequest, refreshed.Code)- require.Equal(t, "invalid_grant", errorCode(t, refreshed))- require.Contains(t, errorDescription(t, refreshed), "reauthorize tool")-- // Refusal happens before consumption: the row is left for the expiry- // sweep (or the user's reauthorize) rather than burned by a refresh- // that issued nothing.- row, err := st.GetOAuth2RefreshToken(ctx, testClientID, auth.HashToken(legacyRefresh))- require.NoError(t, err)- require.NotNil(t, row)-- // A bound grant with no scope is not legacy — only a missing resource- // marks the retired population.- boundCode := consentOK(t, configured, url.Values{"resource": {testResource}})- bound := tokenOK(t, configured, url.Values{- "grant_type": {"authorization_code"},- "code": {boundCode},- "redirect_uri": {testRedirectURI},- "code_verifier": {testCodeVerifier},- "client_id": {testClientID},- })- tokenOK(t, configured, url.Values{- "grant_type": {"refresh_token"},- "refresh_token": {bound.RefreshToken},- "client_id": {testClientID},- })- })-- t.Run("rotating the external URL kills outstanding tokens at /mcp until re-consent", func(t *testing.T) {- code := consentOK(t, configured, url.Values{"resource": {testResource}})- first := tokenOK(t, configured, url.Values{- "grant_type": {"authorization_code"},- "code": {code},- "redirect_uri": {testRedirectURI},- "code_verifier": {testCodeVerifier},- "client_id": {testClientID},- })- require.NotEqual(t, http.StatusUnauthorized, mcpStatus(t, st, "https://bb.example.com", first.AccessToken),- "a token bound to the live resource must be admitted at /mcp")-- // Rotate the trusted external URL. The middleware computes its expected- // audience from live config, so the outstanding token dies at use time- // with a clean 401 — never silently rebinds.- require.Equal(t, http.StatusUnauthorized, mcpStatus(t, st, "https://bb-new.example.com", first.AccessToken))-- // The token endpoint, by contrast, mints from the grant's STORED- // resource, never live config: refresh succeeds but hands back a token- // still bound to the consented (old) resource, equally dead at /mcp.- rotated := newTestService(st, "https://bb-new.example.com")- second := tokenOK(t, rotated, url.Values{- "grant_type": {"refresh_token"},- "refresh_token": {first.RefreshToken},- "client_id": {testClientID},- })- require.Equal(t, http.StatusUnauthorized, mcpStatus(t, st, "https://bb-new.example.com", second.AccessToken))-- // Recovery is a fresh consent under the new URL — here without a- // resource parameter, so this also proves the defaulted binding works- // end to end.- codeB := consentOK(t, rotated, url.Values{})- third := tokenOK(t, rotated, url.Values{- "grant_type": {"authorization_code"},- "code": {codeB},- "redirect_uri": {testRedirectURI},- "code_verifier": {testCodeVerifier},- "client_id": {testClientID},- })- require.NotEqual(t, http.StatusUnauthorized, mcpStatus(t, st, "https://bb-new.example.com", third.AccessToken))- })-- t.Run("the /mcp middleware trusts the workspace-setting external URL tier", func(t *testing.T) {- // The audience matrix and the rotation chain drive the --external-url- // flag tier only; the normal self-hosted shape configures the URL in- // Settings. A regression that silently drops the setting tier from the- // middleware would 401 every MCP request on such deployments while the- // consent-side tests stay green.- _, err := st.UpsertSetting(ctx, &store.SettingMessage{- Name: storepb.SettingName_WORKSPACE_PROFILE,- Workspace: testWorkspace,- Value: &storepb.WorkspaceProfileSetting{ExternalUrl: "https://bb.example.com"},- })- require.NoError(t, err)- t.Cleanup(func() {- _, err := st.UpsertSetting(ctx, &store.SettingMessage{- Name: storepb.SettingName_WORKSPACE_PROFILE,- Workspace: testWorkspace,- Value: &storepb.WorkspaceProfileSetting{},- })- require.NoError(t, err)- })-- fromSetting := newTestService(st, "")- code := consentOK(t, fromSetting, url.Values{"resource": {testResource}})- got := tokenOK(t, fromSetting, url.Values{- "grant_type": {"authorization_code"},- "code": {code},- "redirect_uri": {testRedirectURI},- "code_verifier": {testCodeVerifier},- "client_id": {testClientID},- })- require.NotEqual(t, http.StatusUnauthorized, mcpStatus(t, st, "", got.AccessToken),- "an empty flag must resolve the audience from the workspace setting")- })-- t.Run("an MCP token is refused on the general API but keeps serving /mcp", func(t *testing.T) {- // PR 4's private transport took /mcp tool traffic off the general API,- // so PR 5 retired the audit-only admission this subtest used to pin: an- // MCP token is refused outright on the general API — even for a- // principal with a valid workspace membership, which is why the policy- // patch stays — while /mcp keeps accepting the same token. A regression- // that re-admits it would make every MCP token a universal API bearer- // again.- _, err := st.PatchWorkspaceIamPolicy(ctx, &store.PatchIamPolicyMessage{- Workspace: testWorkspace,- Member: common.FormatUserEmail(testUserEmail),- Roles: []string{"roles/workspaceMember"},- })- require.NoError(t, err)-- code := consentOK(t, configured, url.Values{"resource": {testResource}})- got := tokenOK(t, configured, url.Values{- "grant_type": {"authorization_code"},- "code": {code},- "redirect_uri": {testRedirectURI},- "code_verifier": {testCodeVerifier},- "client_id": {testClientID},- })-- require.Equal(t, http.StatusOK, mcpStatus(t, st, "https://bb.example.com", got.AccessToken),- "the same token must keep working at /mcp")-- interceptor := auth.New(st, testSecret, nil, nil, &config.Profile{})- _, _, _, err = interceptor.AuthenticateToken(ctx, got.AccessToken)- require.Error(t, err, "an MCP token must not authenticate on the general API")- require.Contains(t, err.Error(), "only accepted at /mcp")- })-- t.Run("a requested scope set is consented as its maximum", func(t *testing.T) {- // Distinct from the repeated-parameter case above: one `scope` value- // naming both tiers, which is what the v1 bootstrap produces — the 401- // challenge advertises every mode pre-authentication, so clients ask for- // all of them. The set resolves to one stored mode; a multi-mode string- // must never reach the grant record.- code := consentOK(t, configured, url.Values{"scope": {"mcp:read-only mcp:read-write"}})-- got, err := st.GetOAuth2AuthorizationCode(ctx, testClientID, code)- require.NoError(t, err)- require.Equal(t, "mcp:read-write", got.Config.Scope)-- // And the client can keep sending the set it asked for: it normalizes to- // the same mode, so the exchange matches instead of failing.- first := tokenOK(t, configured, url.Values{- "grant_type": {"authorization_code"},- "code": {code},- "redirect_uri": {testRedirectURI},- "code_verifier": {testCodeVerifier},- "client_id": {testClientID},- "scope": {"mcp:read-only mcp:read-write"},- })- require.Equal(t, "mcp:read-write", first.Scope)-- stored, err := st.GetOAuth2RefreshToken(ctx, testClientID, auth.HashToken(first.RefreshToken))- require.NoError(t, err)- require.NotNil(t, stored)- require.Equal(t, "mcp:read-write", stored.Config.GetScope())- })-}--func newTestService(st *store.Store, externalURL string) *Service {- return NewService(st, &config.Profile{ExternalURL: externalURL}, testSecret)-}--// consent posts an approved consent form and returns the callback URL the-// handler redirects the browser to (carrying either `code` or `error`).-func consent(t *testing.T, s *Service, extra url.Values) *url.URL {- t.Helper()-- challenge := sha256.Sum256([]byte(testCodeVerifier))- form := url.Values{- "client_id": {testClientID},- "redirect_uri": {testRedirectURI},- "state": {"state-1"},- "code_challenge": {base64.RawURLEncoding.EncodeToString(challenge[:])},- "code_challenge_method": {"S256"},- "action": {"allow"},- }- for name, values := range extra {- form[name] = values- }-- sessionToken, err := auth.GenerateAccessToken(testUserEmail, testWorkspace, testSecret, time.Hour)- require.NoError(t, err)-- req := httptest.NewRequest(http.MethodPost, "/api/oauth2/authorize", strings.NewReader(form.Encode()))- req.Header.Set("Content-Type", "application/x-www-form-urlencoded")- req.Header.Set("Authorization", "Bearer "+sessionToken)- rec := httptest.NewRecorder()-- e := echo.New()- s.RegisterRoutes(e)- e.ServeHTTP(rec, req)- require.Equal(t, http.StatusOK, rec.Code)-- return redirectTargetOf(t, rec.Body.String())-}--// consentOK asserts the consent succeeded and returns the issued code.-func consentOK(t *testing.T, s *Service, extra url.Values) string {- t.Helper()- redirect := consent(t, s, extra)- require.Empty(t, redirect.Query().Get("error"), redirect.Query().Get("error_description"))- code := redirect.Query().Get("code")- require.NotEmpty(t, code)- return code-}--func postToken(t *testing.T, s *Service, form url.Values) *httptest.ResponseRecorder {- t.Helper()-- req := httptest.NewRequest(http.MethodPost, "/api/oauth2/token", strings.NewReader(form.Encode()))- req.Header.Set("Content-Type", "application/x-www-form-urlencoded")- rec := httptest.NewRecorder()-- e := echo.New()- s.RegisterRoutes(e)- e.ServeHTTP(rec, req)- return rec-}--func tokenOK(t *testing.T, s *Service, form url.Values) tokenResponse {- t.Helper()- rec := postToken(t, s, form)- require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())- var got tokenResponse- require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &got))- require.NotEmpty(t, got.AccessToken)- return got-}--func errorCode(t *testing.T, rec *httptest.ResponseRecorder) string {- t.Helper()- var body map[string]string- require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &body))- return body["error"]-}--func errorDescription(t *testing.T, rec *httptest.ResponseRecorder) string {- t.Helper()- var body map[string]string- require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &body))- return body["error_description"]-}--// mcpStatus posts an MCP initialize request to a freshly wired /mcp endpoint-// whose trusted external URL is externalURL, and returns the HTTP status. 401-// means the auth middleware refused the token; anything else means the token-// got through to the MCP handler.-func mcpStatus(t *testing.T, st *store.Store, externalURL, accessToken string) int {- t.Helper()- srv, err := mcp.NewServer(st, &config.Profile{ExternalURL: externalURL}, testSecret, nil)- require.NoError(t, err)- e := echo.New()- srv.RegisterRoutes(e)-- body := `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"test","version":"0"}}}`- req := httptest.NewRequest(http.MethodPost, "/mcp", strings.NewReader(body))- req.Header.Set("Content-Type", "application/json")- req.Header.Set("Accept", "application/json, text/event-stream")- req.Header.Set("Authorization", "Bearer "+accessToken)- rec := httptest.NewRecorder()- e.ServeHTTP(rec, req)- return rec.Code-}--// redirectTargetOf pulls the callback URL out of the meta-refresh page the-// authorize handler returns (it renders HTML rather than a 302 to sidestep CSP-// form-action restrictions).-func redirectTargetOf(t *testing.T, body string) *url.URL {- t.Helper()- matches := regexp.MustCompile(`content="0;url=([^"]+)"`).FindStringSubmatch(body)- require.Len(t, matches, 2, "expected a meta-refresh redirect, got: %s", body)- parsed, err := url.Parse(html.UnescapeString(matches[1]))- require.NoError(t, err)- return parsed-}diff --git a/backend/api/oauth2/main_test.go b/backend/api/oauth2/main_test.godeleted file mode 100644index 6dae3c8abd..0000000000--- a/backend/api/oauth2/main_test.go+++ /dev/null@@ -1,9 +0,0 @@-package oauth2--import (- "testing"-- "github.com/bytebase/bytebase/backend/common/testcontainer"-)--func TestMain(m *testing.M) { testcontainer.Main(m) }diff --git a/backend/api/oauth2/token_test.go b/backend/api/oauth2/token_test.goindex 95b74ca6d6..4891eb739a 100644--- a/backend/api/oauth2/token_test.go+++ b/backend/api/oauth2/token_test.go@@ -17,6 +17,8 @@ import ("github.com/bytebase/bytebase/backend/store")+const testResource = "https://bb.example.com/mcp"+// fakeWorkspaceResolver implements workspaceResolver for unit tests so we can// exercise resolveBoundWorkspace without standing up a real Postgres store.type fakeWorkspaceResolver struct {diff --git a/backend/api/v1/acl_test.go b/backend/api/v1/acl_test.goindex cce5de6bd0..47311177f0 100644--- a/backend/api/v1/acl_test.go+++ b/backend/api/v1/acl_test.go@@ -29,42 +29,13 @@ func TestResourceResolutionConnectError(t *testing.T) {})}-func TestACLCheckResourceResolutionStatus(t *testing.T) {- t.Parallel()- ctx, stores, _, _, _, _ := setupWorkspaceInstanceDescendantServiceTest(t)- interceptor := NewACLInterceptor(stores, "", nil, nil)-- for _, test := range []struct {- name string- request *v1pb.GetDatabaseRequest- want connect.Code- }{- {- name: "missing workspace database parent instance",- request: &v1pb.GetDatabaseRequest{Name: common.FormatDatabase("missing", "app")},- want: connect.CodeNotFound,- },- {- name: "malformed workspace database name",- request: &v1pb.GetDatabaseRequest{Name: "instances//databases/app"},- want: connect.CodeInvalidArgument,- },- } {- t.Run(test.name, func(t *testing.T) {- t.Parallel()- err := interceptor.doACLCheck(authenticatedACLContext(ctx), test.request, v1connect.DatabaseServiceGetDatabaseProcedure)- require.Equal(t, test.want, connect.CodeOf(err))- })- }-}-func TestACLCheckAuthenticatesBeforeResolvingResources(t *testing.T) {t.Parallel()- ctx, stores, _, _, _, _ := setupWorkspaceInstanceDescendantServiceTest(t)- interceptor := NewACLInterceptor(stores, "", nil, nil)+ ctx := unauthenticatedACLContext(context.WithValue(context.Background(), common.WorkspaceIDContextKey, "default"))+ interceptor := NewACLInterceptor(nil, "", nil, nil)err := interceptor.doACLCheck(- unauthenticatedACLContext(ctx),+ ctx,&v1pb.GetDatabaseRequest{Name: common.FormatDatabase("missing", "app")},v1connect.DatabaseServiceGetDatabaseProcedure,)@@ -238,46 +209,22 @@ func TestFindResourceResolver(t *testing.T) {}}-func TestResolveRawResource(t *testing.T) {- ctx, stores, instanceID, _, _, _ := setupProjectInstanceDescendantServiceTest(t)-- t.Run("rejects project instance in another project", func(t *testing.T) {- resource, err := resolveRawResource(ctx, stores, common.FormatProjectInstance("project-b", instanceID)+"/roles/role-a")- require.Error(t, err)- require.Equal(t, connect.CodeNotFound, connect.CodeOf(err))- require.Nil(t, resource)- })-- t.Run("rejects missing project database", func(t *testing.T) {- resource, err := resolveRawResource(ctx, stores, common.FormatProjectDatabase("project-a", instanceID, "missing")+"/schema")- require.Error(t, err)- require.Equal(t, connect.CodeNotFound, connect.CodeOf(err))- require.Nil(t, resource)- })-- t.Run("requires resolver identifiers", func(t *testing.T) {- for _, name := range []string{- "workspaces/",- "projects/",- "projects/project-a/instances/",- "projects/project-a/instances/instance-a/databases/",- "instances/",- "instances/instance-a/databases/",- } {- resource, err := resolveRawResource(ctx, nil, name)- require.Error(t, err, name)- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err), name)- require.Nil(t, resource, name)- }- })-}--func TestResolveRawResourceWorkspaceDatabaseUsesDatabaseProject(t *testing.T) {+func TestResolveRawResourceRequiresIdentifiers(t *testing.T) {t.Parallel()- ctx, stores, instanceID, databaseName, _, _ := setupWorkspaceInstanceDescendantServiceTest(t)- resource, err := resolveRawResource(ctx, stores, common.FormatDatabase(instanceID, databaseName)+"/revisions/1")- require.NoError(t, err)- require.Equal(t, &common.Resource{Type: common.ResourceTypeProject, ID: "project-a"}, resource)+ ctx := context.WithValue(context.Background(), common.WorkspaceIDContextKey, "default")+ for _, name := range []string{+ "workspaces/",+ "projects/",+ "projects/project-a/instances/",+ "projects/project-a/instances/instance-a/databases/",+ "instances/",+ "instances/instance-a/databases/",+ } {+ resource, err := resolveRawResource(ctx, nil, name)+ require.Error(t, err, name)+ require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err), name)+ require.Nil(t, resource, name)+ }}func TestPopulateRawResourcesUsesWorkspaceFallback(t *testing.T) {@@ -297,41 +244,32 @@ func TestPopulateRawResourcesUsesWorkspaceFallback(t *testing.T) {require.Empty(t, resources)}-func TestPopulateRawResourcesBatchSyncUsesDatabaseProject(t *testing.T) {- t.Parallel()- ctx, stores, instanceID, databaseName, _, _ := setupWorkspaceInstanceDescendantServiceTest(t)- resources, err := populateRawResources(- ctx,- stores,- &v1pb.BatchSyncDatabasesRequest{- Parent: "-",- Names: []string{common.FormatDatabase(instanceID, databaseName)},- },- v1connect.DatabaseServiceBatchSyncDatabasesProcedure,- )- require.NoError(t, err)- require.Equal(t, []*common.Resource{{Type: common.ResourceTypeProject, ID: "project-a"}}, resources)-}-+// TestPopulateRawResourcesAllowsDeletedSampleProjectInstanceProject pins that+// the lifecycle methods resolve a project without requiring it to be live —+// so that, for one, a sample request against a deleted project reaches the+// handler that answers "entitlement consumed" rather than dying as not found+// at the door. The lifecycle route needs no lookup at all for a project name,+// which the nil store proves.func TestPopulateRawResourcesAllowsDeletedSampleProjectInstanceProject(t *testing.T) {t.Parallel()- ctx, stores, projectID, _, _ := setupProjectInstanceLifecycleAPITest(t)- workspaceID := common.GetWorkspaceIDFromContext(ctx)- _, err := stores.GetDB().ExecContext(ctx, `- UPDATE project- SET deleted = TRUE- WHERE workspace = $1 AND resource_id = $2- `, workspaceID, projectID)- require.NoError(t, err)+ ctx := context.WithValue(context.Background(), common.WorkspaceIDContextKey, "default")+ for _, method := range []string{+ v1connect.InstanceServicePrepareSampleProjectInstanceProcedure,+ v1connect.InstanceServiceDeleteInstanceProcedure,+ v1connect.InstanceServiceUndeleteInstanceProcedure,+ } {+ require.True(t, allowsArchivedProjectResourceResolution(method), method)+ }+ require.False(t, allowsArchivedProjectResourceResolution(v1connect.InstanceServiceGetInstanceProcedure))resources, err := populateRawResources(ctx,- stores,- &v1pb.PrepareSampleProjectInstanceRequest{Parent: common.FormatProject(projectID)},+ nil,+ &v1pb.PrepareSampleProjectInstanceRequest{Parent: common.FormatProject("project-a")},v1connect.InstanceServicePrepareSampleProjectInstanceProcedure,)require.NoError(t, err)- require.Equal(t, []*common.Resource{{Type: common.ResourceTypeProject, ID: projectID}}, resources)+ require.Equal(t, []*common.Resource{{Type: common.ResourceTypeProject, ID: "project-a"}}, resources)}func TestGetResourceFromRequest(t *testing.T) {diff --git a/backend/api/v1/actuator_service.go b/backend/api/v1/actuator_service.goindex 4b8694e055..bc85a99dd4 100644--- a/backend/api/v1/actuator_service.go+++ b/backend/api/v1/actuator_service.go@@ -79,6 +79,23 @@ func (s *ActuatorService) GetActuatorInfo(return connect.NewResponse(info), nil}+// actuatorMCPSetting is the MCP policy actuator info discloses: the resolution+// the gate admitted this request under when there is one, else a fresh read.+// The consent page derives its states from this alone, so it is the backend+// receipt for what the page may show. A row this build cannot parse leaves the+// setting absent rather than refusing the whole bootstrap response — the policy+// page then withholds editing instead of showing a guessed ceiling. A ceiling+// nobody serves arrives as the stored number: a client that has no name for it+// is exactly the client that must not disclose it.+func actuatorMCPSetting(ctx context.Context, reader mcpSettingsReader, workspaceID string) *v1pb.MCPSetting {+ mcpSetting, err := mcpSettingsForCurrentWorkspace(ctx, reader, workspaceID)+ if err != nil {+ slog.Error("failed to read the MCP setting", slog.String("workspace", workspaceID), log.BBError(err))+ return nil+ }+ return convertToMCPSetting(mcpSetting)+}+func (s *ActuatorService) getServerInfo(ctx context.Context, workspaceID string) (*v1pb.ActuatorInfo, error) {serverInfo := v1pb.ActuatorInfo{Version: s.profile.Version,@@ -93,12 +110,7 @@ func (s *ActuatorService) getServerInfo(ctx context.Context, workspaceID string)if workspaceID != "" {serverInfo.Workspace = common.FormatWorkspace(workspaceID)- mcpSetting, err := mcpSettingsForCurrentWorkspace(ctx, s.store, workspaceID)- if err != nil {- slog.Error("failed to read the MCP setting", slog.String("workspace", workspaceID), log.BBError(err))- } else {- serverInfo.McpSetting = convertToMCPSetting(mcpSetting)- }+ serverInfo.McpSetting = actuatorMCPSetting(ctx, s.store, workspaceID)if s.sampleManager != nil {serverInfo.Sample.Available = s.sampleManager.CheckAvailable(ctx) == nilinstances, err := s.sampleManager.ListInstances(ctx, workspaceID)diff --git a/backend/api/v1/actuator_service_test.go b/backend/api/v1/actuator_service_test.goindex 1878cb47e4..6989954c51 100644--- a/backend/api/v1/actuator_service_test.go+++ b/backend/api/v1/actuator_service_test.go@@ -2,179 +2,73 @@ package v1import ("context"- "errors"- "slices""testing"- "time"- "github.com/bytebase/bytebase/backend/common/testcontainer"-- "connectrpc.com/connect"+ "github.com/pkg/errors""github.com/stretchr/testify/require"- "google.golang.org/protobuf/encoding/protojson"- "google.golang.org/protobuf/reflect/protoreflect"- "github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/component/config"- "github.com/bytebase/bytebase/backend/component/sample"- "github.com/bytebase/bytebase/backend/component/sample/saas"- "github.com/bytebase/bytebase/backend/component/sample/selfhost"- "github.com/bytebase/bytebase/backend/enterprise"storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"- "github.com/bytebase/bytebase/backend/store")-func TestAuthenticationInfoAndActuatorBoundary(t *testing.T) {- t.Parallel()- ctx := context.Background()- _, stores, pgURL := testcontainer.NewMetadataDB(t)-- licenseService, err := enterprise.NewLicenseService(common.ReleaseModeDev, stores, false, "")- require.NoError(t, err)- profile := &config.Profile{- Version: "9.9.9",- GitCommit: "sensitive-commit",- ExternalURL: "https://bytebase.example.com",- }- sampleManager, err := saas.NewManager(- stores,- pgURL,- nil,- sample.ManagerOptions{ReplicaID: "replica-a"},- )- require.NoError(t, err)- actuatorService := NewActuatorService(stores, profile, nil, licenseService, sampleManager)- authService := NewAuthService(stores, "test-secret", licenseService, profile, nil)-- publicResponse, err := authService.GetAuthenticationInfo(ctx, connect.NewRequest(&v1pb.GetAuthenticationInfoRequest{}))- require.NoError(t, err)-- var populated []string- publicResponse.Msg.ProtoReflect().Range(func(field protoreflect.FieldDescriptor, _ protoreflect.Value) bool {- populated = append(populated, string(field.Name()))- return true- })- slices.Sort(populated)- require.Equal(t, []string{"restriction"}, populated)- require.Empty(t, publicResponse.Msg.Workspace)-- profile.SaaS = true- saasPublicResponse, err := authService.GetAuthenticationInfo(ctx, connect.NewRequest(&v1pb.GetAuthenticationInfoRequest{}))- require.NoError(t, err)- require.True(t, saasPublicResponse.Msg.Restriction.DisallowSignup)- require.True(t, saasPublicResponse.Msg.Restriction.DisallowPasswordSignin)- profile.SaaS = false-- const workspaceID = "actuator-auth-boundary"- _, err = stores.CreateWorkspace(ctx, &store.WorkspaceMessage{- ResourceID: workspaceID,- Payload: &storepb.WorkspacePayload{Title: "Actuator auth boundary"},- }, "[email protected]")- require.NoError(t, err)-- publicResponse, err = authService.GetAuthenticationInfo(ctx, connect.NewRequest(&v1pb.GetAuthenticationInfoRequest{}))- require.NoError(t, err)- populated = nil- publicResponse.Msg.ProtoReflect().Range(func(field protoreflect.FieldDescriptor, _ protoreflect.Value) bool {- populated = append(populated, string(field.Name()))- return true- })- slices.Sort(populated)- require.Equal(t, []string{"restriction", "workspace"}, populated)- require.Equal(t, common.FormatWorkspace(workspaceID), publicResponse.Msg.Workspace)-- _, err = actuatorService.GetActuatorInfo(ctx, connect.NewRequest(&v1pb.GetActuatorInfoRequest{}))- require.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))-- user, err := stores.CreateUser(ctx, &store.UserMessage{- Email: "[email protected]",- Name: "Admin",- PasswordHash: "unused",- Profile: &storepb.UserProfile{},- })- require.NoError(t, err)- authenticatedCtx := context.WithValue(ctx, common.UserContextKey, user)- authenticatedCtx = context.WithValue(authenticatedCtx, common.WorkspaceIDContextKey, workspaceID)- actuatorService.sampleManager = nil- nonSaaSResponse, err := actuatorService.GetActuatorInfo(authenticatedCtx, connect.NewRequest(&v1pb.GetActuatorInfoRequest{}))- require.NoError(t, err)- require.False(t, nonSaaSResponse.Msg.Sample.Available)-- selfHostManager := selfhost.NewManager(stores, profile, nil, sample.ManagerOptions{ReplicaID: "replica-a"})- actuatorService.sampleManager = selfHostManager- profile.PgURL = pgURL- externalMetadataResponse, err := actuatorService.GetActuatorInfo(authenticatedCtx, connect.NewRequest(&v1pb.GetActuatorInfoRequest{}))- require.NoError(t, err)- require.False(t, externalMetadataResponse.Msg.Sample.Available)- profile.PgURL = ""- embeddedMetadataResponse, err := actuatorService.GetActuatorInfo(authenticatedCtx, connect.NewRequest(&v1pb.GetActuatorInfoRequest{}))- require.NoError(t, err)- require.True(t, embeddedMetadataResponse.Msg.Sample.Available)-- actuatorService.sampleManager = &sampleManagerStub{listInstancesErr: errors.New("failed to decode sample setup")}- degradedSampleResponse, err := actuatorService.GetActuatorInfo(authenticatedCtx, connect.NewRequest(&v1pb.GetActuatorInfoRequest{}))- require.NoError(t, err)- require.True(t, degradedSampleResponse.Msg.Sample.Available)- require.Empty(t, degradedSampleResponse.Msg.Sample.Instances)-- profile.SaaS = true- actuatorService.sampleManager = sampleManager-- availableResponse, err := actuatorService.GetActuatorInfo(authenticatedCtx, connect.NewRequest(&v1pb.GetActuatorInfoRequest{}))- require.NoError(t, err)- require.True(t, availableResponse.Msg.Sample.Available)+// mcpSettingRead is one answer from the MCP settings reader.+type mcpSettingRead struct {+ setting *storepb.MCPSetting+ err error+}- defaultProjectID, err := stores.GetDefaultProjectID(ctx, workspaceID)- require.NoError(t, err)- projectID := defaultProjectID- payload, err := protojson.Marshal(&storepb.SaaSSampleInstanceSetupPayload{- ProjectId: projectID,- InstanceId: "sample-instance",- Title: "Sample Project Instance",- DatabaseName: "sample-database",- RoleName: "sample-role",- })- require.NoError(t, err)- reservation, created, err := stores.ReserveSampleInstanceSetup(ctx, &store.SampleInstanceSetupMessage{- WorkspaceID: workspaceID,- ReplicaID: "replica-a",- Payload: payload,- })- require.NoError(t, err)- require.True(t, created)- expiresAt := time.Now().Add(7 * 24 * time.Hour).Truncate(time.Microsecond)- activatedAt := expiresAt.Add(-7 * 24 * time.Hour)- activated, err := stores.ActivateSampleInstanceSetup(ctx, workspaceID, reservation.ReplicaID, []string{projectID}, activatedAt, &expiresAt)- require.NoError(t, err)- require.True(t, activated)- privateResponse, err := actuatorService.GetActuatorInfo(authenticatedCtx, connect.NewRequest(&v1pb.GetActuatorInfoRequest{}))- require.NoError(t, err)- require.Equal(t, "9.9.9", privateResponse.Msg.Version)- require.Equal(t, "sensitive-commit", privateResponse.Msg.GitCommit)- require.Equal(t, common.FormatWorkspace(workspaceID), privateResponse.Msg.Workspace)- require.NotEmpty(t, privateResponse.Msg.DefaultProject)- require.True(t, privateResponse.Msg.Sample.Available)- require.Len(t, privateResponse.Msg.Sample.Instances, 1)- require.Equal(t, common.FormatProjectInstance(projectID, "sample-instance"), privateResponse.Msg.Sample.Instances[0].Instance)- require.True(t, expiresAt.Equal(privateResponse.Msg.Sample.Instances[0].ExpireTime.AsTime()))+func (r mcpSettingRead) GetMCPSettingsUncached(context.Context, string) (*storepb.MCPSetting, error) {+ return r.setting, r.err+}- cleanupNow := expiresAt.Add(time.Second)- cleanupResult, err := stores.WithLockedSampleInstanceSetupForCleanup(- ctx,- cleanupNow,- cleanupNow.Add(-time.Hour),- "",- func(ctx context.Context, tx *store.SampleInstanceSetupTx, _ *store.SampleInstanceSetupMessage) error {- return tx.MarkDeleted(ctx, cleanupNow)+// TestActuatorMCPSetting pins the three decisions in what actuator info+// discloses about the MCP policy: the masking toggle travels with the ceiling,+// an unreadable row leaves the setting absent instead of failing the shared+// bootstrap response, and the resolution the gate already made for this+// request wins over a second read.+func TestActuatorMCPSetting(t *testing.T) {+ ctx := context.Background()+ for _, tc := range []struct {+ name string+ read mcpSettingRead+ ctx context.Context+ want *v1pb.MCPSetting+ }{+ {+ // Every masking state is written in terms of this, so withholding it+ // leaves the table unusable.+ name: "the toggle reaches the response",+ read: mcpSettingRead{setting: &storepb.MCPSetting{Capability: storepb.MCPSetting_READ_ONLY, IgnoreMaskingExemptions: true}},+ ctx: ctx,+ want: &v1pb.MCPSetting{Capability: v1pb.MCPSetting_READ_ONLY, IgnoreMaskingExemptions: true},+ },+ {+ // Actuator info is a shared bootstrap response, so it remains+ // available and leaves the optional setting absent; the policy page+ // then withholds editing rather than showing a guessed ceiling.+ name: "a row that does not unmarshal leaves the setting absent",+ read: mcpSettingRead{err: errors.New("failed to unmarshal setting MCP")},+ ctx: ctx,+ want: nil,},- )- require.NoError(t, err)- require.True(t, cleanupResult.Found)- deletedResponse, err := actuatorService.GetActuatorInfo(authenticatedCtx, connect.NewRequest(&v1pb.GetActuatorInfoRequest{}))- require.NoError(t, err)- require.True(t, deletedResponse.Msg.Sample.Available)- require.Len(t, deletedResponse.Msg.Sample.Instances, 1)- require.Equal(t, common.FormatProjectInstance(projectID, "sample-instance"), deletedResponse.Msg.Sample.Instances[0].Instance)- require.Nil(t, deletedResponse.Msg.Sample.Instances[0].ExpireTime)+ {+ // The stored ceiling says DISABLED; the gate admitted this request+ // under READ_ONLY. Answering from the store would report a ceiling+ // the request was not admitted under.+ name: "the gate's resolution wins over a second read",+ read: mcpSettingRead{setting: &storepb.MCPSetting{Capability: storepb.MCPSetting_DISABLED}},+ ctx: withMCPSettings(ctx, &storepb.MCPSetting{Capability: storepb.MCPSetting_READ_ONLY, IgnoreMaskingExemptions: true}),+ want: &v1pb.MCPSetting{Capability: v1pb.MCPSetting_READ_ONLY, IgnoreMaskingExemptions: true},+ },+ } {+ t.Run(tc.name, func(t *testing.T) {+ got := actuatorMCPSetting(tc.ctx, tc.read, "ws-test")+ if tc.want == nil {+ require.Nil(t, got)+ return+ }+ require.Equal(t, tc.want.Capability, got.GetCapability())+ require.Equal(t, tc.want.IgnoreMaskingExemptions, got.GetIgnoreMaskingExemptions())+ })+ }}diff --git a/backend/api/v1/audit.go b/backend/api/v1/audit.goindex c38fa504bb..eb2ad8c67f 100644--- a/backend/api/v1/audit.go+++ b/backend/api/v1/audit.go@@ -40,8 +40,8 @@ type AuditInterceptor struct {secret stringprofile *config.Profile- // createAuditLogFunc, when set, replaces createAuditLog for streaming sends.- // Test-only seam that lets unit tests observe audit persistence ordering+ // createAuditLogFunc, when set, replaces createAuditLog. Test-only seam+ // that lets unit tests observe what an audited call writes, and when,// without a database.createAuditLogFunc func(context.Context, *auditEntry) error}@@ -111,7 +111,7 @@ func (in *AuditInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFuncpeerAddr: req.Peer().Addr,latency: latency,}- if err := in.createAuditLog(ctx, entry); err != nil {+ if err := in.writeAuditLog(ctx, entry); err != nil {slog.Warn("audit interceptor: failed to create audit log", log.BBError(err), slog.String("method", req.Spec().Procedure))}}@@ -177,11 +177,7 @@ func (c *auditConnectStreamingConn) Send(resp any) error {peerAddr: c.Peer().Addr,latency: time.Since(c.startTime),}- writeAuditLog := c.interceptor.createAuditLog- if c.interceptor.createAuditLogFunc != nil {- writeAuditLog = c.interceptor.createAuditLogFunc- }- if auditErr := writeAuditLog(c.ctx, entry); auditErr != nil {+ if auditErr := c.interceptor.writeAuditLog(c.ctx, entry); auditErr != nil {return auditErr}}@@ -208,7 +204,43 @@ type auditEntry struct {latency time.Duration}+// auditRow is one row an audited call leaves: the payload, under the+// workspace it belongs to.+type auditRow struct {+ workspaceID string+ payload *storepb.AuditLog+}++// writeAuditLog records an audited call, through the test seam when one is set.+func (in *AuditInterceptor) writeAuditLog(ctx context.Context, e *auditEntry) error {+ if in.createAuditLogFunc != nil {+ return in.createAuditLogFunc(ctx, e)+ }+ return in.createAuditLog(ctx, e)+}+func (in *AuditInterceptor) createAuditLog(ctx context.Context, e *auditEntry) error {+ rows, err := in.buildAuditRows(ctx, e)+ if err != nil {+ return err+ }+ createAuditLogCtx := context.WithoutCancel(ctx)+ for _, row := range rows {+ if err := in.store.CreateAuditLog(createAuditLogCtx, row.workspaceID, row.payload); err != nil {+ return err+ }+ // Log audit event to stdout using slog (if enabled)+ if in.profile.RuntimeEnableAuditLogStdout.Load() {+ common.LogAuditToStdout(ctx, row.payload)+ }+ }+ return nil+}++// buildAuditRows decides what an audited call leaves behind — which parents+// it is filed under, what each row carries, and whether it is recorded at+// all — without writing anything. createAuditLog writes what it returns.+func (in *AuditInterceptor) buildAuditRows(ctx context.Context, e *auditEntry) ([]auditRow, error) {// Skip audit logging for validate-only requests that SUCCEEDED. A dry run// that Bytebase accepted changed nothing, so recording it is noise — that// is the whole reason for the skip.@@ -230,7 +262,7 @@ func (in *AuditInterceptor) createAuditLog(ctx context.Context, e *auditEntry) e// refusals that happen to carry that code and silently drop every other// rejected attempt.if e.rerr == nil && isValidateOnlyRequest(e.request) {- return nil+ return nil, nil}requestString := marshalAuditPayload(e.request)@@ -249,7 +281,7 @@ func (in *AuditInterceptor) createAuditLog(ctx context.Context, e *auditEntry) eauthContextAny := ctx.Value(common.AuthContextKey)authContext, ok := authContextAny.(*common.AuthContext)if !ok {- return connect.NewError(connect.CodeInternal, errors.New("auth context not found"))+ return nil, connect.NewError(connect.CodeInternal, errors.New("auth context not found"))}requestMetadata := getRequestMetadataFromHeaders(e.headers, e.peerAddr)@@ -356,7 +388,7 @@ func (in *AuditInterceptor) createAuditLog(ctx context.Context, e *auditEntry) eauditStatus := redactAuditStatus(convertErrToStatus(e.rerr))mcpDelegation := mcpDelegationFromAuthContext(authContext)- createAuditLogCtx := context.WithoutCancel(ctx)+ var rows []auditRowfor _, ap := range parents {resource := getRequestResource(e.request, e.method)// For login requests, if resource is empty, try to get email from user context or MFA temp token.@@ -372,40 +404,34 @@ func (in *AuditInterceptor) createAuditLog(ctx context.Context, e *auditEntry) e}}- p := &storepb.AuditLog{- Parent: ap.parent,- Method: e.method,- Resource: resource,- Severity: storepb.AuditLog_INFO,- User: user,- Request: requestString,- Response: responseString,- Status: auditStatus,- Latency: durationpb.New(e.latency),- ServiceData: serviceData,- RequestMetadata: requestMetadata,- McpDelegation: mcpDelegation,- }// Resolve workspace for audit log.workspaceIDForAudit := ap.auditWorkspaceIDif workspaceIDForAudit == "" {- workspaceIDForAudit = common.GetWorkspaceIDFromContext(createAuditLogCtx)+ workspaceIDForAudit = common.GetWorkspaceIDFromContext(ctx)}if workspaceIDForAudit == "" {// Skip audit log if no workspace can be determined (e.g., unauthenticated request).continue}- if err := in.store.CreateAuditLog(createAuditLogCtx, workspaceIDForAudit, p); err != nil {- return err- }-- // Log audit event to stdout using slog (if enabled)- if in.profile.RuntimeEnableAuditLogStdout.Load() {- common.LogAuditToStdout(ctx, p)- }+ rows = append(rows, auditRow{+ workspaceID: workspaceIDForAudit,+ payload: &storepb.AuditLog{+ Parent: ap.parent,+ Method: e.method,+ Resource: resource,+ Severity: storepb.AuditLog_INFO,+ User: user,+ Request: requestString,+ Response: responseString,+ Status: auditStatus,+ Latency: durationpb.New(e.latency),+ ServiceData: serviceData,+ RequestMetadata: requestMetadata,+ McpDelegation: mcpDelegation,+ },+ })}-- return nil+ return rows, nil}// mcpDelegationFromAuthContext copies the delegated MCP grant state onto thediff --git a/backend/api/v1/audit_mcp_provenance_test.go b/backend/api/v1/audit_mcp_provenance_test.godeleted file mode 100644index e47de77947..0000000000--- a/backend/api/v1/audit_mcp_provenance_test.go+++ /dev/null@@ -1,263 +0,0 @@-package v1--import (- "context"- "fmt"- "testing"-- "github.com/bytebase/bytebase/backend/common/testcontainer"-- "connectrpc.com/connect"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/component/config"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"- "github.com/bytebase/bytebase/backend/store"-- _ "github.com/bytebase/bytebase/backend/plugin/db/pg"-)--const auditTestWorkspace = "ws-audit"--// specRequest overrides a request's Spec so the interceptors see a full-// procedure name — connect.NewRequest alone leaves it empty.-type specRequest struct {- connect.AnyRequest- procedure string-}--func (r *specRequest) Spec() connect.Spec {- return connect.Spec{Procedure: r.procedure}-}--// newAuditLiveStore boots a migrated store with one workspace, so-// CreateAuditLog has a real parent row to write under.-func newAuditLiveStore(t *testing.T) *store.Store {- ctx := context.Background()- db, st, _ := testcontainer.NewMetadataDB(t)-- _, err := db.ExecContext(ctx, fmt.Sprintf(- `INSERT INTO workspace (resource_id) VALUES ('%s')`, auditTestWorkspace))- require.NoError(t, err)-- return st-}--func auditTestUser() *store.UserMessage {- return &store.UserMessage{- ID: 1,- Email: "[email protected]",- Type: storepb.PrincipalType_END_USER,- }-}--func newAuditTestContext(authCtx *common.AuthContext) context.Context {- ctx := context.WithValue(context.Background(), common.AuthContextKey, authCtx)- ctx = context.WithValue(ctx, common.UserContextKey, auditTestUser())- ctx = context.WithValue(ctx, common.WorkspaceIDContextKey, auditTestWorkspace)- return ctx-}--// findRowsByCorrelation returns the workspace's audit rows whose MCP-// delegation carries the given correlation ID.-func findRowsByCorrelation(t *testing.T, st *store.Store, correlationID string) []*store.AuditLog {- t.Helper()- rows, err := st.SearchAuditLogs(context.Background(), &store.AuditLogFind{- Workspace: auditTestWorkspace,- })- require.NoError(t, err)- var matched []*store.AuditLog- for _, row := range rows {- if row.Payload.GetMcpDelegation().GetCorrelationId() == correlationID {- matched = append(matched, row)- }- }- return matched-}--// TestAuditRowCarriesMCPDelegationProvenance pins P1a PR 5b's provenance-// contract at the store level: an audited call that arrived with a delegated-// MCP credential (AuthContext.DelegatedGrant non-nil) writes its grant state-// verbatim onto the audit row, empty values preserved as empty; a public-chain-// call (nil grant) writes a row with no MCP fields at all.-func TestAuditRowCarriesMCPDelegationProvenance(t *testing.T) {- st := newAuditLiveStore(t)- in := NewAuditInterceptor(st, "test-secret", &config.Profile{})-- invoke := func(t *testing.T, grant *common.DelegatedGrant) {- t.Helper()- authCtx := &common.AuthContext{- Audit: true,- AuthMethod: common.AuthMethodIAM,- Resources: []*common.Resource{{Type: common.ResourceTypeWorkspace, ID: auditTestWorkspace}},- DelegatedGrant: grant,- }- next := func(_ context.Context, _ connect.AnyRequest) (connect.AnyResponse, error) {- return connect.NewResponse(&v1pb.QueryResponse{}), nil- }- req := &specRequest{- AnyRequest: connect.NewRequest(&v1pb.QueryRequest{Name: "instances/i/databases/d"}),- procedure: "/bytebase.v1.SQLService/Query",- }- _, err := in.WrapUnary(next)(newAuditTestContext(authCtx), req)- require.NoError(t, err)- }-- t.Run("a consented grant is stamped verbatim", func(t *testing.T) {- invoke(t, &common.DelegatedGrant{- Scope: "mcp:read-only",- Resource: "https://bb.example.com/mcp",- ClientID: "client-A",- CorrelationID: "corr-full",- })- rows := findRowsByCorrelation(t, st, "corr-full")- require.Len(t, rows, 1, "an audited internal-chain call must produce exactly one provenance-carrying row")- got := rows[0].Payload.GetMcpDelegation()- require.Equal(t, "mcp:read-only", got.GetScope())- require.Equal(t, "https://bb.example.com/mcp", got.GetResource())- require.Equal(t, "client-A", got.GetClientId())- })-- t.Run("a legacy empty grant still marks MCP origin, empty stays empty", func(t *testing.T) {- invoke(t, &common.DelegatedGrant{CorrelationID: "corr-legacy"})- rows := findRowsByCorrelation(t, st, "corr-legacy")- require.Len(t, rows, 1)- got := rows[0].Payload.GetMcpDelegation()- require.NotNil(t, got, "presence of the delegation message is the MCP-origin marker, even for empty legacy grants")- require.Empty(t, got.GetScope(), "an empty grant scope must be recorded empty, never resolved to a label")- require.Empty(t, got.GetResource())- require.Empty(t, got.GetClientId())- })-- t.Run("a public-chain row carries no MCP fields", func(t *testing.T) {- invoke(t, nil)- rows, err := st.SearchAuditLogs(context.Background(), &store.AuditLogFind{- Workspace: auditTestWorkspace,- })- require.NoError(t, err)- var publicRows []*store.AuditLog- for _, row := range rows {- if row.Payload.GetMcpDelegation() == nil {- publicRows = append(publicRows, row)- }- }- require.Len(t, publicRows, 1, "the nil-grant call must produce exactly one row without MCP provenance")- })-}--// TestAuditParentsDeduplicated pins that createAuditLog writes ONE row per-// distinct parent. Batch requests repeat the same project resource once per-// item, and since PR 5b routes ACL-denied internal-chain calls through the-// audit interceptor, an unprivileged caller reaches this fan-out — without-// dedup, a single denied batch call naming N items would write N identical-// rows.-func TestAuditParentsDeduplicated(t *testing.T) {- t.Parallel()- st := newAuditLiveStore(t)- in := NewAuditInterceptor(st, "test-secret", &config.Profile{})-- authCtx := &common.AuthContext{- Audit: true,- AuthMethod: common.AuthMethodIAM,- Resources: []*common.Resource{- {Type: common.ResourceTypeProject, ID: "proj-batch"},- {Type: common.ResourceTypeProject, ID: "proj-batch"},- {Type: common.ResourceTypeProject, ID: "proj-batch"},- {Type: common.ResourceTypeWorkspace, ID: auditTestWorkspace},- },- DelegatedGrant: &common.DelegatedGrant{CorrelationID: "corr-dedup"},- }- next := func(_ context.Context, _ connect.AnyRequest) (connect.AnyResponse, error) {- return connect.NewResponse(&v1pb.QueryResponse{}), nil- }- req := &specRequest{- AnyRequest: connect.NewRequest(&v1pb.QueryRequest{Name: "instances/i/databases/d"}),- procedure: "/bytebase.v1.SQLService/Query",- }- _, err := in.WrapUnary(next)(newAuditTestContext(authCtx), req)- require.NoError(t, err)-- rows := findRowsByCorrelation(t, st, "corr-dedup")- var parents []string- for _, row := range rows {- parents = append(parents, row.Payload.Parent)- }- require.ElementsMatch(t,- []string{common.FormatProject("proj-batch"), "workspaces/" + auditTestWorkspace},- parents,- "one audit row per DISTINCT parent — repeated batch resources must not multiply rows")-}--// TestInternalChainAuditRecordsACLDenial pins PR 5b's denial-audit mechanism:-// with the audit interceptor wrapped OUTSIDE the ACL interceptor (the internal-// MCP chain's order), an ACL denial produces an audit row carrying the-// provenance and the denied status; a method whose annotation opts out of-// auditing stays silent for permitted and denied calls alike.-func TestInternalChainAuditRecordsACLDenial(t *testing.T) {- st := newAuditLiveStore(t)- auditIn := NewAuditInterceptor(st, "test-secret", &config.Profile{})- aclIn := NewACLInterceptor(st, "test-secret", nil /* iamManager: unreached on these paths */, &config.Profile{})-- invoke := func(t *testing.T, audited bool, correlationID, resource string) (handlerReached bool, rerr error) {- t.Helper()- authCtx := &common.AuthContext{- Audit: audited,- AuthMethod: common.AuthMethodCustom,- DelegatedGrant: &common.DelegatedGrant{- Scope: "mcp:read-only",- Resource: "https://bb.example.com/mcp",- ClientID: "client-A",- CorrelationID: correlationID,- },- }- handler := func(_ context.Context, _ connect.AnyRequest) (connect.AnyResponse, error) {- handlerReached = true- return connect.NewResponse(&v1pb.IamPolicy{}), nil- }- // The internal chain's order: audit outside, ACL inside.- chain := auditIn.WrapUnary(aclIn.WrapUnary(handler))- req := &specRequest{- AnyRequest: connect.NewRequest(&v1pb.SetIamPolicyRequest{Resource: resource}),- procedure: "/bytebase.v1.WorkspaceService/SetIamPolicy",- }- _, rerr = chain(newAuditTestContext(authCtx), req)- return handlerReached, rerr- }-- t.Run("an ACL denial produces a provenance-carrying denied row", func(t *testing.T) {- handlerReached, err := invoke(t, true, "corr-denied", "workspaces/other-ws")- require.Error(t, err)- require.Equal(t, connect.CodePermissionDenied, connect.CodeOf(err))- require.False(t, handlerReached, "the denial must come from the ACL interceptor, not the handler")-- rows := findRowsByCorrelation(t, st, "corr-denied")- require.Len(t, rows, 1, "an ACL-denied internal-chain call must still produce an audit row")- row := rows[0].Payload- require.Equal(t, "workspaces/"+auditTestWorkspace, row.Parent,- "a denied cross-workspace attempt must be audited under the CALLER's workspace, never the foreign one it named")- require.Equal(t, "/bytebase.v1.WorkspaceService/SetIamPolicy", row.Method)- require.Equal(t, common.FormatUserEmail(auditTestUser().Email), row.User)- require.NotNil(t, row.Status, "the row must reflect the denial")- require.Equal(t, int32(connect.CodePermissionDenied), row.Status.Code)- require.Equal(t, "mcp:read-only", row.GetMcpDelegation().GetScope())- })-- t.Run("a method opted out of auditing stays silent for denials too", func(t *testing.T) {- _, err := invoke(t, false, "corr-optout", "workspaces/other-ws")- require.Error(t, err)- require.Empty(t, findRowsByCorrelation(t, st, "corr-optout"),- "audit opt-out must behave consistently for permitted and denied calls")- })-- t.Run("a permitted call is audited exactly once", func(t *testing.T) {- handlerReached, err := invoke(t, true, "corr-permitted", "workspaces/"+auditTestWorkspace)- require.NoError(t, err)- require.True(t, handlerReached)-- rows := findRowsByCorrelation(t, st, "corr-permitted")- require.Len(t, rows, 1)- require.Nil(t, rows[0].Payload.Status, "a permitted call keeps its success status")- })-}diff --git a/backend/api/v1/audit_redact_test.go b/backend/api/v1/audit_redact_test.goindex 66a77843f7..3039338e85 100644--- a/backend/api/v1/audit_redact_test.go+++ b/backend/api/v1/audit_redact_test.go@@ -23,6 +23,7 @@ import ("google.golang.org/protobuf/types/known/anypb""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/testcontainer""github.com/bytebase/bytebase/backend/component/config"storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"@@ -591,7 +592,6 @@ func TestAuditRedactsPackedAny(t *testing.T) {func TestStreamingAuditRedactsRows(t *testing.T) {t.Parallel()st := newAuditLiveStore(t)- t.Cleanup(func() { require.NoError(t, st.Close()) })interceptor := NewAuditInterceptor(st, "test-secret", &config.Profile{})handler := interceptor.WrapStreamingHandler(func(_ context.Context, conn connect.StreamingHandlerConn) error {@@ -620,12 +620,24 @@ func TestStreamingAuditRedactsRows(t *testing.T) {rows, err := st.SearchAuditLogs(ctx, &store.AuditLogFind{})require.NoError(t, err)require.Len(t, rows, 1)- require.NotContains(t, rows[0].Payload.Response, secretSentinel, "an admin-mode result row reached the audit log")- require.Contains(t, rows[0].Payload.Response, "card_number", "the column names and the statement are the point of the row")- require.Contains(t, rows[0].Payload.Response, `"rowsCount":"1"`,+ response := rows[0].Payload.Response+ require.NotContains(t, response, secretSentinel, "an admin-mode result row reached the audit log")+ require.Contains(t, response, "card_number", "the column names and the statement are the point of the row")+ require.Contains(t, response, `"rowsCount":"1"`,"how many rows the query returned survives; only the rows themselves are dropped")}+// newAuditLiveStore is a real metadata store holding the audit test+// workspace, for the one test that exercises the interceptor's write half.+func newAuditLiveStore(t *testing.T) *store.Store {+ t.Helper()+ db, st, _ := testcontainer.NewMetadataDB(t)+ _, err := db.ExecContext(context.Background(), fmt.Sprintf(+ `INSERT INTO workspace (resource_id) VALUES ('%s')`, auditTestWorkspace))+ require.NoError(t, err)+ return st+}+// ---- Guards on the fixtures the sweep builds for itself ------------------// TestNoAnnotationOutsideBytebaseProtos is what makes the fill's well-known-typediff --git a/backend/api/v1/audit_test.go b/backend/api/v1/audit_test.goindex 92b93c770a..5a9c27afeb 100644--- a/backend/api/v1/audit_test.go+++ b/backend/api/v1/audit_test.go@@ -2,15 +2,18 @@ package v1import ("context"- "errors"+ "encoding/base64""net/http"+ "strings""testing""connectrpc.com/connect"+ "github.com/pkg/errors""github.com/stretchr/testify/require""github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/component/config"+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1""github.com/bytebase/bytebase/backend/generated-go/v1/v1connect""github.com/bytebase/bytebase/backend/store"@@ -19,11 +22,9 @@ import (func TestFailedLoginWithoutWorkspaceIsSkipped(t *testing.T) {t.Parallel()ctx := context.WithValue(context.Background(), common.AuthContextKey, &common.AuthContext{Audit: true})- st := newAuditLiveStore(t)- t.Cleanup(func() { require.NoError(t, st.Close()) })- in := NewAuditInterceptor(st, "test-secret", &config.Profile{})+ in := NewAuditInterceptor(nil, "test-secret", &config.Profile{})- err := in.createAuditLog(ctx, &auditEntry{+ rows, err := in.buildAuditRows(ctx, &auditEntry{request: &v1pb.LoginRequest{Email: " [email protected] ",Password: "wrong-password",@@ -35,20 +36,15 @@ func TestFailedLoginWithoutWorkspaceIsSkipped(t *testing.T) {),})require.NoError(t, err)-- rows, err := st.SearchAuditLogs(ctx, &store.AuditLogFind{})- require.NoError(t, err)require.Empty(t, rows)}func TestFailedLoginWithHandlerWorkspaceCreatesSingleAuditRow(t *testing.T) {t.Parallel()ctx := context.WithValue(context.Background(), common.AuthContextKey, &common.AuthContext{Audit: true})- st := newAuditLiveStore(t)- t.Cleanup(func() { require.NoError(t, st.Close()) })- in := NewAuditInterceptor(st, "test-secret", &config.Profile{})+ in := NewAuditInterceptor(nil, "test-secret", &config.Profile{})- err := in.createAuditLog(ctx, &auditEntry{+ rows, err := in.buildAuditRows(ctx, &auditEntry{request: &v1pb.LoginRequest{Email: " [email protected] ",Password: "wrong-password",@@ -61,13 +57,10 @@ func TestFailedLoginWithHandlerWorkspaceCreatesSingleAuditRow(t *testing.T) {),})require.NoError(t, err)-- rows, err := st.SearchAuditLogs(ctx, &store.AuditLogFind{})- require.NoError(t, err)require.Len(t, rows, 1)- require.Equal(t, auditTestWorkspace, rows[0].Workspace)- require.Equal(t, common.FormatWorkspace(auditTestWorkspace), rows[0].Payload.GetParent())- require.Equal(t, "[email protected]", rows[0].Payload.GetResource())+ require.Equal(t, auditTestWorkspace, rows[0].workspaceID)+ require.Equal(t, common.FormatWorkspace(auditTestWorkspace), rows[0].payload.GetParent())+ require.Equal(t, "[email protected]", rows[0].payload.GetResource())}// TestStreamingAuditPersistedBeforeSend pins the streaming audit contract: a@@ -219,3 +212,365 @@ func TestLifecycleAuditResource(t *testing.T) {})}}++const auditTestWorkspace = "ws-audit"++// specRequest overrides a request's Spec so the interceptors see a full+// procedure name — connect.NewRequest alone leaves it empty.+type specRequest struct {+ connect.AnyRequest+ procedure string+}++func (r *specRequest) Spec() connect.Spec {+ return connect.Spec{Procedure: r.procedure}+}++// newRecordingAuditInterceptor builds the interceptor with no store and+// captures the rows each audited call would have written, so a test asserts on+// what the interceptor decided rather than on a database. That the rows reach+// the audit page on a live server is backend/tests' TestMCPAuditProvenance.+func newRecordingAuditInterceptor() (*AuditInterceptor, *[]auditRow) {+ in := NewAuditInterceptor(nil, "test-secret", &config.Profile{})+ rows := &[]auditRow{}+ in.createAuditLogFunc = func(ctx context.Context, e *auditEntry) error {+ built, err := in.buildAuditRows(ctx, e)+ if err != nil {+ return err+ }+ *rows = append(*rows, built...)+ return nil+ }+ return in, rows+}++func auditTestUser() *store.UserMessage {+ return &store.UserMessage{+ ID: 1,+ Email: "[email protected]",+ Type: storepb.PrincipalType_END_USER,+ }+}++func newAuditTestContext(authCtx *common.AuthContext) context.Context {+ ctx := context.WithValue(context.Background(), common.AuthContextKey, authCtx)+ ctx = context.WithValue(ctx, common.UserContextKey, auditTestUser())+ ctx = context.WithValue(ctx, common.WorkspaceIDContextKey, auditTestWorkspace)+ return ctx+}++// rowsByCorrelation returns the captured rows whose MCP delegation carries the+// given correlation ID.+func rowsByCorrelation(rows []auditRow, correlationID string) []*storepb.AuditLog {+ var matched []*storepb.AuditLog+ for _, row := range rows {+ if row.payload.GetMcpDelegation().GetCorrelationId() == correlationID {+ matched = append(matched, row.payload)+ }+ }+ return matched+}++// TestAuditRowCarriesMCPDelegationProvenance pins P1a PR 5b's provenance+// contract: an audited call that arrived with a delegated MCP credential+// (AuthContext.DelegatedGrant non-nil) writes its grant state verbatim onto the+// audit row, empty values preserved as empty; a public-chain call (nil grant)+// writes a row with no MCP fields at all.+func TestAuditRowCarriesMCPDelegationProvenance(t *testing.T) {+ in, rows := newRecordingAuditInterceptor()++ invoke := func(t *testing.T, grant *common.DelegatedGrant) {+ t.Helper()+ authCtx := &common.AuthContext{+ Audit: true,+ AuthMethod: common.AuthMethodIAM,+ Resources: []*common.Resource{{Type: common.ResourceTypeWorkspace, ID: auditTestWorkspace}},+ DelegatedGrant: grant,+ }+ next := func(_ context.Context, _ connect.AnyRequest) (connect.AnyResponse, error) {+ return connect.NewResponse(&v1pb.QueryResponse{}), nil+ }+ req := &specRequest{+ AnyRequest: connect.NewRequest(&v1pb.QueryRequest{Name: "instances/i/databases/d"}),+ procedure: "/bytebase.v1.SQLService/Query",+ }+ _, err := in.WrapUnary(next)(newAuditTestContext(authCtx), req)+ require.NoError(t, err)+ }++ t.Run("a consented grant is stamped verbatim", func(t *testing.T) {+ invoke(t, &common.DelegatedGrant{+ Scope: "mcp:read-only",+ Resource: "https://bb.example.com/mcp",+ ClientID: "client-A",+ CorrelationID: "corr-full",+ })+ matched := rowsByCorrelation(*rows, "corr-full")+ require.Len(t, matched, 1, "an audited internal-chain call must produce exactly one provenance-carrying row")+ got := matched[0].GetMcpDelegation()+ require.Equal(t, "mcp:read-only", got.GetScope())+ require.Equal(t, "https://bb.example.com/mcp", got.GetResource())+ require.Equal(t, "client-A", got.GetClientId())+ })++ t.Run("a legacy empty grant still marks MCP origin, empty stays empty", func(t *testing.T) {+ invoke(t, &common.DelegatedGrant{CorrelationID: "corr-legacy"})+ matched := rowsByCorrelation(*rows, "corr-legacy")+ require.Len(t, matched, 1)+ got := matched[0].GetMcpDelegation()+ require.NotNil(t, got, "presence of the delegation message is the MCP-origin marker, even for empty legacy grants")+ require.Empty(t, got.GetScope(), "an empty grant scope must be recorded empty, never resolved to a label")+ require.Empty(t, got.GetResource())+ require.Empty(t, got.GetClientId())+ })++ t.Run("a public-chain row carries no MCP fields", func(t *testing.T) {+ invoke(t, nil)+ var publicRows int+ for _, row := range *rows {+ if row.payload.GetMcpDelegation() == nil {+ publicRows+++ }+ }+ require.Equal(t, 1, publicRows, "the nil-grant call must produce exactly one row without MCP provenance")+ })+}++// TestAuditParentsDeduplicated pins that an audited call writes ONE row per+// distinct parent. Batch requests repeat the same project resource once per+// item, and since PR 5b routes ACL-denied internal-chain calls through the+// audit interceptor, an unprivileged caller reaches this fan-out — without+// dedup, a single denied batch call naming N items would write N identical+// rows.+func TestAuditParentsDeduplicated(t *testing.T) {+ t.Parallel()+ in, rows := newRecordingAuditInterceptor()++ authCtx := &common.AuthContext{+ Audit: true,+ AuthMethod: common.AuthMethodIAM,+ Resources: []*common.Resource{+ {Type: common.ResourceTypeProject, ID: "proj-batch"},+ {Type: common.ResourceTypeProject, ID: "proj-batch"},+ {Type: common.ResourceTypeProject, ID: "proj-batch"},+ {Type: common.ResourceTypeWorkspace, ID: auditTestWorkspace},+ },+ DelegatedGrant: &common.DelegatedGrant{CorrelationID: "corr-dedup"},+ }+ next := func(_ context.Context, _ connect.AnyRequest) (connect.AnyResponse, error) {+ return connect.NewResponse(&v1pb.QueryResponse{}), nil+ }+ req := &specRequest{+ AnyRequest: connect.NewRequest(&v1pb.QueryRequest{Name: "instances/i/databases/d"}),+ procedure: "/bytebase.v1.SQLService/Query",+ }+ _, err := in.WrapUnary(next)(newAuditTestContext(authCtx), req)+ require.NoError(t, err)++ var parents []string+ for _, row := range rowsByCorrelation(*rows, "corr-dedup") {+ parents = append(parents, row.Parent)+ }+ require.ElementsMatch(t,+ []string{common.FormatProject("proj-batch"), "workspaces/" + auditTestWorkspace},+ parents,+ "one audit row per DISTINCT parent — repeated batch resources must not multiply rows")+}++// TestInternalChainAuditRecordsACLDenial pins PR 5b's denial-audit mechanism:+// with the audit interceptor wrapped OUTSIDE the ACL interceptor (the internal+// MCP chain's order), an ACL denial produces an audit row carrying the+// provenance and the denied status; a method whose annotation opts out of+// auditing stays silent for permitted and denied calls alike. The workspace+// mismatch is decided on the request alone, so no store is needed.+func TestInternalChainAuditRecordsACLDenial(t *testing.T) {+ auditIn, rows := newRecordingAuditInterceptor()+ aclIn := NewACLInterceptor(nil, "test-secret", nil /* iamManager: unreached on these paths */, &config.Profile{})++ invoke := func(t *testing.T, audited bool, correlationID, resource string) (handlerReached bool, rerr error) {+ t.Helper()+ authCtx := &common.AuthContext{+ Audit: audited,+ AuthMethod: common.AuthMethodCustom,+ DelegatedGrant: &common.DelegatedGrant{+ Scope: "mcp:read-only",+ Resource: "https://bb.example.com/mcp",+ ClientID: "client-A",+ CorrelationID: correlationID,+ },+ }+ handler := func(_ context.Context, _ connect.AnyRequest) (connect.AnyResponse, error) {+ handlerReached = true+ return connect.NewResponse(&v1pb.IamPolicy{}), nil+ }+ // The internal chain's order: audit outside, ACL inside.+ chain := auditIn.WrapUnary(aclIn.WrapUnary(handler))+ req := &specRequest{+ AnyRequest: connect.NewRequest(&v1pb.SetIamPolicyRequest{Resource: resource}),+ procedure: "/bytebase.v1.WorkspaceService/SetIamPolicy",+ }+ _, rerr = chain(newAuditTestContext(authCtx), req)+ return handlerReached, rerr+ }++ t.Run("an ACL denial produces a provenance-carrying denied row", func(t *testing.T) {+ handlerReached, err := invoke(t, true, "corr-denied", "workspaces/other-ws")+ require.Error(t, err)+ require.Equal(t, connect.CodePermissionDenied, connect.CodeOf(err))+ require.False(t, handlerReached, "the denial must come from the ACL interceptor, not the handler")++ matched := rowsByCorrelation(*rows, "corr-denied")+ require.Len(t, matched, 1, "an ACL-denied internal-chain call must still produce an audit row")+ row := matched[0]+ require.Equal(t, "workspaces/"+auditTestWorkspace, row.Parent,+ "a denied cross-workspace attempt must be audited under the CALLER's workspace, never the foreign one it named")+ require.Equal(t, "/bytebase.v1.WorkspaceService/SetIamPolicy", row.Method)+ require.Equal(t, common.FormatUserEmail(auditTestUser().Email), row.User)+ require.NotNil(t, row.Status, "the row must reflect the denial")+ require.Equal(t, int32(connect.CodePermissionDenied), row.Status.Code)+ require.Equal(t, "mcp:read-only", row.GetMcpDelegation().GetScope())+ })++ t.Run("a method opted out of auditing stays silent for denials too", func(t *testing.T) {+ _, err := invoke(t, false, "corr-optout", "workspaces/other-ws")+ require.Error(t, err)+ require.Empty(t, rowsByCorrelation(*rows, "corr-optout"),+ "audit opt-out must behave consistently for permitted and denied calls")+ })++ t.Run("a permitted call is audited exactly once", func(t *testing.T) {+ handlerReached, err := invoke(t, true, "corr-permitted", "workspaces/"+auditTestWorkspace)+ require.NoError(t, err)+ require.True(t, handlerReached)++ matched := rowsByCorrelation(*rows, "corr-permitted")+ require.Len(t, matched, 1)+ require.Nil(t, matched[0].Status, "a permitted call keeps its success status")+ })+}++// TestValidateOnlyAuditSkipAppliesOnlyToSuccess pins the boundary of the+// validate-only skip in createAuditLog.+//+// The skip exists so a dry run — which changes nothing — does not spam the+// audit log. It said nothing about the outcome, so it also swallowed every+// FAILED attempt whose request carries validate_only. That handed an agent an+// unlogged attempt at any forbidden method taking one of those six requests:+// two identical denials of the same method, in the same session, differing only+// in that flag, produced one row between them.+//+// The three cases below are the whole rule: outcome decides, not the flag+// alone.+func TestValidateOnlyAuditSkipAppliesOnlyToSuccess(t *testing.T) {+ in, captured := newRecordingAuditInterceptor()++ // A retarget with the stored password left to ride along — the shape the+ // MCP class refuses. The secrets are here so the redaction assertion below+ // has something to catch.+ const storedPassword = "stored-db-secret"+ const storedKeytab = "stored-keytab-bytes"+ retargetRequest := func(validateOnly bool) *v1pb.UpdateDataSourceRequest {+ return &v1pb.UpdateDataSourceRequest{+ Name: "instances/probe",+ DataSource: &v1pb.DataSource{+ Id: "admin-ds",+ Host: "attacker.example.com",+ Password: storedPassword,+ SaslConfig: &v1pb.SASLConfig{+ Mechanism: &v1pb.SASLConfig_KrbConfig{+ KrbConfig: &v1pb.KerberosConfig{Keytab: []byte(storedKeytab)},+ },+ },+ },+ ValidateOnly: validateOnly,+ }+ }++ invoke := func(t *testing.T, correlationID string, request *v1pb.UpdateDataSourceRequest, rerr error) {+ t.Helper()+ authCtx := &common.AuthContext{+ Audit: true,+ AuthMethod: common.AuthMethodIAM,+ Resources: []*common.Resource{{Type: common.ResourceTypeWorkspace, ID: auditTestWorkspace}},+ DelegatedGrant: &common.DelegatedGrant{CorrelationID: correlationID},+ }+ next := func(_ context.Context, _ connect.AnyRequest) (connect.AnyResponse, error) {+ if rerr != nil {+ return nil, rerr+ }+ return connect.NewResponse(&v1pb.Instance{Name: "instances/probe"}), nil+ }+ req := &specRequest{+ AnyRequest: connect.NewRequest(request),+ procedure: "/bytebase.v1.InstanceService/UpdateDataSource",+ }+ _, err := in.WrapUnary(next)(newAuditTestContext(authCtx), req)+ if rerr == nil {+ require.NoError(t, err)+ } else {+ require.Error(t, err)+ }+ }++ t.Run("a denied validate-only call is recorded", func(t *testing.T) {+ denial := connect.NewError(connect.CodePermissionDenied,+ errors.New("InstanceService/UpdateDataSource is not available to MCP sessions"))+ invoke(t, "corr-validate-only-denied", retargetRequest(true), denial)++ rows := rowsByCorrelation(*captured, "corr-validate-only-denied")+ require.Len(t, rows, 1,+ "a refused attempt must be auditable whether or not validate_only was set — "++ "otherwise the flag is a switch that turns off the record")+ require.Equal(t, int32(connect.CodePermissionDenied), rows[0].GetStatus().GetCode(),+ "the row must carry the denial, not a blank status")++ // The newly-logged row goes through the same marshalAuditPayload+ // redaction as every other UpdateDataSource row — that is why+ // narrowing the skip exposes no field the plain path did not already+ // write. It pins that the path runs on a row the skip used to swallow;+ // which fields the redaction covers is the annotation's job, pinned by+ // TestAuditRedactionCoversEveryAnnotatedField.+ request := rows[0].GetRequest()+ require.NotEmpty(t, request)+ require.NotContains(t, request, storedPassword,+ "the request payload must be redacted before it reaches the audit row")+ // The row is protojson, which renders a bytes field base64, so the+ // keytab has to be looked for in that form — searching for the ASCII+ // literal would pass even with keytab redaction deleted outright.+ require.NotContains(t, request, base64.StdEncoding.EncodeToString([]byte(storedKeytab)),+ "the keytab reaches the row base64-encoded, and must be masked before it does")+ require.Contains(t, request, "attacker.example.com",+ "the destination is what an operator reads the row for — it must survive redaction")+ })++ t.Run("a succeeding validate-only call stays silent", func(t *testing.T) {+ invoke(t, "corr-validate-only-ok", retargetRequest(true), nil)++ require.Empty(t, rowsByCorrelation(*captured, "corr-validate-only-ok"),+ "a dry run that succeeded changed nothing — the original reason for the skip")+ })++ t.Run("a failed validate-only call is recorded, denial or not", func(t *testing.T) {+ // The rule is any failure, not any refusal, and this is the case that+ // pins it: a validate-only connection test that could not reach the+ // host. It is also the bulk of what the change adds, since the+ // instance form dials before every save.+ failedDial := connect.NewError(connect.CodeInvalidArgument,+ errors.New("failed to connect to attacker.example.com: connection refused"))+ invoke(t, "corr-validate-only-dial-failed", retargetRequest(true), failedDial)++ rows := rowsByCorrelation(*captured, "corr-validate-only-dial-failed")+ require.Len(t, rows, 1,+ "keying on a denial code would drop every other rejected attempt — the hole this change closes")+ require.Equal(t, int32(connect.CodeInvalidArgument), rows[0].GetStatus().GetCode())+ })++ t.Run("a denied ordinary call is recorded", func(t *testing.T) {+ denial := connect.NewError(connect.CodePermissionDenied, errors.New("denied"))+ invoke(t, "corr-plain-denied", retargetRequest(false), denial)++ rows := rowsByCorrelation(*captured, "corr-plain-denied")+ require.Len(t, rows, 1, "control: the flag is the only difference between this and the first case")+ require.True(t, strings.Contains(rows[0].GetMethod(), "UpdateDataSource"))+ })+}diff --git a/backend/api/v1/audit_validate_only_test.go b/backend/api/v1/audit_validate_only_test.godeleted file mode 100644index 53f482fb7d..0000000000--- a/backend/api/v1/audit_validate_only_test.go+++ /dev/null@@ -1,143 +0,0 @@-package v1--import (- "context"- "encoding/base64"- "strings"- "testing"-- "connectrpc.com/connect"- "github.com/pkg/errors"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/component/config"- v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"-)--// TestValidateOnlyAuditSkipAppliesOnlyToSuccess pins the boundary of the-// validate-only skip in createAuditLog.-//-// The skip exists so a dry run — which changes nothing — does not spam the-// audit log. It said nothing about the outcome, so it also swallowed every-// FAILED attempt whose request carries validate_only. That handed an agent an-// unlogged attempt at any forbidden method taking one of those six requests:-// two identical denials of the same method, in the same session, differing only-// in that flag, produced one row between them.-//-// The three cases below are the whole rule: outcome decides, not the flag-// alone.-func TestValidateOnlyAuditSkipAppliesOnlyToSuccess(t *testing.T) {- st := newAuditLiveStore(t)- in := NewAuditInterceptor(st, "test-secret", &config.Profile{})-- // A retarget with the stored password left to ride along — the shape the- // MCP class refuses. The secrets are here so the redaction assertion below- // has something to catch.- const storedPassword = "stored-db-secret"- const storedKeytab = "stored-keytab-bytes"- retargetRequest := func(validateOnly bool) *v1pb.UpdateDataSourceRequest {- return &v1pb.UpdateDataSourceRequest{- Name: "instances/probe",- DataSource: &v1pb.DataSource{- Id: "admin-ds",- Host: "attacker.example.com",- Password: storedPassword,- SaslConfig: &v1pb.SASLConfig{- Mechanism: &v1pb.SASLConfig_KrbConfig{- KrbConfig: &v1pb.KerberosConfig{Keytab: []byte(storedKeytab)},- },- },- },- ValidateOnly: validateOnly,- }- }-- invoke := func(t *testing.T, correlationID string, request *v1pb.UpdateDataSourceRequest, rerr error) {- t.Helper()- authCtx := &common.AuthContext{- Audit: true,- AuthMethod: common.AuthMethodIAM,- Resources: []*common.Resource{{Type: common.ResourceTypeWorkspace, ID: auditTestWorkspace}},- DelegatedGrant: &common.DelegatedGrant{CorrelationID: correlationID},- }- next := func(_ context.Context, _ connect.AnyRequest) (connect.AnyResponse, error) {- if rerr != nil {- return nil, rerr- }- return connect.NewResponse(&v1pb.Instance{Name: "instances/probe"}), nil- }- req := &specRequest{- AnyRequest: connect.NewRequest(request),- procedure: "/bytebase.v1.InstanceService/UpdateDataSource",- }- _, err := in.WrapUnary(next)(newAuditTestContext(authCtx), req)- if rerr == nil {- require.NoError(t, err)- } else {- require.Error(t, err)- }- }-- t.Run("a denied validate-only call is recorded", func(t *testing.T) {- denial := connect.NewError(connect.CodePermissionDenied,- errors.New("InstanceService/UpdateDataSource is not available to MCP sessions"))- invoke(t, "corr-validate-only-denied", retargetRequest(true), denial)-- rows := findRowsByCorrelation(t, st, "corr-validate-only-denied")- require.Len(t, rows, 1,- "a refused attempt must be auditable whether or not validate_only was set — "+- "otherwise the flag is a switch that turns off the record")- require.Equal(t, int32(connect.CodePermissionDenied), rows[0].Payload.GetStatus().GetCode(),- "the row must carry the denial, not a blank status")-- // The newly-logged row goes through the same marshalAuditPayload- // redaction as every other UpdateDataSource row — that is why- // narrowing the skip exposes no field the plain path did not already- // write. It pins that the path runs on a row the skip used to swallow;- // which fields the redaction covers is the annotation's job, pinned by- // TestAuditRedactionCoversEveryAnnotatedField.- request := rows[0].Payload.GetRequest()- require.NotEmpty(t, request)- require.NotContains(t, request, storedPassword,- "the request payload must be redacted before it reaches the audit row")- // The row is protojson, which renders a bytes field base64, so the- // keytab has to be looked for in that form — searching for the ASCII- // literal would pass even with keytab redaction deleted outright.- require.NotContains(t, request, base64.StdEncoding.EncodeToString([]byte(storedKeytab)),- "the keytab reaches the row base64-encoded, and must be masked before it does")- require.Contains(t, request, "attacker.example.com",- "the destination is what an operator reads the row for — it must survive redaction")- })-- t.Run("a succeeding validate-only call stays silent", func(t *testing.T) {- invoke(t, "corr-validate-only-ok", retargetRequest(true), nil)-- require.Empty(t, findRowsByCorrelation(t, st, "corr-validate-only-ok"),- "a dry run that succeeded changed nothing — the original reason for the skip")- })-- t.Run("a failed validate-only call is recorded, denial or not", func(t *testing.T) {- // The rule is any failure, not any refusal, and this is the case that- // pins it: a validate-only connection test that could not reach the- // host. It is also the bulk of what the change adds, since the- // instance form dials before every save.- failedDial := connect.NewError(connect.CodeInvalidArgument,- errors.New("failed to connect to attacker.example.com: connection refused"))- invoke(t, "corr-validate-only-dial-failed", retargetRequest(true), failedDial)-- rows := findRowsByCorrelation(t, st, "corr-validate-only-dial-failed")- require.Len(t, rows, 1,- "keying on a denial code would drop every other rejected attempt — the hole this change closes")- require.Equal(t, int32(connect.CodeInvalidArgument), rows[0].Payload.GetStatus().GetCode())- })-- t.Run("a denied ordinary call is recorded", func(t *testing.T) {- denial := connect.NewError(connect.CodePermissionDenied, errors.New("denied"))- invoke(t, "corr-plain-denied", retargetRequest(false), denial)-- rows := findRowsByCorrelation(t, st, "corr-plain-denied")- require.Len(t, rows, 1, "control: the flag is the only difference between this and the first case")- require.True(t, strings.Contains(rows[0].Payload.GetMethod(), "UpdateDataSource"))- })-}diff --git a/backend/api/v1/auth_service_email_code.go b/backend/api/v1/auth_service_email_code.goindex b30af4b6cc..10f6d28279 100644--- a/backend/api/v1/auth_service_email_code.go+++ b/backend/api/v1/auth_service_email_code.go@@ -394,6 +394,14 @@ type emailCodeTemplate struct {BodyFmt string}+// eligibleForPasswordReset reports whether a reset code may be mailed for an+// account: only a live end user has a password to reset. A missing, deleted+// or non-human account is skipped silently, so the reset flow never confirms+// which addresses exist.+func eligibleForPasswordReset(account *store.AccountMessage) bool {+ return account != nil && account.Type == storepb.PrincipalType_END_USER && !account.MemberDeleted+}+// sendEmailVerificationCode is package-level because both AuthService (login// and password-reset codes) and UserService (the re-authentication code) send// them; the deps it needs are passed rather than reached through a receiver.@@ -405,7 +413,7 @@ func sendEmailVerificationCode(ctx context.Context, stores *store.Store, secret,if err != nil {return errors.Wrap(err, "failed to look up account for password reset")}- if account == nil || account.Type != storepb.PrincipalType_END_USER || account.MemberDeleted {+ if !eligibleForPasswordReset(account) {return nil // silent: account doesn't exist}}diff --git a/backend/api/v1/auth_service_email_code_test.go b/backend/api/v1/auth_service_email_code_test.gonew file mode 100644index 0000000000..3bae54dd13--- /dev/null+++ b/backend/api/v1/auth_service_email_code_test.go@@ -0,0 +1,167 @@+package v1++import (+ "context"+ "testing"+ "time"++ "connectrpc.com/connect"+ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/component/config"+ "github.com/bytebase/bytebase/backend/enterprise"+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+ "github.com/bytebase/bytebase/backend/store"+)++// spendSendBudget drives the deployment bucket to its cap without sending mail.+// Claims until refused rather than a fixed count, since a send earlier in the+// test may already have taken a slot.+func spendSendBudget(ctx context.Context, t *testing.T, stores *store.Store) {+ t.Helper()+ claim := func() bool {+ granted, err := stores.ClaimAttempt(ctx, signinCodeSendBudgetKey, storepb.LoginAttemptKind_EMAIL_CODE_SEND, emailCodeSendPerWindow, emailCodeSendWindow)+ require.NoError(t, err)+ return granted+ }+ for range emailCodeSendPerWindow {+ if !claim() {+ return+ }+ }+ require.False(t, claim(), "the bucket must be spent")+}++// newBudgetTestService builds a SaaS service whose mail host refuses on connect,+// so Internal means a send reached delivery and ResourceExhausted means it was+// refused a budget slot.+func newBudgetTestService(t *testing.T, stores *store.Store) *AuthService {+ t.Helper()+ t.Setenv("EMAIL_CONFIG", `{"from":"[email protected]","type":"SMTP","smtp":{"host":"127.0.0.1","port":1,"encryption":"ENCRYPTION_NONE","authentication":"AUTHENTICATION_NONE"}}`)+ licenseService, err := enterprise.NewLicenseService(common.ReleaseModeDev, stores, false, "")+ require.NoError(t, err)+ return NewAuthService(stores, "test-secret", licenseService, &config.Profile{SaaS: true}, nil)+}++// TestSendEmailLoginCodeBudgetsEverySender pins that naming a workspace does not+// buy a separate budget. SaaS copies EMAIL_CONFIG into every workspace it+// creates, so a named send leaves over the same deployment sender; giving each+// workspace its own bucket would meter a shared reputation per tenant, and the+// workspace is caller-supplied besides.+func TestSendEmailLoginCodeBudgetsEverySender(t *testing.T) {+ const workspaceID = "email-code-shared-sender"++ ctx := context.Background()+ stores := newAuthTestStore(t)+ _, err := stores.CreateWorkspace(ctx, &store.WorkspaceMessage{+ ResourceID: workspaceID,+ Payload: &storepb.WorkspacePayload{Title: "Shared sender"},+ }, "[email protected]")+ require.NoError(t, err)+ _, err = stores.UpsertSetting(ctx, &store.SettingMessage{+ Name: storepb.SettingName_WORKSPACE_PROFILE,+ Workspace: workspaceID,+ Value: &storepb.WorkspaceProfileSetting{+ AllowEmailCodeSignin: true,+ PasswordRestriction: &storepb.WorkspaceProfileSetting_PasswordRestriction{MinLength: 8},+ },+ })+ require.NoError(t, err)+ // The workspace carries the deployment's own mail config, as SaaS signup writes it.+ _, err = stores.UpsertSetting(ctx, &store.SettingMessage{+ Name: storepb.SettingName_EMAIL,+ Workspace: workspaceID,+ Value: &storepb.EmailSetting{+ From: "[email protected]",+ Type: storepb.EmailSetting_SMTP,+ Config: &storepb.EmailSetting_Smtp{Smtp: &storepb.EmailSetting_SMTPConfig{+ Host: "127.0.0.1",+ Port: 1,+ Encryption: storepb.EmailSetting_SMTPConfig_ENCRYPTION_NONE,+ Authentication: storepb.EmailSetting_SMTPConfig_AUTHENTICATION_NONE,+ }},+ },+ })+ require.NoError(t, err)++ service := newBudgetTestService(t, stores)+ workspaceName := common.FormatWorkspace(workspaceID)+ spendSendBudget(ctx, t, stores)++ _, err = service.SendEmailLoginCode(ctx, connect.NewRequest(&v1pb.SendEmailLoginCodeRequest{+ Email: "[email protected]",+ Workspace: &workspaceName,+ }))+ require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(err),+ "naming a workspace must not escape the deployment budget")+}++// TestSendEmailLoginCodeBudgetsWorkspacelessSends pins the cap on the path a+// brand-new signup takes, where the 60s per-recipient cooldown never fires+// because a campaign writes each address exactly once.+func TestSendEmailLoginCodeBudgetsWorkspacelessSends(t *testing.T) {+ ctx := context.Background()+ stores := newAuthTestStore(t)+ service := newBudgetTestService(t, stores)++ send := func(email string) error {+ _, err := service.SendEmailLoginCode(ctx, connect.NewRequest(&v1pb.SendEmailLoginCodeRequest{Email: email}))+ return err+ }++ require.Equal(t, connect.CodeInternal, connect.CodeOf(send("[email protected]")),+ "a send within budget must fail only on delivery")+ spendSendBudget(ctx, t, stores)+ require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(send("[email protected]")))+ // The bucket is the sender, so a spent budget stops the next send whatever+ // address it names.+ require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(send("[email protected]")))+}++// TestSendEmailLoginCodeBudgetKeepsExistingCode pins that a refused budget is+// inert: the upsert replaces the stored hash, so claiming after it would let an+// exhausted bucket destroy a pending code and send no replacement. It also pins+// that the refusal does not depend on the recipient — an address mid-cooldown is+// answered exactly as any other, or an exhausted bucket would let a caller probe+// who had just been sent a code.+func TestSendEmailLoginCodeBudgetKeepsExistingCode(t *testing.T) {+ ctx := context.Background()+ stores := newAuthTestStore(t)+ service := newBudgetTestService(t, stores)++ send := func(email string) error {+ _, err := service.SendEmailLoginCode(ctx, connect.NewRequest(&v1pb.SendEmailLoginCodeRequest{Email: email}))+ return err+ }++ const victim = "[email protected]"+ const heldHash = "held-code-hash"+ seed := func(email, hash string, lastSent time.Time) {+ t.Helper()+ sent, err := stores.UpsertEmailVerificationCodeIfCooldownExpired(ctx, &store.EmailVerificationCodeMessage{+ Email: email,+ Purpose: storepb.EmailVerificationCodePurpose_LOGIN,+ CodeHash: hash,+ ExpiresAt: time.Now().Add(emailCodeExpiry),+ LastSentAt: lastSent,+ }, emailCodeResendCooldown)+ require.NoError(t, err)+ require.True(t, sent)+ }+ // A held code the upsert would replace once the cooldown passes.+ seed(victim, heldHash, time.Now().Add(-5*time.Minute))+ seed("[email protected]", "recent-code-hash", time.Now())++ spendSendBudget(ctx, t, stores)++ require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(send(victim)))+ row, err := stores.GetEmailVerificationCode(ctx, victim, storepb.EmailVerificationCodePurpose_LOGIN)+ require.NoError(t, err)+ require.NotNil(t, row, "a refused budget must not delete the code the recipient is holding")+ require.Equal(t, heldHash, row.CodeHash, "a refused budget must not replace it either")++ require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(send("[email protected]")),+ "an address mid-cooldown must be answered as any other is")+}diff --git a/backend/api/v1/auth_service_lockout_test.go b/backend/api/v1/auth_service_lockout_test.goindex 856516ea3f..3794a449ca 100644--- a/backend/api/v1/auth_service_lockout_test.go+++ b/backend/api/v1/auth_service_lockout_test.go@@ -8,89 +8,30 @@ import ("github.com/bytebase/bytebase/backend/common/testcontainer""connectrpc.com/connect"- "github.com/pquerna/otp/totp""github.com/stretchr/testify/require""golang.org/x/crypto/bcrypt"- "github.com/bytebase/bytebase/backend/api/auth""github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/component/config"- "github.com/bytebase/bytebase/backend/enterprise"storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1""github.com/bytebase/bytebase/backend/runner/cleaner""github.com/bytebase/bytebase/backend/store")-// The lockout tests below pin docs/design/login-attempt-lockout.md: every-// guessable credential claims a login_attempt slot for the identity under-// attack before the credential is checked, success deletes the row, and locked-// identities are refused with ResourceExhausted before any bcrypt, TOTP, or-// hash comparison — independent of audit logs, so identical on Cloud and-// self-hosted.--func TestPasswordLockoutClaims(t *testing.T) {- ctx := context.Background()- stores := newAuthTestStore(t)- service := &AuthService{store: stores}-- const password = "1024bytebase"- createLockoutTestUser(ctx, t, stores, "[email protected]", password)-- t.Run("unknown and known emails lock at the same attempt with the same error", func(t *testing.T) {- for _, email := range []string{"[email protected]", "[email protected]"} {- for range loginAttemptMax {- _, err := service.getAndVerifyUser(ctx, &v1pb.LoginRequest{Email: email, Password: "wrong-password"})- require.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))- require.ErrorContains(t, err, errMsgInvalidCredentials)- }- _, err := service.getAndVerifyUser(ctx, &v1pb.LoginRequest{Email: email, Password: "wrong-password"})- require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(err))- require.ErrorContains(t, err, errMsgTooManyPassword)- }- })-- t.Run("a locked identity is refused before bcrypt", func(t *testing.T) {- _, err := service.getAndVerifyUser(ctx, &v1pb.LoginRequest{Email: "[email protected]", Password: password})- require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(err),- "the correct password must not slip through a lock: the slot is claimed before the credential is checked")- })-- t.Run("success clears the counter", func(t *testing.T) {- const email = "[email protected]"- createLockoutTestUser(ctx, t, stores, email, password)- for range 3 {- _, err := service.getAndVerifyUser(ctx, &v1pb.LoginRequest{Email: email, Password: "wrong-password"})- require.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))- }- user, err := service.getAndVerifyUser(ctx, &v1pb.LoginRequest{Email: email, Password: password})- require.NoError(t, err)- require.Equal(t, email, user.Email)-- // The forgotten failures must not count against the fresh window.- for range loginAttemptMax {- _, err := service.getAndVerifyUser(ctx, &v1pb.LoginRequest{Email: email, Password: "wrong-password"})- require.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))- }- _, err = service.getAndVerifyUser(ctx, &v1pb.LoginRequest{Email: email, Password: "wrong-password"})- require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(err))- })-- t.Run("garbage identities never write a row", func(t *testing.T) {- // Over-length emails are refused at the proto edge (see- // TestAuthEmailFieldsAreLengthBounded); invalid syntax is refused here.- for range loginAttemptMax + 1 {- _, err := service.getAndVerifyUser(ctx, &v1pb.LoginRequest{Email: "not-an-email", Password: "wrong-password"})- require.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err),- "an invalid-syntax email is rejected before the claim, with the same error as a wrong password")- }- var count int- require.NoError(t, stores.GetDB().QueryRowContext(ctx,- `SELECT COUNT(*) FROM login_attempt WHERE identity = 'not-an-email'`).Scan(&count))- require.Zero(t, count)- })-}-+// The lockout rules of docs/design/login-attempt-lockout.md — every guessable+// credential claims a login_attempt slot for the identity under attack before+// the credential is checked, success deletes the row, and locked identities+// are refused with ResourceExhausted before any bcrypt, TOTP, or hash+// comparison — are pinned on a live server in backend/tests+// (TestLoginFailureLockout), and the claim itself in backend/store+// (TestLoginAttemptClaim). What stays here needs state no API can set up.++// TestEmailCodeLockoutClaims pins the claims around a pending code, which no+// API can plant without SMTP: the row outlives wrong guesses, a resend does+// not reset the counter, a matched code clears it and is single-use, and+// login and reset codes draw from one bucket per email. That the lockout+// precedes the code row at all is TestLoginFailureLockout in backend/tests.func TestEmailCodeLockoutClaims(t *testing.T) {ctx := context.Background()stores := newAuthTestStore(t)@@ -109,19 +50,6 @@ func TestEmailCodeLockoutClaims(t *testing.T) {require.True(t, sent)}- t.Run("the lockout precedes the code row", func(t *testing.T) {- const email = "[email protected]"- for range loginAttemptMax {- err := service.verifyEmailCode(ctx, email, storepb.EmailVerificationCodePurpose_LOGIN, "000000")- require.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))- require.ErrorContains(t, err, "invalid or expired code")- }- err := service.verifyEmailCode(ctx, email, storepb.EmailVerificationCodePurpose_LOGIN, "000000")- require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(err),- "guessing must be bounded per identity even when no code is pending")- require.ErrorContains(t, err, errMsgTooManyEmailCode)- })-t.Run("resending a code does not reset the counter", func(t *testing.T) {const email = "[email protected]"upsertCode(email, storepb.EmailVerificationCodePurpose_LOGIN, "111111")@@ -138,7 +66,7 @@ func TestEmailCodeLockoutClaims(t *testing.T) {"the bound must survive code rotation: five wrong guesses lock the email even against a fresh valid code")})- t.Run("wrong guesses no longer delete the code row", func(t *testing.T) {+ t.Run("wrong guesses do not delete the code row", func(t *testing.T) {const email = "[email protected]"upsertCode(email, storepb.EmailVerificationCodePurpose_LOGIN, "111111")for range loginAttemptMax {@@ -238,126 +166,6 @@ func TestResetPasswordClearsPasswordLockout(t *testing.T) {require.Equal(t, email, user.Email)}-func TestMFALockoutClaims(t *testing.T) {- ctx := context.Background()- stores := newAuthTestStore(t)- const secret = "test-secret"- service := &AuthService{store: stores, secret: secret}-- attempt := func(email string, code *string) error {- t.Helper()- token, err := auth.GenerateMFATempTokenWithLoginMethod(email, string(loginAuthMethodPassword), secret, mfaTempTokenDuration)- require.NoError(t, err)- _, _, err = service.completeMFALogin(ctx, &v1pb.LoginRequest{MfaTempToken: &token, OtpCode: code})- return err- }-- t.Run("guesses are bounded per identity across temp tokens", func(t *testing.T) {- const email = "[email protected]"- _, otpSecret := setupMFAUser(ctx, t, stores, email)- for i := range loginAttemptMax {- // A fresh token per attempt must not buy fresh guesses.- err := attempt(email, ptr("not-a-code"))- require.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err), "attempt %d", i+1)- require.ErrorContains(t, err, errMsgInvalidMFACode)- }- err := attempt(email, ptr("not-a-code"))- require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(err))- require.ErrorContains(t, err, errMsgTooManyMFA)-- validOTP, err := totp.GenerateCode(otpSecret, time.Now())- require.NoError(t, err)- err = attempt(email, &validOTP)- require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(err),- "a locked identity is refused before the TOTP comparison")- })-- t.Run("success clears the counter", func(t *testing.T) {- const email = "[email protected]"- _, otpSecret := setupMFAUser(ctx, t, stores, email)- for range loginAttemptMax - 1 {- err := attempt(email, ptr("not-a-code"))- require.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))- }- validOTP, err := totp.GenerateCode(otpSecret, time.Now())- require.NoError(t, err)- require.NoError(t, attempt(email, &validOTP))-- for range loginAttemptMax {- err := attempt(email, ptr("not-a-code"))- require.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))- }- err = attempt(email, ptr("not-a-code"))- require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(err))- })-- t.Run("codeless requests claim nothing", func(t *testing.T) {- const email = "[email protected]"- setupMFAUser(ctx, t, stores, email)- for range loginAttemptMax + 1 {- err := attempt(email, nil)- require.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))- require.ErrorContains(t, err, "OTP or recovery code is required")- }- // The budget is untouched: a real wrong guess still gets the- // invalid-code answer, not a lockout.- err := attempt(email, ptr("not-a-code"))- require.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))- require.ErrorContains(t, err, errMsgInvalidMFACode)- })-}--func TestSwitchWorkspaceMFAClaims(t *testing.T) {- t.Parallel()- ctx := context.Background()- stores := newAuthTestStore(t)- const secret = "test-secret"- const workspaceID = "switch-mfa-test"- const email = "[email protected]"-- _, err := stores.CreateWorkspace(ctx, &store.WorkspaceMessage{- ResourceID: workspaceID,- Payload: &storepb.WorkspacePayload{Title: "Switch MFA test"},- }, email)- require.NoError(t, err)- licenseService, err := enterprise.NewLicenseService(common.ReleaseModeDev, stores, false, "")- require.NoError(t, err)- require.NoError(t, licenseService.StoreLicense(ctx, workspaceID, authTestEnterpriseLicense))- service := NewAuthService(stores, secret, licenseService, &config.Profile{}, nil)-- user, _ := setupMFAUser(ctx, t, stores, email)-- userCtx := context.WithValue(ctx, common.UserContextKey, user)- token, err := auth.GenerateMFATempTokenWithLoginMethod(email, string(loginAuthMethodPassword), secret, mfaTempTokenDuration)- require.NoError(t, err)-- // An argument error must not consume attempt budget — the loop below- // still needs the full allowance before the lock engages.- _, err = service.SwitchWorkspace(userCtx, connect.NewRequest(&v1pb.SwitchWorkspaceRequest{- Workspace: common.FormatWorkspace(workspaceID),- MfaTempToken: &token,- }))- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))-- for range loginAttemptMax {- _, err := service.SwitchWorkspace(userCtx, connect.NewRequest(&v1pb.SwitchWorkspaceRequest{- Workspace: common.FormatWorkspace(workspaceID),- MfaTempToken: &token,- OtpCode: ptr("not-a-code"),- }))- require.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))- require.ErrorContains(t, err, errMsgInvalidMFACode)- }- _, err = service.SwitchWorkspace(userCtx, connect.NewRequest(&v1pb.SwitchWorkspaceRequest{- Workspace: common.FormatWorkspace(workspaceID),- MfaTempToken: &token,- OtpCode: ptr("not-a-code"),- }))- require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(err),- "MFA completion on workspace switch draws from the same per-identity bucket")- require.ErrorContains(t, err, errMsgTooManyMFA)-}-// TestLDAPLoginIdentity pins the provider-scoped lockout key. "/" is legal in// an email local part, so a "/" separator would let the literal email account// "corpldap/[email protected]" share — lock out, and on success clear — the@@ -374,55 +182,6 @@ func TestLDAPLoginIdentity(t *testing.T) {require.False(t, common.IsValidEmail(ldapLoginIdentity("corpldap", "[email protected]")))}-func TestLDAPLockoutClaims(t *testing.T) {- t.Parallel()- ctx := context.Background()- stores := newAuthTestStore(t)- service := &AuthService{store: stores, secret: "test-secret", profile: &config.Profile{ExternalURL: "http://localhost"}}-- const idpID = "ldap-lockout-test"- _, err := stores.CreateWorkspace(ctx, &store.WorkspaceMessage{- ResourceID: "ldap-lockout-ws",- Payload: &storepb.WorkspacePayload{Title: "LDAP lockout test"},- }, "[email protected]")- require.NoError(t, err)- _, err = stores.CreateIdentityProvider(ctx, &store.IdentityProviderMessage{- ResourceID: idpID,- Workspace: "ldap-lockout-ws",- Title: "Unreachable directory",- Type: storepb.IdentityProviderType_LDAP,- Config: &storepb.IdentityProviderConfig{Config: &storepb.IdentityProviderConfig_LdapConfig{LdapConfig: &storepb.LDAPIdentityProviderConfig{- Host: "127.0.0.1",- Port: 1,- BindDn: "cn=admin,dc=example,dc=com",- BindPassword: "unused",- BaseDn: "dc=example,dc=com",- UserFilter: "(uid=%s)",- FieldMapping: &storepb.FieldMapping{Identifier: "uid"},- }}},- })- require.NoError(t, err)-- request := &v1pb.LoginRequest{IdpName: "idps/" + idpID, Email: "Alice", Password: "wrong"}- _, err = service.getOrCreateUserWithIDP(ctx, request)- require.Error(t, err, "the directory is unreachable")-- // The slot is claimed before the bind, under the provider-scoped key.- var identity string- require.NoError(t, stores.GetDB().QueryRowContext(ctx,- `SELECT identity FROM login_attempt WHERE kind = 'PASSWORD'`).Scan(&identity))- require.Equal(t, idpID+":Alice", identity)-- for range loginAttemptMax - 1 {- _, err := service.getOrCreateUserWithIDP(ctx, request)- require.Error(t, err)- require.NotEqual(t, connect.CodeResourceExhausted, connect.CodeOf(err))- }- _, err = service.getOrCreateUserWithIDP(ctx, request)- require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(err))- require.ErrorContains(t, err, errMsgTooManyPassword)-}-// TestLoginAttemptRetentionOutlivesLockouts ties the cleaner's purge horizon// to the lockout window: a retention shorter than the window would delete// still-running locks, silently capping every lockout at the retention.@@ -452,15 +211,3 @@ func createLockoutTestUser(ctx context.Context, t *testing.T, stores *store.Storrequire.NoError(t, err)return user}--func setupMFAUser(ctx context.Context, t *testing.T, stores *store.Store, email string) (*store.UserMessage, string) {- t.Helper()- user := createLockoutTestUser(ctx, t, stores, email, "1024bytebase")- key, err := totp.Generate(totp.GenerateOpts{Issuer: "bytebase-test", AccountName: email})- require.NoError(t, err)- user, err = stores.UpdateUser(ctx, user, &store.UpdateUserMessage{- MFAConfig: &storepb.MFAConfig{OtpSecret: key.Secret()},- })- require.NoError(t, err)- return user, key.Secret()-}diff --git a/backend/api/v1/auth_service_test.go b/backend/api/v1/auth_service_test.goindex 0bab06815a..9c068366c1 100644--- a/backend/api/v1/auth_service_test.go+++ b/backend/api/v1/auth_service_test.go@@ -6,8 +6,6 @@ import ("testing""time"- "github.com/bytebase/bytebase/backend/common/testcontainer"-"connectrpc.com/connect""github.com/golang-jwt/jwt/v5""github.com/stretchr/testify/require"@@ -15,96 +13,10 @@ import ("github.com/bytebase/bytebase/backend/api/auth""github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/component/config"- "github.com/bytebase/bytebase/backend/enterprise"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1""github.com/bytebase/bytebase/backend/store")-const authTestEnterpriseLicense = "eyJhbGciOiJSUzI1NiIsImtpZCI6InYxIiwidHlwIjoiSldUIn0.eyJpbnN0YW5jZUNvdW50Ijo5OTksInRyaWFsaW5nIjpmYWxzZSwicGxhbiI6IkVOVEVSUFJJU0UiLCJvcmdOYW1lIjoiYmIiLCJhdWQiOiJiYi5saWNlbnNlIiwiZXhwIjo3OTc0OTc5MjAwLCJpYXQiOjE2NjM2Njc1NjEsImlzcyI6ImJ5dGViYXNlIiwic3ViIjoiMDAwMDEwMDAuIn0.JjYCMeAAMB9FlVeDFLdN3jvFcqtPsbEzaIm1YEDhUrfekthCbIOeX_DB2Bg2OUji3HSX5uDvG9AkK4Gtrc4gLMPI3D5mk3L-6wUKZ0L4REztS47LT4oxVhpqPQayYa9lKJB1YoHaqeMV4Z5FXeOXwuACoELznlwpT6pXo9xXm_I6QwQiO7-zD83XOTO4PRjByc-q3GKQu_64zJMIKiCW0I8a3GvrdSnO7jUuYU1KPmCuk0ZRq3I91m29LTo478BMST59HqCLj1GGuCKtR3SL_376XsZfUUM0iSAur5scg99zNGWRj-sUo05wbAadYx6V6TKaWrBUi_8_0RnJyP5gbA"--func TestLoginAnnouncesPreAuthenticationWorkspace(t *testing.T) {- ctx := context.Background()- db, stores, _ := testcontainer.NewMetadataDB(t)- _, err := db.ExecContext(ctx, `INSERT INTO workspace (resource_id) VALUES ('ws-test')`)- require.NoError(t, err)-- t.Run("self-hosted singleton", func(t *testing.T) {- service := &AuthService{store: stores, profile: &config.Profile{}}- var auditWorkspaceID string- ctx := common.WithSetAuditWorkspaceID(ctx, func(workspaceID string) {- auditWorkspaceID = workspaceID- })- _, err := service.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{- Email: "[email protected]",- Password: "wrong-password",- }))- require.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))- require.Equal(t, "ws-test", auditWorkspaceID)- })-- t.Run("SaaS requested workspace", func(t *testing.T) {- service := &AuthService{store: stores, profile: &config.Profile{SaaS: true}}- var auditWorkspaceID string- ctx := common.WithSetAuditWorkspaceID(ctx, func(workspaceID string) {- auditWorkspaceID = workspaceID- })- workspace := common.FormatWorkspace("ws-test")- _, err := service.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{- Email: "[email protected]",- Password: "wrong-password",- Workspace: &workspace,- }))- require.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))- require.Equal(t, "ws-test", auditWorkspaceID)- })-- t.Run("SaaS requested workspace without membership", func(t *testing.T) {- user, err := stores.CreateUser(ctx, &store.UserMessage{- Email: "[email protected]",- Name: "Member",- PasswordHash: "wrong-password-hash",- Profile: &storepb.UserProfile{},- })- require.NoError(t, err)- _, err = stores.CreateWorkspace(ctx, &store.WorkspaceMessage{- ResourceID: "ws-member",- Payload: &storepb.WorkspacePayload{Title: "Member workspace"},- }, user.Email)- require.NoError(t, err)-- service := &AuthService{store: stores, profile: &config.Profile{SaaS: true}}- var auditWorkspaceIDs []string- ctx := common.WithSetAuditWorkspaceID(ctx, func(workspaceID string) {- auditWorkspaceIDs = append(auditWorkspaceIDs, workspaceID)- })- workspace := common.FormatWorkspace("ws-test")- _, err = service.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{- Email: user.Email,- Password: "wrong-password",- Workspace: &workspace,- }))- require.Equal(t, connect.CodeUnauthenticated, connect.CodeOf(err))- require.Equal(t, []string{"ws-test"}, auditWorkspaceIDs)- })-- t.Run("rejects nonexistent requested workspace", func(t *testing.T) {- service := &AuthService{store: stores, profile: &config.Profile{}}- var auditWorkspaceID string- ctx := common.WithSetAuditWorkspaceID(ctx, func(workspaceID string) {- auditWorkspaceID = workspaceID- })- workspace := common.FormatWorkspace("missing")- _, err := service.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{- Email: "[email protected]",- Password: "wrong-password",- Workspace: &workspace,- }))- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))- require.Empty(t, auditWorkspaceID)- })-}-func TestExtractDomain(t *testing.T) {t.Parallel()tests := []struct {@@ -137,205 +49,6 @@ func TestExtractDomain(t *testing.T) {}}-func TestPasswordResetEmailSkipsDeletedUser(t *testing.T) {- ctx := context.Background()- stores := newAuthTestStore(t)-- user, err := stores.CreateUser(ctx, &store.UserMessage{- Email: "[email protected]",- Name: "Deleted user",- PasswordHash: "unused",- Profile: &storepb.UserProfile{},- })- require.NoError(t, err)- deleted := true- _, err = stores.UpdateUser(ctx, user, &store.UpdateUserMessage{Delete: &deleted})- require.NoError(t, err)-- t.Setenv("EMAIL_CONFIG", "")- service := &AuthService{store: stores, secret: "test-secret"}- require.NoError(t, service.sendEmailVerificationCode(- ctx,- "",- user.Email,- storepb.EmailVerificationCodePurpose_PASSWORD_RESET,- "subject",- "code: %s, expires in %d minutes",- "", 0,- ))-}--func TestLoginEnforcesWorkspaceDomains(t *testing.T) {- const (- workspaceID = "email-code-domain-test"- secret = "test-secret"- code = "123456"- )-- ctx := context.Background()- stores := newAuthTestStore(t)-- _, err := stores.CreateWorkspace(ctx, &store.WorkspaceMessage{- ResourceID: workspaceID,- Payload: &storepb.WorkspacePayload{Title: "Email code domain test"},- }, "[email protected]")- require.NoError(t, err)- _, err = stores.UpsertSetting(ctx, &store.SettingMessage{- Name: storepb.SettingName_WORKSPACE_PROFILE,- Workspace: workspaceID,- Value: &storepb.WorkspaceProfileSetting{- Domains: []string{"allowed.example"},- EnforceIdentityDomain: true,- AllowEmailCodeSignin: true,- PasswordRestriction: &storepb.WorkspaceProfileSetting_PasswordRestriction{MinLength: 8},- EnableMetricCollection: true,- DisallowSignup: false,- DisallowPasswordSignin: true,- },- })- require.NoError(t, err)- _, err = stores.UpsertSetting(ctx, &store.SettingMessage{- Name: storepb.SettingName_EMAIL,- Workspace: workspaceID,- Value: &storepb.EmailSetting{},- })- require.NoError(t, err)-- licenseService, err := enterprise.NewLicenseService(common.ReleaseModeDev, stores, false, "")- require.NoError(t, err)- require.NoError(t, licenseService.StoreLicense(ctx, workspaceID, authTestEnterpriseLicense))- service := NewAuthService(stores, secret, licenseService, &config.Profile{}, nil)- workspaceName := common.FormatWorkspace(workspaceID)-- allowedAdmin, err := stores.CreateUser(ctx, &store.UserMessage{- Email: "[email protected]",- Name: "Allowed admin",- PasswordHash: "unused",- Profile: &storepb.UserProfile{},- })- require.NoError(t, err)- blockedAdmin, err := stores.CreateUser(ctx, &store.UserMessage{- Email: "[email protected]",- Name: "Blocked admin",- PasswordHash: "unused",- Profile: &storepb.UserProfile{},- })- require.NoError(t, err)- _, err = stores.PatchWorkspaceIamPolicy(ctx, &store.PatchIamPolicyMessage{- Workspace: workspaceID,- Member: common.FormatUserEmail(blockedAdmin.Email),- Roles: []string{common.FormatRole(store.WorkspaceAdminRole)},- })- require.NoError(t, err)-- t.Run("workspace admin", func(t *testing.T) {- _, err := service.validateLoginPermissions(ctx, blockedAdmin, workspaceID, &v1pb.LoginRequest{- Email: blockedAdmin.Email,- IdpName: "idps/test",- })- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))- require.ErrorContains(t, err, "does not belong to allowed domains")-- _, err = service.validateLoginPermissions(ctx, allowedAdmin, workspaceID, &v1pb.LoginRequest{- Email: allowedAdmin.Email,- Password: "password",- })- require.Equal(t, connect.CodePermissionDenied, connect.CodeOf(err))- require.ErrorContains(t, err, "password signin is disallowed")- })-- t.Run("send code", func(t *testing.T) {- for _, email := range []string{"[email protected]", blockedAdmin.Email} {- _, err := service.SendEmailLoginCode(ctx, connect.NewRequest(&v1pb.SendEmailLoginCodeRequest{- Email: email,- Workspace: &workspaceName,- }))- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))- require.ErrorContains(t, err, "does not belong to allowed domains")-- row, err := stores.GetEmailVerificationCode(ctx, email, storepb.EmailVerificationCodePurpose_LOGIN)- require.NoError(t, err)- require.Nil(t, row)- }- })-- t.Run("authenticate existing user", func(t *testing.T) {- const email = "[email protected]"- _, err := stores.CreateUser(ctx, &store.UserMessage{- Email: email,- Name: "Existing user",- PasswordHash: "unused",- Profile: &storepb.UserProfile{},- })- require.NoError(t, err)- _, err = stores.UpsertEmailVerificationCodeIfCooldownExpired(ctx, &store.EmailVerificationCodeMessage{- Email: email,- Purpose: storepb.EmailVerificationCodePurpose_LOGIN,- CodeHash: hashEmailCode(service.secret, code),- ExpiresAt: time.Now().Add(time.Minute),- LastSentAt: time.Now(),- }, 0)- require.NoError(t, err)-- // The domain gate for existing users runs downstream in- // validateLoginPermissions against the server-resolved workspace, so- // exercise the full Login pipeline.- _, err = service.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{- Email: email,- EmailCode: ptr(code),- }))- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))- require.ErrorContains(t, err, "does not belong to allowed domains")- })-- t.Run("authenticate workspace admin", func(t *testing.T) {- _, err := stores.UpsertEmailVerificationCodeIfCooldownExpired(ctx, &store.EmailVerificationCodeMessage{- Email: blockedAdmin.Email,- Purpose: storepb.EmailVerificationCodePurpose_LOGIN,- CodeHash: hashEmailCode(service.secret, code),- ExpiresAt: time.Now().Add(time.Minute),- LastSentAt: time.Now(),- }, 0)- require.NoError(t, err)-- _, err = service.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{- Email: blockedAdmin.Email,- EmailCode: ptr(code),- }))- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))- require.ErrorContains(t, err, "does not belong to allowed domains")- })-- t.Run("authenticate unknown user before provisioning", func(t *testing.T) {- const email = "[email protected]"- _, err := stores.UpsertEmailVerificationCodeIfCooldownExpired(ctx, &store.EmailVerificationCodeMessage{- Email: email,- Purpose: storepb.EmailVerificationCodePurpose_LOGIN,- CodeHash: hashEmailCode(service.secret, code),- ExpiresAt: time.Now().Add(time.Minute),- LastSentAt: time.Now(),- }, 0)- require.NoError(t, err)-- _, err = service.authenticateEmailCodeLogin(ctx, &v1pb.LoginRequest{- Email: email,- EmailCode: ptr(code),- })- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))- require.ErrorContains(t, err, "does not belong to allowed domains")-- user, err := stores.GetUserByEmail(ctx, email)- require.NoError(t, err)- require.Nil(t, user)-- policy, err := stores.GetWorkspaceIamPolicy(ctx, workspaceID)- require.NoError(t, err)- for _, binding := range policy.Policy.Bindings {- require.NotContains(t, binding.Members, common.FormatUserEmail(email))- }- })-}-// TestSwitchWorkspaceMCPRecognition pins the SwitchWorkspace guard predicate// across both MCP credential generations. An MCP session must never mint a// plain user token: that token is not audience-bound to the MCP resource, does@@ -531,217 +244,27 @@ func TestSwitchWorkspaceInternalRefusesMCPCaller(t *testing.T) {require.NoError(t, s.rejectMCPOriginatedTokenMint(context.Background(), webHeaders, "obtain a workspace token"))}-func TestSignupChecksRestrictionBeforeExistence(t *testing.T) {- const secret = "test-secret"-- ctx := context.Background()- stores := newAuthTestStore(t)- licenseService, err := enterprise.NewLicenseService(common.ReleaseModeDev, stores, false, "")- require.NoError(t, err)-- // The binding matters: signup resolves the workspace via FindWorkspace(email),- // so a member resolves to a real ID and a stranger to none. Without it both- // resolve to none and a denial naming the workspace would look identical.- takenUser, err := stores.CreateUser(ctx, &store.UserMessage{- Email: "[email protected]",- Name: "Taken",- PasswordHash: "unused",- Profile: &storepb.UserProfile{},- })- require.NoError(t, err)- _, err = stores.CreateWorkspace(ctx, &store.WorkspaceMessage{- ResourceID: "signup-order-test",- Payload: &storepb.WorkspacePayload{Title: "Signup order test"},- }, takenUser.Email)- require.NoError(t, err)-- signup := func(service *AuthService, email string) error {- _, err := service.Signup(ctx, connect.NewRequest(&v1pb.SignupRequest{+// TestSignupOnSaaSDeniesBeforeAnyLookup pins that whether signup is allowed+// never depends on the address: SaaS refuses every signup outright, and it+// must not answer differently for an address that has an account than for+// one that does not. The nil store is the proof that nothing the address+// selects was read. The self-hosted ordering — a duplicate is reported once+// the workspace would accept a signup at all, and outranks the password+// policy — is pinned on a live server in backend/tests.+func TestSignupOnSaaSDeniesBeforeAnyLookup(t *testing.T) {+ t.Parallel()+ service := &AuthService{profile: &config.Profile{SaaS: true}}+ signup := func(email string) error {+ _, err := service.Signup(context.Background(), connect.NewRequest(&v1pb.SignupRequest{Email: email,Title: "Signup test",Password: "password-long-enough",}))return err}-- // SaaS forces DisallowSignup for every workspace, so the RPC can never- // succeed there — and must not answer differently for an address that has- // an account than for one that does not.- t.Run("denied signup reveals nothing", func(t *testing.T) {- service := NewAuthService(stores, secret, licenseService, &config.Profile{SaaS: true}, nil)- takenErr := signup(service, "[email protected]")- unknownErr := signup(service, "[email protected]")- require.Equal(t, connect.CodePermissionDenied, connect.CodeOf(takenErr))- require.Equal(t, connect.CodeOf(unknownErr), connect.CodeOf(takenErr))- require.Equal(t, unknownErr.Error(), takenErr.Error())- })-- // The reorder moved the check behind the DisallowSignup gate, not away.- t.Run("allowed signup still reports a duplicate", func(t *testing.T) {- service := NewAuthService(stores, secret, licenseService, &config.Profile{}, nil)- require.Equal(t, connect.CodeAlreadyExists, connect.CodeOf(signup(service, "[email protected]")))- })-- // Only DisallowSignup gates disclosure. Ordering the password policy first- // hides nothing from a caller who submits a valid password, and tells a- // registered user to fix their password when they should be logging in.- t.Run("a duplicate outranks the password policy", func(t *testing.T) {- service := NewAuthService(stores, secret, licenseService, &config.Profile{}, nil)- _, err := service.Signup(ctx, connect.NewRequest(&v1pb.SignupRequest{- Email: "[email protected]",- Title: "Signup test",- Password: "short",- }))- require.Equal(t, connect.CodeAlreadyExists, connect.CodeOf(err))- })-}--// spendSendBudget drives the deployment bucket to its cap without sending mail.-// Claims until refused rather than a fixed count, since a send earlier in the-// test may already have taken a slot.-func spendSendBudget(ctx context.Context, t *testing.T, stores *store.Store) {- t.Helper()- claim := func() bool {- granted, err := stores.ClaimAttempt(ctx, signinCodeSendBudgetKey, storepb.LoginAttemptKind_EMAIL_CODE_SEND, emailCodeSendPerWindow, emailCodeSendWindow)- require.NoError(t, err)- return granted- }- for range emailCodeSendPerWindow {- if !claim() {- return- }- }- require.False(t, claim(), "the bucket must be spent")-}--// newBudgetTestService builds a SaaS service whose mail host refuses on connect,-// so Internal means a send reached delivery and ResourceExhausted means it was-// refused a budget slot.-func newBudgetTestService(t *testing.T, stores *store.Store) *AuthService {- t.Helper()- t.Setenv("EMAIL_CONFIG", `{"from":"[email protected]","type":"SMTP","smtp":{"host":"127.0.0.1","port":1,"encryption":"ENCRYPTION_NONE","authentication":"AUTHENTICATION_NONE"}}`)- licenseService, err := enterprise.NewLicenseService(common.ReleaseModeDev, stores, false, "")- require.NoError(t, err)- return NewAuthService(stores, "test-secret", licenseService, &config.Profile{SaaS: true}, nil)-}--// TestSendEmailLoginCodeBudgetsEverySender pins that naming a workspace does not-// buy a separate budget. SaaS copies EMAIL_CONFIG into every workspace it-// creates, so a named send leaves over the same deployment sender; giving each-// workspace its own bucket would meter a shared reputation per tenant, and the-// workspace is caller-supplied besides.-func TestSendEmailLoginCodeBudgetsEverySender(t *testing.T) {- const workspaceID = "email-code-shared-sender"-- ctx := context.Background()- stores := newAuthTestStore(t)- _, err := stores.CreateWorkspace(ctx, &store.WorkspaceMessage{- ResourceID: workspaceID,- Payload: &storepb.WorkspacePayload{Title: "Shared sender"},- }, "[email protected]")- require.NoError(t, err)- _, err = stores.UpsertSetting(ctx, &store.SettingMessage{- Name: storepb.SettingName_WORKSPACE_PROFILE,- Workspace: workspaceID,- Value: &storepb.WorkspaceProfileSetting{- AllowEmailCodeSignin: true,- PasswordRestriction: &storepb.WorkspaceProfileSetting_PasswordRestriction{MinLength: 8},- },- })- require.NoError(t, err)- // The workspace carries the deployment's own mail config, as SaaS signup writes it.- _, err = stores.UpsertSetting(ctx, &store.SettingMessage{- Name: storepb.SettingName_EMAIL,- Workspace: workspaceID,- Value: &storepb.EmailSetting{- From: "[email protected]",- Type: storepb.EmailSetting_SMTP,- Config: &storepb.EmailSetting_Smtp{Smtp: &storepb.EmailSetting_SMTPConfig{- Host: "127.0.0.1",- Port: 1,- Encryption: storepb.EmailSetting_SMTPConfig_ENCRYPTION_NONE,- Authentication: storepb.EmailSetting_SMTPConfig_AUTHENTICATION_NONE,- }},- },- })- require.NoError(t, err)-- service := newBudgetTestService(t, stores)- workspaceName := common.FormatWorkspace(workspaceID)- spendSendBudget(ctx, t, stores)-- _, err = service.SendEmailLoginCode(ctx, connect.NewRequest(&v1pb.SendEmailLoginCodeRequest{- Email: "[email protected]",- Workspace: &workspaceName,- }))- require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(err),- "naming a workspace must not escape the deployment budget")-}--// TestSendEmailLoginCodeBudgetsWorkspacelessSends pins the cap on the path a-// brand-new signup takes, where the 60s per-recipient cooldown never fires-// because a campaign writes each address exactly once.-func TestSendEmailLoginCodeBudgetsWorkspacelessSends(t *testing.T) {- ctx := context.Background()- stores := newAuthTestStore(t)- service := newBudgetTestService(t, stores)-- send := func(email string) error {- _, err := service.SendEmailLoginCode(ctx, connect.NewRequest(&v1pb.SendEmailLoginCodeRequest{Email: email}))- return err- }-- require.Equal(t, connect.CodeInternal, connect.CodeOf(send("[email protected]")),- "a send within budget must fail only on delivery")- spendSendBudget(ctx, t, stores)- require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(send("[email protected]")))- // The bucket is the sender, so a spent budget stops the next send whatever- // address it names.- require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(send("[email protected]")))-}--// TestSendEmailLoginCodeBudgetKeepsExistingCode pins that a refused budget is-// inert: the upsert replaces the stored hash, so claiming after it would let an-// exhausted bucket destroy a pending code and send no replacement. It also pins-// that the refusal does not depend on the recipient — an address mid-cooldown is-// answered exactly as any other, or an exhausted bucket would let a caller probe-// who had just been sent a code.-func TestSendEmailLoginCodeBudgetKeepsExistingCode(t *testing.T) {- ctx := context.Background()- stores := newAuthTestStore(t)- service := newBudgetTestService(t, stores)-- send := func(email string) error {- _, err := service.SendEmailLoginCode(ctx, connect.NewRequest(&v1pb.SendEmailLoginCodeRequest{Email: email}))- return err- }-- const victim = "[email protected]"- const heldHash = "held-code-hash"- seed := func(email, hash string, lastSent time.Time) {- t.Helper()- sent, err := stores.UpsertEmailVerificationCodeIfCooldownExpired(ctx, &store.EmailVerificationCodeMessage{- Email: email,- Purpose: storepb.EmailVerificationCodePurpose_LOGIN,- CodeHash: hash,- ExpiresAt: time.Now().Add(emailCodeExpiry),- LastSentAt: lastSent,- }, emailCodeResendCooldown)- require.NoError(t, err)- require.True(t, sent)- }- // A held code the upsert would replace once the cooldown passes.- seed(victim, heldHash, time.Now().Add(-5*time.Minute))- seed("[email protected]", "recent-code-hash", time.Now())-- spendSendBudget(ctx, t, stores)-- require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(send(victim)))- row, err := stores.GetEmailVerificationCode(ctx, victim, storepb.EmailVerificationCodePurpose_LOGIN)- require.NoError(t, err)- require.NotNil(t, row, "a refused budget must not delete the code the recipient is holding")- require.Equal(t, heldHash, row.CodeHash, "a refused budget must not replace it either")-- require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(send("[email protected]")),- "an address mid-cooldown must be answered as any other is")+ takenErr := signup("[email protected]")+ unknownErr := signup("[email protected]")+ require.Equal(t, connect.CodePermissionDenied, connect.CodeOf(takenErr))+ require.Equal(t, connect.CodeOf(unknownErr), connect.CodeOf(takenErr))+ require.Equal(t, unknownErr.Error(), takenErr.Error())}diff --git a/backend/api/v1/database_diff_schema_project_test.go b/backend/api/v1/database_diff_schema_project_test.godeleted file mode 100644index b58c2b656d..0000000000--- a/backend/api/v1/database_diff_schema_project_test.go+++ /dev/null@@ -1,158 +0,0 @@-package v1--import (- "context"- "testing"-- "github.com/bytebase/bytebase/backend/common/testcontainer"-- "connectrpc.com/connect"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/common"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"- "github.com/bytebase/bytebase/backend/store"-)--// The ACL interceptor authorizes request.name only, so the handler confines the-// changelog target to the source's project. One container, since the fixture is-// the expensive part.-func TestDiffSchemaTargetProject(t *testing.T) {- ctx, stores, instanceID, changelogByDatabase := setupDiffSchemaProjectTest(t)- service := NewDatabaseService(stores, nil, nil, nil, nil)-- // Databases without a changelog still get a well-formed name, so a- // rejection is about the project and not about a malformed one.- changelogOn := func(databaseName string) string {- changelogID, ok := changelogByDatabase[databaseName]- if !ok {- changelogID = "1"- }- return common.FormatChangelog(instanceID, databaseName, changelogID)- }- diffAgainstChangelog := func(sourceName, changelogName string) (string, error) {- response, err := service.DiffSchema(ctx, connect.NewRequest(&v1pb.DiffSchemaRequest{- Name: sourceName,- Target: &v1pb.DiffSchemaRequest_Changelog{Changelog: changelogName},- }))- if err != nil {- return "", err- }- return response.Msg.Diff, nil- }-- t.Run("same project diffs", func(t *testing.T) {- // The diff must actually run: a gate that rejected everything would- // still satisfy the negative cases below.- diff, err := diffAgainstChangelog(common.FormatDatabase(instanceID, "app-a"), changelogOn("app-a"))- require.NoError(t, err)- require.Empty(t, diff)- })-- t.Run("changelog source resolves to its database", func(t *testing.T) {- // request.name may itself be a changelog.- diff, err := diffAgainstChangelog(changelogOn("app-a"), changelogOn("app-a"))- require.NoError(t, err)- require.Empty(t, diff)- })-- t.Run("foreign project is rejected", func(t *testing.T) {- // app-b's changelog is real, so without the gate this succeeds and- // returns another project's schema.- _, err := diffAgainstChangelog(common.FormatDatabase(instanceID, "app-a"), changelogOn("app-b"))- require.Error(t, err)- require.Equal(t, connect.CodeNotFound, connect.CodeOf(err))- require.NotContains(t, err.Error(), "project-b")-- // Indistinguishable from a database that does not exist, or the error- // itself answers the question.- _, missing := diffAgainstChangelog(common.FormatDatabase(instanceID, "app-a"), changelogOn("no-such-db"))- require.Equal(t, connect.CodeOf(err), connect.CodeOf(missing))- })-- t.Run("foreign changelog id under an owned database is rejected", func(t *testing.T) {- // Changelog ids are globally unique, so the path's database name alone- // does not establish which changelog it addresses. Same error as an id- // that does not exist, which is what the pre-existing not-found path- // already returns.- _, err := diffAgainstChangelog(- common.FormatDatabase(instanceID, "app-a"),- common.FormatChangelog(instanceID, "app-a", changelogByDatabase["app-b"]),- )- require.Error(t, err)-- _, missing := diffAgainstChangelog(- common.FormatDatabase(instanceID, "app-a"),- common.FormatChangelog(instanceID, "app-a", "999999"),- )- require.Equal(t, connect.CodeOf(missing), connect.CodeOf(err))- })-- t.Run("schema target skips the gate", func(t *testing.T) {- // One-resource requests must not reach the gate at all.- response, err := service.DiffSchema(ctx, connect.NewRequest(&v1pb.DiffSchemaRequest{- Name: common.FormatDatabase(instanceID, "app-a"),- Target: &v1pb.DiffSchemaRequest_Schema{Schema: ""},- }))- require.NoError(t, err)- require.Empty(t, response.Msg.Diff)- })-}--func setupDiffSchemaProjectTest(t *testing.T) (context.Context, *store.Store, string, map[string]string) {- t.Helper()- ctx := context.WithValue(context.Background(), common.WorkspaceIDContextKey, "default")- db, stores, _ := testcontainer.NewMetadataDB(t)- _, err := db.ExecContext(ctx, `- INSERT INTO workspace (resource_id) VALUES ('default');- INSERT INTO project (resource_id, workspace, name) VALUES- ('project-a', 'default', 'Project A'),- ('project-b', 'default', 'Project B');- `)- require.NoError(t, err)-- // A workspace-level instance whose databases sit in different projects: the- // shape that lets one name reach across a project boundary.- instanceID := "shared-instance"- _, err = stores.CreateInstance(ctx, &store.InstanceMessage{- ResourceID: instanceID,- Workspace: "default",- Metadata: &storepb.Instance{- Engine: storepb.Engine_POSTGRES,- DataSources: []*storepb.DataSource{{- Id: "admin",- Type: storepb.DataSourceType_ADMIN,- }},- },- })- require.NoError(t, err)-- for databaseName, projectID := range map[string]string{"app-a": "project-a", "app-b": "project-b"} {- _, err = stores.UpsertDatabase(ctx, &store.DatabaseMessage{- InstanceID: instanceID,- DatabaseName: databaseName,- ProjectID: projectID,- Metadata: &storepb.DatabaseMetadata{},- })- require.NoError(t, err)- require.NoError(t, stores.UpsertDBSchema(ctx, instanceID, databaseName,- &storepb.DatabaseSchemaMetadata{Name: databaseName}, &storepb.DatabaseConfig{}, nil))- }-- // The foreign changelog must exist, or the cross-project case would fail- // for the wrong reason and still pass.- changelogByDatabase := map[string]string{}- for _, databaseName := range []string{"app-a", "app-b"} {- changelogID, err := stores.CreateChangelog(ctx, &store.ChangelogMessage{- InstanceID: instanceID,- DatabaseName: databaseName,- Payload: &storepb.ChangelogPayload{},- Status: store.ChangelogStatusDone,- })- require.NoError(t, err)- changelogByDatabase[databaseName] = changelogID- }-- return ctx, stores, instanceID, changelogByDatabase-}diff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.goindex 4c5fad730d..06c4883185 100644--- a/backend/api/v1/database_service.go+++ b/backend/api/v1/database_service.go@@ -899,9 +899,16 @@ func (s *DatabaseService) validateDiffSchemaTargetProject(ctx context.Context, rif err != nil {return err}- // One error for both: a distinct one would confirm what lives in a project- // the caller cannot see.- if target == nil || target.ProjectID != source.ProjectID {+ return checkDiffSchemaTargetProject(source.ProjectID, target, changelog)+}++// checkDiffSchemaTargetProject confines a changelog target to the source+// database's project. The ACL interceptor authorizes request.name only, so without this+// a changelog under another project's database would hand over that project's+// schema. One error for a missing and a foreign changelog: a distinct one+// would confirm what lives in a project the caller cannot see.+func checkDiffSchemaTargetProject(projectID string, target *store.DatabaseMessage, changelog string) error {+ if target == nil || target.ProjectID != projectID {return connect.NewError(connect.CodeNotFound, errors.Errorf("changelog %q not found", changelog))}return nildiff --git a/backend/api/v1/database_service_test.go b/backend/api/v1/database_service_test.goindex f4a4bb782f..a682329a58 100644--- a/backend/api/v1/database_service_test.go+++ b/backend/api/v1/database_service_test.go@@ -279,3 +279,24 @@ func TestResolveDiffSchemaTargetSDL(t *testing.T) {require.ErrorContains(t, err, "target must be either schema text or changelog")})}++// TestCheckDiffSchemaTargetProject pins the gate DiffSchema puts on a changelog+// target. The ACL interceptor authorizes request.name only, so without it a+// changelog under another project's database would hand over that project's+// schema. A foreign changelog is indistinguishable from one that does not+// exist, or the error itself answers the question.+func TestCheckDiffSchemaTargetProject(t *testing.T) {+ t.Parallel()+ source := &store.DatabaseMessage{ProjectID: "project-a", InstanceID: "shared-instance", DatabaseName: "app-a"}++ require.NoError(t, checkDiffSchemaTargetProject(source.ProjectID, source, "instances/shared-instance/databases/app-a/changelogs/1"))++ foreign := checkDiffSchemaTargetProject(source.ProjectID,+ &store.DatabaseMessage{ProjectID: "project-b", InstanceID: "shared-instance", DatabaseName: "app-b"},+ "instances/shared-instance/databases/app-b/changelogs/2")+ require.Equal(t, connect.CodeNotFound, connect.CodeOf(foreign))+ require.NotContains(t, foreign.Error(), "project-b")++ missing := checkDiffSchemaTargetProject(source.ProjectID, nil, "instances/shared-instance/databases/no-such-db/changelogs/1")+ require.Equal(t, connect.CodeOf(foreign), connect.CodeOf(missing))+}diff --git a/backend/api/v1/instance_descendant_service_test.go b/backend/api/v1/instance_descendant_service_test.godeleted file mode 100644index 3fa70d6aa8..0000000000--- a/backend/api/v1/instance_descendant_service_test.go+++ /dev/null@@ -1,174 +0,0 @@-package v1--import (- "context"- "testing"-- "github.com/bytebase/bytebase/backend/common/testcontainer"-- "connectrpc.com/connect"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/common"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"- "github.com/bytebase/bytebase/backend/store"-)--func TestProjectInstanceDescendantServicesUseCanonicalNames(t *testing.T) {- t.Parallel()- ctx, stores, instanceID, databaseName, changelogID, revisionID := setupProjectInstanceDescendantServiceTest(t)- projectID := "project-a"- parent := common.FormatProjectDatabase(projectID, instanceID, databaseName)-- roles, err := NewInstanceRoleService(stores).ListInstanceRoles(ctx, connect.NewRequest(&v1pb.ListInstanceRolesRequest{- Parent: common.FormatProjectInstance(projectID, instanceID),- }))- require.NoError(t, err)- require.Equal(t, common.FormatProjectInstance(projectID, instanceID)+"/roles/role-a", roles.Msg.Roles[0].Name)-- changelogService := NewChangelogService(stores)- changelogs, err := changelogService.ListChangelogs(ctx, connect.NewRequest(&v1pb.ListChangelogsRequest{Parent: parent}))- require.NoError(t, err)- require.Equal(t, common.FormatProjectChangelog(projectID, instanceID, databaseName, changelogID), changelogs.Msg.Changelogs[0].Name)- changelog, err := changelogService.GetChangelog(ctx, connect.NewRequest(&v1pb.GetChangelogRequest{- Name: common.FormatProjectChangelog(projectID, instanceID, databaseName, changelogID),- }))- require.NoError(t, err)- require.Equal(t, common.FormatProjectChangelog(projectID, instanceID, databaseName, changelogID), changelog.Msg.Name)-- revisionService := NewRevisionService(stores)- revisions, err := revisionService.ListRevisions(ctx, connect.NewRequest(&v1pb.ListRevisionsRequest{Parent: parent}))- require.NoError(t, err)- require.Equal(t, common.FormatProjectRevision(projectID, instanceID, databaseName, revisionID), revisions.Msg.Revisions[0].Name)- require.Equal(t, common.FormatSheet(projectID, "sheet-a"), revisions.Msg.Revisions[0].Sheet)- revision, err := revisionService.GetRevision(ctx, connect.NewRequest(&v1pb.GetRevisionRequest{- Name: common.FormatProjectRevision(projectID, instanceID, databaseName, revisionID),- }))- require.NoError(t, err)- require.Equal(t, common.FormatProjectRevision(projectID, instanceID, databaseName, revisionID), revision.Msg.Name)-}--func TestProjectInstanceDescendantServicesRejectWrongScopeParents(t *testing.T) {- t.Parallel()- ctx, stores, instanceID, databaseName, changelogID, revisionID := setupProjectInstanceDescendantServiceTest(t)- workspaceParent := common.FormatDatabase(instanceID, databaseName)- wrongProjectID := "project-b"- wrongProjectParent := common.FormatProjectDatabase(wrongProjectID, instanceID, databaseName)-- roleService := NewInstanceRoleService(stores)- _, err := roleService.ListInstanceRoles(ctx, connect.NewRequest(&v1pb.ListInstanceRolesRequest{Parent: common.FormatInstance(instanceID)}))- require.Equal(t, connect.CodeNotFound, connect.CodeOf(err))- _, err = roleService.ListInstanceRoles(ctx, connect.NewRequest(&v1pb.ListInstanceRolesRequest{- Parent: common.FormatProjectInstance(wrongProjectID, instanceID),- }))- require.Equal(t, connect.CodeNotFound, connect.CodeOf(err))-- changelogService := NewChangelogService(stores)- _, err = changelogService.ListChangelogs(ctx, connect.NewRequest(&v1pb.ListChangelogsRequest{Parent: workspaceParent}))- require.Equal(t, connect.CodeNotFound, connect.CodeOf(err))- _, err = changelogService.GetChangelog(ctx, connect.NewRequest(&v1pb.GetChangelogRequest{- Name: common.FormatProjectChangelog(wrongProjectID, instanceID, databaseName, changelogID),- }))- require.Equal(t, connect.CodeNotFound, connect.CodeOf(err))-- revisionService := NewRevisionService(stores)- _, err = revisionService.ListRevisions(ctx, connect.NewRequest(&v1pb.ListRevisionsRequest{Parent: wrongProjectParent}))- require.Equal(t, connect.CodeNotFound, connect.CodeOf(err))- _, err = revisionService.GetRevision(ctx, connect.NewRequest(&v1pb.GetRevisionRequest{- Name: common.FormatRevision(instanceID, databaseName, revisionID),- }))- require.Equal(t, connect.CodeNotFound, connect.CodeOf(err))-}--func TestWorkspaceInstanceDescendantServicesKeepWorkspaceNames(t *testing.T) {- t.Parallel()- ctx, stores, instanceID, databaseName, changelogID, revisionID := setupWorkspaceInstanceDescendantServiceTest(t)- parent := common.FormatDatabase(instanceID, databaseName)-- roles, err := NewInstanceRoleService(stores).ListInstanceRoles(ctx, connect.NewRequest(&v1pb.ListInstanceRolesRequest{- Parent: common.FormatInstance(instanceID),- }))- require.NoError(t, err)- require.Equal(t, common.FormatInstance(instanceID)+"/roles/role-a", roles.Msg.Roles[0].Name)-- changelogs, err := NewChangelogService(stores).ListChangelogs(ctx, connect.NewRequest(&v1pb.ListChangelogsRequest{Parent: parent}))- require.NoError(t, err)- require.Equal(t, common.FormatChangelog(instanceID, databaseName, changelogID), changelogs.Msg.Changelogs[0].Name)-- revisions, err := NewRevisionService(stores).ListRevisions(ctx, connect.NewRequest(&v1pb.ListRevisionsRequest{Parent: parent}))- require.NoError(t, err)- require.Equal(t, common.FormatRevision(instanceID, databaseName, revisionID), revisions.Msg.Revisions[0].Name)-}--func setupProjectInstanceDescendantServiceTest(t *testing.T) (context.Context, *store.Store, string, string, string, string) {- t.Helper()- projectID := "project-a"- return setupInstanceDescendantServiceTest(t, &projectID)-}--func setupWorkspaceInstanceDescendantServiceTest(t *testing.T) (context.Context, *store.Store, string, string, string, string) {- t.Helper()- return setupInstanceDescendantServiceTest(t, nil)-}--func setupInstanceDescendantServiceTest(t *testing.T, instanceProjectID *string) (context.Context, *store.Store, string, string, string, string) {- t.Helper()- ctx := context.WithValue(context.Background(), common.WorkspaceIDContextKey, "default")- db, stores, _ := testcontainer.NewMetadataDB(t)- _, err := db.ExecContext(ctx, `- INSERT INTO workspace (resource_id) VALUES ('default');- INSERT INTO project (resource_id, workspace, name) VALUES ('project-a', 'default', 'Project A'), ('project-b', 'default', 'Project B');- `)- require.NoError(t, err)-- instanceID := "project-instance"- _, err = stores.CreateInstance(ctx, &store.InstanceMessage{- ResourceID: instanceID,- Workspace: "default",- ProjectID: instanceProjectID,- Metadata: &storepb.Instance{- DataSources: []*storepb.DataSource{{- Id: "admin",- Type: storepb.DataSourceType_ADMIN,- }},- Roles: []*storepb.InstanceRole{{- Name: "role-a",- }},- },- })- require.NoError(t, err)-- databaseName := "app"- databaseProjectID := "project-a"- _, err = stores.UpsertDatabase(ctx, &store.DatabaseMessage{- InstanceID: instanceID,- DatabaseName: databaseName,- ProjectID: databaseProjectID,- Metadata: &storepb.DatabaseMetadata{},- })- require.NoError(t, err)-- changelogID, err := stores.CreateChangelog(ctx, &store.ChangelogMessage{- InstanceID: instanceID,- DatabaseName: databaseName,- Payload: &storepb.ChangelogPayload{},- Status: store.ChangelogStatusDone,- })- require.NoError(t, err)- revision, err := stores.CreateRevision(ctx, &store.RevisionMessage{- InstanceID: instanceID,- DatabaseName: databaseName,- Version: "1",- Payload: &storepb.RevisionPayload{- SheetSha256: "sheet-a",- Type: storepb.SchemaChangeType_VERSIONED,- // The authoring-project stamp every runtime writer sets; the- // sheet name in responses is derived from it, so an unstamped- // revision carries no sheet name at all.- Project: databaseProjectID,- },- })- require.NoError(t, err)- return ctx, stores, instanceID, databaseName, changelogID, revision.ResourceID-}diff --git a/backend/api/v1/instance_service_test.go b/backend/api/v1/instance_service_test.goindex b36f56db21..4ac1dfaba5 100644--- a/backend/api/v1/instance_service_test.go+++ b/backend/api/v1/instance_service_test.go@@ -3,90 +3,24 @@ package v1import ("context""errors"- "fmt""log/slog""testing""time""connectrpc.com/connect""github.com/stretchr/testify/require"- "google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/common/testcontainer""github.com/bytebase/bytebase/backend/component/config""github.com/bytebase/bytebase/backend/component/sample"+ "github.com/bytebase/bytebase/backend/enterprise"storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1""github.com/bytebase/bytebase/backend/generated-go/v1/v1connect""github.com/bytebase/bytebase/backend/store")-func TestPrepareSampleProjectInstanceValidatesParentAndManager(t *testing.T) {- t.Parallel()- service := &InstanceService{}- _, err := service.PrepareSampleProjectInstance(context.Background(), connect.NewRequest(&v1pb.PrepareSampleProjectInstanceRequest{}))- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))-- ctx, stores, projectID, _, _ := setupProjectInstanceLifecycleAPITest(t)- service = &InstanceService{- store: stores,- licenseService: newInstanceServiceTestLicenseService(t, stores),- }- _, err = service.PrepareSampleProjectInstance(ctx, connect.NewRequest(&v1pb.PrepareSampleProjectInstanceRequest{- Parent: common.FormatProject(projectID),- }))- require.Equal(t, connect.CodeFailedPrecondition, connect.CodeOf(err))-}--//nolint:tparallel // Subtests share the parent's fixture.-func TestPrepareSampleProjectInstanceUsesConfiguredManagerInBothDeployments(t *testing.T) {- t.Parallel()- ctx, stores, projectID, _, _ := setupProjectInstanceLifecycleAPITest(t)- for _, saas := range []bool{false, true} {- t.Run(fmt.Sprintf("saas=%t", saas), func(t *testing.T) {- manager := &sampleManagerStub{instance: &store.InstanceMessage{- ResourceID: "sample-one",- ProjectID: &projectID,- Metadata: &storepb.Instance{Title: "Sample Project Instance"},- }}- service := &InstanceService{- store: stores,- profile: &config.Profile{SaaS: saas},- licenseService: newInstanceServiceTestLicenseService(t, stores),- sampleManager: manager,- }-- response, err := service.PrepareSampleProjectInstance(ctx, connect.NewRequest(&v1pb.PrepareSampleProjectInstanceRequest{- Parent: common.FormatProject(projectID),- }))- require.NoError(t, err)- require.Equal(t, common.FormatProjectInstance(projectID, "sample-one"), response.Msg.Name)- require.Equal(t, 1, manager.checkAvailableCalls)- require.Equal(t, []sample.PrepareRequest{{WorkspaceID: "default", ProjectID: projectID}}, manager.prepareRequests)- })- }-}--func TestPrepareSampleProjectInstanceChecksManagerAvailability(t *testing.T) {- t.Parallel()- ctx, stores, projectID, _, _ := setupProjectInstanceLifecycleAPITest(t)- manager := &sampleManagerStub{- checkAvailableErr: sample.NewFailure(sample.FailureUnavailable, errors.New("sample target unavailable")),- }- service := &InstanceService{- store: stores,- licenseService: newInstanceServiceTestLicenseService(t, stores),- sampleManager: manager,- }-- _, err := service.PrepareSampleProjectInstance(ctx, connect.NewRequest(&v1pb.PrepareSampleProjectInstanceRequest{- Parent: common.FormatProject(projectID),- }))- require.Equal(t, connect.CodeUnavailable, connect.CodeOf(err))- require.Equal(t, 1, manager.checkAvailableCalls)- require.Empty(t, manager.prepareRequests)-}-func TestSampleInstanceLifecycleHooksWrapMetadataUpdate(t *testing.T) {t.Parallel()ctx, stores, projectID, instanceID, _ := setupProjectInstanceLifecycleAPITest(t)@@ -130,83 +64,12 @@ func TestSampleInstanceLifecycleHooksWrapMetadataUpdate(t *testing.T) {require.Equal(t, []bool{true, false}, manager.lifecycleCalls)}-func TestPrepareSampleProjectInstanceRejectsConsumedEntitlementAfterProjectDeletion(t *testing.T) {- t.Parallel()- ctx, stores, projectID, _, _ := setupProjectInstanceLifecycleAPITest(t)- workspaceID := common.GetWorkspaceIDFromContext(ctx)- payload, err := protojson.Marshal(&storepb.SaaSSampleInstanceSetupPayload{- ProjectId: projectID,- InstanceId: "sample-deleted-project",- Title: "Sample Project Instance",- DatabaseName: "bb_sample_deleted_project",- RoleName: "bb_sample_role_deleted_project",- })- require.NoError(t, err)- _, _, err = stores.ReserveSampleInstanceSetup(ctx, &store.SampleInstanceSetupMessage{- WorkspaceID: workspaceID,- ReplicaID: "replica-a",- Payload: payload,- })- require.NoError(t, err)- _, err = stores.GetDB().ExecContext(ctx, `- UPDATE project- SET deleted = TRUE- WHERE workspace = $1 AND resource_id = $2- `, workspaceID, projectID)- require.NoError(t, err)- service := &InstanceService{- store: stores,- profile: &config.Profile{SaaS: true},- licenseService: newInstanceServiceTestLicenseService(t, stores),- sampleManager: &sampleManagerStub{},- }-- _, err = service.PrepareSampleProjectInstance(ctx, connect.NewRequest(&v1pb.PrepareSampleProjectInstanceRequest{- Parent: common.FormatProject(projectID),- }))- require.Equal(t, connect.CodeFailedPrecondition, connect.CodeOf(err))-}-func TestSampleProjectInstanceConnectErrorMapsUnknownFailure(t *testing.T) {t.Parallel()err := sampleProjectInstanceConnectError(errors.New("unexpected manager failure"))require.Equal(t, connect.CodeInternal, connect.CodeOf(err))}-func TestSampleProjectInstanceCapacityGuardMapsCapacityDenial(t *testing.T) {- t.Parallel()- ctx, stores, projectID, _, _ := setupProjectInstanceLifecycleAPITest(t)- for i := 0; i < 9; i++ {- _, err := stores.CreateInstance(ctx, &store.InstanceMessage{- ResourceID: fmt.Sprintf("capacity-instance-%d", i),- Workspace: "default",- ProjectID: &projectID,- Metadata: &storepb.Instance{- DataSources: []*storepb.DataSource{{Id: "admin", Type: storepb.DataSourceType_ADMIN}},- },- })- require.NoError(t, err)- }- service := &InstanceService{- store: stores,- profile: &config.Profile{SaaS: true},- licenseService: newInstanceServiceTestLicenseService(t, stores),- }- err := service.instanceCountGuard(ctx)- require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(err))-}--func TestSampleProjectInstanceCapacityGuardDoesNotConsumeActivationQuota(t *testing.T) {- t.Parallel()- ctx, stores, _, _, _ := setupProjectInstanceLifecycleAPITest(t)- service := &InstanceService{- store: stores,- profile: &config.Profile{SaaS: true},- licenseService: newInstanceServiceTestLicenseService(t, stores),- }- require.NoError(t, service.instanceCountGuard(ctx))-}-type sampleManagerStub struct {instance *store.InstanceMessageprepareErr error@@ -611,3 +474,49 @@ func TestApplyDataSourceUpdateMaskCredentials(t *testing.T) {require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))})}++func newInstanceServiceTestLicenseService(t *testing.T, stores *store.Store) *enterprise.LicenseService {+ t.Helper()+ licenseService, err := enterprise.NewLicenseService(common.ReleaseModeDev, stores, false, "")+ require.NoError(t, err)+ return licenseService+}++func setupProjectInstanceLifecycleAPITest(t *testing.T) (context.Context, *store.Store, string, string, string) {+ t.Helper()+ ctx := context.WithValue(context.Background(), common.WorkspaceIDContextKey, "default")+ db, stores, _ := testcontainer.NewMetadataDB(t)+ _, err := db.ExecContext(ctx, `+ INSERT INTO workspace (resource_id) VALUES ('default');+ INSERT INTO project (resource_id, workspace, name) VALUES+ ('project-a', 'default', 'Project A'),+ ('project-b', 'default', 'Project B');+ `)+ require.NoError(t, err)++ projectID := "project-a"+ instanceID := "project-instance"+ _, err = stores.CreateInstance(ctx, &store.InstanceMessage{+ ResourceID: instanceID,+ Workspace: "default",+ ProjectID: &projectID,+ Metadata: &storepb.Instance{+ Activation: true,+ DataSources: []*storepb.DataSource{{+ Id: "admin",+ Type: storepb.DataSourceType_ADMIN,+ }},+ },+ })+ require.NoError(t, err)++ databaseName := "app"+ _, err = stores.UpsertDatabase(ctx, &store.DatabaseMessage{+ InstanceID: instanceID,+ DatabaseName: databaseName,+ ProjectID: projectID,+ Metadata: &storepb.DatabaseMetadata{},+ })+ require.NoError(t, err)+ return ctx, stores, projectID, instanceID, databaseName+}diff --git a/backend/api/v1/interactive_project_database_test.go b/backend/api/v1/interactive_project_database_test.godeleted file mode 100644index 78be1becbc..0000000000--- a/backend/api/v1/interactive_project_database_test.go+++ /dev/null@@ -1,182 +0,0 @@-package v1--import (- "context"- "testing"-- "github.com/bytebase/bytebase/backend/common/testcontainer"-- "connectrpc.com/connect"- "github.com/stretchr/testify/require"-- "google.golang.org/protobuf/types/known/fieldmaskpb"-- "github.com/bytebase/bytebase/backend/common"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"- "github.com/bytebase/bytebase/backend/store"-)--func TestBOT36SQLPrepareRelatedMessageRequiresCanonicalActiveOwner(t *testing.T) {- t.Parallel()- ctx, stores, projectID, instanceID, databaseName := setupBOT36ProjectDatabase(t)- service := NewSQLService(stores, nil, nil, nil, nil, nil)-- _, _, _, err := service.prepareRelatedMessage(ctx, common.FormatDatabase(instanceID, databaseName))- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))-- _, _, _, err = service.prepareRelatedMessage(ctx, common.FormatProjectDatabase("other-project", instanceID, databaseName))- require.Equal(t, connect.CodeNotFound, connect.CodeOf(err))-- _, _, _, err = service.prepareRelatedMessage(ctx, common.FormatProjectDatabase(projectID, instanceID, databaseName))- require.NoError(t, err)- _, _, _, err = service.prepareRelatedMessage(ctx, common.FormatDatabase("workspace-instance", "shared"))- require.NoError(t, err)-- require.NoError(t, stores.UpdateProjects(ctx, &store.UpdateProjectMessage{- ResourceID: projectID,- Workspace: "default",- Delete: new(true),- }))- _, _, _, err = service.prepareRelatedMessage(ctx, common.FormatProjectDatabase(projectID, instanceID, databaseName))- require.Equal(t, connect.CodeNotFound, connect.CodeOf(err))-}--func TestBOT36SavedQueryDatabasesUseCanonicalOwningProjectNames(t *testing.T) {- t.Parallel()- ctx, stores, projectID, instanceID, databaseName := setupBOT36ProjectDatabase(t)- service := NewSavedQueryService(stores, nil)-- _, err := service.validateSavedQueryDatabase(ctx, projectID, common.FormatDatabase(instanceID, databaseName))- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))-- _, err = service.validateSavedQueryDatabase(ctx, projectID, common.FormatProjectDatabase("other-project", instanceID, databaseName))- require.Equal(t, connect.CodeNotFound, connect.CodeOf(err))-- canonical, err := service.validateSavedQueryDatabase(ctx, projectID, common.FormatProjectDatabase(projectID, instanceID, databaseName))- require.NoError(t, err)- require.Equal(t, common.FormatProjectDatabase(projectID, instanceID, databaseName), canonical)-- // The stored canonical name round-trips through the API shape without- // re-resolution — a dangling reference degrades instead of erroring.- savedQuery := convertToAPISavedQuery(&store.SavedQueryMessage{- ProjectID: projectID,- ResourceID: "saved-query-a",- Database: canonical,- })- require.Equal(t, canonical, savedQuery.Database)-- workspaceCanonical, err := service.validateSavedQueryDatabase(ctx, projectID, common.FormatDatabase("workspace-instance", "shared"))- require.NoError(t, err)- require.Equal(t, common.FormatDatabase("workspace-instance", "shared"), workspaceCanonical)-}--func TestUpdateSavedQueryKeepsDanglingDatabaseReference(t *testing.T) {- t.Parallel()- ctx, stores, projectID, _, _ := setupBOT36ProjectDatabase(t)- service := NewSavedQueryService(stores, nil)-- // The database reference is a soft link: an autosave that re-sends the- // stored (now dangling) value must not fail validation — that would- // brick content saves after the database is deleted or transferred.- dangling := common.FormatDatabase("deleted-instance", "gone")- created, err := stores.CreateSavedQuery(ctx, &store.SavedQueryMessage{- ProjectID: projectID,- Creator: "[email protected]",- Title: "dangling",- Statement: "SELECT 1;",- Database: dangling,- })- require.NoError(t, err)-- updated, err := service.UpdateSavedQuery(ctx, connect.NewRequest(&v1pb.UpdateSavedQueryRequest{- SavedQuery: &v1pb.SavedQuery{- Name: common.FormatSavedQuery(projectID, created.ResourceID),- Database: dangling,- Content: []byte("SELECT 2;"),- },- UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"database", "content"}},- }))- require.NoError(t, err)- require.Equal(t, dangling, updated.Msg.Database)- require.Equal(t, []byte("SELECT 2;"), updated.Msg.Content)-- // An explicit change to a nonexistent database still fails hard.- _, err = service.UpdateSavedQuery(ctx, connect.NewRequest(&v1pb.UpdateSavedQueryRequest{- SavedQuery: &v1pb.SavedQuery{- Name: common.FormatSavedQuery(projectID, created.ResourceID),- Database: common.FormatDatabase("another-missing", "db"),- },- UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"database"}},- }))- require.Equal(t, connect.CodeNotFound, connect.CodeOf(err))-}--func TestBOT36AccessGrantTargetsRequireCanonicalOwningProject(t *testing.T) {- t.Parallel()- ctx, stores, projectID, instanceID, databaseName := setupBOT36ProjectDatabase(t)- service := NewAccessGrantService(stores, nil, nil, nil)-- _, err := service.validateAccessGrantTargets(ctx, projectID, []string{common.FormatDatabase(instanceID, databaseName)})- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))-- _, err = service.validateAccessGrantTargets(ctx, projectID, []string{common.FormatProjectDatabase(projectID, instanceID, databaseName), common.FormatProjectDatabase("other-project", instanceID, databaseName)})- require.Equal(t, connect.CodeNotFound, connect.CodeOf(err))-- instance, err := service.validateAccessGrantTargets(ctx, projectID, []string{common.FormatProjectDatabase(projectID, instanceID, databaseName)})- require.NoError(t, err)- require.Equal(t, instanceID, instance.ResourceID)- instance, err = service.validateAccessGrantTargets(ctx, projectID, []string{common.FormatDatabase("workspace-instance", "shared")})- require.NoError(t, err)- require.Equal(t, "workspace-instance", instance.ResourceID)-}--func setupBOT36ProjectDatabase(t *testing.T) (context.Context, *store.Store, string, string, string) {- t.Helper()- ctx := context.WithValue(context.Background(), common.WorkspaceIDContextKey, "default")- ctx = context.WithValue(ctx, common.UserContextKey, &store.UserMessage{Email: "[email protected]"})- db, stores, _ := testcontainer.NewMetadataDB(t)- _, err := db.ExecContext(ctx, `- INSERT INTO workspace (resource_id) VALUES ('default');- INSERT INTO project (resource_id, workspace, name) VALUES ('project-a', 'default', 'Project A'), ('other-project', 'default', 'Other Project');- `)- require.NoError(t, err)-- projectID := "project-a"- instanceID := "instance-a"- _, err = stores.CreateInstance(ctx, &store.InstanceMessage{- ResourceID: instanceID,- Workspace: "default",- ProjectID: &projectID,- Metadata: &storepb.Instance{DataSources: []*storepb.DataSource{{- Id: "admin",- Type: storepb.DataSourceType_ADMIN,- }}},- })- require.NoError(t, err)- databaseName := "app"- _, err = stores.UpsertDatabase(ctx, &store.DatabaseMessage{- InstanceID: instanceID,- DatabaseName: databaseName,- ProjectID: projectID,- Metadata: &storepb.DatabaseMetadata{},- })- require.NoError(t, err)- _, err = stores.CreateInstance(ctx, &store.InstanceMessage{- ResourceID: "workspace-instance",- Workspace: "default",- Metadata: &storepb.Instance{DataSources: []*storepb.DataSource{{- Id: "admin",- Type: storepb.DataSourceType_ADMIN,- }}},- })- require.NoError(t, err)- _, err = stores.UpsertDatabase(ctx, &store.DatabaseMessage{- InstanceID: "workspace-instance",- DatabaseName: "shared",- ProjectID: projectID,- Metadata: &storepb.DatabaseMetadata{},- })- require.NoError(t, err)- return ctx, stores, projectID, instanceID, databaseName-}diff --git a/backend/api/v1/issue_service.go b/backend/api/v1/issue_service.goindex ef45a8bcfb..d5cfa30be3 100644--- a/backend/api/v1/issue_service.go+++ b/backend/api/v1/issue_service.go@@ -568,17 +568,25 @@ func (s *IssueService) findLinkedIssueForCreate(ctx context.Context, issue *storif err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to find issue by plan"))}+ return linkedIssueForCreate(*issue.PlanUID, existing, issue)+}++// linkedIssueForCreate decides what creating issue means when its plan,+// planUID, already has one. A submitted issue is final for the plan. A draft is the creator's+// own: the same creator asking again gets it back, a submission must go+// through it, and another creator is told the plan is taken.+func linkedIssueForCreate(planUID int64, existing, issue *store.IssueMessage) (*store.IssueMessage, error) {if existing == nil {return nil, nil}if !existing.Payload.GetDraft() {- return nil, connect.NewError(connect.CodeAlreadyExists, errors.Errorf("plan %d already has a non-draft issue", *issue.PlanUID))+ return nil, connect.NewError(connect.CodeAlreadyExists, errors.Errorf("plan %d already has a non-draft issue", planUID))}if !issue.Payload.GetDraft() {- return nil, connect.NewError(connect.CodeFailedPrecondition, errors.Errorf("plan %d already has a draft issue; update or submit the existing draft instead of creating another issue", *issue.PlanUID))+ return nil, connect.NewError(connect.CodeFailedPrecondition, errors.Errorf("plan %d already has a draft issue; update or submit the existing draft instead of creating another issue", planUID))}if existing.CreatorEmail != issue.CreatorEmail {- return nil, connect.NewError(connect.CodeAlreadyExists, errors.Errorf("plan %d already has a draft issue", *issue.PlanUID))+ return nil, connect.NewError(connect.CodeAlreadyExists, errors.Errorf("plan %d already has a draft issue", planUID))}return existing, nil}@@ -1000,49 +1008,13 @@ func (s *IssueService) UpdateIssue(ctx context.Context, req *connect.Request[v1preturn nil, mapIssueSubmissionError(err)}issue = result.Issue- for _, event := range result.Events {- switch event := event.(type) {- case review.IssueTitleUpdatedEvent:- issueCommentCreates = append(issueCommentCreates, &store.IssueCommentMessage{- IssueUID: issue.UID,- Payload: &storepb.IssueCommentPayload{- Event: &storepb.IssueCommentPayload_IssueUpdate_{- IssueUpdate: &storepb.IssueCommentPayload_IssueUpdate{- FromTitle: &event.FromTitle,- ToTitle: &event.ToTitle,- },- },- },- })- case review.IssueDescriptionUpdatedEvent:- issueCommentCreates = append(issueCommentCreates, &store.IssueCommentMessage{- IssueUID: issue.UID,- Payload: &storepb.IssueCommentPayload{- Event: &storepb.IssueCommentPayload_IssueUpdate_{- IssueUpdate: &storepb.IssueCommentPayload_IssueUpdate{- FromDescription: &event.FromDescription,- ToDescription: &event.ToDescription,- },- },- },- })- case review.IssueLabelsUpdatedEvent:- issueCommentCreates = append(issueCommentCreates, &store.IssueCommentMessage{- IssueUID: issue.UID,- Payload: &storepb.IssueCommentPayload{- Event: &storepb.IssueCommentPayload_IssueUpdate_{- IssueUpdate: &storepb.IssueCommentPayload_IssueUpdate{- FromLabels: event.FromLabels,- ToLabels: event.ToLabels,- },- },- },- })- case review.ApprovalCheckEvent:- s.bus.ApprovalCheckChan <- bus.IssueRef{ProjectID: issue.ProjectID, UID: issue.UID}- default:- return nil, connect.NewError(connect.CodeInternal, errors.Errorf("unexpected issue metadata event %T", event))- }+ comments, approvalCheck, err := issueCommentsForMetadataEvents(issue.UID, result.Events)+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, err)+ }+ issueCommentCreates = append(issueCommentCreates, comments...)+ if approvalCheck {+ s.bus.ApprovalCheckChan <- bus.IssueRef{ProjectID: issue.ProjectID, UID: issue.UID}}}@@ -1107,6 +1079,39 @@ func newIssueLabelsUpdateComment(issueUID int64, fromLabels, toLabels []string)}}+// issueCommentsForMetadataEvents turns the events a metadata patch produced+// into the audit comments the issue timeline shows, and reports whether the+// patch reset the approval, which is what asks for the approval check to run+// again. Which patches reset the approval is the review workflow's decision;+// this is only its record.+func issueCommentsForMetadataEvents(issueUID int64, events []review.Event) ([]*store.IssueCommentMessage, bool, error) {+ var comments []*store.IssueCommentMessage+ approvalCheck := false+ for _, event := range events {+ var update *storepb.IssueCommentPayload_IssueUpdate+ switch event := event.(type) {+ case review.IssueTitleUpdatedEvent:+ update = &storepb.IssueCommentPayload_IssueUpdate{FromTitle: &event.FromTitle, ToTitle: &event.ToTitle}+ case review.IssueDescriptionUpdatedEvent:+ update = &storepb.IssueCommentPayload_IssueUpdate{FromDescription: &event.FromDescription, ToDescription: &event.ToDescription}+ case review.IssueLabelsUpdatedEvent:+ update = &storepb.IssueCommentPayload_IssueUpdate{FromLabels: event.FromLabels, ToLabels: event.ToLabels}+ case review.ApprovalCheckEvent:+ approvalCheck = true+ continue+ default:+ return nil, false, errors.Errorf("unexpected issue metadata event %T", event)+ }+ comments = append(comments, &store.IssueCommentMessage{+ IssueUID: issueUID,+ Payload: &storepb.IssueCommentPayload{+ Event: &storepb.IssueCommentPayload_IssueUpdate_{IssueUpdate: update},+ },+ })+ }+ return comments, approvalCheck, nil+}+func mapIssueSubmissionError(err error) error {var workflowErr *review.Errorif !errors.As(err, &workflowErr) {diff --git a/backend/api/v1/issue_service_converter.go b/backend/api/v1/issue_service_converter.goindex 7b812eea11..4e94686ed8 100644--- a/backend/api/v1/issue_service_converter.go+++ b/backend/api/v1/issue_service_converter.go@@ -66,62 +66,11 @@ func (*IssueService) convertToIssue(issue *store.IssueMessage) (*v1pb.Issue, err}issueV1.Approvers = append(issueV1.Approvers, convertedApprover)}- issueV1.ApprovalStatus = computeApprovalStatus(approval)+ issueV1.ApprovalStatus = store.ComputeApprovalStatus(approval)return issueV1, nil}-func computeApprovalStatus(approval *storepb.IssuePayloadApproval) v1pb.ApprovalStatus {- // If approval finding is not done, status is checking- // Note: approval.GetApprovalFindingDone() returns false when approval is nil- if !approval.GetApprovalFindingDone() {- return v1pb.ApprovalStatus_CHECKING- }-- // If no approval template, approval is skipped (not required)- if approval.GetApprovalTemplate() == nil {- return v1pb.ApprovalStatus_SKIPPED- }-- approvalTemplate := approval.GetApprovalTemplate()- approvers := approval.GetApprovers()- totalSteps := len(approvalTemplate.GetFlow().GetRoles())-- // If no approvers are assigned yet, it's pending- if len(approvers) == 0 {- return v1pb.ApprovalStatus_PENDING- }-- // Check approver statuses- for _, approver := range approvers {- if approver.GetStatus() == storepb.IssuePayloadApproval_Approver_REJECTED {- // Short-circuit: if any approver rejected, overall status is rejected- return v1pb.ApprovalStatus_REJECTED- }- }-- // Check if all steps are completed- // Each approver corresponds to one step in the approval flow- // All steps are approved if:- // 1. Number of approvers equals number of steps- // 2. All approvers have APPROVED status- if len(approvers) >= totalSteps {- allApproved := true- for _, approver := range approvers {- if approver.GetStatus() != storepb.IssuePayloadApproval_Approver_APPROVED {- allApproved = false- break- }- }- if allApproved {- return v1pb.ApprovalStatus_APPROVED- }- }-- // Otherwise, approval is pending (more steps to complete or waiting for approvals)- return v1pb.ApprovalStatus_PENDING-}-func convertToIssueType(t storepb.Issue_Type) v1pb.Issue_Type {switch t {case storepb.Issue_DATABASE_CHANGE:diff --git a/backend/api/v1/issue_service_test.go b/backend/api/v1/issue_service_test.goindex e6e6cbb5ed..e288424d6f 100644--- a/backend/api/v1/issue_service_test.go+++ b/backend/api/v1/issue_service_test.go@@ -2,17 +2,13 @@ package v1import ("context"- "fmt"- "slices""testing""github.com/bytebase/bytebase/backend/common/testcontainer""connectrpc.com/connect"- "github.com/stretchr/testify/assert""github.com/stretchr/testify/require""google.golang.org/protobuf/proto"- "google.golang.org/protobuf/types/known/fieldmaskpb""github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/component/bus"@@ -26,228 +22,105 @@ import (_ "github.com/bytebase/bytebase/backend/plugin/db/pg")-func TestUpdateIssueCommentRejectsEmptyComment(t *testing.T) {+// TestIssueCommentsForMetadataEvents pins the record a metadata patch leaves+// on the issue timeline: one audit comment per changed field carrying the+// transition, and an approval check only when the workflow says the patch+// reset the approval. Which patches reset it is pinned in component/review.+func TestIssueCommentsForMetadataEvents(t *testing.T) {t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)- _, issue := createIssueServiceApprovalIssue(ctx, t, stores)- parent := common.FormatIssue(issue.ProjectID, issue.UID)-- created, err := service.CreateIssueComment(ctx, connect.NewRequest(&v1pb.CreateIssueCommentRequest{- Parent: parent,- IssueComment: &v1pb.IssueComment{- Comment: "keep me",- },- }))- require.NoError(t, err)-- _, err = service.UpdateIssueComment(ctx, connect.NewRequest(&v1pb.UpdateIssueCommentRequest{- Parent: parent,- IssueComment: &v1pb.IssueComment{- Name: created.Msg.Name,- Comment: "",- },- UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"comment"}},- }))- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))-- comments, err := stores.ListIssueComment(ctx, &store.FindIssueCommentMessage{- ProjectID: issue.ProjectID,- IssueUID: &issue.UID,- })- require.NoError(t, err)- require.Len(t, comments, 1)- require.Equal(t, "keep me", comments[0].Payload.GetComment())-}--// TestListIssueCommentsExcludesReplies pins the activity timeline to events-// and root comments: the v1 message cannot represent a reply, so a reply must-// neither render as a top-level row nor consume a page slot.-func TestListIssueCommentsExcludesReplies(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)- _, issue := createIssueServiceApprovalIssue(ctx, t, stores)- parent := common.FormatIssue(issue.ProjectID, issue.UID)-- // Only an anchored comment starts a thread, and v1 cannot set the anchor- // yet, so the root is created through the store.- root, err := stores.CreateIssueComments(ctx, "users/[email protected]", &store.IssueCommentMessage{- ProjectID: issue.ProjectID,- IssueUID: issue.UID,- Payload: &storepb.IssueCommentPayload{- Comment: "root",- StatementAnchor: &storepb.IssueCommentPayload_StatementAnchor{- SpecId: "spec-1",- SheetSha256: "0be1f01d6ee8e6f6c6a2ce9b418ba10ea9d16c9b9bfae5548b8fa0e26c04a5e0",- StartPosition: &storepb.Position{Line: 1},- EndPosition: &storepb.Position{Line: 1},- },- },+ comments, approvalCheck, err := issueCommentsForMetadataEvents(7, []review.Event{+ review.IssueTitleUpdatedEvent{FromTitle: "issue-a", ToTitle: "renamed issue"},+ review.IssueDescriptionUpdatedEvent{FromDescription: "", ToDescription: "updated description"},+ review.IssueLabelsUpdatedEvent{FromLabels: []string{"environment:prod"}, ToLabels: []string{"environment:staging"}},+ review.ApprovalCheckEvent{},})require.NoError(t, err)- _, err = stores.CreateIssueCommentReply(ctx, "users/[email protected]", &store.IssueCommentMessage{- ProjectID: issue.ProjectID,- IssueUID: issue.UID,- ParentID: &root.ResourceID,- Payload: &storepb.IssueCommentPayload{Comment: "reply body"},- })- require.NoError(t, err)- rootName := fmt.Sprintf("%s/%s%s", parent, common.IssueCommentNamePrefix, root.ResourceID)-- resp, err := service.ListIssueComments(ctx, connect.NewRequest(&v1pb.ListIssueCommentsRequest{- Parent: parent,- }))- require.NoError(t, err)- names := make([]string, 0, len(resp.Msg.IssueComments))- for _, comment := range resp.Msg.IssueComments {- require.NotEqual(t, "reply body", comment.Comment, "replies must not appear in the activity timeline")- names = append(names, comment.Name)+ require.True(t, approvalCheck, "a reset approval is checked again")+ require.Len(t, comments, 3)+ for _, comment := range comments {+ require.EqualValues(t, 7, comment.IssueUID)}- require.Contains(t, names, rootName, "the thread root stays in the timeline")-}+ require.Equal(t, "issue-a", comments[0].Payload.GetIssueUpdate().GetFromTitle())+ require.Equal(t, "renamed issue", comments[0].Payload.GetIssueUpdate().GetToTitle())+ require.NotNil(t, comments[1].Payload.GetIssueUpdate().FromDescription, "an empty previous description is recorded as empty, not omitted")+ require.Equal(t, "updated description", comments[1].Payload.GetIssueUpdate().GetToDescription())+ require.Equal(t, []string{"environment:prod"}, comments[2].Payload.GetIssueUpdate().GetFromLabels())+ require.Equal(t, []string{"environment:staging"}, comments[2].Payload.GetIssueUpdate().GetToLabels())-func TestUpdateIssueLabelsResetsApprovalBeforeRollout(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)- _, issue := createIssueServiceApprovalIssue(ctx, t, stores)-- updateIssueLabels(ctx, t, service, issue, []string{"environment:staging"})-- got := getIssueForTest(ctx, t, stores, issue.UID)- require.Equal(t, []string{"environment:staging"}, got.Payload.GetLabels())- require.False(t, got.Payload.GetApproval().GetApprovalFindingDone())- require.EqualValues(t, 2, got.Payload.GetApproval().GetApprovalInputVersion())-}--func TestUpdateIssueLabelsClearedBeforeRolloutResetsApproval(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)- _, issue := createIssueServiceApprovalIssue(ctx, t, stores)-- updateIssueLabels(ctx, t, service, issue, nil)-- got := getIssueForTest(ctx, t, stores, issue.UID)- require.Empty(t, got.Payload.GetLabels())- require.False(t, got.Payload.GetApproval().GetApprovalFindingDone())- require.EqualValues(t, 2, got.Payload.GetApproval().GetApprovalInputVersion())- require.Len(t, service.bus.ApprovalCheckChan, 1)-}--func TestUpdateIssueLabelsDoesNotResetApprovalAfterRollout(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)- plan, issue := createIssueServiceApprovalIssue(ctx, t, stores)-- _, err := review.NewWorkflow(stores).CreateRollout(ctx, review.CreateRolloutInput{- Workspace: "default",- ProjectID: "project-a",- PlanUID: plan.UID,- BuildTasks: func(context.Context, *store.PlanMessage, *store.ProjectMessage) ([]*store.TaskMessage, error) {- return nil, nil- },- })+ comments, approvalCheck, err = issueCommentsForMetadataEvents(7, nil)require.NoError(t, err)+ require.Empty(t, comments, "a no-op patch leaves no audit comment")+ require.False(t, approvalCheck)- updateIssueLabels(ctx, t, service, issue, []string{"environment:staging"})-- got := getIssueForTest(ctx, t, stores, issue.UID)- require.Equal(t, []string{"environment:staging"}, got.Payload.GetLabels())- require.True(t, got.Payload.GetApproval().GetApprovalFindingDone())- require.EqualValues(t, 2, got.Payload.GetApproval().GetApprovalInputVersion())- require.Len(t, service.bus.ApprovalCheckChan, 0)+ _, _, err = issueCommentsForMetadataEvents(7, []review.Event{review.SubmittedEvent{}})+ require.Error(t, err, "an event this method does not know how to record is a bug, not a silent drop")}-func TestMixedIssuePatchCommitsWithAuditComments(t *testing.T) {- t.Parallel()+// TestBuildIssueMessageRejectsBeforeAnyLookup pins the request shapes creation+// refuses on the request alone — a nil store proves nothing was read: the+// retired DATABASE_EXPORT type that legacy clients still send as wire value 3,+// a draft of anything but a database change, and a database-change draft+// naming no plan.+func TestBuildIssueMessageRejectsBeforeAnyLookup(t *testing.T) {ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)- _, issue := createIssueServiceApprovalIssue(ctx, t, stores)-- response, err := service.UpdateIssue(ctx, connect.NewRequest(&v1pb.UpdateIssueRequest{- Issue: &v1pb.Issue{- Name: common.FormatIssue(issue.ProjectID, issue.UID),- Title: "renamed issue",- Description: "updated description",- Labels: []string{"environment:staging"},- },- UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"title", "description", "labels"}},- }))- require.NoError(t, err)- require.Equal(t, "renamed issue", response.Msg.Title)- require.Equal(t, "updated description", response.Msg.Description)- require.Equal(t, []string{"environment:staging"}, response.Msg.Labels)-- got := getIssueForTest(ctx, t, stores, issue.UID)- require.False(t, got.Payload.GetApproval().GetApprovalFindingDone())- require.Len(t, service.bus.ApprovalCheckChan, 1)- comments, err := stores.ListIssueComment(ctx, &store.FindIssueCommentMessage{- ProjectID: issue.ProjectID,- IssueUID: &issue.UID,- })- require.NoError(t, err)- require.Len(t, comments, 3)- var titleAudit, descriptionAudit, labelsAudit bool- for _, comment := range comments {- update := comment.Payload.GetIssueUpdate()- require.NotNil(t, update)- switch {- case update.FromTitle != nil:- titleAudit = true- require.Equal(t, "issue-a", update.GetFromTitle())- require.Equal(t, "renamed issue", update.GetToTitle())- case update.FromDescription != nil:- descriptionAudit = true- require.Empty(t, update.GetFromDescription())- require.Equal(t, "updated description", update.GetToDescription())- default:- labelsAudit = true- require.Equal(t, []string{"environment:prod"}, update.GetFromLabels())- require.Equal(t, []string{"environment:staging"}, update.GetToLabels())- }+ service := &IssueService{}+ project := &store.ProjectMessage{ResourceID: "project-a", Setting: &storepb.Project{}}+ for _, tc := range []struct {+ name string+ issue *v1pb.Issue+ want string+ }{+ {"retired export type", &v1pb.Issue{Title: "legacy export issue", Type: v1pb.Issue_Type(3)}, "unknown issue type"},+ {"non-database draft", &v1pb.Issue{Title: "role request", Type: v1pb.Issue_ROLE_GRANT, Draft: true}, "draft issues must be database change issues"},+ {"draft without a plan", &v1pb.Issue{Type: v1pb.Issue_DATABASE_CHANGE, Draft: true}, "plan is required"},+ } {+ t.Run(tc.name, func(t *testing.T) {+ _, err := service.buildIssueMessage(ctx, project, "[email protected]", &v1pb.CreateIssueRequest{Parent: "projects/project-a", Issue: tc.issue}, nil)+ require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))+ require.ErrorContains(t, err, tc.want)+ })}- require.True(t, titleAudit)- require.True(t, descriptionAudit)- require.True(t, labelsAudit)}-func TestIssueMetadataNoopDoesNotCreateAuditComments(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)- _, issue := createIssueServiceApprovalIssue(ctx, t, stores)-- _, err := service.UpdateIssue(ctx, connect.NewRequest(&v1pb.UpdateIssueRequest{- Issue: &v1pb.Issue{- Name: common.FormatIssue(issue.ProjectID, issue.UID),- Title: " issue-a ",- Description: "",- Labels: []string{" environment:prod ", "environment:prod"},- },- UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"title", "description", "labels"}},- }))- require.NoError(t, err)-- got := getIssueForTest(ctx, t, stores, issue.UID)- require.Equal(t, issue.UpdatedAt, got.UpdatedAt)- require.True(t, got.Payload.GetApproval().GetApprovalFindingDone())- require.Empty(t, service.bus.ApprovalCheckChan)- comments, err := stores.ListIssueComment(ctx, &store.FindIssueCommentMessage{- ProjectID: issue.ProjectID,- IssueUID: &issue.UID,- })- require.NoError(t, err)- require.Empty(t, comments)+// TestLinkedIssueForCreate pins what creating an issue means when its plan+// already has one. Idempotent draft creation and the draft-versus-rollout+// ordering are pinned against the database in backend/store+// (TestCreateDraftIssueIsIdempotent, TestCreateDraftAndRolloutAreSerialized).+func TestLinkedIssueForCreate(t *testing.T) {+ draftBy := func(creator string) *store.IssueMessage {+ return &store.IssueMessage{CreatorEmail: creator, Payload: &storepb.Issue{Draft: true}}+ }+ submittedBy := func(creator string) *store.IssueMessage {+ return &store.IssueMessage{CreatorEmail: creator, Payload: &storepb.Issue{}}+ }+ for _, tc := range []struct {+ name string+ existing *store.IssueMessage+ incoming *store.IssueMessage+ wantCode connect.Code+ reuse bool+ }{+ {"an unlinked plan is free", nil, draftBy("[email protected]"), 0, false},+ {"a submitted issue is final for the plan", submittedBy("[email protected]"), draftBy("[email protected]"), connect.CodeAlreadyExists, false},+ {"a submission goes through the existing draft", draftBy("[email protected]"), submittedBy("[email protected]"), connect.CodeFailedPrecondition, false},+ {"another creator's draft is not exposed", draftBy("[email protected]"), draftBy("[email protected]"), connect.CodeAlreadyExists, false},+ {"the same creator gets the draft back", draftBy("[email protected]"), draftBy("[email protected]"), 0, true},+ } {+ t.Run(tc.name, func(t *testing.T) {+ got, err := linkedIssueForCreate(101, tc.existing, tc.incoming)+ if tc.wantCode != 0 {+ require.Equal(t, tc.wantCode, connect.CodeOf(err))+ require.Nil(t, got)+ return+ }+ require.NoError(t, err)+ if tc.reuse {+ require.Same(t, tc.existing, got)+ } else {+ require.Nil(t, got)+ }+ })+ }}func TestApproveIssueFailsClosedWhenIAMLookupFails(t *testing.T) {@@ -364,18 +237,14 @@ func TestCreateRolloutAndPendingTasksRejectsDraft(t *testing.T) {ResourceID: "project-a",Setting: &storepb.Project{RequireIssueApproval: false},}, []*store.TaskMessage{})+ require.ErrorIs(t, err, errDraftIssueNotSubmitted)- gotPlan, getPlanErr := stores.GetPlan(ctx, &store.FindPlanMessage{- ProjectID: "project-a",- UID: &plan.UID,- })- require.NoError(t, getPlanErr)- require.NotNil(t, gotPlan)+ gotPlan, err := stores.GetPlan(ctx, &store.FindPlanMessage{ProjectID: "project-a", UID: &plan.UID})+ require.NoError(t, err)+ require.False(t, gotPlan.Config.GetHasRollout())gotIssue := getIssueForTest(ctx, t, stores, draft.UID)- assert.Error(t, err)- assert.False(t, gotPlan.Config.GetHasRollout())- assert.Equal(t, storepb.Issue_OPEN, gotIssue.Status)- assert.True(t, gotIssue.Payload.GetDraft())+ require.Equal(t, storepb.Issue_OPEN, gotIssue.Status)+ require.True(t, gotIssue.Payload.GetDraft())}func TestCreateRolloutAndPendingTasksRejectsPersistedDraftWithStaleIssueSnapshot(t *testing.T) {@@ -393,757 +262,84 @@ func TestCreateRolloutAndPendingTasksRejectsPersistedDraftWithStaleIssueSnapshotResourceID: "project-a",Setting: &storepb.Project{RequireIssueApproval: false},}, []*store.TaskMessage{})-require.ErrorIs(t, err, errDraftIssueNotSubmitted)- gotPlan, getPlanErr := stores.GetPlan(ctx, &store.FindPlanMessage{- ProjectID: "project-a",- UID: &plan.UID,- })- require.NoError(t, getPlanErr)++ gotPlan, err := stores.GetPlan(ctx, &store.FindPlanMessage{ProjectID: "project-a", UID: &plan.UID})+ require.NoError(t, err)require.False(t, gotPlan.Config.GetHasRollout())gotIssue := getIssueForTest(ctx, t, stores, draft.UID)require.Equal(t, storepb.Issue_OPEN, gotIssue.Status)require.True(t, gotIssue.Payload.GetDraft())}-func TestCreateDraftIssueRejectedAfterRollout(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)- plan, err := stores.CreatePlan(ctx, &store.PlanMessage{- ProjectID: "project-a",- Name: "rolled out plan",- Config: &storepb.PlanConfig{- Specs: []*storepb.PlanConfig_Spec{{- Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{- ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{- SheetSha256: "sheet",- },- },- }},- },- }, "[email protected]")- require.NoError(t, err)- require.NoError(t, CreateRolloutAndPendingTasks(ctx, stores, "[email protected]", plan, nil, &store.ProjectMessage{- ResourceID: "project-a",- Setting: &storepb.Project{RequireIssueApproval: false},- }, []*store.TaskMessage{}))-- _, err = service.CreateIssue(ctx, connect.NewRequest(&v1pb.CreateIssueRequest{- Parent: "projects/project-a",- Issue: &v1pb.Issue{- Type: v1pb.Issue_DATABASE_CHANGE,- Plan: common.FormatPlan("project-a", plan.UID),- Draft: true,- },- }))-- require.Equal(t, connect.CodeFailedPrecondition, connect.CodeOf(err))- linkedIssue, getErr := stores.GetIssue(ctx, &store.FindIssueMessage{+func getIssueForTest(ctx context.Context, t *testing.T, stores *store.Store, issueUID int64) *store.IssueMessage {+ t.Helper()+ got, err := stores.GetIssue(ctx, &store.FindIssueMessage{ProjectIDs: []string{"project-a"},- PlanUID: &plan.UID,+ UID: &issueUID,})- require.NoError(t, getErr)- require.Nil(t, linkedIssue)-}--func TestUpdateIssueLabelsNoopDoesNotResetApproval(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)- _, issue := createIssueServiceApprovalIssue(ctx, t, stores)-- updateIssueLabels(ctx, t, service, issue, []string{" environment:prod ", "environment:prod"})-- got := getIssueForTest(ctx, t, stores, issue.UID)- require.Equal(t, []string{"environment:prod"}, got.Payload.GetLabels())- require.True(t, got.Payload.GetApproval().GetApprovalFindingDone())- require.EqualValues(t, 2, got.Payload.GetApproval().GetApprovalInputVersion())- require.Len(t, service.bus.ApprovalCheckChan, 0)+ require.NoError(t, err)+ require.NotNil(t, got)+ return got}-func TestUpdateIssueLabelsDoesNotResetCreateDatabaseApproval(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)- _, issue := createIssueServiceCreateDatabaseApprovalIssue(ctx, t, stores)-- updateIssueLabels(ctx, t, service, issue, []string{"environment:staging"})-- got := getIssueForTest(ctx, t, stores, issue.UID)- require.Equal(t, []string{"environment:staging"}, got.Payload.GetLabels())- require.True(t, got.Payload.GetApproval().GetApprovalFindingDone())- require.EqualValues(t, 2, got.Payload.GetApproval().GetApprovalInputVersion())- require.Len(t, service.bus.ApprovalCheckChan, 0)-}+func setupIssueServiceTestStore(ctx context.Context, t *testing.T) *store.Store {+ t.Helper()-func TestUpdateIssueLabelsOnDraftPreservesApproval(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)- _, issue := createIssueServiceApprovalIssue(ctx, t, stores)+ db, stores, _ := testcontainer.NewMetadataDB(t)- draft, err := stores.UpdateIssue(ctx, issue.ProjectID, issue.UID, &store.UpdateIssueMessage{- PayloadUpsert: &storepb.Issue{Draft: true},- })+ _, err := db.ExecContext(ctx, `+ INSERT INTO workspace (resource_id) VALUES ('default');+ INSERT INTO principal (name, email, password_hash) VALUES ('creator', '[email protected]', 'unused');+ INSERT INTO project (resource_id, workspace, name) VALUES ('project-a', 'default', 'Project A');+ `)require.NoError(t, err)- require.True(t, draft.Payload.GetDraft())- expectedApproval := draft.Payload.GetApproval()-- updateIssueLabels(ctx, t, service, draft, []string{"environment:staging"})-- got := getIssueForTest(ctx, t, stores, draft.UID)- assert.True(t, got.Payload.GetDraft())- assert.Equal(t, []string{"environment:staging"}, got.Payload.GetLabels())- assert.Equal(t, expectedApproval, got.Payload.GetApproval())- assert.Empty(t, service.bus.ApprovalCheckChan)-}--func TestRetryIssueApprovalRejectsDraft(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)- _, issue := createIssueServiceApprovalIssue(ctx, t, stores)- draft, err := stores.UpdateIssue(ctx, issue.ProjectID, issue.UID, &store.UpdateIssueMessage{- PayloadUpsert: &storepb.Issue{Draft: true},+ // SearchIssues authorizes the caller itself (CUSTOM auth), so the test+ // principal needs a role carrying bb.issues.get.+ _, err = stores.PatchWorkspaceIamPolicy(ctx, &store.PatchIamPolicyMessage{+ Workspace: "default",+ Member: common.FormatUserEmail("[email protected]"),+ Roles: []string{"roles/workspaceAdmin"},})require.NoError(t, err)- require.True(t, draft.Payload.GetDraft())-- _, err = service.RetryIssueApproval(ctx, connect.NewRequest(&v1pb.RetryIssueApprovalRequest{- Name: common.FormatIssue(draft.ProjectID, draft.UID),- }))-- got := getIssueForTest(ctx, t, stores, draft.UID)- assert.Equal(t, connect.CodeFailedPrecondition, connect.CodeOf(err))- assert.True(t, got.Payload.GetDraft())- assert.Equal(t, draft.Payload.GetApproval(), got.Payload.GetApproval())- assert.Empty(t, service.bus.ApprovalCheckChan)+ return stores}-func TestDraftIssueApprovalActionsRejectedBeforeApprovalValidation(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)-- tests := []struct {- name string- call func(service *IssueService, issueName string) error- }{- {- name: "approve",- call: func(service *IssueService, issueName string) error {- _, err := service.ApproveIssue(ctx, connect.NewRequest(&v1pb.ApproveIssueRequest{Name: issueName}))- return err- },- },- {- name: "reject",- call: func(service *IssueService, issueName string) error {- _, err := service.RejectIssue(ctx, connect.NewRequest(&v1pb.RejectIssueRequest{Name: issueName}))- return err- },- },- {- name: "request",- call: func(service *IssueService, issueName string) error {- _, err := service.RequestIssue(ctx, connect.NewRequest(&v1pb.RequestIssueRequest{Name: issueName}))- return err- },- },- }-- for _, test := range tests {- t.Run(test.name, func(t *testing.T) {- t.Parallel()- service := newIssueServiceForTest(t, stores)- plan, err := stores.CreatePlan(ctx, &store.PlanMessage{- ProjectID: "project-a",- Name: fmt.Sprintf("%s draft approval plan", test.name),- Config: &storepb.PlanConfig{},- }, "[email protected]")- require.NoError(t, err)- draft, err := stores.CreateIssue(ctx, &store.IssueMessage{- ProjectID: "project-a",- CreatorEmail: "[email protected]",- Title: fmt.Sprintf("%s draft", test.name),- Type: storepb.Issue_DATABASE_CHANGE,- Payload: &storepb.Issue{Draft: true},- PlanUID: &plan.UID,- })- require.NoError(t, err)- require.True(t, draft.Payload.GetDraft())- require.Nil(t, draft.Payload.GetApproval())-- err = test.call(service, common.FormatIssue(draft.ProjectID, draft.UID))-- got := getIssueForTest(ctx, t, stores, draft.UID)- assert.Equal(t, connect.CodeFailedPrecondition, connect.CodeOf(err))- assert.Equal(t, draft, got)- assert.Empty(t, service.bus.ApprovalCheckChan)- assert.Empty(t, service.bus.RolloutCreationChan)- })- }+func issueServiceTestContext() context.Context {+ ctx := context.Background()+ ctx = context.WithValue(ctx, common.WorkspaceIDContextKey, "default")+ ctx = context.WithValue(ctx, common.UserContextKey, &store.UserMessage{+ Email: "[email protected]",+ Name: "creator",+ })+ return ctx}-func TestStaleReviewRequestDispatchesNoPostCommitEffects(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)- plan, issue := createIssueServiceApprovalIssue(ctx, t, stores)+func newIssueServiceForTest(t *testing.T, stores *store.Store) *IssueService {+ t.Helper()- staleApproval := issue.Payload.GetApproval()- staleApproval.Approvers[0].Status = storepb.IssuePayloadApproval_Approver_REJECTED- _, err := stores.UpdateIssue(ctx, issue.ProjectID, issue.UID, &store.UpdateIssueMessage{- PayloadUpsert: &storepb.Issue{Approval: staleApproval},- })- require.NoError(t, err)- _, err = stores.GetDB().ExecContext(ctx, `- UPDATE plan- SET config = jsonb_set(config, '{approvalInputVersion}', '3')- WHERE project = $1 AND id = $2`, plan.ProjectID, plan.UID)+ b, err := bus.New()require.NoError(t, err)-- for range 2 {- _, err = service.RequestIssue(ctx, connect.NewRequest(&v1pb.RequestIssueRequest{- Name: common.FormatIssue(issue.ProjectID, issue.UID),- Comment: "retry",- }))- require.Equal(t, connect.CodeFailedPrecondition, connect.CodeOf(err))- }- comments, err := stores.ListIssueComment(ctx, &store.FindIssueCommentMessage{- ProjectID: issue.ProjectID,- IssueUID: &issue.UID,- })+ iamManager, err := iam.NewManager(stores, nil, false)require.NoError(t, err)- require.Empty(t, comments)- require.Empty(t, service.bus.ApprovalCheckChan)- require.Empty(t, service.bus.RolloutCreationChan)- got := getIssueForTest(ctx, t, stores, issue.UID)- require.True(t, got.Payload.GetApproval().Equal(staleApproval))+ return NewIssueService(stores, webhook.NewManager(stores, nil), b, nil, iamManager)}-func TestCreateDraftIssue(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)+func createIssueServiceApprovalIssue(ctx context.Context, t *testing.T, stores *store.Store) (*store.PlanMessage, *store.IssueMessage) {+ t.Helper()plan, err := stores.CreatePlan(ctx, &store.PlanMessage{ProjectID: "project-a",- Name: "draft plan",- Description: "draft description",+ Name: "plan-a",+ Description: "",Config: &storepb.PlanConfig{+ ApprovalInputVersion: 2,Specs: []*storepb.PlanConfig_Spec{{Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{- SheetSha256: "sheet",- Targets: []string{"instances/prod/databases/app"},- },- },- },- },- },- }, "[email protected]")- require.NoError(t, err)-- response, err := service.CreateIssue(ctx, connect.NewRequest(&v1pb.CreateIssueRequest{- Parent: "projects/project-a",- Issue: &v1pb.Issue{- Type: v1pb.Issue_DATABASE_CHANGE,- Plan: common.FormatPlan("project-a", plan.UID),- Draft: true,- },- }))- require.NoError(t, err)- require.Equal(t, common.FormatPlan("project-a", plan.UID), response.Msg.Plan)- require.Equal(t, "draft plan", response.Msg.Title)- require.Equal(t, "draft description", response.Msg.Description)- require.True(t, response.Msg.Draft)-- stored, err := stores.GetIssue(ctx, &store.FindIssueMessage{- ProjectIDs: []string{"project-a"},- PlanUID: &plan.UID,- })- require.NoError(t, err)- require.True(t, stored.Payload.GetDraft())-- var rawDraft string- err = stores.GetDB().QueryRowContext(- ctx,- "SELECT payload->>'draft' FROM issue WHERE project = $1 AND id = $2",- "project-a",- stored.UID,- ).Scan(&rawDraft)- require.NoError(t, err)- require.Equal(t, "true", rawDraft)-- _, err = stores.GetDB().ExecContext(- ctx,- "UPDATE project SET setting = jsonb_set(setting, '{forceIssueLabels}', 'true') WHERE resource_id = $1",- "project-a",- )- require.NoError(t, err)-- createDatabasePlan, err := stores.CreatePlan(ctx, &store.PlanMessage{- ProjectID: "project-a",- Name: "create database draft",- Config: &storepb.PlanConfig{- Specs: []*storepb.PlanConfig_Spec{- {- Config: &storepb.PlanConfig_Spec_CreateDatabaseConfig{- CreateDatabaseConfig: &storepb.PlanConfig_CreateDatabaseConfig{- Target: "instances/prod",- Database: "app",- },- },- },- },- },- }, "[email protected]")- require.NoError(t, err)- createDatabaseDraft, err := service.CreateIssue(ctx, connect.NewRequest(&v1pb.CreateIssueRequest{- Parent: "projects/project-a",- Issue: &v1pb.Issue{- Type: v1pb.Issue_DATABASE_CHANGE,- Plan: common.FormatPlan("project-a", createDatabasePlan.UID),- Draft: true,- },- }))- require.NoError(t, err)- require.True(t, createDatabaseDraft.Msg.GetDraft())-}--func TestCreateDraftIssueDoesNotExposeAnotherCreatorsDraft(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)- plan, err := stores.CreatePlan(ctx, &store.PlanMessage{- ProjectID: "project-a",- Name: "private draft plan",- Config: &storepb.PlanConfig{- Specs: []*storepb.PlanConfig_Spec{{- Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{- ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{- SheetSha256: "sheet",- },- },- }},- },- }, "[email protected]")- require.NoError(t, err)- existing, err := stores.CreateIssue(ctx, &store.IssueMessage{- ProjectID: "project-a",- CreatorEmail: "[email protected]",- Title: "private draft",- Type: storepb.Issue_DATABASE_CHANGE,- Payload: &storepb.Issue{Draft: true},- PlanUID: &plan.UID,- })- require.NoError(t, err)-- otherCtx := context.WithValue(ctx, common.UserContextKey, &store.UserMessage{- Email: "[email protected]",- Name: "other",- })- response, err := service.CreateIssue(otherCtx, connect.NewRequest(&v1pb.CreateIssueRequest{- Parent: "projects/project-a",- Issue: &v1pb.Issue{- Type: v1pb.Issue_DATABASE_CHANGE,- Plan: common.FormatPlan("project-a", plan.UID),- Draft: true,- },- }))-- require.Nil(t, response)- require.Equal(t, connect.CodeAlreadyExists, connect.CodeOf(err))- stored, getErr := stores.GetIssue(ctx, &store.FindIssueMessage{- ProjectIDs: []string{"project-a"},- PlanUID: &plan.UID,- })- require.NoError(t, getErr)- require.Equal(t, existing, stored)-}--func TestCreateSubmittedIssueBlockedByExistingDraft(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)-- plan, err := stores.CreatePlan(ctx, &store.PlanMessage{- ProjectID: "project-a",- Name: "draft conflict plan",- Config: &storepb.PlanConfig{- Specs: []*storepb.PlanConfig_Spec{- {- Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{- ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{- SheetSha256: "sheet",- },- },- },- },- },- }, "[email protected]")- require.NoError(t, err)- draft, err := stores.CreateIssue(ctx, &store.IssueMessage{- ProjectID: "project-a",- CreatorEmail: "[email protected]",- Title: "original draft",- Description: "original description",- Type: storepb.Issue_DATABASE_CHANGE,- Payload: &storepb.Issue{- Labels: []string{"original"},- Draft: true,- },- PlanUID: &plan.UID,- })- require.NoError(t, err)- require.True(t, draft.Payload.GetDraft())-- _, err = service.CreateIssue(ctx, connect.NewRequest(&v1pb.CreateIssueRequest{- Parent: "projects/project-a",- Issue: &v1pb.Issue{- Title: "submitted replacement",- Description: "replacement description",- Type: v1pb.Issue_DATABASE_CHANGE,- Plan: common.FormatPlan("project-a", plan.UID),- Labels: []string{"replacement"},- Draft: false,- },- }))-- issues, listErr := stores.ListIssues(ctx, &store.FindIssueMessage{- ProjectIDs: []string{"project-a"},- PlanUID: &plan.UID,- })- require.NoError(t, listErr)- assert.Equal(t, connect.CodeFailedPrecondition, connect.CodeOf(err))- assert.Len(t, issues, 1)- if assert.NotEmpty(t, issues) {- assert.Equal(t, draft, issues[0])- }- assert.Empty(t, service.bus.ApprovalCheckChan)-}--func TestCreateDraftIssueBlockedByExistingNonDraftIssue(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)-- plan, err := stores.CreatePlan(ctx, &store.PlanMessage{- ProjectID: "project-a",- Name: "submitted plan",- Config: &storepb.PlanConfig{- Specs: []*storepb.PlanConfig_Spec{- {- Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{- ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{- SheetSha256: "sheet",- },- },- },- },- },- }, "[email protected]")- require.NoError(t, err)-- existing, err := stores.CreateIssue(ctx, &store.IssueMessage{- ProjectID: "project-a",- CreatorEmail: "[email protected]",- Title: "submitted issue",- Type: storepb.Issue_DATABASE_CHANGE,- Payload: &storepb.Issue{Labels: []string{"original"}},- PlanUID: &plan.UID,- })- require.NoError(t, err)- require.False(t, existing.Payload.GetDraft())-- var hasDraft bool- err = stores.GetDB().QueryRowContext(- ctx,- "SELECT payload ? 'draft' FROM issue WHERE project = $1 AND id = $2",- "project-a",- existing.UID,- ).Scan(&hasDraft)- require.NoError(t, err)- require.False(t, hasDraft)-- _, err = service.CreateIssue(ctx, connect.NewRequest(&v1pb.CreateIssueRequest{- Parent: "projects/project-a",- Issue: &v1pb.Issue{- Title: "draft replacement",- Type: v1pb.Issue_DATABASE_CHANGE,- Plan: common.FormatPlan("project-a", plan.UID),- Draft: true,- },- }))- require.Equal(t, connect.CodeAlreadyExists, connect.CodeOf(err))-- stored, err := stores.GetIssue(ctx, &store.FindIssueMessage{- ProjectIDs: []string{"project-a"},- PlanUID: &plan.UID,- })- require.NoError(t, err)- require.Equal(t, existing.UID, stored.UID)- require.Equal(t, "submitted issue", stored.Title)- require.Equal(t, []string{"original"}, stored.Payload.GetLabels())- require.False(t, stored.Payload.GetDraft())-- direct, err := service.GetIssue(ctx, connect.NewRequest(&v1pb.GetIssueRequest{- Name: common.FormatIssue("project-a", existing.UID),- }))- require.NoError(t, err)- require.False(t, direct.Msg.GetDraft())-}--func TestCreateDraftIssueRejectsUnsupportedWorkflows(t *testing.T) {- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)-- createPlan := func(name string, specs ...*storepb.PlanConfig_Spec) *store.PlanMessage {- t.Helper()- plan, err := stores.CreatePlan(ctx, &store.PlanMessage{- ProjectID: "project-a",- Name: name,- Config: &storepb.PlanConfig{Specs: specs},- }, "[email protected]")- require.NoError(t, err)- return plan- }- changeSpec := func(release string) *storepb.PlanConfig_Spec {- return &storepb.PlanConfig_Spec{- Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{- ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{- SheetSha256: "sheet",- Release: release,- },- },- }- }- createSpec := func() *storepb.PlanConfig_Spec {- return &storepb.PlanConfig_Spec{- Config: &storepb.PlanConfig_Spec_CreateDatabaseConfig{- CreateDatabaseConfig: &storepb.PlanConfig_CreateDatabaseConfig{- Target: "instances/prod",- Database: "app",- },- },- }- }- validPlan := createPlan("valid", changeSpec(""))- gitOpsPlan := createPlan("gitops", changeSpec("projects/project-a/releases/release"))- mixedPlan := createPlan("mixed", createSpec(), changeSpec(""))-- tests := []struct {- name string- issue *v1pb.Issue- }{- {- name: "no plan",- issue: &v1pb.Issue{- Type: v1pb.Issue_DATABASE_CHANGE,- Draft: true,- },- },- {- name: "GitOps plan",- issue: &v1pb.Issue{- Type: v1pb.Issue_DATABASE_CHANGE,- Plan: common.FormatPlan("project-a", gitOpsPlan.UID),- Draft: true,- },- },- {- name: "mixed plan",- issue: &v1pb.Issue{- Type: v1pb.Issue_DATABASE_CHANGE,- Plan: common.FormatPlan("project-a", mixedPlan.UID),- Draft: true,- },- },- {- name: "non-database issue",- issue: &v1pb.Issue{- Title: "role request",- Type: v1pb.Issue_ROLE_GRANT,- Plan: common.FormatPlan("project-a", validPlan.UID),- Draft: true,- },- },- }-- for _, test := range tests {- t.Run(test.name, func(t *testing.T) {- _, err := service.CreateIssue(ctx, connect.NewRequest(&v1pb.CreateIssueRequest{- Parent: "projects/project-a",- Issue: test.issue,- }))- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))- })- }-}--// TestCreateIssueRejectsRetiredExportType pins the retired DATABASE_EXPORT-// issue type: legacy clients still sending wire value 3 get InvalidArgument.-func TestCreateIssueRejectsRetiredExportType(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)-- _, err := service.CreateIssue(ctx, connect.NewRequest(&v1pb.CreateIssueRequest{- Parent: "projects/project-a",- Issue: &v1pb.Issue{- Title: "legacy export issue",- Type: v1pb.Issue_Type(3),- },- }))- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))-}--func TestIssueListsHideDraftIssues(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)-- submittedPlan, err := stores.CreatePlan(ctx, &store.PlanMessage{- ProjectID: "project-a",- Name: "submitted plan",- Config: &storepb.PlanConfig{},- }, "[email protected]")- require.NoError(t, err)- submitted, err := stores.CreateIssue(ctx, &store.IssueMessage{- ProjectID: "project-a",- CreatorEmail: "[email protected]",- Title: "submitted",- Type: storepb.Issue_DATABASE_CHANGE,- Payload: &storepb.Issue{},- PlanUID: &submittedPlan.UID,- })- require.NoError(t, err)-- draftPlan, err := stores.CreatePlan(ctx, &store.PlanMessage{- ProjectID: "project-a",- Name: "draft plan",- Config: &storepb.PlanConfig{},- }, "[email protected]")- require.NoError(t, err)- draft, err := stores.CreateIssue(ctx, &store.IssueMessage{- ProjectID: "project-a",- CreatorEmail: "[email protected]",- Title: "draft",- Type: storepb.Issue_DATABASE_CHANGE,- Payload: &storepb.Issue{Draft: true},- PlanUID: &draftPlan.UID,- })- require.NoError(t, err)-- list, err := service.ListIssues(ctx, connect.NewRequest(&v1pb.ListIssuesRequest{- Parent: "projects/project-a",- PageSize: 1,- }))- require.NoError(t, err)- require.Equal(t, []string{common.FormatIssue("project-a", submitted.UID)}, issueNames(list.Msg.Issues))- require.Empty(t, list.Msg.GetNextPageToken())-- search, err := service.SearchIssues(ctx, connect.NewRequest(&v1pb.SearchIssuesRequest{- Parent: "projects/project-a",- PageSize: 1,- }))- require.NoError(t, err)- require.Equal(t, []string{common.FormatIssue("project-a", submitted.UID)}, issueNames(search.Msg.Issues))- require.Empty(t, search.Msg.GetNextPageToken())-- direct, err := service.GetIssue(ctx, connect.NewRequest(&v1pb.GetIssueRequest{- Name: common.FormatIssue("project-a", draft.UID),- }))- require.NoError(t, err)- require.True(t, direct.Msg.GetDraft())-}--// The fixture helpers below are duplicated in-// backend/store/issue_service_concurrency_test.go, which needs its own copies-// for the concurrency tests that moved there. Change both together: nothing-// catches a divergence. The duplication ends when the tests here move to a fake.-func issueNames(issues []*v1pb.Issue) []string {- names := make([]string, 0, len(issues))- for _, issue := range issues {- names = append(names, issue.GetName())- }- return names-}--func setupIssueServiceTestStore(ctx context.Context, t *testing.T) *store.Store {- t.Helper()-- db, stores, _ := testcontainer.NewMetadataDB(t)-- _, err := db.ExecContext(ctx, `- INSERT INTO workspace (resource_id) VALUES ('default');- INSERT INTO principal (name, email, password_hash) VALUES ('creator', '[email protected]', 'unused');- INSERT INTO project (resource_id, workspace, name) VALUES ('project-a', 'default', 'Project A');- `)- require.NoError(t, err)-- // SearchIssues authorizes the caller itself (CUSTOM auth), so the test- // principal needs a role carrying bb.issues.get.- _, err = stores.PatchWorkspaceIamPolicy(ctx, &store.PatchIamPolicyMessage{- Workspace: "default",- Member: common.FormatUserEmail("[email protected]"),- Roles: []string{"roles/workspaceAdmin"},- })- require.NoError(t, err)- return stores-}--func issueServiceTestContext() context.Context {- ctx := context.Background()- ctx = context.WithValue(ctx, common.WorkspaceIDContextKey, "default")- ctx = context.WithValue(ctx, common.UserContextKey, &store.UserMessage{- Email: "[email protected]",- Name: "creator",- })- return ctx-}--func newIssueServiceForTest(t *testing.T, stores *store.Store) *IssueService {- t.Helper()-- b, err := bus.New()- require.NoError(t, err)- iamManager, err := iam.NewManager(stores, nil, false)- require.NoError(t, err)- return NewIssueService(stores, webhook.NewManager(stores, nil), b, nil, iamManager)-}--func createIssueServiceApprovalIssue(ctx context.Context, t *testing.T, stores *store.Store) (*store.PlanMessage, *store.IssueMessage) {- t.Helper()-- plan, err := stores.CreatePlan(ctx, &store.PlanMessage{- ProjectID: "project-a",- Name: "plan-a",- Description: "",- Config: &storepb.PlanConfig{- ApprovalInputVersion: 2,- Specs: []*storepb.PlanConfig_Spec{- {- Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{- ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{- Targets: []string{"instances/prod/databases/app"},+ Targets: []string{"instances/prod/databases/app"},},},},@@ -1180,177 +376,3 @@ func createIssueServiceApprovalIssue(ctx context.Context, t *testing.T, stores *require.NoError(t, err)return plan, issue}--func createIssueServiceCreateDatabaseApprovalIssue(ctx context.Context, t *testing.T, stores *store.Store) (*store.PlanMessage, *store.IssueMessage) {- t.Helper()-- plan, err := stores.CreatePlan(ctx, &store.PlanMessage{- ProjectID: "project-a",- Name: "plan-a",- Description: "",- Config: &storepb.PlanConfig{- ApprovalInputVersion: 2,- Specs: []*storepb.PlanConfig_Spec{- {- Config: &storepb.PlanConfig_Spec_CreateDatabaseConfig{- CreateDatabaseConfig: &storepb.PlanConfig_CreateDatabaseConfig{- Target: "instances/prod/databases/app",- },- },- },- },- },- }, "[email protected]")- require.NoError(t, err)-- issue, err := stores.CreateIssue(ctx, &store.IssueMessage{- ProjectID: "project-a",- CreatorEmail: "[email protected]",- Title: "issue-a",- Type: storepb.Issue_DATABASE_CHANGE,- Description: "",- Payload: &storepb.Issue{- Labels: []string{"environment:prod"},- Approval: &storepb.IssuePayloadApproval{- ApprovalTemplate: &storepb.ApprovalTemplate{- Flow: &storepb.ApprovalFlow{Roles: []string{"roles/projectOwner"}},- Title: "manual approval",- },- Approvers: []*storepb.IssuePayloadApproval_Approver{- {- Status: storepb.IssuePayloadApproval_Approver_APPROVED,- Principal: common.FormatUserEmail("[email protected]"),- },- },- ApprovalFindingDone: true,- ApprovalInputVersion: 2,- },- },- PlanUID: &plan.UID,- })- require.NoError(t, err)- return plan, issue-}--func updateIssueLabels(ctx context.Context, t *testing.T, service *IssueService, issue *store.IssueMessage, labels []string) {- t.Helper()-- _, err := service.UpdateIssue(ctx, connect.NewRequest(&v1pb.UpdateIssueRequest{- Issue: &v1pb.Issue{- Name: common.FormatIssue(issue.ProjectID, issue.UID),- Labels: labels,- },- UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"labels"}},- }))- require.NoError(t, err)-}--func getIssueForTest(ctx context.Context, t *testing.T, stores *store.Store, issueUID int64) *store.IssueMessage {- t.Helper()-- got, err := stores.GetIssue(ctx, &store.FindIssueMessage{- ProjectIDs: []string{"project-a"},- UID: &issueUID,- })- require.NoError(t, err)- require.NotNil(t, got)- return got-}--func createIssueWithApproval(ctx context.Context, t *testing.T, stores *store.Store, title string, approval *storepb.IssuePayloadApproval) *store.IssueMessage {- t.Helper()-- plan, err := stores.CreatePlan(ctx, &store.PlanMessage{- ProjectID: "project-a",- Name: title + " plan",- Config: &storepb.PlanConfig{},- }, "[email protected]")- require.NoError(t, err)-- issue, err := stores.CreateIssue(ctx, &store.IssueMessage{- ProjectID: "project-a",- CreatorEmail: "[email protected]",- Title: title,- Type: storepb.Issue_DATABASE_CHANGE,- Payload: &storepb.Issue{Approval: approval},- PlanUID: &plan.UID,- })- require.NoError(t, err)- return issue-}--func approvalTemplateWithRoles(roles ...string) *storepb.ApprovalTemplate {- return &storepb.ApprovalTemplate{Flow: &storepb.ApprovalFlow{Roles: roles}}-}--func issueApprover(status storepb.IssuePayloadApproval_Approver_Status) *storepb.IssuePayloadApproval_Approver {- return &storepb.IssuePayloadApproval_Approver{- Status: status,- Principal: common.FormatUserEmail("[email protected]"),- }-}--// TestIssueApprovalFiltersRunBeforePaging is the regression lock for audit-// finding T13: approval_status and current_approver used to be applied to the-// page after the store had already cut it and minted the page token, so-// filtering the default My Issues view answered with an empty page and a live-// next page token underneath it.-func TestIssueApprovalStatusFilterMatchesConverter(t *testing.T) {- t.Parallel()- ctx := issueServiceTestContext()- stores := setupIssueServiceTestStore(ctx, t)- service := newIssueServiceForTest(t, stores)-- cases := []struct {- title string- approval *storepb.IssuePayloadApproval- }{- {"no approval payload at all", nil},- {"approval finding not done", &storepb.IssuePayloadApproval{ApprovalTemplate: approvalTemplateWithRoles("roles/workspaceAdmin")}},- {"no template", &storepb.IssuePayloadApproval{ApprovalFindingDone: true}},- {"template with an empty flow", &storepb.IssuePayloadApproval{ApprovalFindingDone: true, ApprovalTemplate: approvalTemplateWithRoles()}},- {"no approvers yet", &storepb.IssuePayloadApproval{ApprovalFindingDone: true, ApprovalTemplate: approvalTemplateWithRoles("roles/workspaceAdmin")}},- {"one of two steps approved", &storepb.IssuePayloadApproval{- ApprovalFindingDone: true,- ApprovalTemplate: approvalTemplateWithRoles("roles/workspaceAdmin", "roles/projectOwner"),- Approvers: []*storepb.IssuePayloadApproval_Approver{issueApprover(storepb.IssuePayloadApproval_Approver_APPROVED)},- }},- {"rejected at the second step", &storepb.IssuePayloadApproval{- ApprovalFindingDone: true,- ApprovalTemplate: approvalTemplateWithRoles("roles/workspaceAdmin", "roles/projectOwner"),- Approvers: []*storepb.IssuePayloadApproval_Approver{- issueApprover(storepb.IssuePayloadApproval_Approver_APPROVED),- issueApprover(storepb.IssuePayloadApproval_Approver_REJECTED),- },- }},- {"every step approved", &storepb.IssuePayloadApproval{- ApprovalFindingDone: true,- ApprovalTemplate: approvalTemplateWithRoles("roles/workspaceAdmin"),- Approvers: []*storepb.IssuePayloadApproval_Approver{issueApprover(storepb.IssuePayloadApproval_Approver_APPROVED)},- }},- }-- want := map[v1pb.ApprovalStatus][]string{}- for _, tc := range cases {- issue := createIssueWithApproval(ctx, t, stores, tc.title, tc.approval)- status := computeApprovalStatus(tc.approval)- want[status] = append(want[status], common.FormatIssue("project-a", issue.UID))- }- // Every branch of computeApprovalStatus should be represented, otherwise the- // comparison below passes on empty sets.- require.Len(t, want, 5)-- for status, wantNames := range want {- resp, err := service.ListIssues(ctx, connect.NewRequest(&v1pb.ListIssuesRequest{- Parent: "projects/project-a",- Filter: fmt.Sprintf("approval_status == %q", status),- PageSize: 100,- }))- require.NoError(t, err, "approval_status == %q", status)-- gotNames := issueNames(resp.Msg.Issues)- slices.Sort(gotNames)- slices.Sort(wantNames)- require.Equal(t, wantNames, gotNames, "approval_status == %q", status)- }-}diff --git a/backend/api/v1/mcp_gate_test.go b/backend/api/v1/mcp_gate_test.goindex af983f6855..0bde0e1463 100644--- a/backend/api/v1/mcp_gate_test.go+++ b/backend/api/v1/mcp_gate_test.go@@ -17,7 +17,6 @@ import ("github.com/bytebase/bytebase/backend/api/auth""github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/component/config"storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1""github.com/bytebase/bytebase/backend/generated-go/v1/v1connect"@@ -1184,8 +1183,7 @@ func TestMCPGateRefusesGrantIssues(t *testing.T) {//nolint:tparallel // Subtests share the parent's fixture.func TestMCPGateDenialIsAuditedWithoutAnAuditAnnotation(t *testing.T) {t.Parallel()- st := newAuditLiveStore(t)- auditIn := NewAuditInterceptor(st, "test-secret", &config.Profile{})+ auditIn, captured := newRecordingAuditInterceptor()gate := NewInternalMCPGateInterceptor(readWriteCeiling())invoke := func(t *testing.T, correlationID, procedure string, req connect.AnyRequest) {@@ -1213,9 +1211,9 @@ func TestMCPGateDenialIsAuditedWithoutAnAuditAnnotation(t *testing.T) {assertOneDeniedRow := func(t *testing.T, correlationID, procedure string) *storepb.AuditLog {t.Helper()- rows := findRowsByCorrelation(t, st, correlationID)+ rows := rowsByCorrelation(*captured, correlationID)require.Len(t, rows, 1, "a policy denial must be recorded even where the method asks for no audit row")- row := rows[0].Payload+ row := rows[0]require.Equal(t, procedure, row.Method)require.Equal(t, int32(connect.CodePermissionDenied), row.GetStatus().GetCode())require.Equal(t, "workspaces/"+auditTestWorkspace, row.Parent)diff --git a/backend/api/v1/mcp_info_test.go b/backend/api/v1/mcp_info_test.godeleted file mode 100644index 88ac990294..0000000000--- a/backend/api/v1/mcp_info_test.go+++ /dev/null@@ -1,158 +0,0 @@-package v1--import (- "context"- "testing"-- "github.com/bytebase/bytebase/backend/common/testcontainer"-- "connectrpc.com/connect"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/component/config"- "github.com/bytebase/bytebase/backend/enterprise"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"- "github.com/bytebase/bytebase/backend/store"-)--// TestGetActuatorInfoMCPSetting drives the MCP setting in actuator info against-// a real store, across the ways a-// stored row resolves: served, disabled, a name nothing resolves, a number-// nothing serves, and a row that does not unmarshal. capability is the whole-// answer now, and the consent page derives its three states from it alone, so-// this is the backend receipt for what the page may disclose.-func TestGetActuatorInfoMCPSetting(t *testing.T) {- ctx := context.Background()- db, stores, _ := testcontainer.NewMetadataDB(t)-- licenseService, err := enterprise.NewLicenseService(common.ReleaseModeDev, stores, false, "")- require.NoError(t, err)- service := NewActuatorService(stores, &config.Profile{}, nil, licenseService, nil)-- const workspaceID = "mcp-info-handler"- _, err = stores.CreateWorkspace(ctx, &store.WorkspaceMessage{- ResourceID: workspaceID,- Payload: &storepb.WorkspacePayload{Title: "MCP info handler"},- }, "[email protected]")- require.NoError(t, err)-- user, err := stores.CreateUser(ctx, &store.UserMessage{- Email: "[email protected]",- Name: "Admin",- PasswordHash: "unused",- Profile: &storepb.UserProfile{},- })- require.NoError(t, err)-- get := func(ctx context.Context) (*v1pb.MCPSetting, error) {- resp, err := service.GetActuatorInfo(ctx, connect.NewRequest(&v1pb.GetActuatorInfoRequest{}))- if err != nil {- return nil, err- }- return resp.Msg.McpSetting, nil- }- workspaceCtx := func() context.Context {- ctx := context.WithValue(ctx, common.UserContextKey, user)- return context.WithValue(ctx, common.WorkspaceIDContextKey, workspaceID)- }- setCeiling := func(t *testing.T, value string) {- t.Helper()- _, err := db.ExecContext(ctx, `- INSERT INTO setting (name, workspace, value) VALUES ('MCP', $1, $2)- ON CONFLICT (workspace, name) DO UPDATE SET value = EXCLUDED.value`,- workspaceID, value)- require.NoError(t, err)- }-- t.Run("a workspace with the default MCP policy", func(t *testing.T) {- info, err := get(workspaceCtx())- require.NoError(t, err)- require.Equal(t, v1pb.MCPSetting_READ_ONLY, info.Capability,- "a new workspace is seeded READ_ONLY; only a workspace with no row at all falls back to READ_WRITE")- require.False(t, info.IgnoreMaskingExemptions)- })-- t.Run("the toggle reaches the response", func(t *testing.T) {- setCeiling(t, `{"capability":"READ_ONLY","ignoreMaskingExemptions":true}`)- info, err := get(workspaceCtx())- require.NoError(t, err)- require.Equal(t, v1pb.MCPSetting_READ_ONLY, info.Capability)- require.True(t, info.IgnoreMaskingExemptions,- "every masking state is written in terms of this, so withholding it leaves the table unusable")- })-- t.Run("a disabled workspace is still answered", func(t *testing.T) {- setCeiling(t, `{"capability":"DISABLED"}`)- info, err := get(workspaceCtx())- require.NoError(t, err)- require.Equal(t, v1pb.MCPSetting_DISABLED, info.Capability)- })-- // The two subtests below are BOT-106. Both ceilings refuse every MCP- // connection and both used to refuse this call whole, which left the admin- // repairing the row with no reading of what is stored, and the consent page- // with no policy to disclose.- t.Run("a ceiling this build cannot read is described, not refused", func(t *testing.T) {- setCeiling(t, `{"capability":"READ_ONLYY"}`)- info, err := get(workspaceCtx())- require.NoError(t, err, "the ceiling is described, not refused")- require.Equal(t, v1pb.MCPSetting_CAPABILITY_UNSPECIFIED, info.Capability,- "a ceiling nobody can resolve must not arrive as a mode, least of all the permissive one")- require.False(t, info.IgnoreMaskingExemptions,- "no MCP request runs under this ceiling, so the toggle carries neither the row's value nor a decision")- })-- // A wrong-typed field fails the whole setting unmarshal. Actuator info is a- // shared bootstrap response, so it remains available and leaves the optional- // setting absent; the policy page then withholds editing rather than showing- // a guessed ceiling.- t.Run("a row that does not unmarshal leaves the setting absent", func(t *testing.T) {- for _, row := range []string{- `{"capability":{}}`,- `{"capability":true}`,- `{"capability":1.5}`,- `{"capability":[]}`,- `{"capability":"READ_ONLY","ignoreMaskingExemptions":[]}`,- `{"capability":"READ_ONLY","ignoreMaskingExemptions":"yes"}`,- } {- setCeiling(t, row)- setting, err := get(workspaceCtx())- require.NoError(t, err, row)- require.Nil(t, setting, row)- }- })-- t.Run("a ceiling this build does not serve arrives as the stored number", func(t *testing.T) {- // The reserved 2, or a value a newer release wrote. It parses, so the- // number is the answer: a client that has no name for it is exactly the- // client that must not disclose it, and the number is what tells it so.- setCeiling(t, `{"capability":2}`)- info, err := get(workspaceCtx())- require.NoError(t, err)- require.NotEqual(t, v1pb.MCPSetting_CAPABILITY_UNSPECIFIED, info.Capability,- "it parsed; the number is the answer, not a fallback to unresolvable")- require.Equal(t, v1pb.MCPSetting_Capability(2), info.Capability)- })-- t.Run("the gate's resolution wins over a second read", func(t *testing.T) {- // The stored ceiling still says DISABLED below; the gate admitted this- // request under READ_ONLY. Answering from the store would report a- // ceiling the request was not admitted under.- setCeiling(t, `{"capability":"DISABLED"}`)- stamped := withMCPSettings(workspaceCtx(), &storepb.MCPSetting{- Capability: storepb.MCPSetting_READ_ONLY,- IgnoreMaskingExemptions: true,- })- info, err := get(stamped)- require.NoError(t, err)- require.Equal(t, v1pb.MCPSetting_READ_ONLY, info.Capability)- require.True(t, info.IgnoreMaskingExemptions)- })-- t.Run("no workspace on the request", func(t *testing.T) {- _, err := get(context.WithValue(ctx, common.UserContextKey, user))- require.Equal(t, connect.CodeInternal, connect.CodeOf(err))- })-}diff --git a/backend/api/v1/plan_service.go b/backend/api/v1/plan_service.goindex 2f1760487e..f9221704f8 100644--- a/backend/api/v1/plan_service.go+++ b/backend/api/v1/plan_service.go@@ -831,11 +831,6 @@ func convertToPlans(ctx context.Context, s *store.Store, plans []*store.PlanMessreturn nil, nil}- type planKey struct {- projectID string- planUID int64- }-workspaceID := common.GetWorkspaceIDFromContext(ctx)planUIDs := make([]int64, 0, len(plans))rolloutPlanUIDs := make([]int64, 0, len(plans))@@ -860,13 +855,6 @@ func convertToPlans(ctx context.Context, s *store.Store, plans []*store.PlanMessif err != nil {return nil, errors.Wrap(err, "failed to batch list issues")}- issueByPlanKey := make(map[planKey]*store.IssueMessage, len(issues))- for _, issue := range issues {- if issue.PlanUID != nil {- issueByPlanKey[planKey{projectID: issue.ProjectID, planUID: *issue.PlanUID}] = issue- }- }-planCheckRuns, err := s.ListPlanCheckRuns(ctx, &store.FindPlanCheckRunMessage{ProjectIDs: &projectIDs,PlanUIDs: &planUIDs,@@ -874,12 +862,8 @@ func convertToPlans(ctx context.Context, s *store.Store, plans []*store.PlanMessif err != nil {return nil, errors.Wrap(err, "failed to batch list plan check runs")}- planCheckRunByPlanKey := make(map[planKey]*store.PlanCheckRunMessage, len(planCheckRuns))- for _, run := range planCheckRuns {- planCheckRunByPlanKey[planKey{projectID: run.ProjectID, planUID: run.PlanUID}] = run- }- taskStatusCountByPlanKey := make(map[planKey][]*store.TaskStatusCount)+ var taskStatusCounts []*store.TaskStatusCountenvironmentOrderMap := map[string]int{}if len(rolloutPlanUIDs) > 0 {environmentSetting, err := s.GetEnvironment(ctx, workspaceID)@@ -888,15 +872,37 @@ func convertToPlans(ctx context.Context, s *store.Store, plans []*store.PlanMess}environmentOrderMap = common.EnvironmentOrderMap(environmentSetting.GetEnvironments())- taskStatusCounts, err := s.ListTaskStatusCountByPlanIDs(ctx, projectIDs, rolloutPlanUIDs)+ taskStatusCounts, err = s.ListTaskStatusCountByPlanIDs(ctx, projectIDs, rolloutPlanUIDs)if err != nil {return nil, errors.Wrap(err, "failed to batch list task status counts")}- for _, count := range taskStatusCounts {- key := planKey{projectID: count.ProjectID, planUID: count.PlanID}- taskStatusCountByPlanKey[key] = append(taskStatusCountByPlanKey[key], count)+ }+ return buildV1Plans(plans, issues, planCheckRuns, taskStatusCounts, environmentOrderMap), nil+}++// buildV1Plans joins the batch reads onto their plans. Every relation is keyed+// by (project, plan UID) rather than by UID alone: plan UIDs are allocated per+// project, so the same number names a different plan in every project.+func buildV1Plans(plans []*store.PlanMessage, issues []*store.IssueMessage, planCheckRuns []*store.PlanCheckRunMessage, taskStatusCounts []*store.TaskStatusCount, environmentOrderMap map[string]int) []*v1pb.Plan {+ type planKey struct {+ projectID string+ planUID int64+ }+ issueByPlanKey := make(map[planKey]*store.IssueMessage, len(issues))+ for _, issue := range issues {+ if issue.PlanUID != nil {+ issueByPlanKey[planKey{projectID: issue.ProjectID, planUID: *issue.PlanUID}] = issue}}+ planCheckRunByPlanKey := make(map[planKey]*store.PlanCheckRunMessage, len(planCheckRuns))+ for _, run := range planCheckRuns {+ planCheckRunByPlanKey[planKey{projectID: run.ProjectID, planUID: run.PlanUID}] = run+ }+ taskStatusCountByPlanKey := make(map[planKey][]*store.TaskStatusCount)+ for _, count := range taskStatusCounts {+ key := planKey{projectID: count.ProjectID, planUID: count.PlanID}+ taskStatusCountByPlanKey[key] = append(taskStatusCountByPlanKey[key], count)+ }v1Plans := make([]*v1pb.Plan, len(plans))for i, plan := range plans {@@ -907,7 +913,7 @@ func convertToPlans(ctx context.Context, s *store.Store, plans []*store.PlanMessv1Plan.Issue = common.FormatIssue(issue.ProjectID, issue.UID)v1Plan.IssueStatus = convertToIssueStatus(issue.Status)if !issue.Payload.GetDraft() {- v1Plan.ApprovalStatus = computeApprovalStatus(issue.Payload.GetApproval())+ v1Plan.ApprovalStatus = store.ComputeApprovalStatus(issue.Payload.GetApproval())}}@@ -924,7 +930,7 @@ func convertToPlans(ctx context.Context, s *store.Store, plans []*store.PlanMessv1Plans[i] = v1Plan}- return v1Plans, nil+ return v1Plans}func convertToPlan(ctx context.Context, s *store.Store, plan *store.PlanMessage) (*v1pb.Plan, error) {diff --git a/backend/api/v1/plan_service_test.go b/backend/api/v1/plan_service_test.goindex da16c55788..7cd148b612 100644--- a/backend/api/v1/plan_service_test.go+++ b/backend/api/v1/plan_service_test.go@@ -2,368 +2,107 @@ package v1import ("context"- "fmt""testing"- "github.com/bytebase/bytebase/backend/common/testcontainer"-- "connectrpc.com/connect""github.com/stretchr/testify/require"- "google.golang.org/protobuf/types/known/fieldmaskpb"- "github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/component/bus"storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1""github.com/bytebase/bytebase/backend/store")-func TestPlanServiceListPlansHidesMalformedUIPlans(t *testing.T) {+// TestValidateSpecsRejectsShapesBeforeAnyLookup pins the two plan shapes+// creation refuses on the request alone: a plan mixing create-database and+// change-database specs, and a spec with no config at all — which is what a+// legacy client sending the retired export_data_config decodes to. Neither+// reaches the store, so nil is one.+func TestValidateSpecsRejectsShapesBeforeAnyLookup(t *testing.T) {t.Parallel()ctx := context.Background()- stores := setupPlanServiceTestStore(ctx, t)- service := NewPlanService(stores, nil, nil, nil, nil)-- createPlan := func(name string, config *storepb.PlanConfig) *store.PlanMessage {- t.Helper()- plan, err := stores.CreatePlan(ctx, &store.PlanMessage{- ProjectID: "project-a",- Name: name,- Config: config,- }, "[email protected]")- require.NoError(t, err)- return plan- }- changeConfig := func(id, release string) *storepb.PlanConfig {- return &storepb.PlanConfig{Specs: []*storepb.PlanConfig_Spec{{- Id: id,- Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{- ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{Release: release},- },- }}}- }- createConfig := func(id string) *storepb.PlanConfig {- return &storepb.PlanConfig{Specs: []*storepb.PlanConfig_Spec{{- Id: id,- Config: &storepb.PlanConfig_Spec_CreateDatabaseConfig{- CreateDatabaseConfig: &storepb.PlanConfig_CreateDatabaseConfig{},- },- }}}- }- createPlan("malformed change", changeConfig("malformed-change", ""))- createPlan("malformed create", createConfig("malformed-create"))- createPlan("malformed mixed", &storepb.PlanConfig{Specs: []*storepb.PlanConfig_Spec{- createConfig("mixed-create").Specs[0],- changeConfig("mixed-change", "").Specs[0],- }})- oldMalformed := createPlan("old malformed", changeConfig("old", ""))- _, err := stores.GetDB().ExecContext(ctx, `- UPDATE plan SET created_at = CURRENT_TIMESTAMP - INTERVAL '31 days'- WHERE project = $1 AND id = $2`, oldMalformed.ProjectID, oldMalformed.UID)- require.NoError(t, err)- gitOps := createPlan("GitOps", changeConfig("gitops", "projects/project-a/releases/release-a"))- deleted := createPlan("deleted", changeConfig("deleted", ""))- _, err = stores.GetDB().ExecContext(ctx, `- UPDATE plan SET deleted = TRUE- WHERE project = $1 AND id = $2`, deleted.ProjectID, deleted.UID)- require.NoError(t, err)- linked := createPlan("linked", changeConfig("linked", ""))- _, err = stores.CreateIssue(ctx, &store.IssueMessage{- ProjectID: linked.ProjectID, CreatorEmail: "[email protected]", PlanUID: &linked.UID,- Title: "linked issue", Type: storepb.Issue_DATABASE_CHANGE, Payload: &storepb.Issue{},+ _, err := validateSpecs(ctx, nil, "project-a", []*v1pb.Plan_Spec{+ {Id: "create", Config: &v1pb.Plan_Spec_CreateDatabaseConfig{CreateDatabaseConfig: &v1pb.Plan_CreateDatabaseConfig{}}},+ {Id: "change", Config: &v1pb.Plan_Spec_ChangeDatabaseConfig{ChangeDatabaseConfig: &v1pb.Plan_ChangeDatabaseConfig{}}},})- require.NoError(t, err)-- response, err := service.ListPlans(ctx, connect.NewRequest(&v1pb.ListPlansRequest{- Parent: "projects/project-a",- PageSize: 100,- }))- require.NoError(t, err)- var got []string- for _, plan := range response.Msg.Plans {- got = append(got, plan.Title)- }- require.ElementsMatch(t, []string{gitOps.Name, deleted.Name, linked.Name}, got)-- gotMalformed, err := service.GetPlan(ctx, connect.NewRequest(&v1pb.GetPlanRequest{- Name: fmt.Sprintf("projects/project-a/plans/%d", oldMalformed.UID),- }))- require.NoError(t, err)- require.Equal(t, oldMalformed.Name, gotMalformed.Msg.Title)-}--func TestPlanServiceCreatePlanRecordsAuthenticatedPrincipalAsLastEditor(t *testing.T) {- stores := setupPlanServiceTestStore(context.Background(), t)- eventBus, err := bus.New()- require.NoError(t, err)- service := NewPlanService(stores, eventBus, nil, nil, nil)-- for _, test := range []struct {- name string- email string- principalType storepb.PrincipalType- }{- {name: "service account", email: "[email protected]", principalType: storepb.PrincipalType_SERVICE_ACCOUNT},- {name: "workload identity", email: "[email protected]", principalType: storepb.PrincipalType_WORKLOAD_IDENTITY},- } {- t.Run(test.name, func(t *testing.T) {- ctx := context.WithValue(context.Background(), common.WorkspaceIDContextKey, "default")- ctx = context.WithValue(ctx, common.UserContextKey, &store.UserMessage{- Email: test.email,- Name: test.name,- Type: test.principalType,- })-- created, err := service.CreatePlan(ctx, connect.NewRequest(&v1pb.CreatePlanRequest{- Parent: "projects/project-a",- Plan: &v1pb.Plan{- Title: test.name,- Specs: []*v1pb.Plan_Spec{{- Id: "create-" + test.principalType.String(),- Config: &v1pb.Plan_Spec_CreateDatabaseConfig{- CreateDatabaseConfig: &v1pb.Plan_CreateDatabaseConfig{},- },- }},- },- }))- require.NoError(t, err)- require.Equal(t, common.FormatUserEmail(test.email), created.Msg.LastPlanEditor)- })- }-}--func TestPlanServiceReleaseBackedPlanKeepsCreatorAsLastEditor(t *testing.T) {- t.Parallel()- ctx := context.WithValue(context.Background(), common.WorkspaceIDContextKey, "default")- ctx = context.WithValue(ctx, common.UserContextKey, &store.UserMessage{Email: "[email protected]", Name: "creator"})- stores := setupPlanServiceTestStore(ctx, t)- plan, err := stores.CreatePlan(ctx, &store.PlanMessage{- ProjectID: "project-a",- Name: "release-backed",- Config: &storepb.PlanConfig{Specs: []*storepb.PlanConfig_Spec{{- Id: "release",- Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{- ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{Release: "projects/project-a/releases/release-a"},- },- }}},- }, "[email protected]")- require.NoError(t, err)- require.Equal(t, "[email protected]", *plan.LastPlanEditor)-- service := NewPlanService(stores, nil, nil, nil, nil)- _, err = service.UpdatePlan(ctx, connect.NewRequest(&v1pb.UpdatePlanRequest{- Plan: &v1pb.Plan{- Name: common.FormatPlan(plan.ProjectID, plan.UID),- Specs: []*v1pb.Plan_Spec{{- Id: "replacement",- Config: &v1pb.Plan_Spec_ChangeDatabaseConfig{- ChangeDatabaseConfig: &v1pb.Plan_ChangeDatabaseConfig{},- },- }},- },- UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"specs"}},- }))- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))- require.ErrorContains(t, err, "created from a release")+ require.ErrorContains(t, err, "each plan must contain only one type")- got, err := stores.GetPlan(ctx, &store.FindPlanMessage{ProjectID: plan.ProjectID, UID: &plan.UID})- require.NoError(t, err)- require.Equal(t, "[email protected]", *got.LastPlanEditor)+ _, err = validateSpecs(ctx, nil, "project-a", []*v1pb.Plan_Spec{{Id: "spec-1"}})+ require.ErrorContains(t, err, "invalid spec type")}-func TestPlanServiceListPlansIncludesIssueStatus(t *testing.T) {+// TestBuildV1PlansScopesRelationsByProject pins the join the plan list depends+// on: issue status, approval status, plan check counts and rollout stage+// summaries are attached by (project, plan UID), never by UID alone — plan+// UIDs are allocated per project, so two projects' plans share every number.+func TestBuildV1PlansScopesRelationsByProject(t *testing.T) {t.Parallel()- ctx := context.Background()- stores := setupPlanServiceTestStore(ctx, t)- service := NewPlanService(stores, nil, nil, nil, nil)-- createPlan := func(title, release string) *store.PlanMessage {- t.Helper()- plan, err := stores.CreatePlan(ctx, &store.PlanMessage{- ProjectID: "project-a",- Name: title,- Config: &storepb.PlanConfig{Specs: []*storepb.PlanConfig_Spec{{- Id: title,- Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{- ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{Release: release},- },- }}},- }, "[email protected]")- require.NoError(t, err)- return plan+ changeConfig := &storepb.PlanConfig{Specs: []*storepb.PlanConfig_Spec{{+ Id: "change",+ Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{}},+ }}}+ plans := []*store.PlanMessage{+ {ProjectID: "project-a", UID: 101, Name: "canceled in a", Config: changeConfig},+ {ProjectID: "project-b", UID: 101, Name: "done in b", Config: changeConfig},+ {ProjectID: "project-a", UID: 102, Name: "draft", Config: changeConfig},+ {ProjectID: "project-a", UID: 103, Name: "open and skipped", Config: changeConfig},+ {ProjectID: "project-a", UID: 104, Name: "no issue", Config: changeConfig},}- createIssue := func(plan *store.PlanMessage, draft bool, status storepb.Issue_Status) {- t.Helper()- issue, err := stores.CreateIssue(ctx, &store.IssueMessage{- ProjectID: plan.ProjectID,- CreatorEmail: "[email protected]",- PlanUID: &plan.UID,- Title: plan.Name,- Type: storepb.Issue_DATABASE_CHANGE,- Payload: &storepb.Issue{- Draft: draft,- Approval: &storepb.IssuePayloadApproval{- ApprovalFindingDone: true,- },- },- })- require.NoError(t, err)- if status != storepb.Issue_OPEN {- _, err = stores.UpdateIssue(ctx, issue.ProjectID, issue.UID, &store.UpdateIssueMessage{Status: &status})- require.NoError(t, err)- }+ planUID := func(uid int64) *int64 { return &uid }+ issues := []*store.IssueMessage{+ {ProjectID: "project-a", UID: 1, PlanUID: planUID(101), Status: storepb.Issue_CANCELED, Payload: &storepb.Issue{}},+ {ProjectID: "project-b", UID: 1, PlanUID: planUID(101), Status: storepb.Issue_DONE, Payload: &storepb.Issue{}},+ {ProjectID: "project-a", UID: 2, PlanUID: planUID(102), Status: storepb.Issue_OPEN, Payload: &storepb.Issue{+ Draft: true,+ Approval: &storepb.IssuePayloadApproval{ApprovalFindingDone: true},+ }},+ {ProjectID: "project-a", UID: 3, PlanUID: planUID(103), Status: storepb.Issue_OPEN, Payload: &storepb.Issue{+ Approval: &storepb.IssuePayloadApproval{ApprovalFindingDone: true},+ }},}- createPlan("no issue", "projects/project-a/releases/release-a")- draftPlan := createPlan("draft", "")- createIssue(draftPlan, true, storepb.Issue_OPEN)- openPlan := createPlan("open", "")- createIssue(openPlan, false, storepb.Issue_OPEN)- donePlan := createPlan("done", "")- createIssue(donePlan, false, storepb.Issue_DONE)- canceledPlan := createPlan("canceled", "")- createIssue(canceledPlan, false, storepb.Issue_CANCELED)-- response, err := service.ListPlans(ctx, connect.NewRequest(&v1pb.ListPlansRequest{- Parent: "projects/project-a",- PageSize: 100,- }))- require.NoError(t, err)- require.Len(t, response.Msg.Plans, 5)- plansByTitle := make(map[string]*v1pb.Plan, len(response.Msg.Plans))- for _, plan := range response.Msg.Plans {- plansByTitle[plan.Title] = plan+ planCheckRuns := []*store.PlanCheckRunMessage{+ {ProjectID: "project-a", PlanUID: 101, Status: store.PlanCheckRunStatusDone, Result: &storepb.PlanCheckRunResult{+ Results: []*storepb.PlanCheckRunResult_Result{{Status: storepb.Advice_WARNING}},+ }},+ {ProjectID: "project-b", PlanUID: 101, Status: store.PlanCheckRunStatusFailed, Result: &storepb.PlanCheckRunResult{}},}-- require.Empty(t, plansByTitle["no issue"].Issue)- require.Equal(t, v1pb.IssueStatus_ISSUE_STATUS_UNSPECIFIED, plansByTitle["no issue"].IssueStatus)- require.Equal(t, v1pb.ApprovalStatus_APPROVAL_STATUS_UNSPECIFIED, plansByTitle["no issue"].ApprovalStatus)-- require.NotEmpty(t, plansByTitle["draft"].Issue)- require.Equal(t, v1pb.IssueStatus_OPEN, plansByTitle["draft"].IssueStatus)- require.Equal(t, v1pb.ApprovalStatus_APPROVAL_STATUS_UNSPECIFIED, plansByTitle["draft"].ApprovalStatus)-- require.Equal(t, v1pb.IssueStatus_OPEN, plansByTitle["open"].IssueStatus)- require.Equal(t, v1pb.ApprovalStatus_SKIPPED, plansByTitle["open"].ApprovalStatus)- require.Equal(t, v1pb.IssueStatus_DONE, plansByTitle["done"].IssueStatus)- require.Equal(t, v1pb.IssueStatus_CANCELED, plansByTitle["canceled"].IssueStatus)-}--func TestConvertToPlansScopesIssueStatusByProject(t *testing.T) {- t.Parallel()- ctx := context.Background()- stores := setupPlanServiceTestStore(ctx, t)- _, err := stores.GetDB().ExecContext(ctx, `- INSERT INTO project (resource_id, workspace, name)- VALUES ('project-b', 'default', 'Project B')- `)- require.NoError(t, err)-- createPlanWithIssue := func(projectID string, status storepb.Issue_Status) *store.PlanMessage {- t.Helper()- plan, err := stores.CreatePlan(ctx, &store.PlanMessage{- ProjectID: projectID,- Name: projectID,- Config: &storepb.PlanConfig{Specs: []*storepb.PlanConfig_Spec{{- Id: projectID,- Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{- ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{},- },- }}},- }, "[email protected]")- require.NoError(t, err)- issue, err := stores.CreateIssue(ctx, &store.IssueMessage{- ProjectID: projectID,- CreatorEmail: "[email protected]",- PlanUID: &plan.UID,- Title: projectID,- Type: storepb.Issue_DATABASE_CHANGE,- Payload: &storepb.Issue{},- })- require.NoError(t, err)- if status != storepb.Issue_OPEN {- _, err = stores.UpdateIssue(ctx, projectID, issue.UID, &store.UpdateIssueMessage{Status: &status})- require.NoError(t, err)- }- return plan+ taskStatusCounts := []*store.TaskStatusCount{+ {ProjectID: "project-a", PlanID: 101, Environment: "prod", Status: storepb.TaskRun_DONE.String(), Count: 2},+ {ProjectID: "project-b", PlanID: 101, Environment: "prod", Status: storepb.TaskRun_FAILED.String(), Count: 1},}- planA := createPlanWithIssue("project-a", storepb.Issue_CANCELED)- planB := createPlanWithIssue("project-b", storepb.Issue_DONE)- require.Equal(t, planA.UID, planB.UID)-- plans, err := convertToPlans(ctx, stores, []*store.PlanMessage{planA, planB})- require.NoError(t, err)- require.Equal(t, v1pb.IssueStatus_CANCELED, plans[0].IssueStatus)- require.Equal(t, v1pb.IssueStatus_DONE, plans[1].IssueStatus)-}--func TestPlanServiceCreatePlanRejectsMixedDatabaseSpecs(t *testing.T) {- t.Parallel()- ctx := context.WithValue(context.Background(), common.WorkspaceIDContextKey, "default")- ctx = context.WithValue(ctx, common.UserContextKey, &store.UserMessage{- Email: "[email protected]",- Name: "creator",- })- stores := setupPlanServiceTestStore(ctx, t)- service := NewPlanService(stores, nil, nil, nil, nil)-- _, err := service.CreatePlan(ctx, connect.NewRequest(&v1pb.CreatePlanRequest{- Parent: "projects/project-a",- Plan: &v1pb.Plan{- Specs: []*v1pb.Plan_Spec{- {- Id: "create",- Config: &v1pb.Plan_Spec_CreateDatabaseConfig{- CreateDatabaseConfig: &v1pb.Plan_CreateDatabaseConfig{},- },- },- {- Id: "change",- Config: &v1pb.Plan_Spec_ChangeDatabaseConfig{- ChangeDatabaseConfig: &v1pb.Plan_ChangeDatabaseConfig{},- },- },- },- },- }))- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))- require.ErrorContains(t, err, "each plan must contain only one type")-}--// TestCreatePlanRejectsSpecsWithoutConfig pins the retired export_data_config-// spec: a legacy client sending the removed field decodes to a spec with no-// config, which creation must reject.-func TestCreatePlanRejectsSpecsWithoutConfig(t *testing.T) {- t.Parallel()- ctx := context.Background()- ctx = context.WithValue(ctx, common.WorkspaceIDContextKey, "default")- ctx = context.WithValue(ctx, common.UserContextKey, &store.UserMessage{Email: "[email protected]", Name: "creator"})- stores := setupPlanServiceTestStore(ctx, t)- service := NewPlanService(stores, nil, nil, nil, nil)-- _, err := service.CreatePlan(ctx, connect.NewRequest(&v1pb.CreatePlanRequest{- Parent: "projects/project-a",- Plan: &v1pb.Plan{- Title: "legacy export plan",- Specs: []*v1pb.Plan_Spec{{Id: "spec-1"}},- },- }))- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))-}--func setupPlanServiceTestStore(ctx context.Context, t *testing.T) *store.Store {- t.Helper()-- db, s, _ := testcontainer.NewMetadataDB(t)-- _, err := db.ExecContext(ctx, `- INSERT INTO workspace (resource_id) VALUES ('default');- INSERT INTO principal (name, email, password_hash) VALUES ('creator', '[email protected]', 'unused');- INSERT INTO project (resource_id, workspace, name) VALUES ('project-a', 'default', 'Project A');- `)- require.NoError(t, err)+ got := buildV1Plans(plans, issues, planCheckRuns, taskStatusCounts, map[string]int{"prod": 0})+ require.Len(t, got, len(plans))+ byTitle := map[string]*v1pb.Plan{}+ for _, plan := range got {+ byTitle[plan.Title] = plan+ }- return s+ require.Equal(t, "projects/project-a/issues/1", byTitle["canceled in a"].Issue)+ require.Equal(t, v1pb.IssueStatus_CANCELED, byTitle["canceled in a"].IssueStatus)+ require.Equal(t, "projects/project-b/issues/1", byTitle["done in b"].Issue)+ require.Equal(t, v1pb.IssueStatus_DONE, byTitle["done in b"].IssueStatus)++ require.Equal(t, map[string]int32{"DONE": 1, "WARNING": 1}, byTitle["canceled in a"].PlanCheckRunStatusCount)+ require.Equal(t, map[string]int32{"FAILED": 1}, byTitle["done in b"].PlanCheckRunStatusCount)+ require.Empty(t, byTitle["draft"].PlanCheckRunStatusCount)+ stageCounts := func(plan *v1pb.Plan) []*v1pb.Plan_TaskStatusCount {+ require.Len(t, plan.RolloutStageSummaries, 1)+ return plan.RolloutStageSummaries[0].TaskStatusCounts+ }+ require.Len(t, stageCounts(byTitle["canceled in a"]), 1)+ require.Equal(t, v1pb.Task_DONE, stageCounts(byTitle["canceled in a"])[0].Status)+ require.EqualValues(t, 2, stageCounts(byTitle["canceled in a"])[0].Count)+ require.Equal(t, v1pb.Task_FAILED, stageCounts(byTitle["done in b"])[0].Status)+ require.Empty(t, byTitle["draft"].RolloutStageSummaries)++ require.Equal(t, v1pb.IssueStatus_OPEN, byTitle["draft"].IssueStatus)+ require.Equal(t, v1pb.ApprovalStatus_APPROVAL_STATUS_UNSPECIFIED, byTitle["draft"].ApprovalStatus,+ "a draft has no approval yet, whatever its payload says")+ require.Equal(t, v1pb.IssueStatus_OPEN, byTitle["open and skipped"].IssueStatus)+ require.Equal(t, v1pb.ApprovalStatus_SKIPPED, byTitle["open and skipped"].ApprovalStatus)++ require.Empty(t, byTitle["no issue"].Issue)+ require.Equal(t, v1pb.IssueStatus_ISSUE_STATUS_UNSPECIFIED, byTitle["no issue"].IssueStatus)+ require.Equal(t, v1pb.ApprovalStatus_APPROVAL_STATUS_UNSPECIFIED, byTitle["no issue"].ApprovalStatus)}diff --git a/backend/api/v1/project_instance_lifecycle_api_test.go b/backend/api/v1/project_instance_lifecycle_api_test.godeleted file mode 100644index 8dfb2045ad..0000000000--- a/backend/api/v1/project_instance_lifecycle_api_test.go+++ /dev/null@@ -1,278 +0,0 @@-package v1--import (- "context"- "errors"- "os"- "path/filepath"- "testing"- "time"-- "github.com/bytebase/bytebase/backend/common/testcontainer"-- "connectrpc.com/connect"- "github.com/stretchr/testify/require"- "google.golang.org/protobuf/encoding/protojson"-- "github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/component/config"- "github.com/bytebase/bytebase/backend/component/sample"- "github.com/bytebase/bytebase/backend/component/sample/selfhost"- "github.com/bytebase/bytebase/backend/enterprise"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"- "github.com/bytebase/bytebase/backend/generated-go/v1/v1connect"- "github.com/bytebase/bytebase/backend/store"-)--func TestProjectInstanceLifecycleAPIGatesArchivedProjectDescendants(t *testing.T) {- t.Parallel()- ctx, stores, projectID, instanceID, databaseName := setupProjectInstanceLifecycleAPITest(t)- _, err := stores.UpdateInstance(ctx, &store.UpdateInstanceMessage{- ResourceID: &instanceID,- Workspace: "default",- Metadata: &storepb.Instance{- DataSources: []*storepb.DataSource{{Id: "admin", Type: storepb.DataSourceType_ADMIN}},- },- })- require.NoError(t, err)- licenseService := newInstanceServiceTestLicenseService(t, stores)- databaseLicenseService, err := enterprise.NewLicenseService(common.ReleaseModeDev, stores, false, "")- require.NoError(t, err)- projectService := NewProjectService(stores, nil, nil, nil)- instanceService := &InstanceService{store: stores, licenseService: licenseService}- databaseService := NewDatabaseService(stores, nil, nil, nil, databaseLicenseService)- instanceName := common.FormatProjectInstance(projectID, instanceID)- projectName := common.FormatProject(projectID)- databaseResourceName := common.FormatProjectDatabase(projectID, instanceID, databaseName)-- _, err = projectService.DeleteProject(ctx, connect.NewRequest(&v1pb.DeleteProjectRequest{- Name: common.FormatProject("project-b"),- Purge: true,- }))- require.Equal(t, connect.CodeFailedPrecondition, connect.CodeOf(err))-- _, err = instanceService.DeleteInstance(ctx, connect.NewRequest(&v1pb.DeleteInstanceRequest{Name: instanceName}))- require.NoError(t, err)- undeleted, err := instanceService.UndeleteInstance(ctx, connect.NewRequest(&v1pb.UndeleteInstanceRequest{Name: instanceName}))- require.NoError(t, err)- require.False(t, undeleted.Msg.Activation)- _, err = instanceService.DeleteInstance(ctx, connect.NewRequest(&v1pb.DeleteInstanceRequest{Name: instanceName, Force: true}))- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))-- _, err = projectService.DeleteProject(ctx, connect.NewRequest(&v1pb.DeleteProjectRequest{Name: projectName}))- require.NoError(t, err)-- _, err = instanceService.GetInstance(ctx, connect.NewRequest(&v1pb.GetInstanceRequest{Name: instanceName}))- require.Equal(t, connect.CodeNotFound, connect.CodeOf(err))- _, err = databaseService.GetDatabase(ctx, connect.NewRequest(&v1pb.GetDatabaseRequest{Name: databaseResourceName}))- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))- _, err = resolveRawResource(ctx, stores, databaseResourceName)- require.Equal(t, connect.CodeNotFound, connect.CodeOf(err))- resources, err := populateRawResources(ctx, stores, &v1pb.UndeleteInstanceRequest{Name: instanceName}, v1connect.InstanceServiceUndeleteInstanceProcedure)- require.NoError(t, err)- require.Equal(t, []*common.Resource{{Type: common.ResourceTypeProject, ID: projectID}}, resources)-- var databaseCount int- require.NoError(t, stores.GetDB().QueryRowContext(ctx, "SELECT COUNT(*) FROM db WHERE instance = $1 AND name = $2", instanceID, databaseName).Scan(&databaseCount))- require.Equal(t, 1, databaseCount)-- _, err = instanceService.DeleteInstance(ctx, connect.NewRequest(&v1pb.DeleteInstanceRequest{Name: instanceName}))- require.NoError(t, err)- undeleted, err = instanceService.UndeleteInstance(ctx, connect.NewRequest(&v1pb.UndeleteInstanceRequest{Name: instanceName}))- require.NoError(t, err)- require.False(t, undeleted.Msg.Activation)-- _, err = projectService.UndeleteProject(ctx, connect.NewRequest(&v1pb.UndeleteProjectRequest{Name: projectName}))- require.NoError(t, err)- got, err := instanceService.GetInstance(ctx, connect.NewRequest(&v1pb.GetInstanceRequest{Name: instanceName}))- require.NoError(t, err)- require.False(t, got.Msg.Activation)-}--func TestProjectPurgeCleansSampleBeforeDeletingMetadata(t *testing.T) {- t.Parallel()- ctx, stores, projectID, instanceID, _ := setupProjectInstanceLifecycleAPITest(t)- manager := &sampleManagerStub{}- cleanupErr := errors.New("sample cleanup failed")- manager.projectPurge = func(ctx context.Context, workspaceID, gotProjectID string) error {- require.Equal(t, "default", workspaceID)- require.Equal(t, projectID, gotProjectID)- instance, err := stores.GetInstance(ctx, &store.FindInstanceMessage{- Workspace: workspaceID,- ProjectID: &gotProjectID,- ResourceID: &instanceID,- })- require.NoError(t, err)- require.NotNil(t, instance)- if cleanupErr != nil {- return cleanupErr- }- return nil- }- projectService := NewProjectService(stores, nil, nil, manager)- projectName := common.FormatProject(projectID)-- _, err := projectService.DeleteProject(ctx, connect.NewRequest(&v1pb.DeleteProjectRequest{Name: projectName}))- require.NoError(t, err)- _, err = projectService.DeleteProject(ctx, connect.NewRequest(&v1pb.DeleteProjectRequest{Name: projectName, Purge: true}))- require.Equal(t, connect.CodeInternal, connect.CodeOf(err))- project, err := stores.GetProject(ctx, &store.FindProjectMessage{- Workspace: "default",- ResourceID: &projectID,- ShowDeleted: true,- })- require.NoError(t, err)- require.NotNil(t, project)-- cleanupErr = nil- _, err = projectService.DeleteProject(ctx, connect.NewRequest(&v1pb.DeleteProjectRequest{Name: projectName, Purge: true}))- require.NoError(t, err)- require.Equal(t, []string{projectID, projectID}, manager.projectPurgeCalls)-- project, err = stores.GetProject(ctx, &store.FindProjectMessage{- Workspace: "default",- ResourceID: &projectID,- ShowDeleted: true,- })- require.NoError(t, err)- require.Nil(t, project)-}--func TestProjectPurgeRemovesSelfHostSample(t *testing.T) {- t.Parallel()- ctx, stores, projectID, instanceID, databaseName := setupProjectInstanceLifecycleAPITest(t)- payload, err := protojson.Marshal(&storepb.SelfHostSampleInstanceSetupPayload{- Instances: []*storepb.SelfHostSampleInstanceSetupPayload_Instance{{- InstanceId: instanceID,- ProjectId: &projectID,- Title: "Sample Project Instance",- DatabaseName: databaseName,- RoleName: "sample-role",- }},- })- require.NoError(t, err)- const replicaID = "replica-a"- _, created, err := stores.ReserveSampleInstanceSetup(ctx, &store.SampleInstanceSetupMessage{- WorkspaceID: "default",- ReplicaID: replicaID,- Payload: payload,- })- require.NoError(t, err)- require.True(t, created)- activated, err := stores.ActivateSampleInstanceSetup(ctx, "default", replicaID, []string{projectID}, time.Now(), nil)- require.NoError(t, err)- require.True(t, activated)-- dataRoot := t.TempDir()- dataDir := filepath.Join(dataRoot, "pgdata-sample-managed", instanceID)- require.NoError(t, os.MkdirAll(dataDir, 0o755))- require.NoError(t, os.WriteFile(filepath.Join(dataDir, "sample-data"), []byte("sample"), 0o644))- manager := selfhost.NewManager(- stores,- &config.Profile{DataDir: dataRoot, Port: 8080},- nil,- sample.ManagerOptions{ReplicaID: replicaID},- )- projectService := NewProjectService(stores, nil, nil, manager)- projectName := common.FormatProject(projectID)-- _, err = projectService.DeleteProject(ctx, connect.NewRequest(&v1pb.DeleteProjectRequest{Name: projectName}))- require.NoError(t, err)- _, err = projectService.DeleteProject(ctx, connect.NewRequest(&v1pb.DeleteProjectRequest{Name: projectName, Purge: true}))- require.NoError(t, err)-- _, err = os.Stat(dataDir)- require.ErrorIs(t, err, os.ErrNotExist)- setup, err := stores.GetSampleInstanceSetup(ctx, "default")- require.NoError(t, err)- require.NotNil(t, setup)- require.NotNil(t, setup.DeletedAt)- instances, err := manager.ListInstances(ctx, "default")- require.NoError(t, err)- require.Len(t, instances, 1)- require.Equal(t, common.FormatProjectInstance(projectID, instanceID), instances[0].Name)- require.Nil(t, instances[0].ExpireTime)-}--func TestBatchSyncDatabasesValidatesAllTargetsBeforeScheduling(t *testing.T) {- t.Parallel()- ctx, stores, projectID, instanceID, databaseName := setupProjectInstanceLifecycleAPITest(t)- licenseService, err := enterprise.NewLicenseService(common.ReleaseModeDev, stores, false, "")- require.NoError(t, err)- service := NewDatabaseService(stores, nil, nil, nil, licenseService)-- _, err = service.BatchSyncDatabases(ctx, connect.NewRequest(&v1pb.BatchSyncDatabasesRequest{- Parent: common.FormatProjectInstance(projectID, instanceID),- Names: []string{- common.FormatProjectDatabase(projectID, instanceID, databaseName),- "invalid",- },- }))- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))-}--func TestUndeleteProjectInstanceChecksActivationLimit(t *testing.T) {- t.Parallel()- ctx, stores, projectID, instanceID, _ := setupProjectInstanceLifecycleAPITest(t)- licenseService := newInstanceServiceTestLicenseService(t, stores)- instanceService := &InstanceService{store: stores, licenseService: licenseService}- instanceName := common.FormatProjectInstance(projectID, instanceID)-- _, err := instanceService.DeleteInstance(ctx, connect.NewRequest(&v1pb.DeleteInstanceRequest{Name: instanceName}))- require.NoError(t, err)-- _, err = instanceService.UndeleteInstance(ctx, connect.NewRequest(&v1pb.UndeleteInstanceRequest{Name: instanceName}))- require.Equal(t, connect.CodeResourceExhausted, connect.CodeOf(err))-- instance, err := getInstanceMessageForLifecycle(ctx, stores, instanceName)- require.NoError(t, err)- require.True(t, instance.Deleted)- require.True(t, instance.Metadata.GetActivation())-}--func newInstanceServiceTestLicenseService(t *testing.T, stores *store.Store) *enterprise.LicenseService {- t.Helper()- licenseService, err := enterprise.NewLicenseService(common.ReleaseModeDev, stores, false, "")- require.NoError(t, err)- return licenseService-}--func setupProjectInstanceLifecycleAPITest(t *testing.T) (context.Context, *store.Store, string, string, string) {- t.Helper()- ctx := context.WithValue(context.Background(), common.WorkspaceIDContextKey, "default")- db, stores, _ := testcontainer.NewMetadataDB(t)- _, err := db.ExecContext(ctx, `- INSERT INTO workspace (resource_id) VALUES ('default');- INSERT INTO project (resource_id, workspace, name) VALUES- ('project-a', 'default', 'Project A'),- ('project-b', 'default', 'Project B');- `)- require.NoError(t, err)-- projectID := "project-a"- instanceID := "project-instance"- _, err = stores.CreateInstance(ctx, &store.InstanceMessage{- ResourceID: instanceID,- Workspace: "default",- ProjectID: &projectID,- Metadata: &storepb.Instance{- Activation: true,- DataSources: []*storepb.DataSource{{- Id: "admin",- Type: storepb.DataSourceType_ADMIN,- }},- },- })- require.NoError(t, err)-- databaseName := "app"- _, err = stores.UpsertDatabase(ctx, &store.DatabaseMessage{- InstanceID: instanceID,- DatabaseName: databaseName,- ProjectID: projectID,- Metadata: &storepb.DatabaseMetadata{},- })- require.NoError(t, err)- return ctx, stores, projectID, instanceID, databaseName-}diff --git a/backend/api/v1/project_instance_release_check_test.go b/backend/api/v1/project_instance_release_check_test.godeleted file mode 100644index 757658a106..0000000000--- a/backend/api/v1/project_instance_release_check_test.go+++ /dev/null@@ -1,171 +0,0 @@-package v1--import (- "context"- "fmt"- "testing"-- "github.com/bytebase/bytebase/backend/common/testcontainer"-- "connectrpc.com/connect"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/common"- "github.com/bytebase/bytebase/backend/component/sheet"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"- v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"- "github.com/bytebase/bytebase/backend/store"-)--func TestCheckReleaseDatabaseTargetsKeepCanonicalNames(t *testing.T) {- t.Parallel()- ctx, stores, instanceID, databaseName := setupProjectInstanceReleaseCheckTest(t)- projectID := "project-a"- projectTarget := common.FormatProjectDatabase(projectID, instanceID, databaseName)- workspaceTarget := common.FormatDatabase(instanceID, databaseName)-- response, err := NewReleaseService(stores, sheet.NewManager(), nil, nil).CheckRelease(ctx, connect.NewRequest(&v1pb.CheckReleaseRequest{- Parent: common.FormatProject(projectID),- Release: declarativeReleaseWithDisallowedStatement(),- Targets: []string{projectTarget},- }))- require.NoError(t, err)- require.Len(t, response.Msg.Results, 1)- require.Equal(t, projectTarget, response.Msg.Results[0].Target)-- _, err = NewReleaseService(stores, sheet.NewManager(), nil, nil).CheckRelease(ctx, connect.NewRequest(&v1pb.CheckReleaseRequest{- Parent: common.FormatProject(projectID),- Release: declarativeReleaseWithDisallowedStatement(),- Targets: []string{workspaceTarget},- }))- require.Equal(t, connect.CodeNotFound, connect.CodeOf(err))-}--func TestCheckReleaseRejectsCrossProjectTargets(t *testing.T) {- t.Parallel()- ctx, stores, instanceID, databaseName := setupProjectInstanceReleaseCheckTest(t)- service := NewReleaseService(stores, sheet.NewManager(), nil, nil)- request := func(target string) *connect.Request[v1pb.CheckReleaseRequest] {- return connect.NewRequest(&v1pb.CheckReleaseRequest{- Parent: common.FormatProject("project-a"),- Release: declarativeReleaseWithDisallowedStatement(),- Targets: []string{target},- })- }-- for _, target := range []string{- common.FormatProjectDatabase("project-b", instanceID, databaseName),- common.FormatProject("project-b") + "/databaseGroups/all",- } {- _, err := service.CheckRelease(ctx, request(target))- require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err), target)- }-}--func TestCheckReleaseHidesDatabasesOutsideProject(t *testing.T) {- t.Parallel()- ctx, stores, _, _ := setupProjectInstanceReleaseCheckTest(t)- sharedInstanceID := "shared-instance"- _, err := stores.CreateInstance(ctx, &store.InstanceMessage{- ResourceID: sharedInstanceID,- Workspace: "default",- Metadata: &storepb.Instance{- Engine: storepb.Engine_POSTGRES,- DataSources: []*storepb.DataSource{{- Id: "admin",- Type: storepb.DataSourceType_ADMIN,- }},- },- })- require.NoError(t, err)- _, err = stores.UpsertDatabase(ctx, &store.DatabaseMessage{- InstanceID: sharedInstanceID,- DatabaseName: "other",- ProjectID: "project-b",- Metadata: &storepb.DatabaseMetadata{},- })- require.NoError(t, err)-- // A database in another project and one that does not exist must be- // indistinguishable, or the check maps databases workspace-wide.- service := NewReleaseService(stores, sheet.NewManager(), nil, nil)- for _, target := range []string{- common.FormatDatabase(sharedInstanceID, "other"),- common.FormatDatabase(sharedInstanceID, "missing"),- } {- _, err := service.CheckRelease(ctx, connect.NewRequest(&v1pb.CheckReleaseRequest{- Parent: common.FormatProject("project-a"),- Release: declarativeReleaseWithDisallowedStatement(),- Targets: []string{target},- }))- require.EqualError(t, err, fmt.Sprintf("not_found: database %s not found", target))- }-}--func TestCheckReleaseRejectsArchivedProject(t *testing.T) {- t.Parallel()- ctx, stores, instanceID, databaseName := setupProjectInstanceReleaseCheckTest(t)- archived := true- require.NoError(t, stores.UpdateProjects(ctx, &store.UpdateProjectMessage{- ResourceID: "project-a",- Workspace: "default",- Delete: &archived,- }))-- _, err := NewReleaseService(stores, sheet.NewManager(), nil, nil).CheckRelease(ctx, connect.NewRequest(&v1pb.CheckReleaseRequest{- Parent: common.FormatProject("project-a"),- Release: declarativeReleaseWithDisallowedStatement(),- Targets: []string{common.FormatProjectDatabase("project-a", instanceID, databaseName)},- }))- require.Equal(t, connect.CodeFailedPrecondition, connect.CodeOf(err))-}--func declarativeReleaseWithDisallowedStatement() *v1pb.Release {- return &v1pb.Release{- Type: v1pb.Release_DECLARATIVE,- Files: []*v1pb.Release_File{{- Path: "schema.sql",- Version: "1",- Statement: []byte("DROP TABLE obsolete;"),- }},- }-}--func setupProjectInstanceReleaseCheckTest(t *testing.T) (context.Context, *store.Store, string, string) {- t.Helper()- ctx := context.WithValue(context.Background(), common.WorkspaceIDContextKey, "default")- db, stores, _ := testcontainer.NewMetadataDB(t)- _, err := db.ExecContext(ctx, `- INSERT INTO workspace (resource_id) VALUES ('default');- INSERT INTO project (resource_id, workspace, name) VALUES- ('project-a', 'default', 'Project A'),- ('project-b', 'default', 'Project B');- `)- require.NoError(t, err)-- projectID := "project-a"- instanceID := "project-instance"- _, err = stores.CreateInstance(ctx, &store.InstanceMessage{- ResourceID: instanceID,- Workspace: "default",- ProjectID: &projectID,- Metadata: &storepb.Instance{- Engine: storepb.Engine_POSTGRES,- DataSources: []*storepb.DataSource{{- Id: "admin",- Type: storepb.DataSourceType_ADMIN,- }},- },- })- require.NoError(t, err)-- databaseName := "app"- _, err = stores.UpsertDatabase(ctx, &store.DatabaseMessage{- InstanceID: instanceID,- DatabaseName: databaseName,- ProjectID: projectID,- Metadata: &storepb.DatabaseMetadata{},- })- require.NoError(t, err)- return ctx, stores, instanceID, databaseName-}diff --git a/backend/api/v1/project_service_test.go b/backend/api/v1/project_service_test.goindex d5d7d33446..c9eba527c5 100644--- a/backend/api/v1/project_service_test.go+++ b/backend/api/v1/project_service_test.go@@ -1,20 +1,30 @@package v1import (+ "context""fmt"+ "os"+ "path/filepath""strings""testing""time"+ "connectrpc.com/connect""github.com/google/go-cmp/cmp"+ "github.com/pkg/errors""github.com/stretchr/testify/assert""github.com/stretchr/testify/require"colorpb "google.golang.org/genproto/googleapis/type/color""google.golang.org/genproto/googleapis/type/expr"+ "google.golang.org/protobuf/encoding/protojson""google.golang.org/protobuf/testing/protocmp""google.golang.org/protobuf/types/known/durationpb""google.golang.org/protobuf/types/known/wrapperspb"+ "github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/component/config"+ "github.com/bytebase/bytebase/backend/component/sample"+ "github.com/bytebase/bytebase/backend/component/sample/selfhost"storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1""github.com/bytebase/bytebase/backend/store"@@ -576,3 +586,108 @@ func TestValidateIssueLabelsColor(t *testing.T) {})}}++func TestProjectPurgeCleansSampleBeforeDeletingMetadata(t *testing.T) {+ t.Parallel()+ ctx, stores, projectID, instanceID, _ := setupProjectInstanceLifecycleAPITest(t)+ manager := &sampleManagerStub{}+ cleanupErr := errors.New("sample cleanup failed")+ manager.projectPurge = func(ctx context.Context, workspaceID, gotProjectID string) error {+ require.Equal(t, "default", workspaceID)+ require.Equal(t, projectID, gotProjectID)+ instance, err := stores.GetInstance(ctx, &store.FindInstanceMessage{+ Workspace: workspaceID,+ ProjectID: &gotProjectID,+ ResourceID: &instanceID,+ })+ require.NoError(t, err)+ require.NotNil(t, instance)+ if cleanupErr != nil {+ return cleanupErr+ }+ return nil+ }+ projectService := NewProjectService(stores, nil, nil, manager)+ projectName := common.FormatProject(projectID)++ _, err := projectService.DeleteProject(ctx, connect.NewRequest(&v1pb.DeleteProjectRequest{Name: projectName}))+ require.NoError(t, err)+ _, err = projectService.DeleteProject(ctx, connect.NewRequest(&v1pb.DeleteProjectRequest{Name: projectName, Purge: true}))+ require.Equal(t, connect.CodeInternal, connect.CodeOf(err))+ project, err := stores.GetProject(ctx, &store.FindProjectMessage{+ Workspace: "default",+ ResourceID: &projectID,+ ShowDeleted: true,+ })+ require.NoError(t, err)+ require.NotNil(t, project)++ cleanupErr = nil+ _, err = projectService.DeleteProject(ctx, connect.NewRequest(&v1pb.DeleteProjectRequest{Name: projectName, Purge: true}))+ require.NoError(t, err)+ require.Equal(t, []string{projectID, projectID}, manager.projectPurgeCalls)++ project, err = stores.GetProject(ctx, &store.FindProjectMessage{+ Workspace: "default",+ ResourceID: &projectID,+ ShowDeleted: true,+ })+ require.NoError(t, err)+ require.Nil(t, project)+}++func TestProjectPurgeRemovesSelfHostSample(t *testing.T) {+ t.Parallel()+ ctx, stores, projectID, instanceID, databaseName := setupProjectInstanceLifecycleAPITest(t)+ payload, err := protojson.Marshal(&storepb.SelfHostSampleInstanceSetupPayload{+ Instances: []*storepb.SelfHostSampleInstanceSetupPayload_Instance{{+ InstanceId: instanceID,+ ProjectId: &projectID,+ Title: "Sample Project Instance",+ DatabaseName: databaseName,+ RoleName: "sample-role",+ }},+ })+ require.NoError(t, err)+ const replicaID = "replica-a"+ _, created, err := stores.ReserveSampleInstanceSetup(ctx, &store.SampleInstanceSetupMessage{+ WorkspaceID: "default",+ ReplicaID: replicaID,+ Payload: payload,+ })+ require.NoError(t, err)+ require.True(t, created)+ activated, err := stores.ActivateSampleInstanceSetup(ctx, "default", replicaID, []string{projectID}, time.Now(), nil)+ require.NoError(t, err)+ require.True(t, activated)++ dataRoot := t.TempDir()+ dataDir := filepath.Join(dataRoot, "pgdata-sample-managed", instanceID)+ require.NoError(t, os.MkdirAll(dataDir, 0o755))+ require.NoError(t, os.WriteFile(filepath.Join(dataDir, "sample-data"), []byte("sample"), 0o644))+ manager := selfhost.NewManager(+ stores,+ &config.Profile{DataDir: dataRoot, Port: 8080},+ nil,+ sample.ManagerOptions{ReplicaID: replicaID},+ )+ projectService := NewProjectService(stores, nil, nil, manager)+ projectName := common.FormatProject(projectID)++ _, err = projectService.DeleteProject(ctx, connect.NewRequest(&v1pb.DeleteProjectRequest{Name: projectName}))+ require.NoError(t, err)+ _, err = projectService.DeleteProject(ctx, connect.NewRequest(&v1pb.DeleteProjectRequest{Name: projectName, Purge: true}))+ require.NoError(t, err)++ _, err = os.Stat(dataDir)+ require.ErrorIs(t, err, os.ErrNotExist)+ setup, err := stores.GetSampleInstanceSetup(ctx, "default")+ require.NoError(t, err)+ require.NotNil(t, setup)+ require.NotNil(t, setup.DeletedAt)+ instances, err := manager.ListInstances(ctx, "default")+ require.NoError(t, err)+ require.Len(t, instances, 1)+ require.Equal(t, common.FormatProjectInstance(projectID, instanceID), instances[0].Name)+ require.Nil(t, instances[0].ExpireTime)+}diff --git a/backend/api/v1/release_service_check.go b/backend/api/v1/release_service_check.goindex b9d42e1805..37c5fdd207 100644--- a/backend/api/v1/release_service_check.go+++ b/backend/api/v1/release_service_check.go@@ -36,6 +36,37 @@ type releaseCheckTarget struct {name string}+// checkReleaseTargetProject refuses a database target that spells another+// project: it asks for nothing this project could hold, so it is malformed+// for this request rather than merely absent.+func checkReleaseTargetProject(projectID, target string, targetProjectID *string) error {+ if targetProjectID != nil && *targetProjectID != projectID {+ return connect.NewError(connect.CodeInvalidArgument, errors.Errorf("database target %q does not belong to project %q", target, projectID))+ }+ return nil+}++// checkReleaseDatabase refuses a target whose database is missing or belongs+// to another project. One error for both: a distinct one would confirm what+// lives in a project the caller cannot see.+func checkReleaseDatabase(projectID, target string, database *store.DatabaseMessage) error {+ if database == nil || database.ProjectID != projectID {+ return connect.NewError(connect.CodeNotFound, errors.Errorf("database %v not found", target))+ }+ return nil+}++// checkReleaseDatabaseInstance refuses a target whose instance is gone or whose+// name is not the database's own canonical one — a project instance's database+// is not reachable through the workspace form. The same error as+// checkReleaseDatabase, for the same reason.+func checkReleaseDatabaseInstance(target string, database *store.DatabaseMessage, instance *store.InstanceMessage) error {+ if instance == nil || instance.Deleted || database.ResourceName() != target {+ return connect.NewError(connect.CodeNotFound, errors.Errorf("database %v not found", target))+ }+ return nil+}+func (s *ReleaseService) CheckRelease(ctx context.Context, req *connect.Request[v1pb.CheckReleaseRequest]) (*connect.Response[v1pb.CheckReleaseResponse], error) {request := req.MsgprojectID, err := common.GetProjectID(request.GetParent())@@ -78,8 +109,8 @@ func (s *ReleaseService) CheckRelease(ctx context.Context, req *connect.Request[for _, target := range request.Targets {// Handle database target.if targetProjectID, instanceID, databaseName, err := common.GetDatabaseResourceName(target); err == nil {- if targetProjectID != nil && *targetProjectID != projectID {- return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("database target %q does not belong to project %q", target, projectID))+ if err := checkReleaseTargetProject(projectID, target, targetProjectID); err != nil {+ return nil, err}database, err := s.store.GetDatabase(ctx, &store.FindDatabaseMessage{Workspace: workspaceID,@@ -89,12 +120,8 @@ func (s *ReleaseService) CheckRelease(ctx context.Context, req *connect.Request[if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to found database %v", target))}- // One error for missing, foreign, archived, and non-canonical: a- // distinct one would confirm what lives in a project the caller- // cannot see.- notFound := connect.NewError(connect.CodeNotFound, errors.Errorf("database %v not found", target))- if database == nil || database.ProjectID != projectID {- return nil, notFound+ if err := checkReleaseDatabase(projectID, target, database); err != nil {+ return nil, err}instance, err := s.store.GetInstance(ctx, &store.FindInstanceMessage{Workspace: workspaceID,@@ -103,8 +130,8 @@ func (s *ReleaseService) CheckRelease(ctx context.Context, req *connect.Request[if err != nil {return nil, connect.NewError(connect.CodeInternal, err)}- if instance == nil || instance.Deleted || database.ResourceName() != target {- return nil, notFound+ if err := checkReleaseDatabaseInstance(target, database, instance); err != nil {+ return nil, err}targets = append(targets, &releaseCheckTarget{database: database, name: target})continuediff --git a/backend/api/v1/release_service_check_test.go b/backend/api/v1/release_service_check_test.gonew file mode 100644index 0000000000..ce64276f3c--- /dev/null+++ b/backend/api/v1/release_service_check_test.go@@ -0,0 +1,55 @@+package v1++import (+ "testing"++ "connectrpc.com/connect"+ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/store"+)++// TestCheckReleaseDatabaseTarget pins how a release check confines its+// database targets to the project it runs in. A target naming another project+// is malformed for this request; everything else that is not the project's+// own live database under its canonical name is simply not found, so the+// answer never says what lives in a project the caller cannot see. That the+// check runs end to end on a project instance is TestProjectInstanceWorkflowTargets+// in backend/tests.+func TestCheckReleaseDatabaseTarget(t *testing.T) {+ projectA, projectB := "project-a", "project-b"+ canonical := common.FormatProjectDatabase(projectA, "project-instance", "app")+ owned := func() *store.DatabaseMessage {+ return &store.DatabaseMessage{ProjectID: projectA, InstanceID: "project-instance", DatabaseName: "app", InstanceProjectID: &projectA}+ }+ live := &store.InstanceMessage{ResourceID: "project-instance", ProjectID: &projectA}++ require.Equal(t, connect.CodeInvalidArgument,+ connect.CodeOf(checkReleaseTargetProject(projectA, common.FormatProjectDatabase(projectB, "project-instance", "app"), &projectB)))+ require.NoError(t, checkReleaseTargetProject(projectA, canonical, &projectA))+ require.NoError(t, checkReleaseTargetProject(projectA, common.FormatDatabase("workspace-instance", "shared"), nil))++ require.NoError(t, checkReleaseDatabase(projectA, canonical, owned()))+ require.NoError(t, checkReleaseDatabaseInstance(canonical, owned(), live))++ for _, tc := range []struct {+ name string+ target string+ database *store.DatabaseMessage+ instance *store.InstanceMessage+ }{+ {"the workspace form of a project instance's database", common.FormatDatabase("project-instance", "app"), owned(), live},+ {"a database in another project", canonical, &store.DatabaseMessage{ProjectID: projectB, InstanceID: "project-instance", DatabaseName: "app", InstanceProjectID: &projectA}, live},+ {"an archived instance", canonical, owned(), &store.InstanceMessage{ResourceID: "project-instance", ProjectID: &projectA, Deleted: true}},+ } {+ t.Run(tc.name, func(t *testing.T) {+ err := checkReleaseDatabase(projectA, tc.target, tc.database)+ if err == nil {+ err = checkReleaseDatabaseInstance(tc.target, tc.database, tc.instance)+ }+ require.Equal(t, connect.CodeNotFound, connect.CodeOf(err))+ require.EqualError(t, err, "not_found: database "+tc.target+" not found")+ })+ }+}diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.goindex 9b9f2d1e4c..d5467ee146 100644--- a/backend/api/v1/rollout_service.go+++ b/backend/api/v1/rollout_service.go@@ -415,6 +415,27 @@ func (s *RolloutService) ListTaskRuns(ctx context.Context, req *connect.Request[}), nil}+// classifyRolloutError maps what the review workflow refused a rollout for+// onto the two answers a caller acts on: the issue is still a draft, or the+// approval the rollout was built on is no longer current. Anything else is+// passed through as it came.+func classifyRolloutError(err error) error {+ if errors.Is(err, errStaleRolloutApproval) {+ return errStaleRolloutApproval+ }+ var workflowErr *review.Error+ if errors.As(err, &workflowErr) {+ switch workflowErr.Reason {+ case review.ReasonDraftIssue:+ return errDraftIssueNotSubmitted+ case review.ReasonApprovalRequired, review.ReasonStaleInput:+ return errStaleRolloutApproval+ default:+ }+ }+ return err+}+// CreateRolloutAndPendingTasks creates rollout tasks and pending task runs.func CreateRolloutAndPendingTasks(ctx context.Context,@@ -449,20 +470,7 @@ func CreateRolloutAndPendingTasks(},})if err != nil {- if errors.Is(err, errStaleRolloutApproval) {- return errStaleRolloutApproval- }- var workflowErr *review.Error- if errors.As(err, &workflowErr) {- switch workflowErr.Reason {- case review.ReasonDraftIssue:- return errDraftIssueNotSubmitted- case review.ReasonApprovalRequired, review.ReasonStaleInput:- return errStaleRolloutApproval- default:- }- }- return err+ return classifyRolloutError(err)}tasks = result.Tasksissue = result.Issuediff --git a/backend/api/v1/rollout_service_test.go b/backend/api/v1/rollout_service_test.gonew file mode 100644index 0000000000..09ada5b7f1--- /dev/null+++ b/backend/api/v1/rollout_service_test.go@@ -0,0 +1,29 @@+package v1++import (+ "testing"++ "github.com/pkg/errors"+ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/backend/component/review"+ _ "github.com/bytebase/bytebase/backend/plugin/db/pg"+)++// TestClassifyRolloutError pins the two answers a rollout caller acts on. The+// races that produce them — an approval reset while the rollout was being+// built, a draft that was never submitted — are pinned against the database+// in component/review.+func TestClassifyRolloutError(t *testing.T) {+ t.Parallel()+ workflowErr := func(reason review.ErrorReason) error {+ return &review.Error{Code: review.ErrorFailedPrecondition, Reason: reason, Err: errors.New("refused")}+ }+ require.ErrorIs(t, classifyRolloutError(workflowErr(review.ReasonDraftIssue)), errDraftIssueNotSubmitted)+ require.True(t, IsStaleRolloutApprovalError(classifyRolloutError(workflowErr(review.ReasonApprovalRequired))))+ require.True(t, IsStaleRolloutApprovalError(classifyRolloutError(workflowErr(review.ReasonStaleInput))))+ require.True(t, IsStaleRolloutApprovalError(classifyRolloutError(errors.Wrap(errStaleRolloutApproval, "building tasks"))))++ other := errors.New("failed to get pipeline create")+ require.Equal(t, other, classifyRolloutError(other), "anything else passes through as it came")+}diff --git a/backend/component/review/draft_creation_test.go b/backend/component/review/draft_creation_test.goindex 28c6cd995b..dda20adc93 100644--- a/backend/component/review/draft_creation_test.go+++ b/backend/component/review/draft_creation_test.go@@ -45,6 +45,24 @@ func TestCreateDraftIssueRevalidatesPlanAfterProposal(t *testing.T) {},errorCode: ErrorInvalidAction,},+ {+ name: "Plan mixed",+ mutate: func(ctx context.Context, t *testing.T, stores *store.Store, plan *store.PlanMessage) {+ specs := []*storepb.PlanConfig_Spec{+ {Config: &storepb.PlanConfig_Spec_CreateDatabaseConfig{+ CreateDatabaseConfig: &storepb.PlanConfig_CreateDatabaseConfig{},+ }},+ {Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{+ ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{},+ }},+ }+ _, err := NewWorkflow(stores).UpdatePlan(ctx, UpdatePlanInput{+ Workspace: "default", ProjectID: plan.ProjectID, PlanUID: plan.UID, Specs: &specs,+ })+ require.NoError(t, err)+ },+ errorCode: ErrorInvalidAction,+ },{name: "rollout started",mutate: func(ctx context.Context, t *testing.T, stores *store.Store, plan *store.PlanMessage) {diff --git a/backend/store/issue.go b/backend/store/issue.goindex 6aaf1cccb4..9f6a6d1213 100644--- a/backend/store/issue.go+++ b/backend/store/issue.go@@ -123,10 +123,9 @@ type ProjectRole struct {Role string}-// approvalStatusExpr mirrors computeApprovalStatus in-// backend/api/v1/issue_service_converter.go branch for branch — keep the two in-// step. Payloads are marshalled with a bare protojson.Marshal, so an absent key-// is a zero value.+// approvalStatusExpr mirrors ComputeApprovalStatus branch for branch — keep+// the two in step. Payloads are marshalled with a bare protojson.Marshal, so an+// absent key is a zero value.const approvalStatusExpr = `CASEWHEN NOT COALESCE((issue.payload->'approval'->>'approvalFindingDone')::BOOLEAN, FALSE) THEN 'CHECKING'diff --git a/backend/store/issue_approval_status.go b/backend/store/issue_approval_status.gonew file mode 100644index 0000000000..2609aa3494--- /dev/null+++ b/backend/store/issue_approval_status.go@@ -0,0 +1,57 @@+package store++import (+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+)++// ComputeApprovalStatus derives an issue's approval status from its payload.+// approvalStatusExpr is the SQL derivation of the same rule, which the+// approval_status list filter runs; TestIssueApprovalStatusFilterMatchesComputation+// keeps the two in step.+func ComputeApprovalStatus(approval *storepb.IssuePayloadApproval) v1pb.ApprovalStatus {+ // If approval finding is not done, status is checking.+ // approval.GetApprovalFindingDone() returns false when approval is nil.+ if !approval.GetApprovalFindingDone() {+ return v1pb.ApprovalStatus_CHECKING+ }++ // If no approval template, approval is skipped (not required).+ if approval.GetApprovalTemplate() == nil {+ return v1pb.ApprovalStatus_SKIPPED+ }++ approvers := approval.GetApprovers()+ totalSteps := len(approval.GetApprovalTemplate().GetFlow().GetRoles())++ // If no approvers are assigned yet, it's pending.+ if len(approvers) == 0 {+ return v1pb.ApprovalStatus_PENDING+ }++ // Short-circuit: if any approver rejected, overall status is rejected.+ for _, approver := range approvers {+ if approver.GetStatus() == storepb.IssuePayloadApproval_Approver_REJECTED {+ return v1pb.ApprovalStatus_REJECTED+ }+ }++ // Each approver corresponds to one step in the approval flow, so every+ // step is approved once there are as many approvers as steps and all of+ // them approved.+ if len(approvers) >= totalSteps {+ allApproved := true+ for _, approver := range approvers {+ if approver.GetStatus() != storepb.IssuePayloadApproval_Approver_APPROVED {+ allApproved = false+ break+ }+ }+ if allApproved {+ return v1pb.ApprovalStatus_APPROVED+ }+ }++ // Otherwise, approval is pending (more steps to complete or waiting for approvals).+ return v1pb.ApprovalStatus_PENDING+}diff --git a/backend/store/issue_approval_status_test.go b/backend/store/issue_approval_status_test.gonew file mode 100644index 0000000000..9021ebba8a--- /dev/null+++ b/backend/store/issue_approval_status_test.go@@ -0,0 +1,109 @@+package store_test++import (+ "slices"+ "testing"++ "github.com/stretchr/testify/require"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+ "github.com/bytebase/bytebase/backend/store"+)++// TestIssueApprovalStatusFilterMatchesComputation pins the SQL derivation of+// approval_status to ComputeApprovalStatus. They are two implementations of+// one rule, and the SQL side has to read an absent JSON key as a zero value+// because payloads are written with a bare protojson.Marshal.+func TestIssueApprovalStatusFilterMatchesComputation(t *testing.T) {+ t.Parallel()+ ctx := issueServiceTestContext()+ stores := setupIssueServiceTestStore(ctx, t)++ cases := []struct {+ title string+ approval *storepb.IssuePayloadApproval+ }{+ {"no approval payload at all", nil},+ {"approval finding not done", &storepb.IssuePayloadApproval{ApprovalTemplate: approvalTemplateWithRoles("roles/workspaceAdmin")}},+ {"no template", &storepb.IssuePayloadApproval{ApprovalFindingDone: true}},+ {"template with an empty flow", &storepb.IssuePayloadApproval{ApprovalFindingDone: true, ApprovalTemplate: approvalTemplateWithRoles()}},+ {"no approvers yet", &storepb.IssuePayloadApproval{ApprovalFindingDone: true, ApprovalTemplate: approvalTemplateWithRoles("roles/workspaceAdmin")}},+ {"one of two steps approved", &storepb.IssuePayloadApproval{+ ApprovalFindingDone: true,+ ApprovalTemplate: approvalTemplateWithRoles("roles/workspaceAdmin", "roles/projectOwner"),+ Approvers: []*storepb.IssuePayloadApproval_Approver{issueApprover(storepb.IssuePayloadApproval_Approver_APPROVED)},+ }},+ {"rejected at the second step", &storepb.IssuePayloadApproval{+ ApprovalFindingDone: true,+ ApprovalTemplate: approvalTemplateWithRoles("roles/workspaceAdmin", "roles/projectOwner"),+ Approvers: []*storepb.IssuePayloadApproval_Approver{+ issueApprover(storepb.IssuePayloadApproval_Approver_APPROVED),+ issueApprover(storepb.IssuePayloadApproval_Approver_REJECTED),+ },+ }},+ {"every step approved", &storepb.IssuePayloadApproval{+ ApprovalFindingDone: true,+ ApprovalTemplate: approvalTemplateWithRoles("roles/workspaceAdmin"),+ Approvers: []*storepb.IssuePayloadApproval_Approver{issueApprover(storepb.IssuePayloadApproval_Approver_APPROVED)},+ }},+ }++ want := map[v1pb.ApprovalStatus][]int64{}+ for _, tc := range cases {+ issue := createIssueWithApproval(ctx, t, stores, tc.title, tc.approval)+ status := store.ComputeApprovalStatus(tc.approval)+ want[status] = append(want[status], issue.UID)+ }+ // Every branch of ComputeApprovalStatus should be represented, otherwise+ // the comparison below passes on empty sets.+ require.Len(t, want, 5)++ for status, wantUIDs := range want {+ statusName := status.String()+ issues, err := stores.ListIssues(ctx, &store.FindIssueMessage{+ Workspace: "default",+ ProjectIDs: []string{"project-a"},+ ApprovalStatus: &statusName,+ })+ require.NoError(t, err, "approval_status == %q", statusName)+ var gotUIDs []int64+ for _, issue := range issues {+ gotUIDs = append(gotUIDs, issue.UID)+ }+ slices.Sort(gotUIDs)+ slices.Sort(wantUIDs)+ require.Equal(t, wantUIDs, gotUIDs, "approval_status == %q", statusName)+ }+}++// TestListIssuesExcludeDraft pins the list-side half of drafts: a draft is+// reachable by name but never listed, so it takes no page slot and shows up+// nowhere a submitted issue would.+func TestListIssuesExcludeDraft(t *testing.T) {+ t.Parallel()+ ctx := issueServiceTestContext()+ stores := setupIssueServiceTestStore(ctx, t)++ submitted := createIssueWithApproval(ctx, t, stores, "submitted", nil)+ draftPlan, err := stores.CreatePlan(ctx, &store.PlanMessage{ProjectID: "project-a", Name: "draft plan", Config: &storepb.PlanConfig{}}, "[email protected]")+ require.NoError(t, err)+ draft, err := stores.CreateIssue(ctx, &store.IssueMessage{+ ProjectID: "project-a",+ CreatorEmail: "[email protected]",+ Title: "draft",+ Type: storepb.Issue_DATABASE_CHANGE,+ Payload: &storepb.Issue{Draft: true},+ PlanUID: &draftPlan.UID,+ })+ require.NoError(t, err)++ listed, err := stores.ListIssues(ctx, &store.FindIssueMessage{Workspace: "default", ProjectIDs: []string{"project-a"}, ExcludeDraft: true})+ require.NoError(t, err)+ require.Len(t, listed, 1)+ require.Equal(t, submitted.UID, listed[0].UID)++ byName, err := stores.GetIssue(ctx, &store.FindIssueMessage{Workspace: "default", ProjectIDs: []string{"project-a"}, UID: &draft.UID})+ require.NoError(t, err)+ require.True(t, byName.Payload.GetDraft(), "the draft is still there by name")+}diff --git a/backend/store/issue_service_concurrency_test.go b/backend/store/issue_service_concurrency_test.goindex 4eef4dee1d..e2078bba42 100644--- a/backend/store/issue_service_concurrency_test.go+++ b/backend/store/issue_service_concurrency_test.go@@ -499,13 +499,8 @@ func TestIssueApprovalFiltersRunBeforePaging(t *testing.T) {// The fixture helpers below are duplicated from// backend/api/v1/issue_service_test.go, which keeps its own copies for the-// issue tests that stay there. Change both together: nothing catches a-// divergence, and these tests would keep passing against a stale fixture.-// The duplication ends when those tests move to a fake.-// TestIssueApprovalStatusFilterMatchesConverter pins the SQL derivation of-// approval_status to computeApprovalStatus. They are two implementations of one-// rule, and the SQL side has to read an absent JSON key as a zero value because-// payloads are written with a bare protojson.Marshal.+// issue test that stays there. Change both together: nothing catches a+// divergence.func issueNames(issues []*v1pb.Issue) []string {names := make([]string, 0, len(issues))for _, issue := range issues {diff --git a/backend/store/plan_test.go b/backend/store/plan_test.gonew file mode 100644index 0000000000..e858b97558--- /dev/null+++ b/backend/store/plan_test.go@@ -0,0 +1,81 @@+package store_test++import (+ "context"+ "testing"++ "github.com/bytebase/bytebase/backend/common/testcontainer"++ "github.com/stretchr/testify/require"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/store"+)++// TestListPlansExcludesMalformedUIPlans pins the list filter the plan UI reads+// through: an active plan with no issue is hidden unless it is a homogeneous+// release-backed change plan — those are GitOps plans, which never get an+// issue — while a deleted plan and any plan an issue links to stay visible.+// The filter is a list-only view: a hidden plan is still there to GetPlan.+func TestListPlansExcludesMalformedUIPlans(t *testing.T) {+ t.Parallel()+ ctx := context.Background()+ db, s, _ := testcontainer.NewMetadataDB(t)+ _, err := db.ExecContext(ctx, `+ INSERT INTO workspace (resource_id) VALUES ('default');+ INSERT INTO principal (name, email, password_hash) VALUES ('creator', '[email protected]', 'unused');+ INSERT INTO project (resource_id, workspace, name) VALUES ('project-a', 'default', 'Project A');+ `)+ require.NoError(t, err)++ createPlan := func(name string, config *storepb.PlanConfig) *store.PlanMessage {+ t.Helper()+ plan, err := s.CreatePlan(ctx, &store.PlanMessage{ProjectID: "project-a", Name: name, Config: config}, "[email protected]")+ require.NoError(t, err)+ return plan+ }+ changeConfig := func(id, release string) *storepb.PlanConfig {+ return &storepb.PlanConfig{Specs: []*storepb.PlanConfig_Spec{{+ Id: id,+ Config: &storepb.PlanConfig_Spec_ChangeDatabaseConfig{ChangeDatabaseConfig: &storepb.PlanConfig_ChangeDatabaseConfig{Release: release}},+ }}}+ }+ createConfig := func(id string) *storepb.PlanConfig {+ return &storepb.PlanConfig{Specs: []*storepb.PlanConfig_Spec{{+ Id: id,+ Config: &storepb.PlanConfig_Spec_CreateDatabaseConfig{CreateDatabaseConfig: &storepb.PlanConfig_CreateDatabaseConfig{}},+ }}}+ }++ createPlan("malformed change", changeConfig("malformed-change", ""))+ createPlan("malformed create", createConfig("malformed-create"))+ createPlan("malformed mixed", &storepb.PlanConfig{Specs: []*storepb.PlanConfig_Spec{+ createConfig("mixed-create").Specs[0],+ changeConfig("mixed-change", "").Specs[0],+ }})+ oldMalformed := createPlan("old malformed", changeConfig("old", ""))+ _, err = db.ExecContext(ctx, `UPDATE plan SET created_at = CURRENT_TIMESTAMP - INTERVAL '31 days' WHERE project = $1 AND id = $2`, oldMalformed.ProjectID, oldMalformed.UID)+ require.NoError(t, err)+ gitOps := createPlan("GitOps", changeConfig("gitops", "projects/project-a/releases/release-a"))+ deleted := createPlan("deleted", changeConfig("deleted", ""))+ _, err = db.ExecContext(ctx, `UPDATE plan SET deleted = TRUE WHERE project = $1 AND id = $2`, deleted.ProjectID, deleted.UID)+ require.NoError(t, err)+ linked := createPlan("linked", changeConfig("linked", ""))+ _, err = s.CreateIssue(ctx, &store.IssueMessage{+ ProjectID: linked.ProjectID, CreatorEmail: "[email protected]", PlanUID: &linked.UID,+ Title: "linked issue", Type: storepb.Issue_DATABASE_CHANGE, Payload: &storepb.Issue{},+ })+ require.NoError(t, err)++ plans, err := s.ListPlans(ctx, &store.FindPlanMessage{Workspace: "default", ProjectID: "project-a", ExcludeMalformedUIPlans: true})+ require.NoError(t, err)+ var got []string+ for _, plan := range plans {+ got = append(got, plan.Name)+ }+ require.ElementsMatch(t, []string{gitOps.Name, deleted.Name, linked.Name}, got)++ hidden, err := s.GetPlan(ctx, &store.FindPlanMessage{Workspace: "default", ProjectID: "project-a", UID: &oldMalformed.UID})+ require.NoError(t, err)+ require.Equal(t, oldMalformed.Name, hidden.Name, "hidden from the list, still there by name")+}diff --git a/backend/tests/credential_change_test.go b/backend/tests/credential_change_test.goindex 454476cdf3..c04a76bcc0 100644--- a/backend/tests/credential_change_test.go+++ b/backend/tests/credential_change_test.go@@ -134,6 +134,18 @@ func TestChangePasswordProofAndRevocation(t *testing.T) {a.Equal(connect.CodeUnauthenticated, connect.CodeOf(err))_, err = ctl.authServiceClient.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{Email: email, Password: newPassword}))a.NoError(err)++ // On a workspace that accepts signups, a registered address is reported+ // as taken, and that outranks the password policy: a registered user+ // should be told to log in, not to pick a better password.+ for _, password := range []string{"password-long-enough", "short"} {+ _, err := ctl.authServiceClient.Signup(ctx, connect.NewRequest(&v1pb.SignupRequest{+ Email: email,+ Title: "Signup test",+ Password: password,+ }))+ a.Equal(connect.CodeAlreadyExists, connect.CodeOf(err), "password %q", password)+ }ctl.authInterceptor.token = adminToken}diff --git a/backend/tests/gitops_test.go b/backend/tests/gitops_test.goindex 7d2c282056..03e09b7f49 100644--- a/backend/tests/gitops_test.go+++ b/backend/tests/gitops_test.go@@ -12,6 +12,7 @@ import ("github.com/google/uuid""github.com/stretchr/testify/require""google.golang.org/genproto/googleapis/type/expr"+ "google.golang.org/protobuf/types/known/fieldmaskpb""github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store"@@ -716,6 +717,25 @@ func TestGitOpsRollout(t *testing.T) {a.NotNil(changeDatabaseConfig)a.Equal(createReleaseResp.Msg.Name, changeDatabaseConfig.Release)+ // A release-backed plan's specs are the release's: editing them here would+ // desynchronize the plan from what was released, so the update is refused+ // and the plan's editor attribution stays with its creator.+ _, err = ctl.planServiceClient.UpdatePlan(ctx, connect.NewRequest(&v1pb.UpdatePlanRequest{+ Plan: &v1pb.Plan{+ Name: plan.Name,+ Specs: []*v1pb.Plan_Spec{{+ Id: uuid.NewString(),+ Config: &v1pb.Plan_Spec_ChangeDatabaseConfig{ChangeDatabaseConfig: &v1pb.Plan_ChangeDatabaseConfig{}},+ }},+ },+ UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"specs"}},+ }))+ a.Equal(connect.CodeInvalidArgument, connect.CodeOf(err))+ a.ErrorContains(err, "created from a release")+ unchanged, err := ctl.planServiceClient.GetPlan(ctx, connect.NewRequest(&v1pb.GetPlanRequest{Name: plan.Name}))+ a.NoError(err)+ a.Equal(plan.LastPlanEditor, unchanged.Msg.LastPlanEditor)+// Step 3: Create a rollout from the plan.rolloutResp, err := ctl.rolloutServiceClient.CreateRollout(ctx, connect.NewRequest(&v1pb.CreateRolloutRequest{Parent: plan.Name,diff --git a/backend/tests/login_audit_test.go b/backend/tests/login_audit_test.goindex a91db36f6b..74cb2c0afc 100644--- a/backend/tests/login_audit_test.go+++ b/backend/tests/login_audit_test.go@@ -8,6 +8,7 @@ import ("encoding/hex""fmt""regexp"+ "slices""strings""testing""time"@@ -15,6 +16,7 @@ import ("connectrpc.com/connect""github.com/pquerna/otp/totp""github.com/stretchr/testify/require"+ "google.golang.org/protobuf/reflect/protoreflect""google.golang.org/protobuf/types/known/fieldmaskpb""github.com/bytebase/bytebase/backend/common"@@ -67,31 +69,85 @@ func TestLoginFailureLockout(t *testing.T) {}))a.NoError(err)+ // A second account and an unreachable LDAP provider for the arms below;+ // both need the admin session the login attempts run without.+ _, err = ctl.userServiceClient.CreateUser(ctx, connect.NewRequest(&v1pb.CreateUserRequest{+ User: &v1pb.User{Title: "clearing", Email: "[email protected]", Password: "1024bytebase"},+ }))+ a.NoError(err)+ _, err = ctl.identityProviderServiceClient.CreateIdentityProvider(ctx, connect.NewRequest(&v1pb.CreateIdentityProviderRequest{+ IdentityProviderId: "ldap-lockout",+ IdentityProvider: &v1pb.IdentityProvider{+ Title: "Unreachable directory",+ Type: v1pb.IdentityProviderType_LDAP,+ Config: &v1pb.IdentityProviderConfig{Config: &v1pb.IdentityProviderConfig_LdapConfig{LdapConfig: &v1pb.LDAPIdentityProviderConfig{+ Host: "127.0.0.1",+ Port: 1,+ BindDn: "cn=admin,dc=example,dc=com",+ BindPassword: "unused",+ BaseDn: "dc=example,dc=com",+ UserFilter: "(uid=%s)",+ FieldMapping: &v1pb.FieldMapping{Identifier: "uid"},+ }}},+ },+ }))+ a.NoError(err)+adminToken := ctl.authInterceptor.tokenctl.authInterceptor.token = ""- mfaStart, err := ctl.authServiceClient.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{- Email: "[email protected]",- Password: "1024bytebase",- }))- a.NoError(err)- mfaTempToken := mfaStart.Msg.GetMfaTempToken()- a.NotEmpty(mfaTempToken)- invalidOTP := "not-a-code"- for range 5 {- _, err := ctl.authServiceClient.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{- OtpCode: &invalidOTP,+ // Every MFA attempt runs under a fresh temp token: slots are claimed per+ // identity, so a new token must not buy new guesses.+ var mfaTempTokens []string+ mfaLogin := func(otpCode *string) error {+ start, err := ctl.authServiceClient.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{+ Email: "[email protected]",+ Password: "1024bytebase",+ }))+ a.NoError(err)+ mfaTempToken := start.Msg.GetMfaTempToken()+ a.NotEmpty(mfaTempToken)+ mfaTempTokens = append(mfaTempTokens, mfaTempToken)+ _, err = ctl.authServiceClient.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{+ OtpCode: otpCode,MfaTempToken: &mfaTempToken,}))+ return err+ }+ // A request carrying no code is not a guess: nothing is compared, so no+ // slot is consumed and the full allowance is still there afterwards.+ for range 6 {+ err := mfaLogin(nil)+ a.Equal(connect.CodeUnauthenticated, connect.CodeOf(err))+ a.ErrorContains(err, "OTP or recovery code is required")+ }+ invalidOTP := "not-a-code"+ for range 5 {+ err := mfaLogin(&invalidOTP)a.Equal(connect.CodeUnauthenticated, connect.CodeOf(err))a.ErrorContains(err, "invalid MFA code")}- _, err = ctl.authServiceClient.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{- OtpCode: &invalidOTP,+ err = mfaLogin(&invalidOTP)+ a.Equal(connect.CodeResourceExhausted, connect.CodeOf(err))+ a.ErrorContains(err, "too many failed MFA attempts")+ // A locked identity is refused before the TOTP comparison.+ lockedOTP, err := totp.GenerateCode(mfaSetup.Msg.OtpSecret, time.Now())+ a.NoError(err)+ a.Equal(connect.CodeResourceExhausted, connect.CodeOf(mfaLogin(&lockedOTP)))+ mfaTempToken := mfaTempTokens[len(mfaTempTokens)-1]++ // MFA completion on a workspace switch draws from the same per-identity+ // bucket, so the lock demo's guesses earned holds there too: the first+ // wrong code is refused as exhausted, before any TOTP comparison.+ ctl.authInterceptor.token = adminToken+ _, err = ctl.authServiceClient.SwitchWorkspace(ctx, connect.NewRequest(&v1pb.SwitchWorkspaceRequest{+ Workspace: workspace,MfaTempToken: &mfaTempToken,+ OtpCode: &invalidOTP,}))a.Equal(connect.CodeResourceExhausted, connect.CodeOf(err))a.ErrorContains(err, "too many failed MFA attempts")+ ctl.authInterceptor.token = ""for _, email := range []string{" [email protected] ", " [email protected] "} {for range 5 {@@ -110,6 +166,53 @@ func TestLoginFailureLockout(t *testing.T) {a.ErrorContains(err, "too many failed login attempts")}+ // A locked identity is refused before bcrypt: the correct password must+ // not slip through the lock, or the lock would only slow a guesser down+ // until the right guess.+ _, err = ctl.authServiceClient.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{+ Email: "[email protected]",+ Password: "1024bytebase",+ }))+ a.Equal(connect.CodeResourceExhausted, connect.CodeOf(err))++ // Success clears the counter: three wrong guesses, the right password,+ // then the full allowance again before the lock engages — the forgotten+ // failures must not count against the fresh window.+ clearingLogin := func(password string) error {+ _, err := ctl.authServiceClient.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{+ Email: "[email protected]",+ Password: password,+ }))+ return err+ }+ for range 3 {+ a.Equal(connect.CodeUnauthenticated, connect.CodeOf(clearingLogin("wrong-password")))+ }+ a.NoError(clearingLogin("1024bytebase"))+ for range 5 {+ a.Equal(connect.CodeUnauthenticated, connect.CodeOf(clearingLogin("wrong-password")))+ }+ a.Equal(connect.CodeResourceExhausted, connect.CodeOf(clearingLogin("wrong-password")))++ // LDAP logins claim under a provider-scoped key before the bind, so an+ // unreachable directory still counts guesses and locks at the same attempt.+ ldapLogin := func() error {+ _, err := ctl.authServiceClient.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{+ IdpName: "idps/ldap-lockout",+ Email: "Alice",+ Password: "wrong",+ }))+ return err+ }+ for range 5 {+ err := ldapLogin()+ a.Error(err, "the directory is unreachable")+ a.NotEqual(connect.CodeResourceExhausted, connect.CodeOf(err))+ }+ err = ldapLogin()+ a.Equal(connect.CodeResourceExhausted, connect.CodeOf(err))+ a.ErrorContains(err, "too many failed login attempts")+// Email-code guessing is bounded by the same per-identity claims, taken// before the code row is even loaded — no code needs to be pending and no// audit-log counter is consulted, so the bound holds on Cloud and@@ -158,7 +261,9 @@ func TestLoginFailureLockout(t *testing.T) {a.NotEqual(wrongCode, request.GetEmailCode())a.NotContains(auditLog.Request, "wrong-password")a.NotContains(auditLog.Request, invalidOTP)- a.NotContains(auditLog.Request, mfaTempToken)+ for _, token := range mfaTempTokens {+ a.NotContains(auditLog.Request, token)+ }switch auditLog.Status.Message {case "invalid email or password":@@ -178,10 +283,10 @@ func TestLoginFailureLockout(t *testing.T) {}a.Equal(5, invalidPasswordCount["[email protected]"])a.Equal(5, invalidPasswordCount["[email protected]"])- a.Equal(1, passwordLockoutCount["[email protected]"])+ a.Equal(2, passwordLockoutCount["[email protected]"], "the sixth wrong guess and the refused correct password")a.Equal(1, passwordLockoutCount["[email protected]"])a.Equal(5, invalidMFACount)- a.Equal(1, mfaLockoutCount)+ a.Equal(2, mfaLockoutCount, "the sixth wrong guess and the refused valid code")a.Equal(5, invalidEmailCodeCount)a.Equal(1, emailCodeLockoutCount)@@ -913,3 +1018,129 @@ func TestAuditLogFormat(t *testing.T) {a.Empty(workspaceSearch.Msg.AuditLogs,"project-scoped SetIamPolicy audit must not leak into the workspace-scoped log stream")}++// TestLoginAuditsTheRequestedWorkspace pins where a failed login is filed when+// the request names a workspace: under that workspace, before the account is+// even looked up, so an attempt against an unknown address still leaves a row+// where the workspace's operators read. A workspace that does not exist is+// refused outright rather than audited nowhere. Pre-authentication, the login+// page learns only the restriction and the workspace.+func TestLoginAuditsTheRequestedWorkspace(t *testing.T) {+ t.Parallel()+ a := require.New(t)+ ctx := context.Background()+ ctl := &controller{}+ ctx, err := ctl.StartServerWithExternalPg(ctx)+ a.NoError(err)+ defer ctl.Close(ctx)++ workspaceResp, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{Name: "workspaces/-"}))+ a.NoError(err)+ workspace := workspaceResp.Msg.Name++ adminToken := ctl.authInterceptor.token+ ctl.authInterceptor.token = ""++ _, err = ctl.authServiceClient.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{+ Email: "[email protected]",+ Password: "wrong-password",+ Workspace: &workspace,+ }))+ a.Equal(connect.CodeUnauthenticated, connect.CodeOf(err))++ missing := "workspaces/missing"+ _, err = ctl.authServiceClient.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{+ Email: "[email protected]",+ Password: "wrong-password",+ Workspace: &missing,+ }))+ a.Equal(connect.CodeInvalidArgument, connect.CodeOf(err))++ info, err := ctl.authServiceClient.GetAuthenticationInfo(ctx, connect.NewRequest(&v1pb.GetAuthenticationInfoRequest{}))+ a.NoError(err)+ var populated []string+ info.Msg.ProtoReflect().Range(func(field protoreflect.FieldDescriptor, _ protoreflect.Value) bool {+ populated = append(populated, string(field.Name()))+ return true+ })+ slices.Sort(populated)+ a.Equal([]string{"restriction", "workspace"}, populated, "nothing but the restriction and the workspace is disclosed before authentication")+ a.Equal(workspace, info.Msg.Workspace)++ ctl.authInterceptor.token = adminToken+ search, err := ctl.auditLogServiceClient.SearchAuditLogs(ctx, connect.NewRequest(&v1pb.SearchAuditLogsRequest{+ Parent: workspace,+ Filter: `method == "/bytebase.v1.AuthService/Login"`,+ PageSize: 100,+ }))+ a.NoError(err)+ strangerRows := 0+ for _, auditLog := range search.Msg.AuditLogs {+ if auditLog.Resource == "[email protected]" {+ strangerRows+++ a.True(strings.HasPrefix(auditLog.Name, workspace+"/auditLogs/"))+ }+ }+ a.Equal(1, strangerRows, "the named workspace gets the row; the nonexistent one is refused before any row")+}++// TestLoginEnforcesWorkspaceDomains pins the identity-domain gate on the two+// doors that lead to a session: password login of an existing user — an admin+// included — and the sign-in code for an address the workspace would never+// accept, which is refused before a code is written.+func TestLoginEnforcesWorkspaceDomains(t *testing.T) {+ t.Parallel()+ a := require.New(t)+ ctx := context.Background()+ ctl := &controller{}+ ctx, err := ctl.StartServerWithExternalPg(ctx)+ a.NoError(err)+ defer ctl.Close(ctx)++ // The blocked admin exists before the domains are enforced, as an account+ // created under a looser policy would.+ const blockedAdmin = "[email protected]"+ created, err := ctl.userServiceClient.CreateUser(ctx, connect.NewRequest(&v1pb.CreateUserRequest{+ User: &v1pb.User{Title: "Blocked admin", Email: blockedAdmin, Password: "1024bytebase"},+ }))+ a.NoError(err)+ _, err = ctl.addMemberToWorkspaceIAM(ctx, created.Msg.Workspace, "user:"+blockedAdmin, "roles/workspaceAdmin")+ a.NoError(err)++ _, err = ctl.settingServiceClient.UpdateSetting(ctx, connect.NewRequest(&v1pb.UpdateSettingRequest{+ AllowMissing: true,+ Setting: &v1pb.Setting{+ Name: "settings/" + v1pb.Setting_WORKSPACE_PROFILE.String(),+ Value: &v1pb.SettingValue{Value: &v1pb.SettingValue_WorkspaceProfile{WorkspaceProfile: &v1pb.WorkspaceProfileSetting{+ Domains: []string{"example.com"},+ EnforceIdentityDomain: true,+ }}},+ },+ UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{+ "value.workspace_profile.domains",+ "value.workspace_profile.enforce_identity_domain",+ }},+ }))+ a.NoError(err)++ adminToken := ctl.authInterceptor.token+ ctl.authInterceptor.token = ""+ defer func() { ctl.authInterceptor.token = adminToken }()++ _, err = ctl.authServiceClient.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{+ Email: blockedAdmin,+ Password: "1024bytebase",+ }))+ a.Equal(connect.CodeInvalidArgument, connect.CodeOf(err), "%v", err)+ a.ErrorContains(err, "does not belong to allowed domains")++ // The code request names the workspace, as the login page does: an+ // address nobody has seen resolves to no workspace on its own, and the+ // domain gate is the named workspace's.+ _, err = ctl.authServiceClient.SendEmailLoginCode(ctx, connect.NewRequest(&v1pb.SendEmailLoginCodeRequest{+ Email: "[email protected]",+ Workspace: &created.Msg.Workspace,+ }))+ a.Equal(connect.CodeInvalidArgument, connect.CodeOf(err), "%v", err)+ a.ErrorContains(err, "does not belong to allowed domains")+}diff --git a/backend/tests/main_test.go b/backend/tests/main_test.goindex a17356f9c7..9e6a52772a 100644--- a/backend/tests/main_test.go+++ b/backend/tests/main_test.go@@ -29,6 +29,9 @@ func startMain(ctx context.Context, m *testing.M) (int, error) {if pgContainer != nil {pgContainer.Close(ctx)}+ if sampleTargetContainer != nil {+ sampleTargetContainer.Close(ctx)+ }}()externalPgHost = pgContainer.hostexternalPgPort = pgContainer.portdiff --git a/backend/tests/mcp_denial_audit_test.go b/backend/tests/mcp_denial_audit_test.goindex 45d826f529..fb1ef6d219 100644--- a/backend/tests/mcp_denial_audit_test.go+++ b/backend/tests/mcp_denial_audit_test.go@@ -2,13 +2,8 @@ package testsimport ("context"- "crypto/sha256"- "encoding/base64"- "encoding/json"- "io""net/http""net/url"- "strings""testing""connectrpc.com/connect"@@ -77,7 +72,7 @@ func TestMCPPolicyDenialsReachTheAuditPage(t *testing.T) {// The same workspace now refuses a NEW authorization too, and that refusal// is a second row. Without it an operator sees an agent stop connecting and// cannot tell a client that gave up from one that was never let in.- consentRefused := consentUnderDisabledCeiling(t, ctl)+ consentRefused := consentUnderDisabledCeiling(t, ctl, "mcp:read-only")a.Equal(http.StatusForbidden, consentRefused)consentRows := searchMCP(`method == "/bytebase.mcp.Consent/Approve"`)@@ -96,47 +91,28 @@ func TestMCPPolicyDenialsReachTheAuditPage(t *testing.T) {// operator pivoting on a session must not be handed rows that were never// part of one.a.Empty(searchMCP(`mcp_correlation_id == "any-session"`))++ // The refusal cannot be walked around by asking for a different scope, or+ // for none: the ceiling decides whether any grant is issued at all, before+ // the requested mode is considered. And every attempt is recorded, not+ // just the first.+ for _, scope := range []string{"mcp:read-write", ""} {+ a.Equal(http.StatusForbidden, consentUnderDisabledCeiling(t, ctl, scope), "scope %q", scope)+ }+ a.Len(searchMCP(`method == "/bytebase.mcp.Consent/Approve"`), 3)}// consentUnderDisabledCeiling registers a fresh MCP client and posts an// approved consent form, returning the HTTP status the authorize handler// answered with. It is the first half of mintMCPOAuthToken's flow, stopped-// where a refused consent stops it.-func consentUnderDisabledCeiling(t *testing.T, ctl *controller) int {+// where a refused consent stops it. An empty scope omits the parameter.+func consentUnderDisabledCeiling(t *testing.T, ctl *controller, scope string) int {t.Helper()- httpClient := &http.Client{}-- resp, err := httpClient.Post(ctl.rootURL+"/api/oauth2/register", "application/json",- strings.NewReader(`{"client_name":"bb-e2e-refused","redirect_uris":["http://localhost/cb"],"grant_types":["authorization_code"],"token_endpoint_auth_method":"none"}`))- require.NoError(t, err)- defer resp.Body.Close()- var reg struct {- ClientID string `json:"client_id"`- }- require.NoError(t, json.NewDecoder(resp.Body).Decode(®))- require.NotEmpty(t, reg.ClientID)-- verifier := "e2eVerifier_e2eVerifier_e2eVerifier_e2eVerifier"- challenge := sha256.Sum256([]byte(verifier))- form := url.Values{- "client_id": {reg.ClientID},- "redirect_uri": {"http://localhost/cb"},- "state": {"e2e-state"},- "code_challenge": {base64.RawURLEncoding.EncodeToString(challenge[:])},- "code_challenge_method": {"S256"},- "action": {"allow"},- "resource": {ctl.rootURL + "/mcp"},- "scope": {"mcp:read-only"},+ extra := url.Values{"resource": {ctl.rootURL + "/mcp"}}+ if scope != "" {+ extra.Set("scope", scope)}- req, err := http.NewRequest(http.MethodPost, ctl.rootURL+"/api/oauth2/authorize", strings.NewReader(form.Encode()))- require.NoError(t, err)- req.Header.Set("Content-Type", "application/x-www-form-urlencoded")- req.Header.Set("Authorization", "Bearer "+ctl.authInterceptor.token)- consentResp, err := httpClient.Do(req)- require.NoError(t, err)- defer consentResp.Body.Close()- body, err := io.ReadAll(consentResp.Body)- require.NoError(t, err)- require.NotContains(t, string(body), "code=", "a refused consent must issue no authorization code")- return consentResp.StatusCode+ status, body := postOAuth2Consent(t, ctl, ctl.authInterceptor.token, oauth2ConsentForm(registerOAuth2Client(t, ctl), extra))+ require.NotContains(t, body, "code=", "a refused consent must issue no authorization code")+ return status}diff --git a/backend/tests/mcp_migration_matrix_test.go b/backend/tests/mcp_migration_matrix_test.goindex 25b4b88a67..a48f4e33e8 100644--- a/backend/tests/mcp_migration_matrix_test.go+++ b/backend/tests/mcp_migration_matrix_test.go@@ -11,17 +11,15 @@ package tests// - accepted: backend/api/mcp/server_test.go// TestMCPAuthMiddlewareAudienceMatrix/"unexpired legacy oauth2 audience is// accepted" and TestDecideAudience/"legacy oauth2 audience is accepted-// while its token lives"; against a real store,-// backend/api/mcp/server_killswitch_test.go TestMCPKillSwitchEndToEnd-// (its tokenForWorkspace helper mints the legacy audience).+// while its token lives".// - expired: backend/api/mcp/server_test.go TestMCPAuthMiddleware/"expired// token returns 401", whose generateExpiredToken deliberately carries the// legacy audience for exactly this reason.//// Row 2 — a legacy refresh grant with no stored resource is refused with the// re-consent signal (invalid_grant naming the reauthorize tool). MAPPED:-// backend/api/oauth2/grant_test.go TestResourceScopeGrantLifecycle/"legacy-// unbound grants are refused at the token endpoint with re-auth guidance" —+// TestOAuth2GrantLifecycle/"legacy unbound grants are refused at the token+// endpoint with re-auth guidance" in this package —// both grant types, plus the negative control that a resource-bound grant// with an empty scope is NOT legacy.//@@ -29,17 +27,19 @@ package tests// delegation state is BOTH-EMPTY. The links were pinned separately (session// works: TestMCPToolCallParity; credential: backend/api/mcp/// internal_transport_test.go TestMCPAuthMiddlewareLegacySessionEmptyGrantState;-// AuthContext: backend/api/auth/internal_interceptor_livestate_test.go;-// audit row: backend/api/v1/audit_mcp_provenance_test.go) but nothing composed-// them on a live server. NEW: TestMCPMigrationGrantStateMatrix.+// AuthContext: backend/api/auth/internal_interceptor_test.go+// TestAuthenticateDelegatedCarriesGrantVerbatim; audit row:+// backend/api/v1/audit_test.go TestAuditRowCarriesMCPDelegationProvenance)+// but nothing composed them on a live server. NEW:+// TestMCPMigrationGrantStateMatrix.//// Row 4 — a grant whose client omitted `scope` at consent: resource bound,// scope empty. The state has two indistinguishable origins — scope-omitting// clients, which make it permanent rather than only migration-era, and,// transiently, PR-3-era tokens whose grant DID record a scope — and it must// never collapse into row 3. The AuthContext-level distinction is pinned-// (internal_interceptor_livestate_test.go/"grant-backed token: resource-// present, scope empty"); nothing minted the state from a real scope-less+// (TestAuthenticateDelegatedCarriesGrantVerbatim/"grant-backed token:+// resource present, scope empty"); nothing minted the state from a real scope-less// token, carried it to an audit row, or checked it survives a refresh that// names a scope — the one path that could widen a grant which recorded none.// NEW: TestMCPMigrationGrantStateMatrix, which asserts rows 3 and 4 against@@ -47,10 +47,8 @@ package tests//// Row 5 — bb.oauth2.access on the public v1 API is refused. MAPPED:// backend/api/auth/auth_test.go TestCheckTokenAudience/"legacy oauth2-// audience is refused: it is MCP-minted too" (the legacy audience by name),-// backend/api/oauth2/grant_test.go TestResourceScopeGrantLifecycle/"an MCP-// token is refused on the general API but keeps serving /mcp", and the-// end-to-end flow in TestMCPTokenIsRejectedOnGeneralAPI.+// audience is refused: it is MCP-minted too" (the legacy audience by name)+// and the end-to-end flow in TestMCPTokenIsRejectedOnGeneralAPI.//// Row 6 — a ceiling lookup FAILURE fails closed (lookup error → DISABLED →// connection refused). NEW: TestMCPMigrationCeilingLookupFailureFailsClosed.@@ -60,12 +58,10 @@ package tests// backend/api/mcp/server_test.go TestDecideAudience.//// Row 7 — a tightened ceiling bites the NEXT request of a live MCP session,-// with no re-auth. The request-level half is pinned-// (backend/api/mcp/server_killswitch_test.go-// TestMCPKillSwitchBypassesSettingCache, which also pins that the read-// bypasses the setting cache), but that test has no session and the-// session-level test runs with a nil store, so the ceiling is never consulted-// there. NEW: TestMCPMigrationTightenedCeilingBitesLiveSession.+// with no re-auth. That the gate reads the stored setting fresh, past the+// setting cache, is pinned by TestMCPMaskingToggleBitesTheNextRequest in this+// package, which flips the same setting out of band. NEW:+// TestMCPMigrationTightenedCeilingBitesLiveSession.import ("context"@@ -74,6 +70,7 @@ import ("encoding/json""io""net/http"+ "net/url""strings""testing"@@ -313,9 +310,8 @@ func TestMCPMigrationCeilingLookupFailureFailsClosed(t *testing.T) {// The stored ceiling is given a value the MCP setting cannot be parsed with,// so the read errors outright. That is a distinct arm from an unrecognized// enum NAME, which the store's unmarshaler discards: a name it does not- // know is parsed as UNSPECIFIED and fails closed, and- // TestMCPCeilingStoredValueFailsClosed- // (backend/api/mcp) owns that case. This one is the unmarshal error itself.+ // know is parsed as UNSPECIFIED and fails closed, and TestMCPCutoverAdmitsReadOnlyAndNothingElse+ // owns that case. This one is the unmarshal error itself.restore := func() {result, err := db.ExecContext(ctx, `UPDATE setting SET value = '{"capability":"READ_WRITE"}'::jsonb@@ -350,6 +346,30 @@ func TestMCPMigrationCeilingLookupFailureFailsClosed(t *testing.T) {a.Contains(body, "could not be read")a.NotContains(body, "turned MCP access off")+ // The same row must not take the bootstrap response down with it (BOT-106):+ // actuator info still answers, with the setting absent rather than guessed,+ // so the admin repairing the row is not locked out of the app.+ info, err := ctl.actuatorServiceClient.GetActuatorInfo(ctx, connect.NewRequest(&v1pb.GetActuatorInfoRequest{}))+ a.NoError(err)+ a.Nil(info.Msg.McpSetting)++ // The same outage refuses a NEW consent as an outage too, never as a+ // policy: the client is told to retry in its own vocabulary and gets no+ // code, and no denial row is written — telling a user their admin+ // disabled MCP during a database blip would send them to an admin with+ // nothing to fix, and an outage recorded as a denial is a decision nobody+ // made.+ consentStatus, consentBody := postOAuth2Consent(t, ctl, ctl.authInterceptor.token,+ oauth2ConsentForm(registerOAuth2Client(t, ctl), url.Values{"resource": {ctl.rootURL + "/mcp"}, "scope": {"mcp:read-only"}}))+ a.Equal(http.StatusOK, consentStatus, "an error redirect is a 200 carrying the error")+ consentCallback := oauth2Callback(t, consentBody)+ a.Equal("temporarily_unavailable", consentCallback.Query().Get("error"))+ a.Empty(consentCallback.Query().Get("code"), "an outage must not mint a credential either")+ var consentDenials int+ a.NoError(db.QueryRowContext(ctx,+ `SELECT COUNT(*) FROM audit_log WHERE payload->>'method' = '/bytebase.mcp.Consent/Approve'`).Scan(&consentDenials))+ a.Zero(consentDenials)+// Restoring the row proves the refusal was the unreadable policy and// nothing else: the very same token opens a session again.restore()@@ -414,3 +434,78 @@ func TestMCPMigrationTightenedCeilingBitesLiveSession(t *testing.T) {defer restored.Close()a.Equal(http.StatusOK, callAPIStatus(ctx, t, restored, "WorkspaceService/ListWorkspaces", nil))}++// TestMCPMembershipRevocationBitesLiveSession pins the property the delegated+// credential rests on: it carries identity and grant state only, so+// authorization-relevant state — here workspace membership — is re-resolved+// against the store on EVERY internal request. Revoking membership bites on+// the very next tool call with the SAME still-valid bearer: no re-consent, no+// token expiry, no session restart. If a refactor ever starts trusting the+// credential for authorization state, this goes red.+func TestMCPMembershipRevocationBitesLiveSession(t *testing.T) {+ t.Parallel()+ a := require.New(t)+ ctx := context.Background()+ ctl := &controller{}+ ctx, err := ctl.StartServerWithExternalPg(ctx)+ a.NoError(err)+ defer ctl.Close(ctx)++ const memberEmail = "[email protected]"+ const memberPassword = "1024bytebase"+ memberResp, err := ctl.userServiceClient.CreateUser(ctx, connect.NewRequest(&v1pb.CreateUserRequest{+ User: &v1pb.User{Title: "revoked member", Email: memberEmail, Password: memberPassword},+ }))+ a.NoError(err)+ workspace := memberResp.Msg.Workspace+ member := "user:" + memberEmail+ _, err = ctl.addMemberToWorkspaceIAM(ctx, workspace, member, "roles/workspaceMember")+ a.NoError(err)+ memberLogin, err := ctl.authServiceClient.Login(ctx, connect.NewRequest(&v1pb.LoginRequest{+ Email: memberEmail,+ Password: memberPassword,+ }))+ a.NoError(err)++ mcpToken, _ := mintMCPOAuthToken(t, ctl, memberLogin.Msg.Token)+ session := openMCPSession(ctx, t, ctl, mcpToken)+ defer session.Close()+ a.Equal(http.StatusOK, callAPIStatus(ctx, t, session, "WorkspaceService/ListWorkspaces", nil),+ "a member's session serves tool calls")++ setWorkspaceMemberRoles(ctx, t, ctl, workspace, member, nil)+ a.Equal(http.StatusUnauthorized, callAPIStatus(ctx, t, session, "WorkspaceService/ListWorkspaces", nil),+ "the same credential must stop working the moment membership is revoked")++ // Live in both directions, not a one-way latch: restoring membership+ // restores service with the unchanged credential.+ setWorkspaceMemberRoles(ctx, t, ctl, workspace, member, []string{"roles/workspaceMember"})+ a.Equal(http.StatusOK, callAPIStatus(ctx, t, session, "WorkspaceService/ListWorkspaces", nil))+}++// setWorkspaceMemberRoles rewrites the workspace IAM policy so that member holds+// exactly roles — none, to revoke the membership entirely.+func setWorkspaceMemberRoles(ctx context.Context, t *testing.T, ctl *controller, workspace, member string, roles []string) {+ t.Helper()+ policyResp, err := ctl.workspaceServiceClient.GetIamPolicy(ctx, connect.NewRequest(&v1pb.GetIamPolicyRequest{Resource: workspace}))+ require.NoError(t, err)+ policy := policyResp.Msg+ for _, binding := range policy.Bindings {+ kept := binding.Members[:0]+ for _, m := range binding.Members {+ if m != member {+ kept = append(kept, m)+ }+ }+ binding.Members = kept+ }+ for _, role := range roles {+ policy.Bindings = append(policy.Bindings, &v1pb.Binding{Role: role, Members: []string{member}})+ }+ _, err = ctl.workspaceServiceClient.SetIamPolicy(ctx, connect.NewRequest(&v1pb.SetIamPolicyRequest{+ Etag: policy.Etag,+ Policy: policy,+ Resource: workspace,+ }))+ require.NoError(t, err)+}diff --git a/backend/tests/oauth2_grant_test.go b/backend/tests/oauth2_grant_test.gonew file mode 100644index 0000000000..b4df7c2afe--- /dev/null+++ b/backend/tests/oauth2_grant_test.go@@ -0,0 +1,459 @@+package tests++import (+ "context"+ "crypto/sha256"+ "database/sql"+ "encoding/base64"+ "encoding/json"+ "html"+ "io"+ "net/http"+ "net/url"+ "regexp"+ "strings"+ "testing"++ "connectrpc.com/connect"+ "github.com/modelcontextprotocol/go-sdk/mcp"+ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/backend/api/auth"+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+)++const (+ oauth2TestRedirectURI = "http://localhost/cb"+ // 43-128 chars per RFC 7636.+ oauth2TestVerifier = "e2eVerifier_e2eVerifier_e2eVerifier_e2eVerifier"+)++// registerOAuth2Client registers a public client through RFC 7591 dynamic+// registration, allowed both grant types, and returns its ID.+func registerOAuth2Client(t *testing.T, ctl *controller) string {+ t.Helper()+ resp, err := (&http.Client{}).Post(ctl.rootURL+"/api/oauth2/register", "application/json",+ strings.NewReader(`{"client_name":"bb-e2e","redirect_uris":["http://localhost/cb"],"grant_types":["authorization_code","refresh_token"],"token_endpoint_auth_method":"none"}`))+ require.NoError(t, err)+ defer resp.Body.Close()+ var reg struct {+ ClientID string `json:"client_id"`+ }+ require.NoError(t, json.NewDecoder(resp.Body).Decode(®))+ require.NotEmpty(t, reg.ClientID)+ return reg.ClientID+}++// oauth2ConsentForm is an approved consent for clientID with PKCE filled in.+// extra adds or overrides parameters; a repeated parameter is sent repeated.+func oauth2ConsentForm(clientID string, extra url.Values) url.Values {+ challenge := sha256.Sum256([]byte(oauth2TestVerifier))+ form := url.Values{+ "client_id": {clientID},+ "redirect_uri": {oauth2TestRedirectURI},+ "state": {"e2e-state"},+ "code_challenge": {base64.RawURLEncoding.EncodeToString(challenge[:])},+ "code_challenge_method": {"S256"},+ "action": {"allow"},+ }+ for name, values := range extra {+ form[name] = values+ }+ return form+}++// postOAuth2Consent posts a consent form as the bearer's user and returns the+// raw answer, so a caller can read either a refusal page or the meta-refresh+// carrying the callback.+func postOAuth2Consent(t *testing.T, ctl *controller, bearer string, form url.Values) (int, string) {+ t.Helper()+ req, err := http.NewRequest(http.MethodPost, ctl.rootURL+"/api/oauth2/authorize", strings.NewReader(form.Encode()))+ require.NoError(t, err)+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")+ req.Header.Set("Authorization", "Bearer "+bearer)+ resp, err := (&http.Client{}).Do(req)+ require.NoError(t, err)+ defer resp.Body.Close()+ body, err := io.ReadAll(resp.Body)+ require.NoError(t, err)+ return resp.StatusCode, string(body)+}++// oauth2Callback pulls the callback URL out of the meta-refresh page the+// authorize handler answers with (a page rather than a 302, to sidestep CSP+// form-action restrictions); it carries either `code` or `error`.+func oauth2Callback(t *testing.T, body string) *url.URL {+ t.Helper()+ matches := regexp.MustCompile(`content="0;url=([^"]+)"`).FindStringSubmatch(body)+ require.Len(t, matches, 2, "expected a meta-refresh redirect, got: %s", body)+ callback, err := url.Parse(html.UnescapeString(matches[1]))+ require.NoError(t, err)+ return callback+}++// consentOAuth2 posts an approved consent and returns its callback.+func consentOAuth2(t *testing.T, ctl *controller, bearer, clientID string, extra url.Values) *url.URL {+ t.Helper()+ status, body := postOAuth2Consent(t, ctl, bearer, oauth2ConsentForm(clientID, extra))+ require.Equal(t, http.StatusOK, status, body)+ return oauth2Callback(t, body)+}++// consentOAuth2Code asserts the consent was granted and returns the code.+func consentOAuth2Code(t *testing.T, ctl *controller, bearer, clientID string, extra url.Values) string {+ t.Helper()+ callback := consentOAuth2(t, ctl, bearer, clientID, extra)+ require.Empty(t, callback.Query().Get("error"), callback.Query().Get("error_description"))+ code := callback.Query().Get("code")+ require.NotEmpty(t, code)+ return code+}++func oauth2ExchangeForm(clientID, code string, extra url.Values) url.Values {+ form := url.Values{+ "grant_type": {"authorization_code"},+ "code": {code},+ "redirect_uri": {oauth2TestRedirectURI},+ "code_verifier": {oauth2TestVerifier},+ "client_id": {clientID},+ }+ for name, values := range extra {+ form[name] = values+ }+ return form+}++func oauth2RefreshForm(clientID, refreshToken string, extra url.Values) url.Values {+ form := url.Values{+ "grant_type": {"refresh_token"},+ "refresh_token": {refreshToken},+ "client_id": {clientID},+ }+ for name, values := range extra {+ form[name] = values+ }+ return form+}++// postOAuth2Token posts to the token endpoint and returns the status and the+// decoded JSON body, which carries either the tokens or an RFC 6749 error.+func postOAuth2Token(t *testing.T, ctl *controller, form url.Values) (int, map[string]any) {+ t.Helper()+ resp, err := (&http.Client{}).PostForm(ctl.rootURL+"/api/oauth2/token", form)+ require.NoError(t, err)+ defer resp.Body.Close()+ raw, err := io.ReadAll(resp.Body)+ require.NoError(t, err)+ body := map[string]any{}+ require.NoError(t, json.Unmarshal(raw, &body), string(raw))+ return resp.StatusCode, body+}++// oauth2TokenOK asserts the token endpoint issued and returns the body.+func oauth2TokenOK(t *testing.T, ctl *controller, form url.Values) map[string]any {+ t.Helper()+ status, body := postOAuth2Token(t, ctl, form)+ require.Equal(t, http.StatusOK, status, body)+ require.NotEmpty(t, body["access_token"])+ return body+}++func oauth2Error(t *testing.T, ctl *controller, form url.Values, wantStatus int) (string, string) {+ t.Helper()+ status, body := postOAuth2Token(t, ctl, form)+ require.Equal(t, wantStatus, status, body)+ code := stringField(t, body, "error")+ description := stringField(t, body, "error_description")+ return code, description+}++// stringField reads a string out of a decoded token response, failing the+// test if it is missing or not a string.+func stringField(t *testing.T, body map[string]any, key string) string {+ t.Helper()+ value, ok := body[key].(string)+ require.True(t, ok, "%s is not a string in %v", key, body)+ return value+}++// storedGrant is the consented state a code or refresh row carries.+type storedGrant struct {+ Resource string `json:"resource"`+ Scope string `json:"scope"`+}++func storedAuthorizationCode(ctx context.Context, t *testing.T, db *sql.DB, clientID, code string) *storedGrant {+ t.Helper()+ return storedGrantRow(ctx, t, db, `SELECT config FROM oauth2_authorization_code WHERE client_id = $1 AND code = $2`, clientID, code)+}++func storedRefreshToken(ctx context.Context, t *testing.T, db *sql.DB, clientID, refreshToken string) *storedGrant {+ t.Helper()+ return storedGrantRow(ctx, t, db, `SELECT config FROM oauth2_refresh_token WHERE client_id = $1 AND token_hash = $2`, clientID, auth.HashToken(refreshToken))+}++func storedGrantRow(ctx context.Context, t *testing.T, db *sql.DB, query string, args ...any) *storedGrant {+ t.Helper()+ var raw []byte+ err := db.QueryRowContext(ctx, query, args...).Scan(&raw)+ if err == sql.ErrNoRows {+ return nil+ }+ require.NoError(t, err)+ grant := &storedGrant{}+ require.NoError(t, json.Unmarshal(raw, grant))+ return grant+}++// TestOAuth2GrantLifecycle drives the whole consent → token → refresh path on a+// live server. The pure rule space — resource canonicalization, scope+// canonicalization, consented-value checks — is table-tested in+// backend/api/oauth2; what this pins is the wiring, which is where a+// resource/scope binding silently disappears: the code row must carry what was+// consented, the token endpoint must compare against it, and every refresh+// must re-issue the same grant rather than whatever the client asks for.+//+//nolint:tparallel // Subtests share one server lifecycle.+func TestOAuth2GrantLifecycle(t *testing.T) {+ t.Parallel()+ a := require.New(t)+ ctx := context.Background()+ ctl := &controller{}+ ctx, err := ctl.StartServerWithExternalPg(ctx)+ a.NoError(err)+ defer ctl.Close(ctx)++ db, err := sql.Open("pgx", ctl.profile.PgURL)+ a.NoError(err)+ defer db.Close()++ workspace, err := ctl.workspaceServiceClient.GetWorkspace(ctx, connect.NewRequest(&v1pb.GetWorkspaceRequest{Name: "workspaces/-"}))+ a.NoError(err)+ workspaceID := strings.TrimPrefix(workspace.Msg.Name, "workspaces/")++ bearer := ctl.authInterceptor.token+ clientID := registerOAuth2Client(t, ctl)+ canonical := ctl.rootURL + "/mcp"++ t.Run("matching resource and known scope are consented", func(t *testing.T) {+ code := consentOAuth2Code(t, ctl, bearer, clientID, url.Values{"resource": {canonical}, "scope": {"mcp:read-only"}})+ require.Equal(t, &storedGrant{Resource: canonical, Scope: "mcp:read-only"}, storedAuthorizationCode(ctx, t, db, clientID, code))+ })++ t.Run("resource is stored canonicalized, not as sent", func(t *testing.T) {+ shouted := strings.ToUpper(strings.TrimPrefix(ctl.rootURL, "http://"))+ code := consentOAuth2Code(t, ctl, bearer, clientID, url.Values{"resource": {"HTTP://" + shouted + "/mcp/"}})+ require.Equal(t, canonical, storedAuthorizationCode(ctx, t, db, clientID, code).Resource,+ "the token endpoint compares against this value, so it must already be canonical")+ })++ t.Run("a bare origin is consented as the canonical MCP resource", func(t *testing.T) {+ // Accepting the bare origin (our unsuffixed RFC 9728 document publishes+ // it) must not put a second spelling in the grant: the token audience+ // is bound to the stored value, and two spellings would mean two+ // audiences to honor at /mcp for as long as any grant lives.+ code := consentOAuth2Code(t, ctl, bearer, clientID, url.Values{"resource": {ctl.rootURL}})+ require.Equal(t, canonical, storedAuthorizationCode(ctx, t, db, clientID, code).Resource)+ })++ t.Run("no resource requested: the grant is bound to this server's MCP resource", func(t *testing.T) {+ // Every grant is resource-bound, because its tokens carry the resource+ // as their audience: an unbound grant would mint tokens no audience+ // check accepts. A client that never sends `resource` is defaulted to+ // the only resource this server would accept anyway.+ code := consentOAuth2Code(t, ctl, bearer, clientID, url.Values{})+ got := storedAuthorizationCode(ctx, t, db, clientID, code)+ require.Equal(t, canonical, got.Resource)+ require.Empty(t, got.Scope, "only the resource is defaulted; scope stays exactly as requested")+ })++ for _, tc := range []struct {+ name string+ params url.Values+ wantCode string+ }{+ {"resource on another host", url.Values{"resource": {"https://evil.example.com/mcp"}}, "invalid_target"},+ {"resource parameter repeated", url.Values{"resource": {canonical, canonical}}, "invalid_target"},+ {"unknown scope", url.Values{"scope": {"mcp:admin"}}, "invalid_scope"},+ {"scope parameter repeated", url.Values{"scope": {"mcp:read-only", "mcp:read-write"}}, "invalid_scope"},+ } {+ t.Run("consent rejected: "+tc.name, func(t *testing.T) {+ callback := consentOAuth2(t, ctl, bearer, clientID, tc.params)+ require.Equal(t, tc.wantCode, callback.Query().Get("error"))+ require.Empty(t, callback.Query().Get("code"))+ })+ }++ t.Run("token exchange and refresh carry the grant forward unchanged", func(t *testing.T) {+ code := consentOAuth2Code(t, ctl, bearer, clientID, url.Values{"resource": {canonical}, "scope": {"mcp:read-write"}})++ // Exchange, echoing the resource back the way RFC 8707 clients do.+ first := oauth2TokenOK(t, ctl, oauth2ExchangeForm(clientID, code, url.Values{"resource": {canonical}}))+ require.Equal(t, "mcp:read-write", first["scope"], "the consented scope must be echoed (RFC 6749 §5.1)")+ refreshToken := stringField(t, first, "refresh_token")+ require.NotEmpty(t, refreshToken)+ require.Equal(t, &storedGrant{Resource: canonical, Scope: "mcp:read-write"}, storedRefreshToken(ctx, t, db, clientID, refreshToken))++ // A refresh that names a different scope or resource is refused, and+ // refusal happens before consumption so the client can retry correctly.+ errCode, _ := oauth2Error(t, ctl, oauth2RefreshForm(clientID, refreshToken, url.Values{"scope": {"mcp:read-only"}}), http.StatusBadRequest)+ require.Equal(t, "invalid_scope", errCode)+ errCode, _ = oauth2Error(t, ctl, oauth2RefreshForm(clientID, refreshToken, url.Values{"resource": {"https://evil.example.com/mcp"}}), http.StatusBadRequest)+ require.Equal(t, "invalid_target", errCode)++ // The rejected attempts left the grant usable, and the honest refresh+ // re-issues the same resource and scope.+ second := oauth2TokenOK(t, ctl, oauth2RefreshForm(clientID, refreshToken, nil))+ require.Equal(t, "mcp:read-write", second["scope"])+ rotated := stringField(t, second, "refresh_token")+ require.Equal(t, &storedGrant{Resource: canonical, Scope: "mcp:read-write"}, storedRefreshToken(ctx, t, db, clientID, rotated))+ })++ t.Run("a bare-origin grant refreshes as the canonical resource, named either way", func(t *testing.T) {+ // Consent with the bare origin, then drive the whole exchange naming+ // the bare origin at every step. The stored value stays the canonical+ // MCP URI throughout, the token's audience is that stored value, and+ // the client is never told its own spelling is wrong.+ code := consentOAuth2Code(t, ctl, bearer, clientID, url.Values{"resource": {ctl.rootURL + "/"}})+ first := oauth2TokenOK(t, ctl, oauth2ExchangeForm(clientID, code, url.Values{"resource": {ctl.rootURL}}))+ refreshToken := stringField(t, first, "refresh_token")+ require.Equal(t, canonical, storedRefreshToken(ctx, t, db, clientID, refreshToken).Resource)+ accessToken := stringField(t, first, "access_token")+ require.Contains(t, jwtClaims(t, accessToken)["aud"], canonical, "the token is minted from the grant's stored resource")++ second := oauth2TokenOK(t, ctl, oauth2RefreshForm(clientID, refreshToken, url.Values{"resource": {ctl.rootURL}}))+ rotated := stringField(t, second, "refresh_token")+ require.Equal(t, canonical, storedRefreshToken(ctx, t, db, clientID, rotated).Resource,+ "the canonical form must survive rotation, not drift back to what the client sent")+ })++ t.Run("legacy unbound grants are refused at the token endpoint with re-auth guidance", func(t *testing.T) {+ // Rows created before 3.22.1 carry no resource in their config. Tokens+ // are audience-bound to the grant's stored resource, so an unbound+ // grant has nothing valid to mint. invalid_grant is what makes+ // RFC-compliant clients discard the grant and rerun the OAuth flow,+ // and the description points at the reauthorize tool as the in-band+ // recovery.+ challenge := sha256.Sum256([]byte(oauth2TestVerifier))+ const legacyCode = "bb_code_legacy_unbound"+ _, err := db.ExecContext(ctx, `+ INSERT INTO oauth2_authorization_code (code, client_id, user_email, workspace, config, expires_at)+ VALUES ($1, $2, '[email protected]', $3, $4, now() + interval '10 minutes')`,+ legacyCode, clientID, workspaceID,+ `{"redirectUri":"http://localhost/cb","codeChallenge":"`+base64.RawURLEncoding.EncodeToString(challenge[:])+`","codeChallengeMethod":"S256"}`)+ require.NoError(t, err)++ code, description := oauth2Error(t, ctl, oauth2ExchangeForm(clientID, legacyCode, nil), http.StatusBadRequest)+ require.Equal(t, "invalid_grant", code)+ require.Contains(t, description, "re-authorize")+ require.Contains(t, description, "reauthorize tool")++ const legacyRefresh = "bb_refresh_legacy_unbound"+ _, err = db.ExecContext(ctx, `+ INSERT INTO oauth2_refresh_token (token_hash, client_id, user_email, workspace, config, expires_at)+ VALUES ($1, $2, '[email protected]', $3, '{}', now() + interval '1 day')`,+ auth.HashToken(legacyRefresh), clientID, workspaceID)+ require.NoError(t, err)++ code, description = oauth2Error(t, ctl, oauth2RefreshForm(clientID, legacyRefresh, nil), http.StatusBadRequest)+ require.Equal(t, "invalid_grant", code)+ require.Contains(t, description, "reauthorize tool")+ // Refusal happens before consumption: the row is left for the expiry+ // sweep (or the user's reauthorize) rather than burned by a refresh+ // that issued nothing.+ require.NotNil(t, storedRefreshToken(ctx, t, db, clientID, legacyRefresh))++ // A bound grant with no scope is not legacy — only a missing resource+ // marks the retired population.+ _, boundRefresh, boundClient := mintMCPOAuthTokenWithScope(t, ctl, bearer, "")+ oauth2TokenOK(t, ctl, oauth2RefreshForm(boundClient, boundRefresh, nil))+ })++ t.Run("the reauthorize tool revokes the grant it is called under", func(t *testing.T) {+ // backend/api/mcp only records that the tool asked its store to delete+ // the caller's refresh grants; this is the contract that the real+ // store deletes them, so the token endpoint stops refreshing, and that+ // the bearer the tool was called with is refused from then on.+ accessToken, refreshToken, toolClient := mintMCPOAuthTokenWithScope(t, ctl, bearer, "mcp:read-only")+ session := openMCPSession(ctx, t, ctl, accessToken)+ defer session.Close()+ result, err := session.CallTool(ctx, &mcp.CallToolParams{Name: "reauthorize"})+ require.NoError(t, err)+ require.False(t, result.IsError)++ require.Nil(t, storedRefreshToken(ctx, t, db, toolClient, refreshToken))+ code, _ := oauth2Error(t, ctl, oauth2RefreshForm(toolClient, refreshToken, nil), http.StatusBadRequest)+ require.Equal(t, "invalid_grant", code)+ status, _ := postMCP(t, ctl, accessToken)+ require.Equal(t, http.StatusUnauthorized, status)+ })++ t.Run("a requested scope set is consented as its maximum", func(t *testing.T) {+ // One `scope` value naming both tiers, which is what the v1 bootstrap+ // produces — the 401 challenge advertises every mode+ // pre-authentication, so clients ask for all of them. The set resolves+ // to one stored mode; a multi-mode string must never reach the grant+ // record. And the client can keep sending the set it asked for: it+ // normalizes to the same mode, so the exchange matches.+ code := consentOAuth2Code(t, ctl, bearer, clientID, url.Values{"scope": {"mcp:read-only mcp:read-write"}})+ require.Equal(t, "mcp:read-write", storedAuthorizationCode(ctx, t, db, clientID, code).Scope)++ first := oauth2TokenOK(t, ctl, oauth2ExchangeForm(clientID, code, url.Values{"scope": {"mcp:read-only mcp:read-write"}}))+ require.Equal(t, "mcp:read-write", first["scope"])+ refreshToken := stringField(t, first, "refresh_token")+ require.Equal(t, "mcp:read-write", storedRefreshToken(ctx, t, db, clientID, refreshToken).Scope)+ })+}++// TestOAuth2IssuanceRechecksTheCeiling pins the half a consent-time check alone+// would miss. The consent decision is captured once and reused at every later+// issuance: an authorization code outlives the consent by ten minutes and a+// refresh token by thirty days, so a grant consented while MCP was on would+// keep minting access tokens long after an admin turned MCP off.+//+// The refusal must read as reversible, not as a dead grant: invalid_grant makes+// a compliant client discard the credential and rerun the OAuth flow, exactly+// what this path retains the credential to avoid. And it must not burn the+// credential it refuses — the ceiling is a toggle, and raising it again must+// restore service without a fresh interactive consent.+func TestOAuth2IssuanceRechecksTheCeiling(t *testing.T) {+ t.Parallel()+ a := require.New(t)+ ctx := context.Background()+ ctl := &controller{}+ ctx, err := ctl.StartServerWithExternalPg(ctx)+ a.NoError(err)+ defer ctl.Close(ctx)++ db, err := sql.Open("pgx", ctl.profile.PgURL)+ a.NoError(err)+ defer db.Close()++ bearer := ctl.authInterceptor.token+ canonical := ctl.rootURL + "/mcp"+ a.NoError(ctl.setMCPCapability(ctx, v1pb.MCPSetting_READ_WRITE))++ // Consent while MCP is on, so the grant is genuine and only the ceiling+ // moves underneath it.+ _, refreshToken, clientID := mintMCPOAuthTokenWithScope(t, ctl, bearer, "mcp:read-only")+ a.NoError(ctl.setMCPCapability(ctx, v1pb.MCPSetting_DISABLED))++ code, description := oauth2Error(t, ctl, oauth2RefreshForm(clientID, refreshToken, nil), http.StatusServiceUnavailable)+ a.Equal("temporarily_unavailable", code, "a compliant client discards the grant on invalid_grant, and this one must keep it")+ a.Contains(description, "turned MCP access off")+ a.NotNil(storedRefreshToken(ctx, t, db, clientID, refreshToken), "a refused refresh must not consume the token")++ a.NoError(ctl.setMCPCapability(ctx, v1pb.MCPSetting_READ_WRITE))+ oauth2TokenOK(t, ctl, oauth2RefreshForm(clientID, refreshToken, nil))++ // A code minted before the ceiling moved. The consent that produced it was+ // allowed; the exchange is not, and is not burned either.+ fresh := consentOAuth2Code(t, ctl, bearer, clientID, url.Values{"resource": {canonical}, "scope": {"mcp:read-only"}})+ a.NoError(ctl.setMCPCapability(ctx, v1pb.MCPSetting_DISABLED))+ code, _ = oauth2Error(t, ctl, oauth2ExchangeForm(clientID, fresh, nil), http.StatusServiceUnavailable)+ a.Equal("temporarily_unavailable", code)+ a.NotNil(storedAuthorizationCode(ctx, t, db, clientID, fresh), "a refused exchange must not consume the code")++ a.NoError(ctl.setMCPCapability(ctx, v1pb.MCPSetting_READ_WRITE))+ oauth2TokenOK(t, ctl, oauth2ExchangeForm(clientID, fresh, nil))+}diff --git a/backend/tests/project_instance_compat_extras_test.go b/backend/tests/project_instance_compat_extras_test.goindex ea58b4b98c..7272102089 100644--- a/backend/tests/project_instance_compat_extras_test.go+++ b/backend/tests/project_instance_compat_extras_test.go@@ -13,6 +13,7 @@ import ("github.com/alexmullins/zip""github.com/stretchr/testify/require""google.golang.org/protobuf/types/known/durationpb"+ "google.golang.org/protobuf/types/known/fieldmaskpb""github.com/bytebase/bytebase/backend/common"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"@@ -165,6 +166,26 @@ func TestProjectInstanceSavedQuery(t *testing.T) {a.Error(err)a.Equal(connect.CodeInvalidArgument, connect.CodeOf(err))a.Contains(err.Error(), fmt.Sprintf("database name %q is not canonical for its instance", workspaceFormName))++ // The database reference is a soft link. Once the instance is purged, an+ // autosave that re-sends the stored, now dangling, value must not fail+ // validation — that would brick content saves — while an explicit change+ // to a database that does not exist still fails hard.+ _, err = ctl.instanceServiceClient.DeleteInstance(ctx, connect.NewRequest(&v1pb.DeleteInstanceRequest{Name: instance.Name}))+ a.NoError(err)+ _, err = ctl.instanceServiceClient.DeleteInstance(ctx, connect.NewRequest(&v1pb.DeleteInstanceRequest{Name: instance.Name, Purge: true}))+ a.NoError(err)+ updated, err := ctl.savedQueryServiceClient.UpdateSavedQuery(ctx, connect.NewRequest(&v1pb.UpdateSavedQueryRequest{+ SavedQuery: &v1pb.SavedQuery{Name: created.Name, Database: databaseName, Content: []byte("SELECT 2;")},+ UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"database", "content"}},+ }))+ a.NoError(err)+ a.Equal(databaseName, updated.Msg.Database)+ _, err = ctl.savedQueryServiceClient.UpdateSavedQuery(ctx, connect.NewRequest(&v1pb.UpdateSavedQueryRequest{+ SavedQuery: &v1pb.SavedQuery{Name: created.Name, Database: "instances/another-missing/databases/db"},+ UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"database"}},+ }))+ a.Equal(connect.CodeNotFound, connect.CodeOf(err))}// TestProjectInstanceAccessGrant verifies that access grants targeting adiff --git a/backend/tests/project_instance_test.go b/backend/tests/project_instance_test.goindex b3091c8d14..fcf4a8ad06 100644--- a/backend/tests/project_instance_test.go+++ b/backend/tests/project_instance_test.go@@ -88,6 +88,14 @@ func TestProjectInstanceCoreBehavior(t *testing.T) {a.NoError(err)a.Len(query.Msg.Results, 1)+ // A batch validates every target before scheduling any: one name that+ // cannot be resolved fails the whole request.+ _, err = ctl.databaseServiceClient.BatchSyncDatabases(ctx, connect.NewRequest(&v1pb.BatchSyncDatabasesRequest{+ Parent: projectInstance.Name,+ Names: []string{databaseName, "invalid"},+ }))+ a.Equal(connect.CodeInvalidArgument, connect.CodeOf(err))+// The history row carries the project-scoped database name, so the// `instance ==` filter must accept the project-scoped instance name// (T18c-ii); the workspace form of the same ID names no instance here.@@ -436,3 +444,34 @@ func projectInstanceTestInstanceNames(instances []*v1pb.Instance) []string {}return names}++// TestUndeleteProjectInstanceChecksActivationLimit pins that restoring an+// archived, activated project instance counts against the plan's activated+// instance limit the way creating one does: with the license gone it is+// refused, and it comes back once the license is.+func TestUndeleteProjectInstanceChecksActivationLimit(t *testing.T) {+ t.Parallel()+ a := require.New(t)+ ctx := context.Background()+ ctl := &controller{}+ ctx, err := ctl.StartServerWithExternalPg(ctx)+ a.NoError(err)+ defer ctl.Close(ctx)++ pg, err := provisionPgInstance(ctx, t)+ a.NoError(err)+ instance := createProjectInstanceTestInstance(ctx, t, ctl, &ctl.project.Name, "bot35-activation-limit", "activated project instance", pg)+ a.True(instance.Activation)++ _, err = ctl.instanceServiceClient.DeleteInstance(ctx, connect.NewRequest(&v1pb.DeleteInstanceRequest{Name: instance.Name}))+ a.NoError(err)++ a.NoError(ctl.removeLicense(ctx))+ _, err = ctl.instanceServiceClient.UndeleteInstance(ctx, connect.NewRequest(&v1pb.UndeleteInstanceRequest{Name: instance.Name}))+ a.Equal(connect.CodeResourceExhausted, connect.CodeOf(err))++ a.NoError(ctl.setLicense(ctx))+ restored, err := ctl.instanceServiceClient.UndeleteInstance(ctx, connect.NewRequest(&v1pb.UndeleteInstanceRequest{Name: instance.Name}))+ a.NoError(err)+ a.True(restored.Msg.Activation)+}diff --git a/backend/tests/project_instance_workflow_test.go b/backend/tests/project_instance_workflow_test.goindex 019da40dbc..e7bf40b5e2 100644--- a/backend/tests/project_instance_workflow_test.go+++ b/backend/tests/project_instance_workflow_test.go@@ -3,6 +3,7 @@ package testsimport ("context""fmt"+ "strings""testing""connectrpc.com/connect"@@ -101,6 +102,66 @@ func TestProjectInstanceWorkflowTargets(t *testing.T) {a.NoError(err)a.Equal(databaseName, rollout.Msg.Stages[0].Tasks[0].Target)+ // The instance's and database's descendant collections answer under the+ // canonical names and refuse the workspace form of the same instance.+ roles, err := ctl.instanceRoleServiceClient.ListInstanceRoles(ctx, connect.NewRequest(&v1pb.ListInstanceRolesRequest{Parent: instance.Name}))+ a.NoError(err)+ a.NotEmpty(roles.Msg.Roles)+ for _, role := range roles.Msg.Roles {+ a.True(strings.HasPrefix(role.Name, instance.Name+"/roles/"), role.Name)+ }+ changelogs, err := ctl.changelogServiceClient.ListChangelogs(ctx, connect.NewRequest(&v1pb.ListChangelogsRequest{Parent: databaseName}))+ a.NoError(err)+ for _, changelog := range changelogs.Msg.Changelogs {+ a.True(strings.HasPrefix(changelog.Name, databaseName+"/changelogs/"), changelog.Name)+ }+ revisions, err := ctl.revisionServiceClient.ListRevisions(ctx, connect.NewRequest(&v1pb.ListRevisionsRequest{Parent: databaseName}))+ a.NoError(err)+ for _, revision := range revisions.Msg.Revisions {+ a.True(strings.HasPrefix(revision.Name, databaseName+"/revisions/"), revision.Name)+ }+ _, err = ctl.instanceRoleServiceClient.ListInstanceRoles(ctx, connect.NewRequest(&v1pb.ListInstanceRolesRequest{Parent: "instances/bot36-project-instance"}))+ a.Equal(connect.CodeNotFound, connect.CodeOf(err))+ _, err = ctl.changelogServiceClient.ListChangelogs(ctx, connect.NewRequest(&v1pb.ListChangelogsRequest{Parent: workspaceAliasDatabase}))+ a.Equal(connect.CodeNotFound, connect.CodeOf(err))+ _, err = ctl.revisionServiceClient.ListRevisions(ctx, connect.NewRequest(&v1pb.ListRevisionsRequest{Parent: workspaceAliasDatabase}))+ a.Equal(connect.CodeNotFound, connect.CodeOf(err))++ // A release check confines its targets the same way: the canonical name+ // is checked and answered under that name, the workspace form of the same+ // database is not found, a target spelling another project is malformed+ // for this project, and an archived project checks nothing at all.+ release := &v1pb.Release{+ Type: v1pb.Release_DECLARATIVE,+ Files: []*v1pb.Release_File{{Path: "schema.sql", Version: "1", Statement: []byte("DROP TABLE obsolete;")}},+ }+ checkRelease := func(parent string, target string) (*v1pb.CheckReleaseResponse, error) {+ resp, err := ctl.releaseServiceClient.CheckRelease(ctx, connect.NewRequest(&v1pb.CheckReleaseRequest{+ Parent: parent,+ Release: release,+ Targets: []string{target},+ }))+ if err != nil {+ return nil, err+ }+ return resp.Msg, nil+ }+ checked, err := checkRelease(ctl.project.Name, databaseName)+ a.NoError(err)+ a.Len(checked.Results, 1)+ a.Equal(databaseName, checked.Results[0].Target)+ _, err = checkRelease(ctl.project.Name, workspaceAliasDatabase)+ a.Equal(connect.CodeNotFound, connect.CodeOf(err))+ _, err = checkRelease(ctl.project.Name, crossProjectDatabaseName)+ a.Equal(connect.CodeInvalidArgument, connect.CodeOf(err))+ _, err = checkRelease(otherProject.Name, databaseName)+ a.Equal(connect.CodeInvalidArgument, connect.CodeOf(err))++ _, err = ctl.projectServiceClient.DeleteProject(ctx, connect.NewRequest(&v1pb.DeleteProjectRequest{Name: otherProject.Name}))+ a.NoError(err)+ _, err = checkRelease(otherProject.Name, databaseName)+ a.Equal(connect.CodeFailedPrecondition, connect.CodeOf(err))+_, err = ctl.planServiceClient.CreatePlan(ctx, connect.NewRequest(&v1pb.CreatePlanRequest{Parent: otherProject.Name,Plan: &v1pb.Plan{Specs: []*v1pb.Plan_Spec{changeSpec(databaseName)}},diff --git a/backend/api/v1/sample_project_instance_integration_test.go b/backend/tests/sample_project_instance_test.gosimilarity index 84%rename from backend/api/v1/sample_project_instance_integration_test.gorename to backend/tests/sample_project_instance_test.goindex 8a0b104193..d54d89eefd 100644--- a/backend/api/v1/sample_project_instance_integration_test.go+++ b/backend/tests/sample_project_instance_test.go@@ -1,10 +1,9 @@-package v1+package testsimport ("context""crypto/sha256""database/sql"- "errors""fmt""net/http/httptest""net/url"@@ -14,15 +13,16 @@ import ("testing""time"- "github.com/bytebase/bytebase/backend/common/testcontainer"-"connectrpc.com/connect""github.com/jackc/pgx/v5"+ "github.com/pkg/errors""github.com/stretchr/testify/require""google.golang.org/protobuf/encoding/protojson"+ apiv1 "github.com/bytebase/bytebase/backend/api/v1""github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/permission"+ "github.com/bytebase/bytebase/backend/common/testcontainer""github.com/bytebase/bytebase/backend/component/config""github.com/bytebase/bytebase/backend/component/dbfactory""github.com/bytebase/bytebase/backend/component/iam"@@ -32,16 +32,77 @@ import (storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1""github.com/bytebase/bytebase/backend/generated-go/v1/v1connect"- _ "github.com/bytebase/bytebase/backend/plugin/db/pg"+ "github.com/bytebase/bytebase/backend/migrator"samplerunner "github.com/bytebase/bytebase/backend/runner/sample""github.com/bytebase/bytebase/backend/runner/schemasync""github.com/bytebase/bytebase/backend/store")+// The Sample Project Instance tests provision roles and databases on a real+// TLS Postgres target, run the cleanup runner against it, and issue DDL as the+// sample role: that is engine plus workflow, which is why they live here and+// not beside the handler. They drive the service directly rather than a+// server, so each gets its own migrated metadata database on the package's+// Postgres and they share one target, isolated by workspace.++// newMetadataStore gives a test its own migrated metadata database on the+// package's Postgres, for service-level tests that need a Store but no server.+func newMetadataStore(ctx context.Context, t *testing.T) (*sql.DB, *store.Store) {+ t.Helper()+ admin, err := sql.Open("pgx", fmt.Sprintf("postgresql://postgres:root-password@%s:%s/postgres", externalPgHost, externalPgPort))+ require.NoError(t, err)+ defer admin.Close()+ name := getTestDatabaseString()+ _, err = admin.ExecContext(ctx, "CREATE DATABASE "+name)+ require.NoError(t, err)++ dsn := fmt.Sprintf("postgresql://postgres:root-password@%s:%s/%s", externalPgHost, externalPgPort, name)+ db, err := sql.Open("pgx", dsn)+ require.NoError(t, err)+ require.NoError(t, migrator.MigrateSchema(ctx, db))+ stores, err := store.New(ctx, dsn, false)+ require.NoError(t, err)+ t.Cleanup(func() {+ require.NoError(t, stores.Close())+ require.NoError(t, db.Close())+ })+ return db, stores+}++var (+ sampleTargetOnce sync.Once+ sampleTargetContainer *testcontainer.Container+ sampleTargetErr error+)++// sharedSampleTarget starts the TLS Postgres the sample tests provision onto,+// once for the package; startMain takes it down. The tests isolate by+// workspace — every sample resource name derives from the workspace ID — so+// one target serves them all.+func sharedSampleTarget(t *testing.T) *testcontainer.Container {+ t.Helper()+ sampleTargetOnce.Do(func() {+ ctx := context.Background()+ container, err := testcontainer.GetTLSPgContainer(ctx)+ if err != nil {+ sampleTargetErr = err+ return+ }+ if err := prepareSampleTargetBaseline(ctx, container.GetDB()); err != nil {+ container.Close(ctx)+ sampleTargetErr = err+ return+ }+ sampleTargetContainer = container+ })+ require.NoError(t, sampleTargetErr)+ return sampleTargetContainer+}+func TestPrepareSampleProjectInstanceLifecycle(t *testing.T) {t.Parallel()now := time.Date(2026, time.August, 17, 12, 0, 0, 0, time.UTC)- ctx, fixture := newSampleProjectInstanceFixture(t, func() time.Time { return now })+ ctx, fixture := newSampleProjectInstanceFixture(t, func() time.Time { return now }, "sample-lifecycle")runner := samplerunner.NewRunner(fixture.manager)prepared, err := fixture.service.PrepareSampleProjectInstance(ctx, connect.NewRequest(&v1pb.PrepareSampleProjectInstanceRequest{@@ -151,7 +212,7 @@ func TestPrepareSampleProjectInstanceLifecycle(t *testing.T) {func TestSampleProjectPurgeRejectsPendingReservation(t *testing.T) {t.Parallel()- ctx, fixture := newSampleProjectInstanceFixture(t, time.Now)+ ctx, fixture := newSampleProjectInstanceFixture(t, time.Now, "sample-purge")payload := &storepb.SaaSSampleInstanceSetupPayload{ProjectId: fixture.projectID,InstanceId: "pending-sample",@@ -179,17 +240,47 @@ func TestSampleProjectPurgeRejectsPendingReservation(t *testing.T) {require.Nil(t, setup.DeletedAt)}-//nolint:tparallel // Subtests share the parent's fixture.+// TestPrepareSampleProjectInstanceRejectsConsumedEntitlementAfterProjectDeletion+// pins that a workspace's one sample entitlement stays consumed once it was+// spent, even after the project it was spent on is deleted: the next request+// is refused rather than provisioning a second sample.+func TestPrepareSampleProjectInstanceRejectsConsumedEntitlementAfterProjectDeletion(t *testing.T) {+ t.Parallel()+ ctx, fixture := newSampleProjectInstanceFixture(t, time.Now, "sample-consumed")+ payload, err := protojson.Marshal(&storepb.SaaSSampleInstanceSetupPayload{+ ProjectId: fixture.projectID,+ InstanceId: "sample-deleted-project",+ Title: "Sample Project Instance",+ DatabaseName: "bb_sample_deleted_project",+ RoleName: "bb_sample_role_deleted_project",+ })+ require.NoError(t, err)+ _, _, err = fixture.store.ReserveSampleInstanceSetup(ctx, &store.SampleInstanceSetupMessage{+ WorkspaceID: fixture.workspaceID,+ ReplicaID: "replica-a",+ Payload: payload,+ })+ require.NoError(t, err)+ _, err = fixture.store.GetDB().ExecContext(ctx, `UPDATE project SET deleted = TRUE WHERE workspace = $1 AND resource_id = $2`, fixture.workspaceID, fixture.projectID)+ require.NoError(t, err)++ _, err = fixture.service.PrepareSampleProjectInstance(ctx, connect.NewRequest(&v1pb.PrepareSampleProjectInstanceRequest{+ Parent: common.FormatProject(fixture.projectID),+ }))+ require.Equal(t, connect.CodeFailedPrecondition, connect.CodeOf(err))+}++//nolint:tparallel // Subtests share one fixture and advance its clock in order.func TestPrepareSampleProjectInstanceAdditionalLifecycleCoverage(t *testing.T) {t.Parallel()now := time.Date(2026, time.August, 17, 12, 0, 0, 0, time.UTC)- ctx, fixture := newSampleProjectInstanceFixture(t, func() time.Time { return now })+ ctx, fixture := newSampleProjectInstanceFixture(t, func() time.Time { return now }, "sample-extra")runner := samplerunner.NewRunner(fixture.manager)t.Run("ACL denies callers without project permission", func(t *testing.T) {iamManager, err := iam.NewManager(fixture.store, nil, true)require.NoError(t, err)- aclInterceptor := NewACLInterceptor(fixture.store, "", iamManager, fixture.service.profile)+ aclInterceptor := apiv1.NewACLInterceptor(fixture.store, "", iamManager, fixture.profile)injectIdentity := connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc {return func(requestCtx context.Context, request connect.AnyRequest) (connect.AnyResponse, error) {requestCtx = context.WithValue(requestCtx, common.WorkspaceIDContextKey, fixture.workspaceID)@@ -277,7 +368,7 @@ func TestPrepareSampleProjectInstanceAdditionalLifecycleCoverage(t *testing.T) {require.False(t, fixture.target.roleExists(staleCtx, allocation.Role))instance, err := fixture.store.GetInstance(staleCtx, &store.FindInstanceMessage{Workspace: "sample-workspace-stale",- ResourceID: ptr(sampleInstanceID("sample-workspace-stale")),+ ResourceID: new(sampleInstanceID("sample-workspace-stale")),ShowDeleted: true,})require.NoError(t, err)@@ -326,37 +417,33 @@ func TestPrepareSampleProjectInstanceAdditionalLifecycleCoverage(t *testing.T) {type sampleProjectInstanceFixture struct {store *store.Store- service *InstanceService+ service *apiv1.InstanceService+ profile *config.Profile+ syncer *schemasync.Syncermanager *saas.Managertarget *sampleTargetInspectorworkspaceID stringprojectID string}-func newSampleProjectInstanceFixture(t *testing.T, clock func() time.Time) (context.Context, *sampleProjectInstanceFixture) {+func newSampleProjectInstanceFixture(t *testing.T, clock func() time.Time, workspaceID string) (context.Context, *sampleProjectInstanceFixture) {t.Helper()ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)t.Cleanup(cancel)- // Only the target is a container: it is a TLS server Bytebase connects to,- // not metadata.- target := testcontainer.GetTestTLSPgContainer(ctx, t)- t.Cleanup(func() { target.Close(context.Background()) })-- metadataDB, stores, _ := testcontainer.NewMetadataDB(t)- _, err := metadataDB.ExecContext(ctx, `- INSERT INTO workspace (resource_id) VALUES ('sample-workspace');- INSERT INTO project (resource_id, workspace, name) VALUES ('sample-project', 'sample-workspace', 'Sample Project');- `)+ target := sharedSampleTarget(t)+ _, stores := newMetadataStore(ctx, t)+ _, err := stores.GetDB().ExecContext(ctx, `INSERT INTO workspace (resource_id) VALUES ($1)`, workspaceID)+ require.NoError(t, err)+ _, err = stores.GetDB().ExecContext(ctx, `INSERT INTO project (resource_id, workspace, name) VALUES ('sample-project', $1, 'Sample Project')`, workspaceID)require.NoError(t, err)- setSampleProjectInstanceTestEnvironment(ctx, t, stores, "sample-workspace")+ setSampleProjectInstanceTestEnvironment(ctx, t, stores, workspaceID)licenseService, err := enterprise.NewLicenseService(common.ReleaseModeDev, stores, false, "")require.NoError(t, err)dbFactory := dbfactory.New(stores, licenseService)syncer := schemasync.NewSyncer(stores, dbFactory, licenseService, nil)- require.NoError(t, prepareSampleTargetBaseline(ctx, target.GetDB()))targetURL := tlsPostgresTestURL(target)inspector := newSampleTargetInspector(t, targetURL)manager, err := saas.NewManager(@@ -366,19 +453,15 @@ func newSampleProjectInstanceFixture(t *testing.T, clock func() time.Time) (contsample.ManagerOptions{Clock: clock, ReplicaID: "replica-a"},)require.NoError(t, err)- return context.WithValue(ctx, common.WorkspaceIDContextKey, "sample-workspace"), &sampleProjectInstanceFixture{- store: stores,- service: &InstanceService{- store: stores,- profile: &config.Profile{SaaS: true},- licenseService: licenseService,- dbFactory: dbFactory,- schemaSyncer: syncer,- sampleManager: manager,- },+ profile := &config.Profile{SaaS: true}+ return context.WithValue(ctx, common.WorkspaceIDContextKey, workspaceID), &sampleProjectInstanceFixture{+ store: stores,+ service: apiv1.NewInstanceService(stores, profile, licenseService, dbFactory, syncer, manager),+ profile: profile,+ syncer: syncer,manager: manager,target: inspector,- workspaceID: "sample-workspace",+ workspaceID: workspaceID,projectID: "sample-project",}}@@ -414,7 +497,7 @@ func (f *sampleProjectInstanceFixture) sampleRecordFor(ctx context.Context, t *tpayload := &storepb.SaaSSampleInstanceSetupPayload{}require.NoError(t, common.ProtojsonUnmarshaler.Unmarshal(setup.Payload, payload))require.NotNil(t, setup.ExpiresAt)- record := sampleProjectInstanceRecord{+ return sampleProjectInstanceRecord{workspace: workspaceID,project: payload.ProjectId,instance: payload.InstanceId,@@ -424,7 +507,6 @@ func (f *sampleProjectInstanceFixture) sampleRecordFor(ctx context.Context, t *texpiresAt: *setup.ExpiresAt,deletedAt: setup.DeletedAt,}- return record}func (f *sampleProjectInstanceFixture) sampleRecordCount(ctx context.Context, t *testing.T) int {@@ -455,11 +537,8 @@ func (f *sampleProjectInstanceFixture) allocation(ctx context.Context, t *testinreturn f.allocationFor(ctx, t, f.workspaceID)}-func (f *sampleProjectInstanceFixture) allocationFor(- ctx context.Context,- t *testing.T,- workspaceID string,-) testAllocation {+func (f *sampleProjectInstanceFixture) allocationFor(ctx context.Context, t *testing.T, workspaceID string) testAllocation {+ t.Helper()record := f.sampleRecordFor(ctx, t, workspaceID)instance, err := f.store.GetInstance(ctx, &store.FindInstanceMessage{Workspace: workspaceID,@@ -544,7 +623,7 @@ func (f *sampleProjectInstanceFixture) createPartialReservation(ProjectId: projectID,InstanceId: sampleInstanceID(workspaceID),Title: "Sample Project Instance",- EnvironmentId: ptr("test"),+ EnvironmentId: new("test"),DatabaseName: sampleDatabaseName(workspaceID),RoleName: sampleRoleName(workspaceID),}@@ -567,7 +646,7 @@ func (f *sampleProjectInstanceFixture) createPartialReservation(instance, err := f.store.CreateInstance(ctx, &store.InstanceMessage{Workspace: workspaceID,ProjectID: &projectID,- EnvironmentID: ptr("test"),+ EnvironmentID: new("test"),ResourceID: payload.InstanceId,Metadata: &storepb.Instance{Title: "Sample Project Instance",@@ -582,7 +661,7 @@ func (f *sampleProjectInstanceFixture) createPartialReservation(},})require.NoError(t, err)- _, _, databases, err := f.service.schemaSyncer.SyncInstance(ctx, instance)+ _, _, databases, err := f.syncer.SyncInstance(ctx, instance)require.NoError(t, err)require.Len(t, databases, 1)_, err = f.store.GetDB().ExecContext(ctx, `diff --git a/backend/tests/tests.go b/backend/tests/tests.goindex 9e0b74ec91..f7909afeac 100644--- a/backend/tests/tests.go+++ b/backend/tests/tests.go@@ -114,6 +114,8 @@ type controller struct {serviceAccountServiceClient v1connect.ServiceAccountServiceClientworkloadIdentityServiceClient v1connect.WorkloadIdentityServiceClientaccessGrantServiceClient v1connect.AccessGrantServiceClient+ identityProviderServiceClient v1connect.IdentityProviderServiceClient+ instanceRoleServiceClient v1connect.InstanceRoleServiceClientproject *v1pb.Project@@ -288,6 +290,8 @@ func (ctl *controller) start(ctx context.Context, port int) (context.Context, erctl.projectServiceClient = v1connect.NewProjectServiceClient(ctl.client, baseURL, interceptors)ctl.databaseGroupServiceClient = v1connect.NewDatabaseGroupServiceClient(ctl.client, baseURL, interceptors)ctl.authServiceClient = v1connect.NewAuthServiceClient(ctl.client, baseURL, interceptors)+ ctl.identityProviderServiceClient = v1connect.NewIdentityProviderServiceClient(ctl.client, baseURL, interceptors)+ ctl.instanceRoleServiceClient = v1connect.NewInstanceRoleServiceClient(ctl.client, baseURL, interceptors)ctl.userServiceClient = v1connect.NewUserServiceClient(ctl.client, baseURL, interceptors)ctl.settingServiceClient = v1connect.NewSettingServiceClient(ctl.client, baseURL, interceptors)ctl.instanceServiceClient = v1connect.NewInstanceServiceClient(ctl.client, baseURL, interceptors)diff --git a/docs/design/backend-test-execution-time.md b/docs/design/backend-test-execution-time.mdindex 1b2ae7fd25..00cbae1d7b 100644--- a/docs/design/backend-test-execution-time.md+++ b/docs/design/backend-test-execution-time.md@@ -509,8 +509,8 @@ below stands; only the payoff line has changed. `backend/api/mcp` remains theproof it is livable: 168 tests in 19 s, 16 of its 18 files containerless.Effort 2 declined a fake store on two grounds, and both fail here. `api/v1` is-not testing its API — `mcp_info_test.go` calls `service.GetMCPInfo` as a Go-method, so the container buys a `*store.Store` and nothing the test asserts. And+not testing its API — `mcp_info_test.go` called `service.GetMCPInfo` as a Go+method, so the container bought a `*store.Store` and nothing the test asserts. And"a fake store" was the wrong unit: `store.Store` has 305 methods and `api/v1`calls 181, but `api/mcp` needed five (`serverStore`, faked in 62 lines) and`mcp_gate.go` needed one (`mcpSettingsReader`). Interfaces go beside the handler@@ -602,8 +602,20 @@ Five of the 18 call unexported `api/v1` methods — `getAndVerifyUser`,move without exporting them. `TestLoginAttemptRetentionOutlivesLockouts` is nota database test at all; it compares two constants.-Convert `mcp_info_test.go` first — `mcpSettingsReader` exists and the test is 60-lines — and measure before doing the rest. Server boots go the same way:+**Landed for `backend/api`.** `auth`, `oauth2`, `lsp` and `mcp` hold no+metadata Postgres now — about 1.5 s each, `mcp` 9.6 s of deliberate sleeps —+and `api/v1` runs in 9 s with 14 tests left on the shared container. No fake+was written. The decide-and-convert halves came out as functions over plain+data (`buildAuditRows`, `buildV1Plans`, `linkedIssueForCreate`,+`checkReleaseDatabase`, `consentRefusalRow`), `mcp_info_test.go` became+`TestActuatorMCPSetting` over the existing `mcpSettingsReader`, and what needs+a live workflow — the OAuth2 grant lifecycle, login lockouts, the sample+project instance — joined the tests already in `backend/tests`. The 14 that+stay need state no API can set up: a planted email code, a stale approval+snapshot, a sample instance's lifecycle hooks. The seven packages outside+`api/` are untouched.++Server boots go the same way:`mcp_capability_setting_test.go` spends 6 to read and write one settings row,the saved-query list and filter tests 12, GitOps `CheckRelease` 11 of 17. Keepthe 27 `TestCollision*` and `TestClaim*` tests where they are —