Browse Source

[Rework] Reputation: Use more flexible types in get/set functions

tags/3.0
Vsevolod Stakhov 3 years ago
parent
commit
fb789af8bb
1 changed files with 49 additions and 24 deletions
  1. 49
    24
      src/plugins/lua/reputation.lua

+ 49
- 24
src/plugins/lua/reputation.lua View File

for dom,res in pairs(requests) do for dom,res in pairs(requests) do
-- tld + "." + check_result, e.g. example.com.+ - reputation for valid sigs -- tld + "." + check_result, e.g. example.com.+ - reputation for valid sigs
local query = string.format('%s.%s', dom, res) local query = string.format('%s.%s', dom, res)
rule.backend.get_token(task, rule, query, tokens_cb)
rule.backend.get_token(task, rule, nil, query, tokens_cb, 'string')
end end
end end


for dom,res in pairs(requests) do for dom,res in pairs(requests) do
-- tld + "." + check_result, e.g. example.com.+ - reputation for valid sigs -- tld + "." + check_result, e.g. example.com.+ - reputation for valid sigs
local query = string.format('%s.%s', dom, res) local query = string.format('%s.%s', dom, res)
rule.backend.set_token(task, rule, query, sc)
rule.backend.set_token(task, rule, nil, query, sc, 'string')
end end
end end
end end
indexed_tokens_cb(err, i, values) indexed_tokens_cb(err, i, values)
end end


rule.backend.get_token(task, rule, req[1], tokens_cb)
rule.backend.get_token(task, rule, nil, req[1], tokens_cb, 'string')
end end
end end




if sc then if sc then
for _,tld in ipairs(requests) do for _,tld in ipairs(requests) do
rule.backend.set_token(task, rule, tld[1], sc)
rule.backend.set_token(task, rule, nil, tld[1], sc, 'string')
end end
end end
end end
end end


if asn then if asn then
rule.backend.get_token(task, rule, cfg.asn_prefix .. asn, gen_token_callback('asn'))
rule.backend.get_token(task, rule, cfg.asn_prefix, asn,
gen_token_callback('asn'), 'string')
end end
if country then if country then
rule.backend.get_token(task, rule, cfg.country_prefix .. country, gen_token_callback('country'))
rule.backend.get_token(task, rule, cfg.country_prefix, country,
gen_token_callback('country'), 'string')
end end


rule.backend.get_token(task, rule, cfg.ip_prefix .. tostring(ip), gen_token_callback('ip'))
rule.backend.get_token(task, rule, cfg.ip_prefix, ip,
gen_token_callback('ip'), 'ip')
end end


-- Used to set scores -- Used to set scores
local sc = extract_task_score(task, rule) local sc = extract_task_score(task, rule)
if sc then if sc then
if asn then if asn then
rule.backend.set_token(task, rule, cfg.asn_prefix .. asn, sc)
rule.backend.set_token(task, rule, cfg.asn_prefix, asn, sc, 'string')
end end
if country then if country then
rule.backend.set_token(task, rule, cfg.country_prefix .. country, sc)
rule.backend.set_token(task, rule, cfg.country_prefix, country, sc, 'string')
end end


rule.backend.set_token(task, rule, cfg.ip_prefix .. tostring(ip), sc)
rule.backend.set_token(task, rule, cfg.ip_prefix, ip, sc, 'ip')
end end
end end


end end
end end


rule.backend.get_token(task, rule, hkey, tokens_cb)
rule.backend.get_token(task, rule, nil, hkey, tokens_cb, 'string')
end end


local function spf_reputation_idempotent(task, rule) local function spf_reputation_idempotent(task, rule)


lua_util.debugm(N, task, 'set spf record %s -> %s = %s', lua_util.debugm(N, task, 'set spf record %s -> %s = %s',
spf_record, hkey, sc) spf_record, hkey, sc)
rule.backend.set_token(task, rule, hkey, sc)
rule.backend.set_token(task, rule, nil, hkey, sc, 'string')
end end




