diff --git a/README.md b/README.md index 1c3a073..88f7562 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ The config is composed of many objects in the `patterns` array: - `field`: a string representing the name of the field. - `position`: the 1-based index of what number column this field represents. For instance, assuming a table with 3 columns `foo`, `bar`, and `baz`, and you wished to modify the `bar` column, this value would be `2`. - `value`: string value to match against. + - `compare`: An optional string stating how to treat the constraints. ### Constraints @@ -126,13 +127,23 @@ Supposing you have a WordPress database and you need to modify certain meta, be { "field": "meta_key", "position": 3, - "value": "last_ip_address" + "value": "last_ip_address", + "compare": "like" } ] } - ``` +#### Compare rules +Constraints allow the user to define rules for how to treat the comparison value. The following rules are supported: + +**PS: Remember that comparison rules are first come first serve, so as soon as a rule that would negate the anonymization of a field is found, it will short-circuit any further rules. You should also try to avoid comapring against other fields, remember that a field you may wish to compare against may already have been modified and no longer give the expected value!** + +- `like`: The default behavior. The SQL value must be equal to the constraint `value` field. +- `not like`: The SQL value must not be equal to the constraint `value` field. +- `regex`: The SQL value must match the regex string given in the `value` field. +- `regex not like`: The inverse of `regex`, and requires the regex patter to not match the SQL value. + ### Field Types Each column stores a certain type of data, be it a name, username, email, etc. The `type` property in the config is used to define the type of data stored, and ultimately the type of random data to be inserted into the field. [https://github.com/dmgk/faker](https://github.com/dmgk/faker) is used for generating the fake data. These are the types currently supported: diff --git a/internal/anonymize/anonymize.go b/internal/anonymize/anonymize.go index fbe97ae..a06cdbc 100644 --- a/internal/anonymize/anonymize.go +++ b/internal/anonymize/anonymize.go @@ -357,10 +357,37 @@ func rowObeysConstraints(constraints []config.PatternFieldConstraint, row sqlpar parsedValue := convertSQLValToString(value) // TODO: Add behing a flag for debugging. - //log.Printf("Error: Constraint obediance, parsed value: %s, constraint value: %s.", parsedValue, constraint.Value) - - if parsedValue != constraint.Value { - return false + //log.Printf("Error: Constraint obediance, parsed value: %s, constraint value: %s, and comparator: %s.", parsedValue, constraint.Value, constraint.Compare) + + switch constraint.Compare { + case "not like", + "<>", + "!=": + if parsedValue == constraint.Value { + return false + } + case "regex not like": + re := regexp.MustCompile(constraint.Value) + match := re.MatchString(parsedValue) + if match { + return false + } + case "regex": + re := regexp.MustCompile(constraint.Value) + match := re.MatchString(parsedValue) + if !match { + return false + } + case "like", + "==", + "=": + if parsedValue != constraint.Value { + return false + } + default: + if parsedValue != constraint.Value { + return false + } } } return true diff --git a/internal/config/config.go b/internal/config/config.go index 96a7b37..7f550da 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -38,6 +38,7 @@ type PatternFieldConstraint struct { Field string `json:"field"` Position int `json:"position"` Value string `json:"value"` + Compare string `json:"compare"` } // New creates a new Config from flags and environment variables diff --git a/internal/embed/files/config.default.json b/internal/embed/files/config.default.json index c78cde9..a159bd7 100644 --- a/internal/embed/files/config.default.json +++ b/internal/embed/files/config.default.json @@ -7,13 +7,27 @@ "field": "user_login", "position": 2, "type": "username", - "constraints": null + "constraints": [ + { + "field": "user_login", + "position": 2, + "value": "myaccount", + "compare": "not like" + } + ] }, { "field": "user_pass", "position": 3, "type": "password", - "constraints": null + "constraints": [ + { + "field": "user_login", + "position": 2, + "value": "myaccount", + "compare": "not like" + } + ] }, { "field": "user_nicename",