Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V0.2 #92

Closed
wants to merge 63 commits into from
Closed

V0.2 #92

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
f9eaaf7
Parametrizer abstraction
wwoytenko Jan 10, 2024
89642f7
Fixed restore section for playground config
wwoytenko Jan 10, 2024
c7c97a3
Merge branch 'main' into v0.2
wwoytenko Jan 10, 2024
520d70f
Refactored code:
wwoytenko Jan 11, 2024
8d98cdb
Refactored code:
wwoytenko Jan 12, 2024
7b14c5b
Refactored code:
wwoytenko Jan 12, 2024
46a1e4d
Fixes
wwoytenko Jan 12, 2024
849f848
Implemented dynamic parameters basic usage
wwoytenko Jan 14, 2024
8a69fa5
New rails
wwoytenko Jan 14, 2024
5159645
New rails
wwoytenko Jan 15, 2024
b57b84a
Adapted tests for new rails
wwoytenko Jan 17, 2024
c35d4bc
Minor changes:
wwoytenko Jan 17, 2024
56a09a6
Get rid of the deprecated:
wwoytenko Jan 18, 2024
26f2170
Get rid of the deprecated:
wwoytenko Jan 18, 2024
ea5e9e2
Introduced dynamic parameters compatible types
wwoytenko Jan 20, 2024
ad1c6d8
Dynamic parameters
wwoytenko Jan 20, 2024
1cfe53b
Refactored:
wwoytenko Jan 21, 2024
6aa5d98
Fixed unit tests
wwoytenko Jan 22, 2024
760daf4
Merge branch 'main' into v0.2
wwoytenko Jan 26, 2024
1066f9d
Dynamic parameters with cast_template execution
wwoytenko Jan 27, 2024
19a44f0
Merged with main
wwoytenko Jan 31, 2024
e3af948
cast_to implementation and auto-cast
wwoytenko Jan 31, 2024
bf02864
cast_to implementation
wwoytenko Feb 4, 2024
8d129cc
Fixed test errors
wwoytenko Feb 4, 2024
828827f
Fixes
wwoytenko Feb 4, 2024
7c45cc3
Fixes
wwoytenko Feb 4, 2024
b2719c9
Merged with main
wwoytenko Feb 23, 2024
4c848c4
Fixed errors after merge with actual v0.1.6 version
wwoytenko Feb 23, 2024
86a11f9
Added DynamicUnmarshaler logic and fixed type in method name
wwoytenko Feb 23, 2024
f1b276f
Init deterministic lib
wwoytenko Mar 1, 2024
d7750fa
Saved changes
wwoytenko Mar 1, 2024
ab7035f
Implemented random generator for int values
wwoytenko Mar 2, 2024
9fdf89a
Changed layout
wwoytenko Mar 2, 2024
007862b
Changed layout
wwoytenko Mar 2, 2024
5981859
New transformation framework
wwoytenko Mar 3, 2024
e0949e0
Saved changes
wwoytenko Mar 4, 2024
c2544ca
Pulled from deterministic lib
wwoytenko Mar 4, 2024
b3a712d
First deterministic transformer implementation
wwoytenko Mar 4, 2024
cb6c57b
Fixed integer transformer initialization and salt value parsing. Min …
wwoytenko Mar 5, 2024
c1f08d3
Implemented Decimal transformer support
wwoytenko Mar 5, 2024
583b85e
BigInt transformer implementation
wwoytenko Mar 6, 2024
9e1a26d
Fixed dynamic params for big int
wwoytenko Mar 6, 2024
b3d6d50
Re-designed unified transformers added tests from big_integer
wwoytenko Mar 9, 2024
3144369
Implemented common random date transformer
wwoytenko Mar 9, 2024
9ca0561
Fixed errors
wwoytenko Mar 9, 2024
854a4c0
Merged with the latest release v0.1.7
wwoytenko Mar 16, 2024
2c44954
Implemented Timestamp transformer
wwoytenko Mar 16, 2024
0074b48
Refactored engine usage logic
wwoytenko Mar 17, 2024
ff8fe01
Fixed Integer transformer size
wwoytenko Mar 17, 2024
d7101a8
Fixed unit tests
wwoytenko Mar 17, 2024
ce77a23
Implemented Float transformer and fixed hash reducer
wwoytenko Mar 18, 2024
2f7a3a6
Merge remote-tracking branch 'origin/main' into deterministic_lib_v0.2
wwoytenko Mar 22, 2024
e1480c0
New transformers and removed artifacts
wwoytenko Mar 22, 2024
5ea1b46
Bunch of changes
wwoytenko Apr 8, 2024
e8c848b
NoiseDate transformer implementation
wwoytenko Apr 10, 2024
95f71ad
Added template support for static parameter
wwoytenko Apr 17, 2024
582e412
Implemented NoiseFloat and NoiseNumeric transformers
wwoytenko Apr 22, 2024
4feaa05
Implemented Hybrid generator (random + hash) and fixed unit test
wwoytenko Apr 22, 2024
4f252aa
feat: Email transformer is implemented
wwoytenko May 1, 2024
2f1dd67
fix: Fixed email transformer UX
wwoytenko May 2, 2024
3515ecf
fix: Added lost RoRecordContext
wwoytenko May 2, 2024
883f786
merge: Pulled from main
wwoytenko May 2, 2024
31840ad
merge: Pulled from main
wwoytenko May 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/greenmask/cmd/list_transformers/list_transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func listTransformersText(registry *utils.TransformerRegistry) error {
return nil
}