fun.each(function(e) fun.each(function(e)
lua_util.debugm(N, task, 'check generic reputation (%s) %s', lua_util.debugm(N, task, 'check generic reputation (%s) %s',
rule['symbol'], e) rule['symbol'], e)
rule.backend.get_token(task, rule, e, tokens_cb)
rule.backend.get_token(task, rule, nil, e, tokens_cb, 'string')
end, selector_res) end, selector_res)
else else
lua_util.debugm(N, task, 'check generic reputation (%s) %s', lua_util.debugm(N, task, 'check generic reputation (%s) %s',
rule['symbol'], selector_res) rule['symbol'], selector_res)
rule.backend.get_token(task, rule, selector_res, tokens_cb)
rule.backend.get_token(task, rule, nil, selector_res, tokens_cb, 'string')
end end
end end
end end
fun.each(function(e) fun.each(function(e)
lua_util.debugm(N, task, 'set generic selector (%s) %s = %s', lua_util.debugm(N, task, 'set generic selector (%s) %s = %s',
rule['symbol'], e, sc) rule['symbol'], e, sc)
rule.backend.set_token(task, rule, e, sc)
rule.backend.set_token(task, rule, nil, e, sc, 'string')
end, selector_res) end, selector_res)
else else
lua_util.debugm(N, task, 'set generic selector (%s) %s = %s', lua_util.debugm(N, task, 'set generic selector (%s) %s = %s',
rule['symbol'], selector_res, sc) rule['symbol'], selector_res, sc)
rule.backend.set_token(task, rule, selector_res, sc)
rule.backend.set_token(task, rule, nil, selector_res, sc, 'string')
end end
end end
end end
end end




local function gen_token_key(token, rule)
local res = token
local function gen_token_key(prefix, token, rule)
if prefix then
token = prefix .. token
end
local res = prefix
if rule.backend.config.hashed then if rule.backend.config.hashed then
local hash_alg = rule.backend.config.hash_alg or "blake2" local hash_alg = rule.backend.config.hash_alg or "blake2"
local encoding = "base32" local encoding = "base32"


--[[ --[[
-- Generic interface for get and set tokens functions: -- Generic interface for get and set tokens functions:
-- get_token(task, rule, prefix, token, continuation, token_type), where `continuation` is the following function:
-- --
-- function(err, token, values) ... end -- function(err, token, values) ... end
-- `err`: string value for error (similar to redis or DNS callbacks) -- `err`: string value for error (similar to redis or DNS callbacks)
-- example of tokens: {'s': 0, 'h': 0, 'p': 1} -- example of tokens: {'s': 0, 'h': 0, 'p': 1}
--]] --]]


local function reputation_dns_get_token(task, rule, token, continuation_cb)
local function reputation_dns_get_token(task, rule, prefix, token, continuation_cb, token_type)
-- local r = task:get_resolver() -- local r = task:get_resolver()
local key = gen_token_key(token, rule)
-- In DNS we never ever use prefix as prefix, we use if as a suffix!
if token_type == 'ip' then
token = table.concat(token:inversed_str_octets(), '.')
end

local key = gen_token_key(nil, token, rule)
local dns_name = key .. '.' .. rule.backend.config.list local dns_name = key .. '.' .. rule.backend.config.list


if prefix then
dns_name = string.format('%s.%s.%s', key, prefix,
rule.backend.config.list)
else
dns_name = string.format('%s.%s', key, rule.backend.config.list)
end

local function dns_cb(_, _, results, err) local function dns_cb(_, _, results, err)
if err and (err ~= 'requested record is not found' and if err and (err ~= 'requested record is not found' and
err ~= 'no records with this name') then err ~= 'no records with this name') then
return true return true
end end


local function reputation_redis_get_token(task, rule, token, continuation_cb)
local key = gen_token_key(token, rule)
local function reputation_redis_get_token(task, rule, prefix, token, continuation_cb, token_type)
if token_type == 'ip' then
token = tostring(token)
end
local key = gen_token_key(prefix, token, rule)


local function redis_get_cb(err, data) local function redis_get_cb(err, data)
if data then if data then
end end
end end


local function reputation_redis_set_token(task, rule, token, sc, continuation_cb)
local key = gen_token_key(token, rule)
local function reputation_redis_set_token(task, rule, prefix, token, sc, continuation_cb, token_type)
if token_type == 'ip' then
token = tostring(token)
end
local key = gen_token_key(prefix, token, rule)


local function redis_set_cb(err, data) local function redis_set_cb(err, data)
if err then if err then

Loading…
Cancel
Save