Skip to content

Commit

Permalink
[Feature] Clickhouse: Allow to store subject in Clickhouse
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Feb 12, 2019
1 parent 2a82f0c commit 3b303b1
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/plugins/lua/clickhouse.lua
Expand Up @@ -30,7 +30,7 @@ end
local data_rows = {}
local custom_rows = {}
local nrows = 0
local schema_version = 2 -- Current schema version
local schema_version = 3 -- Current schema version

local settings = {
limit = 1000,
Expand All @@ -54,6 +54,11 @@ local settings = {
use_https = false,
use_gzip = true,
allow_local = false,
insert_subject = false,
subject_privacy = false, -- subject privacy is off
subject_privacy_alg = 'blake2', -- default hash-algorithm to obfuscate subject
subject_privacy_prefix = 'obf', -- prefix to show it's obfuscated
subject_privacy_length = 16, -- cut the length of the hash
user = nil,
password = nil,
no_ssl_verify = false,
Expand Down Expand Up @@ -91,6 +96,7 @@ CREATE TABLE rspamd
RcptUser String,
RcptDomain String,
ListId String,
Subject String,
`Attachments.FileName` Array(String),
`Attachments.ContentType` Array(String),
`Attachments.Length` Array(UInt32),
Expand Down Expand Up @@ -132,6 +138,13 @@ local migrations = {
-- Add explicit version
[[CREATE TABLE rspamd_version ( Version UInt32) ENGINE = TinyLog]],
[[INSERT INTO rspamd_version (Version) Values (2)]],
},
[2] = {
-- Add `Subject` column
[[ALTER TABLE rspamd
ADD COLUMN Subject String AFTER ListId]],
-- New version
[[INSERT INTO rspamd_version (Version) Values (3)]],
}
}

Expand Down Expand Up @@ -159,7 +172,8 @@ local function clickhouse_main_row(res)
'RcptUser',
'RcptDomain',
'ListId',
'Digest'
'Subject',
'Digest',
}

for _,v in ipairs(fields) do table.insert(res, v) end
Expand Down Expand Up @@ -435,6 +449,11 @@ local function clickhouse_collect(task)
local action = task:get_metric_action('default')
local digest = task:get_digest()

local subject = ''
if settings.insert_subject then
subject = lua_util.maybe_obfuscate_subject(task:get_subject() or '', settings)
end

local row = {
today(timestamp),
timestamp,
Expand All @@ -457,6 +476,7 @@ local function clickhouse_collect(task)
rcpt_user,
rcpt_domain,
list_id,
subject,
digest
}

Expand Down

0 comments on commit 3b303b1

Please sign in to comment.