aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/lua/clustering.lua
blob: d6c78ef79663afb323949b1dfd4f467f4c316966 (plain)
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
--[[
Copyright (c) 2018, Vsevolod Stakhov

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.
]]--

if confighelp then
  return
end

-- Plugin for finding patterns in email flows

local N = 'clustering'

local rspamd_logger = require "rspamd_logger"
local lua_util = require "lua_util"
local lua_redis = require "lua_redis"
local lua_selectors = require "lua_selectors"
local ts = require("tableshape").types

local redis_params

local rules = {} -- Rules placement

local default_rule = {
  max_elts = 100, -- Maximum elements in a cluster
  expire = 3600, -- Expire for a bucket when limit is not reached
  expire_overflow = 36000, -- Expire for a bucket when limit is reached
  spam_mult = 1.0, -- Increase on spam hit
  junk_mult = 0.5, -- Increase on junk
  ham_mult = -0.1, -- Increase on ham
  size_mult = 0.01, -- Reaches 1.0 on `max_elts`
  score_mult = 0.1,
}

local rule_schema = ts.shape{
  max_elts = ts.number + ts.string / tonumber,
  expire = ts.number + ts.string / lua_util.parse_time_interval,
  expire_overflow = ts.number + ts.string / lua_util.parse_time_interval,
  spam_mult = ts.number,
  junk_mult = ts.number,
  ham_mult = ts.number,
  size_mult = ts.number,
  score_mult = ts.number,
  source_selector = ts.string,
  cluster_selector = ts.string,
  symbol = ts.string:is_optional(),
  prefix = ts.string:is_optional(),
}

-- Redis scripts

-- Queries for a cluster's data
-- Arguments:
-- 1. Source selector (string)
-- 2. Cluster selector (string)
-- Returns: {cur_elts, total_score, element_score}
local query_cluster_script = [[
local sz = redis.call('HLEN', KEYS[1])

if not sz or not tonumber(sz) then
  -- New bucket, will update on idempotent phase
  return {0, '0', '0'}
end

local total_score = redis.call('HGET', KEYS[1], '__s')
total_score = tonumber(total_score) or 0
local score = redis.call('HGET', KEYS[1], KEYS[2])
if not score or not tonumber(score) then
  return {sz, tostring(total_score), '0'}
end
return {sz, tostring(total_score), tostring(score)}
]]
local query_cluster_id

-- Updates cluster's data
-- Arguments:
-- 1. Source selector (string)
-- 2. Cluster selector (string)
-- 3. Score (number)
-- 4. Max buckets (number)
-- 5. Expire (number)
-- 6. Expire overflow (number)
-- Returns: nothing
local update_cluster_script = [[
local sz = redis.call('HLEN', KEYS[1])

if not sz or not tonumber(sz) then
  -- Create bucket
  redis.call('HSET', KEYS[1], KEYS[2], math.abs(KEYS[3]))
  redis.call('HSET', KEYS[1], '__s', KEYS[3])
  redis.call('EXPIRE', KEYS[1], KEYS[5])

  return
end

sz = tonumber(sz)
local lim = tonumber(KEYS[4])

if sz > lim then

  if k then
    -- Existing key
    redis.call('HINCRBYFLOAT', KEYS[1], KEYS[2], math.abs(KEYS[3]))
  end
else
  redis.call('HINCRBYFLOAT', KEYS[1], KEYS[2], math.abs(KEYS[3]))
  redis.call('EXPIRE', KEYS[1], KEYS[6])
end

redis.call('HINCRBYFLOAT', KEYS[1], '__s', KEYS[3])
redis.call('EXPIRE', KEYS[1], KEYS[5])
]]
local update_cluster_id

-- Callbacks and logic

