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(policy): move the service test to api/v1 and collide the store keys
rebelicebdebba860494
diff --git a/backend/api/v1/org_policy_service_review_rule_test.go b/backend/api/v1/org_policy_service_review_rule_test.goindex a94335ee57..227a2c7966 100644--- a/backend/api/v1/org_policy_service_review_rule_test.go+++ b/backend/api/v1/org_policy_service_review_rule_test.go@@ -3,9 +3,13 @@ package v1import ("testing"+ "connectrpc.com/connect""github.com/stretchr/testify/require""google.golang.org/protobuf/encoding/protojson"+ "google.golang.org/protobuf/types/known/fieldmaskpb"+ "github.com/bytebase/bytebase/backend/common"+ "github.com/bytebase/bytebase/backend/component/iam"storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1""github.com/bytebase/bytebase/backend/store"@@ -121,3 +125,127 @@ func TestDefaultReviewRulePolicy(t *testing.T) {require.NotContains(t, got, v1pb.ReviewRuleType_REVIEW_RULE_TYPE_UNSPECIFIED)require.Equal(t, len(store.GetDefaultReviewRulePolicy().Rules), len(got))}++// TestReviewRulePolicyService walks the REVIEW_RULE policy through+// OrgPolicyService at both levels against PostgreSQL: the all-rules default+// on a missing row, create, get, list, update, validation, delete, and the+// nearest-wins resolution the store exposes to the executor.+func TestReviewRulePolicyService(t *testing.T) {+ t.Parallel()+ ctx := issueServiceTestContext()+ stores := setupIssueServiceTestStore(ctx, t)+ iamManager, err := iam.NewManager(stores, nil, false)+ require.NoError(t, err)+ service := NewOrgPolicyService(stores, nil, iamManager)++ const (+ workspaceID = "default"+ projectID = "project-a"+ )+ projectPolicyName := common.FormatProject(projectID) + "/policies/review_rule"+ workspacePolicyName := common.FormatWorkspace(workspaceID) + "/policies/review_rule"+ everyRule := len(v1pb.ReviewRuleType_name) - 1++ rules := func(policy *v1pb.Policy) []v1pb.ReviewRuleType {+ return policy.GetReviewRulePolicy().GetRules()+ }+ reviewRule := func(list ...v1pb.ReviewRuleType) *v1pb.Policy {+ return &v1pb.Policy{+ Type: v1pb.PolicyType_REVIEW_RULE,+ Policy: &v1pb.Policy_ReviewRulePolicy{ReviewRulePolicy: &v1pb.ReviewRulePolicy{Rules: list}},+ }+ }+ get := func(name string) *v1pb.Policy {+ t.Helper()+ resp, err := service.GetPolicy(ctx, connect.NewRequest(&v1pb.GetPolicyRequest{Name: name}))+ require.NoError(t, err)+ return resp.Msg+ }+ create := func(parent string, policy *v1pb.Policy) (*v1pb.Policy, error) {+ resp, err := service.CreatePolicy(ctx, connect.NewRequest(&v1pb.CreatePolicyRequest{Parent: parent, Policy: policy}))+ if err != nil {+ return nil, err+ }+ return resp.Msg, nil+ }+ update := func(name string, policy *v1pb.Policy) (*v1pb.Policy, error) {+ policy.Name = name+ resp, err := service.UpdatePolicy(ctx, connect.NewRequest(&v1pb.UpdatePolicyRequest{+ Policy: policy,+ UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"review_rule_policy"}},+ }))+ if err != nil {+ return nil, err+ }+ return resp.Msg, nil+ }+ effective := func() []string {+ t.Helper()+ p, err := stores.GetEffectiveReviewRulePolicy(ctx, workspaceID, projectID)+ require.NoError(t, err)+ names := make([]string, 0, len(p.Rules))+ for _, r := range p.Rules {+ names = append(names, r.String())+ }+ return names+ }++ // No row at either level: every rule, not NotFound.+ got := get(projectPolicyName)+ require.Equal(t, projectPolicyName, got.Name)+ require.Equal(t, v1pb.PolicyType_REVIEW_RULE, got.Type)+ require.Equal(t, v1pb.PolicyResourceType_PROJECT, got.ResourceType)+ require.Len(t, rules(got), everyRule)+ got = get(workspacePolicyName)+ require.Equal(t, v1pb.PolicyResourceType_WORKSPACE, got.ResourceType)+ require.Len(t, rules(got), everyRule)+ require.Len(t, effective(), everyRule)++ // Create on the project and read it back.+ created, err := create(common.FormatProject(projectID), reviewRule(v1pb.ReviewRuleType_SYNTAX, v1pb.ReviewRuleType_DISALLOW_DROP_OBJECT))+ require.NoError(t, err)+ require.Equal(t, projectPolicyName, created.Name)+ require.Equal(t, []v1pb.ReviewRuleType{v1pb.ReviewRuleType_SYNTAX, v1pb.ReviewRuleType_DISALLOW_DROP_OBJECT}, rules(created))+ require.Equal(t, rules(created), rules(get(projectPolicyName)))+ require.Equal(t, []string{"SYNTAX", "DISALLOW_DROP_OBJECT"}, effective())++ // List with the type filter finds it.+ listed, err := service.ListPolicies(ctx, connect.NewRequest(&v1pb.ListPoliciesRequest{+ Parent: common.FormatProject(projectID),+ PolicyType: v1pb.PolicyType_REVIEW_RULE.Enum(),+ }))+ require.NoError(t, err)+ require.Len(t, listed.Msg.Policies, 1)+ require.Equal(t, projectPolicyName, listed.Msg.Policies[0].Name)++ // Update through the review_rule_policy mask path.+ updated, err := update(projectPolicyName, reviewRule(v1pb.ReviewRuleType_REQUIRE_WHERE))+ require.NoError(t, err)+ require.Equal(t, []v1pb.ReviewRuleType{v1pb.ReviewRuleType_REQUIRE_WHERE}, rules(updated))+ require.Equal(t, []string{"REQUIRE_WHERE"}, effective())++ // Unspecified and repeated rules are refused.+ for _, bad := range [][]v1pb.ReviewRuleType{+ {v1pb.ReviewRuleType_REVIEW_RULE_TYPE_UNSPECIFIED},+ {v1pb.ReviewRuleType_SYNTAX, v1pb.ReviewRuleType_SYNTAX},+ } {+ _, err = update(projectPolicyName, reviewRule(bad...))+ require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))+ }++ // The policy does not attach to an environment.+ _, err = create("environments/prod", reviewRule(v1pb.ReviewRuleType_SYNTAX))+ require.Equal(t, connect.CodeInvalidArgument, connect.CodeOf(err))++ // Workspace level, and nearest wins: the project's policy while it has+ // one, the workspace's after the project's is deleted.+ created, err = create(common.FormatWorkspace(workspaceID), reviewRule(v1pb.ReviewRuleType_DISALLOW_TRUNCATE))+ require.NoError(t, err)+ require.Equal(t, workspacePolicyName, created.Name)+ require.Equal(t, []string{"REQUIRE_WHERE"}, effective())++ _, err = service.DeletePolicy(ctx, connect.NewRequest(&v1pb.DeletePolicyRequest{Name: projectPolicyName}))+ require.NoError(t, err)+ require.Equal(t, []string{"DISALLOW_TRUNCATE"}, effective())+ require.Len(t, rules(get(projectPolicyName)), everyRule)+}diff --git a/backend/store/policy_review_rule_test.go b/backend/store/policy_review_rule_test.goindex fac824efaa..441b0b919e 100644--- a/backend/store/policy_review_rule_test.go+++ b/backend/store/policy_review_rule_test.go@@ -76,17 +76,28 @@ func TestGetEffectiveReviewRulePolicy(t *testing.T) {upsert(storepb.Policy_WORKSPACE, common.FormatWorkspace("ws"), false, storepb.ReviewRuleType_SYNTAX)require.Equal(t, every, effective())- // Another workspace's policy never leaks in.+ // The same resource under another workspace is a colliding local key: the+ // policy primary key is (workspace, resource_type, resource, type). Each+ // workspace resolves to its own row and neither sees the other's._, err = db.ExecContext(ctx, `INSERT INTO workspace (resource_id) VALUES ('other');`)require.NoError(t, err)_, err = stores.CreatePolicy(ctx, &store.PolicyMessage{Workspace: "other",- ResourceType: storepb.Policy_WORKSPACE,- Resource: common.FormatWorkspace("other"),+ ResourceType: storepb.Policy_PROJECT,+ Resource: common.FormatProject("p"),Type: storepb.Policy_REVIEW_RULE,- Payload: payload(storepb.ReviewRuleType_SYNTAX),+ Payload: payload(storepb.ReviewRuleType_REQUIRE_IS_NULL),Enforce: true,})require.NoError(t, err)+ otherEffective, err := stores.GetEffectiveReviewRulePolicy(ctx, "other", "p")+ require.NoError(t, err)+ require.Equal(t, []storepb.ReviewRuleType{storepb.ReviewRuleType_REQUIRE_IS_NULL}, otherEffective.Rules)require.Equal(t, every, effective())++ upsert(storepb.Policy_PROJECT, common.FormatProject("p"), true, storepb.ReviewRuleType_DISALLOW_RENAME)+ require.Equal(t, []storepb.ReviewRuleType{storepb.ReviewRuleType_DISALLOW_RENAME}, effective())+ otherEffective, err = stores.GetEffectiveReviewRulePolicy(ctx, "other", "p")+ require.NoError(t, err)+ require.Equal(t, []storepb.ReviewRuleType{storepb.ReviewRuleType_REQUIRE_IS_NULL}, otherEffective.Rules)}diff --git a/backend/tests/review_rule_policy_test.go b/backend/tests/review_rule_policy_test.godeleted file mode 100644index 3a65eada74..0000000000--- a/backend/tests/review_rule_policy_test.go+++ /dev/null@@ -1,132 +0,0 @@-package tests--import (- "context"- "testing"-- "connectrpc.com/connect"- "github.com/stretchr/testify/require"- "google.golang.org/protobuf/types/known/fieldmaskpb"-- "github.com/bytebase/bytebase/backend/common"- v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"-)--// TestReviewRulePolicy walks the REVIEW_RULE policy through the org policy-// service at both levels: the all-rules default on a missing row, create,-// get, list, update, validation, and delete.-func TestReviewRulePolicy(t *testing.T) {- t.Parallel()- a := require.New(t)- ctx := context.Background()- ctl, ctx := startWorkspace(ctx, t)-- projectPolicyName := ctl.project.Name + "/policies/review_rule"- rules := func(policy *v1pb.Policy) []v1pb.ReviewRuleType {- return policy.GetReviewRulePolicy().GetRules()- }- reviewRule := func(list ...v1pb.ReviewRuleType) *v1pb.Policy {- return &v1pb.Policy{- Type: v1pb.PolicyType_REVIEW_RULE,- Policy: &v1pb.Policy_ReviewRulePolicy{ReviewRulePolicy: &v1pb.ReviewRulePolicy{Rules: list}},- }- }-- // No row: the default is every rule, not NotFound.- got, err := ctl.orgPolicyServiceClient.GetPolicy(ctx, connect.NewRequest(&v1pb.GetPolicyRequest{Name: projectPolicyName}))- a.NoError(err)- a.Equal(projectPolicyName, got.Msg.Name)- a.Equal(v1pb.PolicyType_REVIEW_RULE, got.Msg.Type)- a.Equal(v1pb.PolicyResourceType_PROJECT, got.Msg.ResourceType)- a.Len(rules(got.Msg), len(v1pb.ReviewRuleType_name)-1)-- // Create on the project.- created, err := ctl.orgPolicyServiceClient.CreatePolicy(ctx, connect.NewRequest(&v1pb.CreatePolicyRequest{- Parent: ctl.project.Name,- Policy: reviewRule(v1pb.ReviewRuleType_SYNTAX, v1pb.ReviewRuleType_DISALLOW_DROP_OBJECT),- }))- a.NoError(err)- a.Equal(projectPolicyName, created.Msg.Name)- a.Equal([]v1pb.ReviewRuleType{v1pb.ReviewRuleType_SYNTAX, v1pb.ReviewRuleType_DISALLOW_DROP_OBJECT}, rules(created.Msg))-- got, err = ctl.orgPolicyServiceClient.GetPolicy(ctx, connect.NewRequest(&v1pb.GetPolicyRequest{Name: projectPolicyName}))- a.NoError(err)- a.Equal(rules(created.Msg), rules(got.Msg))-- // List with the type filter finds it.- listed, err := ctl.orgPolicyServiceClient.ListPolicies(ctx, connect.NewRequest(&v1pb.ListPoliciesRequest{- Parent: ctl.project.Name,- PolicyType: v1pb.PolicyType_REVIEW_RULE.Enum(),- }))- a.NoError(err)- a.Len(listed.Msg.Policies, 1)- a.Equal(projectPolicyName, listed.Msg.Policies[0].Name)-- // Update through the review_rule_policy mask path.- updatedPolicy := reviewRule(v1pb.ReviewRuleType_REQUIRE_WHERE)- updatedPolicy.Name = projectPolicyName- updated, err := ctl.orgPolicyServiceClient.UpdatePolicy(ctx, connect.NewRequest(&v1pb.UpdatePolicyRequest{- Policy: updatedPolicy,- UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"review_rule_policy"}},- }))- a.NoError(err)- a.Equal([]v1pb.ReviewRuleType{v1pb.ReviewRuleType_REQUIRE_WHERE}, rules(updated.Msg))-- // Validation: unspecified and repeated rules are refused on update and create.- for _, bad := range [][]v1pb.ReviewRuleType{- {v1pb.ReviewRuleType_REVIEW_RULE_TYPE_UNSPECIFIED},- {v1pb.ReviewRuleType_SYNTAX, v1pb.ReviewRuleType_SYNTAX},- } {- badPolicy := reviewRule(bad...)- badPolicy.Name = projectPolicyName- _, err = ctl.orgPolicyServiceClient.UpdatePolicy(ctx, connect.NewRequest(&v1pb.UpdatePolicyRequest{- Policy: badPolicy,- UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"review_rule_policy"}},- }))- a.Equal(connect.CodeInvalidArgument, connect.CodeOf(err))- }-- // The policy does not attach to an environment.- _, err = ctl.orgPolicyServiceClient.CreatePolicy(ctx, connect.NewRequest(&v1pb.CreatePolicyRequest{- Parent: "environments/prod",- Policy: reviewRule(v1pb.ReviewRuleType_SYNTAX),- }))- a.Equal(connect.CodeInvalidArgument, connect.CodeOf(err))-- // Workspace level.- stores := getStore(t, ctl.server)- workspaceID, err := stores.GetWorkspaceID(ctx)- a.NoError(err)- workspacePolicyName := common.FormatWorkspace(workspaceID) + "/policies/review_rule"- got, err = ctl.orgPolicyServiceClient.GetPolicy(ctx, connect.NewRequest(&v1pb.GetPolicyRequest{Name: workspacePolicyName}))- a.NoError(err)- a.Equal(v1pb.PolicyResourceType_WORKSPACE, got.Msg.ResourceType)- a.Len(rules(got.Msg), len(v1pb.ReviewRuleType_name)-1)- created, err = ctl.orgPolicyServiceClient.CreatePolicy(ctx, connect.NewRequest(&v1pb.CreatePolicyRequest{- Parent: common.FormatWorkspace(workspaceID),- Policy: reviewRule(v1pb.ReviewRuleType_DISALLOW_TRUNCATE),- }))- a.NoError(err)- a.Equal(workspacePolicyName, created.Msg.Name)-- // The effective set is the nearest policy: the project's while it has one,- // the workspace's after the project's is deleted.- projectID, err := common.GetProjectID(ctl.project.Name)- a.NoError(err)- effective, err := stores.GetEffectiveReviewRulePolicy(ctx, workspaceID, projectID)- a.NoError(err)- a.Equal("REQUIRE_WHERE", effective.Rules[0].String())- a.Len(effective.Rules, 1)-- _, err = ctl.orgPolicyServiceClient.DeletePolicy(ctx, connect.NewRequest(&v1pb.DeletePolicyRequest{Name: projectPolicyName}))- a.NoError(err)- effective, err = stores.GetEffectiveReviewRulePolicy(ctx, workspaceID, projectID)- a.NoError(err)- a.Equal("DISALLOW_TRUNCATE", effective.Rules[0].String())- a.Len(effective.Rules, 1)-- // Deleted: back to the default.- got, err = ctl.orgPolicyServiceClient.GetPolicy(ctx, connect.NewRequest(&v1pb.GetPolicyRequest{Name: projectPolicyName}))- a.NoError(err)- a.Len(rules(got.Msg), len(v1pb.ReviewRuleType_name)-1)-}