feat(email): native SMTP-listener ingress (A3)
An opt-in receive-only SMTP listener (PICLOUD_SMTP_BIND) so an MX can point straight at PiCloud. It speaks minimal SMTP (HELO/EHLO/MAIL/RCPT/DATA/RSET/ NOOP/QUIT), resolves each RCPT TO mailbox to the app-owned email trigger that claims it, and inserts an Email outbox row the dispatcher fires — the same tail as the HMAC webhook, unchanged. - migration 0076: email_trigger_details.inbound_address (case-insensitively unique among app triggers) + TriggerRepo::email_inbound_target_by_address / SmtpInboundTarget; the interactive create-email API accepts inbound_address. - crates/picloud/src/smtp.rs: a small tokio accept loop + session state machine + a testable deliver() core + a minimal RFC-5322 header/body split. DATA is size-capped with dot-unstuffing; no AUTH/STARTTLS (TLS terminates upstream — the recipient address + per-app isolation are the boundary). - spawned in run_server alongside axum::serve, sharing the pool, on the same shutdown signal. Pinned by a picloud integration test (deliver → one Email outbox row for a known mailbox, none for an unknown one) + smtp.rs unit tests (address parse, header/body split). Multipart/MIME decoding is a documented follow-up. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
-- A3 raw SMTP ingress — an inbound email ADDRESS an email trigger listens on.
|
||||
--
|
||||
-- The HMAC-webhook path (v1.1.7) addresses a trigger by its UUID in the URL. A
|
||||
-- native SMTP listener instead receives `RCPT TO:<address>` and must resolve
|
||||
-- that address to the bound trigger. `inbound_address` is the mailbox the
|
||||
-- trigger claims; NULL for a webhook-only trigger. Case-insensitively unique
|
||||
-- among app-owned triggers so an address resolves to exactly one script.
|
||||
ALTER TABLE email_trigger_details
|
||||
ADD COLUMN inbound_address TEXT;
|
||||
|
||||
CREATE UNIQUE INDEX email_trigger_details_inbound_address_uidx
|
||||
ON email_trigger_details (lower(inbound_address))
|
||||
WHERE inbound_address IS NOT NULL;
|
||||
@@ -269,7 +269,8 @@ pub use trigger_repo::{
|
||||
collection_matches, CreateDeadLetterTrigger, CreateDocsTrigger, CreateEmailTrigger,
|
||||
CreateFilesTrigger, CreateKvTrigger, CreatePubsubTrigger, DeadLetterTriggerMatch,
|
||||
DocsTriggerMatch, EmailInboundTarget, FilesTriggerMatch, KvTriggerMatch, PostgresTriggerRepo,
|
||||
Trigger, TriggerDetails, TriggerDispatchMode, TriggerKind, TriggerRepo, TriggerRepoError,
|
||||
SmtpInboundTarget, Trigger, TriggerDetails, TriggerDispatchMode, TriggerKind, TriggerRepo,
|
||||
TriggerRepoError,
|
||||
};
|
||||
pub use triggers_api::{triggers_router, TriggersApiError, TriggersState};
|
||||
pub use users_admin_api::{app_users_router, AppUsersApiError, AppUsersState};
|
||||
|
||||
@@ -316,6 +316,21 @@ pub struct CreateEmailTrigger {
|
||||
/// Envelope version of the sealed inbound secret (0 = legacy no-AAD, 1 =
|
||||
/// AAD-bound to the sealing owner). `0` when there is no secret.
|
||||
pub inbound_secret_version: i16,
|
||||
/// A3: the mailbox this trigger receives at over the native SMTP listener.
|
||||
/// `None` = webhook-only (addressed by trigger UUID in the URL).
|
||||
pub inbound_address: Option<String>,
|
||||
pub registered_by_principal: AdminUserId,
|
||||
}
|
||||
|
||||
/// A3: the SMTP-ingress lookup result — the app-owned email trigger claiming an
|
||||
/// `RCPT TO` address, plus what an `Email` outbox row needs. No secret: the
|
||||
/// SMTP path has no HMAC (the mailbox address + per-app isolation are the
|
||||
/// boundary), unlike the webhook path.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SmtpInboundTarget {
|
||||
pub trigger_id: TriggerId,
|
||||
pub app_id: AppId,
|
||||
pub script_id: ScriptId,
|
||||
pub registered_by_principal: AdminUserId,
|
||||
}
|
||||
|
||||
@@ -475,6 +490,17 @@ pub trait TriggerRepo: Send + Sync {
|
||||
trigger_id: TriggerId,
|
||||
) -> Result<Option<EmailInboundTarget>, TriggerRepoError>;
|
||||
|
||||
/// A3: resolve an inbound SMTP `RCPT TO` address to the app-owned email
|
||||
/// trigger claiming it (case-insensitive). `None` when no enabled app
|
||||
/// trigger claims the address. Defaults to `None` so non-Postgres impls
|
||||
/// (tests) need not provide it.
|
||||
async fn email_inbound_target_by_address(
|
||||
&self,
|
||||
_address: &str,
|
||||
) -> Result<Option<SmtpInboundTarget>, TriggerRepoError> {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
async fn list_for_app(&self, app_id: AppId) -> Result<Vec<Trigger>, TriggerRepoError>;
|
||||
|
||||
/// Group-owned trigger TEMPLATES (§11 tail) declared directly at `group_id`.
|
||||
@@ -1420,13 +1446,15 @@ impl TriggerRepo for PostgresTriggerRepo {
|
||||
|
||||
sqlx::query(
|
||||
"INSERT INTO email_trigger_details \
|
||||
(trigger_id, inbound_secret_encrypted, inbound_secret_nonce, inbound_secret_version) \
|
||||
VALUES ($1, $2, $3, $4)",
|
||||
(trigger_id, inbound_secret_encrypted, inbound_secret_nonce, \
|
||||
inbound_secret_version, inbound_address) \
|
||||
VALUES ($1, $2, $3, $4, $5)",
|
||||
)
|
||||
.bind(parent.id)
|
||||
.bind(req.inbound_secret_encrypted.as_deref())
|
||||
.bind(req.inbound_secret_nonce.as_deref())
|
||||
.bind(req.inbound_secret_version)
|
||||
.bind(req.inbound_address.as_deref())
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
@@ -1493,6 +1521,33 @@ impl TriggerRepo for PostgresTriggerRepo {
|
||||
}))
|
||||
}
|
||||
|
||||
async fn email_inbound_target_by_address(
|
||||
&self,
|
||||
address: &str,
|
||||
) -> Result<Option<SmtpInboundTarget>, TriggerRepoError> {
|
||||
// `t.app_id IS NOT NULL` excludes group templates (only their
|
||||
// materialized per-app copies are invocable); `t.enabled` skips inert
|
||||
// triggers. The address is matched case-insensitively (mailboxes are).
|
||||
let row: Option<(Uuid, Uuid, Uuid, Uuid)> = sqlx::query_as(
|
||||
"SELECT t.id, t.app_id, t.script_id, t.registered_by_principal \
|
||||
FROM triggers t \
|
||||
JOIN email_trigger_details d ON d.trigger_id = t.id \
|
||||
WHERE lower(d.inbound_address) = lower($1) \
|
||||
AND t.kind = 'email' AND t.app_id IS NOT NULL AND t.enabled",
|
||||
)
|
||||
.bind(address)
|
||||
.fetch_optional(&self.pool)
|
||||
.await?;
|
||||
Ok(
|
||||
row.map(|(tid, app_id, script_id, principal)| SmtpInboundTarget {
|
||||
trigger_id: tid.into(),
|
||||
app_id: app_id.into(),
|
||||
script_id: script_id.into(),
|
||||
registered_by_principal: principal.into(),
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
async fn list_for_app(&self, app_id: AppId) -> Result<Vec<Trigger>, TriggerRepoError> {
|
||||
let parents: Vec<TriggerRow> = sqlx::query_as(
|
||||
"SELECT id, app_id, script_id, name, kind, enabled, sealed, shared, materialized_from, \
|
||||
|
||||
@@ -576,6 +576,10 @@ struct CreateEmailTriggerRequest {
|
||||
/// (or absent) means the trigger accepts unsigned POSTs.
|
||||
#[serde(default)]
|
||||
inbound_secret: Option<String>,
|
||||
/// A3: the mailbox this trigger receives at over the native SMTP listener
|
||||
/// (`RCPT TO`). Absent = webhook-only. Case-insensitively unique.
|
||||
#[serde(default)]
|
||||
inbound_address: Option<String>,
|
||||
}
|
||||
|
||||
async fn create_email_trigger(
|
||||
@@ -628,6 +632,10 @@ async fn create_email_trigger(
|
||||
inbound_secret_encrypted,
|
||||
inbound_secret_nonce,
|
||||
inbound_secret_version: version,
|
||||
inbound_address: input
|
||||
.inbound_address
|
||||
.map(|a| a.trim().to_string())
|
||||
.filter(|a| !a.is_empty()),
|
||||
registered_by_principal: principal.user_id,
|
||||
};
|
||||
let created = s.triggers.create_email_trigger(app_id, req).await?;
|
||||
|
||||
@@ -183,6 +183,7 @@ table: email_trigger_details
|
||||
inbound_secret_encrypted: bytea NULL
|
||||
inbound_secret_nonce: bytea NULL
|
||||
inbound_secret_version: smallint NOT NULL default=0
|
||||
inbound_address: text NULL
|
||||
|
||||
table: execution_logs
|
||||
id: uuid NOT NULL default=gen_random_uuid()
|
||||
@@ -614,6 +615,7 @@ indexes on docs_trigger_details:
|
||||
docs_trigger_details_pkey: public.docs_trigger_details USING btree (trigger_id)
|
||||
|
||||
indexes on email_trigger_details:
|
||||
email_trigger_details_inbound_address_uidx: public.email_trigger_details USING btree (lower(inbound_address)) WHERE (inbound_address IS NOT NULL)
|
||||
email_trigger_details_pkey: public.email_trigger_details USING btree (trigger_id)
|
||||
|
||||
indexes on execution_logs:
|
||||
@@ -1151,3 +1153,4 @@ constraints on workflows:
|
||||
0073: interceptors
|
||||
0074: interceptor phase
|
||||
0075: interceptor timeout
|
||||
0076: email inbound address
|
||||
|
||||
Reference in New Issue
Block a user