local function clusterting_filter_cb(task, rule)
  local source_selector = rule.source_selector(task)
  local cluster_selector

  if source_selector then
    cluster_selector = rule.cluster_selector(task)
  end

  if not cluster_selector or not source_selector then
    rspamd_logger.debugm(N, task, 'skip rule %s, selectors: source="%s", cluster="%s"',
        rule.name, source_selector, cluster_selector)
    return
  end

  local function combine_scores(cur_elts, total_score, element_score)
    local final_score

    local size_score = cur_elts * rule.size_mult
    local cluster_score = total_score * rule.score_mult

    if element_score > 0 then
      -- We have seen this element mostly in junk/spam
      final_score = math.min(1.0, size_score + cluster_score)
    else
      -- We have seen this element in ham mostly, so subtract average it from the size score
      final_score = math.min(1.0, size_score - cluster_score / cur_elts)
    end
    rspamd_logger.debugm(N, task, 'processed rule %s, selectors: source="%s", cluster="%s"; data: %s elts, %s score, %s elt score',
        rule.name, source_selector, cluster_selector, cur_elts, total_score, element_score)
    if final_score > 0.1 then
      task:insert_result(rule.symbol, final_score, {source_selector,
                                                    tostring(size_score),
                                                    tostring(cluster_score)})
    end
  end

  local function redis_get_cb(err, data)
    if data then
      if type(data) == 'table' then
        combine_scores(tonumber(data[1]), tonumber(data[2]), tonumber(data[3]))
      else
        rspamd_logger.errx(task, 'invalid type while getting clustering keys %s: %s',
            source_selector, type(data))
      end

    elseif err then
      rspamd_logger.errx(task, 'got error while getting clustering keys %s: %s',
          source_selector, err)
    else
      rspamd_logger.errx(task, 'got error while getting clustering keys %s: %s',
          source_selector, "unknown error")
    end
  end

  lua_redis.exec_redis_script(query_cluster_id,
      {task = task, is_write = false, key = source_selector},
      redis_get_cb,
      {source_selector, cluster_selector})
end

local function clusterting_idempotent_cb(task, rule)
  local action = task:get_action()
  local score

  if action == 'no action' then
    score = rule.ham_mult
  elseif action == 'reject' then
    score = rule.spam_mult
  elseif action == 'add header' or action == 'rewrite subject' then
    score = rule.junk_mult
  else
    rspamd_logger.debugm(N, task, 'skip rule %s, action=%s',
        rule.name, action)
    return
  end

  local source_selector = rule.source_selector(task)
  local cluster_selector

  if source_selector then
    cluster_selector = rule.cluster_selector(task)
  end

  if not cluster_selector or not source_selector then
    rspamd_logger.debugm(N, task, 'skip rule %s, selectors: source="%s", cluster="%s"',
        rule.name, source_selector, cluster_selector)
    return
  end

  local function redis_set_cb(err, data)
    if err then
      rspamd_logger.errx(task, 'got error while getting clustering keys %s: %s',
          source_selector, err)
    else
      rspamd_logger.debugm(task, 'set clustering key for %s: %s{%s} = %s',
          source_selector, "unknown error")
    end
  end

  lua_redis.exec_redis_script(update_cluster_id,
      {task = task, is_write = true, key = source_selector},
      redis_set_cb,
      {
        source_selector,
        cluster_selector,
        tostring(score),
        tostring(rule.max_elts),
        tostring(rule.expire),
        tostring(rule.expire_overflow)
      }
  )
end
-- Init part
redis_params = lua_redis.parse_redis_server('clustering')
local opts = rspamd_config:get_all_opt("clustering")

-- Initialization part
if not (opts and type(opts) == 'table') then
  lua_util.disable_module(N, "config")
  return
end

if not redis_params then
  lua_util.disable_module(N, "redis")
  return
end

if opts['rules'] then
  for k,v in pairs(opts['rules']) do
    local raw_rule = lua_util.override_defaults(default_rule, v)

    local rule,err = rule_schema:transform(raw_rule)

    if not rule then
      rspamd_logger.errx(rspamd_config, 'invalid clustering rule %s: %s',
          k, err)
    else

      if not rule.symbol then rule.symbol = k end
      if not rule.prefix then rule.prefix = k .. "_" end

      rule.source_selector = lua_selectors.create_selector_closure(rspamd_config,
          rule.source_selector, '')
      rule.cluster_selector =  lua_selectors.create_selector_closure(rspamd_config,
          rule.cluster_selector, '')
      if rule.source_selector and rule.cluster_selector then
        rule.name = k
        table.insert(rules, rule)
      end
    end
  end

  if #rules > 0 then

    query_cluster_id = lua_redis.add_redis_script(query_cluster_script, redis_params)
    update_cluster_id = lua_redis.add_redis_script(update_cluster_script, redis_params)
    local function callback_gen(f, rule)
      return function(task) return f(task, rule) end
    end

    for _,rule in ipairs(rules) do
      rspamd_config:register_symbol{
        name = rule.symbol,
        type = 'normal',
        callback = callback_gen(clusterting_filter_cb, rule),
      }
      rspamd_config:register_symbol{
        name = rule.symbol + '_STORE',
        type = 'idempotent',
        callback = callback_gen(clusterting_idempotent_cb, rule),
      }
    end
  else
    lua_util.disable_module(N, "config")
  end
else
  lua_util.disable_module(N, "config")
end