func getColumnTypes(p *toolkit.Parameter) []string {
func getColumnTypes(p *toolkit.ParameterDefinition) []string {
if p.ColumnProperties != nil && len(p.ColumnProperties.AllowedTypes) > 0 {
return p.ColumnProperties.AllowedTypes
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/greenmask/cmd/show_transformer/show_transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func run(name string) error {
}

func showTransformerJson(registry *utils.TransformerRegistry, transformerName string) error {
var transformers []*utils.Definition
var transformers []*utils.TransformerDefinition

def, ok := registry.M[transformerName]
if ok {
Expand Down Expand Up @@ -114,8 +114,8 @@ func showTransformerText(registry *utils.TransformerRegistry, name string) error
if p.DefaultValue != nil {
data = append(data, []string{def.Properties.Name, "parameters", p.Name, "default", string(p.DefaultValue), ""})
}
if p.LinkParameter != "" {
data = append(data, []string{def.Properties.Name, "parameters", p.Name, "linked_parameter", p.LinkParameter, ""})
if p.LinkColumnParameter != "" {
data = append(data, []string{def.Properties.Name, "parameters", p.Name, "linked_parameter", p.LinkColumnParameter, ""})
}
if p.CastDbType != "" {
data = append(data, []string{def.Properties.Name, "parameters", p.Name, "cast_to_db_type", p.CastDbType, ""})
Expand Down Expand Up @@ -146,7 +146,7 @@ func showTransformerText(registry *utils.TransformerRegistry, name string) error
return nil
}

func getTransformerDefinition(registry *utils.TransformerRegistry, name string) (*utils.Definition, error) {
func getTransformerDefinition(registry *utils.TransformerRegistry, name string) (*utils.TransformerDefinition, error) {
def, ok := registry.M[name]
if ok {
return def, nil
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/aws/aws-sdk-go v1.51.14
github.com/dchest/siphash v1.2.3
github.com/ggwhite/go-masker v1.1.0
github.com/go-faker/faker/v4 v4.4.0
github.com/google/uuid v1.6.0
Expand All @@ -15,6 +16,8 @@ require (
github.com/olekukonko/tablewriter v0.0.5
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.32.0
github.com/shopspring/decimal v1.3.1
github.com/spaolacci/murmur3 v1.1.0
github.com/spf13/cast v1.6.0
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
Expand Down Expand Up @@ -48,7 +51,6 @@ require (
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dchest/siphash v1.2.3 h1:QXwFc8cFOR2dSa/gE6o/HokBMWtLUaNDVd+22aKHeEA=
github.com/dchest/siphash v1.2.3/go.mod h1:0NvQU092bT0ipiFN++/rXm69QG9tVxLAlQHIXMPAkHc=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
Expand Down Expand Up @@ -93,6 +95,8 @@ github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5g
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
Expand Down
2 changes: 1 addition & 1 deletion internal/db/postgres/cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (v *Validate) dumpTables(ctx context.Context) error {
var tablesWithTransformers []entries.Entry
for _, item := range v.context.DataSectionObjects {

if t, ok := item.(*entries.Table); ok && len(t.Transformers) > 0 {
if t, ok := item.(*entries.Table); ok && len(t.TransformersContext) > 0 {
t.ValidateLimitedRecords = v.config.Validate.RowsLimit
tablesWithTransformers = append(tablesWithTransformers, t)
}
Expand Down
8 changes: 5 additions & 3 deletions internal/db/postgres/cmd/validate_utils/json_document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestJsonDocument_GetRecords(t *testing.T) {
// panic(err.Error())
//}
//row := pgcopy.NewRow(1)
//_ = row.Decode([]byte(value))
//_ = row.DecodeValue([]byte(value))
//r := toolkit.NewRecord(
// driver,
//)
Expand Down Expand Up @@ -176,8 +176,10 @@ func getTableAndRows() (table *entries.Table, original, transformed [][]byte) {
}

table = &entries.Table{
Table: t,
Transformers: []utils.Transformer{&testTransformer{}},
Table: t,
TransformersContext: []*utils.TransformerContext{
{Transformer: &testTransformer{}},
},
}

return table, original, transformed
Expand Down
4 changes: 2 additions & 2 deletions internal/db/postgres/cmd/validate_utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const nullStringValue = "NULL"

func getAffectedColumns(t *entries.Table) map[string]struct{} {
affectedColumns := make(map[string]struct{})
for _, tr := range t.Transformers {
ac := tr.GetAffectedColumns()
for _, tr := range t.TransformersContext {
ac := tr.Transformer.GetAffectedColumns()
for _, name := range ac {
affectedColumns[name] = struct{}{}
}
Expand Down
13 changes: 13 additions & 0 deletions internal/db/postgres/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package context

import (
"context"
"encoding/hex"
"fmt"
"os"
"slices"

"github.com/jackc/pgx/v5"
Expand Down Expand Up @@ -57,6 +59,17 @@ func NewRuntimeContext(
ctx context.Context, tx pgx.Tx, cfg []*domains.Table, r *transformersUtils.TransformerRegistry, opt *pgdump.Options,
version int,
) (*RuntimeContext, error) {
var salt []byte
saltHex := os.Getenv("GREENMASK_GLOBAL_SALT")
if saltHex != "" {
salt = make([]byte, hex.DecodedLen(len(saltHex)))
_, err := hex.Decode(salt, []byte(saltHex))
if err != nil {
return nil, fmt.Errorf("error decoding salt from hex: %w", err)
}
}
ctx = context.WithValue(ctx, "salt", salt)

typeMap := tx.Conn().TypeMap()
types, err := getCustomTypesUsedInTables(ctx, tx)
if err != nil {
Expand Down
9 changes: 6 additions & 3 deletions internal/db/postgres/context/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ var (
a.atttypid::TEXT::INT as typeoid,
pg_catalog.format_type(a.atttypid, a.atttypmod) as typename,
a.attnotnull as notnull,
a.atttypmod as mod,
a.attnum as num
a.atttypmod as att_len,
a.attnum as num,
t.typlen as type_len
{{ if ge .Version 120000 }}
,a.attgenerated != '' as attgenerated
{{ end }}
FROM pg_catalog.pg_attribute a
JOIN pg_catalog.pg_type t ON a.atttypid = t.oid
WHERE a.attrelid = $1 AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
`))
Expand Down Expand Up @@ -116,7 +118,8 @@ var (
GROUP BY ct.oid
)
SELECT pt.oid::TEXT::INT AS oid,
twc.chain::INT[] AS chain,
twc.chain::INT[] AS chain_oids,
(select array_agg(t.typname)::TEXT[] FROM pg_type t WHERE t.oid = ANY (twc.chain)) AS chain_names,
pn.nspname AS schema,
pt.typname AS name,
pt.typlen AS len,
Expand Down
34 changes: 31 additions & 3 deletions internal/db/postgres/context/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,37 @@ func validateAndBuildTablesConfig(
}
table.Constraints = constraints

// Assigning overridden column types for driver initialization
if tableCfg.ColumnsTypeOverride != nil {
for _, c := range table.Columns {
overridingType, ok := tableCfg.ColumnsTypeOverride[c.Name]
if ok {
c.OverriddenTypeName = overridingType
}
}
}

// Assign columns and transformersMap if were found
columns, err := getColumnsConfig(ctx, tx, table.Oid, version)
if err != nil {
return nil, nil, err
}
table.Columns = columns

driver, err := toolkit.NewDriver(table.Table, types, tableCfg.ColumnsTypeOverride)
driver, driverWarnings, err := toolkit.NewDriver(table.Table, types)
if err != nil {
return nil, nil, fmt.Errorf("unnable to initialise driver: %w", err)
}
table.Driver = driver

if len(driverWarnings) > 0 {
for _, w := range driverWarnings {
w.AddMeta("SchemaName", table.Schema).
AddMeta("TableName", table.Name)
}
warnings = append(warnings, driverWarnings...)
}

// InitTransformation toolkit
if len(tableCfg.Transformers) > 0 {
for _, tc := range tableCfg.Transformers {
Expand All @@ -84,11 +102,13 @@ func validateAndBuildTablesConfig(

}
}
// Not only errors might be in driver initialization but also a warnings that's why we have to add
// append medata to validation warnings and the check error and return error with warnings
if err != nil {
return nil, warnings, err
}
warnings = append(warnings, initWarnings...)
table.Transformers = append(table.Transformers, transformer)
table.TransformersContext = append(table.TransformersContext, transformer)
}
}

Expand Down Expand Up @@ -178,6 +198,7 @@ func getTable(ctx context.Context, tx pgx.Tx, t *domains.Table) ([]*entries.Tabl
}

func getColumnsConfig(ctx context.Context, tx pgx.Tx, oid toolkit.Oid, version int) ([]*toolkit.Column, error) {
defaultTypeMap := pgtype.NewMap()
var res []*toolkit.Column
buf := bytes.NewBuffer(nil)
err := TableColumnsQuery.Execute(
Expand All @@ -198,14 +219,21 @@ func getColumnsConfig(ctx context.Context, tx pgx.Tx, oid toolkit.Oid, version i
column := toolkit.Column{Idx: idx}
if version >= 120000 {
err = rows.Scan(&column.Name, &column.TypeOid, &column.TypeName,
&column.NotNull, &column.Length, &column.Num, &column.IsGenerated)
&column.NotNull, &column.Length, &column.Num, &column.TypeLength, &column.IsGenerated)
} else {
err = rows.Scan(&column.Name, &column.TypeOid, &column.TypeName,
&column.NotNull, &column.Length, &column.Num)
}
if err != nil {
return nil, fmt.Errorf("cannot scan tableColumnQuery: %w", err)
}
column.CanonicalTypeName = column.TypeName
// Getting canonical type name if exists. For instance - PostgreSQL type Integer is alias for int4
// (int4 - canonical type name)
canonicalType, ok := defaultTypeMap.TypeForOID(uint32(column.TypeOid))
if ok {
column.CanonicalTypeName = canonicalType.Name
}
res = append(res, &column)
idx++
}
Expand Down
4 changes: 2 additions & 2 deletions internal/db/postgres/context/transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func initTransformer(
c *domains.TransformerConfig,
r *transformersUtils.TransformerRegistry,
types []*toolkit.Type,
) (transformersUtils.Transformer, toolkit.ValidationWarnings, error) {
) (*transformersUtils.TransformerContext, toolkit.ValidationWarnings, error) {
var totalWarnings toolkit.ValidationWarnings
td, ok := r.Get(c.Name)
if !ok {
Expand All @@ -42,7 +42,7 @@ func initTransformer(
}))
return nil, totalWarnings, nil
}
transformer, warnings, err := td.Instance(ctx, d, c.Params, types)
transformer, warnings, err := td.Instance(ctx, d, c.Params, c.DynamicParams)
if err != nil {
return nil, nil, fmt.Errorf("unable to init transformer: %w", err)
}
Expand Down
7 changes: 4 additions & 3 deletions internal/db/postgres/context/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func getCustomTypesUsedInTables(ctx context.Context, tx pgx.Tx) ([]*toolkit.Type
for rows.Next() {
t := &toolkit.Type{}
var hasDomainConstraint bool
err = rows.Scan(&t.Oid, &t.Chain, &t.Schema, &t.Name, &t.Length, &t.Kind,
err = rows.Scan(&t.Oid, &t.ChainOids, &t.ChainNames, &t.Schema, &t.Name, &t.Length, &t.Kind,
&t.ComposedRelation, &t.ElementType, &t.ArrayType, &t.NotNull, &t.BaseType,
&hasDomainConstraint)
if err != nil {
Expand All @@ -45,8 +45,9 @@ func getCustomTypesUsedInTables(ctx context.Context, tx pgx.Tx) ([]*toolkit.Type
if hasDomainConstraint {
domainsWithConstraint = append(domainsWithConstraint, t)
}
if t.Kind == 'd' && len(t.Chain) > 0 {
t.RootBuiltInType = t.Chain[len(t.Chain)-1]
if t.Kind == 'd' && len(t.ChainOids) > 0 {
t.RootBuiltInTypeOid = t.ChainOids[len(t.ChainOids)-1]
t.RootBuiltInTypeName = t.ChainNames[len(t.ChainNames)-1]
}
_, exists := tx.Conn().TypeMap().TypeForOID(uint32(t.Oid))
if !exists {
Expand Down
2 changes: 1 addition & 1 deletion internal/db/postgres/dumpers/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (td *TableDumper) Execute(ctx context.Context, tx pgx.Tx, st storages.Stora
func() error {
var pipeline Pipeliner
var err error
if len(td.table.Transformers) > 0 {
if len(td.table.TransformersContext) > 0 {
if td.validate {
pipeline, err = NewValidationPipeline(gtx, eg, td.table, w)
if err != nil {
Expand Down
Loading
Loading