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

[WIP] Add additional data types #8

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ jobs:
#### expecting it in the form of
#### /go/src/github.com/circleci/go-tool
#### /go/src/bitbucket.org/circleci/go-tool
working_directory: /go/src/github.com/humanmade/go-anonymize-mysqldump
working_directory: /go/src/github.com/Lusitaniae/go-anonymize-mysqldump
steps:
- checkout

# specify any bash command here prefixed with `run: `
- run: make vet
- run: make test
- run: go build

- store_artifacts:
path: go-anonymize-mysqldump
55 changes: 45 additions & 10 deletions anonymize-mysqldump.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type PatternField struct {
Field string `json:"field"`
Position int `json:"position"`
Type string `json:"type"`
String string `json:"string"`
Constraints []PatternFieldConstraint `json:"constraints"`
}

Expand All @@ -38,15 +39,38 @@ type PatternFieldConstraint struct {

var (
transformationFunctionMap = map[string]func(*sqlparser.SQLVal) *sqlparser.SQLVal{
"username": generateUsername,
"password": generatePassword,
"email": generateEmail,
"url": generateURL,
"name": generateName,
"firstName": generateFirstName,
"lastName": generateLastName,
"paragraph": generateParagraph,
"ipv4": generateIPv4,
"username": generateUsername,
"password": generatePassword,
"email": generateEmail,
"url": generateURL,
"name": generateName,
"firstName": generateFirstName,
"lastName": generateLastName,
"paragraph": generateParagraph,
"ipv4": generateIPv4,
"customPhoneNumber": generateCustomPhoneNumber,
"customUserAgent": generateCustomUserAgent,
"libPrefix": generateLibPrefix,
"libCompanyName": generateLibCompanyName,
"libStreet": generateLibStreet,
"customStreet": generateCustomStreet,
"libBuildingNumber": generateLibBuildingNumber,
"libCity": generateLibCity,
"libZip": generateLibZip,
"libState": generateLibState,
"libCountry": generateLibCountry,
"customCountry": generateCustomCountry,
"libParagraph": generateLibParagraph,
"customSocialNetwork": generateCustomSocialNetwork,
"customSocialId": generateCustomSocialId,
"customSocialToken": generateCustomSocialToken,
"libInternetUser": generateLibInternetUser,
"customUniqueUser": generateCustomUniqueUser,
"customPassword": generateCustomPassword,
"customRecoverToken": generateCustomRecoverToken,
"customUserToken": generateCustomUserToken,
"libAdditionalAddress": generateLibAdditionalAddress,
"customTitle": generateCustomTitle,
}
)

Expand Down Expand Up @@ -296,7 +320,18 @@ func modifyValues(values sqlparser.Values, pattern ConfigPattern) (sqlparser.Val
// Position is 1 indexed instead of 0, so let's subtract 1 in order to get
// it to line up with the value inside the ValTuple inside of values.Values
valTupleIndex := fieldPattern.Position - 1
value := values[row][valTupleIndex].(*sqlparser.SQLVal)
value, isNotNull := values[row][valTupleIndex].(*sqlparser.SQLVal)

// Skip transformation of null values
if !isNotNull {
continue
}

// Use provided string as value
if fieldPattern.Type == "custom" {
values[row][valTupleIndex] = generateCustomString(value, fieldPattern.String)
continue;
}

// Skip transformation if transforming function doesn't exist
if transformationFunctionMap[fieldPattern.Type] == nil {
Expand Down
4 changes: 2 additions & 2 deletions anonymize-mysqldump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ var (
(3,1,'foobar','bazquz'),
(4,1,'nickname','Jim'),
(5,1,'description','Lorum ipsum.');`
multilineQueryRecompiled = "insert into wp_usermeta values (1, 1, 'first_name', 'Jazmyn'), (2, 1, 'last_name', 'Reynolds'), (3, 1, 'foobar', 'bazquz'), (4, 1, 'nickname', 'Sherman'), (5, 1, 'description', 'Vel at et.');\n"
multilineQueryRecompiled = "insert into wp_usermeta values (1, 1, 'first_name', 'Enos'), (2, 1, 'last_name', 'Ebert'), (3, 1, 'foobar', 'bazquz'), (4, 1, 'nickname', 'Ara'), (5, 1, 'description', 'Voluptas molestiae facilis.');\n"
commentsQuery = "INSERT INTO `wp_comments` VALUES (1,1,'A WordPress Commenter','[email protected]','https://wordpress.org/','','2019-06-12 00:59:19','2019-06-12 00:59:19','Hi, this is a comment.\\nTo get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.\\nCommenter avatars come from <a href=\\\"https://gravatar.com\\\">Gravatar</a>.',0,'1','','',0,0);\n"
commentsQueryRecompiled = "insert into wp_comments values (1, 1, 'kamren.ohara', '[email protected]', 'http://ebert.com/korey_keeling', '', '2019-06-12 00:59:19', '2019-06-12 00:59:19', 'Hi, this is a comment.\\nTo get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.\\nCommenter avatars come from <a href=\\\"https://gravatar.com\\\">Gravatar</a>.', 0, '1', '', '', 0, 0);\n"
commentsQueryRecompiled = "insert into wp_comments values (1, 1, 'kamren.ohara', '[email protected]', 'https://example.com/', '', '2019-06-12 00:59:19', '2019-06-12 00:59:19', 'Hi, this is a comment.\\nTo get started with moderating, editing, and deleting comments, please visit the Comments screen in the dashboard.\\nCommenter avatars come from <a href=\\\"https://gravatar.com\\\">Gravatar</a>.', 0, '1', '', '', 0, 0);\n"
usersQuery = "INSERT INTO `wp_users` VALUES (1,'username','user_pass','username','[email protected]','','2019-06-12 00:59:19','',0,'username'),(2,'username','user_pass','username','[email protected]','http://notreal.com/username','2019-06-12 00:59:19','',0,'username');\n"
usersQueryRecompiled = "insert into wp_users values (1, 'treva_cremin', 'NjaK5HeMAMuv', 'hailey', '[email protected]', '', '2019-06-12 00:59:19', '', 0, 'Kylie Rice'), (2, 'eduardo', 'J3JRQ4XoIxXX6A', 'albert.okeefe', '[email protected]', 'http://pfannerstill.net/brando', '2019-06-12 00:59:19', '', 0, 'Ardella Jenkins PhD');\n"
userMetaQuery = "INSERT INTO `wp_usermeta` VALUES (1,1,'first_name','John'),(2,1,'last_name','Doe'),(3,1,'foobar','bazquz'),(4,1,'nickname','Jim'),(5,1,'description','Lorum ipsum.'),(6,2,'first_name','Janet'),(7,2,'last_name','Doe'),(8,2,'foobar','bazquz'),(9,2,'nickname','Jane'),(10,2,'description','Lorum ipsum.');\n"
Expand Down
3 changes: 2 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@
{
"field": "comment_author_url",
"position": 5,
"type": "url",
"type": "custom",
"string": "https://example.com/",
"constraints": null
},
{
Expand Down
Binary file added go-anonymize-mysqldump
Binary file not shown.
101 changes: 100 additions & 1 deletion transformation-functions.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package main

import (
"github.com/xwb1989/sqlparser"
"fmt"
"syreclabs.com/go/faker"
"github.com/xwb1989/sqlparser"
"math/rand"
"strings"
)

func generateUsername(value *sqlparser.SQLVal) *sqlparser.SQLVal {
Expand Down Expand Up @@ -38,6 +41,102 @@ func generateParagraph(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.Lorem().Sentence(3)))
}

func generateCustomTitle(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.Lorem().Word()))
}

func generateIPv4(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.Internet().IpV4Address()))
}

func generateCustomPhoneNumber(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(fmt.Sprintf("0%v", faker.Number().Number(rand.Intn(12-7)+7))))
}

func generateCustomUserAgent(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/601.3.9 (KHTML, like Gecko) Version/9.0.2 Safari/601.3.9"))
}

func generateLibPrefix(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.Name().Prefix()))
}

func generateLibCompanyName(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.Company().Name()))
}

func generateLibStreet(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.Address().StreetName()))
}

func generateCustomStreet(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.Address().StreetName()))
}

func generateLibBuildingNumber(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.Address().BuildingNumber()))
}

func generateLibAdditionalAddress(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.Address().SecondaryAddress()))
}

func generateLibCity(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.Address().City()))
}

func generateLibZip(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.Address().ZipCode()))
}

func generateLibState(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.Address().State()))
}

func generateLibCountry(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.Address().Country()))
}

func generateCustomCountry(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte("Deutschland"))
}

func generateLibParagraph(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.Lorem().Paragraph(3)))
}

func generateCustomSocialNetwork(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.RandomChoice([]string{"Google", "Facebook"})))
}

func generateCustomSocialId(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte("REDACTED"))
}

func generateCustomSocialToken(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte("REDACTED"))
}

func generateLibInternetUser(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.Internet().UserName()))
}

func generateCustomUniqueUser(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(fmt.Sprintf("%v.%v", faker.RandomString(16), faker.Internet().SafeEmail())))
}

func generateCustomPassword(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(strings.ToLower(faker.Internet().Password(64, 64))))
}

func generateCustomRecoverToken(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.Internet().Password(30, 30)))
}

func generateCustomUserToken(value *sqlparser.SQLVal) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(faker.Internet().Password(8, 20)))
}

func generateCustomString(value *sqlparser.SQLVal, string string) *sqlparser.SQLVal {
return sqlparser.NewStrVal([]byte(string))
}