Comparison

util/prosodyctl/check.lua @ 13301:84d83f4a190f

util.prosodyctl.check: Wrap each check in a function One small refactor but one huge step in the right direction Mostly because adding another check would make the line checking for a valid check exceed the column limit.
author Kim Alvefur <zash@zash.se>
date Fri, 03 Nov 2023 22:16:53 +0100
parent 13256:53708752cc5d
child 13302:30b7cd40ee14
comparison
equal deleted inserted replaced
13300:b73547cfd736 13301:84d83f4a190f
313 local set = require "prosody.util.set"; 313 local set = require "prosody.util.set";
314 local it = require "prosody.util.iterators"; 314 local it = require "prosody.util.iterators";
315 local ok = true; 315 local ok = true;
316 local function disabled_hosts(host, conf) return host ~= "*" and conf.enabled ~= false; end 316 local function disabled_hosts(host, conf) return host ~= "*" and conf.enabled ~= false; end
317 local function enabled_hosts() return it.filter(disabled_hosts, pairs(configmanager.getconfig())); end 317 local function enabled_hosts() return it.filter(disabled_hosts, pairs(configmanager.getconfig())); end
318 if not (what == nil or what == "disabled" or what == "config" or what == "dns" or what == "certs" or what == "connectivity" or what == "turn") then 318 local checks = {};
319 show_warning("Don't know how to check '%s'. Try one of 'config', 'dns', 'certs', 'disabled', 'turn' or 'connectivity'.", what); 319 function checks.disabled()
320 show_warning("Note: The connectivity check will connect to a remote server.");
321 return 1;
322 end
323 if not what or what == "disabled" then
324 local disabled_hosts_set = set.new(); 320 local disabled_hosts_set = set.new();
325 for host in it.filter("*", pairs(configmanager.getconfig())) do 321 for host in it.filter("*", pairs(configmanager.getconfig())) do
326 if api(host):get_option_boolean("enabled") == false then 322 if api(host):get_option_boolean("enabled") == false then
327 disabled_hosts_set:add(host); 323 disabled_hosts_set:add(host);
328 end 324 end
333 show_warning(msg, tostring(disabled_hosts_set)); 329 show_warning(msg, tostring(disabled_hosts_set));
334 if what then return 0; end 330 if what then return 0; end
335 print"" 331 print""
336 end 332 end
337 end 333 end
338 if not what or what == "config" then 334 function checks.config()
339 print("Checking config..."); 335 print("Checking config...");
340 336
341 if what == "config" then 337 if what == "config" then
342 local files = configmanager.files(); 338 local files = configmanager.files();
343 print(" The following configuration files have been loaded:"); 339 print(" The following configuration files have been loaded:");
738 end 734 end
739 end 735 end
740 736
741 print("Done.\n"); 737 print("Done.\n");
742 end 738 end
743 if not what or what == "dns" then 739 function checks.dns()
744 local dns = require "prosody.net.dns"; 740 local dns = require "prosody.net.dns";
745 pcall(function () 741 pcall(function ()
746 local unbound = require"prosody.net.unbound"; 742 local unbound = require"prosody.net.unbound";
747 dns = unbound.dns; 743 dns = unbound.dns;
748 end) 744 end)
1113 print("For more information about DNS configuration please see https://prosody.im/doc/dns"); 1109 print("For more information about DNS configuration please see https://prosody.im/doc/dns");
1114 print(""); 1110 print("");
1115 ok = false; 1111 ok = false;
1116 end 1112 end
1117 end 1113 end
1118 if not what or what == "certs" then 1114 function checks.certs()
1119 local cert_ok; 1115 local cert_ok;
1120 print"Checking certificates..." 1116 print"Checking certificates..."
1121 local x509_verify_identity = require"prosody.util.x509".verify_identity; 1117 local x509_verify_identity = require"prosody.util.x509".verify_identity;
1122 local create_context = require "prosody.core.certmanager".create_context; 1118 local create_context = require "prosody.core.certmanager".create_context;
1123 local ssl = dependencies.softreq"ssl"; 1119 local ssl = dependencies.softreq"ssl";
1135 print("Checking certificate for "..host); 1131 print("Checking certificate for "..host);
1136 -- First, let's find out what certificate this host uses. 1132 -- First, let's find out what certificate this host uses.
1137 local host_ssl_config = configmanager.rawget(host, "ssl") 1133 local host_ssl_config = configmanager.rawget(host, "ssl")
1138 or configmanager.rawget(host:match("%.(.*)"), "ssl"); 1134 or configmanager.rawget(host:match("%.(.*)"), "ssl");
1139 local global_ssl_config = configmanager.rawget("*", "ssl"); 1135 local global_ssl_config = configmanager.rawget("*", "ssl");
1140 local ok, err, ssl_config = create_context(host, "server", host_ssl_config, global_ssl_config); 1136 local ctx_ok, err, ssl_config = create_context(host, "server", host_ssl_config, global_ssl_config);
1141 if not ok then 1137 if not ctx_ok then
1142 print(" Error: "..err); 1138 print(" Error: "..err);
1143 cert_ok = false 1139 cert_ok = false
1144 elseif not ssl_config.certificate then 1140 elseif not ssl_config.certificate then
1145 print(" No 'certificate' found for "..host) 1141 print(" No 'certificate' found for "..host)
1146 cert_ok = false 1142 cert_ok = false
1194 ok = false 1190 ok = false
1195 end 1191 end
1196 print("") 1192 print("")
1197 end 1193 end
1198 -- intentionally not doing this by default 1194 -- intentionally not doing this by default
1199 if what == "connectivity" then 1195 function checks.connectivity()
1200 local _, prosody_is_running = is_prosody_running(); 1196 local _, prosody_is_running = is_prosody_running();
1201 if api("*"):get_option_string("pidfile") and not prosody_is_running then 1197 if api("*"):get_option_string("pidfile") and not prosody_is_running then
1202 print("Prosody does not appear to be running, which is required for this test."); 1198 print("Prosody does not appear to be running, which is required for this test.");
1203 print("Start it and then try again."); 1199 print("Start it and then try again.");
1204 return 1; 1200 return 1;
1286 end 1282 end
1287 print("Note: The connectivity check only checks the reachability of the domain.") 1283 print("Note: The connectivity check only checks the reachability of the domain.")
1288 print("Note: It does not ensure that the check actually reaches this specific prosody instance.") 1284 print("Note: It does not ensure that the check actually reaches this specific prosody instance.")
1289 end 1285 end
1290 1286
1291 if not what or what == "turn" then 1287 function checks.turn()
1292 local turn_enabled_hosts = {}; 1288 local turn_enabled_hosts = {};
1293 local turn_services = {}; 1289 local turn_services = {};
1294 1290
1295 for host in enabled_hosts() do 1291 for host in enabled_hosts() do
1296 local has_external_turn = modulemanager.get_modules_for_host(host):contains("turn_external"); 1292 local has_external_turn = modulemanager.get_modules_for_host(host):contains("turn_external");
1361 else 1357 else
1362 print("Success!\n"); 1358 print("Success!\n");
1363 end 1359 end
1364 end 1360 end
1365 end 1361 end
1362 if what == nil or what == "all" then
1363 local ret;
1364 ret = checks.disabled();
1365 if ret ~= nil then return ret; end
1366 ret = checks.config();
1367 if ret ~= nil then return ret; end
1368 ret = checks.dns();
1369 if ret ~= nil then return ret; end
1370 ret = checks.certs();
1371 if ret ~= nil then return ret; end
1372 ret = checks.turn();
1373 if ret ~= nil then return ret; end
1374 elseif checks[what] then
1375 local ret = checks[what]();
1376 if ret ~= nil then return ret; end
1377 else
1378 show_warning("Don't know how to check '%s'. Try one of 'config', 'dns', 'certs', 'disabled', 'turn' or 'connectivity'.", what);
1379 show_warning("Note: The connectivity check will connect to a remote server.");
1380 return 1;
1381 end
1366 1382
1367 if not ok then 1383 if not ok then
1368 print("Problems found, see above."); 1384 print("Problems found, see above.");
1369 else 1385 else
1370 print("All checks passed, congratulations!"); 1386 print("All checks passed, congratulations!");