forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bl.go
317 lines (284 loc) · 7.71 KB
/
bl.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
Copyright 2015 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package boltlog is a Bolt-backend implementation of the event log
package boltlog
import (
"bytes"
"encoding/binary"
"encoding/json"
"time"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/backend/boltbk"
"github.com/gravitational/teleport/lib/events"
"github.com/gravitational/teleport/lib/session"
"github.com/boltdb/bolt"
"github.com/codahale/lunk"
"github.com/gravitational/trace"
)
type BoltLog struct {
db *bolt.DB
}
func New(path string) (*BoltLog, error) {
db, err := bolt.Open(path, 0600, nil)
if err != nil {
return nil, err
}
return &BoltLog{
db: db,
}, nil
}
func (b *BoltLog) Close() error {
return b.db.Close()
}
func (b *BoltLog) Log(id lunk.EventID, e lunk.Event) {
en := lunk.NewEntry(id, e)
en.Time = time.Now()
b.LogEntry(en)
}
func (b *BoltLog) LogEntry(en lunk.Entry) error {
sid := en.Properties[SessionID]
btm, err := en.Time.MarshalText()
if err != nil {
return err
}
bytes, err := json.Marshal(en)
if err != nil {
return err
}
return b.db.Update(func(tx *bolt.Tx) error {
if sid != "" {
bkt, err := boltbk.UpsertBucket(tx, []string{"sessions", sid})
if err != nil {
return err
}
if err := bkt.Put([]byte(btm), bytes); err != nil {
return err
}
}
bkt, err := boltbk.UpsertBucket(tx, []string{"events"})
if err != nil {
return err
}
return bkt.Put([]byte(btm), bytes)
})
}
func (b *BoltLog) LogSession(sess session.Session) error {
sessionBytes, err := json.Marshal(sess)
if err != nil {
return trace.Wrap(err)
}
return b.db.Update(func(tx *bolt.Tx) error {
bkt, err := boltbk.UpsertBucket(tx, []string{"sessionlog"})
if err != nil {
return trace.Wrap(err)
}
if err := bkt.Put(key(sess.ID.UUID()), sessionBytes); err != nil {
return trace.Wrap(err)
}
return nil
})
}
func (b *BoltLog) GetSessionEvents(f events.Filter) ([]session.Session, error) {
if f.SessionID != "" {
if err := f.SessionID.Check(); err != nil {
return nil, trace.Wrap(err)
}
}
if f.Order != events.Desc {
return nil, trace.Wrap(teleport.BadParameter("order", "only descending order is supported with this backend"))
}
if f.Start.IsZero() {
return nil, trace.Wrap(teleport.BadParameter("start", "supply starting point"))
}
if f.Limit > events.MaxLimit {
return nil, trace.Wrap(teleport.BadParameter("limit", "limit exceeds maximum"))
}
if f.Limit == 0 {
f.Limit = events.DefaultLimit
}
startKey := key(maxTimeUUID(f.Start))
if f.SessionID != "" {
startKey = key(f.SessionID.UUID())
}
endKey := key(minTimeUUID(f.End))
out := []session.Session{}
err := b.db.View(func(tx *bolt.Tx) error {
bkt, err := boltbk.GetBucket(tx, []string{"sessionlog"})
if err != nil {
if teleport.IsNotFound(err) {
return nil
}
return trace.Wrap(err)
}
// choose start key and iter function based on start or end
c := bkt.Cursor()
var count = 0
var flimit limitfn
var fnext nextfn
var key, val []byte
if f.Order >= 0 { // ascending
fnext = c.Next
flimit = func() bool {
if !f.End.IsZero() && bytes.Compare(key, endKey) > 0 {
return false
}
return count < f.Limit
}
} else {
fnext = c.Prev // descending
flimit = func() bool {
if !f.End.IsZero() && bytes.Compare(key, endKey) <= 0 {
return false
}
return count < f.Limit
}
}
key, val = c.Seek(startKey)
// this can happen if we supplied the key after last key
// we jump to last key to check if it still matches
if key == nil && f.Order < 0 {
key, val = c.Last()
}
for ; key != nil && flimit(); key, val = fnext() {
var sess *session.Session
if err := json.Unmarshal(val, &sess); err != nil {
return trace.Wrap(err)
}
out = append(out, *sess)
count++
}
return nil
})
if err != nil {
return nil, trace.Wrap(err)
}
return out, nil
}
func (b *BoltLog) GetEvents(f events.Filter) ([]lunk.Entry, error) {
if f.Start.IsZero() {
return nil, trace.Wrap(teleport.BadParameter("start", "supply starting point"))
}
if f.Limit == 0 {
f.Limit = events.DefaultLimit
}
startKey, err := f.Start.MarshalText()
if err != nil {
return nil, trace.Wrap(err)
}
endKey, err := f.End.MarshalText()
if err != nil {
return nil, trace.Wrap(err)
}
out := []lunk.Entry{}
err = b.db.View(func(tx *bolt.Tx) error {
var bkt *bolt.Bucket
var err error
if f.SessionID != "" {
bkt, err = boltbk.GetBucket(tx, []string{"sessions", string(f.SessionID)})
} else {
bkt, err = boltbk.GetBucket(tx, []string{"events"})
}
if err != nil {
if teleport.IsNotFound(err) {
return nil
}
return trace.Wrap(err)
}
// choose start key and iter function based on start or end
c := bkt.Cursor()
var count = 0
var flimit limitfn
var fnext nextfn
var key, val []byte
if f.Order >= 0 { // ascending
fnext = c.Next
flimit = func() bool {
if !f.End.IsZero() && bytes.Compare(key, endKey) > 0 {
return false
}
return count < f.Limit
}
} else {
fnext = c.Prev // descending
flimit = func() bool {
if !f.End.IsZero() && bytes.Compare(key, endKey) <= 0 {
return false
}
return count < f.Limit
}
}
key, val = c.Seek(startKey)
// this can happen if we supplied the key after last key
// we jump to last key to check if it still matches
if key == nil && f.Order < 0 {
key, val = c.Last()
}
for ; key != nil && flimit(); key, val = fnext() {
var e *lunk.Entry
if err := json.Unmarshal(val, &e); err != nil {
return err
}
out = append(out, *e)
count++
}
return nil
})
if err != nil {
return nil, err
}
return out, nil
}
const SessionID = "sessionid"
type nextfn func() ([]byte, []byte)
type limitfn func() bool
// fromTimeAndBits composes fake UUID based on given time
// and most significant bits
func fromTimeAndBits(tm time.Time, highBits uint64) []byte {
bytes := make([]byte, 16)
utcTime := tm.In(time.UTC)
// according to https://tools.ietf.org/html/rfc4122#page-8 time portion is
// count of 100 nanosecond intervals since 00:00:00.00, 15 October 1582 (the date of
// Gregorian reform to the Christian calendar).
count := uint64(utcTime.Unix()-timeBase)*10000000 + uint64(utcTime.Nanosecond()/100)
low := uint32(count & 0xffffffff)
mid := uint16((count >> 32) & 0xffff)
hi := uint16((count >> 48) & 0x0fff)
hi |= 0x1000 // Version 1
binary.BigEndian.PutUint32(bytes[0:], low)
binary.BigEndian.PutUint16(bytes[4:], mid)
binary.BigEndian.PutUint16(bytes[6:], hi)
binary.BigEndian.PutUint64(bytes[8:], highBits)
return bytes
}
// the date of Gregorian reform to the Christian calendar
var timeBase = time.Date(1582, time.October, 15, 0, 0, 0, 0, time.UTC).Unix()
func maxTimeUUID(tm time.Time) []byte {
return fromTimeAndBits(tm, maxClockSeqAndNode)
}
func minTimeUUID(tm time.Time) []byte {
return fromTimeAndBits(tm, minClockSeqAndNode)
}
const (
minClockSeqAndNode uint64 = 0x8080808080808080
maxClockSeqAndNode uint64 = 0x7f7f7f7f7f7f7f7f
)
// key returns proper byte sequence for lexicographical sorting
// that BoltDB uses
func key(id []byte) []byte {
out := make([]byte, len(id))
copy(out[:2], id[6:8]) // time hi
copy(out[2:4], id[4:6]) // time mid
copy(out[4:8], id[0:4]) // time low
copy(out[8:], id[8:]) // node id and sequence
return out
}