-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudit.go
123 lines (105 loc) · 2.53 KB
/
audit.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
package audit
import (
"fmt"
"log"
"regexp"
"strings"
"time"
)
var (
ErrInvalidConnection = fmt.Errorf("invalid database connection")
)
type Action string
const (
Select Action = "select"
Insert Action = "insert"
Update Action = "update"
Delete Action = "delete"
)
type Event struct {
ActorID uint64 `db:"actor_id"`
TableRowID uint64 `db:"table_row_id"`
Table string `db:"table_name"`
Action Action `db:"action"`
OldValues string `db:"old_values"`
NewValues string `db:"new_values"`
HTTPMethod string `db:"http_method"`
URL string `db:"url"`
IPAddress string `db:"ip_address"`
UserAgent string `db:"user_agent"`
CreatedAt time.Time `db:"created_at"`
WhereClause WhereClause
IsExempted bool
}
type Option func(*Auditor)
var defaultAuditor = &Auditor{
auditTableName: "audits",
tableException: []string{"audits"},
}
// NewAudit created a new auditor instance
func NewAudit(opts ...Option) (*Auditor, error) {
a := defaultAuditor
for _, opt := range opts {
opt(a)
}
return a, nil
}
// WithTableName customise the audit table name
func WithTableName(tableName string) Option {
return func(a *Auditor) {
sanitised, err := sanitise(tableName)
if err != nil {
log.Fatalln(err)
}
a.auditTableName = sanitised
a.tableException = append(a.tableException, tableName)
}
}
//func WithMongo(dsn string) Option {
// return func(a *Auditor) {
// client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(dsn))
// if err != nil {
// log.Fatal(err)
// }
// a.store.mongo = client
// }
//}
//
//// WithQueue todo: Allow client to use existing queue to save into database
//func WithQueue() Option {
// return func(a *Auditor) {
//
// }
//}
// WithTableException list of tables not to be audited
func WithTableException(tableNames ...string) Option {
exceptions := make([]string, 0)
for _, name := range tableNames {
exceptions = append(exceptions, strings.ToLower(name))
}
return func(a *Auditor) {
a.tableException = append(a.tableException, exceptions...)
}
}
func isExempted(exception []string, tableName string) bool {
if tableName == "" {
return true
}
for _, table := range exception {
if tableName == table {
return true
}
}
return false
}
func sanitise(name string) (string, error) {
return onlyAlphaNumeric(name)
}
func onlyAlphaNumeric(name string) (string, error) {
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
if err != nil {
return "", err
}
processedString := reg.ReplaceAllString(name, "")
return processedString, nil
}