Skip to content

Commit

Permalink
Merge pull request #22 from nixys/fix/19
Browse files Browse the repository at this point in the history
fix(#19): Preserve MySQL `CREATE TABLE` statement
  • Loading branch information
borisershov authored Jun 7, 2024
2 parents 11afbc7 + 8e5cb1e commit 215a9db
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 78 deletions.
66 changes: 22 additions & 44 deletions modules/anonymizers/mysql/dh.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import (
"github.com/nixys/nxs-data-anonymizer/modules/filters/relfilter"
)

func dhSecurityCreateTable(usrCtx any, deferred, token []byte) ([]byte, error) {
func dhSecurityInsertInto(usrCtx any, deferred, token []byte) ([]byte, error) {

uctx := usrCtx.(*userCtx)

uctx.security.tmpBuf = append(uctx.security.tmpBuf, token...)
uctx.security.tmpBuf = token

return deferred, nil
}

func dhSecurityCreateTableName(usrCtx any, deferred, token []byte) ([]byte, error) {
func dhSecurityInsertIntoTableNameSearch(usrCtx any, deferred, token []byte) ([]byte, error) {

uctx := usrCtx.(*userCtx)

Expand All @@ -42,37 +42,14 @@ func dhSecurityNil(usrCtx any, deferred, token []byte) ([]byte, error) {
func dhCreateTableName(usrCtx any, deferred, token []byte) ([]byte, error) {

uctx := usrCtx.(*userCtx)
uctx.filter.TableCreate(string(deferred))

tn := string(deferred)

// Check table pass through security rules
if !securityPolicyCheck(uctx, tn) {

// If not: table will be skipped from result dump

uctx.security.isSkip = true
uctx.security.tmpBuf = []byte{}
return []byte{}, nil
}

uctx.filter.TableCreate(tn)

d := append(uctx.security.tmpBuf, append(deferred, token...)...)

uctx.security.isSkip = false
uctx.security.tmpBuf = []byte{}

return d, nil
return append(deferred, token...), nil
}

func dhCreateTableFieldName(usrCtx any, deferred, token []byte) ([]byte, error) {

uctx := usrCtx.(*userCtx)

if uctx.security.isSkip == true {
return []byte{}, nil
}

uctx.column.name = string(deferred)

return append(deferred, token...), nil
Expand All @@ -82,10 +59,6 @@ func dhCreateTableColumnTypeAdd(usrCtx any, deferred, token []byte) ([]byte, err

uctx := usrCtx.(*userCtx)

if uctx.security.isSkip == true {
return []byte{}, nil
}

for k, v := range typeKeys {
if k == "generated" {
if k == string(token) || strings.ToUpper(k) == string(token) {
Expand All @@ -100,21 +73,13 @@ func dhCreateTableColumnTypeAdd(usrCtx any, deferred, token []byte) ([]byte, err
}
}

if uctx.column.columnType == "" {
fmt.Println("token:", token)
}

return append(deferred, token...), nil
}

func dhCreateTableColumnAdd(usrCtx any, deferred, token []byte) ([]byte, error) {

uctx := usrCtx.(*userCtx)

if uctx.security.isSkip == true {
return []byte{}, nil
}

if uctx.column.isSkip == false {
uctx.filter.ColumnAdd(uctx.column.name, uctx.column.columnType)
}
Expand All @@ -128,16 +93,29 @@ func dhInsertIntoTableName(usrCtx any, deferred, token []byte) ([]byte, error) {

uctx := usrCtx.(*userCtx)

if uctx.security.isSkip == true {
tn := string(deferred)

// Check table pass through security rules
if !securityPolicyCheck(uctx, tn) {

// If not: table will be skipped from result dump

uctx.security.isSkip = true
uctx.security.tmpBuf = []byte{}
return []byte{}, nil
}

d := append(uctx.security.tmpBuf, append(deferred, token...)...)

uctx.security.isSkip = false
uctx.security.tmpBuf = []byte{}

// Check insert into table name
if bytes.Compare([]byte(uctx.filter.TableNameGet()), deferred) != 0 {
return append(deferred, token...), fmt.Errorf("`create` and `insert into` table names are mismatch (create table: '%s', insert into table: '%s')", uctx.filter.TableNameGet(), string(deferred))
if tn != uctx.filter.TableNameGet() {
return d, fmt.Errorf("`create` and `insert into` table names are mismatch (create table: '%s', insert into table: '%s')", uctx.filter.TableNameGet(), tn)
}

return append(deferred, token...), nil
return d, nil
}

func dhCreateTableValues(usrCtx any, deferred, token []byte) ([]byte, error) {
Expand Down
51 changes: 18 additions & 33 deletions modules/anonymizers/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func Init(ctx context.Context, r io.Reader, s InitSettings) io.Reader {
R: []byte{' '},
},
},
DataHandler: dhSecurityCreateTable,
DataHandler: nil,
},
},
},
Expand All @@ -158,7 +158,7 @@ func Init(ctx context.Context, r io.Reader, s InitSettings) io.Reader {
R: []byte{' '},
},
},
DataHandler: dhSecurityCreateTableName,
DataHandler: nil,
},
},
},
Expand All @@ -169,7 +169,7 @@ func Init(ctx context.Context, r io.Reader, s InitSettings) io.Reader {
Switch: fsm.Switch{
Trigger: []byte("`"),
},
DataHandler: dhSecurityCreateTableName,
DataHandler: nil,
},
},
},
Expand All @@ -191,7 +191,7 @@ func Init(ctx context.Context, r io.Reader, s InitSettings) io.Reader {
Switch: fsm.Switch{
Trigger: []byte("("),
},
DataHandler: dhSecurityNil,
DataHandler: nil,
},
},
},
Expand All @@ -207,7 +207,7 @@ func Init(ctx context.Context, r io.Reader, s InitSettings) io.Reader {
R: []byte{' '},
},
},
DataHandler: dhSecurityNil,
DataHandler: nil,
},
{
// Skip table keys description
Expand All @@ -219,7 +219,7 @@ func Init(ctx context.Context, r io.Reader, s InitSettings) io.Reader {
R: []byte{' '},
},
},
DataHandler: dhSecurityNil,
DataHandler: nil,
},
{
// Skip table keys description
Expand All @@ -231,7 +231,7 @@ func Init(ctx context.Context, r io.Reader, s InitSettings) io.Reader {
R: []byte{' '},
},
},
DataHandler: dhSecurityNil,
DataHandler: nil,
},
{
// Skip table keys description
Expand All @@ -243,7 +243,7 @@ func Init(ctx context.Context, r io.Reader, s InitSettings) io.Reader {
R: []byte{' '},
},
},
DataHandler: dhSecurityNil,
DataHandler: nil,
},
{
// Skip table keys description
Expand All @@ -255,14 +255,14 @@ func Init(ctx context.Context, r io.Reader, s InitSettings) io.Reader {
R: []byte{' '},
},
},
DataHandler: dhSecurityNil,
DataHandler: nil,
},
{
Name: stateFieldsDescriptionName,
Switch: fsm.Switch{
Trigger: []byte("`"),
},
DataHandler: dhSecurityNil,
DataHandler: nil,
},
},
},
Expand All @@ -276,7 +276,7 @@ func Init(ctx context.Context, r io.Reader, s InitSettings) io.Reader {
R: []byte{'\n'},
},
},
DataHandler: dhSecurityNil,
DataHandler: nil,
},
{
Name: statefFieldsDescriptionBlockEnd,
Expand All @@ -286,7 +286,7 @@ func Init(ctx context.Context, r io.Reader, s InitSettings) io.Reader {
L: []byte{'\n'},
},
},
DataHandler: dhSecurityNil,
DataHandler: nil,
},
},
},
Expand Down Expand Up @@ -363,7 +363,7 @@ func Init(ctx context.Context, r io.Reader, s InitSettings) io.Reader {
R: []byte{'\n'},
},
},
DataHandler: dhSecurityNil,
DataHandler: nil,
},
},
},
Expand All @@ -378,45 +378,30 @@ func Init(ctx context.Context, r io.Reader, s InitSettings) io.Reader {
R: []byte{' '},
},
},
DataHandler: dhSecurityCreateTable,
DataHandler: nil,
},
{
Name: stateInsertInto,
Name: stateInsertIntoTableNameSearch,
Switch: fsm.Switch{
Trigger: []byte("INSERT"),
Trigger: []byte("INSERT INTO"),
Delimiters: fsm.Delimiters{
L: []byte{'\n'},
R: []byte{' '},
},
},
DataHandler: dhSecurityNil,
DataHandler: dhSecurityInsertInto,
},
},
},

