Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: update range mask algorithm (#11832)
ecmadaod01cba069034
diff --git a/backend/api/v1/setting_service.go b/backend/api/v1/setting_service.goindex 07b63c1bd2..2f547a2266 100644--- a/backend/api/v1/setting_service.go+++ b/backend/api/v1/setting_service.go@@ -1186,9 +1186,6 @@ func validateMaskingAlgorithm(algorithm *v1pb.MaskingAlgorithmSetting_Algorithm)if len(slice.Substitution) > 16 {return status.Errorf(codes.InvalidArgument, "the substitution should less than 16 bytes")}- if slice.Start >= slice.End {- return status.Errorf(codes.InvalidArgument, "the slice end must smaller than the start: [%d,%d)", slice.Start, slice.End)- }for j := 0; j < i; j++ {pre := m.RangeMask.Slices[j]if slice.Start >= pre.End || pre.Start >= slice.End {diff --git a/backend/component/masker/masker.go b/backend/component/masker/masker.goindex 02ff87c8b3..0a0eb4b871 100644--- a/backend/component/masker/masker.go+++ b/backend/component/masker/masker.go@@ -171,45 +171,77 @@ type RangeMasker struct {// NewRangeMasker returns a new RangeMasker.func NewRangeMasker(maskRangeSlice []*MaskRangeSlice) *RangeMasker {- sort.SliceStable(maskRangeSlice, func(i, j int) bool {- return maskRangeSlice[i].Start < maskRangeSlice[j].Start- })+ return &RangeMasker{+ MaskRangeSlice: maskRangeSlice,+ }+}++func (m *RangeMasker) enableMask() bool {+ return len(m.MaskRangeSlice) > 0+}++func getValidRangeValue(length, val int32) int32 {+ if val >= 0 {+ return val+ }+ resp := length + val + 1+ if resp < 0 {+ return 0+ }+ return resp+}++func (m *RangeMasker) formatRanges(maxLength int32) []*MaskRangeSlice {// Merge the overlapping ranges.var mergedMaskRangeSlice []*MaskRangeSlice- for _, maskRange := range maskRangeSlice {- if maskRange.Start > maskRange.End {- maskRange.End = maskRange.Start + 1++ for _, maskRange := range m.MaskRangeSlice {+ start := getValidRangeValue(maxLength, maskRange.Start)+ end := getValidRangeValue(maxLength, maskRange.End)++ if start > end {+ end = start + 1}+if len(mergedMaskRangeSlice) == 0 {- mergedMaskRangeSlice = append(mergedMaskRangeSlice, maskRange)+ mergedMaskRangeSlice = append(mergedMaskRangeSlice, &MaskRangeSlice{+ Start: start,+ End: end,+ Substitution: maskRange.Substitution,+ })continue}lastMaskRange := mergedMaskRangeSlice[len(mergedMaskRangeSlice)-1]- if lastMaskRange.End >= maskRange.Start {- mergedMaskRangeSlice[len(mergedMaskRangeSlice)-1].End = maskRange.End+ if lastMaskRange.End >= start {+ mergedMaskRangeSlice[len(mergedMaskRangeSlice)-1].End = end} else {- mergedMaskRangeSlice = append(mergedMaskRangeSlice, maskRange)+ mergedMaskRangeSlice = append(mergedMaskRangeSlice, &MaskRangeSlice{+ Start: start,+ End: end,+ Substitution: maskRange.Substitution,+ })}}- return &RangeMasker{- MaskRangeSlice: mergedMaskRangeSlice,- }-}-func (m *RangeMasker) enableMask() bool {- return len(m.MaskRangeSlice) > 0+ sort.SliceStable(mergedMaskRangeSlice, func(i, j int) bool {+ return mergedMaskRangeSlice[i].Start < mergedMaskRangeSlice[j].Start+ })++ return mergedMaskRangeSlice}// Mask implements Masker.Mask.func (m *RangeMasker) Mask(data *MaskData) *v1pb.RowValue {+ if !m.enableMask() {+ return noneMask(data)+ }+fRune := func(s []rune) []rune {- if !m.enableMask() {- return s- }+ maskRangeSlice := m.formatRanges(int32(len(s)))var ret []runeprevEnd := 0- for _, maskRange := range m.MaskRangeSlice {+ for _, maskRange := range maskRangeSlice {// First, append the unmasked part.begin, end := prevEnd, int(maskRange.Start)if begin >= len(s) {@@ -238,14 +270,13 @@ func (m *RangeMasker) Mask(data *MaskData) *v1pb.RowValue {}return ret}+fBytes := func(s []byte) []byte {- if !m.enableMask() {- return s- }+ maskRangeSlice := m.formatRanges(int32(len(s)))var ret []byteprevEnd := 0- for _, maskRange := range m.MaskRangeSlice {+ for _, maskRange := range maskRangeSlice {// First, append the unmasked part.begin, end := prevEnd, int(maskRange.Start)if begin >= len(s) {@@ -274,9 +305,7 @@ func (m *RangeMasker) Mask(data *MaskData) *v1pb.RowValue {}return ret}- if !m.enableMask() {- return noneMask(data)- }+if data.Data != nil {var stringValue stringvar valid booldiff --git a/backend/component/masker/masker_test.go b/backend/component/masker/masker_test.goindex e0808e235c..1caa7ef747 100644--- a/backend/component/masker/masker_test.go+++ b/backend/component/masker/masker_test.go@@ -78,6 +78,42 @@ func TestRangeMask(t *testing.T) {},},},+ {+ description: "Single slice - only keep 1st raw value",+ input: &MaskData{+ Data: &sql.NullString{String: "012345678", Valid: true},+ },+ slices: []*MaskRangeSlice{+ {+ Start: 1,+ End: -1,+ Substitution: "***",+ },+ },+ want: &v1pb.RowValue{+ Kind: &v1pb.RowValue_StringValue{+ StringValue: "0***",+ },+ },+ },+ {+ description: "Single slice - mask last 3 value",+ input: &MaskData{+ Data: &sql.NullString{String: "012345678", Valid: true},+ },+ slices: []*MaskRangeSlice{+ {+ Start: -4,+ End: -1,+ Substitution: "***",+ },+ },+ want: &v1pb.RowValue{+ Kind: &v1pb.RowValue_StringValue{+ StringValue: "012345***",+ },+ },+ },{description: "Multiple slices",input: &MaskData{@@ -101,6 +137,29 @@ func TestRangeMask(t *testing.T) {},},},+ {+ description: "Multiple slices",+ input: &MaskData{+ Data: &sql.NullString{String: "012345678", Valid: true},+ },+ slices: []*MaskRangeSlice{+ {+ Start: 0,+ End: 2,+ Substitution: "###",+ },+ {+ Start: -2,+ End: -1,+ Substitution: "***",+ },+ },+ want: &v1pb.RowValue{+ Kind: &v1pb.RowValue_StringValue{+ StringValue: "###234567***",+ },+ },+ },{description: "Mask slices out of data range",input: &MaskData{diff --git a/frontend/src/components/SensitiveData/SensitiveColumnDrawer.vue b/frontend/src/components/SensitiveData/SensitiveColumnDrawer.vueindex 562ad7bee4..acac0a25ea 100644--- a/frontend/src/components/SensitiveData/SensitiveColumnDrawer.vue+++ b/frontend/src/components/SensitiveData/SensitiveColumnDrawer.vue@@ -22,10 +22,16 @@/></div><div class="w-full">- <h1 class="mb-2 font-semibold">- {{ $t("settings.sensitive-data.algorithms.self") }}- </h1>- <div class="flex flex-col space-y-2">+ <div+ v-if="+ state.maskingLevel === MaskingLevel.FULL ||+ columnMetadata?.effectiveMaskingLevel === MaskingLevel.FULL+ "+ class="flex flex-col space-y-2"+ >+ <h1 class="font-semibold">+ {{ $t("settings.sensitive-data.algorithms.self") }}+ </h1><span class="textinfolabel">{{$t(@@ -50,7 +56,16 @@@change="onMaskingAlgorithmChanged"/></div>- <div class="flex flex-col mt-4 space-y-2">+ <div+ v-else-if="+ state.maskingLevel === MaskingLevel.PARTIAL ||+ columnMetadata?.effectiveMaskingLevel === MaskingLevel.PARTIAL+ "+ class="flex flex-col space-y-2"+ >+ <h1 class="font-semibold">+ {{ $t("settings.sensitive-data.algorithms.self") }}+ </h1><span class="textinfolabel">{{$t(diff --git a/frontend/src/components/SensitiveData/components/MaskingAlgorithmsCreateDrawer.vue b/frontend/src/components/SensitiveData/components/MaskingAlgorithmsCreateDrawer.vueindex a279b4f27c..e0259a5edf 100644--- a/frontend/src/components/SensitiveData/components/MaskingAlgorithmsCreateDrawer.vue+++ b/frontend/src/components/SensitiveData/components/MaskingAlgorithmsCreateDrawer.vue@@ -94,7 +94,6 @@</label><NInputNumber:value="slice.start"- :min="0":placeholder="t('settings.sensitive-data.algorithms.range-mask.slice-start'@@ -116,7 +115,6 @@</label><NInputNumber:value="slice.end"- :min="1":placeholder="t('settings.sensitive-data.algorithms.range-mask.slice-end')"@@ -348,17 +346,9 @@ const rangeMaskErrorMessage = computed(() => {}for (let i = 0; i < state.rangeMask.slices.length; i++) {const slice = state.rangeMask.slices[i];- if (- Number.isNaN(slice.start) ||- Number.isNaN(slice.end) ||- slice.start < 0 ||- slice.end <= 0- ) {+ if (Number.isNaN(slice.start) || Number.isNaN(slice.end)) {return t("settings.sensitive-data.algorithms.error.slice-invalid-number");}- if (slice.start >= slice.end) {- return t("settings.sensitive-data.algorithms.error.slice-number-range");- }for (let j = 0; j < i; j++) {const pre = state.rangeMask.slices[j];@@ -493,7 +483,7 @@ const onSliceStartChange = (index: number, val: number | null) => {return;}const slice = state.rangeMask.slices[index];- slice.start = Math.max(0, val);+ slice.start = val;if (slice.end <= slice.start) {slice.end = slice.start + 1;}@@ -504,9 +494,6 @@ const onSliceEndChange = (index: number, val: number | null) => {return;}const slice = state.rangeMask.slices[index];- slice.end = Math.max(1, val);- if (slice.start >= slice.end) {- slice.start = slice.end - 1;- }+ slice.end = val;};</script>diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.jsonindex 8a56a548ef..44282dd34d 100644--- a/frontend/src/locales/en-US.json+++ b/frontend/src/locales/en-US.json@@ -789,7 +789,7 @@},"range-mask": {"self": "Range mask",- "label": "Range mask will replace the original value from start index to end index (end not included) with the substitution.",+ "label": "Range mask will replace the original value from start index to end index (end not included) with the substitution. The index starts from 0.","slice-start": "Slice start","slice-start-label": "Slice start is the start index of the original value, start from 0 and should be less than stop.","slice-end": "Slice end",diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.jsonindex e614f9f986..1ea49ab7e1 100644--- a/frontend/src/locales/es-ES.json+++ b/frontend/src/locales/es-ES.json@@ -789,7 +789,7 @@},"range-mask": {"self": "Máscara de rango",- "label": "La máscara de rango reemplazará el valor original desde el índice inicial hasta el índice final (final no incluido) con la sustitución.",+ "label": "La máscara de rango reemplazará el valor original desde el índice inicial hasta el índice final (final no incluido) con la sustitución. El índice comienza desde 0.","slice-start": "Inicio de corte","slice-start-label": "El inicio del segmento es el índice de inicio del valor original, comienza desde 0 y debe ser menor que detener.","slice-end": "Fin del corte",diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.jsonindex da6e7f4721..1b939c8125 100644--- a/frontend/src/locales/ja-JP.json+++ b/frontend/src/locales/ja-JP.json@@ -789,7 +789,7 @@},"range-mask": {"self": "範囲マスキング",- "label": "範囲マスキングでは、マスクされた文字列を使用して、開始インデックスから終了インデックスまでの元の値を置き換えます (排他的)。",+ "label": "範囲マスキングでは、マスクされた文字列を使用して、開始インデックスから終了インデックスまでの元の値を置き換えます (排他的)。インデックスは 0 から始まります。","slice-start": "出発点","slice-start-label": "開始位置は元の値の開始インデックスであり、0 から始まり、終了位置より小さい値になります。","slice-end": "終了位置",diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.jsonindex 110e605c7a..43062603cf 100644--- a/frontend/src/locales/vi-VN.json+++ b/frontend/src/locales/vi-VN.json@@ -789,7 +789,7 @@},"range-mask": {"self": "Mặt nạ phạm vi",- "label": "Mặt nạ phạm vi sẽ thay thế giá trị ban đầu từ chỉ mục bắt đầu đến chỉ mục kết thúc (không bao gồm phần cuối) bằng giá trị thay thế.",+ "label": "Mặt nạ phạm vi sẽ thay thế giá trị ban đầu từ chỉ mục bắt đầu đến chỉ mục kết thúc (không bao gồm phần cuối) bằng giá trị thay thế. Chỉ số bắt đầu từ 0.","slice-start": "Bắt đầu lát cắt","slice-start-label": "Bắt đầu lát cắt là chỉ số bắt đầu của giá trị ban đầu, bắt đầu từ 0 và phải nhỏ hơn điểm dừng.","slice-end": "Phần cuối",diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.jsonindex 500384c3bb..585961c67b 100644--- a/frontend/src/locales/zh-CN.json+++ b/frontend/src/locales/zh-CN.json@@ -789,7 +789,7 @@},"range-mask": {"self": "范围遮掩",- "label": "范围遮掩使用遮掩字符串来替换从起始索引到结束索引(不包含结束索引)的原始值。",+ "label": "范围遮掩使用遮掩字符串来替换从起始索引到结束索引(不包含结束索引)的原始值,索引从 0 开始。","slice-start": "起始位置","slice-start-label": "起始位置是原始值的起始索引,从 0 开始且小于结束位置。","slice-end": "结束位置",diff --git a/frontend/src/types/proto/store/setting.ts b/frontend/src/types/proto/store/setting.tsindex 682e527261..f6e3188985 100644--- a/frontend/src/types/proto/store/setting.ts+++ b/frontend/src/types/proto/store/setting.ts@@ -351,10 +351,10 @@ export interface MaskingAlgorithmSetting_Algorithm {/** description is the description for masking algorithm. */description: string;/**- * Category is the category for masking algorithm. Currently, it accepts 2 categories only: MASKING and HASHING.+ * Category is the category for masking algorithm. Currently, it accepts 2 categories only: MASK and HASH.* The range of accepted Payload is decided by the category.- * Mask: FullMask, RangeMask- * Hash: MD5Mask+ * MASK: FullMask, RangeMask+ * HASH: MD5Mask*/category: string;fullMask?: MaskingAlgorithmSetting_Algorithm_FullMask | undefined;diff --git a/proto/gen/grpc-doc/openapi.yaml b/proto/gen/grpc-doc/openapi.yamlindex c167e9643d..0370e8b1cc 100644--- a/proto/gen/grpc-doc/openapi.yaml+++ b/proto/gen/grpc-doc/openapi.yaml@@ -11089,10 +11089,10 @@ components:category:type: stringdescription: |-- Category is the category for masking algorithm. Currently, it accepts 2 categories only: MASKING and HASHING.+ Category is the category for masking algorithm. Currently, it accepts 2 categories only: MASK and HASH.The range of accepted Payload is decided by the category.- Mask: FullMask, RangeMask- Hash: MD5Mask+ MASK: FullMask, RangeMask+ HASH: MD5MaskfullMask:$ref: '#/components/schemas/Algorithm_FullMask'rangeMask:diff --git a/proto/gen/grpc-doc/store/README.md b/proto/gen/grpc-doc/store/README.mdindex a578efbb09..27529784ae 100644--- a/proto/gen/grpc-doc/store/README.md+++ b/proto/gen/grpc-doc/store/README.md@@ -3153,7 +3153,7 @@ The type of target.| id | [string](#string) | | id is the uuid for masking algorithm. || title | [string](#string) | | title is the title for masking algorithm. || description | [string](#string) | | description is the description for masking algorithm. |-| category | [string](#string) | | Category is the category for masking algorithm. Currently, it accepts 2 categories only: MASKING and HASHING. The range of accepted Payload is decided by the category. Mask: FullMask, RangeMask Hash: MD5Mask |+| category | [string](#string) | | Category is the category for masking algorithm. Currently, it accepts 2 categories only: MASK and HASH. The range of accepted Payload is decided by the category. MASK: FullMask, RangeMask HASH: MD5Mask || full_mask | [MaskingAlgorithmSetting.Algorithm.FullMask](#bytebase-store-MaskingAlgorithmSetting-Algorithm-FullMask) | | || range_mask | [MaskingAlgorithmSetting.Algorithm.RangeMask](#bytebase-store-MaskingAlgorithmSetting-Algorithm-RangeMask) | | || md5_mask | [MaskingAlgorithmSetting.Algorithm.MD5Mask](#bytebase-store-MaskingAlgorithmSetting-Algorithm-MD5Mask) | | |diff --git a/proto/gen/grpc-doc/store/index.html b/proto/gen/grpc-doc/store/index.htmlindex 0768ec23c3..a41d2c5428 100644--- a/proto/gen/grpc-doc/store/index.html+++ b/proto/gen/grpc-doc/store/index.html@@ -7838,10 +7838,10 @@ service to identify the node. </p></td><td>category</td><td><a href="#string">string</a></td><td></td>- <td><p>Category is the category for masking algorithm. Currently, it accepts 2 categories only: MASKING and HASHING.+ <td><p>Category is the category for masking algorithm. Currently, it accepts 2 categories only: MASK and HASH.The range of accepted Payload is decided by the category.-Mask: FullMask, RangeMask-Hash: MD5Mask </p></td>+MASK: FullMask, RangeMask+HASH: MD5Mask </p></td></tr><tr>diff --git a/proto/generated-go/store/setting.pb.go b/proto/generated-go/store/setting.pb.goindex 2accbf4ffa..323e1abb6f 100644--- a/proto/generated-go/store/setting.pb.go+++ b/proto/generated-go/store/setting.pb.go@@ -1501,10 +1501,10 @@ type MaskingAlgorithmSetting_Algorithm struct {Title string `protobuf:"bytes,2,opt,name=title,proto3" json:"title,omitempty"`// description is the description for masking algorithm.Description string `protobuf:"bytes,3,opt,name=description,proto3" json:"description,omitempty"`- // Category is the category for masking algorithm. Currently, it accepts 2 categories only: MASKING and HASHING.+ // Category is the category for masking algorithm. Currently, it accepts 2 categories only: MASK and HASH.// The range of accepted Payload is decided by the category.- // Mask: FullMask, RangeMask- // Hash: MD5Mask+ // MASK: FullMask, RangeMask+ // HASH: MD5MaskCategory string `protobuf:"bytes,4,opt,name=category,proto3" json:"category,omitempty"`// Types that are assignable to Mask://diff --git a/proto/store/setting.proto b/proto/store/setting.protoindex 1bfacd6645..8ff1864214 100644--- a/proto/store/setting.proto+++ b/proto/store/setting.proto@@ -230,10 +230,10 @@ message MaskingAlgorithmSetting {// description is the description for masking algorithm.string description = 3;- // Category is the category for masking algorithm. Currently, it accepts 2 categories only: MASKING and HASHING.+ // Category is the category for masking algorithm. Currently, it accepts 2 categories only: MASK and HASH.// The range of accepted Payload is decided by the category.- // Mask: FullMask, RangeMask- // Hash: MD5Mask+ // MASK: FullMask, RangeMask+ // HASH: MD5Maskstring category = 4;message FullMask {