Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(advanced-masking-policy): curd for masking rule policy (#7774)
h3n4leb375f242fec
diff --git a/backend/api/v1/org_policy_service.go b/backend/api/v1/org_policy_service.goindex d7add4302f..1e13799e87 100644--- a/backend/api/v1/org_policy_service.go+++ b/backend/api/v1/org_policy_service.go@@ -8,6 +8,7 @@ import ("github.com/google/cel-go/cel""github.com/pkg/errors""go.uber.org/zap"+ expr "google.golang.org/genproto/googleapis/type/expr""google.golang.org/grpc/codes""google.golang.org/grpc/status""google.golang.org/protobuf/encoding/protojson"@@ -151,6 +152,9 @@ func (s *OrgPolicyService) UpdatePolicy(ctx context.Context, request *v1pb.Updatcase "inherit_from_parent":patch.InheritFromParent = &request.Policy.InheritFromParentcase "payload":+ if err := validatePolicyPayload(policy.Type, request.Policy); err != nil {+ return nil, status.Errorf(codes.InvalidArgument, "invalid policy: %v", err)+ }payloadStr, err := s.convertPolicyPayloadToString(request.Policy)if err != nil {return nil, err@@ -387,6 +391,10 @@ func (s *OrgPolicyService) createPolicyMessage(ctx context.Context, creatorID inreturn nil, err}+ if err := validatePolicyPayload(policyType, policy); err != nil {+ return nil, status.Errorf(codes.InvalidArgument, "invalid policy: %v", err)+ }+payloadStr, err := s.convertPolicyPayloadToString(policy)if err != nil {return nil, err@@ -461,6 +469,33 @@ func validatePolicyType(policyType api.PolicyType, policyResourceType api.Policyreturn status.Errorf(codes.InvalidArgument, "policy %v is not allowed in resource %v", policyType, policyResourceType)}+func validatePolicyPayload(policyType api.PolicyType, policy *v1pb.Policy) error {+ //nolint+ switch policyType {+ case api.PolicyTypeMaskingRule:+ maskingRulePolicy, ok := policy.Policy.(*v1pb.Policy_MaskingRulePolicy)+ if !ok {+ return status.Errorf(codes.InvalidArgument, "unmatched policy type %v and policy %v", policyType, policy.Policy)+ }+ if maskingRulePolicy.MaskingRulePolicy == nil {+ return status.Errorf(codes.InvalidArgument, "masking rule policy must be set")+ }+ for _, rule := range maskingRulePolicy.MaskingRulePolicy.Rules {+ if rule.Id == "" {+ return status.Errorf(codes.InvalidArgument, "masking rule must have ID set")+ }+ if rule.MaskingLevel == v1pb.MaskingLevel_MASKING_LEVEL_UNSPECIFIED {+ return status.Errorf(codes.InvalidArgument, "masking rule must have masking level set")+ }+ if _, err := common.ValidateMaskingRuleCELExpr(rule.Condition.Expression); err != nil {+ return status.Errorf(codes.InvalidArgument, "invalid masking rule expression: %v", err)+ }+ }+ default:+ }+ return nil+}+func (s *OrgPolicyService) convertPolicyPayloadToString(policy *v1pb.Policy) (string, error) {switch policy.Type {case v1pb.PolicyType_WORKSPACE_IAM:@@ -562,6 +597,20 @@ func (s *OrgPolicyService) convertPolicyPayloadToString(policy *v1pb.Policy) (streturn "", status.Errorf(codes.InvalidArgument, err.Error())}return payload.String()+ case v1pb.PolicyType_MASKING_RULE:+ if err := s.licenseService.IsFeatureEnabled(api.FeatureSensitiveData); err != nil {+ return "", status.Errorf(codes.PermissionDenied, err.Error())+ }+ payload, err := convertToStorePBMskingRulePolicy(policy.GetMaskingRulePolicy())+ if err != nil {+ return "", status.Errorf(codes.InvalidArgument, err.Error())+ }+ payloadBytes, err := protojson.Marshal(payload)+ if err != nil {+ return "", errors.Wrap(err, "failed to marshal masking rule policy")+ }++ return string(payloadBytes), nil}return "", status.Errorf(codes.InvalidArgument, "invalid policy %v", policy.Type)@@ -645,6 +694,19 @@ func convertToPolicy(parentPath string, policyMessage *store.PolicyMessage) (*v1return nil, err}policy.Policy = payload+ case api.PolicyTypeMaskingRule:+ pType = v1pb.PolicyType_MASKING_RULE+ maskingRulePolicy := &storepb.MaskingRulePolicy{}+ if err := protojson.Unmarshal([]byte(policyMessage.Payload), maskingRulePolicy); err != nil {+ return nil, errors.Wrap(err, "failed to unmarshal masking rule policy")+ }+ payload, err := convertToV1PBMaskingRulePolicy(maskingRulePolicy)+ if err != nil {+ return nil, errors.Wrap(err, "failed to convert masking rule policy")+ }+ policy.Policy = &v1pb.Policy_MaskingRulePolicy{+ MaskingRulePolicy: payload,+ }}policy.Type = pType@@ -793,21 +855,6 @@ func convertToV1PBSensitiveDataPolicy(payloadStr string) (*v1pb.Policy_MaskingPo}, nil}-func convertToV1PBMaskingLevel(level storepb.MaskingLevel) v1pb.MaskingLevel {- switch level {- case storepb.MaskingLevel_MASKING_LEVEL_UNSPECIFIED:- return v1pb.MaskingLevel_MASKING_LEVEL_UNSPECIFIED- case storepb.MaskingLevel_NONE:- return v1pb.MaskingLevel_NONE- case storepb.MaskingLevel_PARTIAL:- return v1pb.MaskingLevel_PARTIAL- case storepb.MaskingLevel_FULL:- return v1pb.MaskingLevel_FULL- default:- return v1pb.MaskingLevel_MASKING_LEVEL_UNSPECIFIED- }-}-func convertToStorePBMaskingPolicyPayload(policy *v1pb.MaskingPolicy) (*storepb.MaskingPolicy, error) {var maskData []*storepb.MaskData@@ -826,6 +873,21 @@ func convertToStorePBMaskingPolicyPayload(policy *v1pb.MaskingPolicy) (*storepb.}, nil}+func convertToV1PBMaskingLevel(level storepb.MaskingLevel) v1pb.MaskingLevel {+ switch level {+ case storepb.MaskingLevel_MASKING_LEVEL_UNSPECIFIED:+ return v1pb.MaskingLevel_MASKING_LEVEL_UNSPECIFIED+ case storepb.MaskingLevel_NONE:+ return v1pb.MaskingLevel_NONE+ case storepb.MaskingLevel_PARTIAL:+ return v1pb.MaskingLevel_PARTIAL+ case storepb.MaskingLevel_FULL:+ return v1pb.MaskingLevel_FULL+ default:+ return v1pb.MaskingLevel_MASKING_LEVEL_UNSPECIFIED+ }+}+func convertToStorePBMaskingLevel(level v1pb.MaskingLevel) storepb.MaskingLevel {switch level {case v1pb.MaskingLevel_MASKING_LEVEL_UNSPECIFIED:@@ -1016,6 +1078,46 @@ func convertToDisableCopyDataPolicyPayload(policy *v1pb.DisableCopyDataPolicy) (}, nil}+func convertToStorePBMskingRulePolicy(policy *v1pb.MaskingRulePolicy) (*storepb.MaskingRulePolicy, error) {+ var rules []*storepb.MaskingRulePolicy_MaskingRule+ for _, rule := range policy.Rules {+ rules = append(rules, &storepb.MaskingRulePolicy_MaskingRule{+ Id: rule.Id,+ Condition: &expr.Expr{+ Title: rule.Condition.Title,+ Expression: rule.Condition.Expression,+ Description: rule.Condition.Description,+ Location: rule.Condition.Location,+ },+ MaskingLevel: convertToStorePBMaskingLevel(rule.MaskingLevel),+ })+ }++ return &storepb.MaskingRulePolicy{+ Rules: rules,+ }, nil+}++func convertToV1PBMaskingRulePolicy(policy *storepb.MaskingRulePolicy) (*v1pb.MaskingRulePolicy, error) {+ var rules []*v1pb.MaskingRulePolicy_MaskingRule+ for _, rule := range policy.Rules {+ rules = append(rules, &v1pb.MaskingRulePolicy_MaskingRule{+ Id: rule.Id,+ Condition: &expr.Expr{+ Title: rule.Condition.Title,+ Expression: rule.Condition.Expression,+ Description: rule.Condition.Description,+ Location: rule.Condition.Location,+ },+ MaskingLevel: convertToV1PBMaskingLevel(rule.MaskingLevel),+ })+ }++ return &v1pb.MaskingRulePolicy{+ Rules: rules,+ }, nil+}+func convertIssueTypeToDeplymentType(issueType api.IssueType) v1pb.DeploymentType {res := v1pb.DeploymentType_DEPLOYMENT_TYPE_UNSPECIFIEDswitch issueType {diff --git a/backend/common/convert.go b/backend/common/convert.goindex 03c421f050..fcef6abd84 100644--- a/backend/common/convert.go+++ b/backend/common/convert.go@@ -43,6 +43,16 @@ var QueryExportPolicyCELAttributes = []cel.EnvOption{cel.Variable("resource.table", cel.StringType),}+// MaskingRulePolicyCELAttributes are the variables when evaluating masking rule.+var MaskingRulePolicyCELAttributes = []cel.EnvOption{+ cel.Variable("environment_id", cel.StringType),+ cel.Variable("project_id", cel.StringType),+ cel.Variable("instance_id", cel.StringType),+ cel.Variable("database_name", cel.StringType),+ cel.Variable("table_name", cel.StringType),+ cel.Variable("column_classification", cel.StringType),+}+// ConvertParsedRisk converts parsed risk to unparsed format.func ConvertParsedRisk(expression *v1alpha1.ParsedExpr) (*expr.Expr, error) {if expression == nil || expression.Expr == nil {@@ -134,6 +144,25 @@ func ValidateGroupCELExpr(expr string) (cel.Program, error) {return prog, nil}+// ValidateMaskingRuleCELExpr validates masking rule expr.+func ValidateMaskingRuleCELExpr(expr string) (cel.Program, error) {+ e, err := cel.NewEnv(+ MaskingRulePolicyCELAttributes...,+ )+ if err != nil {+ return nil, status.Errorf(codes.Internal, err.Error())+ }+ ast, issues := e.Parse(expr)+ if issues != nil && issues.Err() != nil {+ return nil, status.Errorf(codes.InvalidArgument, issues.Err().Error())+ }+ prog, err := e.Program(ast)+ if err != nil {+ return nil, status.Errorf(codes.InvalidArgument, err.Error())+ }+ return prog, nil+}+// QueryExportFactors is the factors for query and export.type QueryExportFactors struct {DatabaseNames []stringdiff --git a/backend/legacyapi/policy.go b/backend/legacyapi/policy.goindex 93d7d44be4..8d1f3e1185 100644--- a/backend/legacyapi/policy.go+++ b/backend/legacyapi/policy.go@@ -42,14 +42,14 @@ const (PolicyTypeEnvironmentTier PolicyType = "bb.policy.environment-tier"// PolicyTypeMasking is the masking policy type.PolicyTypeMasking PolicyType = "bb.policy.masking"- // PolicyTypeMaskingRule is the masking rule policy type.- PolicyTypeMaskingRule PolicyType = "bb.policy.masking-rule"// PolicyTypeMaskingException is the masking exception policy type.PolicyTypeMaskingException PolicyType = "bb.policy.masking-exception"// PolicyTypeSlowQuery is the slow query policy type.PolicyTypeSlowQuery PolicyType = "bb.policy.slow-query"// PolicyTypeDisableCopyData is the disable copy data policy type.PolicyTypeDisableCopyData PolicyType = "bb.policy.disable-copy-data"+ // PolicyTypeMaskingRule is the masking rule policy type.+ PolicyTypeMaskingRule PolicyType = "bb.policy.masking-rule"// PipelineApprovalValueManualNever means the pipeline will automatically be approved without user intervention.PipelineApprovalValueManualNever PipelineApprovalValue = "MANUAL_APPROVAL_NEVER"@@ -98,6 +98,7 @@ var (PolicyTypeMasking: {PolicyResourceTypeDatabase},PolicyTypeSlowQuery: {PolicyResourceTypeInstance},PolicyTypeDisableCopyData: {PolicyResourceTypeEnvironment},+ PolicyTypeMaskingRule: {PolicyResourceTypeWorkspace},})diff --git a/frontend/src/types/proto/store/policy.ts b/frontend/src/types/proto/store/policy.tsindex ace636c3ba..65796de7d0 100644--- a/frontend/src/types/proto/store/policy.ts+++ b/frontend/src/types/proto/store/policy.ts@@ -50,6 +50,8 @@ export interface MaskingRulePolicy {}export interface MaskingRulePolicy_MaskingRule {+ /** A unique identifier for a node in UUID format. */+ id: string;condition?: Expr | undefined;maskingLevel: MaskingLevel;}@@ -439,16 +441,19 @@ export const MaskingRulePolicy = {};function createBaseMaskingRulePolicy_MaskingRule(): MaskingRulePolicy_MaskingRule {- return { condition: undefined, maskingLevel: 0 };+ return { id: "", condition: undefined, maskingLevel: 0 };}export const MaskingRulePolicy_MaskingRule = {encode(message: MaskingRulePolicy_MaskingRule, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {+ if (message.id !== "") {+ writer.uint32(10).string(message.id);+ }if (message.condition !== undefined) {- Expr.encode(message.condition, writer.uint32(10).fork()).ldelim();+ Expr.encode(message.condition, writer.uint32(18).fork()).ldelim();}if (message.maskingLevel !== 0) {- writer.uint32(16).int32(message.maskingLevel);+ writer.uint32(24).int32(message.maskingLevel);}return writer;},@@ -465,10 +470,17 @@ export const MaskingRulePolicy_MaskingRule = {break;}- message.condition = Expr.decode(reader, reader.uint32());+ message.id = reader.string();continue;case 2:- if (tag !== 16) {+ if (tag !== 18) {+ break;+ }++ message.condition = Expr.decode(reader, reader.uint32());+ continue;+ case 3:+ if (tag !== 24) {break;}@@ -485,6 +497,7 @@ export const MaskingRulePolicy_MaskingRule = {fromJSON(object: any): MaskingRulePolicy_MaskingRule {return {+ id: isSet(object.id) ? String(object.id) : "",condition: isSet(object.condition) ? Expr.fromJSON(object.condition) : undefined,maskingLevel: isSet(object.maskingLevel) ? maskingLevelFromJSON(object.maskingLevel) : 0,};@@ -492,6 +505,7 @@ export const MaskingRulePolicy_MaskingRule = {toJSON(message: MaskingRulePolicy_MaskingRule): unknown {const obj: any = {};+ message.id !== undefined && (obj.id = message.id);message.condition !== undefined && (obj.condition = message.condition ? Expr.toJSON(message.condition) : undefined);message.maskingLevel !== undefined && (obj.maskingLevel = maskingLevelToJSON(message.maskingLevel));return obj;@@ -503,6 +517,7 @@ export const MaskingRulePolicy_MaskingRule = {fromPartial(object: DeepPartial<MaskingRulePolicy_MaskingRule>): MaskingRulePolicy_MaskingRule {const message = createBaseMaskingRulePolicy_MaskingRule();+ message.id = object.id ?? "";message.condition = (object.condition !== undefined && object.condition !== null)? Expr.fromPartial(object.condition): undefined;diff --git a/proto/gen/grpc-doc/store/README.md b/proto/gen/grpc-doc/store/README.mdindex f6ea3cb688..b144af05dc 100644--- a/proto/gen/grpc-doc/store/README.md+++ b/proto/gen/grpc-doc/store/README.md@@ -1890,6 +1890,7 @@ Reference: https://cloud.google.com/pubsub/docs/reference/rpc/google.iam.v1#bind| Field | Type | Label | Description || ----- | ---- | ----- | ----------- |+| id | [string](#string) | | A unique identifier for a node in UUID format. || condition | [google.type.Expr](#google-type-Expr) | | || masking_level | [MaskingLevel](#bytebase-store-MaskingLevel) | | |diff --git a/proto/generated-go/store/policy.pb.go b/proto/generated-go/store/policy.pb.goindex bd7eabad0e..870bece58a 100644--- a/proto/generated-go/store/policy.pb.go+++ b/proto/generated-go/store/policy.pb.go@@ -321,8 +321,10 @@ type MaskingRulePolicy_MaskingRule struct {sizeCache protoimpl.SizeCacheunknownFields protoimpl.UnknownFields- Condition *expr.Expr `protobuf:"bytes,1,opt,name=condition,proto3" json:"condition,omitempty"`- MaskingLevel MaskingLevel `protobuf:"varint,2,opt,name=masking_level,json=maskingLevel,proto3,enum=bytebase.store.MaskingLevel" json:"masking_level,omitempty"`+ // A unique identifier for a node in UUID format.+ Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`+ Condition *expr.Expr `protobuf:"bytes,2,opt,name=condition,proto3" json:"condition,omitempty"`+ MaskingLevel MaskingLevel `protobuf:"varint,3,opt,name=masking_level,json=maskingLevel,proto3,enum=bytebase.store.MaskingLevel" json:"masking_level,omitempty"`}func (x *MaskingRulePolicy_MaskingRule) Reset() {@@ -357,6 +359,13 @@ func (*MaskingRulePolicy_MaskingRule) Descriptor() ([]byte, []int) {return file_store_policy_proto_rawDescGZIP(), []int{4, 0}}+func (x *MaskingRulePolicy_MaskingRule) GetId() string {+ if x != nil {+ return x.Id+ }+ return ""+}+func (x *MaskingRulePolicy_MaskingRule) GetCondition() *expr.Expr {if x != nil {return x.Condition@@ -406,18 +415,19 @@ var file_store_policy_proto_rawDesc = []byte{0x6b, 0x69, 0x6e, 0x67, 0x5f, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0e,0x32, 0x1c, 0x2e, 0x62, 0x79, 0x74, 0x65, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x73, 0x74, 0x6f, 0x72,0x65, 0x2e, 0x4d, 0x61, 0x73, 0x6b, 0x69, 0x6e, 0x67, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x52, 0x0c,- 0x6d, 0x61, 0x73, 0x6b, 0x69, 0x6e, 0x67, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x22, 0xdc, 0x01, 0x0a,+ 0x6d, 0x61, 0x73, 0x6b, 0x69, 0x6e, 0x67, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x22, 0xec, 0x01, 0x0a,0x11, 0x4d, 0x61, 0x73, 0x6b, 0x69, 0x6e, 0x67, 0x52, 0x75, 0x6c, 0x65, 0x50, 0x6f, 0x6c, 0x69,0x63, 0x79, 0x12, 0x43, 0x0a, 0x05, 0x72, 0x75, 0x6c, 0x65, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28,0x0b, 0x32, 0x2d, 0x2e, 0x62, 0x79, 0x74, 0x65, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x73, 0x74, 0x6f,0x72, 0x65, 0x2e, 0x4d, 0x61, 0x73, 0x6b, 0x69, 0x6e, 0x67, 0x52, 0x75, 0x6c, 0x65, 0x50, 0x6f,0x6c, 0x69, 0x63, 0x79, 0x2e, 0x4d, 0x61, 0x73, 0x6b, 0x69, 0x6e, 0x67, 0x52, 0x75, 0x6c, 0x65,- 0x52, 0x05, 0x72, 0x75, 0x6c, 0x65, 0x73, 0x1a, 0x81, 0x01, 0x0a, 0x0b, 0x4d, 0x61, 0x73, 0x6b,- 0x69, 0x6e, 0x67, 0x52, 0x75, 0x6c, 0x65, 0x12, 0x2f, 0x0a, 0x09, 0x63, 0x6f, 0x6e, 0x64, 0x69,- 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x67, 0x6f, 0x6f,+ 0x52, 0x05, 0x72, 0x75, 0x6c, 0x65, 0x73, 0x1a, 0x91, 0x01, 0x0a, 0x0b, 0x4d, 0x61, 0x73, 0x6b,+ 0x69, 0x6e, 0x67, 0x52, 0x75, 0x6c, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20,+ 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x2f, 0x0a, 0x09, 0x63, 0x6f, 0x6e, 0x64, 0x69,+ 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x67, 0x6f, 0x6f,0x67, 0x6c, 0x65, 0x2e, 0x74, 0x79, 0x70, 0x65, 0x2e, 0x45, 0x78, 0x70, 0x72, 0x52, 0x09, 0x63,0x6f, 0x6e, 0x64, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x41, 0x0a, 0x0d, 0x6d, 0x61, 0x73, 0x6b,- 0x69, 0x6e, 0x67, 0x5f, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0e, 0x32,+ 0x69, 0x6e, 0x67, 0x5f, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32,0x1c, 0x2e, 0x62, 0x79, 0x74, 0x65, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x73, 0x74, 0x6f, 0x72, 0x65,0x2e, 0x4d, 0x61, 0x73, 0x6b, 0x69, 0x6e, 0x67, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x52, 0x0c, 0x6d,0x61, 0x73, 0x6b, 0x69, 0x6e, 0x67, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x42, 0x14, 0x5a, 0x12, 0x67,diff --git a/proto/store/policy.proto b/proto/store/policy.protoindex de7ad058ed..1e460d2625 100644--- a/proto/store/policy.proto+++ b/proto/store/policy.proto@@ -45,9 +45,12 @@ message MaskData {message MaskingRulePolicy {message MaskingRule {- google.type.Expr condition = 1;+ // A unique identifier for a node in UUID format.+ string id = 1;- MaskingLevel masking_level = 2;+ google.type.Expr condition = 2;++ MaskingLevel masking_level = 3;}repeated MaskingRule rules = 1;}