-
Notifications
You must be signed in to change notification settings - Fork 26
/
hashFilter.go
48 lines (40 loc) · 1.44 KB
/
hashFilter.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
package servicehttp
import (
"net/http"
"github.com/go-kit/kit/log/level"
"github.com/xmidt-org/webpa-common/device"
"github.com/xmidt-org/webpa-common/logging"
"github.com/xmidt-org/webpa-common/service"
"github.com/xmidt-org/webpa-common/xhttp/xfilter"
)
// NewHashFilter constructs an xfilter that enforces device hashing to an instance that represents this server process.
// Any request that does not pass the self predicate will be rejected with the reject error.
//
// The returned filter will check the request's context for a device id, using that to hash with if one is found.
// Otherwise, the device key is parsed from the request via device.IDHashParser.
func NewHashFilter(a service.Accessor, reject error, self func(string) bool) xfilter.Interface {
// allow any nil parameter to simply disable the filtering
if a == nil || reject == nil || self == nil {
return xfilter.Allow()
}
return xfilter.Func(func(r *http.Request) error {
var key []byte
if id, ok := device.GetID(r.Context()); ok {
key = id.Bytes()
} else {
var err error
if key, err = device.IDHashParser(r); err != nil {
return err
}
}
i, err := a.Get(key)
if err != nil {
return err
}
if !self(i) {
logging.GetLogger(r.Context()).Log(level.Key(), level.ErrorValue(), logging.MessageKey(), "device does not hash to this instance", "hashKey", string(key), logging.ErrorKey(), reject, "instance", i)
return reject
}
return nil
})
}