stateInsertInto: {
NextStates: []fsm.NextState{
{
Name: stateInsertIntoTableNameSearch,
Switch: fsm.Switch{
Trigger: []byte("INTO"),
Delimiters: fsm.Delimiters{
L: []byte{' '},
R: []byte{' '},
},
},
DataHandler: dhSecurityNil,
},
},
},
stateInsertIntoTableNameSearch: {
NextStates: []fsm.NextState{
{
Name: stateInsertIntoTableName,
Switch: fsm.Switch{
Trigger: []byte("`"),
},
DataHandler: dhSecurityNil,
DataHandler: dhSecurityInsertIntoTableNameSearch,
},
},
},
Expand Down
1 change: 0 additions & 1 deletion modules/anonymizers/mysql/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ var (
stateFieldDescriptionTailSkip = fsm.StateName("fields description tail skip")
statefFieldsDescriptionBlockEnd = fsm.StateName("fields description block end")
stateSomeIntermediateState = fsm.StateName("some intermediate state")
stateInsertInto = fsm.StateName("insert into")
stateInsertIntoTableNameSearch = fsm.StateName("insert into table name search")
stateInsertIntoTableName = fsm.StateName("insert into table name")
stateValuesSearch = fsm.StateName("values search")
Expand Down

0 comments on commit 215a9db

Please sign in to comment.