Debian Codegraph @

lualib/rspamadm/vault.lua

raw
1 --[[
2 Copyright (c) 2022, Vsevolod Stakhov <[email protected]>
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 ]] --
16
17
18 local rspamd_logger = require "rspamd_logger"
19 local ansicolors = require "ansicolors"
20 local ucl = require "ucl"
21 local argparse = require "argparse"
22 local fun = require "fun"
23 local rspamd_http = require "rspamd_http"
24 local cr = require "rspamd_cryptobox"
25
26 local parser = argparse()
27 :name "rspamadm vault"
28 :description "Perform Hashicorp Vault management"
29 :help_description_margin(32)
30 :command_target("command")
31 :require_command(true)
32
33 parser:flag "-s --silent"
34 :description "Do not output extra information"
35 parser:option "-a --addr"
36 :description "Vault address (if not defined in VAULT_ADDR env)"
37 parser:option "-t --token"
38 :description "Vault token (not recommended, better define VAULT_TOKEN env)"
39 parser:option "-p --path"
40 :description "Path to work with in the vault"
41 :default "dkim"
42 parser:option "-o --output"
43 :description "Output format ('ucl', 'json', 'json-compact', 'yaml')"
44 :argname("<type>")
45 :convert {
46 ucl = "ucl",
47 json = "json",
48 ['json-compact'] = "json-compact",
49 yaml = "yaml",
50 }
51 :default "ucl"
52 parser:option "-k --kv-version"
53 :description "Vault KV store version (1 or 2)"
54 :argname("<version>")
55 :convert(tonumber)
56 :default "1"
57
58 parser:command "list ls l"
59 :description "List elements in the vault"
60
61 local show = parser:command "show get"
62 :description "Extract element from the vault"
63 show:argument "domain"
64 :description "Domain to create key for"
65 :args "+"
66
67 local delete = parser:command "delete del rm remove"
68 :description "Delete element from the vault"
69 delete:argument "domain"
70 :description "Domain to create delete key(s) for"
71 :args "+"
72
73 local newkey = parser:command "newkey new create"
74 :description "Add new key to the vault"
75 newkey:argument "domain"
76 :description "Domain to create key for"
77 :args "+"
78 newkey:option "-s --selector"
79 :description "Selector to use"
80 :count "?"
81 newkey:option "-A --algorithm"
82 :argname("<type>")
83 :convert {
84 rsa = "rsa",
85 ed25519 = "ed25519",
86 eddsa = "ed25519",
87 }
88 :default "rsa"
89 newkey:option "-b --bits"
90 :argname("<nbits>")
91 :convert(tonumber)
92 :default "1024"
93 newkey:option "-x --expire"
94 :argname("<days>")
95 :convert(tonumber)
96 newkey:flag "-r --rewrite"
97
98 local roll = parser:command "roll rollover"
99 :description "Perform keys rollover"
100 roll:argument "domain"
101 :description "Domain to roll key(s) for"
102 :args "+"
103 roll:option "-T --ttl"
104 :description "Validity period for old keys (days)"
105 :convert(tonumber)
106 :default "1"
107 roll:flag "-r --remove-expired"
108 :description "Remove expired keys"
109 roll:option "-x --expire"
110 :argname("<days>")
111 :convert(tonumber)
112
113 local function printf(fmt, ...)
114 if fmt then
115 io.write(rspamd_logger.slog(fmt, ...))
116 end
117 io.write('\n')
118 end
119
120 local function maybe_printf(opts, fmt, ...)
121 if not opts.silent then
122 printf(fmt, ...)
123 end
124 end
125
126 local function highlight(str, color)
127 return ansicolors[color or 'white'] .. str .. ansicolors.reset
128 end
129
130 local function vault_url(opts, path)
131 local vault_path = opts.path
132
133 -- For KV v2, we need to add 'data' to the path for read/write operations
134 if opts.kv_version == 2 then
135 -- Split the path to inject 'data' after the mount point
136 -- e.g., 'secret/dkim' becomes 'secret/data/dkim'
137 local mount_point = vault_path:match('^([^/]+)')
138 local subpath = vault_path:match('^[^/]+/?(.*)')
139 if subpath and subpath ~= '' then
140 vault_path = mount_point .. '/data/' .. subpath
141 else
142 vault_path = mount_point .. '/data'
143 end
144 end
145
146 if path then
147 return string.format('%s/v1/%s/%s', opts.addr, vault_path, path)
148 end
149
150 return string.format('%s/v1/%s', opts.addr, vault_path)
151 end
152
153 local function vault_url_metadata(opts, path)
154 -- For KV v2 metadata operations (like list)
155 local vault_path = opts.path
156
157 if opts.kv_version == 2 then
158 local mount_point = vault_path:match('^([^/]+)')
159 local subpath = vault_path:match('^[^/]+/?(.*)')
160 if subpath and subpath ~= '' then
161 vault_path = mount_point .. '/metadata/' .. subpath
162 else
163 vault_path = mount_point .. '/metadata'
164 end
165 end
166
167 if path then
168 return string.format('%s/v1/%s/%s', opts.addr, vault_path, path)
169 end
170
171 return string.format('%s/v1/%s', opts.addr, vault_path)
172 end
173
174 local function is_http_error(err, data)
175 return err or (math.floor(data.code / 100) ~= 2)
176 end
177
178 local function parse_vault_reply(data)
179 local p = ucl.parser()
180 local res, parser_err = p:parse_string(data)
181
182 if not res then
183 return nil, parser_err
184 else
185 return p:get_object(), nil
186 end
187 end
188
189 local function maybe_print_vault_data(opts, data, func)
190 if data then
191 local res, parser_err = parse_vault_reply(data)
192
193 if not res then
194 printf('vault reply for cannot be parsed: %s', parser_err)
195 else
196 if func then
197 printf(ucl.to_format(func(res), opts.output))
198 else
199 printf(ucl.to_format(res, opts.output))
200 end
201 end
202 else
203 printf('no data received')
204 end
205 end
206
207 local function print_dkim_txt_record(b64, selector, alg)
208 local labels = {}
209 local prefix = string.format("v=DKIM1; k=%s; p=", alg)
210 b64 = prefix .. b64
211 if #b64 < 255 then
212 labels = { '"' .. b64 .. '"' }
213 else
214 for sl = 1, #b64, 256 do
215 table.insert(labels, '"' .. b64:sub(sl, sl + 255) .. '"')
216 end
217 end
218
219 printf("%s._domainkey IN TXT ( %s )", selector,
220 table.concat(labels, "\n\t"))
221 end
222
223 local function show_handler(opts, domain)
224 local uri = vault_url(opts, domain)
225 local err, data = rspamd_http.request {
226 config = rspamd_config,
227 ev_base = rspamadm_ev_base,
228 session = rspamadm_session,
229 resolver = rspamadm_dns_resolver,
230 url = uri,
231 headers = {
232 ['X-Vault-Token'] = opts.token
233 }
234 }
235
236 if is_http_error(err, data) then
237 printf('cannot get request to the vault (%s), HTTP error code %s', uri, data.code)
238 maybe_print_vault_data(opts, err)
239 os.exit(1)
240 else
241 maybe_print_vault_data(opts, data.content, function(obj)
242 -- For KV v2, data is nested under obj.data.data
243 -- For KV v1, data is under obj.data
244 local vault_data = opts.kv_version == 2 and obj.data.data or obj.data
245 if vault_data then
246 return vault_data.selectors
247 end
248 return nil
249 end)
250 end
251 end
252
253 local function delete_handler(opts, domain)
254 local uri = vault_url(opts, domain)
255 local err, data = rspamd_http.request {
256 config = rspamd_config,
257 ev_base = rspamadm_ev_base,
258 session = rspamadm_session,
259 resolver = rspamadm_dns_resolver,
260 url = uri,
261 method = 'delete',
262 headers = {
263 ['X-Vault-Token'] = opts.token
264 }
265 }
266
267 if is_http_error(err, data) then
268 printf('cannot get request to the vault (%s), HTTP error code %s', uri, data.code)
269 maybe_print_vault_data(opts, err)
270 os.exit(1)
271 else
272 printf('deleted key(s) for %s', domain)
273 end
274 end
275
276 local function list_handler(opts)
277 -- For KV v2, list operations use the metadata endpoint
278 local uri = opts.kv_version == 2 and vault_url_metadata(opts) or vault_url(opts)
279 local err, data = rspamd_http.request {
280 config = rspamd_config,
281 ev_base = rspamadm_ev_base,
282 session = rspamadm_session,
283 resolver = rspamadm_dns_resolver,
284 url = uri .. '?list=true',
285 headers = {
286 ['X-Vault-Token'] = opts.token
287 }
288 }
289
290 if is_http_error(err, data) then
291 printf('cannot get request to the vault (%s), HTTP error code %s', uri, data.code)
292 maybe_print_vault_data(opts, err)
293 os.exit(1)
294 else
295 maybe_print_vault_data(opts, data.content, function(obj)
296 return obj.data.keys
297 end)
298 end
299 end
300
301 -- Returns pair privkey+pubkey
302 local function genkey(opts)
303 return cr.gen_dkim_keypair(opts.algorithm, opts.bits)
304 end
305
306 local function create_and_push_key(opts, domain, existing)
307 local uri = vault_url(opts, domain)
308 local sk, pk = genkey(opts)
309
310 local payload = {
311 selectors = {
312 [1] = {
313 selector = opts.selector,
314 domain = domain,
315 key = tostring(sk),
316 pubkey = tostring(pk),
317 alg = opts.algorithm,
318 bits = opts.bits or 0,
319 valid_start = os.time(),
320 }
321 }
322 }
323
324 for _, sel in ipairs(existing) do
325 payload.selectors[#payload.selectors + 1] = sel
326 end
327
328 if opts.expire then
329 payload.selectors[1].valid_end = os.time() + opts.expire * 3600 * 24
330 end
331
332 -- For KV v2, wrap the payload in a 'data' object
333 local res = opts.kv_version == 2 and { data = payload } or payload
334
335 local err, data = rspamd_http.request {
336 config = rspamd_config,
337 ev_base = rspamadm_ev_base,
338 session = rspamadm_session,
339 resolver = rspamadm_dns_resolver,
340 url = uri,
341 method = 'put',
342 headers = {
343 ['Content-Type'] = 'application/json',
344 ['X-Vault-Token'] = opts.token
345 },
346 body = {
347 ucl.to_format(res, 'json-compact')
348 },
349 }
350
351 if is_http_error(err, data) then
352 printf('cannot get request to the vault (%s), HTTP error code %s', uri, data.code)
353 maybe_print_vault_data(opts, data.content)
354 os.exit(1)
355 else
356 maybe_printf(opts, 'stored key for: %s, selector: %s', domain, opts.selector)
357 maybe_printf(opts, 'please place the corresponding public key as following:')
358
359 if opts.silent then
360 printf('%s', pk)
361 else
362 print_dkim_txt_record(tostring(pk), opts.selector, opts.algorithm)
363 end
364 end
365 end
366
367 local function newkey_handler(opts, domain)
368 local uri = vault_url(opts, domain)
369
370 if not opts.selector then
371 opts.selector = string.format('%s-%s', opts.algorithm,
372 os.date("!%Y%m%d"))
373 end
374
375 local err, data = rspamd_http.request {
376 config = rspamd_config,
377 ev_base = rspamadm_ev_base,
378 session = rspamadm_session,
379 resolver = rspamadm_dns_resolver,
380 url = uri,
381 method = 'get',
382 headers = {
383 ['X-Vault-Token'] = opts.token
384 }
385 }
386
387 if is_http_error(err, data) or not data.content then
388 create_and_push_key(opts, domain, {})
389 else
390 -- Key exists
391 local rep = parse_vault_reply(data.content)
392
393 if not rep or not rep.data then
394 printf('cannot parse reply for %s: %s', uri, data.content)
395 os.exit(1)
396 end
397
398 -- For KV v2, data is nested under rep.data.data
399 -- For KV v1, data is under rep.data
400 local vault_data = opts.kv_version == 2 and rep.data.data or rep.data
401 local elts = vault_data and vault_data.selectors or nil
402
403 if not elts then
404 create_and_push_key(opts, domain, {})
405 os.exit(0)
406 end
407
408 for _, sel in ipairs(elts) do
409 if sel.alg == opts.algorithm then
410 printf('key with the specific algorithm %s is already presented at %s selector for %s domain',
411 opts.algorithm, sel.selector, domain)
412 os.exit(1)
413 else
414 create_and_push_key(opts, domain, elts)
415 end
416 end
417 end
418 end
419
420 local function roll_handler(opts, domain)
421 local uri = vault_url(opts, domain)
422 local payload = {
423 selectors = {}
424 }
425
426 local err, data = rspamd_http.request {
427 config = rspamd_config,
428 ev_base = rspamadm_ev_base,
429 session = rspamadm_session,
430 resolver = rspamadm_dns_resolver,
431 url = uri,
432 method = 'get',
433 headers = {
434 ['X-Vault-Token'] = opts.token
435 }
436 }
437
438 if is_http_error(err, data) or not data.content then
439 printf("No keys to roll for domain %s", domain)
440 os.exit(1)
441 else
442 local rep = parse_vault_reply(data.content)
443
444 if not rep or not rep.data then
445 printf('cannot parse reply for %s: %s', uri, data.content)
446 os.exit(1)
447 end
448
449 -- For KV v2, data is nested under rep.data.data
450 -- For KV v1, data is under rep.data
451 local vault_data = opts.kv_version == 2 and rep.data.data or rep.data
452 local elts = vault_data and vault_data.selectors or nil
453
454 if not elts then
455 printf("No keys to roll for domain %s", domain)
456 os.exit(1)
457 end
458
459 local nkeys = {} -- indexed by algorithm
460
461 local function insert_key(sel, add_expire)
462 if not nkeys[sel.alg] then
463 nkeys[sel.alg] = {}
464 end
465
466 if add_expire then
467 sel.valid_end = os.time() + opts.ttl * 3600 * 24
468 end
469
470 table.insert(nkeys[sel.alg], sel)
471 end
472
473 for _, sel in ipairs(elts) do
474 if sel.valid_end and sel.valid_end < os.time() then
475 if not opts.remove_expired then
476 insert_key(sel, false)
477 else
478 maybe_printf(opts, 'removed expired key for %s (selector %s, expire "%s"',
479 domain, sel.selector, os.date('%c', sel.valid_end))
480 end
481 else
482 insert_key(sel, true)
483 end
484 end
485
486 -- Now we need to ensure that all but one selectors have either expired or just a single key
487 for alg, keys in pairs(nkeys) do
488 table.sort(keys, function(k1, k2)
489 if k1.valid_end and k2.valid_end then
490 return k1.valid_end > k2.valid_end
491 elseif k1.valid_end then
492 return true
493 elseif k2.valid_end then
494 return false
495 end
496 return false
497 end)
498 -- Exclude the key with the highest expiration date and examine the rest
499 if not (#keys == 1 or fun.all(function(k)
500 return k.valid_end and k.valid_end < os.time()
501 end, fun.tail(keys))) then
502 printf('bad keys list for %s and %s algorithm', domain, alg)
503 fun.each(function(k)
504 if not k.valid_end then
505 printf('selector %s, algorithm %s has a key with no expire',
506 k.selector, k.alg)
507 elseif k.valid_end >= os.time() then
508 printf('selector %s, algorithm %s has a key that not yet expired: %s',
509 k.selector, k.alg, os.date('%c', k.valid_end))
510 end
511 end, fun.tail(keys))
512 os.exit(1)
513 end
514 -- Do not create new keys, if we only want to remove expired keys
515 if not opts.remove_expired then
516 -- OK to process
517 -- Insert keys for each algorithm in pairs <old_key(s)>, <new_key>
518 local sk, pk = genkey({ algorithm = alg, bits = keys[1].bits })
519 local selector = string.format('%s-%s', alg,
520 os.date("!%Y%m%d"))
521
522 if selector == keys[1].selector then
523 selector = selector .. '-1'
524 end
525 local nelt = {
526 selector = selector,
527 domain = domain,
528 key = tostring(sk),
529 pubkey = tostring(pk),
530 alg = alg,
531 bits = keys[1].bits,
532 valid_start = os.time(),
533 }
534
535 if opts.expire then
536 nelt.valid_end = os.time() + opts.expire * 3600 * 24
537 end
538
539 table.insert(payload.selectors, nelt)
540 end
541 for _, k in ipairs(keys) do
542 table.insert(payload.selectors, k)
543 end
544 end
545 end
546
547 -- For KV v2, wrap the payload in a 'data' object
548 local res = opts.kv_version == 2 and { data = payload } or payload
549
550 -- We can now store res in the vault
551 err, data = rspamd_http.request {
552 config = rspamd_config,
553 ev_base = rspamadm_ev_base,
554 session = rspamadm_session,
555 resolver = rspamadm_dns_resolver,
556 url = uri,
557 method = 'put',
558 headers = {
559 ['Content-Type'] = 'application/json',
560 ['X-Vault-Token'] = opts.token
561 },
562 body = {
563 ucl.to_format(res, 'json-compact')
564 },
565 }
566
567 if is_http_error(err, data) then
568 printf('cannot put request to the vault (%s), HTTP error code %s', uri, data.code)
569 maybe_print_vault_data(opts, data.content)
570 os.exit(1)
571 else
572 for _, key in ipairs(payload.selectors) do
573 if not key.valid_end or key.valid_end > os.time() + opts.ttl * 3600 * 24 then
574 maybe_printf(opts, 'rolled key for: %s, new selector: %s', domain, key.selector)
575 maybe_printf(opts, 'please place the corresponding public key as following:')
576
577 if opts.silent then
578 printf('%s', key.pubkey)
579 else
580 print_dkim_txt_record(key.pubkey, key.selector, key.alg)
581 end
582 end
583 end
584
585 maybe_printf(opts, 'your old keys will be valid until %s',
586 os.date('%c', os.time() + opts.ttl * 3600 * 24))
587 end
588 end
589
590 local function handler(args)
591 local opts = parser:parse(args)
592
593 if not opts.addr then
594 opts.addr = os.getenv('VAULT_ADDR')
595 end
596
597 if not opts.token then
598 opts.token = os.getenv('VAULT_TOKEN')
599 else
600 maybe_printf(opts, 'defining token via command line is insecure, define it via environment variable %s',
601 highlight('VAULT_TOKEN', 'red'))
602 end
603
604 if not opts.token or not opts.addr then
605 printf('no token or/and vault addr has been specified, exiting')
606 os.exit(1)
607 end
608
609 local command = opts.command
610
611 if command == 'list' then
612 list_handler(opts)
613 elseif command == 'show' then
614 fun.each(function(d)
615 show_handler(opts, d)
616 end, opts.domain)
617 elseif command == 'newkey' then
618 fun.each(function(d)
619 newkey_handler(opts, d)
620 end, opts.domain)
621 elseif command == 'roll' then
622 fun.each(function(d)
623 roll_handler(opts, d)
624 end, opts.domain)
625 elseif command == 'delete' then
626 fun.each(function(d)
627 delete_handler(opts, d)
628 end, opts.domain)
629 else
630 parser:error(string.format('command %s is not implemented', command))
631 end
632 end
633
634 return {
635 handler = handler,
636 description = parser._description,
637 name = 'vault'
638 }