Skip to content

Commit

Permalink
Add support for negative lookups to constraints. (#38)
Browse files Browse the repository at this point in the history
Fixes #21
  • Loading branch information
Clorith authored Nov 11, 2024
1 parent bf9c3f7 commit 308bb40
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
35 changes: 31 additions & 4 deletions internal/anonymize/anonymize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions internal/embed/files/config.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 308bb40

Please sign in to comment.