I’m trying to get the tenant ID based on the “from” domain from vault. Here’s my code inside init.lua:
function get_tenant_id(domain)
if domain == "example.com" then
return "test"
end
return kumo.secrets.load {
vault_mount = "secret",
vault_path = "tenants/" .. domain,
}
end
local cached_tenant_id = kumo.memoize(get_tenant_id, {
name = 'tenants',
ttl = '5 minutes',
capacity = 1000,
})
kumo.on('smtp_server_message_received', function(msg)
-- Assign tenant based on "from" domain.
local tenant = cached_tenant_id(msg:from_header().domain)
if tenant then
msg:set_meta('tenant', tenant)
msg:set_header('X-CUSTOMER-ID', tenant)
else
kumo.reject(
500,
string.format("from domain '%s' is not allowed.", msg.from_header().domain)
)
end
queue_helper:apply(msg)
dkim_signer(msg)
end)
kumo.on('http_message_generated', function(msg)
local tenant = cached_tenant_id(msg:from_header().domain)
if tenant then
msg:set_meta('tenant', tenant)
msg:set_header('X-CUSTOMER-ID', tenant)
else
kumo.reject(
500,
string.format("from domain '%s' is not allowed.", msg.from_header().domain)
)
end
queue_helper:apply(msg)
dkim_signer(msg)
end)
The error I get while injecting the message via SMTP:
421 4.3.0 ubuntu technical difficulties
The error I get while injecting the message via HTTP API:
{"success_count":0,"fail_count":1,"failed_recipients":["recipient@example.com"],"errors":["recipient@example.com: runtime error: [string \"/opt/kumomta/etc/policy/init.lua\"]:181: attempt to index a nil value\nstack traceback:\n\t[string \"/opt/kumomta/etc/policy/init.lua\"]:181: in function <[string \"/opt/kumomta/etc/policy/init.lua\"]:180>"]}
Line 181 in init.lua is local tenant = cached_tenant_id(msg:from_header().domain).
Also, just to make sure that the vault token is causing the issue, I changed the VAULT_TOKEN env variable in the systemctl file to the root token which doesn’t expire and should have full permission.
Dec 04 06:58:55 ubuntu systemd[1]: Started KumoMTA SMTP service.
Dec 04 06:58:55 ubuntu kumod[7763]: 2023-12-04T06:58:55.068435Z INFO localset-0 kumod: NodeId is 6c48c1dc-5f6a-487c-ab67-c3e6f426d6a6
Dec 04 06:58:55 ubuntu kumod[7763]: 2023-12-04T06:58:55.115695Z INFO localset-0 kumo_server_common::http_server: http listener on 0.0.0.0:8000
Dec 04 06:58:55 ubuntu kumod[7763]: 2023-12-04T06:58:55.116031Z INFO localset-0 kumod::smtp_server: smtp listener on 0.0.0.0:2525
Dec 04 06:58:55 ubuntu kumod[7763]: 2023-12-04T06:58:55.116157Z INFO localset-0 kumod::smtp_server: smtp listener on 0.0.0.0:25
Dec 04 06:58:55 ubuntu kumod[7763]: 2023-12-04T06:58:55.330341Z INFO localset-0 kumod::spool: start_spool: enumeration done, spooled in 0 msgs over 1.536179ms
Dec 04 06:58:55 ubuntu kumod[7763]: 2023-12-04T06:58:55.330362Z INFO localset-0 kumo_server_common::start: initialization complete
I’ve also added a few print() statements in the lua config to try and see where the issue is originating from, but they are not showing up anywhere in the logs.
I’d suggest adding in a print(msg:get_data() at the top of your http_message_generated event handler to see what your message looks like; the error message sounds to me as though msg:from_header() is returning nil, which suggests that it didn’t parse out the From header, and that it may not be present.
2023-12-04T14:52:03.872028Z INFO localset-0 kumod::spool: start_spool: enumeration done, spooled in 0 msgs over 1.550816ms
from Content-Type: text/plain;
charset="us-ascii"
Subject: hello
Mime-Version: 1.0
Hello there