-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
216 lines (185 loc) · 6.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"database/sql"
"errors"
"flag"
"fmt"
"html/template"
"os"
"path/filepath"
"github.com/vicdeo/go-obfuscate/config"
"github.com/vicdeo/go-obfuscate/mysqldump"
)
const (
version = "0.9.0"
errConfigFileNotFound = 1
errConfigFileInvalidMarkUp = 2
errOutputDirectoryMissing = 3
errDBConnectionFailed = 4
errShowTablesFailed = 5
errDumpFileIsNotWritable = 6
errConfigHasDuplicates = 7
errConfigIncomplete = 8
errConfigHasUnknownType = 9
statsTemplate = `Config parsed. Found tables count:
- to dump as is: {{.keep}}
- to ignore: {{.ignore}}
- to truncate: {{.truncate}}
- to obfuscate: {{.obfuscate}}
Total: {{.total}}
`
validationTemplate = `Checking for duplicated table names...done
{{range $k, $v := .}}{{if $v}} - {{range $v}}{{.}}{{end}} spotted multiple times in the {{$k}} section
{{end}}{{end}}
`
dbValidationTemplate = `Checking for missing/extra tables...done
{{if .missedInDb}}Tables that are found in config but not in DB:
{{range $v := .missedInDb}} - {{.}}
{{end}}{{end -}}
{{if .missedInConfig}}Tables that are found in DB but not in config:
{{range .missedInConfig}} - {{.}}
{{end}}{{end}}
`
fakerValidationTemplate = `Checking obfuscated columns type...done
{{range $v := .}} - Column {{index $v 1}} in the table {{index $v 0}} has unknown or missing type
{{end}}
`
)
var (
conf *config.Config
)
func init() {
loadConfig()
prepareFS()
}
func main() {
// Open connection to database
db, err := sql.Open("mysql", conf.Database.GetMysqlConfigDSN())
if err != nil {
fmt.Println("Error opening database: ", err)
return
}
err = db.Ping()
exitOnError(err != nil, errDBConnectionFailed, fmt.Sprintf("Please validate DB credentials.\n%v", err))
allConfigTables := conf.GetAllUniqueTableNames()
allDbTables, err := mysqldump.ShowTables(db)
exitOnError(err != nil, errShowTablesFailed, fmt.Sprintf("Error getting database table list: %v", err))
diff, diff2 := difference(allConfigTables, allDbTables)
dbValTmpl, err := template.New("dbValidation").Parse(dbValidationTemplate)
if err == nil {
dbValTmpl.Execute(os.Stdout, map[string][]string{
"missedInDb": diff,
"missedInConfig": diff2,
})
}
exitOnError(len(diff) > 0 || len(diff2) > 0, errConfigIncomplete, "Please fix the reported errors in your config file before proceeding")
// Register database with mysqldump
dumper, err := mysqldump.Register(db, conf)
exitOnError(err != nil, errDumpFileIsNotWritable, fmt.Sprintf("Error registering database: %v", err))
// TODO: add to config support of dumper.LockTables = true
err = dumper.Dump()
if err != nil {
fmt.Println("Error dumping:", err)
return
}
fmt.Printf("File is saved to %s\n", conf.GetDumpFileName())
// Close dumper, connected database and file stream.
dumper.Close()
}
func loadConfig() {
var configFilePath string
var error error
fmt.Println("go-obfuscate version", version)
flag.StringVar(&configFilePath, "c", "./config.yaml", "MySQL connection details(./config.yaml)")
flag.Parse()
evaledPath, _ := filepath.EvalSymlinks(configFilePath)
_, err := os.Stat(evaledPath)
exitOnError(errors.Is(err, os.ErrNotExist), errConfigFileNotFound, fmt.Sprintf("Config file does not exist: %s", configFilePath))
fmt.Println("Using config file:", evaledPath)
conf, error = config.GetConf(filepath.Dir(evaledPath), filepath.Base(evaledPath))
exitOnError(error != nil, errConfigFileInvalidMarkUp, fmt.Sprintf("Config file contains invalid YAML markup:\n%v\n", error))
statsTmpl, err := template.New("statistics").Parse(statsTemplate)
if err == nil {
statsTmpl.Execute(os.Stdout, map[string]int{
"keep": len(conf.Tables.Keep),
"ignore": len(conf.Tables.Ignore),
"truncate": len(conf.Tables.Truncate),
"obfuscate": len(conf.Tables.Obfuscate),
"total": len(conf.Tables.Keep) + len(conf.Tables.Ignore) + len(conf.Tables.Truncate) + len(conf.Tables.Obfuscate),
})
}
// Sanity check 1: each table name should be unique across all lists
messages, hasErrors := conf.ValidateConfig()
valTmpl, err := template.New("validation").Parse(validationTemplate)
if err == nil {
valTmpl.Execute(os.Stdout, messages)
}
exitOnError(hasErrors, errConfigHasDuplicates, "Please fix the reported errors in your config file before proceeding")
// Sanity check 2: each obfuscated column type should be known
unknown, hasErrors := conf.ValidateObfuscateSection()
fakerTmpl, err := template.New("faker").Parse(fakerValidationTemplate)
if err == nil {
fakerTmpl.Execute(os.Stdout, unknown)
}
exitOnError(hasErrors, errConfigHasDuplicates, "Please fix the reported errors in your config file before proceeding")
}
func prepareFS() {
// Dump dir exists
os.MkdirAll(conf.Output.Directory, 0777)
exitOnError(!isDir(conf.Output.Directory), errOutputDirectoryMissing, fmt.Sprintf("Could not create directory %s\n", conf.Output.Directory))
// Dump file does not exist
p := conf.GetDumpFullPath()
if e, _ := exists(p); e {
// TODO: recoverable error - just add an increasing postfix or whatever
fmt.Println("Dump '" + p + "' already exists.")
}
}
func isDir(p string) bool {
if e, fi := exists(p); e {
return fi.Mode().IsDir()
}
return false
}
func exists(p string) (bool, os.FileInfo) {
f, err := os.Open(p)
if err != nil {
return false, nil
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return false, nil
}
return true, fi
}
func difference(slice1 []string, slice2 []string) ([]string, []string) {
diff := make(map[int][]string, 0)
// Loop two times, first to find slice1 strings not in slice2,
// second loop to find slice2 strings not in slice1
for i := 0; i < 2; i++ {
for _, s1 := range slice1 {
found := false
for _, s2 := range slice2 {
if s1 == s2 {
found = true
break
}
}
// String not found. We add it to return slice
if !found {
diff[i] = append(diff[i], s1)
}
}
// Swap the slices, only if it was the first loop
if i == 0 {
slice1, slice2 = slice2, slice1
}
}
return diff[0], diff[1]
}
func exitOnError(hasErrors bool, exitCode int, message string) {
if hasErrors {
fmt.Println(message)
os.Exit(exitCode)
}
}