-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloomd.go
186 lines (146 loc) · 3.73 KB
/
bloomd.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
package bloomd
import (
"fmt"
"time"
"errors"
"encoding/json"
"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/logging"
"github.com/krakendio/krakend-jose/v2"
"github.com/geetarista/go-bloomd/bloomd"
)
// namespace
const Namespace = "github_com/openrm/krakend-bloomd"
const prefix = "bloomd:"
// consts
const (
delimiter = "."
keepAlivePeriod = 20 * time.Second
)
// errors
var (
errNoConfig = errors.New("no config for bloomd")
errInvalidConfig = errors.New("invalid config for bloomd")
errNoFilterName = errors.New("filter name is required")
errFieldNotExist = errors.New("token missing required field")
errInvalidField = errors.New("token contains invalid field")
)
// config map
type Config struct {
Name string `json:"name"`
Address string `json:"server_addr"`
TokenKeys []string `json:"token_keys"`
}
func Register(scfg config.ServiceConfig, logger logging.Logger) (jose.Rejecter, error) {
data, ok := scfg.ExtraConfig[Namespace]
if !ok {
logger.Debug(prefix, errNoConfig.Error())
return nopRejecter{}, errNoConfig
}
raw, err := json.Marshal(data)
if err != nil {
logger.Debug(prefix, errInvalidConfig.Error())
return nopRejecter{}, errInvalidConfig
}
var cfg Config
if err := json.Unmarshal(raw, &cfg); err != nil {
logger.Debug(prefix, err.Error(), string(raw))
return nopRejecter{}, errInvalidConfig
}
if cfg.Name == "" {
return nopRejecter{}, errNoFilterName
}
filter, err := createFilter(cfg.Address, cfg.Name, logger)
if err != nil {
logger.Error(prefix, "error connecting to bloomd:", err)
return nopRejecter{}, errInvalidConfig
}
return rejecter{
filter: filter,
logger: logger,
tokenKeys: cfg.TokenKeys,
}, nil
}
// rejecter impl.
type rejecter struct {
filter *bloomd.Filter
logger logging.Logger
tokenKeys []string
}
func (r rejecter) handlePanic() {
if err := recover(); err != nil {
if err, ok := err.(error); ok {
r.logger.Error(prefix, err.Error())
}
if err, ok := err.(string); ok {
r.logger.Error(prefix, err)
}
}
}
func (r rejecter) Reject(claims map[string]interface{}) bool {
defer r.handlePanic()
if r.filter == nil || r.filter.Conn == nil {
return false
}
i, keys := 0, make([]string, len(r.tokenKeys))
for _, k := range r.tokenKeys {
v, ok := claims[k]
if !ok {
continue
}
switch v := v.(type) {
case int:
keys[i] = fmt.Sprintf("%s-%d", k, v)
case int64:
keys[i] = fmt.Sprintf("%s-%d", k, v)
case string:
keys[i] = k + "-" + v
}
i++
}
// XXX needs testing
if r.filter.Conn.Socket != nil {
t := time.Now().Add(1 * time.Second)
r.filter.Conn.Socket.SetReadDeadline(t)
r.filter.Conn.Socket.SetWriteDeadline(t)
}
responses, err := r.filter.Multi(keys[:i])
if err != nil {
r.logger.Error(prefix, err.Error())
_ = setupConn(r.filter, r.logger)
}
for i, v := range responses {
if v {
r.logger.Info(prefix, "rejecting by key:", keys[i])
return true
}
}
return false
}
type nopRejecter struct {}
func (nr nopRejecter) Reject(map[string]interface{}) bool { return false }
func setupConn(filter *bloomd.Filter, logger logging.Logger) error {
if filter.Conn.Socket != nil {
_ = filter.Conn.Socket.Close()
filter.Conn.Socket = nil
}
info, err := filter.Info()
if err != nil {
return err
}
logger.Info(prefix, "connected to bloomd:", info)
// XXX needs testing
if err := filter.Conn.Socket.SetKeepAlive(true); err != nil {
return err
}
// XXX needs testing
if err := filter.Conn.Socket.SetKeepAlivePeriod(keepAlivePeriod); err != nil {
return err
}
return nil
}
func createFilter(addr string, filterName string, logger logging.Logger) (*bloomd.Filter, error) {
client := bloomd.NewClient(addr)
filter := client.GetFilter(filterName)
return filter, setupConn(filter, logger)
}