Debian Codegraph @

lualib/rspamadm/configgraph.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 local rspamd_logger = require "rspamd_logger"
18 local rspamd_util = require "rspamd_util"
19 local rspamd_regexp = require "rspamd_regexp"
20 local argparse = require "argparse"
21
22 -- Define command line options
23 local parser = argparse()
24 :name "rspamadm configgraph"
25 :description "Produces graph of Rspamd includes"
26 :help_description_margin(30)
27 parser:option "-c --config"
28 :description "Path to config file"
29 :argname("<file>")
30 :default(rspamd_paths["CONFDIR"] .. "/" .. "rspamd.conf")
31 parser:flag "-a --all"
32 :description('Show all nodes, not just existing ones')
33
34 local function process_filename(fname)
35 local cdir = rspamd_paths['CONFDIR'] .. '/'
36 fname = fname:gsub(cdir, '')
37 return fname
38 end
39
40 local function output_dot(opts, nodes, adjacency)
41 rspamd_logger.messagex("digraph rspamd {")
42 for k, node in pairs(nodes) do
43 local attrs = { "shape=box" }
44 local skip = false
45 if node.exists then
46 if node.priority >= 10 then
47 attrs[#attrs + 1] = "color=red"
48 elseif node.priority > 0 then
49 attrs[#attrs + 1] = "color=blue"
50 end
51 else
52 if opts.all then
53 attrs[#attrs + 1] = "style=dotted"
54 else
55 skip = true
56 end
57 end
58
59 if not skip then
60 rspamd_logger.messagex("\"%s\" [%s];", process_filename(k),
61 table.concat(attrs, ','))
62 end
63 end
64 for _, adj in ipairs(adjacency) do
65 local attrs = {}
66 local skip = false
67
68 if adj.to.exists then
69 if adj.to.merge then
70 attrs[#attrs + 1] = "arrowhead=diamond"
71 attrs[#attrs + 1] = "label=\"+\""
72 elseif adj.to.priority > 1 then
73 attrs[#attrs + 1] = "color=red"
74 end
75 else
76 if opts.all then
77 attrs[#attrs + 1] = "style=dotted"
78 else
79 skip = true
80 end
81 end
82
83 if not skip then
84 rspamd_logger.messagex("\"%s\" -> \"%s\" [%s];", process_filename(adj.from),
85 adj.to.short_path, table.concat(attrs, ','))
86 end
87 end
88 rspamd_logger.messagex("}")
89 end
90
91 local function load_config_traced(opts)
92 local glob_traces = {}
93 local adjacency = {}
94 local nodes = {}
95
96 local function maybe_match_glob(file)
97 for _, gl in ipairs(glob_traces) do
98 if gl.re:match(file) then
99 return gl
100 end
101 end
102
103 return nil
104 end
105
106 local function add_dep(from, node, args)
107 adjacency[#adjacency + 1] = {
108 from = from,
109 to = node,
110 args = args
111 }
112 end
113
114 local function process_node(fname, args)
115 local node = nodes[fname]
116 if not node then
117 node = {
118 path = fname,
119 short_path = process_filename(fname),
120 exists = rspamd_util.file_exists(fname),
121 merge = args.duplicate and args.duplicate == 'merge',
122 priority = args.priority or 0,
123 glob = args.glob,
124 try = args.try,
125 }
126 nodes[fname] = node
127 end
128
129 return node
130 end
131
132 local function trace_func(cur_file, included_file, args, parent)
133 if args.glob then
134 glob_traces[#glob_traces + 1] = {
135 re = rspamd_regexp.import_glob(included_file, ''),
136 parent = cur_file,
137 args = args,
138 seen = {},
139 }
140 else
141 local node = process_node(included_file, args)
142 if opts.all or node.exists then
143 local gl_parent = maybe_match_glob(included_file)
144 if gl_parent and not gl_parent.seen[cur_file] then
145 add_dep(gl_parent.parent, nodes[cur_file], gl_parent.args)
146 gl_parent.seen[cur_file] = true
147 end
148 add_dep(cur_file, node, args)
149 end
150 end
151 end
152
153 local _r, err = rspamd_config:load_ucl(opts['config'], trace_func)
154 if not _r then
155 rspamd_logger.errx('cannot parse %s: %s', opts['config'], err)
156 os.exit(1)
157 end
158
159 output_dot(opts, nodes, adjacency)
160 end
161
162 local function handler(args)
163 local res = parser:parse(args)
164
165 load_config_traced(res)
166 end
167
168 return {
169 handler = handler,
170 description = parser._description,
171 name = 'configgraph'
172 }