-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
284 lines (271 loc) · 14.2 KB
/
util.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package testutil
import (
"context"
"errors"
"fmt"
"github.com/ScoreTrak/ScoreTrak/pkg/check"
"github.com/ScoreTrak/ScoreTrak/pkg/config"
"github.com/ScoreTrak/ScoreTrak/pkg/host"
"github.com/ScoreTrak/ScoreTrak/pkg/hostgroup"
"github.com/ScoreTrak/ScoreTrak/pkg/policy"
"github.com/ScoreTrak/ScoreTrak/pkg/property"
"github.com/ScoreTrak/ScoreTrak/pkg/round"
"github.com/ScoreTrak/ScoreTrak/pkg/service"
"github.com/ScoreTrak/ScoreTrak/pkg/servicegroup"
"github.com/ScoreTrak/ScoreTrak/pkg/storage"
"github.com/ScoreTrak/ScoreTrak/pkg/team"
"github.com/ScoreTrak/ScoreTrak/pkg/user"
"github.com/gofrs/uuid"
"github.com/jackc/pgconn"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"os"
"time"
)
// CleanAllTables Drops all tables
func CleanAllTables(db *gorm.DB) error {
err := db.Migrator().DropTable(&check.Check{})
if err != nil {
return err
}
err = db.Migrator().DropTable(&property.Property{})
if err != nil {
return err
}
err = db.Migrator().DropTable(&service.Service{})
if err != nil {
return err
}
err = db.Migrator().DropTable(&host.Host{})
if err != nil {
return err
}
err = db.Migrator().DropTable(&hostgroup.HostGroup{})
if err != nil {
return err
}
err = db.Migrator().DropTable(&round.Round{})
if err != nil {
return err
}
err = db.Migrator().DropTable(&servicegroup.ServiceGroup{})
if err != nil {
return err
}
err = db.Migrator().DropTable(&user.User{})
if err != nil {
return err
}
err = db.Migrator().DropTable(&team.Team{})
if err != nil {
return err
}
err = db.Migrator().DropTable(&config.DynamicConfig{})
if err != nil {
return err
}
return nil
}
// uuid1 is a uuid of the initial admin user, and admin team
var uuid1 = uuid.FromStringOrNil("00000000-0000-0000-0000-000000000001")
// CreateBlackTeam Ensures that black team exists
func CreateBlackTeam(db *gorm.DB) (err error) {
err = db.Create([]*team.Team{{ID: uuid1, Name: "Black Team"}}).Error
if err != nil {
var serr *pgconn.PgError
ok := errors.As(err, &serr)
if !ok || serr.Code != "23505" {
return err
}
}
return nil
}
// CreatePolicy Ensure policy.Policy exists
func CreatePolicy(db *gorm.DB) (*policy.Policy, error) {
p := &policy.Policy{ID: 1}
err := db.Create(p).Error
if err != nil {
var serr *pgconn.PgError
ok := errors.As(err, &serr)
if !ok {
if serr.Code != "23505" {
panic(err)
} else {
db.Take(p)
}
}
}
return p, nil
}
// TruncateAllTables truncates all tables (A little faster than dropping)
func TruncateAllTables(db *gorm.DB) {
db.Exec(fmt.Sprintf("TRUNCATE TABLE %s CASCADE", "checks"))
db.Exec(fmt.Sprintf("TRUNCATE TABLE %s CASCADE", "properties"))
db.Exec(fmt.Sprintf("TRUNCATE TABLE %s CASCADE", "services"))
db.Exec(fmt.Sprintf("TRUNCATE TABLE %s CASCADE", "hosts"))
db.Exec(fmt.Sprintf("TRUNCATE TABLE %s CASCADE", "host_groups"))
db.Exec(fmt.Sprintf("TRUNCATE TABLE %s CASCADE", "rounds"))
db.Exec(fmt.Sprintf("TRUNCATE TABLE %s CASCADE", "service_groups"))
db.Exec(fmt.Sprintf("TRUNCATE TABLE %s CASCADE", "users"))
db.Exec(fmt.Sprintf("TRUNCATE TABLE %s CASCADE", "teams"))
db.Exec(fmt.Sprintf("TRUNCATE TABLE %s CASCADE", "config"))
}
func DataPreload(db *gorm.DB) {
var count int64
//Creating Config
db.Exec("INSERT INTO config (id, round_duration, enabled) VALUES (1, 60, true)")
db.Table("config").Count(&count)
if count != 1 {
panic("There should be 1 entry in config")
}
//Creating Teams
db.Exec("INSERT INTO teams (id, name, pause) VALUES ('11111111-1111-1111-1111-111111111111', 'TeamOne', true)")
db.Exec("INSERT INTO teams (id, name, pause) VALUES ('22222222-2222-2222-2222-222222222222', 'TeamTwo', false)")
db.Exec("INSERT INTO teams (id, name, pause) VALUES ('33333333-3333-3333-3333-333333333333', 'TeamThree', true)")
db.Exec("INSERT INTO teams (id, name, pause) VALUES ('44444444-4444-4444-4444-444444444444', 'TeamFour', false)")
db.Table("teams").Count(&count)
if count != 4 {
panic("There should be 4 entry in teams")
}
//Creating Users
db.Exec("INSERT INTO users (team_id, id, username, role, password_hash) VALUES ('11111111-1111-1111-1111-111111111111', '11111111-1111-1111-1111-111111111111', 'TeamOneUser', 'black', '$2a$10$SDWdEajPUVYx1wczissDSur3R0/TmNsvCi/KivaKUM2DQi0Hc9EDG')")
db.Exec("INSERT INTO users (team_id, id, username, role, password_hash) VALUES ('22222222-2222-2222-2222-222222222222', '22222222-2222-2222-2222-222222222222', 'TeamTwoUser2', 'red', '$2a$10$SDWdEajPUVYx1wczissDSur3R0/TmNsvCi/KivaKUM2DQi0Hc9EDG')")
db.Exec("INSERT INTO users (team_id, id, username, role, password_hash) VALUES ('22222222-2222-2222-2222-222222222222', '33333333-3333-3333-3333-333333333333', 'TeamTwoUser1', 'blue', '$2a$10$SDWdEajPUVYx1wczissDSur3R0/TmNsvCi/KivaKUM2DQi0Hc9EDG')")
db.Table("users").Count(&count)
if count != 3 {
panic("There should be 3 entries in users")
}
//Creating Host Groups
db.Exec("INSERT INTO host_groups (id, name, pause) VALUES ('11111111-1111-1111-1111-111111111111', 'HostGroup1', true)")
db.Exec("INSERT INTO host_groups (id, name, pause) VALUES ('22222222-2222-2222-2222-222222222222', 'HostGroup2', false)")
db.Exec("INSERT INTO host_groups (id, name, pause) VALUES ('33333333-3333-3333-3333-333333333333', 'HostGroup3', true)")
db.Exec("INSERT INTO host_groups (id, name, pause) VALUES ('44444444-4444-4444-4444-444444444444', 'HostGroup4', false)")
db.Table("host_groups").Count(&count)
if count != 4 {
panic("There should be 4 entry in host groups")
}
//Creating Service Groups
db.Exec("INSERT INTO service_groups (id, name, enabled) VALUES ('11111111-1111-1111-1111-111111111111', 'ServiceGroup1', true)")
db.Exec("INSERT INTO service_groups (id, name, enabled) VALUES ('22222222-2222-2222-2222-222222222222', 'ServiceGroup2', false)")
db.Exec("INSERT INTO service_groups (id, name, enabled) VALUES ('33333333-3333-3333-3333-333333333333', 'ServiceGroup3', true)")
db.Exec("INSERT INTO service_groups (id, name, enabled) VALUES ('44444444-4444-4444-4444-444444444444', 'ServiceGroup4', false)")
db.Table("service_groups").Count(&count)
if count != 4 {
panic("There should be 4 entry in check_service groups")
}
//Creating Hosts
db.Exec("INSERT INTO hosts (id, address, team_id, host_group_id, pause, edit_host) VALUES ('11111111-1111-1111-1111-111111111111', '10.0.0.1', '11111111-1111-1111-1111-111111111111', NULL, true, true)")
db.Exec("INSERT INTO hosts (id, address, team_id, host_group_id, pause, edit_host) VALUES ('22222222-2222-2222-2222-222222222222', '10.0.0.2', '22222222-2222-2222-2222-222222222222', NULL, false, true)")
db.Exec("INSERT INTO hosts (id, address, team_id, host_group_id, pause, edit_host) VALUES ('33333333-3333-3333-3333-333333333333', '10.0.0.3', '11111111-1111-1111-1111-111111111111', '33333333-3333-3333-3333-333333333333', true, false)")
db.Exec("INSERT INTO hosts (id, address, team_id, host_group_id, pause, edit_host) VALUES ('44444444-4444-4444-4444-444444444444', '10.0.0.4', '44444444-4444-4444-4444-444444444444', '44444444-4444-4444-4444-444444444444', false, false)")
db.Table("hosts").Count(&count)
if count != 4 {
panic("There should be 4 entry in hosts")
}
//Creating Services
db.Exec("INSERT INTO services (id, service_group_id, host_id, name, display_name, weight, round_units, round_delay, pause) VALUES ('11111111-1111-1111-1111-111111111111', '11111111-1111-1111-1111-111111111111', '11111111-1111-1111-1111-111111111111', 'WINRM', 'host1-service1', 0, 1, 0, true)")
db.Exec("INSERT INTO services (id, service_group_id, host_id, name, display_name, weight, round_units, round_delay, pause) VALUES ('22222222-2222-2222-2222-222222222222', '22222222-2222-2222-2222-222222222222', '22222222-2222-2222-2222-222222222222', 'FTP', 'host2-service2', 40, 23, 2, true)")
db.Exec("INSERT INTO services (id, service_group_id, host_id, name, display_name, weight, round_units, round_delay, pause) VALUES ('33333333-3333-3333-3333-333333333333', '44444444-4444-4444-4444-444444444444', '22222222-2222-2222-2222-222222222222', 'SSH', 'host3-service3', 50, 3, 0, true)")
db.Exec("INSERT INTO services (id, service_group_id, host_id, name, display_name, weight, round_units, round_delay, pause) VALUES ('44444444-4444-4444-4444-444444444444', '44444444-4444-4444-4444-444444444444', '44444444-4444-4444-4444-444444444444', 'HTTP', 'host4-service4', 200, 4, 3, true)")
db.Exec("INSERT INTO services (id, service_group_id, host_id, name, display_name, weight, round_units, round_delay, pause) VALUES ('55555555-5555-5555-5555-555555555555', '11111111-1111-1111-1111-111111111111', '22222222-2222-2222-2222-222222222222', 'SSH', 'host1-service2', 30, 5, 4, false)")
db.Exec("INSERT INTO services (id, service_group_id, host_id, name, display_name, weight, round_units, round_delay, pause) VALUES ('66666666-6666-6666-6666-666666666666', '22222222-2222-2222-2222-222222222222', '11111111-1111-1111-1111-111111111111', 'SMB', 'host2-service1', 2, 5, 2, true)")
db.Exec("INSERT INTO services (id, service_group_id, host_id, name, display_name, weight, round_units, round_delay, pause) VALUES ('77777777-7777-7777-7777-777777777777', '22222222-2222-2222-2222-222222222222', '44444444-4444-4444-4444-444444444444', 'FTP', 'host3-service4', 55, 6, 3, false)")
db.Exec("INSERT INTO services (id, service_group_id, host_id, name, display_name, weight, round_units, round_delay, pause) VALUES ('88888888-8888-8888-8888-888888888888', '44444444-4444-4444-4444-444444444444', '44444444-4444-4444-4444-444444444444', 'IMAP', 'host4-service3', 44, 23, 22, false)")
db.Table("services").Count(&count)
if count != 8 {
panic("There should be 8 entry in services")
}
//Creating Properties
db.Exec("INSERT INTO properties (service_id, key, value, status) VALUES ( '11111111-1111-1111-1111-111111111111', 'Port','80','View')")
db.Exec("INSERT INTO properties (service_id, key, value, status) VALUES ( '11111111-1111-1111-1111-111111111111', 'Username','root','Edit')")
db.Exec("INSERT INTO properties (service_id, key, value, status) VALUES ( '22222222-2222-2222-2222-222222222222', 'Port','53','View')")
db.Exec("INSERT INTO properties (service_id, key, value, status) VALUES ( '22222222-2222-2222-2222-222222222222', 'Password','changeme','Hide')")
db.Exec("INSERT INTO properties (service_id, key, value, status) VALUES ( '22222222-2222-2222-2222-222222222222', 'Username','root','Hide')")
db.Exec("INSERT INTO properties (service_id, key, value, status) VALUES ( '33333333-3333-3333-3333-333333333333', 'Password','changeme','Hide')")
db.Exec("INSERT INTO properties (service_id, key, value, status) VALUES ( '44444444-4444-4444-4444-444444444444', 'Port','443','Edit')")
db.Exec("INSERT INTO properties (service_id, key, value, status) VALUES ( '55555555-5555-5555-5555-555555555555', 'Username','admin','Edit')")
db.Exec("INSERT INTO properties (service_id, key, value, status) VALUES ( '66666666-6666-6666-6666-666666666666', 'Password','Change.me!','View')")
db.Exec("INSERT INTO properties (service_id, key, value, status) VALUES ( '66666666-6666-6666-6666-666666666666', 'Content','Sample Content!', 'Hide')")
db.Exec("INSERT INTO properties (service_id, key, value, status) VALUES ( '66666666-6666-6666-6666-666666666666', 'EncodedContent', 'RW5jb2RlZENvbnRlbnQ=', 'Edit')")
db.Exec("INSERT INTO properties (service_id, key, value, status) VALUES ( '66666666-6666-6666-6666-666666666666', 'domain', 'sample.com', 'View')")
db.Table("properties").Count(&count)
if count != 12 {
panic("There should be 13 entry in properties")
}
//Creating Rounds
db.Exec("INSERT INTO rounds (id, start, finish) VALUES (1, ?, ?)", time.Now().Add(-time.Second*60*3), time.Now().Add(-time.Second*60*3+time.Second*30))
db.Exec("INSERT INTO rounds (id, start, finish) VALUES (2, ?, ?)", time.Now().Add(-time.Second*60*2), time.Now().Add(-time.Second*60*2+time.Second*20))
db.Exec("INSERT INTO rounds (id, start, finish) VALUES (3, ?, ?)", time.Now().Add(-time.Second*60), time.Now().Add(-time.Second*60+time.Second*40))
db.Exec("INSERT INTO rounds (id, start) VALUES (4, ?)", time.Now())
db.Table("rounds").Count(&count)
if count != 4 {
panic("There should be 4 entry in rounds")
}
//Creating Checks
db.Exec("INSERT INTO checks (round_id, service_id, log, passed) VALUES (1, '11111111-1111-1111-1111-111111111111', 'Successful Check One!',true)")
db.Exec("INSERT INTO checks (round_id, service_id, log, passed) VALUES (2, '22222222-2222-2222-2222-222222222222', 'Successful Check Two!',true)")
db.Exec("INSERT INTO checks (round_id, service_id, log, passed) VALUES (3, '11111111-1111-1111-1111-111111111111', 'Failed because of incorrect password',false)")
db.Exec("INSERT INTO checks (round_id, service_id, log, passed) VALUES (3, '33333333-3333-3333-3333-333333333333', 'Successful Check Four!',true)")
db.Table("checks").Count(&count)
if count != 4 {
panic("There should be 4 entry in checks")
}
}
func DropDB(db *gorm.DB, c config.StaticConfig) {
if c.DB.Use == "cockroach" {
db.Exec(fmt.Sprintf("drop database %s", c.DB.Cockroach.Database))
} else if c.DB.Use == "sqlite" {
err := os.RemoveAll(c.DB.Sqlite.Path)
if err != nil {
fmt.Println(err)
}
}
}
func TruncateTable(ctx context.Context, v interface{}, db *gorm.DB) error {
stmt := &gorm.Statement{DB: db}
err := stmt.Parse(v)
if err != nil {
return err
}
return db.WithContext(ctx).Exec(fmt.Sprintf("TRUNCATE TABLE %s CASCADE", stmt.Schema.Table)).Error //POSTGRES SPECIFIC. FOR MYSQL, CHANGE THIS TO SET FOREIGN_KEY_CHECKS=0 ; <TRUNCATE> ; SET FOREIGN_KEY_CHECKS=1
}
func SetupDB(c storage.Config) *gorm.DB {
var db *gorm.DB
if c.Use == "cockroach" {
db = SetupCockroachDB(c)
} else if c.Use == "sqlite" {
db = SetupSqliteDB(c)
}
return db
}
// SetupCockroachDB
func SetupCockroachDB(c storage.Config) *gorm.DB {
var err error
psqlInfo := fmt.Sprintf("host=%s port=%s user=%s sslmode=disable",
c.Cockroach.Host,
c.Cockroach.Port,
c.Cockroach.UserName)
dbPrep, err := gorm.Open(postgres.Open(psqlInfo), &gorm.Config{})
if err != nil {
panic(err)
}
dbPrep.Exec(fmt.Sprintf("drop database if exists %s", c.Cockroach.Database))
dbPrep.Exec(fmt.Sprintf("create database if not exists %s", c.Cockroach.Database))
db, err := storage.NewDB(c)
if err != nil {
panic(err)
}
return db
}
// SetupSqliteDB creates a new database instance using
func SetupSqliteDB(c storage.Config) (db *gorm.DB) {
var err error
db, err = storage.NewDB(c)
if err != nil {
panic(err)
}
if res := db.Exec("PRAGMA foreign_keys = ON", nil); res.Error != nil {
panic(err)
}
return db
}