generated from compscore/check-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
218 lines (179 loc) · 5.12 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
217
218
package mysql
import (
"context"
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
type optionsStruct struct {
// Database is the name of the database to connect to
Database string `compscore:"database"`
// Table is the name of the table to check
Table string `compscore:"table"`
// Field is the name of the field to check
Field string `compscore:"field"`
// Check if a database connection can be made
Connect bool `compscore:"connect"`
// Check if a table exists
TableExists bool `compscore:"table_exists"`
// Check if any row exists in table
RowExists bool `compscore:"row_exists"`
// Check if a field of row matches regex in
RegexMatch bool `compscore:"regex_match"`
// Check if a field of row matches substring
SubstringMatch bool `compscore:"substring_match"`
// Check if a field of row matches exact string
Match bool `compscore:"match"`
}
func (o *optionsStruct) Unmarshal(options map[string]interface{}) {
databaseInterface, ok := options["database"]
if ok {
database, ok := databaseInterface.(string)
if ok {
o.Database = database
}
}
tableInterface, ok := options["table"]
if ok {
table, ok := tableInterface.(string)
if ok {
o.Table = table
}
}
fieldInterface, ok := options["field"]
if ok {
field, ok := fieldInterface.(string)
if ok {
o.Field = field
}
}
_, ok = options["connect"]
if ok {
o.Connect = true
}
_, ok = options["table_exists"]
if ok {
o.TableExists = true
}
_, ok = options["row_exists"]
if ok {
o.RowExists = true
}
_, ok = options["regex_match"]
if ok {
o.RegexMatch = true
}
_, ok = options["substring_match"]
if ok {
o.SubstringMatch = true
}
_, ok = options["match"]
if ok {
o.Match = true
}
}
func Run(ctx context.Context, target string, command string, expectedOutput string, username string, password string, options map[string]interface{}) (bool, string) {
optionsStruct := optionsStruct{}
optionsStruct.Unmarshal(options)
conn, err := sql.Open(
"mysql",
fmt.Sprintf(
"%s:%s@tcp(%s)/%s",
username,
password,
target,
optionsStruct.Database,
),
)
if err != nil {
return false, err.Error()
}
defer conn.Close()
// Check if connection can be made
if optionsStruct.Connect {
err = conn.Ping()
if err != nil {
return false, err.Error()
}
}
// Check if table exists
if optionsStruct.TableExists {
query, err := conn.PrepareContext(ctx, "SHOW TABLES LIKE ?")
if err != nil {
return false, err.Error()
}
defer query.Close()
rows, err := query.QueryContext(ctx, optionsStruct.Table)
if err != nil {
return false, err.Error()
}
if !rows.Next() {
return false, fmt.Sprintf("table does not exist: \"%s\"", optionsStruct.Table)
}
}
// Check if row exists
if optionsStruct.RowExists {
queryStr := fmt.Sprintf("SELECT * FROM %s LIMIT 1", optionsStruct.Table)
query, err := conn.PrepareContext(ctx, queryStr)
if err != nil {
return false, err.Error()
}
defer query.Close()
rows, err := query.QueryContext(ctx)
if err != nil {
return false, err.Error()
}
if !rows.Next() {
return false, fmt.Sprintf("table is empty: \"%s\"", optionsStruct.Table)
}
}
// Check if field matches regex
if optionsStruct.RegexMatch {
queryStr := fmt.Sprintf("SELECT %s FROM %s WHERE %s REGEXP ? LIMIT 1", optionsStruct.Field, optionsStruct.Table, optionsStruct.Field)
query, err := conn.PrepareContext(ctx, queryStr)
if err != nil {
return false, err.Error()
}
defer query.Close()
rows, err := query.QueryContext(ctx, expectedOutput)
if err != nil {
return false, err.Error()
}
if !rows.Next() {
return false, fmt.Sprintf("no results exist that match regex: \"%s\" for field: \"%s\" in table: \"%s\" in database: \"%s\"", expectedOutput, optionsStruct.Field, optionsStruct.Table, optionsStruct.Database)
}
}
// Check if field matches substring
if optionsStruct.SubstringMatch {
queryStr := fmt.Sprintf("SELECT %s FROM %s WHERE %s LIKE ? LIMIT 1", optionsStruct.Field, optionsStruct.Table, optionsStruct.Field)
query, err := conn.PrepareContext(ctx, queryStr)
if err != nil {
return false, err.Error()
}
defer query.Close()
rows, err := query.QueryContext(ctx, expectedOutput)
if err != nil {
return false, err.Error()
}
if !rows.Next() {
return false, fmt.Sprintf("no results exist that match substring: \"%s\" for field: \"%s\" in table: \"%s\" in database: \"%s\"", expectedOutput, optionsStruct.Field, optionsStruct.Table, optionsStruct.Database)
}
}
// Check if field matches exact string
if optionsStruct.Match {
queryStr := fmt.Sprintf("SELECT %s FROM %s WHERE %s = ? LIMIT 1", optionsStruct.Field, optionsStruct.Table, optionsStruct.Field)
query, err := conn.PrepareContext(ctx, queryStr)
if err != nil {
return false, err.Error()
}
defer query.Close()
rows, err := query.QueryContext(ctx, expectedOutput)
if err != nil {
return false, err.Error()
}
if !rows.Next() {
return false, fmt.Sprintf("no results exist that match string: \"%s\" for field: \"%s\" in table: \"%s\" in database: \"%s\"", expectedOutput, optionsStruct.Field, optionsStruct.Table, optionsStruct.Database)
}
}
return true, ""
}