Skip to content

Commit 127b6ef

Browse files
committed
add sensitive column validation
1 parent a50c330 commit 127b6ef

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

plugin/testing_validation.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,12 @@ func ValidateNoEmptyColumns(t *testing.T, tables schema.Tables, messages message
1414
if len(emptyColumns) > 0 {
1515
t.Fatalf("found empty column(s): %v in %s", emptyColumns, table.Name)
1616
}
17+
nonMatchingColumns, nonMatchingJsonColumns := schema.FindNotMatchingSensitiveColumns(table, records)
18+
if len(nonMatchingColumns) > 0 {
19+
t.Fatalf("found non-matching sensitive column(s): %v in %s", nonMatchingColumns, table.Name)
20+
}
21+
if len(nonMatchingJsonColumns) > 0 {
22+
t.Fatalf("found non-matching sensitive JSON column(s): %v in %s", nonMatchingJsonColumns, table.Name)
23+
}
1724
}
1825
}

schema/validators.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package schema
22

33
import (
44
"encoding/json"
5+
"slices"
6+
"strings"
57

68
"github.com/apache/arrow-go/v18/arrow"
79
"github.com/cloudquery/plugin-sdk/v4/types"
@@ -40,6 +42,36 @@ func FindEmptyColumns(table *Table, records []arrow.Record) []string {
4042
return emptyColumns
4143
}
4244

45+
func FindNotMatchingSensitiveColumns(table *Table, records []arrow.Record) ([]string, []string) {
46+
if len(table.SensitiveColumns) == 0 {
47+
return []string{}, []string{}
48+
}
49+
50+
nonMatchingColumns := make([]string, 0)
51+
nonMatchingJsonColumns := make([]string, 0)
52+
tableColumns := table.Columns.Names()
53+
for _, c := range table.SensitiveColumns {
54+
isJsonPath := false
55+
if strings.Contains(c, ".") {
56+
c = strings.Split(c, ".")[0]
57+
isJsonPath = true
58+
}
59+
if !slices.Contains(tableColumns, c) {
60+
nonMatchingColumns = append(nonMatchingColumns, c)
61+
continue
62+
}
63+
if !isJsonPath {
64+
continue
65+
}
66+
col := table.Columns.Get(c)
67+
if !arrow.TypeEqual(col.Type, types.ExtensionTypes.JSON) {
68+
nonMatchingJsonColumns = append(nonMatchingJsonColumns, c)
69+
continue
70+
}
71+
}
72+
return nonMatchingColumns, nonMatchingJsonColumns
73+
}
74+
4375
func isEmptyJSON(msg json.RawMessage) bool {
4476
if len(msg) == 0 {
4577
return true

0 commit comments

Comments
 (0)