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: backend of masking algorithm (#8800)
h3n4l3b5021827152
diff --git a/backend/api/v1/setting_service.go b/backend/api/v1/setting_service.goindex 89541f13a6..30fa3f7836 100644--- a/backend/api/v1/setting_service.go+++ b/backend/api/v1/setting_service.go@@ -67,7 +67,7 @@ var whitelistSettings = []api.SettingName{api.SettingSchemaTemplate,api.SettingDataClassification,api.SettingSemanticTypes,- api.SettingMaskingAlgorithms,+ api.SettingMaskingAlgorithm,}//go:embed mail_templates/testmail/template.html@@ -141,7 +141,7 @@ func (s *SettingService) SetSetting(ctx context.Context, request *v1pb.SetSettin}apiSettingName := api.SettingName(settingName)// TODO(zp): remove the following hard code when we persist the algorithm setting.- if apiSettingName == api.SettingMaskingAlgorithms {+ if apiSettingName == api.SettingMaskingAlgorithm {return nil, status.Errorf(codes.InvalidArgument, "setting masking algorithm is not available")}@@ -416,7 +416,7 @@ func (s *SettingService) SetSetting(ctx context.Context, request *v1pb.SetSettinif err := convertV1PbToStorePb(request.Setting.Value.GetSemanticTypeSettingValue(), storeSemanticTypeSetting); err != nil {return nil, status.Errorf(codes.Internal, "failed to unmarshal setting value for %s with error: %v", apiSettingName, err)}- idMap := make(map[string]any)+ idMap := make(map[string]struct{})for _, tp := range storeSemanticTypeSetting.Types {if !isValidUUID(tp.Id) {return nil, status.Errorf(codes.InvalidArgument, "invalid semantic type id format: %s", tp.Id)@@ -427,13 +427,37 @@ func (s *SettingService) SetSetting(ctx context.Context, request *v1pb.SetSettinif _, ok := idMap[tp.Id]; ok {return nil, status.Errorf(codes.InvalidArgument, "duplicate semantic type id: %s", tp.Id)}- idMap[tp.Id] = any(nil)+ idMap[tp.Id] = struct{}{}}bytes, err := protojson.Marshal(storeSemanticTypeSetting)if err != nil {return nil, status.Errorf(codes.Internal, "failed to marshal setting for %s with error: %v", apiSettingName, err)}storeSettingValue = string(bytes)+ case api.SettingMaskingAlgorithm:+ storeMaskingAlgorithmSetting := new(storepb.MaskingAlgorithmSetting)+ if err := convertV1PbToStorePb(request.Setting.Value.GetMaskingAlgorithmSettingValue(), storeMaskingAlgorithmSetting); err != nil {+ return nil, status.Errorf(codes.Internal, "failed to unmarshal setting value for %s with error: %v", apiSettingName, err)+ }+ idMap := make(map[string]struct{})+ for _, algorithm := range storeMaskingAlgorithmSetting.Algorithms {+ if !isValidUUID(algorithm.Id) {+ return nil, status.Errorf(codes.InvalidArgument, "invalid masking algorithm id format: %s", algorithm.Id)+ }+ if algorithm.Title == "" {+ return nil, status.Errorf(codes.InvalidArgument, "masking algorithm title cannot be empty: %s", algorithm.Id)+ }++ if _, ok := idMap[algorithm.Id]; ok {+ return nil, status.Errorf(codes.InvalidArgument, "duplicate masking algorithm id: %s", algorithm.Id)+ }+ idMap[algorithm.Id] = struct{}{}+ }+ bytes, err := protojson.Marshal(storeMaskingAlgorithmSetting)+ if err != nil {+ return nil, status.Errorf(codes.Internal, "failed to marshal setting for %s with error: %v", apiSettingName, err)+ }+ storeSettingValue = string(bytes)default:storeSettingValue = request.Setting.Value.GetStringValue()}@@ -643,7 +667,7 @@ func (s *SettingService) convertToSettingMessage(ctx context.Context, setting *s},},}, nil- case api.SettingMaskingAlgorithms:+ case api.SettingMaskingAlgorithm: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)diff --git a/backend/legacyapi/setting.go b/backend/legacyapi/setting.goindex 5d9bbd74da..3a3d2e69e3 100644--- a/backend/legacyapi/setting.go+++ b/backend/legacyapi/setting.go@@ -45,7 +45,7 @@ const (// 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"+ SettingMaskingAlgorithm SettingName = "bb.workspace.masking-algorithm")// IMType is the type of IM.diff --git a/backend/store/setting.go b/backend/store/setting.goindex 3d6863db88..7b9e663e8e 100644--- a/backend/store/setting.go+++ b/backend/store/setting.go@@ -14,7 +14,6 @@ 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.@@ -38,23 +37,6 @@ 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_Algorithm{- {- Id: "substitution",- Title: "Substitution algorithm",- },- {- Id: "hash-md5",- Title: "MD5 encryption",- },- },- }-)-// GetWorkspaceGeneralSetting gets the workspace general setting payload.func (s *Store) GetWorkspaceGeneralSetting(ctx context.Context) (*storepb.WorkspaceProfileSetting, error) {settingName := api.SettingWorkspaceProfile@@ -184,20 +166,6 @@ func (s *Store) GetSettingV2(ctx context.Context, find *FindSettingMessage) (*Se}}- // 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")@@ -236,18 +204,6 @@ func (s *Store) ListSettingV2(ctx context.Context, find *FindSettingMessage) ([]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)}@@ -256,10 +212,6 @@ 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}@@ -303,11 +255,6 @@ 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}@@ -354,11 +301,6 @@ 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")diff --git a/frontend/src/components/SensitiveData/SemanticTypesView.vue b/frontend/src/components/SensitiveData/SemanticTypesView.vueindex 6c1624b602..58b7daadeb 100644--- a/frontend/src/components/SensitiveData/SemanticTypesView.vue+++ b/frontend/src/components/SensitiveData/SemanticTypesView.vue@@ -62,7 +62,7 @@ const hasSensitiveDataFeature = featureToRef("bb.feature.sensitive-data");const algorithmList = computed((): SelectOption[] => {return (- settingStore.getSettingByName("bb.workspace.masking-algorithms")?.value+ settingStore.getSettingByName("bb.workspace.masking-algorithm")?.value?.maskingAlgorithmSettingValue?.algorithms ?? []).map((algorithm) => ({label: algorithm.title,diff --git a/frontend/src/components/SensitiveData/components/SemanticTypesTable.vue b/frontend/src/components/SensitiveData/components/SemanticTypesTable.vueindex 024833df43..151297b954 100644--- a/frontend/src/components/SensitiveData/components/SemanticTypesTable.vue+++ b/frontend/src/components/SensitiveData/components/SemanticTypesTable.vue@@ -203,7 +203,7 @@ const onInput = (index: number, callback: (item: SemanticItem) => void) => {const algorithmList = computed((): SelectOption[] => {return (- settingStore.getSettingByName("bb.workspace.masking-algorithms")?.value+ settingStore.getSettingByName("bb.workspace.masking-algorithm")?.value?.maskingAlgorithmSettingValue?.algorithms ?? []).map((algorithm) => ({label: algorithm.title,diff --git a/frontend/src/types/setting.ts b/frontend/src/types/setting.tsindex fab636ced7..931470f510 100644--- a/frontend/src/types/setting.ts+++ b/frontend/src/types/setting.ts@@ -11,6 +11,6 @@ export type SettingName =| "bb.workspace.schema-template"| "bb.workspace.data-classification"| "bb.workspace.semantic-types"- | "bb.workspace.masking-algorithms";+ | "bb.workspace.masking-algorithm";export const defaultTokenDurationInHours = 7 * 24;