/
access_log.go
276 lines (214 loc) · 6.54 KB
/
access_log.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
package main
import (
"encoding/json"
"errors"
"fmt"
"html/template"
"net/http"
"net/url"
"strconv"
"github.com/go-martini/martini"
"github.com/jmoiron/sqlx"
"github.com/xrstf/pager"
)
type AccessLogEntry struct {
Id int `db:"id"`
Secret *int `db:"secret_id"`
Consumer *int `db:"consumer_id"`
RequestedAt string `db:"requested_at"`
OriginIp string `db:"origin_ip"`
Status int `db:"status"`
Context *string `db:"context"`
RequestBody *string `db:"request_body"`
_db *sqlx.Tx
}
func (a *AccessLogEntry) Save() error {
if a.Id > 0 {
return errors.New("Existing access log entries cannot be updated.")
}
result, err := a._db.Exec(
"INSERT INTO `access_log` (`secret_id`, `consumer_id`, `requested_at`, `origin_ip`, `status`, `context`, `request_body`) VALUES (?,?,NOW(),?,?,?,?)",
a.Secret, a.Consumer, a.OriginIp, a.Status, a.Context, a.RequestBody,
)
if err != nil {
return err
}
id, err := result.LastInsertId()
if err != nil {
return err
}
a.Id = int(id)
return nil
}
func (e *AccessLogEntry) GetSecret() *Secret {
if e.Secret == nil {
return nil
}
return findSecret(*e.Secret, false, e._db)
}
func (e *AccessLogEntry) GetConsumer() *Consumer {
if e.Consumer == nil {
return nil
}
return findConsumer(*e.Consumer, e._db)
}
type AccessLog interface {
FindAll(int, int) []AccessLogEntry
Find([]int, []int, []int, int, int) []AccessLogEntry
Count([]int, []int, []int) int
LogNotFound(*Consumer, *Secret, *http.Request)
LogAccess(*Consumer, *Secret, *http.Request, int, interface{})
}
type accessLogStruct struct {
db *sqlx.Tx
}
func NewAccessLog(db *sqlx.Tx) AccessLog {
return &accessLogStruct{db}
}
func (a *accessLogStruct) FindAll(limit int, offset int) []AccessLogEntry {
empty := make([]int, 0)
return a.Find(empty, empty, empty, limit, offset)
}
func (a *accessLogStruct) Find(secretIds []int, consumerIds []int, states []int, limit int, offset int) []AccessLogEntry {
list := make([]AccessLogEntry, 0)
where := a.buildWhereStatement(secretIds, consumerIds, states)
limitStmt := a.buildLimitStatement(limit, offset)
a.db.Select(&list, "SELECT `id`, `secret_id`, `consumer_id`, `requested_at`, `origin_ip`, `status`, `context`, `request_body` FROM `access_log` WHERE "+where+" ORDER BY id DESC "+limitStmt)
for i := range list {
list[i]._db = a.db
}
return list
}
func (a *accessLogStruct) Count(secretIds []int, consumerIds []int, states []int) int {
count := 0
where := a.buildWhereStatement(secretIds, consumerIds, states)
a.db.Get(&count, "SELECT COUNT(*) AS `c` FROM `access_log` WHERE "+where)
return count
}
func (a *accessLogStruct) LogNotFound(consumer *Consumer, secret *Secret, req *http.Request) {
a.LogAccess(consumer, secret, req, 404, nil)
}
func (a *accessLogStruct) LogAccess(consumer *Consumer, secret *Secret, req *http.Request, status int, ctx interface{}) {
entry := AccessLogEntry{}
entry.OriginIp = getIP(req)
entry.Status = status
entry._db = a.db
if consumer != nil {
entry.Consumer = &consumer.Id
}
if secret != nil {
entry.Secret = &secret.Id
}
if ctx != nil {
asserted, ok := ctx.(map[string]interface{})
if !ok || len(asserted) > 0 {
json, err := json.Marshal(ctx)
if err != nil {
panic(err)
}
str := string(json)
entry.Context = &str
}
}
err := entry.Save()
if err != nil {
panic(err)
}
}
func (a *accessLogStruct) buildWhereStatement(secretIds []int, consumerIds []int, states []int) string {
where := "1"
if len(secretIds) > 0 {
where += fmt.Sprintf(" AND `secret_id` IN (%s)", concatIntList(secretIds))
}
if len(consumerIds) > 0 {
where += fmt.Sprintf(" AND `consumer_id` IN (%s)", concatIntList(consumerIds))
}
if len(states) > 0 {
where += fmt.Sprintf(" AND `status` IN (%s)", concatIntList(states))
}
return where
}
func (a *accessLogStruct) buildLimitStatement(limit int, offset int) string {
// return a sane fallback
if limit <= 0 {
return "LIMIT 10"
}
if offset > 0 {
return fmt.Sprintf("LIMIT %d,%d", offset, limit)
}
return fmt.Sprintf("LIMIT %d", limit)
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// Access Log controllers
////////////////////////////////////////////////////////////////////////////////////////////////////
type accessSecret struct {
Id int
Name string
Selected bool
}
type accessConsumer struct {
Id int
Name string
Selected bool
}
type accessLogListData struct {
layoutData
Entries []AccessLogEntry
Secrets []accessSecret
Consumers []accessConsumer
States map[int]bool
Query template.URL
Pager pager.Pager
}
func (a *accessLogListData) HasStatus(status int) bool {
_, ok := a.States[status]
return ok
}
func accessLogIndexAction(user *User, req *http.Request, session *Session, db *sqlx.Tx) response {
selectedSecrets := getIntList(req, "secrets[]")
selectedConsumers := getIntList(req, "consumers[]")
selectedStates := getIntList(req, "status[]")
// get current page
page, err := strconv.Atoi(req.FormValue("page"))
if err != nil {
page = 0
}
if page < 0 {
page = 0
}
limit := 25
offset := page * limit
accessLog := NewAccessLog(db)
entries := accessLog.Find(selectedSecrets, selectedConsumers, selectedStates, limit, offset)
total := accessLog.Count(selectedSecrets, selectedConsumers, selectedStates)
pgr := pager.NewBasicPager(page, total, limit)
data := &accessLogListData{layoutData: NewLayoutData("Access Log", "accesslog", user, session.CsrfToken)}
data.Entries = entries
data.Pager = pgr
data.Secrets = []accessSecret{}
data.Consumers = []accessConsumer{}
data.States = make(map[int]bool)
for _, secret := range findAllSecrets(false, db) {
selected := isInIntList(secret.Id, selectedSecrets)
data.Secrets = append(data.Secrets, accessSecret{secret.Id, secret.Name, selected})
}
for _, consumer := range findAllConsumers(db) {
selected := isInIntList(consumer.Id, selectedConsumers)
data.Consumers = append(data.Consumers, accessConsumer{consumer.Id, consumer.Name, selected})
}
for _, status := range selectedStates {
data.States[status] = true
}
// re-create the query string
url := url.Values{}
addIntsToUrl(&url, "secrets[]", selectedSecrets)
addIntsToUrl(&url, "consumers[]", selectedConsumers)
addIntsToUrl(&url, "status[]", selectedStates)
data.Query = template.URL(url.Encode())
return renderTemplate(200, "access_log/index", data)
}
func setupAccessLogCtrl(app *martini.ClassicMartini) {
app.Group("/accesslog", func(r martini.Router) {
app.Get("", accessLogIndexAction)
}, sessions.RequireLogin)
}