Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: dml for export jit (#20501)

ecmadaoa2a2db2c2842
diff --git a/backend/api/mcp/gen/openapi.yaml b/backend/api/mcp/gen/openapi.yaml
index 7eafae87c5..bcba1dec17 100644
--- a/backend/api/mcp/gen/openapi.yaml
+++ b/backend/api/mcp/gen/openapi.yaml
@@ -20509,7 +20509,7 @@ components:
When source is CREATE_DATABASE, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name
When source is EXPORT_DATA, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name, resource.schema_name, resource.table_name
When source is REQUEST_ROLE, support: resource.project_id, request.expiration_days, request.role
- When source is REQUEST_ACCESS, support: resource.environment_id, resource.project_id, request.unmask, request.data_export
+ When source is REQUEST_ACCESS, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name, resource.schema_name, resource.table_name, request.unmask, request.data_export
For examples:
resource.environment_id == "prod" && statement.affected_rows >= 100
diff --git a/backend/api/v1/release_service_check.go b/backend/api/v1/release_service_check.go
index 10ec4587cf..0d3055b0a9 100644
--- a/backend/api/v1/release_service_check.go
+++ b/backend/api/v1/release_service_check.go
@@ -14,6 +14,7 @@ import (
"google.golang.org/protobuf/proto"
"github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/component/parsercontext"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
"github.com/bytebase/bytebase/backend/plugin/advisor"
@@ -735,7 +736,7 @@ func (s *ReleaseService) runSQLReviewCheckForFile(
Driver: connection,
CurrentDatabase: database.DatabaseName,
TenantMode: project.Setting.GetPostgresDatabaseTenantMode(),
- ListDatabaseNamesFunc: BuildListDatabaseNamesFunc(s.store),
+ ListDatabaseNamesFunc: parsercontext.BuildListDatabaseNamesFunc(s.store),
InstanceID: instance.ResourceID,
IsObjectCaseSensitive: store.IsObjectCaseSensitive(instance),
}
diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.go
index ba63b0e576..297363bc8b 100644
--- a/backend/api/v1/rollout_service.go
+++ b/backend/api/v1/rollout_service.go
@@ -21,6 +21,7 @@ import (
"github.com/bytebase/bytebase/backend/component/bus"
"github.com/bytebase/bytebase/backend/component/dbfactory"
"github.com/bytebase/bytebase/backend/component/iam"
+ "github.com/bytebase/bytebase/backend/component/parsercontext"
"github.com/bytebase/bytebase/backend/component/webhook"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
@@ -1180,8 +1181,8 @@ func (s *RolloutService) PreviewTaskRunRollback(ctx context.Context, req *connec
for _, item := range backupDetail.Items {
restore, err := parserbase.GenerateRestoreSQL(ctx, instance.Metadata.GetEngine(), parserbase.RestoreContext{
InstanceID: instance.ResourceID,
- GetDatabaseMetadataFunc: BuildGetDatabaseMetadataFunc(s.store),
- ListDatabaseNamesFunc: BuildListDatabaseNamesFunc(s.store),
+ GetDatabaseMetadataFunc: parsercontext.BuildGetDatabaseMetadataFunc(s.store),
+ ListDatabaseNamesFunc: parsercontext.BuildListDatabaseNamesFunc(s.store),
IsCaseSensitive: store.IsObjectCaseSensitive(instance),
}, statements, item)
if err != nil {
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index 6f4575e450..3719bab367 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -24,6 +24,7 @@ import (
"github.com/bytebase/bytebase/backend/component/dbfactory"
"github.com/bytebase/bytebase/backend/component/export"
"github.com/bytebase/bytebase/backend/component/iam"
+ "github.com/bytebase/bytebase/backend/component/parsercontext"
"github.com/bytebase/bytebase/backend/enterprise"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
@@ -642,9 +643,9 @@ func queryRetry(
ctx,
parserbase.GetQuerySpanContext{
InstanceID: instance.ResourceID,
- GetDatabaseMetadataFunc: BuildGetDatabaseMetadataFunc(stores),
- ListDatabaseNamesFunc: BuildListDatabaseNamesFunc(stores),
- GetLinkedDatabaseMetadataFunc: BuildGetLinkedDatabaseMetadataFunc(stores, instance.Metadata.GetEngine()),
+ GetDatabaseMetadataFunc: parsercontext.BuildGetDatabaseMetadataFunc(stores),
+ ListDatabaseNamesFunc: parsercontext.BuildListDatabaseNamesFunc(stores),
+ GetLinkedDatabaseMetadataFunc: parsercontext.BuildGetLinkedDatabaseMetadataFunc(stores, instance.Metadata.GetEngine()),
},
instance.Metadata.GetEngine(),
statements,
@@ -745,9 +746,9 @@ func queryRetry(
ctx,
parserbase.GetQuerySpanContext{
InstanceID: instance.ResourceID,
- GetDatabaseMetadataFunc: BuildGetDatabaseMetadataFunc(stores),
- ListDatabaseNamesFunc: BuildListDatabaseNamesFunc(stores),
- GetLinkedDatabaseMetadataFunc: BuildGetLinkedDatabaseMetadataFunc(stores, instance.Metadata.GetEngine()),
+ GetDatabaseMetadataFunc: parsercontext.BuildGetDatabaseMetadataFunc(stores),
+ ListDatabaseNamesFunc: parsercontext.BuildListDatabaseNamesFunc(stores),
+ GetLinkedDatabaseMetadataFunc: parsercontext.BuildGetLinkedDatabaseMetadataFunc(stores, instance.Metadata.GetEngine()),
},
instance.Metadata.GetEngine(),
statements,
@@ -1305,9 +1306,9 @@ func exportSQLWithContext(
database.DatabaseName,
request.Statement,
instance,
- BuildGetDatabaseMetadataFunc(stores),
- BuildListDatabaseNamesFunc(stores),
- BuildGetLinkedDatabaseMetadataFunc(stores, instance.Metadata.GetEngine()),
+ parsercontext.BuildGetDatabaseMetadataFunc(stores),
+ parsercontext.BuildListDatabaseNamesFunc(stores),
+ parsercontext.BuildGetLinkedDatabaseMetadataFunc(stores, instance.Metadata.GetEngine()),
)
if err != nil {
return errors.Wrapf(err, "failed to extract resource list")
@@ -1439,119 +1440,10 @@ func (s *SQLService) SearchQueryHistories(ctx context.Context, req *connect.Requ
return connect.NewResponse(resp), nil
}
-func BuildGetLinkedDatabaseMetadataFunc(storeInstance *store.Store, engine storepb.Engine) parserbase.GetLinkedDatabaseMetadataFunc {
- if engine != storepb.Engine_ORACLE {
- return nil
- }
- return func(ctx context.Context, instanceID string, linkedDatabaseName string, schemaName string) (string, string, *model.DatabaseMetadata, error) {
- // Find the linked database metadata.
- databases, err := storeInstance.ListDatabases(ctx, &store.FindDatabaseMessage{
- Workspace: common.GetWorkspaceIDFromContext(ctx),
- InstanceID: &instanceID,
- })
- if err != nil {
- return "", "", nil, err
- }
- var linkedMeta *storepb.LinkedDatabaseMetadata
- for _, database := range databases {
- meta, err := storeInstance.GetDBSchema(ctx, &store.FindDBSchemaMessage{
- Workspace: common.GetWorkspaceIDFromContext(ctx),
- InstanceID: database.InstanceID,
- DatabaseName: database.DatabaseName,
- })
- if err != nil {
- return "", "", nil, err
- }
- if linkedMeta = meta.GetLinkedDatabase(linkedDatabaseName); linkedMeta != nil {
- break
- }
- }
- if linkedMeta == nil {
- return "", "", nil, nil
- }
- // Find the linked database in Bytebase.
- var linkedDatabase *store.DatabaseMessage
- databaseName := linkedMeta.GetUsername()
- if schemaName != "" {
- databaseName = schemaName
- }
- databaseList, err := storeInstance.ListDatabases(ctx, &store.FindDatabaseMessage{
- Workspace: common.GetWorkspaceIDFromContext(ctx),
- DatabaseName: &databaseName,
- Engine: &engine,
- })
- if err != nil {
- return "", "", nil, err
- }
- for _, database := range databaseList {
- instance, err := storeInstance.GetInstance(ctx, &store.FindInstanceMessage{Workspace: common.GetWorkspaceIDFromContext(ctx), ResourceID: &database.InstanceID})
- if err != nil {
- return "", "", nil, err
- }
- if instance != nil {
- for _, dataSource := range instance.Metadata.DataSources {
- if strings.Contains(linkedMeta.GetHost(), dataSource.GetHost()) {
- linkedDatabase = database
- break
- }
- }
- if linkedDatabase != nil {
- break
- }
- }
- }
- if linkedDatabase == nil {
- return "", "", nil, nil
- }
- // Get the linked database metadata.
- linkedDatabaseMetadata, err := storeInstance.GetDBSchema(ctx, &store.FindDBSchemaMessage{
- Workspace: common.GetWorkspaceIDFromContext(ctx),
- InstanceID: linkedDatabase.InstanceID,
- DatabaseName: linkedDatabase.DatabaseName,
- })
- if err != nil {
- return "", "", nil, err
- }
- if linkedDatabaseMetadata == nil {
- return "", "", nil, nil
- }
- return linkedDatabase.InstanceID, linkedDatabaseName, linkedDatabaseMetadata, nil
- }
-}
-
-func BuildGetDatabaseMetadataFunc(storeInstance *store.Store) parserbase.GetDatabaseMetadataFunc {
- return func(ctx context.Context, instanceID, databaseName string) (string, *model.DatabaseMetadata, error) {
- databaseMetadata, err := storeInstance.GetDBSchema(ctx, &store.FindDBSchemaMessage{
- Workspace: common.GetWorkspaceIDFromContext(ctx),
- InstanceID: instanceID,
- DatabaseName: databaseName,
- })
- if err != nil {
- return "", nil, err
- }
- if databaseMetadata == nil {
- return "", nil, nil
- }
- return databaseName, databaseMetadata, nil
- }
-}
-
-func BuildListDatabaseNamesFunc(storeInstance *store.Store) parserbase.ListDatabaseNamesFunc {
- return func(ctx context.Context, instanceID string) ([]string, error) {
- databases, err := storeInstance.ListDatabases(ctx, &store.FindDatabaseMessage{
- Workspace: common.GetWorkspaceIDFromContext(ctx),
- InstanceID: &instanceID,
- })
- if err != nil {
- return nil, err
- }
- names := make([]string, 0, len(databases))
- for _, database := range databases {
- names = append(names, database.DatabaseName)
- }
- return names, nil
- }
-}
+// The Build*Func parser-context helpers moved to
+// `backend/component/parsercontext` so the runner layer can use them
+// without an import cycle. See parsercontext.BuildGetDatabaseMetadataFunc,
+// BuildListDatabaseNamesFunc, BuildGetLinkedDatabaseMetadataFunc.
// accessCheck check the access for the database. Do not support cross-project resources.
func (s *SQLService) accessCheck(
diff --git a/backend/component/parsercontext/parsercontext.go b/backend/component/parsercontext/parsercontext.go
new file mode 100644
index 0000000000..1408a06348
--- /dev/null
+++ b/backend/component/parsercontext/parsercontext.go
@@ -0,0 +1,146 @@
+// Package parsercontext builds the GetDatabaseMetadataFunc /
+// ListDatabaseNamesFunc / GetLinkedDatabaseMetadataFunc closures the SQL
+// parsers need to resolve cross-database / linked-database references.
+//
+// Lives outside the api/v1 package so both the api layer (e.g. sql_service,
+// release_service_check) and the runner layer (approval rule evaluation,
+// taskrun export executor) can use these without creating an import cycle
+// — `api/v1` imports `runner/approval`, so `runner/approval` can't import
+// from `api/v1` in return.
+package parsercontext
+
+import (
+ "context"
+ "strings"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/store"
+ "github.com/bytebase/bytebase/backend/store/model"
+
+ "github.com/bytebase/bytebase/backend/common"
+ parserbase "github.com/bytebase/bytebase/backend/plugin/parser/base"
+)
+
+// BuildGetDatabaseMetadataFunc returns a closure that loads a database's
+// metadata (schema graph) by (instance, database) name. Used by the SQL
+// parser to resolve unqualified table references to their owning database.
+func BuildGetDatabaseMetadataFunc(storeInstance *store.Store) parserbase.GetDatabaseMetadataFunc {
+ return func(ctx context.Context, instanceID, databaseName string) (string, *model.DatabaseMetadata, error) {
+ databaseMetadata, err := storeInstance.GetDBSchema(ctx, &store.FindDBSchemaMessage{
+ Workspace: common.GetWorkspaceIDFromContext(ctx),
+ InstanceID: instanceID,
+ DatabaseName: databaseName,
+ })
+ if err != nil {
+ return "", nil, err
+ }
+ if databaseMetadata == nil {
+ return "", nil, nil
+ }
+ return databaseName, databaseMetadata, nil
+ }
+}
+
+// BuildListDatabaseNamesFunc returns a closure that lists all database
+// names on a given instance. Used by the parser when a statement references
+// a database by name without prior context.
+func BuildListDatabaseNamesFunc(storeInstance *store.Store) parserbase.ListDatabaseNamesFunc {
+ return func(ctx context.Context, instanceID string) ([]string, error) {
+ databases, err := storeInstance.ListDatabases(ctx, &store.FindDatabaseMessage{
+ Workspace: common.GetWorkspaceIDFromContext(ctx),
+ InstanceID: &instanceID,
+ })
+ if err != nil {
+ return nil, err
+ }
+ names := make([]string, 0, len(databases))
+ for _, database := range databases {
+ names = append(names, database.DatabaseName)
+ }
+ return names, nil
+ }
+}
+
+// BuildGetLinkedDatabaseMetadataFunc returns a closure that resolves Oracle
+// linked-database references (DBLINK syntax) to the actual remote database
+// metadata. Returns nil for non-Oracle engines — the parser interprets that
+// as "linked references not supported here".
+func BuildGetLinkedDatabaseMetadataFunc(storeInstance *store.Store, engine storepb.Engine) parserbase.GetLinkedDatabaseMetadataFunc {
+ if engine != storepb.Engine_ORACLE {
+ return nil
+ }
+ return func(ctx context.Context, instanceID string, linkedDatabaseName string, schemaName string) (string, string, *model.DatabaseMetadata, error) {
+ // Find the linked database metadata.
+ databases, err := storeInstance.ListDatabases(ctx, &store.FindDatabaseMessage{
+ Workspace: common.GetWorkspaceIDFromContext(ctx),
+ InstanceID: &instanceID,
+ })
+ if err != nil {
+ return "", "", nil, err
+ }
+ var linkedMeta *storepb.LinkedDatabaseMetadata
+ for _, database := range databases {
+ meta, err := storeInstance.GetDBSchema(ctx, &store.FindDBSchemaMessage{
+ Workspace: common.GetWorkspaceIDFromContext(ctx),
+ InstanceID: database.InstanceID,
+ DatabaseName: database.DatabaseName,
+ })
+ if err != nil {
+ return "", "", nil, err
+ }
+ if linkedMeta = meta.GetLinkedDatabase(linkedDatabaseName); linkedMeta != nil {
+ break
+ }
+ }
+ if linkedMeta == nil {
+ return "", "", nil, nil
+ }
+ // Find the linked database in Bytebase.
+ var linkedDatabase *store.DatabaseMessage
+ databaseName := linkedMeta.GetUsername()
+ if schemaName != "" {
+ databaseName = schemaName
+ }
+ databaseList, err := storeInstance.ListDatabases(ctx, &store.FindDatabaseMessage{
+ Workspace: common.GetWorkspaceIDFromContext(ctx),
+ DatabaseName: &databaseName,
+ Engine: &engine,
+ })
+ if err != nil {
+ return "", "", nil, err
+ }
+ for _, database := range databaseList {
+ instance, err := storeInstance.GetInstance(ctx, &store.FindInstanceMessage{Workspace: common.GetWorkspaceIDFromContext(ctx), ResourceID: &database.InstanceID})
+ if err != nil {
+ return "", "", nil, err
+ }
+ if instance != nil {
+ for _, dataSource := range instance.Metadata.DataSources {
+ if strings.Contains(linkedMeta.GetHost(), dataSource.GetHost()) {
+ linkedDatabase = database
+ break
+ }
+ }
+ if linkedDatabase != nil {
+ break
+ }
+ }
+ }
+ if linkedDatabase == nil {
+ return "", "", nil, nil
+ }
+ // Get the linked database metadata.
+ linkedDatabaseMetadata, err := storeInstance.GetDBSchema(ctx, &store.FindDBSchemaMessage{
+ Workspace: common.GetWorkspaceIDFromContext(ctx),
+ InstanceID: linkedDatabase.InstanceID,
+ DatabaseName: linkedDatabase.DatabaseName,
+ })
+ if err != nil {
+ return "", "", nil, err
+ }
+ if linkedDatabaseMetadata == nil {
+ return "", "", nil, nil
+ }
+ return linkedDatabase.InstanceID, linkedDatabaseName, linkedDatabaseMetadata, nil
+ }
+}
diff --git a/backend/generated-go/v1/setting_service.pb.go b/backend/generated-go/v1/setting_service.pb.go
index 7b6307741c..2c0e5a7b48 100644
--- a/backend/generated-go/v1/setting_service.pb.go
+++ b/backend/generated-go/v1/setting_service.pb.go
@@ -2514,7 +2514,7 @@ type WorkspaceApprovalSetting_Rule struct {
// When source is CREATE_DATABASE, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name
// When source is EXPORT_DATA, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name, resource.schema_name, resource.table_name
// When source is REQUEST_ROLE, support: resource.project_id, request.expiration_days, request.role
- // When source is REQUEST_ACCESS, support: resource.environment_id, resource.project_id, request.unmask, request.data_export
+ // When source is REQUEST_ACCESS, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name, resource.schema_name, resource.table_name, request.unmask, request.data_export
//
// For examples:
// resource.environment_id == "prod" && statement.affected_rows >= 100
diff --git a/backend/migrator/migration/3.19/0001##mirror_export_data_approval_to_request_access.sql b/backend/migrator/migration/3.19/0001##mirror_export_data_approval_to_request_access.sql
new file mode 100644
index 0000000000..ffd2bd8e93
--- /dev/null
+++ b/backend/migrator/migration/3.19/0001##mirror_export_data_approval_to_request_access.sql
@@ -0,0 +1,110 @@
+-- Backfill REQUEST_ACCESS approval rules from existing EXPORT_DATA rules.
+--
+-- Before this migration access-grant requests with `export = true` had no
+-- matching approval rule (the WORKSPACE_APPROVAL setting only covered the
+-- direct DATABASE_EXPORT issue path), so JIT-grant exports effectively
+-- skipped the workspace's export approval policy — a data-security gap.
+--
+-- For each EXPORT_DATA rule we copy the template into a sibling
+-- REQUEST_ACCESS rule and AND `request.data_export == true` onto the
+-- existing CEL expression. The original EXPORT_DATA rule stays in place.
+-- We also append a catch-all REQUEST_ACCESS rule keyed on just
+-- `request.data_export == true` so any export-capable access-grant
+-- request still triggers approval even if no specific rule matches.
+--
+-- New rules are appended at the END of the array — we deliberately do
+-- NOT reorder existing REQUEST_ACCESS rules (for role grants / unmask /
+-- etc.) ahead of our additions. If a workspace already has a broad
+-- REQUEST_ACCESS rule that intentionally gates every REQUEST_ACCESS
+-- request, reordering would override the admin's chosen evaluation
+-- order. The runner returns the first source-matching rule whose
+-- condition holds; admins can manually reorder in the UI after this
+-- backfill if they want export-specific gates to win over their
+-- existing broad rules.
+--
+-- Per-workspace: `setting` is keyed by `(workspace, name)` so each
+-- workspace gets its own computed rules instead of one workspace's rules
+-- being broadcast to all (PR #20501 bot review #3353501482).
+--
+-- This migration relies on REQUEST_ACCESS rule evaluation populating the
+-- same `resource.db_engine` / `resource.database_name` /
+-- `resource.schema_name` / `resource.table_name` CEL attributes that
+-- EXPORT_DATA rules use (see `buildCELVariablesForAccessGrant` in
+-- `backend/runner/approval/runner.go`) — so an EXPORT_DATA rule scoped
+-- to a specific database / engine / table mirrors verbatim and continues
+-- to gate the same exports under the JIT path (PR #20501 bot review
+-- #3353501487).
+--
+-- Idempotency: REQUEST_ACCESS is a long-established source used for role
+-- grants / unmask requests. Those existing rules must coexist. We only
+-- skip a workspace when at least one of its REQUEST_ACCESS rules already
+-- references `request.data_export == true` — meaning an admin (or a
+-- prior run) has already wired up the export gate.
+
+DO $$
+DECLARE
+ ws_row RECORD;
+ setting_value jsonb;
+ rules jsonb;
+ rule jsonb;
+ result_rules jsonb;
+ fallback_template jsonb;
+ existing_expression text;
+ combined_expression text;
+BEGIN
+ FOR ws_row IN
+ SELECT workspace, value FROM setting WHERE name = 'WORKSPACE_APPROVAL'
+ LOOP
+ setting_value := ws_row.value;
+ rules := COALESCE(setting_value->'rules', '[]'::jsonb);
+
+ IF EXISTS (
+ SELECT 1 FROM jsonb_array_elements(rules) r
+ WHERE r->>'source' = 'REQUEST_ACCESS'
+ AND COALESCE(r->'condition'->>'expression', '') LIKE '%request.data_export == true%'
+ ) THEN CONTINUE; END IF;
+
+ result_rules := rules;
+ fallback_template := NULL;
+
+ FOR rule IN
+ SELECT value
+ FROM jsonb_array_elements(rules) WITH ORDINALITY AS arr(value, idx)
+ WHERE value->>'source' = 'EXPORT_DATA'
+ ORDER BY arr.idx
+ LOOP
+ existing_expression := COALESCE(rule->'condition'->>'expression', '');
+
+ IF btrim(existing_expression) IN ('', 'true') THEN
+ -- Trivial EXPORT_DATA fallback — don't mirror (the explicit
+ -- fallback at the end already covers this), but adopt its
+ -- template as the canonical fallback template.
+ fallback_template := rule->'template';
+ CONTINUE;
+ END IF;
+
+ combined_expression := '(' || existing_expression || ') && request.data_export == true';
+ result_rules := result_rules || jsonb_build_array(jsonb_build_object(
+ 'template', rule->'template',
+ 'source', 'REQUEST_ACCESS',
+ 'condition', jsonb_build_object('expression', combined_expression)
+ ));
+
+ IF fallback_template IS NULL THEN
+ fallback_template := rule->'template';
+ END IF;
+ END LOOP;
+
+ IF fallback_template IS NOT NULL THEN
+ result_rules := result_rules || jsonb_build_array(jsonb_build_object(
+ 'template', fallback_template,
+ 'source', 'REQUEST_ACCESS',
+ 'condition', jsonb_build_object('expression', 'request.data_export == true')
+ ));
+ END IF;
+
+ UPDATE setting
+ SET value = jsonb_build_object('rules', result_rules)
+ WHERE workspace = ws_row.workspace AND name = 'WORKSPACE_APPROVAL';
+ END LOOP;
+END $$;
diff --git a/backend/migrator/migrator_test.go b/backend/migrator/migrator_test.go
index ad8f94551d..17941ea732 100644
--- a/backend/migrator/migrator_test.go
+++ b/backend/migrator/migrator_test.go
@@ -15,8 +15,8 @@ import (
func TestLatestVersion(t *testing.T) {
files, err := getSortedVersionedFiles()
require.NoError(t, err)
- require.Equal(t, semver.MustParse("3.19.0"), *files[len(files)-1].version)
- require.Equal(t, "migration/3.19/0000##vcs_provider_user.sql", files[len(files)-1].path)
+ require.Equal(t, semver.MustParse("3.19.1"), *files[len(files)-1].version)
+ require.Equal(t, "migration/3.19/0001##mirror_export_data_approval_to_request_access.sql", files[len(files)-1].path)
}
func TestVersionUnique(t *testing.T) {
diff --git a/backend/runner/approval/runner.go b/backend/runner/approval/runner.go
index e82998734c..2bf70661cc 100644
--- a/backend/runner/approval/runner.go
+++ b/backend/runner/approval/runner.go
@@ -15,10 +15,13 @@ import (
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/component/bus"
+ "github.com/bytebase/bytebase/backend/component/export"
+ "github.com/bytebase/bytebase/backend/component/parsercontext"
"github.com/bytebase/bytebase/backend/component/webhook"
"github.com/bytebase/bytebase/backend/enterprise"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
+ parserbase "github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store"
"github.com/bytebase/bytebase/backend/utils"
)
@@ -838,6 +841,19 @@ func buildCELVariablesForRoleGrant(ctx context.Context, stores *store.Store, iss
}
// buildCELVariablesForAccessGrant builds CEL variables for ACCESS_GRANT issues.
+//
+// Emits one CEL variable map per (target database, schema, table) referenced
+// by the grant's query — mirroring how EXPORT_DATA expands its rule
+// evaluation across the per-statement schema/table set. This lets
+// REQUEST_ACCESS rules use the same `resource.db_engine /
+// resource.database_name / resource.schema_name / resource.table_name`
+// attributes EXPORT_DATA rules already use, so a workspace's export
+// approval policy applies identically whether the export goes through
+// the direct DATABASE_EXPORT issue path or the JIT access-grant path.
+//
+// Statement parsing failures fall back to per-target vars without
+// schema/table info — rules keyed on db_engine / database_name / env
+// still evaluate.
func buildCELVariablesForAccessGrant(ctx context.Context, stores *store.Store, issue *store.IssueMessage) ([]map[string]any, bool, error) {
payload := issue.Payload
if payload.AccessGrantId == "" {
@@ -864,7 +880,9 @@ func buildCELVariablesForAccessGrant(ctx context.Context, stores *store.Store, i
return []map[string]any{baseVars}, true, nil
}
+ statement := accessGrant.Payload.Query
var celVarsList []map[string]any
+
for _, target := range targets {
instanceID, databaseName, err := common.GetInstanceDatabaseID(target)
if err != nil {
@@ -881,15 +899,67 @@ func buildCELVariablesForAccessGrant(ctx context.Context, stores *store.Store, i
if len(databases) == 0 {
continue
}
-
database := databases[0]
- celVars := maps.Clone(baseVars)
+
+ instance, err := stores.GetInstance(ctx, &store.FindInstanceMessage{ResourceID: &database.InstanceID})
+ if err != nil {
+ return nil, false, errors.Wrapf(err, "failed to get instance %q", database.InstanceID)
+ }
+ if instance == nil {
+ continue
+ }
+ engine := instance.Metadata.GetEngine()
+
+ targetVars := maps.Clone(baseVars)
+ targetVars[common.CELAttributeResourceInstanceID] = database.InstanceID
+ targetVars[common.CELAttributeResourceDatabaseName] = database.DatabaseName
+ targetVars[common.CELAttributeResourceDBEngine] = engine.String()
if database.EffectiveEnvironmentID != nil {
- celVars[common.CELAttributeResourceEnvironmentID] = *database.EffectiveEnvironmentID
+ targetVars[common.CELAttributeResourceEnvironmentID] = *database.EffectiveEnvironmentID
} else {
- celVars[common.CELAttributeResourceEnvironmentID] = ""
+ targetVars[common.CELAttributeResourceEnvironmentID] = ""
+ }
+
+ // Extract referenced schemas/tables from the grant's query. On
+ // failure (parser error, unsupported engine, empty query) we
+ // emit one vars map without schema/table — rules keyed on
+ // db_engine / database_name / env still get to evaluate.
+ var resources []parserbase.SchemaResource
+ if statement != "" {
+ resources, err = export.GetResources(
+ ctx,
+ stores,
+ engine,
+ database.DatabaseName,
+ statement,
+ instance,
+ parsercontext.BuildGetDatabaseMetadataFunc(stores),
+ parsercontext.BuildListDatabaseNamesFunc(stores),
+ parsercontext.BuildGetLinkedDatabaseMetadataFunc(stores, engine),
+ )
+ if err != nil {
+ slog.Debug("failed to extract resources from access grant query; emitting target vars without schema/table",
+ slog.String("target", target), slog.String("instance", database.InstanceID),
+ log.BBError(err))
+ resources = nil
+ }
+ }
+
+ if len(resources) == 0 {
+ celVarsList = append(celVarsList, targetVars)
+ continue
+ }
+
+ for _, r := range resources {
+ v := maps.Clone(targetVars)
+ if r.Schema != "" {
+ v[common.CELAttributeResourceSchemaName] = r.Schema
+ }
+ if r.Table != "" {
+ v[common.CELAttributeResourceTableName] = r.Table
+ }
+ celVarsList = append(celVarsList, v)
}
- celVarsList = append(celVarsList, celVars)
}
if len(celVarsList) == 0 {
diff --git a/backend/runner/taskrun/data_export_executor.go b/backend/runner/taskrun/data_export_executor.go
index 5f5f2119f3..93fab214f4 100644
--- a/backend/runner/taskrun/data_export_executor.go
+++ b/backend/runner/taskrun/data_export_executor.go
@@ -15,11 +15,11 @@ import (
"github.com/pkg/errors"
"google.golang.org/protobuf/types/known/durationpb"
- apiv1 "github.com/bytebase/bytebase/backend/api/v1"
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/component/dbfactory"
"github.com/bytebase/bytebase/backend/component/export"
+ "github.com/bytebase/bytebase/backend/component/parsercontext"
"github.com/bytebase/bytebase/backend/enterprise"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
@@ -353,9 +353,9 @@ func (exec *DataExportExecutor) exportSQLWithContext(
database.DatabaseName,
result.Statement,
instance,
- apiv1.BuildGetDatabaseMetadataFunc(exec.store),
- apiv1.BuildListDatabaseNamesFunc(exec.store),
- apiv1.BuildGetLinkedDatabaseMetadataFunc(exec.store, instance.Metadata.GetEngine()),
+ parsercontext.BuildGetDatabaseMetadataFunc(exec.store),
+ parsercontext.BuildListDatabaseNamesFunc(exec.store),
+ parsercontext.BuildGetLinkedDatabaseMetadataFunc(exec.store, instance.Metadata.GetEngine()),
)
if err != nil {
return errors.Wrapf(err, "failed to extract resource list")
diff --git a/frontend/src/react/components/CustomApproval/utils.ts b/frontend/src/react/components/CustomApproval/utils.ts
index 96799ce811..939d73b4c1 100644
--- a/frontend/src/react/components/CustomApproval/utils.ts
+++ b/frontend/src/react/components/CustomApproval/utils.ts
@@ -147,9 +147,14 @@ export const ApprovalSourceFactorMap: Map<
],
[
WorkspaceApprovalSetting_Rule_Source.REQUEST_ACCESS,
+ // Same resource scope as EXPORT_DATA — `buildCELVariablesForAccessGrant`
+ // now populates db_engine / database_name / schema_name / table_name
+ // for ACCESS_GRANT issues (via export.GetResources on the grant's
+ // query), so REQUEST_ACCESS rules can use the same per-target
+ // attributes EXPORT_DATA rules already use.
[
- CEL_ATTRIBUTE_RESOURCE_ENVIRONMENT_ID,
- CEL_ATTRIBUTE_RESOURCE_PROJECT_ID,
+ ...commonFactorList,
+ ...schemaObjectNameFactorList,
CEL_ATTRIBUTE_REQUEST_UNMASK,
CEL_ATTRIBUTE_REQUEST_EXPORT,
],
diff --git a/frontend/src/types/proto-es/v1/setting_service_pb.d.ts b/frontend/src/types/proto-es/v1/setting_service_pb.d.ts
index 9f44a30f5c..8ac29b515e 100644
--- a/frontend/src/types/proto-es/v1/setting_service_pb.d.ts
+++ b/frontend/src/types/proto-es/v1/setting_service_pb.d.ts
@@ -820,7 +820,7 @@ export declare type WorkspaceApprovalSetting_Rule = Message<"bytebase.v1.Workspa
* When source is CREATE_DATABASE, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name
* When source is EXPORT_DATA, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name, resource.schema_name, resource.table_name
* When source is REQUEST_ROLE, support: resource.project_id, request.expiration_days, request.role
- * When source is REQUEST_ACCESS, support: resource.environment_id, resource.project_id, request.unmask, request.data_export
+ * When source is REQUEST_ACCESS, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name, resource.schema_name, resource.table_name, request.unmask, request.data_export
*
* For examples:
* resource.environment_id == "prod" && statement.affected_rows >= 100
diff --git a/proto/gen/grpc-doc/openapi.yaml b/proto/gen/grpc-doc/openapi.yaml
index dadc614581..208bb27280 100644
--- a/proto/gen/grpc-doc/openapi.yaml
+++ b/proto/gen/grpc-doc/openapi.yaml
@@ -29962,7 +29962,7 @@ components:
When source is CREATE_DATABASE, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name
When source is EXPORT_DATA, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name, resource.schema_name, resource.table_name
When source is REQUEST_ROLE, support: resource.project_id, request.expiration_days, request.role
- When source is REQUEST_ACCESS, support: resource.environment_id, resource.project_id, request.unmask, request.data_export
+ When source is REQUEST_ACCESS, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name, resource.schema_name, resource.table_name, request.unmask, request.data_export
For examples:
resource.environment_id == "prod" && statement.affected_rows >= 100
diff --git a/proto/gen/grpc-doc/v1/README.md b/proto/gen/grpc-doc/v1/README.md
index c10819a442..1781fa8b41 100644
--- a/proto/gen/grpc-doc/v1/README.md
+++ b/proto/gen/grpc-doc/v1/README.md
@@ -6586,7 +6586,7 @@ The `source` field filters which rules apply. The `condition` field then evaluat
All supported variables: statement.affected_rows: affected row count in the DDL/DML, support &#34;==&#34;, &#34;!=&#34;, &#34;&lt;&#34;, &#34;&lt;=&#34;, &#34;&gt;&#34;, &#34;&gt;=&#34; operations. statement.table_rows: table row count number, support &#34;==&#34;, &#34;!=&#34;, &#34;&lt;&#34;, &#34;&lt;=&#34;, &#34;&gt;&#34;, &#34;&gt;=&#34; operations. resource.environment_id: the environment resource id, support &#34;==&#34;, &#34;!=&#34;, &#34;in [xx]&#34;, &#34;!(in [xx])&#34; operations. resource.project_id: the project resource id, support &#34;==&#34;, &#34;!=&#34;, &#34;in [xx]&#34;, &#34;!(in [xx])&#34;, &#34;contains()&#34;, &#34;matches()&#34;, &#34;startsWith()&#34;, &#34;endsWith()&#34; operations. resource.db_engine: the database engine type, support &#34;==&#34;, &#34;!=&#34;, &#34;in [xx]&#34;, &#34;!(in [xx])&#34; operations. Check the Engine enum for values. statement.sql_type: the SQL type, support &#34;==&#34;, &#34;!=&#34;, &#34;in [xx]&#34;, &#34;!(in [xx])&#34; operations. resource.database_name: the database name, support &#34;==&#34;, &#34;!=&#34;, &#34;in [xx]&#34;, &#34;!(in [xx])&#34;, &#34;contains()&#34;, &#34;matches()&#34;, &#34;startsWith()&#34;, &#34;endsWith()&#34; operations. resource.schema_name: the schema name, support &#34;==&#34;, &#34;!=&#34;, &#34;in [xx]&#34;, &#34;!(in [xx])&#34;, &#34;contains()&#34;, &#34;matches()&#34;, &#34;startsWith()&#34;, &#34;endsWith()&#34; operations. resource.table_name: the table name, support &#34;==&#34;, &#34;!=&#34;, &#34;in [xx]&#34;, &#34;!(in [xx])&#34;, &#34;contains()&#34;, &#34;matches()&#34;, &#34;startsWith()&#34;, &#34;endsWith()&#34; operations. statement.text: the SQL statement, support &#34;contains()&#34;, &#34;matches()&#34;, &#34;startsWith()&#34;, &#34;endsWith()&#34; operations. request.expiration_days: the role expiration days for the request, support &#34;==&#34;, &#34;!=&#34;, &#34;&lt;&#34;, &#34;&lt;=&#34;, &#34;&gt;&#34;, &#34;&gt;=&#34; operations. request.role: the request role full name, support &#34;==&#34;, &#34;!=&#34;, &#34;in [xx]&#34;, &#34;!(in [xx])&#34;, &#34;contains()&#34;, &#34;matches()&#34;, &#34;startsWith()&#34;, &#34;endsWith()&#34; operations.
-When source is CHANGE_DATABASE, support: statement.*, resource.* (excluding request.*) When source is CREATE_DATABASE, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name When source is EXPORT_DATA, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name, resource.schema_name, resource.table_name When source is REQUEST_ROLE, support: resource.project_id, request.expiration_days, request.role When source is REQUEST_ACCESS, support: resource.environment_id, resource.project_id, request.unmask, request.data_export
+When source is CHANGE_DATABASE, support: statement.*, resource.* (excluding request.*) When source is CREATE_DATABASE, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name When source is EXPORT_DATA, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name, resource.schema_name, resource.table_name When source is REQUEST_ROLE, support: resource.project_id, request.expiration_days, request.role When source is REQUEST_ACCESS, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name, resource.schema_name, resource.table_name, request.unmask, request.data_export
For examples: resource.environment_id == &#34;prod&#34; &amp;&amp; statement.affected_rows &gt;= 100 resource.table_name.matches(&#34;sensitive_.*&#34;) &amp;&amp; resource.db_engine == &#34;MYSQL&#34; |
| source | [WorkspaceApprovalSetting.Rule.Source](#bytebase-v1-WorkspaceApprovalSetting-Rule-Source) | | |
diff --git a/proto/gen/grpc-doc/v1/index.html b/proto/gen/grpc-doc/v1/index.html
index 46a19785b1..6b8ac9d5e3 100644
--- a/proto/gen/grpc-doc/v1/index.html
+++ b/proto/gen/grpc-doc/v1/index.html
@@ -19102,7 +19102,7 @@ When source is CHANGE_DATABASE, support: statement.*, resource.* (excluding requ
When source is CREATE_DATABASE, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name
When source is EXPORT_DATA, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name, resource.schema_name, resource.table_name
When source is REQUEST_ROLE, support: resource.project_id, request.expiration_days, request.role
-When source is REQUEST_ACCESS, support: resource.environment_id, resource.project_id, request.unmask, request.data_export
+When source is REQUEST_ACCESS, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name, resource.schema_name, resource.table_name, request.unmask, request.data_export
For examples:
resource.environment_id == &#34;prod&#34; &amp;&amp; statement.affected_rows &gt;= 100
diff --git a/proto/v1/v1/setting_service.proto b/proto/v1/v1/setting_service.proto
index 84d564d6ce..98cea10be4 100644
--- a/proto/v1/v1/setting_service.proto
+++ b/proto/v1/v1/setting_service.proto
@@ -315,7 +315,7 @@ message WorkspaceApprovalSetting {
// When source is CREATE_DATABASE, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name
// When source is EXPORT_DATA, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name, resource.schema_name, resource.table_name
// When source is REQUEST_ROLE, support: resource.project_id, request.expiration_days, request.role
- // When source is REQUEST_ACCESS, support: resource.environment_id, resource.project_id, request.unmask, request.data_export
+ // When source is REQUEST_ACCESS, support: resource.environment_id, resource.project_id, resource.db_engine, resource.database_name, resource.schema_name, resource.table_name, request.unmask, request.data_export
//
// For examples:
// resource.environment_id == "prod" && statement.affected_rows >= 100