Skip to content

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

feat: algorithm setting curd (#8211)

h3n4l85284eb6f5fe
diff --git a/backend/api/v1/org_policy_service.go b/backend/api/v1/org_policy_service.go
index 807acbb6dc..6bd1a4d290 100644
--- a/backend/api/v1/org_policy_service.go
+++ b/backend/api/v1/org_policy_service.go
@@ -890,11 +890,13 @@ func convertToV1PBMaskingPolicy(payloadStr string) (*v1pb.Policy_MaskingPolicy,
var maskDataList []*v1pb.MaskData
for _, data := range maskingPolicy.MaskData {
maskDataList = append(maskDataList, &v1pb.MaskData{
- Schema: data.Schema,
- Table: data.Table,
- Column: data.Column,
- SemanticTypeId: data.SemanticTypeId,
- MaskingLevel: convertToV1PBMaskingLevel(data.MaskingLevel),
+ Schema: data.Schema,
+ Table: data.Table,
+ Column: data.Column,
+ SemanticTypeId: data.SemanticTypeId,
+ FullMaskingAlgorithmId: data.FullMaskingAlgorithmId,
+ PartialMaskingAlgorithmId: data.PartialMaskingAlgorithmId,
+ MaskingLevel: convertToV1PBMaskingLevel(data.MaskingLevel),
})
}
@@ -910,11 +912,13 @@ func convertToStorePBMaskingPolicyPayload(policy *v1pb.MaskingPolicy) (*storepb.
for _, data := range policy.MaskData {
maskData = append(maskData, &storepb.MaskData{
- Schema: data.Schema,
- Table: data.Table,
- Column: data.Column,
- SemanticTypeId: data.SemanticTypeId,
- MaskingLevel: convertToStorePBMaskingLevel(data.MaskingLevel),
+ Schema: data.Schema,
+ Table: data.Table,
+ Column: data.Column,
+ SemanticTypeId: data.SemanticTypeId,
+ FullMaskingAlgorithmId: data.FullMaskingAlgorithmId,
+ PartialMaskingAlgorithmId: data.PartialMaskingAlgorithmId,
+ MaskingLevel: convertToStorePBMaskingLevel(data.MaskingLevel),
})
}
diff --git a/backend/api/v1/setting_service.go b/backend/api/v1/setting_service.go
index 8a9c2003e6..80bede9e52 100644
--- a/backend/api/v1/setting_service.go
+++ b/backend/api/v1/setting_service.go
@@ -73,6 +73,7 @@ var whitelistSettings = []api.SettingName{
api.SettingSchemaTemplate,
api.SettingDataClassification,
api.SettingSemanticTypes,
+ api.SettingMaskingAlgorithms,
}
//go:embed mail_templates/testmail/template.html
@@ -144,8 +145,12 @@ func (s *SettingService) SetSetting(ctx context.Context, request *v1pb.SetSettin
if s.profile.IsFeatureUnavailable(settingName) {
return nil, status.Errorf(codes.InvalidArgument, "feature %s is unavailable in current mode", settingName)
}
-
apiSettingName := api.SettingName(settingName)
+ // TODO(zp): remove the following hard code when we persist the algorithm setting.
+ if apiSettingName == api.SettingMaskingAlgorithms {
+ return nil, status.Errorf(codes.InvalidArgument, "setting masking algorithm is not available")
+ }
+
var storeSettingValue string
switch apiSettingName {
case api.SettingWorkspaceProfile:
@@ -712,6 +717,20 @@ func (s *SettingService) convertToSettingMessage(ctx context.Context, setting *s
},
},
}, nil
+ case api.SettingMaskingAlgorithms:
+ v1Value := new(v1pb.MaskingAlgorithmSetting)
+ if err := protojson.Unmarshal([]byte(setting.Value), v1Value); err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to unmarshal setting value for %s with error: %v", setting.Name, err)
+ }
+ return &v1pb.Setting{
+ Name: settingName,
+ Value: &v1pb.Value{
+ Value: &v1pb.Value_MaskingAlgorithmSettingValue{
+ MaskingAlgorithmSettingValue: v1Value,
+ },
+ },
+ }, nil
+
default:
return &v1pb.Setting{
Name: settingName,
diff --git a/backend/legacyapi/setting.go b/backend/legacyapi/setting.go
index dde866629a..5d9bbd74da 100644
--- a/backend/legacyapi/setting.go
+++ b/backend/legacyapi/setting.go
@@ -44,6 +44,8 @@ const (
SettingDataClassification SettingName = "bb.workspace.data-classification"
// SettingSemanticTypes is the setting name for semantic types.
SettingSemanticTypes SettingName = "bb.workspace.semantic-types"
+ // SettingMaskingAlgorithms is the setting name for masking algorithms.
+ SettingMaskingAlgorithms SettingName = "bb.workspace.masking-algorithms"
)
// IMType is the type of IM.
diff --git a/backend/store/setting.go b/backend/store/setting.go
index e6d93944bb..6d1eef33bf 100644
--- a/backend/store/setting.go
+++ b/backend/store/setting.go
@@ -14,6 +14,7 @@ import (
"github.com/bytebase/bytebase/backend/common"
api "github.com/bytebase/bytebase/backend/legacyapi"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+ v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
// FindSettingMessage is the message for finding setting.
@@ -37,6 +38,14 @@ type SettingMessage struct {
CreatedTs int64
}
+var (
+ // Currently, we do not persist the algorithm setting, in order to use it conveniently, we hard code it here.
+ // TODO(zp): remove the following hard code when we persist the algorithm setting.
+ mockAlgorithmSetting = &v1pb.MaskingAlgorithmSetting{
+ Algorithms: []*v1pb.MaskingAlgorithmSetting_MaskingAlgorithm{},
+ }
+)
+
// GetWorkspaceGeneralSetting gets the workspace general setting payload.
func (s *Store) GetWorkspaceGeneralSetting(ctx context.Context) (*storepb.WorkspaceProfileSetting, error) {
settingName := api.SettingWorkspaceProfile
@@ -185,6 +194,21 @@ func (s *Store) GetSettingV2(ctx context.Context, find *FindSettingMessage) (*Se
return setting.(*SettingMessage), nil
}
}
+
+ // TODO(zp): remove the following hard code when we persist the algorithm setting.
+ if find.Name != nil && *find.Name == api.SettingMaskingAlgorithms {
+ value, err := protojson.Marshal(mockAlgorithmSetting)
+ if err != nil {
+ return nil, err
+ }
+ setting := &SettingMessage{
+ Name: api.SettingMaskingAlgorithms,
+ Value: string(value),
+ Description: "",
+ }
+ return setting, nil
+ }
+
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, errors.Wrap(err, "failed to begin transaction")
@@ -222,6 +246,19 @@ func (s *Store) ListSettingV2(ctx context.Context, find *FindSettingMessage) ([]
if err := tx.Commit(); err != nil {
return nil, errors.Wrap(err, "failed to commit transaction")
}
+
+ // TODO(zp): remove the following hard code when we persist the algorithm setting.
+ value, err := protojson.Marshal(mockAlgorithmSetting)
+ if err != nil {
+ return nil, err
+ }
+ setting := &SettingMessage{
+ Name: api.SettingMaskingAlgorithms,
+ Value: string(value),
+ Description: "",
+ }
+ settings = append(settings, setting)
+
for _, setting := range settings {
s.settingCache.Store(setting.Name, setting)
}
@@ -230,6 +267,10 @@ func (s *Store) ListSettingV2(ctx context.Context, find *FindSettingMessage) ([]
// UpsertSettingV2 upserts the setting by name.
func (s *Store) UpsertSettingV2(ctx context.Context, update *SetSettingMessage, principalUID int) (*SettingMessage, error) {
+ if update.Name == api.SettingMaskingAlgorithms {
+ return nil, errors.New("cannot update masking algorithm setting")
+ }
+
fields := []string{"creator_id", "updater_id", "name", "value"}
updateFields := []string{"value = EXCLUDED.value", "updater_id = EXCLUDED.updater_id"}
valuePlaceholders, args := []string{"$1", "$2", "$3", "$4"}, []any{principalUID, principalUID, update.Name, update.Value}
@@ -273,6 +314,11 @@ func (s *Store) UpsertSettingV2(ctx context.Context, update *SetSettingMessage,
// CreateSettingIfNotExistV2 creates a new setting only if the named setting doesn't exist.
func (s *Store) CreateSettingIfNotExistV2(ctx context.Context, create *SettingMessage, principalUID int) (*SettingMessage, bool, error) {
+ // TODO(zp): remove the following hard code when we persist the algorithm setting.
+ if create.Name == api.SettingMaskingAlgorithms {
+ return nil, false, errors.New("cannot create masking algorithm setting")
+ }
+
if setting, ok := s.settingCache.Load(create.Name); ok {
return setting.(*SettingMessage), false, nil
}
@@ -319,6 +365,11 @@ func (s *Store) CreateSettingIfNotExistV2(ctx context.Context, create *SettingMe
// DeleteSettingV2 deletes a setting by the name.
func (s *Store) DeleteSettingV2(ctx context.Context, name api.SettingName) error {
+ // TODO(zp): remove the following hard code when we persist the algorithm setting.
+ if name == api.SettingMaskingAlgorithms {
+ return errors.New("cannot delete masking algorithm setting")
+ }
+
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return errors.Wrap(err, "failed to begin transaction")