/
id_lookup.go
232 lines (207 loc) · 6 KB
/
id_lookup.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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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 aucoalesce
import (
"math"
"os/user"
"strings"
"sync"
"time"
)
const cacheTimeout = time.Minute
var (
userLookup = NewUserCache(cacheTimeout)
groupLookup = NewGroupCache(cacheTimeout)
// noExpiration = time.Unix(math.MaxInt64, 0)
// The above breaks time.Before and time.After due to overflows.
// See https://stackoverflow.com/questions/25065055/what-is-the-maximum-time-time-in-go
//
// Safe alternative:
noExpiration = time.Unix(0, 0).Add(math.MaxInt64 - 1)
)
type stringItem struct {
timeout time.Time
value string
}
func (i *stringItem) isExpired() bool {
return time.Now().After(i.timeout)
}
// EntityCache is a cache of IDs and usernames.
type EntityCache struct {
byID, byName stringCache
}
// NewUserCache returns a new EntityCache to resolve users. EntityCache is thread-safe.
func NewUserCache(expiration time.Duration) *EntityCache {
return &EntityCache{
byID: stringCache{
expiration: expiration,
data: map[string]stringItem{
"0": {timeout: noExpiration, value: "root"},
},
lookupFn: func(s string) string {
user, err := user.LookupId(s)
if err != nil {
return ""
}
return user.Username
},
},
byName: stringCache{
expiration: expiration,
data: map[string]stringItem{
"root": {timeout: noExpiration, value: "0"},
},
lookupFn: func(s string) string {
user, err := user.Lookup(s)
if err != nil {
return ""
}
return user.Uid
},
},
}
}
// LookupID looks up an UID/GID and returns the user/group name associated with it. If
// no name could be found an empty string is returned. The value will be
// cached for a minute.
func (c *EntityCache) LookupID(uid string) string {
return c.byID.lookup(uid)
}
// LookupName looks up an user/group name and returns the ID associated with it. If
// no ID could be found an empty string is returned. The value will be
// cached for a minute. This requires cgo on Linux.
func (c *EntityCache) LookupName(name string) string {
return c.byName.lookup(name)
}
// NewGroupCache returns a new EntityCache to resolve groups. EntityCache is thread-safe.
func NewGroupCache(expiration time.Duration) *EntityCache {
return &EntityCache{
byID: stringCache{
expiration: expiration,
data: map[string]stringItem{
"0": {timeout: noExpiration, value: "root"},
},
lookupFn: func(s string) string {
grp, err := user.LookupGroupId(s)
if err != nil {
return ""
}
return grp.Name
},
},
byName: stringCache{
expiration: expiration,
data: map[string]stringItem{
"root": {timeout: noExpiration, value: "0"},
},
lookupFn: func(s string) string {
grp, err := user.LookupGroup(s)
if err != nil {
return ""
}
return grp.Gid
},
},
}
}
// ResolveIDs translates all uid and gid values to their associated names.
// Prior to Go 1.9 this requires cgo on Linux. UID and GID values are cached
// for 60 seconds from the time they are read.
func ResolveIDs(event *Event) {
ResolveIDsFromCaches(event, userLookup, groupLookup)
}
// ResolveIDsFromCaches translates all uid and gid values to their associated
// names using the provided caches. Prior to Go 1.9 this requires cgo on Linux.
func ResolveIDsFromCaches(event *Event, users, groups *EntityCache) {
// Actor
if v := users.LookupID(event.Summary.Actor.Primary); v != "" {
event.Summary.Actor.Primary = v
}
if v := users.LookupID(event.Summary.Actor.Secondary); v != "" {
event.Summary.Actor.Secondary = v
}
// User
names := map[string]string{}
for key, id := range event.User.IDs {
if strings.HasSuffix(key, "uid") {
if v := users.LookupID(id); v != "" {
names[key] = v
}
} else if strings.HasSuffix(key, "gid") {
if v := groups.LookupID(id); v != "" {
names[key] = v
}
}
}
if len(names) > 0 {
event.User.Names = names
}
// File owner/group
if event.File != nil {
if event.File.UID != "" {
event.File.Owner = users.LookupID(event.File.UID)
}
if event.File.GID != "" {
event.File.Group = groups.LookupID(event.File.GID)
}
}
// ECS User and groups
event.ECS.User.lookup(users)
event.ECS.Group.lookup(groups)
}
// HardcodeUsers is useful for injecting values for testing.
func HardcodeUsers(users ...user.User) {
for _, usr := range users {
userLookup.byID.hardcode(usr.Uid, usr.Username)
userLookup.byName.hardcode(usr.Username, usr.Uid)
}
}
// HardcodeGroups is useful for injecting values for testing.
func HardcodeGroups(groups ...user.Group) {
for _, grp := range groups {
groupLookup.byID.hardcode(grp.Gid, grp.Name)
groupLookup.byName.hardcode(grp.Name, grp.Gid)
}
}
type stringCache struct {
mutex sync.Mutex
expiration time.Duration
data map[string]stringItem
lookupFn func(string) string
}
func (c *stringCache) lookup(key string) string {
if key == "" || key == "unset" {
return ""
}
c.mutex.Lock()
defer c.mutex.Unlock()
if item, found := c.data[key]; found && !item.isExpired() {
return item.value
}
// Cache the result (even on error).
resolved := c.lookupFn(key)
c.data[key] = stringItem{timeout: time.Now().Add(c.expiration), value: resolved}
return resolved
}
func (c *stringCache) hardcode(key, value string) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.data[key] = stringItem{
timeout: noExpiration,
value: value,
}
}