mirror of
https://tangled.org/tranquil.farm/tranquil-pds
synced 2026-08-25 18:56:05 +00:00
Compare commits
57
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e4c9e1dab | ||
|
|
e9dc57d6f4 | ||
|
|
4e2525b245 | ||
|
|
38508c2c6e | ||
|
|
7a54ccf6a3 | ||
|
|
e85394c314 | ||
|
|
411c6be108 | ||
|
|
28aa7ab7fc | ||
|
|
348b69d76b | ||
|
|
4b23ca2c36 | ||
|
|
a7052e878c | ||
|
|
fc6063dba8 | ||
|
|
44cb016762 | ||
|
|
e6eee18ace | ||
|
|
96c8375706 | ||
|
|
86c5995568 | ||
|
|
036c317fd6 | ||
|
|
4d2c7d4723 | ||
|
|
f24a9f8bc0 | ||
|
|
f6ef6ecbd9 | ||
|
|
a2567bdb1a | ||
|
|
56120d252d | ||
|
|
210b0f463c | ||
|
|
c80f504dc0 | ||
|
|
a3bd7c59ad | ||
|
|
9ebde27540 | ||
|
|
28a7834304 | ||
|
|
191da5b311 | ||
|
|
a5b4ba7d65 | ||
|
|
90dabd8840 | ||
|
|
19eaccea74 | ||
|
|
f620a6bc43 | ||
|
|
04f370aaa1 | ||
|
|
3fd8f7ebbf | ||
|
|
0afcb2ee28 | ||
|
|
8bd556f65b | ||
|
|
cc92594506 | ||
|
|
76f22b801b | ||
|
|
021b7dbec4 | ||
|
|
eb034cb8b3 | ||
|
|
bdaf510898 | ||
|
|
deb2502112 | ||
|
|
1815ddba9f | ||
|
|
a7517ed5c9 | ||
|
|
d07d702dd4 | ||
|
|
1901b0a630 | ||
|
|
58f8d327c1 | ||
|
|
a13343e1de | ||
|
|
60e10af4aa | ||
|
|
f176f55862 | ||
|
|
fac9520a16 | ||
|
|
eee6fb9ff4 | ||
|
|
2462d0ab3b | ||
|
|
85f87f7b28 | ||
|
|
b1d86caa78 | ||
|
|
9b2cfb3a7e | ||
|
|
efd499bb26 |
@@ -68,6 +68,10 @@ test-group = "serial-env-tests"
|
||||
filter = "package(tranquil-signal)"
|
||||
test-group = "serial-env-tests"
|
||||
|
||||
[[profile.default.overrides]]
|
||||
filter = "package(tranquil-config)"
|
||||
test-group = "serial-env-tests"
|
||||
|
||||
[[profile.default.overrides]]
|
||||
filter = "binary(whole_story)"
|
||||
test-group = "heavy-load-tests"
|
||||
@@ -118,6 +122,10 @@ test-group = "serial-env-tests"
|
||||
filter = "package(tranquil-signal)"
|
||||
test-group = "serial-env-tests"
|
||||
|
||||
[[profile.ci.overrides]]
|
||||
filter = "package(tranquil-config)"
|
||||
test-group = "serial-env-tests"
|
||||
|
||||
[[profile.ci.overrides]]
|
||||
filter = "binary(whole_story)"
|
||||
test-group = "heavy-load-tests"
|
||||
|
||||
@@ -4,3 +4,5 @@ target/
|
||||
result
|
||||
frontend/node_modules/
|
||||
frontend/dist/
|
||||
frontend/.pnpm-store
|
||||
frontend/.npmrc
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT nextval('firehose_seq') as \"seq!\"",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "seq!",
|
||||
"type_info": "Int8"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": []
|
||||
},
|
||||
"nullable": [
|
||||
null
|
||||
]
|
||||
},
|
||||
"hash": "09142b5a6b91fe4fd795fb6c85af5461e2621f6c3545b6d26e4618d813904b75"
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "UPDATE repo_seq r\n SET seq = $1::bigint + (o.ord - 1)\n FROM unnest($2::bigint[]) WITH ORDINALITY AS o(id, ord)\n WHERE r.id = o.id",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Int8",
|
||||
"Int8Array"
|
||||
]
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "0b895d105363a60040323c5c47bc897905af3da980d0dc1b3b60bbdcdd90ea7a"
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n DELETE FROM repo_seq\n WHERE did = $1\n AND id <> (SELECT id FROM repo_seq WHERE did = $1 ORDER BY id DESC LIMIT 1)\n ",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "122d79e006c0e11214e3cf11cfb98bdff6fbf156893b482572532ebcd8979e61"
|
||||
}
|
||||
+8
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT id, did, handle, email, created_at, deactivated_at, takedown_ref, is_admin\n FROM users WHERE did = $1",
|
||||
"query": "SELECT id, did, handle, email, created_at, deactivated_at, takedown_ref, is_admin, inbound_migration\n FROM users WHERE handle = $1",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
@@ -42,6 +42,11 @@
|
||||
"ordinal": 7,
|
||||
"name": "is_admin",
|
||||
"type_info": "Bool"
|
||||
},
|
||||
{
|
||||
"ordinal": 8,
|
||||
"name": "inbound_migration",
|
||||
"type_info": "Bool"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
@@ -57,8 +62,9 @@
|
||||
false,
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "6b51995c40519a63f85c70f29ca8bd6ec1963c8562d78215d980785dc46a6384"
|
||||
"hash": "18bbda5582db1b32d02ab8a3eee970c9508b9bd67239c2f936639a9f863b30ff"
|
||||
}
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "UPDATE users SET deactivated_at = NULL WHERE did = $1",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "23201d4e26bc650939e30f69fb0bca00d351d057098afebc1017f70a84b4bd22"
|
||||
}
|
||||
+3
-3
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n INSERT INTO repo_seq (did, event_type, active, status)\n VALUES ($1, 'account', false, 'deleted')\n RETURNING seq\n ",
|
||||
"query": "\n INSERT INTO repo_seq (did, event_type, active, status)\n VALUES ($1, 'account', false, 'deleted')\n RETURNING id\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "seq",
|
||||
"name": "id",
|
||||
"type_info": "Int8"
|
||||
}
|
||||
],
|
||||
@@ -18,5 +18,5 @@
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "a0ea3630adb9e411d94d88084df6b05d7fc3ba8c306146fc932b736fe342eceb"
|
||||
"hash": "2c3c0740793f29b9bd9e0f81c2372d427dee091e20da8f71ae1e5f2769aa985d"
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT pg_try_advisory_xact_lock($1) as \"locked!\"",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "locked!",
|
||||
"type_info": "Bool"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Int8"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
null
|
||||
]
|
||||
},
|
||||
"hash": "3cc7247d2cd7e85a6f21d783dd956f8fecc6f63c994014bb4e91504d498b5fd5"
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n INSERT INTO repo_seq (did, event_type, handle)\n VALUES ($1, 'identity', $2)\n ",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text",
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "4f3ef746cb5fc0916d3df5f92494c538497d3b9c1cfcc6e44c9b80f2193094f6"
|
||||
}
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n INSERT INTO repo_seq (did, event_type, handle)\n VALUES ($1, 'identity', $2)\n RETURNING seq\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "seq",
|
||||
"type_info": "Int8"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text",
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "50b2178cd174af1dc132a22048e52a6af492f4d8025ec512dd9c0c9ba62eb202"
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "UPDATE users SET deactivated_at = NULL, inbound_migration = FALSE WHERE did = $1",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "51e029e572777e6a103fd7fd5550494de9d4cac7e3ff84e27ddec1a6aaefc047"
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "UPDATE comms_queue\n SET status = 'failed'::comms_status,\n attempts = max_attempts,\n last_error = $2,\n updated_at = NOW()\n WHERE id = $1",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Uuid",
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "5bee4ed5296667e4ca7e1a97aec28d30a470b8aee7b378ec9ca4e34de4faf349"
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT id as \"id!\" FROM repo_seq WHERE seq IS NULL ORDER BY id LIMIT $1",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "id!",
|
||||
"type_info": "Int8"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Int8"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "714a1c9a73e2fed8182d4ab2fe4a2c33bc10a52e27ed95d3a6641477ae42395e"
|
||||
}
|
||||
+4
-4
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT seq, did, created_at, event_type as \"event_type: RepoEventType\", commit_cid, prev_cid, prev_data_cid,\n ops, blobs, block_cids, block_data, blocks_cids, handle, active, status, rev\n FROM repo_seq\n WHERE seq = $1",
|
||||
"query": "SELECT seq as \"seq!\", did, created_at, event_type as \"event_type: RepoEventType\", commit_cid, prev_cid, prev_data_cid,\n ops, blobs, block_cids, block_data, blocks_cids, handle, active, status, rev\n FROM repo_seq\n WHERE seq = $1",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "seq",
|
||||
"name": "seq!",
|
||||
"type_info": "Int8"
|
||||
},
|
||||
{
|
||||
@@ -90,7 +90,7 @@
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
@@ -108,5 +108,5 @@
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "4702a94384ce938d3f5384bc2106a48500f8749134e6bfd1c619a9151f5d3772"
|
||||
"hash": "7cdf3b8136782a5b2e4aff91907a680566b4610e6cbf9d9396d31f9bbd99c3b8"
|
||||
}
|
||||
+4
-12
@@ -1,14 +1,8 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n INSERT INTO repo_seq (did, event_type, commit_cid, prev_cid, ops, blobs, block_cids, block_data, rev)\n VALUES ($1, 'commit', $2, $3::TEXT, $4, $5, $6, $7, $8)\n RETURNING seq\n ",
|
||||
"query": "\n INSERT INTO repo_seq (did, event_type, commit_cid, prev_cid, ops, blobs, block_cids, block_data, rev)\n VALUES ($1, 'commit', $2, $3::TEXT, $4, $5, $6, $7, $8)\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "seq",
|
||||
"type_info": "Int8"
|
||||
}
|
||||
],
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text",
|
||||
@@ -21,9 +15,7 @@
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "a325862f897484bb1a8863fd7a1bc3fd17ab20b6e30498f9900daf80609c413d"
|
||||
"hash": "800b93a38eef70bafdf0a1b221994f3c19ab562f84d18265376296a592b4c767"
|
||||
}
|
||||
+4
-12
@@ -1,14 +1,8 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n INSERT INTO repo_seq (did, event_type, commit_cid, prev_cid, ops, blobs, block_cids, block_data, prev_data_cid, rev)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)\n RETURNING seq\n ",
|
||||
"query": "\n INSERT INTO repo_seq (did, event_type, commit_cid, prev_cid, ops, blobs, block_cids, block_data, prev_data_cid, rev)\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "seq",
|
||||
"type_info": "Int8"
|
||||
}
|
||||
],
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text",
|
||||
@@ -23,9 +17,7 @@
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "97dd3cfe72b74b4dac686c030a07f6eb56dabb2df72815f3248760b4e3f7950e"
|
||||
"hash": "82276a34f473ff8b9790b78915ae3f72873d4b748cdb47bd2b81edc2f7a6e5a8"
|
||||
}
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "DELETE FROM repo_seq WHERE did = $1 AND seq != $2",
|
||||
"query": "DELETE FROM repo_seq WHERE did = $1 AND id <> $2",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
@@ -11,5 +11,5 @@
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "b6d6548acb89d6384cd226f6ed0d66de27fde3af24b4a7a3fce7e098812e38a5"
|
||||
"hash": "856e458c1c78f030545dc147f76d1b91fa4cc6e178eb51133c52358855ba3f02"
|
||||
}
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "UPDATE comms_queue\n SET status = 'processing', updated_at = NOW()\n WHERE id IN (\n SELECT id FROM comms_queue\n WHERE status = 'pending'\n AND scheduled_for <= $1\n AND attempts < max_attempts\n ORDER BY scheduled_for ASC\n LIMIT $2\n FOR UPDATE SKIP LOCKED\n )\n RETURNING\n id, user_id,\n channel as \"channel: CommsChannel\",\n comms_type as \"comms_type: CommsType\",\n status as \"status: CommsStatus\",\n recipient, subject, body, metadata,\n attempts, max_attempts, last_error,\n created_at, updated_at, scheduled_for, processed_at",
|
||||
"query": "UPDATE comms_queue\n SET status = 'processing', updated_at = NOW()\n WHERE id IN (\n SELECT id FROM comms_queue\n WHERE attempts < max_attempts\n AND scheduled_for <= $1\n AND (\n status = 'pending'\n OR (status = 'processing'\n AND updated_at < $1 - INTERVAL '10 minutes')\n )\n ORDER BY scheduled_for ASC\n LIMIT $2\n FOR UPDATE SKIP LOCKED\n )\n RETURNING\n id, user_id,\n channel as \"channel: CommsChannel\",\n comms_type as \"comms_type: CommsType\",\n status as \"status: CommsStatus\",\n recipient, subject, body, metadata,\n attempts, max_attempts, last_error,\n created_at, updated_at, scheduled_for, processed_at",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
@@ -154,5 +154,5 @@
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "8047fda41bd94f819213decb8b3e0aba49a8dbdb10217eefd77e3567f8c9694a"
|
||||
"hash": "890aa92acdcb0fe2a3bf04d87e1f16a801d271da7cedc32fc42c2ef5b100faae"
|
||||
}
|
||||
+4
-12
@@ -1,14 +1,8 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n INSERT INTO repo_seq (did, event_type, active, status)\n VALUES ($1, 'account', $2, $3)\n RETURNING seq\n ",
|
||||
"query": "\n INSERT INTO repo_seq (did, event_type, active, status)\n VALUES ($1, 'account', $2, $3)\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "seq",
|
||||
"type_info": "Int8"
|
||||
}
|
||||
],
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text",
|
||||
@@ -16,9 +10,7 @@
|
||||
"Text"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "623042c0b901e17ff37c5bcc1a4eefe56fc8b996ca8326e75796897d13c9e9ed"
|
||||
"hash": "8ccd42b5b1b75f45e094889ee26af8517de28ca687efa81fd9c4fce598338fe9"
|
||||
}
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n DELETE FROM oauth_token\n WHERE id IN (\n SELECT id FROM oauth_token\n WHERE did = $1\n ORDER BY updated_at ASC\n OFFSET $2\n )\n ",
|
||||
"query": "\n DELETE FROM oauth_token\n WHERE id IN (\n SELECT id FROM oauth_token\n WHERE did = $1\n ORDER BY created_at DESC\n OFFSET $2\n )\n ",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
@@ -11,5 +11,5 @@
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "56cd24903171eddc2ededd9079ffe10937c34e99b0305f25c980ca754da44625"
|
||||
"hash": "8f4357f7a18ddcf6b686a4555f244d37c35917364b8f917ca6ee2d4030ace742"
|
||||
}
|
||||
+4
-4
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT seq, did, created_at, event_type as \"event_type: RepoEventType\", commit_cid, prev_cid, prev_data_cid,\n ops, blobs, block_cids, block_data, blocks_cids, handle, active, status, rev\n FROM repo_seq\n WHERE seq > $1\n ORDER BY seq ASC",
|
||||
"query": "SELECT seq as \"seq!\", did, created_at, event_type as \"event_type: RepoEventType\", commit_cid, prev_cid, prev_data_cid,\n ops, blobs, block_cids, block_data, blocks_cids, handle, active, status, rev\n FROM repo_seq\n WHERE seq > $1\n ORDER BY seq ASC",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "seq",
|
||||
"name": "seq!",
|
||||
"type_info": "Int8"
|
||||
},
|
||||
{
|
||||
@@ -90,7 +90,7 @@
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
@@ -108,5 +108,5 @@
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "c47746c256fd8c9d386551f3bd270e9a96fecdb713642d07d60fb4aeb02c8426"
|
||||
"hash": "957bc81c63510ab502d2f8cea75dafcbdf2c8ebb448c86990d5e404ee5280438"
|
||||
}
|
||||
+4
-12
@@ -1,14 +1,8 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "\n INSERT INTO repo_seq (did, event_type, commit_cid, rev, block_cids, block_data)\n VALUES ($1, 'sync', $2, $3, $4, $5)\n RETURNING seq\n ",
|
||||
"query": "\n INSERT INTO repo_seq (did, event_type, commit_cid, rev, block_cids, block_data)\n VALUES ($1, 'sync', $2, $3, $4, $5)\n ",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "seq",
|
||||
"type_info": "Int8"
|
||||
}
|
||||
],
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Text",
|
||||
@@ -18,9 +12,7 @@
|
||||
"ByteaArray"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false
|
||||
]
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "5996774d097a484f81353d49e4a99b07c30a003377c924add576bfb48ddde29b"
|
||||
"hash": "9ba7869f29286cfd6b811ffa1e26bda1d637b98b44be66cdc265b1af4cec2d4f"
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT pg_advisory_xact_lock($1)",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "pg_advisory_xact_lock",
|
||||
"type_info": "Void"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Int8"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
null
|
||||
]
|
||||
},
|
||||
"hash": "a06e1d9f6f95e4c4c2b98310ebddcc9d963cc033582bf2e945e8bf3a301b4247"
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT setval('firehose_seq', $1)",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "setval",
|
||||
"type_info": "Int8"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
"Left": [
|
||||
"Int8"
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
null
|
||||
]
|
||||
},
|
||||
"hash": "bd0407818c8bdae1f1024777951b08cb9c386fe751722e1caf6ad243e271697a"
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "NOTIFY repo_updates",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Left": []
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "d4eacb9bca838dd18dcfd8ca39b83ef50968bfaaabc213536c049cc222ffe9c8"
|
||||
}
|
||||
+4
-4
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT seq, did, created_at, event_type as \"event_type: RepoEventType\", commit_cid, prev_cid, prev_data_cid,\n ops, blobs, block_cids, block_data, blocks_cids, handle, active, status, rev\n FROM repo_seq\n WHERE seq > $1\n ORDER BY seq ASC\n LIMIT $2",
|
||||
"query": "SELECT seq as \"seq!\", did, created_at, event_type as \"event_type: RepoEventType\", commit_cid, prev_cid, prev_data_cid,\n ops, blobs, block_cids, block_data, blocks_cids, handle, active, status, rev\n FROM repo_seq\n WHERE seq > $1\n ORDER BY seq ASC\n LIMIT $2",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "seq",
|
||||
"name": "seq!",
|
||||
"type_info": "Int8"
|
||||
},
|
||||
{
|
||||
@@ -91,7 +91,7 @@
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
@@ -109,5 +109,5 @@
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "89c8ce80d8c52b4668c34f63e48b51cacc7e8ff300958a192378f1d72de04f9a"
|
||||
"hash": "da2770e44c0ec21756ce4b6b0c57abff4f38cd8c680c5ced7d3b35f3c1f58da7"
|
||||
}
|
||||
+4
-4
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT seq, did, created_at, event_type as \"event_type: RepoEventType\", commit_cid, prev_cid, prev_data_cid,\n ops, blobs, block_cids, block_data, blocks_cids, handle, active, status, rev\n FROM repo_seq\n WHERE seq > $1\n ORDER BY seq ASC\n LIMIT $2",
|
||||
"query": "SELECT seq as \"seq!\", did, created_at, event_type as \"event_type: RepoEventType\", commit_cid, prev_cid, prev_data_cid,\n ops, blobs, block_cids, block_data, blocks_cids, handle, active, status, rev\n FROM repo_seq\n WHERE seq > $1 AND seq < $2\n ORDER BY seq ASC",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "seq",
|
||||
"name": "seq!",
|
||||
"type_info": "Int8"
|
||||
},
|
||||
{
|
||||
@@ -91,7 +91,7 @@
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
@@ -109,5 +109,5 @@
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "0349b2af4d21105029ecc2bf81ad7be9987ec17ac088730596849d7a45558e57"
|
||||
"hash": "dfe011b2d2bafbbbbea508a4977fc34bb1be4cf2612296df23763e6e5841c33e"
|
||||
}
|
||||
+4
-4
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT seq, did, created_at, event_type as \"event_type: RepoEventType\", commit_cid, prev_cid, prev_data_cid,\n ops, blobs, block_cids, block_data, blocks_cids, handle, active, status, rev\n FROM repo_seq\n WHERE seq > $1 AND seq < $2\n ORDER BY seq ASC",
|
||||
"query": "SELECT seq as \"seq!\", did, created_at, event_type as \"event_type: RepoEventType\", commit_cid, prev_cid, prev_data_cid,\n ops, blobs, block_cids, block_data, blocks_cids, handle, active, status, rev\n FROM repo_seq\n WHERE seq > $1\n ORDER BY seq ASC\n LIMIT $2",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"ordinal": 0,
|
||||
"name": "seq",
|
||||
"name": "seq!",
|
||||
"type_info": "Int8"
|
||||
},
|
||||
{
|
||||
@@ -91,7 +91,7 @@
|
||||
]
|
||||
},
|
||||
"nullable": [
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
@@ -109,5 +109,5 @@
|
||||
true
|
||||
]
|
||||
},
|
||||
"hash": "ad784aff9bb5c1f19f90a3b1c3b3694a3e1aa889b0fe3e55cfebb7af8c620256"
|
||||
"hash": "f16d41b41a5388997e6ed408da55056173340ed12b60b1a956de87f73f9d0824"
|
||||
}
|
||||
+8
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"db_name": "PostgreSQL",
|
||||
"query": "SELECT id, did, handle, email, created_at, deactivated_at, takedown_ref, is_admin\n FROM users WHERE handle = $1",
|
||||
"query": "SELECT id, did, handle, email, created_at, deactivated_at, takedown_ref, is_admin, inbound_migration\n FROM users WHERE did = $1",
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
@@ -42,6 +42,11 @@
|
||||
"ordinal": 7,
|
||||
"name": "is_admin",
|
||||
"type_info": "Bool"
|
||||
},
|
||||
{
|
||||
"ordinal": 8,
|
||||
"name": "inbound_migration",
|
||||
"type_info": "Bool"
|
||||
}
|
||||
],
|
||||
"parameters": {
|
||||
@@ -57,8 +62,9 @@
|
||||
false,
|
||||
true,
|
||||
true,
|
||||
false,
|
||||
false
|
||||
]
|
||||
},
|
||||
"hash": "584bceda60d8b6a02e7dc44d833e3fba13151f36ba9f75c64e33d6cb534cc939"
|
||||
"hash": "f1c4ec28b02d09ffce35aa8249c1747a70c12a3ecfc1ff6ca1847840f770db2f"
|
||||
}
|
||||
@@ -18,4 +18,4 @@ steps:
|
||||
- name: Build and push aarch64
|
||||
command: |
|
||||
SUBS="--option extra-substituters https://tranquil.cachix.org --option extra-trusted-public-keys tranquil.cachix.org-1:PoO+mGL6a6LcJiPakMDHN4E218/ei/7v2sxeDtNkSRg="
|
||||
nix-store -qR --include-outputs $(nix-store -qd $(nix build .#packages.x86_64-linux.tranquil-pds-aarch64 $SUBS --print-out-paths --no-link)) | grep -v '\.drv$' | cachix push "$CACHIX_CACHE_NAME"
|
||||
nix-store -qR --include-outputs $(nix-store -qd $(nix build .#packages.x86_64-linux.tranquil-pds-aarch64 $SUBS --print-out-paths --no-link)) | grep -v '\.drv$' | cachix push tranquil
|
||||
|
||||
@@ -18,11 +18,11 @@ steps:
|
||||
- name: Build and push x86_64
|
||||
command: |
|
||||
SUBS="--option extra-substituters https://tranquil.cachix.org --option extra-trusted-public-keys tranquil.cachix.org-1:PoO+mGL6a6LcJiPakMDHN4E218/ei/7v2sxeDtNkSRg="
|
||||
nix-store -qR --include-outputs $(nix-store -qd $(nix build .#packages.x86_64-linux.tranquil-pds $SUBS --print-out-paths --no-link)) | grep -v '\.drv$' | cachix push "$CACHIX_CACHE_NAME"
|
||||
nix-store -qR --include-outputs $(nix-store -qd $(nix build .#packages.x86_64-linux.tranquil-frontend $SUBS --print-out-paths --no-link)) | grep -v '\.drv$' | cachix push "$CACHIX_CACHE_NAME"
|
||||
nix-store -qR --include-outputs $(nix-store -qd $(nix build .#packages.x86_64-linux.tranquil-pds $SUBS --print-out-paths --no-link)) | grep -v '\.drv$' | cachix push tranquil
|
||||
nix-store -qR --include-outputs $(nix-store -qd $(nix build .#packages.x86_64-linux.tranquil-frontend $SUBS --print-out-paths --no-link)) | grep -v '\.drv$' | cachix push tranquil
|
||||
|
||||
- name: Build and push devShell
|
||||
command: |
|
||||
SUBS="--option extra-substituters https://tranquil.cachix.org --option extra-trusted-public-keys tranquil.cachix.org-1:PoO+mGL6a6LcJiPakMDHN4E218/ei/7v2sxeDtNkSRg="
|
||||
nix develop $SUBS --profile dev-profile -c true
|
||||
cachix push "$CACHIX_CACHE_NAME" dev-profile
|
||||
cachix push tranquil dev-profile
|
||||
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
# Contributing to Tranquil PDS
|
||||
|
||||
## When PRing
|
||||
|
||||
In order of importance:
|
||||
|
||||
- **You must run your change! Every contribution that says "here's xyz. untested." does not help the project.**
|
||||
- Relevant tests to your PR must pass. The whole suite doesn't have to be proven to have run, because there are a *ton* of tests and they're quite heavy, but hopefully there are existing tests for whatever you're PRing, and if there aren't, please add those too.
|
||||
- Run cargo fmt :P
|
||||
|
||||
> 🦪 Lewis
|
||||
>
|
||||
> Good CI fixes some of these. We should really get around to that.
|
||||
|
||||
Things that would also be nice but aren't like, a pain in our side:
|
||||
|
||||
- Big changes should be stacked PRs that are broken up into digestible pieces. Those stacked PRs should hopefully be able to be merged individually if necessary.
|
||||
|
||||
## Local Development
|
||||
|
||||
### Prerequisites
|
||||
|
||||
- [Docker](https://docs.docker.com/get-docker/) and Docker Compose
|
||||
- Add `pds.test` to your hosts file (one-time setup):
|
||||
|
||||
```
|
||||
127.0.0.1 pds.test
|
||||
```
|
||||
|
||||
- **macOS / Linux:** `/etc/hosts`
|
||||
- **Windows:** `C:\Windows\System32\drivers\etc\hosts`
|
||||
|
||||
### Starting the dev environment
|
||||
|
||||
```bash
|
||||
just run-dev
|
||||
```
|
||||
|
||||
This starts the following services via `docker-compose`:
|
||||
|
||||
- **Traefik** — HTTPS reverse proxy at `https://pds.test`
|
||||
- **Backend** — Rust server with `cargo-watch` (auto-rebuilds on file changes)
|
||||
- **Frontend** — Vite dev server with hot module replacement
|
||||
- **Postgres** — Database on port 5432
|
||||
- **PLC Directory** — Local [did-method-plc](https://github.com/did-method-plc/did-method-plc) server for DID registration
|
||||
- **Mailpit** — Local email server with web UI at [http://localhost:8025](http://localhost:8025)
|
||||
|
||||
Once all services are running, open **https://pds.test** in your browser.
|
||||
|
||||
### Trusting the self-signed certificate
|
||||
|
||||
Traefik generates a self-signed TLS certificate. Your browser will show a security warning on first visit. You can either click through it, or add the certificate to your system trust store for a seamless experience:
|
||||
|
||||
**macOS:**
|
||||
|
||||
```bash
|
||||
# Extract the cert from traefik and add it to the system keychain
|
||||
echo | openssl s_client -connect localhost:443 -servername pds.test 2>/dev/null | openssl x509 > /tmp/pds-test.pem
|
||||
sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain /tmp/pds-test.pem
|
||||
```
|
||||
|
||||
**Linux (Debian/Ubuntu):**
|
||||
|
||||
```bash
|
||||
echo | openssl s_client -connect localhost:443 -servername pds.test 2>/dev/null | openssl x509 | sudo tee /usr/local/share/ca-certificates/pds-test.crt
|
||||
sudo update-ca-certificates
|
||||
```
|
||||
|
||||
**Linux (Fedora/RHEL):**
|
||||
|
||||
```bash
|
||||
echo | openssl s_client -connect localhost:443 -servername pds.test 2>/dev/null | openssl x509 | sudo tee /etc/pki/ca-trust/source/anchors/pds-test.pem
|
||||
sudo update-ca-trust
|
||||
```
|
||||
|
||||
**Windows (PowerShell as Administrator):**
|
||||
|
||||
```powershell
|
||||
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
|
||||
$cert.Import([System.Text.Encoding]::UTF8.GetBytes((echo | openssl s_client -connect localhost:443 -servername pds.test 2>$null | openssl x509)))
|
||||
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store("Root", "LocalMachine")
|
||||
$store.Open("ReadWrite")
|
||||
$store.Add($cert)
|
||||
$store.Close()
|
||||
```
|
||||
|
||||
Restart your browser after adding the certificate.
|
||||
|
||||
### Stopping the dev environment
|
||||
|
||||
```bash
|
||||
# Stop containers (preserves database + build cache)
|
||||
docker compose --profile dev down
|
||||
|
||||
# Stop and wipe all data (fresh start)
|
||||
docker compose --profile dev down -v
|
||||
```
|
||||
|
||||
### Direct database access
|
||||
|
||||
Postgres is exposed on port 5432:
|
||||
|
||||
```bash
|
||||
psql postgres://postgres:postgres@localhost:5432/pds
|
||||
```
|
||||
|
||||
### How it works
|
||||
|
||||
- **Source code** is bind-mounted into the containers so that changes made on the host will be immediately reflected in the application
|
||||
- **Backend** uses `cargo-watch` to recompile and restart when Rust files change
|
||||
- **Frontend** uses Vite's HMR for instant browser updates when frontend files change
|
||||
- **Build cache** (`target/` directory and cargo registry) are stored in Docker volumes, so incremental compilation persists across container restarts
|
||||
- **Traefik** routes `/`, `/xrpc`, `/oauth`, `/.well-known`, `/u`, and `/health` to the backend; everything else goes to the Vite dev server
|
||||
- **Mailpit** captures all outgoing email — open [http://localhost:8025](http://localhost:8025) to view verification emails during registration
|
||||
- **PLC Directory** runs locally so DID registration doesn't hit the real `plc.directory`
|
||||
|
||||
### Running the backend natively
|
||||
|
||||
If you prefer running the Rust backend outside Docker (faster incremental builds on host), you need:
|
||||
|
||||
- Rust toolchain (see `rust-toolchain.toml`)
|
||||
- `protoc` (`brew install protobuf` on macOS)
|
||||
- PostgreSQL (start with `docker compose up db`)
|
||||
|
||||
Then run:
|
||||
|
||||
```bash
|
||||
cargo run -p tranquil-server -- --config config.toml
|
||||
```
|
||||
|
||||
And start the frontend separately:
|
||||
|
||||
```bash
|
||||
cd frontend && pnpm install && pnpm dev
|
||||
```
|
||||
Generated
+120
-30
@@ -9,7 +9,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "087113bd50d9adce24850eed5d0476c7d199d532fce8fab5173650331e09033a"
|
||||
dependencies = [
|
||||
"abnf-core",
|
||||
"nom",
|
||||
"nom 7.1.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -18,7 +18,7 @@ version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c44e09c43ae1c368fb91a03a566472d0087c26cf7e1b9e8e289c14ede681dd7d"
|
||||
dependencies = [
|
||||
"nom",
|
||||
"nom 7.1.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -213,7 +213,7 @@ dependencies = [
|
||||
"asn1-rs-derive",
|
||||
"asn1-rs-impl",
|
||||
"displaydoc",
|
||||
"nom",
|
||||
"nom 7.1.3",
|
||||
"num-traits",
|
||||
"rusticata-macros",
|
||||
"thiserror 1.0.69",
|
||||
@@ -1972,7 +1972,7 @@ checksum = "5cd0a5c643689626bec213c4d8bd4d96acc8ffdb4ad4bb6bc16abf27d5f4b553"
|
||||
dependencies = [
|
||||
"asn1-rs",
|
||||
"displaydoc",
|
||||
"nom",
|
||||
"nom 7.1.3",
|
||||
"num-bigint",
|
||||
"num-traits",
|
||||
"rusticata-macros",
|
||||
@@ -2216,6 +2216,22 @@ dependencies = [
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "email-encoding"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9298e6504d9b9e780ed3f7dfd43a61be8cd0e09eb07f7706a945b0072b6670b6"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "email_address"
|
||||
version = "0.2.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e079f19b08ca6239f47f8ba8509c11cf3ea30095831f7fed61441475edd8c449"
|
||||
|
||||
[[package]]
|
||||
name = "embedded-io"
|
||||
version = "0.4.0"
|
||||
@@ -3780,6 +3796,37 @@ version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2"
|
||||
|
||||
[[package]]
|
||||
name = "lettre"
|
||||
version = "0.11.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dabda5859ee7c06b995b9d1165aa52c39110e079ef609db97178d86aeb051fa7"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base64 0.22.1",
|
||||
"ed25519-dalek",
|
||||
"email-encoding",
|
||||
"email_address",
|
||||
"fastrand",
|
||||
"futures-io",
|
||||
"futures-util",
|
||||
"httpdate",
|
||||
"idna",
|
||||
"mime",
|
||||
"nom 8.0.0",
|
||||
"percent-encoding",
|
||||
"quoted_printable",
|
||||
"rsa",
|
||||
"rustls 0.23.37",
|
||||
"sha2",
|
||||
"socket2 0.6.3",
|
||||
"tokio",
|
||||
"tokio-rustls 0.26.4",
|
||||
"tracing",
|
||||
"url",
|
||||
"webpki-roots 1.0.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.183"
|
||||
@@ -4409,6 +4456,15 @@ dependencies = [
|
||||
"minimal-lexical",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "8.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df9761775871bdef83bee530e60050f7e54b1105350d6884eb0fb4f46c2f9405"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nonzero_ext"
|
||||
version = "0.3.0"
|
||||
@@ -4839,7 +4895,7 @@ checksum = "9114f9c1683dd09c5f4fa024c89fdad783eaae21d3d52dd23ddaaffa29ffb168"
|
||||
dependencies = [
|
||||
"either",
|
||||
"fnv",
|
||||
"nom",
|
||||
"nom 7.1.3",
|
||||
"once_cell",
|
||||
"postcard",
|
||||
"quick-xml",
|
||||
@@ -5427,6 +5483,12 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quoted_printable"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "478e0585659a122aa407eb7e3c0e1fa51b1d8a870038bd29f0cf4a8551eea972"
|
||||
|
||||
[[package]]
|
||||
name = "r-efi"
|
||||
version = "5.3.0"
|
||||
@@ -5833,7 +5895,7 @@ version = "4.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "faf0c4a6ece9950b9abdb62b1cfcf2a68b3b67a10ba445b3bb85be2a293d0632"
|
||||
dependencies = [
|
||||
"nom",
|
||||
"nom 7.1.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -6050,6 +6112,16 @@ dependencies = [
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "secrecy"
|
||||
version = "0.10.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e891af845473308773346dc847b2c23ee78fe442e0472ac50e22a18a93d3ae5a"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "security-framework"
|
||||
version = "3.7.0"
|
||||
@@ -7455,7 +7527,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-api"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"axum",
|
||||
@@ -7506,7 +7578,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-auth"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"base32",
|
||||
@@ -7529,7 +7601,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-cache"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base64 0.22.1",
|
||||
@@ -7543,11 +7615,19 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-comms"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base64 0.22.1",
|
||||
"chrono",
|
||||
"ed25519-dalek",
|
||||
"futures",
|
||||
"hickory-resolver",
|
||||
"lettre",
|
||||
"rand 0.8.5",
|
||||
"reqwest",
|
||||
"rsa",
|
||||
"secrecy",
|
||||
"serde_json",
|
||||
"sqlx",
|
||||
"thiserror 2.0.18",
|
||||
@@ -7561,7 +7641,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-config"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"confique",
|
||||
"serde",
|
||||
@@ -7569,7 +7649,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-crypto"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"aes-gcm",
|
||||
"base64 0.22.1",
|
||||
@@ -7585,7 +7665,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-db"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"chrono",
|
||||
@@ -7602,7 +7682,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-db-traits"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base64 0.22.1",
|
||||
@@ -7618,7 +7698,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-infra"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"bytes",
|
||||
@@ -7629,7 +7709,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-lexicon"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"futures",
|
||||
@@ -7648,7 +7728,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-oauth"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"axum",
|
||||
@@ -7671,7 +7751,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-oauth-server"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"axum",
|
||||
"base64 0.22.1",
|
||||
@@ -7704,7 +7784,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-pds"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"aes-gcm",
|
||||
"anyhow",
|
||||
@@ -7744,6 +7824,7 @@ dependencies = [
|
||||
"multibase",
|
||||
"multihash",
|
||||
"p256 0.13.2",
|
||||
"parking_lot",
|
||||
"rand 0.8.5",
|
||||
"redis",
|
||||
"regex",
|
||||
@@ -7796,7 +7877,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-repo"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"cid",
|
||||
@@ -7808,7 +7889,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-ripple"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"backon",
|
||||
@@ -7833,7 +7914,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-scopes"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"axum",
|
||||
"futures",
|
||||
@@ -7849,15 +7930,24 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-server"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"arc-swap",
|
||||
"axum",
|
||||
"clap",
|
||||
"dotenvy",
|
||||
"ed25519-dalek",
|
||||
"futures-util",
|
||||
"hex",
|
||||
"hyper 1.8.1",
|
||||
"hyper-util",
|
||||
"rustls 0.23.37",
|
||||
"rustls-pemfile",
|
||||
"thiserror 2.0.18",
|
||||
"tokio",
|
||||
"tokio-rustls 0.26.4",
|
||||
"tokio-util",
|
||||
"tower",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
"tranquil-api",
|
||||
@@ -7870,7 +7960,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-signal"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"chrono",
|
||||
@@ -7893,7 +7983,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-storage"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"aws-config",
|
||||
@@ -7910,7 +8000,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-store"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"bytes",
|
||||
@@ -7959,7 +8049,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-sync"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"axum",
|
||||
@@ -7981,7 +8071,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tranquil-types"
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"cid",
|
||||
@@ -8496,7 +8586,7 @@ dependencies = [
|
||||
"base64urlsafedata",
|
||||
"der-parser",
|
||||
"hex",
|
||||
"nom",
|
||||
"nom 7.1.3",
|
||||
"openssl",
|
||||
"openssl-sys",
|
||||
"rand 0.9.2",
|
||||
@@ -9068,7 +9158,7 @@ dependencies = [
|
||||
"data-encoding",
|
||||
"der-parser",
|
||||
"lazy_static",
|
||||
"nom",
|
||||
"nom 7.1.3",
|
||||
"oid-registry",
|
||||
"rusticata-macros",
|
||||
"thiserror 1.0.69",
|
||||
|
||||
+11
-2
@@ -26,7 +26,7 @@ members = [
|
||||
]
|
||||
|
||||
[workspace.package]
|
||||
version = "0.5.7"
|
||||
version = "0.6.4"
|
||||
edition = "2024"
|
||||
license = "AGPL-3.0-or-later"
|
||||
|
||||
@@ -59,6 +59,7 @@ presage = { git = "https://github.com/whisperfish/presage", rev = "fe3ed54c4844a
|
||||
unicode-segmentation = "1"
|
||||
|
||||
aes-gcm = "0.10"
|
||||
arc-swap = "1"
|
||||
backon = "1"
|
||||
bincode = { version = "2", features = ["serde"] }
|
||||
anyhow = "1.0"
|
||||
@@ -86,6 +87,8 @@ hickory-resolver = { version = "0.24", features = ["tokio-runtime"] }
|
||||
hkdf = "0.12"
|
||||
hmac = "0.12"
|
||||
http = "1.4"
|
||||
hyper = { version = "1", features = ["server", "http1", "http2"] }
|
||||
hyper-util = { version = "0.1", features = ["server", "server-auto", "server-graceful", "service", "tokio"] }
|
||||
image = { version = "0.25", default-features = false, features = ["jpeg", "png", "gif", "webp"] }
|
||||
qrcodegen = "1.8"
|
||||
infer = "0.19"
|
||||
@@ -93,6 +96,7 @@ ipld-core = "0.4"
|
||||
iroh-car = "0.5"
|
||||
jacquard-common = { version = "0.9", features = ["crypto-k256"] }
|
||||
jacquard-repo = "0.9"
|
||||
lettre = { version = "0.11", default-features = false, features = ["builder", "smtp-transport", "tokio1", "tokio1-rustls-tls", "pool", "dkim", "tracing"] }
|
||||
jsonwebtoken = { version = "10.2", features = ["rust_crypto"] }
|
||||
k256 = { version = "0.13", features = ["ecdsa", "pem", "pkcs8"] }
|
||||
metrics = "0.24"
|
||||
@@ -105,6 +109,10 @@ p384 = { version = "0.13", features = ["ecdsa"] }
|
||||
rand = "0.8"
|
||||
redis = { version = "1.0", features = ["tokio-comp", "connection-manager"] }
|
||||
regex = "1"
|
||||
rsa = "0.9"
|
||||
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12", "logging"] }
|
||||
rustls-pemfile = "2"
|
||||
secrecy = { version = "0.10", features = ["serde"] }
|
||||
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls-webpki-roots", "http2", "charset", "macos-system-configuration"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_bytes = "0.11"
|
||||
@@ -116,8 +124,9 @@ sqlx = { version = "0.8", features = ["runtime-tokio-rustls", "postgres", "uuid"
|
||||
subtle = "2.5"
|
||||
thiserror = "2.0"
|
||||
tokio = { version = "1.48", features = ["macros", "rt-multi-thread", "time", "signal", "process", "io-util", "fs"] }
|
||||
tokio-util = "0.7.18"
|
||||
tokio-util = { version = "0.7.18", features = ["rt"] }
|
||||
tokio-tungstenite = { version = "0.28", features = ["rustls-tls-webpki-roots"] }
|
||||
tokio-rustls = { version = "0.26", default-features = false, features = ["ring", "tls12", "logging"] }
|
||||
totp-rs = { version = "5", features = ["qr"] }
|
||||
tower = "0.5"
|
||||
tower-http = { version = "0.6", features = ["fs", "cors"] }
|
||||
|
||||
+4
-5
@@ -1,12 +1,12 @@
|
||||
FROM node:24-alpine AS frontend
|
||||
RUN corepack enable && corepack prepare pnpm@latest --activate
|
||||
WORKDIR /app
|
||||
COPY frontend/package.json frontend/pnpm-lock.yaml ./
|
||||
COPY frontend/package.json frontend/pnpm-lock.yaml frontend/pnpm-workspace.yaml ./
|
||||
RUN pnpm install --frozen-lockfile
|
||||
COPY frontend/ ./
|
||||
RUN pnpm build
|
||||
|
||||
FROM rust:1.92-alpine AS builder
|
||||
FROM rust:1.96-alpine AS builder
|
||||
RUN apk add --no-cache ca-certificates musl-dev pkgconfig openssl-dev openssl-libs-static mold clang protoc
|
||||
ENV RUSTFLAGS="-C linker=clang -C link-arg=-fuse-ld=mold"
|
||||
WORKDIR /app
|
||||
@@ -46,12 +46,11 @@ RUN --mount=type=cache,target=/usr/local/cargo/registry \
|
||||
cp target/release/tranquil-server /tmp/tranquil-pds
|
||||
|
||||
FROM alpine:3.23
|
||||
RUN apk add --no-cache msmtp ca-certificates \
|
||||
&& ln -sf /usr/bin/msmtp /usr/sbin/sendmail
|
||||
RUN apk add --no-cache ca-certificates
|
||||
COPY --from=builder /tmp/tranquil-pds /usr/local/bin/tranquil-pds
|
||||
COPY --from=frontend /app/dist /var/lib/tranquil-pds/frontend
|
||||
WORKDIR /app
|
||||
ENV SERVER_HOST=0.0.0.0
|
||||
ENV SERVER_HOST=[::]
|
||||
ENV SERVER_PORT=3000
|
||||
EXPOSE 3000
|
||||
CMD ["tranquil-pds"]
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
FROM rust:1.96-alpine
|
||||
|
||||
RUN apk add --no-cache \
|
||||
ca-certificates \
|
||||
musl-dev \
|
||||
pkgconfig \
|
||||
openssl-dev \
|
||||
openssl-libs-static \
|
||||
mold \
|
||||
clang \
|
||||
protoc
|
||||
|
||||
RUN cargo install cargo-watch
|
||||
|
||||
ENV RUSTFLAGS="-C linker=clang -C link-arg=-fuse-ld=mold"
|
||||
ENV SQLX_OFFLINE=true
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
CMD ["cargo", "watch", "-x", "run -p tranquil-server -- --config /app/config.dev.toml"]
|
||||
@@ -6,7 +6,7 @@ A Personal Data Server for the AT Protocol.
|
||||
|
||||
We came together to make this PDS to enable and empower our users to better host their data on this shared protocol. All of our decisions as a project are guided by their usefulness to the community: PDS hosters and end-users both.
|
||||
|
||||
Comparatively: Bluesky the company created a "reference PDS" that we can self-host quite easily, and that's great, but Bluesky has an incentive to make software for themselvess first & foremost, then secondly their software can be useful for us self-hosters. In contrast, Tranquil is not from a company, and will never be.
|
||||
Comparatively: Bluesky the company created a "reference PDS" that we can self-host quite easily, and that's great, but Bluesky has an incentive to make software for themselves first & foremost, then secondly their software can be useful for us self-hosters. In contrast, Tranquil is not from a company, and will never be.
|
||||
|
||||
## What's different about Tranquil PDS
|
||||
|
||||
@@ -20,7 +20,7 @@ It is a superset of the reference PDS, including:
|
||||
- account delegation: letting others manage an account with configurable permission levels
|
||||
- a built-in web UI for account management, repo browsing, and admin
|
||||
|
||||
Unlike the ref PDS, Tranquil itself is compiled to a single binary with no nodeJS runtime. However, at time of writing, Tranquil requires postgres running separately.
|
||||
Unlike the ref PDS, Tranquil is a single binary with no nodejs runtime. That said, at time of writing, Tranquil does require postgres running separately.
|
||||
|
||||
## Quick Start
|
||||
|
||||
@@ -46,24 +46,28 @@ just test
|
||||
just lint
|
||||
```
|
||||
|
||||
Nix users can enter a devshell with `nix develop`, or `direnv allow` to auto-enter via the bundled `.envrc`. Pre-built artifacts (including the devshell) are available from our [binary cache](docs/install-nix.md#binary-cache).
|
||||
Nix users can enter a devshell with `nix develop`, or `direnv allow` to auto-enter via the bundled `.envrc`. Pre-built artifacts including the devshell are available from our [binary cache](docs/2_INSTALL_NIX.md#binary-cache).
|
||||
|
||||
## Production Deployment
|
||||
|
||||
### Quick Deploy (Docker/Podman Compose)
|
||||
|
||||
Edit `config.toml` with your values. Generate secrets with `openssl rand -base64 48`.
|
||||
`docker-compose.prod.yaml` pulls the prebuilt image `atcr.io/tranquil.farm/tranquil-pds:latest`. Sign in to the registry first with `podman login atcr.io`. The Containers guide covers building from source.
|
||||
|
||||
```bash
|
||||
cp example.toml config.toml
|
||||
```
|
||||
|
||||
Edit `config.toml` with your values and generate secrets with `openssl rand -base64 48`. Set the postgres password to match `docker-compose.prod.yaml`. nginx needs a TLS certificate before it starts, so follow the wildcard cert steps in the [Containers guide](docs/2_INSTALL_CONTAINERS.md).
|
||||
|
||||
```bash
|
||||
podman-compose -f docker-compose.prod.yaml up -d
|
||||
```
|
||||
|
||||
### Installation Guides
|
||||
|
||||
- [Nix](docs/install-nix.md)
|
||||
- [Containers](docs/install-containers.md)
|
||||
- [Kubernetes](docs/install-kubernetes.md)
|
||||
- [Nix](docs/2_INSTALL_NIX.md)
|
||||
- [Containers](docs/2_INSTALL_CONTAINERS.md)
|
||||
|
||||
## Community
|
||||
|
||||
@@ -76,7 +80,7 @@ We currently don't have a shared space to chat and organize Tranquil things, but
|
||||
- [@oyster.cafe](https://tangled.org/did:plc:3fwecdnvtcscjnrx2p4n7alz)
|
||||
- [@nel.pet](https://tangled.org/did:plc:h5wsnqetncv6lu2weom35lg2)
|
||||
|
||||
### Amazing contributers
|
||||
### Amazing contributors
|
||||
|
||||
- [@isabelroses.com](https://tangled.org/did:plc:qxichs7jsycphrsmbujwqbfb)
|
||||
- [@quilling.dev](https://tangled.org/did:plc:jrtgsidnmxaen4offglr5lsh)
|
||||
@@ -87,6 +91,7 @@ We currently don't have a shared space to chat and organize Tranquil things, but
|
||||
- [@a.starrysky.fyi](https://tangled.org/did:plc:uuyqs6y3pwtbteet4swt5i5y)
|
||||
- [@sans-self.org](https://tangled.org/did:plc:wydyrngmxbcsqdvhmd7whmye)
|
||||
- [@tachyonism.tngl.sh](https://tangled.org/did:plc:w6qiwij62bmdugsd3gemhpy2)
|
||||
- [@trezy.codes](https://tangled.org/did:plc:4jrld6fwpnwqehtce56qshzv)
|
||||
- Could be your name here too!
|
||||
|
||||
### Tranquil PDS instances in the wild!
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
[server]
|
||||
hostname = "pds.test"
|
||||
allow_http_proxy = true
|
||||
invite_code_required = false
|
||||
disable_rate_limiting = true
|
||||
|
||||
[frontend]
|
||||
enabled = true
|
||||
dir = "/app/frontend/public"
|
||||
|
||||
[database]
|
||||
url = "postgres://postgres:postgres@db:5432/pds"
|
||||
|
||||
[storage]
|
||||
path = "/var/lib/tranquil-pds/blobs"
|
||||
|
||||
[plc]
|
||||
directory_url = "http://plc:2582"
|
||||
|
||||
[email]
|
||||
from_address = "noreply@pds.test"
|
||||
from_name = "Tranquil PDS (Dev)"
|
||||
|
||||
[email.smarthost]
|
||||
host = "mailpit"
|
||||
port = 1025
|
||||
tls = "none"
|
||||
|
||||
[secrets]
|
||||
allow_insecure = true
|
||||
@@ -9,7 +9,6 @@ use tranquil_pds::state::AppState;
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SignalStatusOutput {
|
||||
pub enabled: bool,
|
||||
pub linked: bool,
|
||||
}
|
||||
|
||||
@@ -23,13 +22,12 @@ pub async fn get_signal_status(
|
||||
State(state): State<AppState>,
|
||||
_auth: Auth<Admin>,
|
||||
) -> Result<Json<SignalStatusOutput>, ApiError> {
|
||||
let enabled = tranquil_config::get().signal.enabled;
|
||||
let linked = match &state.signal_sender {
|
||||
Some(slot) => slot.is_linked().await,
|
||||
None => false,
|
||||
};
|
||||
|
||||
Ok(Json(SignalStatusOutput { enabled, linked }))
|
||||
Ok(Json(SignalStatusOutput { linked }))
|
||||
}
|
||||
|
||||
pub async fn link_signal_device(
|
||||
|
||||
@@ -510,6 +510,7 @@ pub async fn create_account(
|
||||
telegram_username: comms.telegram,
|
||||
signal_username: comms.signal,
|
||||
deactivated_at,
|
||||
inbound_migration: is_migration || is_did_web_byod,
|
||||
encrypted_key_bytes: repo.encrypted_key_bytes,
|
||||
encryption_version: tranquil_pds::config::ENCRYPTION_VERSION,
|
||||
reserved_key_id,
|
||||
|
||||
@@ -129,20 +129,13 @@ pub async fn submit_plc_operation(
|
||||
.await
|
||||
.map_err(ApiError::from)?;
|
||||
|
||||
match state
|
||||
if let Err(e) = state
|
||||
.repos
|
||||
.repo
|
||||
.insert_identity_event(did, Some(&user.handle))
|
||||
.await
|
||||
{
|
||||
Ok(seq) => {
|
||||
if let Err(e) = state.repos.repo.notify_update(seq).await {
|
||||
warn!("Failed to notify identity event: {:?}", e);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("Failed to sequence identity event: {:?}", e);
|
||||
}
|
||||
warn!("Failed to sequence identity event: {:?}", e);
|
||||
}
|
||||
let _ = state
|
||||
.cache
|
||||
|
||||
@@ -106,7 +106,7 @@ pub async fn import_repo(
|
||||
.map(|c| c.import.skip_verification)
|
||||
.unwrap_or(false)
|
||||
});
|
||||
let is_migration = user.deactivated_at.is_some();
|
||||
let is_migration = user.inbound_migration && user.deactivated_at.is_some();
|
||||
if skip_verification {
|
||||
warn!("Skipping all CAR verification for import (SKIP_IMPORT_VERIFICATION=true)");
|
||||
} else if is_migration {
|
||||
@@ -417,7 +417,6 @@ async fn sequence_import_event(
|
||||
rev: None,
|
||||
};
|
||||
|
||||
let seq = state.repos.repo.insert_commit_event(&data).await?;
|
||||
state.repos.repo.notify_update(seq).await?;
|
||||
state.repos.repo.insert_commit_event(&data).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -665,27 +665,22 @@ pub async fn delete_account(
|
||||
error!("DB error deleting account: {:?}", e);
|
||||
ApiError::InternalError(None)
|
||||
})?;
|
||||
let account_seq = tranquil_pds::repo_ops::sequence_account_event(
|
||||
if let Err(e) = tranquil_pds::repo_ops::sequence_account_event(
|
||||
&state,
|
||||
did,
|
||||
tranquil_db_traits::AccountStatus::Deleted,
|
||||
)
|
||||
.await;
|
||||
match account_seq {
|
||||
Ok(seq) => {
|
||||
if let Err(e) = state.repos.repo.delete_sequences_except(did, seq).await {
|
||||
warn!(
|
||||
"Failed to cleanup sequences for deleted account {}: {}",
|
||||
did, e
|
||||
);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
warn!(
|
||||
"Failed to sequence account deletion event for {}: {}",
|
||||
did, e
|
||||
);
|
||||
}
|
||||
.await
|
||||
{
|
||||
warn!(
|
||||
"Failed to sequence account deletion event for {}: {}",
|
||||
did, e
|
||||
);
|
||||
} else if let Err(e) = state.repos.repo.purge_did_events_keeping_latest(did).await {
|
||||
warn!(
|
||||
"Failed to cleanup sequences for deleted account {}: {}",
|
||||
did, e
|
||||
);
|
||||
}
|
||||
let _ = state
|
||||
.cache
|
||||
|
||||
@@ -5,7 +5,7 @@ use tranquil_pds::BUILD_VERSION;
|
||||
use tranquil_pds::state::AppState;
|
||||
use tranquil_pds::util::{discord_app_id, discord_bot_username, telegram_bot_username};
|
||||
|
||||
fn get_available_comms_channels() -> Vec<CommsChannel> {
|
||||
async fn get_available_comms_channels(state: &AppState) -> Vec<CommsChannel> {
|
||||
let cfg = tranquil_config::get();
|
||||
let mut channels = vec![CommsChannel::Email];
|
||||
if cfg.discord.bot_token.is_some() {
|
||||
@@ -14,7 +14,9 @@ fn get_available_comms_channels() -> Vec<CommsChannel> {
|
||||
if cfg.telegram.bot_token.is_some() {
|
||||
channels.push(CommsChannel::Telegram);
|
||||
}
|
||||
if cfg.signal.enabled {
|
||||
if let Some(slot) = &state.signal_sender
|
||||
&& slot.is_linked().await
|
||||
{
|
||||
channels.push(CommsChannel::Signal);
|
||||
}
|
||||
channels
|
||||
@@ -66,7 +68,7 @@ pub struct DescribeServerOutput {
|
||||
pub telegram_bot_username: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn describe_server() -> Json<DescribeServerOutput> {
|
||||
pub async fn describe_server(State(state): State<AppState>) -> Json<DescribeServerOutput> {
|
||||
let cfg = tranquil_config::get();
|
||||
let pds_hostname = &cfg.server.hostname;
|
||||
|
||||
@@ -82,7 +84,7 @@ pub async fn describe_server() -> Json<DescribeServerOutput> {
|
||||
email: cfg.server.contact_email.clone(),
|
||||
},
|
||||
version: BUILD_VERSION,
|
||||
available_comms_channels: get_available_comms_channels(),
|
||||
available_comms_channels: get_available_comms_channels(&state).await,
|
||||
self_hosted_did_web_enabled: is_self_hosted_did_web_enabled(),
|
||||
discord_bot_username: discord_bot_username().map(String::from),
|
||||
discord_app_id: discord_app_id().map(String::from),
|
||||
|
||||
@@ -51,6 +51,7 @@ pub use session::{
|
||||
auto_resend_verification, confirm_signup, create_session, delete_session,
|
||||
get_legacy_login_preference, get_session, list_sessions, refresh_session, resend_verification,
|
||||
revoke_all_sessions, revoke_session, update_legacy_login_preference, update_locale,
|
||||
verification_blocks_login,
|
||||
};
|
||||
pub use signing_key::reserve_signing_key;
|
||||
pub use totp::{
|
||||
|
||||
@@ -8,7 +8,7 @@ use bcrypt::verify;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use tracing::{error, info, warn};
|
||||
use tranquil_db_traits::{SessionId, TokenFamilyId};
|
||||
use tranquil_db_traits::{ChannelVerificationStatus, SessionId, TokenFamilyId};
|
||||
use tranquil_pds::api::error::{ApiError, DbResultExt};
|
||||
use tranquil_pds::api::{EmptyResponse, PreferredLocaleOutput, SuccessResponse};
|
||||
use tranquil_pds::auth::{
|
||||
@@ -20,6 +20,13 @@ use tranquil_pds::state::AppState;
|
||||
use tranquil_pds::types::{AccountState, Did, Handle, PlainPassword};
|
||||
use tranquil_types::TokenId;
|
||||
|
||||
pub fn verification_blocks_login(channel_verification: &ChannelVerificationStatus) -> bool {
|
||||
!tranquil_config::get()
|
||||
.server
|
||||
.disable_account_verification_gate
|
||||
&& !channel_verification.has_any_verified()
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct CreateSessionInput {
|
||||
@@ -129,14 +136,13 @@ pub async fn create_session(
|
||||
warn!("Login attempt for takendown account: {}", row.did);
|
||||
return Err(ApiError::AccountTakedown);
|
||||
}
|
||||
let is_verified = row.channel_verification.has_any_verified();
|
||||
let is_delegated = state
|
||||
.repos
|
||||
.delegation
|
||||
.is_delegated_account(&row.did)
|
||||
.await
|
||||
.unwrap_or(false);
|
||||
if !is_verified && !is_delegated {
|
||||
if verification_blocks_login(&row.channel_verification) && !is_delegated {
|
||||
warn!("Login attempt for unverified account: {}", row.did);
|
||||
let resend_info = auto_resend_verification(&state, &row.did).await;
|
||||
let handle = resend_info
|
||||
|
||||
@@ -10,7 +10,14 @@ tranquil-signal = { workspace = true }
|
||||
|
||||
async-trait = { workspace = true }
|
||||
base64 = { workspace = true }
|
||||
ed25519-dalek = { workspace = true }
|
||||
futures = { workspace = true }
|
||||
hickory-resolver = { workspace = true }
|
||||
lettre = { workspace = true }
|
||||
rand = { workspace = true }
|
||||
reqwest = { workspace = true }
|
||||
rsa = { workspace = true }
|
||||
secrecy = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
sqlx = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
@@ -18,3 +25,7 @@ tokio = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
tranquil-db-traits = { workspace = true }
|
||||
uuid = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
chrono = { workspace = true }
|
||||
tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time", "io-util", "net"] }
|
||||
|
||||
@@ -0,0 +1,227 @@
|
||||
use std::fs;
|
||||
|
||||
use base64::Engine;
|
||||
use base64::engine::general_purpose::STANDARD as BASE64_STANDARD;
|
||||
use ed25519_dalek::pkcs8::DecodePrivateKey as _;
|
||||
use lettre::Message;
|
||||
use lettre::message::dkim::{
|
||||
DkimCanonicalization, DkimCanonicalizationType, DkimConfig as LettreDkimConfig,
|
||||
DkimSigningAlgorithm, DkimSigningKey,
|
||||
};
|
||||
use lettre::message::header::HeaderName;
|
||||
use rsa::pkcs1::EncodeRsaPrivateKey;
|
||||
use rsa::pkcs8::LineEnding;
|
||||
|
||||
use super::types::{DkimKeyPath, DkimSelector, EmailDomain};
|
||||
use crate::sender::SendError;
|
||||
|
||||
const SIGNED_HEADERS: &[&str] = &[
|
||||
"From",
|
||||
"Sender",
|
||||
"Reply-To",
|
||||
"To",
|
||||
"Cc",
|
||||
"Subject",
|
||||
"Date",
|
||||
"In-Reply-To",
|
||||
"References",
|
||||
"MIME-Version",
|
||||
"Content-Type",
|
||||
"Content-Transfer-Encoding",
|
||||
];
|
||||
|
||||
pub struct DkimSigner {
|
||||
config: LettreDkimConfig,
|
||||
}
|
||||
|
||||
impl DkimSigner {
|
||||
pub fn load(
|
||||
selector: DkimSelector,
|
||||
domain: EmailDomain,
|
||||
path: DkimKeyPath,
|
||||
) -> Result<Self, SendError> {
|
||||
let pem = fs::read_to_string(path.as_path()).map_err(|e| {
|
||||
SendError::DkimSign(format!("read DKIM key {}: {e}", path.as_path().display()))
|
||||
})?;
|
||||
Self::from_pem(selector, domain, &pem)
|
||||
}
|
||||
|
||||
pub fn from_pem(
|
||||
selector: DkimSelector,
|
||||
domain: EmailDomain,
|
||||
pem: &str,
|
||||
) -> Result<Self, SendError> {
|
||||
let key = parse_key(pem)?;
|
||||
let canonicalization = DkimCanonicalization {
|
||||
header: DkimCanonicalizationType::Relaxed,
|
||||
body: DkimCanonicalizationType::Relaxed,
|
||||
};
|
||||
let headers = SIGNED_HEADERS
|
||||
.iter()
|
||||
.copied()
|
||||
.map(HeaderName::new_from_ascii_str)
|
||||
.collect();
|
||||
let config = LettreDkimConfig::new(
|
||||
selector.into_inner(),
|
||||
domain.into_inner(),
|
||||
key,
|
||||
headers,
|
||||
canonicalization,
|
||||
);
|
||||
Ok(Self { config })
|
||||
}
|
||||
|
||||
pub fn sign(&self, message: &mut Message) {
|
||||
message.sign(&self.config);
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for DkimSigner {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str("DkimSigner")
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_key(input: &str) -> Result<DkimSigningKey, SendError> {
|
||||
let trimmed = input.trim_start();
|
||||
match trimmed {
|
||||
s if s.starts_with("-----BEGIN RSA PRIVATE KEY-----") => {
|
||||
DkimSigningKey::new(input, DkimSigningAlgorithm::Rsa)
|
||||
.map_err(|e| SendError::DkimSign(format!("RSA PKCS#1 PEM rejected: {e}")))
|
||||
}
|
||||
s if s.starts_with("-----BEGIN PRIVATE KEY-----") => parse_pkcs8(input),
|
||||
s if s.starts_with("-----BEGIN") => Err(SendError::DkimSign(
|
||||
"unrecognized PEM type; expected an RSA or Ed25519 private key".to_string(),
|
||||
)),
|
||||
_ => DkimSigningKey::new(input.trim(), DkimSigningAlgorithm::Ed25519).map_err(|e| {
|
||||
SendError::DkimSign(format!(
|
||||
"expected base64-encoded 32-byte Ed25519 seed or a PEM-wrapped key: {e}"
|
||||
))
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_pkcs8(pem: &str) -> Result<DkimSigningKey, SendError> {
|
||||
let ed25519_err = match ed25519_dalek::SigningKey::from_pkcs8_pem(pem) {
|
||||
Ok(key) => {
|
||||
let seed = BASE64_STANDARD.encode(key.to_bytes());
|
||||
return DkimSigningKey::new(&seed, DkimSigningAlgorithm::Ed25519)
|
||||
.map_err(|e| SendError::DkimSign(format!("re-import Ed25519 seed: {e}")));
|
||||
}
|
||||
Err(e) => e,
|
||||
};
|
||||
|
||||
let rsa_err = match rsa::RsaPrivateKey::from_pkcs8_pem(pem) {
|
||||
Ok(key) => {
|
||||
let pkcs1 = key
|
||||
.to_pkcs1_pem(LineEnding::LF)
|
||||
.map_err(|e| SendError::DkimSign(format!("re-encode RSA PKCS#8 as PKCS#1: {e}")))?;
|
||||
return DkimSigningKey::new(pkcs1.as_str(), DkimSigningAlgorithm::Rsa)
|
||||
.map_err(|e| SendError::DkimSign(format!("re-import RSA PKCS#1: {e}")));
|
||||
}
|
||||
Err(e) => e,
|
||||
};
|
||||
|
||||
Err(SendError::DkimSign(format!(
|
||||
"PKCS#8 PEM rejected by both parsers; ed25519: {ed25519_err}; rsa: {rsa_err}"
|
||||
)))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use ed25519_dalek::pkcs8::EncodePrivateKey as _;
|
||||
use lettre::message::Mailbox;
|
||||
use lettre::message::header::ContentType;
|
||||
use rsa::pkcs1::DecodeRsaPrivateKey as _;
|
||||
|
||||
const ED25519_RAW_SEED_B64: &str = "QkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkI=";
|
||||
|
||||
const RSA_PKCS1_PEM: &str = include_str!("test_fixtures/rsa2048-priv-pkcs1.pem");
|
||||
|
||||
fn ed25519_pkcs8_pem() -> String {
|
||||
let key = ed25519_dalek::SigningKey::from_bytes(&[7u8; 32]);
|
||||
key.to_pkcs8_pem(LineEnding::LF).unwrap().to_string()
|
||||
}
|
||||
|
||||
fn rsa_pkcs8_pem() -> String {
|
||||
let key = rsa::RsaPrivateKey::from_pkcs1_pem(RSA_PKCS1_PEM).unwrap();
|
||||
key.to_pkcs8_pem(LineEnding::LF).unwrap().to_string()
|
||||
}
|
||||
|
||||
fn signer(pem: &str) -> DkimSigner {
|
||||
DkimSigner::from_pem(
|
||||
DkimSelector::parse("default").unwrap(),
|
||||
EmailDomain::parse("nel.pet").unwrap(),
|
||||
pem,
|
||||
)
|
||||
.expect("key should load")
|
||||
}
|
||||
|
||||
fn signed_headers(signer: &DkimSigner) -> String {
|
||||
let from: Mailbox = "sender@nel.pet".parse().unwrap();
|
||||
let to: Mailbox = "recipient@nel.pet".parse().unwrap();
|
||||
let mut message = Message::builder()
|
||||
.from(from)
|
||||
.to(to)
|
||||
.subject("Roundtrip")
|
||||
.header(ContentType::TEXT_PLAIN)
|
||||
.body("Body".to_string())
|
||||
.unwrap();
|
||||
signer.sign(&mut message);
|
||||
String::from_utf8(message.formatted()).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_garbage() {
|
||||
assert!(matches!(
|
||||
parse_key("not a key"),
|
||||
Err(SendError::DkimSign(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_unknown_pem_type() {
|
||||
let pem = "-----BEGIN OPENSSH PRIVATE KEY-----\nx\n-----END OPENSSH PRIVATE KEY-----\n";
|
||||
match parse_key(pem) {
|
||||
Err(SendError::DkimSign(msg)) => assert!(msg.contains("unrecognized"), "msg: {msg}"),
|
||||
other => panic!("expected unrecognized PEM error, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ed25519_raw_seed_signs() {
|
||||
let raw = signed_headers(&signer(ED25519_RAW_SEED_B64));
|
||||
assert_signed_with(&raw, "a=ed25519-sha256");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ed25519_pkcs8_pem_signs() {
|
||||
let raw = signed_headers(&signer(&ed25519_pkcs8_pem()));
|
||||
assert_signed_with(&raw, "a=ed25519-sha256");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rsa_pkcs1_pem_signs() {
|
||||
let raw = signed_headers(&signer(RSA_PKCS1_PEM));
|
||||
assert_signed_with(&raw, "a=rsa-sha256");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rsa_pkcs8_pem_signs() {
|
||||
let raw = signed_headers(&signer(&rsa_pkcs8_pem()));
|
||||
assert_signed_with(&raw, "a=rsa-sha256");
|
||||
}
|
||||
|
||||
fn assert_signed_with(raw: &str, algorithm: &str) {
|
||||
assert!(
|
||||
raw.contains("DKIM-Signature:"),
|
||||
"no signature header: {raw}"
|
||||
);
|
||||
assert!(raw.contains(algorithm), "missing {algorithm}: {raw}");
|
||||
assert!(
|
||||
raw.contains("c=relaxed/relaxed"),
|
||||
"expected relaxed/relaxed canonicalization: {raw}"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
use lettre::Message;
|
||||
use lettre::message::Mailbox;
|
||||
use lettre::message::header::ContentType;
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::types::EmailDomain;
|
||||
use crate::sender::SendError;
|
||||
use crate::types::QueuedComms;
|
||||
|
||||
pub(super) fn build(from: &Mailbox, qc: &QueuedComms) -> Result<Message, SendError> {
|
||||
let to: Mailbox = qc
|
||||
.recipient
|
||||
.parse()
|
||||
.map_err(|e: lettre::address::AddressError| SendError::InvalidRecipient(e.to_string()))?;
|
||||
let subject = qc.subject.as_deref().unwrap_or("Notification");
|
||||
let message_id = format!("<{}@{}>", Uuid::new_v4(), from.email.domain());
|
||||
Message::builder()
|
||||
.from(from.clone())
|
||||
.to(to)
|
||||
.subject(subject)
|
||||
.message_id(Some(message_id))
|
||||
.header(ContentType::TEXT_PLAIN)
|
||||
.body(qc.body.clone())
|
||||
.map_err(|e| SendError::MessageBuild(e.to_string()))
|
||||
}
|
||||
|
||||
pub(super) fn recipient_domain(message: &Message) -> Result<EmailDomain, SendError> {
|
||||
let envelope = message.envelope();
|
||||
let first = envelope
|
||||
.to()
|
||||
.first()
|
||||
.ok_or_else(|| SendError::MessageBuild("envelope has no recipients".to_string()))?;
|
||||
EmailDomain::parse(first.domain())
|
||||
.map_err(|e| SendError::InvalidRecipient(format!("invalid recipient domain: {e}")))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::types::{CommsChannel, CommsStatus, CommsType};
|
||||
use chrono::Utc;
|
||||
use uuid::Uuid;
|
||||
|
||||
fn from_mailbox() -> Mailbox {
|
||||
"Test Sender <noreply@nel.pet>".parse().unwrap()
|
||||
}
|
||||
|
||||
fn fixture(recipient: &str, subject: Option<&str>, body: &str) -> QueuedComms {
|
||||
QueuedComms {
|
||||
id: Uuid::new_v4(),
|
||||
user_id: None,
|
||||
channel: CommsChannel::Email,
|
||||
comms_type: CommsType::Welcome,
|
||||
status: CommsStatus::Pending,
|
||||
recipient: recipient.to_string(),
|
||||
subject: subject.map(String::from),
|
||||
body: body.to_string(),
|
||||
metadata: None,
|
||||
attempts: 0,
|
||||
max_attempts: 3,
|
||||
last_error: None,
|
||||
created_at: Utc::now(),
|
||||
updated_at: Utc::now(),
|
||||
scheduled_for: Utc::now(),
|
||||
processed_at: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_basic_message() {
|
||||
let msg = build(
|
||||
&from_mailbox(),
|
||||
&fixture("user@nel.pet", Some("Welcome"), "Hello world."),
|
||||
)
|
||||
.unwrap();
|
||||
let raw = String::from_utf8(msg.formatted()).unwrap();
|
||||
let lower = raw.to_lowercase();
|
||||
assert!(raw.contains("From: \"Test Sender\" <noreply@nel.pet>"));
|
||||
assert!(raw.contains("To: user@nel.pet"));
|
||||
assert!(raw.contains("Subject: Welcome"));
|
||||
assert!(lower.contains("content-type: text/plain"));
|
||||
assert!(raw.contains("Hello world."));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn utf8_subject_is_encoded() {
|
||||
let msg = build(
|
||||
&from_mailbox(),
|
||||
&fixture("user@nel.pet", Some("héllo wörld"), "Body"),
|
||||
)
|
||||
.unwrap();
|
||||
let raw = String::from_utf8(msg.formatted()).unwrap();
|
||||
assert!(raw.contains("=?utf-8?"));
|
||||
assert!(!raw.contains("héllo"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn header_injection_rejected() {
|
||||
let result = build(
|
||||
&from_mailbox(),
|
||||
&fixture("x@nel.pet\r\nBcc: evil@x", Some("s"), "b"),
|
||||
);
|
||||
assert!(matches!(result, Err(SendError::InvalidRecipient(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subject_crlf_does_not_inject_headers() {
|
||||
let msg = build(
|
||||
&from_mailbox(),
|
||||
&fixture("user@nel.pet", Some("hi\r\nBcc: evil@nel.pet"), "body"),
|
||||
)
|
||||
.expect("subject CRLF should be encoded, not rejected");
|
||||
let raw = String::from_utf8(msg.formatted()).unwrap();
|
||||
assert!(
|
||||
!raw.contains("Bcc:"),
|
||||
"CRLF in subject must not produce a Bcc header: {raw}"
|
||||
);
|
||||
assert!(
|
||||
raw.contains("Subject: ="),
|
||||
"subject with non-printable chars should be RFC 2047 encoded: {raw}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn message_id_uses_from_domain() {
|
||||
let msg = build(&from_mailbox(), &fixture("user@nel.pet", Some("s"), "b")).unwrap();
|
||||
let raw = String::from_utf8(msg.formatted()).unwrap();
|
||||
let line = raw
|
||||
.lines()
|
||||
.find(|l| l.starts_with("Message-ID:") || l.starts_with("Message-Id:"))
|
||||
.expect("message-id header present");
|
||||
assert!(
|
||||
line.contains("@nel.pet>"),
|
||||
"message-id should use From domain: {line}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_subject_uses_default() {
|
||||
let msg = build(&from_mailbox(), &fixture("user@nel.pet", None, "Body")).unwrap();
|
||||
let raw = String::from_utf8(msg.formatted()).unwrap();
|
||||
assert!(raw.contains("Subject: Notification"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recipient_domain_extracted() {
|
||||
let msg = build(&from_mailbox(), &fixture("user@Nel.PET", Some("s"), "b")).unwrap();
|
||||
let d = recipient_domain(&msg).unwrap();
|
||||
assert_eq!(d.as_str(), "nel.pet");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
pub mod dkim;
|
||||
pub mod message;
|
||||
mod mx;
|
||||
pub mod transport;
|
||||
pub mod types;
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use hickory_resolver::TokioAsyncResolver;
|
||||
use lettre::message::Mailbox;
|
||||
use lettre::transport::smtp::AsyncSmtpTransport;
|
||||
use lettre::transport::smtp::PoolConfig;
|
||||
use lettre::transport::smtp::authentication::Credentials;
|
||||
use lettre::transport::smtp::extension::ClientId;
|
||||
use tokio::sync::Semaphore;
|
||||
use tracing::{info, warn};
|
||||
|
||||
pub use self::dkim::DkimSigner;
|
||||
pub use self::transport::SendMode;
|
||||
use self::types::{
|
||||
DkimKeyPath, DkimSelector, EmailDomain, HeloName, SmtpHost, SmtpPassword, SmtpPort,
|
||||
SmtpUsername, TlsMode,
|
||||
};
|
||||
use crate::sender::{CommsSender, SendError};
|
||||
use crate::types::{CommsChannel, QueuedComms};
|
||||
|
||||
pub struct EmailSender {
|
||||
from: Mailbox,
|
||||
mode: SendMode,
|
||||
dkim: Option<DkimSigner>,
|
||||
}
|
||||
|
||||
impl EmailSender {
|
||||
pub fn new(from: Mailbox, mode: SendMode, dkim: Option<DkimSigner>) -> Self {
|
||||
Self { from, mode, dkim }
|
||||
}
|
||||
|
||||
pub fn from_config(cfg: &tranquil_config::TranquilConfig) -> Result<Option<Self>, SendError> {
|
||||
let Some(from_address) = cfg.email.from_address.as_deref().filter(|s| !s.is_empty()) else {
|
||||
info!("Email sender disabled: MAIL_FROM_ADDRESS unset");
|
||||
return Ok(None);
|
||||
};
|
||||
let from = build_from(&cfg.email.from_name, from_address)?;
|
||||
let dkim = build_dkim(&cfg.email.dkim)?;
|
||||
let mode = match cfg
|
||||
.email
|
||||
.smarthost
|
||||
.host
|
||||
.as_deref()
|
||||
.filter(|h| !h.is_empty())
|
||||
{
|
||||
Some(host) => build_smarthost(cfg, host)?,
|
||||
None => build_direct_mx(cfg)?,
|
||||
};
|
||||
info!(?mode, dkim = dkim.is_some(), "Email sender initialized");
|
||||
Ok(Some(Self { from, mode, dkim }))
|
||||
}
|
||||
}
|
||||
|
||||
fn config_invalid(field: &str, error: impl std::fmt::Display) -> SendError {
|
||||
SendError::ConfigInvalid(format!("{field}: {error}"))
|
||||
}
|
||||
|
||||
fn build_from(from_name: &str, from_address: &str) -> Result<Mailbox, SendError> {
|
||||
let raw = match from_name.is_empty() {
|
||||
true => from_address.to_string(),
|
||||
false => format!("\"{}\" <{}>", from_name.replace('"', "'"), from_address),
|
||||
};
|
||||
raw.parse::<Mailbox>()
|
||||
.map_err(|e| config_invalid("MAIL_FROM_ADDRESS / MAIL_FROM_NAME", e))
|
||||
}
|
||||
|
||||
fn build_smarthost(
|
||||
cfg: &tranquil_config::TranquilConfig,
|
||||
host_raw: &str,
|
||||
) -> Result<SendMode, SendError> {
|
||||
let host = SmtpHost::parse(host_raw).map_err(|e| config_invalid("MAIL_SMARTHOST_HOST", e))?;
|
||||
let port = SmtpPort::parse(cfg.email.smarthost.port)
|
||||
.map_err(|e| config_invalid("MAIL_SMARTHOST_PORT", e))?;
|
||||
let tls = TlsMode::parse(&cfg.email.smarthost.tls)
|
||||
.map_err(|e| config_invalid("MAIL_SMARTHOST_TLS", e))?;
|
||||
let helo = resolve_helo(cfg)?;
|
||||
let pool = PoolConfig::new()
|
||||
.max_size(cfg.email.smarthost.pool_size)
|
||||
.idle_timeout(Duration::from_secs(60));
|
||||
let command_timeout = Duration::from_secs(cfg.email.smarthost.command_timeout_secs);
|
||||
let total_timeout = Duration::from_secs(cfg.email.smarthost.total_timeout_secs);
|
||||
|
||||
let builder = match tls {
|
||||
TlsMode::Implicit => AsyncSmtpTransport::<lettre::Tokio1Executor>::relay(host.as_str())
|
||||
.map_err(|e| config_invalid("smarthost TLS setup", e))?,
|
||||
TlsMode::Starttls => {
|
||||
AsyncSmtpTransport::<lettre::Tokio1Executor>::starttls_relay(host.as_str())
|
||||
.map_err(|e| config_invalid("smarthost TLS setup", e))?
|
||||
}
|
||||
TlsMode::None => {
|
||||
AsyncSmtpTransport::<lettre::Tokio1Executor>::builder_dangerous(host.as_str())
|
||||
}
|
||||
};
|
||||
let builder = builder
|
||||
.port(port.as_u16())
|
||||
.hello_name(ClientId::Domain(helo.into_inner()))
|
||||
.timeout(Some(command_timeout))
|
||||
.pool_config(pool);
|
||||
let builder = match (
|
||||
cfg.email.smarthost.username.as_deref(),
|
||||
cfg.email.smarthost.password.as_deref(),
|
||||
) {
|
||||
(Some(u), Some(p)) => {
|
||||
let username =
|
||||
SmtpUsername::parse(u).map_err(|e| config_invalid("MAIL_SMARTHOST_USERNAME", e))?;
|
||||
let password =
|
||||
SmtpPassword::parse(p).map_err(|e| config_invalid("MAIL_SMARTHOST_PASSWORD", e))?;
|
||||
builder.credentials(Credentials::new(
|
||||
username.into_inner(),
|
||||
password.expose().to_string(),
|
||||
))
|
||||
}
|
||||
_ => builder,
|
||||
};
|
||||
Ok(SendMode::Smarthost {
|
||||
transport: Box::new(builder.build()),
|
||||
total_timeout,
|
||||
})
|
||||
}
|
||||
|
||||
fn build_direct_mx(cfg: &tranquil_config::TranquilConfig) -> Result<SendMode, SendError> {
|
||||
let helo = resolve_helo(cfg)?;
|
||||
let resolver = TokioAsyncResolver::tokio_from_system_conf()
|
||||
.map(Arc::new)
|
||||
.map_err(|e| config_invalid("system DNS configuration", e))?;
|
||||
let max_concurrent = cfg.email.direct_mx.max_concurrent_sends.max(1);
|
||||
Ok(SendMode::DirectMx {
|
||||
resolver,
|
||||
helo,
|
||||
command_timeout: Duration::from_secs(cfg.email.direct_mx.command_timeout_secs),
|
||||
total_timeout: Duration::from_secs(cfg.email.direct_mx.total_timeout_secs),
|
||||
require_tls: cfg.email.direct_mx.require_tls,
|
||||
inflight: Arc::new(Semaphore::new(max_concurrent)),
|
||||
})
|
||||
}
|
||||
|
||||
fn resolve_helo(cfg: &tranquil_config::TranquilConfig) -> Result<HeloName, SendError> {
|
||||
let raw = cfg
|
||||
.email
|
||||
.helo_name
|
||||
.clone()
|
||||
.unwrap_or_else(|| cfg.server.hostname_without_port().to_string());
|
||||
HeloName::parse(&raw).map_err(|e| config_invalid(&format!("HELO name {raw:?}"), e))
|
||||
}
|
||||
|
||||
fn build_dkim(cfg: &tranquil_config::DkimConfig) -> Result<Option<DkimSigner>, SendError> {
|
||||
let selector = match cfg.selector.as_deref() {
|
||||
Some(s) => s,
|
||||
None => return Ok(None),
|
||||
};
|
||||
let domain = cfg
|
||||
.domain
|
||||
.as_deref()
|
||||
.ok_or_else(|| SendError::DkimSign("MAIL_DKIM_DOMAIN required when selector set".into()))?;
|
||||
let key_path = cfg.private_key_path.as_deref().ok_or_else(|| {
|
||||
SendError::DkimSign("MAIL_DKIM_KEY_PATH required when selector set".into())
|
||||
})?;
|
||||
let selector = DkimSelector::parse(selector)
|
||||
.map_err(|e| SendError::DkimSign(format!("invalid DKIM selector: {e}")))?;
|
||||
let domain = EmailDomain::parse(domain)
|
||||
.map_err(|e| SendError::DkimSign(format!("invalid DKIM domain: {e}")))?;
|
||||
let path = DkimKeyPath::parse(key_path)
|
||||
.map_err(|e| SendError::DkimSign(format!("DKIM key path invalid: {e}")))?;
|
||||
DkimSigner::load(selector, domain, path).map(Some)
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl CommsSender for EmailSender {
|
||||
fn channel(&self) -> CommsChannel {
|
||||
CommsChannel::Email
|
||||
}
|
||||
|
||||
async fn send(&self, notification: &QueuedComms) -> Result<(), SendError> {
|
||||
let mut message = message::build(&self.from, notification)?;
|
||||
if let Some(signer) = &self.dkim {
|
||||
signer.sign(&mut message);
|
||||
}
|
||||
match transport::dispatch(&self.mode, message).await {
|
||||
Ok(()) => Ok(()),
|
||||
Err(e) => {
|
||||
warn!(comms_id = %notification.id, error = %e, "SMTP send failed");
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
use hickory_resolver::TokioAsyncResolver;
|
||||
use hickory_resolver::error::{ResolveError, ResolveErrorKind};
|
||||
use hickory_resolver::proto::op::ResponseCode;
|
||||
use rand::seq::SliceRandom;
|
||||
|
||||
use super::types::{EmailDomain, MxHost, MxPriority, MxRecord};
|
||||
use crate::sender::SendError;
|
||||
|
||||
pub async fn resolve(
|
||||
resolver: &TokioAsyncResolver,
|
||||
domain: &EmailDomain,
|
||||
) -> Result<Vec<MxRecord>, SendError> {
|
||||
match resolver.mx_lookup(domain.as_str()).await {
|
||||
Ok(lookup) => interpret_lookup(
|
||||
lookup
|
||||
.iter()
|
||||
.map(|mx| (mx.preference(), mx.exchange().clone())),
|
||||
domain,
|
||||
),
|
||||
Err(e) => classify_lookup_error(e, domain),
|
||||
}
|
||||
}
|
||||
|
||||
fn interpret_lookup(
|
||||
items: impl IntoIterator<Item = (u16, hickory_resolver::Name)>,
|
||||
domain: &EmailDomain,
|
||||
) -> Result<Vec<MxRecord>, SendError> {
|
||||
let entries: Vec<_> = items.into_iter().collect();
|
||||
match entries.iter().any(|(_, name)| name.is_root()) {
|
||||
true => Err(SendError::DnsPermanent(format!(
|
||||
"null MX record at {}: domain refuses mail",
|
||||
domain.as_str()
|
||||
))),
|
||||
false => {
|
||||
let records: Vec<MxRecord> = entries
|
||||
.into_iter()
|
||||
.filter_map(|(prio, name)| {
|
||||
MxHost::parse(&name.to_utf8()).ok().map(|host| MxRecord {
|
||||
priority: MxPriority::new(prio),
|
||||
host,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
match records.is_empty() {
|
||||
true => implicit_mx(domain),
|
||||
false => Ok(prioritize(records)),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn prioritize(mut records: Vec<MxRecord>) -> Vec<MxRecord> {
|
||||
records.shuffle(&mut rand::thread_rng());
|
||||
records.sort_by_key(|r| r.priority);
|
||||
records
|
||||
}
|
||||
|
||||
fn classify_lookup_error(
|
||||
e: ResolveError,
|
||||
domain: &EmailDomain,
|
||||
) -> Result<Vec<MxRecord>, SendError> {
|
||||
match e.kind() {
|
||||
ResolveErrorKind::NoRecordsFound { response_code, .. } => match *response_code {
|
||||
ResponseCode::NoError => implicit_mx(domain),
|
||||
ResponseCode::NXDomain => Err(SendError::DnsPermanent(format!(
|
||||
"domain {} does not exist",
|
||||
domain.as_str()
|
||||
))),
|
||||
other => Err(SendError::DnsTransient(format!(
|
||||
"MX lookup for {} failed with {other}",
|
||||
domain.as_str()
|
||||
))),
|
||||
},
|
||||
_ => Err(SendError::DnsTransient(e.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
fn implicit_mx(domain: &EmailDomain) -> Result<Vec<MxRecord>, SendError> {
|
||||
MxHost::parse(domain.as_str())
|
||||
.map(|host| {
|
||||
vec![MxRecord {
|
||||
priority: MxPriority::new(0),
|
||||
host,
|
||||
}]
|
||||
})
|
||||
.map_err(|e| SendError::DnsPermanent(format!("invalid recipient domain: {e}")))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn record(prio: u16, host: &str) -> MxRecord {
|
||||
MxRecord {
|
||||
priority: MxPriority::new(prio),
|
||||
host: MxHost::parse(host).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prioritize_sorts_by_priority_ascending() {
|
||||
let result = prioritize(vec![
|
||||
record(20, "mx2.nel.pet"),
|
||||
record(10, "mx1.nel.pet"),
|
||||
record(10, "mx1b.nel.pet"),
|
||||
]);
|
||||
assert_eq!(result[0].priority.as_u16(), 10);
|
||||
assert_eq!(result[1].priority.as_u16(), 10);
|
||||
assert_eq!(result[2].priority.as_u16(), 20);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prioritize_randomizes_equal_priority_order() {
|
||||
let attempts: Vec<Vec<String>> = (0..200)
|
||||
.map(|_| {
|
||||
prioritize(vec![
|
||||
record(10, "a.nel.pet"),
|
||||
record(10, "b.nel.pet"),
|
||||
record(10, "c.nel.pet"),
|
||||
record(10, "d.nel.pet"),
|
||||
])
|
||||
.into_iter()
|
||||
.map(|r| r.host.as_str().to_string())
|
||||
.collect()
|
||||
})
|
||||
.collect();
|
||||
let distinct: std::collections::HashSet<_> = attempts.iter().cloned().collect();
|
||||
assert!(
|
||||
distinct.len() > 1,
|
||||
"equal-priority MX order should vary across calls; got only {}",
|
||||
distinct.len()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn implicit_mx_uses_domain_as_host() {
|
||||
let d = EmailDomain::parse("nel.pet").unwrap();
|
||||
let result = implicit_mx(&d).unwrap();
|
||||
assert_eq!(result.len(), 1);
|
||||
assert_eq!(result[0].priority.as_u16(), 0);
|
||||
assert_eq!(result[0].host.as_str(), "nel.pet");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_error_response_yields_implicit_mx() {
|
||||
let d = EmailDomain::parse("nel.pet").unwrap();
|
||||
let err = ResolveError::from(ResolveErrorKind::NoRecordsFound {
|
||||
query: Box::new(hickory_resolver::proto::op::Query::default()),
|
||||
soa: None,
|
||||
negative_ttl: None,
|
||||
response_code: ResponseCode::NoError,
|
||||
trusted: false,
|
||||
});
|
||||
let result = classify_lookup_error(err, &d).unwrap();
|
||||
assert_eq!(result.len(), 1);
|
||||
assert_eq!(result[0].host.as_str(), "nel.pet");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nxdomain_response_is_permanent() {
|
||||
let d = EmailDomain::parse("does-not-exist.invalid").unwrap();
|
||||
let err = ResolveError::from(ResolveErrorKind::NoRecordsFound {
|
||||
query: Box::new(hickory_resolver::proto::op::Query::default()),
|
||||
soa: None,
|
||||
negative_ttl: None,
|
||||
response_code: ResponseCode::NXDomain,
|
||||
trusted: true,
|
||||
});
|
||||
match classify_lookup_error(err, &d) {
|
||||
Err(SendError::DnsPermanent(_)) => {}
|
||||
other => panic!("expected DnsPermanent, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn servfail_response_is_transient() {
|
||||
let d = EmailDomain::parse("nel.pet").unwrap();
|
||||
let err = ResolveError::from(ResolveErrorKind::NoRecordsFound {
|
||||
query: Box::new(hickory_resolver::proto::op::Query::default()),
|
||||
soa: None,
|
||||
negative_ttl: None,
|
||||
response_code: ResponseCode::ServFail,
|
||||
trusted: false,
|
||||
});
|
||||
match classify_lookup_error(err, &d) {
|
||||
Err(SendError::DnsTransient(_)) => {}
|
||||
other => panic!("expected DnsTransient, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timeout_is_transient() {
|
||||
let d = EmailDomain::parse("nel.pet").unwrap();
|
||||
let err = ResolveError::from(ResolveErrorKind::Timeout);
|
||||
match classify_lookup_error(err, &d) {
|
||||
Err(SendError::DnsTransient(_)) => {}
|
||||
other => panic!("expected DnsTransient, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn message_variant_is_transient() {
|
||||
let d = EmailDomain::parse("nel.pet").unwrap();
|
||||
let err = ResolveError::from(ResolveErrorKind::Message("transient resolver glitch"));
|
||||
match classify_lookup_error(err, &d) {
|
||||
Err(SendError::DnsTransient(_)) => {}
|
||||
other => panic!("expected DnsTransient default, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn null_mx_is_permanent() {
|
||||
let d = EmailDomain::parse("nomail.nel.pet").unwrap();
|
||||
let result = interpret_lookup(vec![(0, hickory_resolver::Name::root())], &d);
|
||||
match result {
|
||||
Err(SendError::DnsPermanent(msg)) => {
|
||||
assert!(msg.contains("null MX"), "msg: {msg}")
|
||||
}
|
||||
other => panic!("expected DnsPermanent, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn null_mx_alongside_real_records_still_permanent() {
|
||||
let d = EmailDomain::parse("mixed.nel.pet").unwrap();
|
||||
let real = hickory_resolver::Name::from_ascii("mx1.nel.pet.").unwrap();
|
||||
let result = interpret_lookup(vec![(10, real), (0, hickory_resolver::Name::root())], &d);
|
||||
assert!(matches!(result, Err(SendError::DnsPermanent(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_lookup_uses_implicit_mx() {
|
||||
let d = EmailDomain::parse("nel.pet").unwrap();
|
||||
let result = interpret_lookup(Vec::<(u16, hickory_resolver::Name)>::new(), &d).unwrap();
|
||||
assert_eq!(result.len(), 1);
|
||||
assert_eq!(result[0].host.as_str(), "nel.pet");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_records_pass_through_with_priority_sort() {
|
||||
let d = EmailDomain::parse("nel.pet").unwrap();
|
||||
let mx1 = hickory_resolver::Name::from_ascii("mx1.nel.pet.").unwrap();
|
||||
let mx2 = hickory_resolver::Name::from_ascii("mx2.nel.pet.").unwrap();
|
||||
let result = interpret_lookup(vec![(20, mx2), (10, mx1)], &d).unwrap();
|
||||
assert_eq!(result.len(), 2);
|
||||
assert_eq!(result[0].priority.as_u16(), 10);
|
||||
assert_eq!(result[0].host.as_str(), "mx1.nel.pet");
|
||||
assert_eq!(result[1].priority.as_u16(), 20);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIEowIBAAKCAQEAtsQsUV8QpqrygsY+2+JCQ6Fw8/omM71IM2N/R8pPbzbgOl0p
|
||||
78MZGsgPOQ2HSznjD0FPzsH8oO2B5Uftws04LHb2HJAYlz25+lN5cqfHAfa3fgmC
|
||||
38FfwBkn7l582UtPWZ/wcBOnyCgb3yLcvJrXyrt8QxHJgvWO23ITrUVYszImbXQ6
|
||||
7YGS0YhMrbixRzmo2tpm3JcIBtnHrEUMsT0NfFdfsZhTT8YbxBvA8FdODgEwx7u/
|
||||
vf3J9qbi4+Kv8cvqyJuleIRSjVXPsIMnoejIn04APPKIjpMyQdnWlby7rNyQtE4+
|
||||
CV+jcFjqJbE/Xilcvqxt6DirjFCvYeKYl1uHLwIDAQABAoIBAH7Mg2LA7bB0EWQh
|
||||
XiL3SrnZG6BpAHAM9jaQ5RFNjua9z7suP5YUaSpnegg/FopeUuWWjmQHudl8bg5A
|
||||
ZPgtoLdYoU8XubfUH19I4o1lUXBPVuaeeqn6Yw/HZCjAbSXkVdz8VbesK092ZD/e
|
||||
0/4V/3irsn5lrMSq0L322yfvYKaRDFxKCF7UMnWrGcHZl6Msbv/OffLRk19uYB7t
|
||||
4WGhK1zCfKIfgdLJnD0eoI6Q4wU6sJvvpyTe8NDDo8HpdAwNn3YSahSewKp9gHgg
|
||||
VIQlTZUdsHxM+R+2RUwJZYj9WSTbq+s1nKICUmjQBPnWbrPW963BE5utQPFt3mOe
|
||||
EWRzdsECgYEA3MBhJC1Okq+u5yrFE8plufdwNvm9fg5uYUYafvdlQiXsFTx+XDGm
|
||||
FXpuWhP/bheOh1jByzPZ1rvjF57xiZjkIuzcvtePTs/b5fT82K7CydDchkc8qb0W
|
||||
2dI40h+13e++sUPKYdC9aqjZHzOgl3kOlkDbyRCF3F8mNDujE49rLWcCgYEA0/MU
|
||||
dX5A6VSDb5K+JCNq8vDaBKNGU8GAr2fpYAhtk/3mXLI+/Z0JN0di9ZgeNhhJr2jN
|
||||
11OU/2pOButpsgnkIo2y36cOQPf5dQpSgXZke3iNDld3osuLIuPNJn/3C087AtOq
|
||||
+w4YxZClZLAxiLCqX8SBVrB2IiFCQ70SJ++n8vkCgYEAzmi3rBsNEA1jblVIh1PF
|
||||
wJhD/bOQ4nBd92iUV8m9jZdl4wl4YX4u/IBI9MMkIG24YIe2VOl7s9Rk5+4/jNg/
|
||||
4QQ2998Y6aljxOZJEdZ+3jQELy4m49OhrTRq2ta5t/Z3CMsJTmLe6f9NXWZpr5iK
|
||||
8iVdHOjtMXxqfYaR2jVNEtsCgYAl9uWUQiAoa037v0I1wO5YQ9IZgJGJUSDWynsg
|
||||
C4JtPs5zji4ASY+sCipsqWnH8MPKGrC8QClxMr51ONe+30yw78a5jvfbpU9Wqpmq
|
||||
vOU0xJwnlH1GeMUcY8eMfOFocjG0yOtYeubvBIDLr0/AFzz9WHp+Z69RX7m53nUR
|
||||
GDlyKQKBgDGZVAbUBiB8rerqNbONBAxfipoa4IJ+ntBrFT2DtoIZNbSzaoK+nVbH
|
||||
kbWMJycaV5PVOh1lfAiZeWCxQz5RcZh/RS8USnxyMG1j4dP/wLcbdasI8uRaSC6Y
|
||||
hFHL5HjhLrIo0HRWySS2b2ztBI2FP1M+MaaGFPHDzm2OyZg85yr3
|
||||
-----END RSA PRIVATE KEY-----
|
||||
@@ -0,0 +1,164 @@
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use futures::StreamExt;
|
||||
use hickory_resolver::TokioAsyncResolver;
|
||||
use lettre::transport::smtp::AsyncSmtpTransport;
|
||||
use lettre::transport::smtp::Error as SmtpError;
|
||||
use lettre::transport::smtp::client::{Tls, TlsParameters};
|
||||
use lettre::transport::smtp::extension::ClientId;
|
||||
use lettre::{AsyncTransport, Message, Tokio1Executor};
|
||||
use tokio::sync::Semaphore;
|
||||
|
||||
use super::message::recipient_domain;
|
||||
use super::mx;
|
||||
use super::types::{HeloName, MxRecord};
|
||||
use crate::sender::SendError;
|
||||
|
||||
pub enum SendMode {
|
||||
Smarthost {
|
||||
transport: Box<AsyncSmtpTransport<Tokio1Executor>>,
|
||||
total_timeout: Duration,
|
||||
},
|
||||
DirectMx {
|
||||
resolver: Arc<TokioAsyncResolver>,
|
||||
helo: HeloName,
|
||||
command_timeout: Duration,
|
||||
total_timeout: Duration,
|
||||
require_tls: bool,
|
||||
inflight: Arc<Semaphore>,
|
||||
},
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for SendMode {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Smarthost { total_timeout, .. } => {
|
||||
write!(f, "SendMode::Smarthost(total_timeout={total_timeout:?})")
|
||||
}
|
||||
Self::DirectMx {
|
||||
helo, require_tls, ..
|
||||
} => write!(
|
||||
f,
|
||||
"SendMode::DirectMx({}, require_tls={require_tls})",
|
||||
helo.as_str()
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn dispatch(mode: &SendMode, message: Message) -> Result<(), SendError> {
|
||||
match mode {
|
||||
SendMode::Smarthost {
|
||||
transport,
|
||||
total_timeout,
|
||||
} => with_total_timeout(*total_timeout, run_send(transport, message)).await,
|
||||
SendMode::DirectMx {
|
||||
resolver,
|
||||
helo,
|
||||
command_timeout,
|
||||
total_timeout,
|
||||
require_tls,
|
||||
inflight,
|
||||
} => {
|
||||
with_total_timeout(*total_timeout, async {
|
||||
let _permit =
|
||||
inflight.clone().acquire_owned().await.map_err(|_| {
|
||||
SendError::SmtpTransient("send semaphore closed".to_string())
|
||||
})?;
|
||||
send_direct(
|
||||
resolver.as_ref(),
|
||||
helo,
|
||||
*command_timeout,
|
||||
*require_tls,
|
||||
message,
|
||||
)
|
||||
.await
|
||||
})
|
||||
.await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn with_total_timeout<F: std::future::Future<Output = Result<(), SendError>>>(
|
||||
total: Duration,
|
||||
fut: F,
|
||||
) -> Result<(), SendError> {
|
||||
tokio::time::timeout(total, fut)
|
||||
.await
|
||||
.unwrap_or(Err(SendError::Timeout))
|
||||
}
|
||||
|
||||
async fn run_send(
|
||||
transport: &AsyncSmtpTransport<Tokio1Executor>,
|
||||
message: Message,
|
||||
) -> Result<(), SendError> {
|
||||
transport
|
||||
.send(message)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(classify_smtp_error)
|
||||
}
|
||||
|
||||
async fn send_direct(
|
||||
resolver: &TokioAsyncResolver,
|
||||
helo: &HeloName,
|
||||
command_timeout: Duration,
|
||||
require_tls: bool,
|
||||
message: Message,
|
||||
) -> Result<(), SendError> {
|
||||
let domain = recipient_domain(&message)?;
|
||||
let mxs = mx::resolve(resolver, &domain).await?;
|
||||
let outcome = futures::stream::iter(mxs)
|
||||
.fold(None::<Result<(), SendError>>, |acc, mx_record| {
|
||||
let message = message.clone();
|
||||
async move {
|
||||
match &acc {
|
||||
Some(Ok(())) | Some(Err(SendError::SmtpPermanent(_))) => acc,
|
||||
_ => Some(
|
||||
attempt_one_host(mx_record, helo, command_timeout, require_tls, message)
|
||||
.await,
|
||||
),
|
||||
}
|
||||
}
|
||||
})
|
||||
.await;
|
||||
outcome.unwrap_or_else(|| {
|
||||
Err(SendError::SmtpTransient(format!(
|
||||
"no MX records returned for {}",
|
||||
domain.as_str()
|
||||
)))
|
||||
})
|
||||
}
|
||||
|
||||
async fn attempt_one_host(
|
||||
mx_record: MxRecord,
|
||||
helo: &HeloName,
|
||||
command_timeout: Duration,
|
||||
require_tls: bool,
|
||||
message: Message,
|
||||
) -> Result<(), SendError> {
|
||||
let host = mx_record.host.as_str().to_string();
|
||||
let tls_params = TlsParameters::new(host.clone())
|
||||
.map_err(|e| SendError::SmtpTransient(format!("TLS params for {host}: {e}")))?;
|
||||
let tls = match require_tls {
|
||||
true => Tls::Required(tls_params),
|
||||
false => Tls::Opportunistic(tls_params),
|
||||
};
|
||||
let transport: AsyncSmtpTransport<Tokio1Executor> =
|
||||
AsyncSmtpTransport::<Tokio1Executor>::builder_dangerous(&host)
|
||||
.port(25)
|
||||
.tls(tls)
|
||||
.hello_name(ClientId::Domain(helo.as_str().to_string()))
|
||||
.timeout(Some(command_timeout))
|
||||
.build();
|
||||
run_send(&transport, message).await
|
||||
}
|
||||
|
||||
fn classify_smtp_error(e: SmtpError) -> SendError {
|
||||
match () {
|
||||
_ if e.is_permanent() => SendError::SmtpPermanent(e.to_string()),
|
||||
_ if e.is_timeout() => SendError::Timeout,
|
||||
_ => SendError::SmtpTransient(e.to_string()),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ParseError {
|
||||
#[error("empty value")]
|
||||
Empty,
|
||||
#[error("invalid character {0:?}")]
|
||||
InvalidChar(char),
|
||||
#[error("zero {0}")]
|
||||
Zero(&'static str),
|
||||
#[error("invalid TLS mode {0:?}")]
|
||||
InvalidTlsMode(String),
|
||||
}
|
||||
|
||||
fn parse_token(raw: &str, lowercase: bool, strip_trailing_dot: bool) -> Result<String, ParseError> {
|
||||
let mut s = raw.trim();
|
||||
if strip_trailing_dot {
|
||||
s = s.trim_end_matches('.');
|
||||
}
|
||||
match s {
|
||||
"" => Err(ParseError::Empty),
|
||||
_ if s.chars().any(char::is_whitespace) => Err(ParseError::InvalidChar(' ')),
|
||||
_ => Ok(match lowercase {
|
||||
true => s.to_lowercase(),
|
||||
false => s.to_string(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct SmtpHost(String);
|
||||
|
||||
impl SmtpHost {
|
||||
pub fn parse(raw: &str) -> Result<Self, ParseError> {
|
||||
parse_token(raw, true, false).map(Self)
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct SmtpPort(u16);
|
||||
|
||||
impl SmtpPort {
|
||||
pub fn parse(raw: u16) -> Result<Self, ParseError> {
|
||||
match raw {
|
||||
0 => Err(ParseError::Zero("smtp port")),
|
||||
n => Ok(Self(n)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_u16(self) -> u16 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct HeloName(String);
|
||||
|
||||
impl HeloName {
|
||||
pub fn parse(raw: &str) -> Result<Self, ParseError> {
|
||||
parse_token(raw, false, false).map(Self)
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> String {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct EmailDomain(String);
|
||||
|
||||
impl EmailDomain {
|
||||
pub fn parse(raw: &str) -> Result<Self, ParseError> {
|
||||
parse_token(raw, true, true).map(Self)
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> String {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct MxHost(String);
|
||||
|
||||
impl MxHost {
|
||||
pub fn parse(raw: &str) -> Result<Self, ParseError> {
|
||||
parse_token(raw, true, true).map(Self)
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct MxPriority(u16);
|
||||
|
||||
impl MxPriority {
|
||||
pub fn new(value: u16) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
|
||||
pub fn as_u16(self) -> u16 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MxRecord {
|
||||
pub priority: MxPriority,
|
||||
pub host: MxHost,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct DkimSelector(String);
|
||||
|
||||
impl DkimSelector {
|
||||
pub fn parse(raw: &str) -> Result<Self, ParseError> {
|
||||
let trimmed = raw.trim();
|
||||
let valid = !trimmed.is_empty() && trimmed.split('.').all(valid_subdomain);
|
||||
match valid {
|
||||
true => Ok(Self(trimmed.to_string())),
|
||||
false => Err(ParseError::InvalidChar('?')),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> String {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
fn valid_subdomain(seg: &str) -> bool {
|
||||
let starts_alnum = seg
|
||||
.chars()
|
||||
.next()
|
||||
.is_some_and(|c| c.is_ascii_alphanumeric());
|
||||
let ends_alnum = seg
|
||||
.chars()
|
||||
.next_back()
|
||||
.is_some_and(|c| c.is_ascii_alphanumeric());
|
||||
let body_ok = seg.chars().all(|c| c.is_ascii_alphanumeric() || c == '-');
|
||||
starts_alnum && ends_alnum && body_ok
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DkimKeyPath(PathBuf);
|
||||
|
||||
impl DkimKeyPath {
|
||||
pub fn parse(raw: &str) -> Result<Self, ParseError> {
|
||||
let trimmed = raw.trim();
|
||||
match trimmed.is_empty() {
|
||||
true => Err(ParseError::Empty),
|
||||
false => Ok(Self(PathBuf::from(trimmed))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_path(&self) -> &std::path::Path {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct SmtpUsername(String);
|
||||
|
||||
impl SmtpUsername {
|
||||
pub fn parse(raw: &str) -> Result<Self, ParseError> {
|
||||
match raw.is_empty() {
|
||||
true => Err(ParseError::Empty),
|
||||
false => Ok(Self(raw.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_inner(self) -> String {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SmtpPassword(secrecy::SecretString);
|
||||
|
||||
impl SmtpPassword {
|
||||
pub fn parse(raw: &str) -> Result<Self, ParseError> {
|
||||
match raw.is_empty() {
|
||||
true => Err(ParseError::Empty),
|
||||
false => Ok(Self(secrecy::SecretString::from(raw.to_string()))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn expose(&self) -> &str {
|
||||
use secrecy::ExposeSecret;
|
||||
self.0.expose_secret()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for SmtpPassword {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str("SmtpPassword(***)")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum TlsMode {
|
||||
Implicit,
|
||||
Starttls,
|
||||
None,
|
||||
}
|
||||
|
||||
impl TlsMode {
|
||||
pub fn parse(raw: &str) -> Result<Self, ParseError> {
|
||||
match raw.to_ascii_lowercase().as_str() {
|
||||
"implicit" => Ok(Self::Implicit),
|
||||
"starttls" => Ok(Self::Starttls),
|
||||
"none" => Ok(Self::None),
|
||||
other => Err(ParseError::InvalidTlsMode(other.to_string())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn smtp_host_lowercases_and_trims() {
|
||||
let h = SmtpHost::parse(" SMTP.NEL.PET ").unwrap();
|
||||
assert_eq!(h.as_str(), "smtp.nel.pet");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn smtp_host_rejects_whitespace() {
|
||||
assert!(SmtpHost::parse("a b").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn smtp_host_rejects_empty() {
|
||||
assert!(SmtpHost::parse("").is_err());
|
||||
assert!(SmtpHost::parse(" ").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn smtp_port_rejects_zero() {
|
||||
assert!(SmtpPort::parse(0).is_err());
|
||||
assert_eq!(SmtpPort::parse(587).unwrap().as_u16(), 587);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn email_domain_strips_trailing_dot() {
|
||||
assert_eq!(EmailDomain::parse("Nel.pet.").unwrap().as_str(), "nel.pet");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dkim_selector_validates() {
|
||||
assert!(DkimSelector::parse("default").is_ok());
|
||||
assert!(DkimSelector::parse("s1.nel.pet").is_ok());
|
||||
assert!(DkimSelector::parse("s2024-q1").is_ok());
|
||||
assert!(DkimSelector::parse("mailo-2024.nel.pet").is_ok());
|
||||
assert!(DkimSelector::parse("a-b").is_ok());
|
||||
assert!(DkimSelector::parse("").is_err());
|
||||
assert!(DkimSelector::parse("a..b").is_err());
|
||||
assert!(DkimSelector::parse("-leading").is_err());
|
||||
assert!(DkimSelector::parse("trailing-").is_err());
|
||||
assert!(DkimSelector::parse("s_under").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tls_mode_parses_known_modes() {
|
||||
assert_eq!(TlsMode::parse("STARTTLS").unwrap(), TlsMode::Starttls);
|
||||
assert_eq!(TlsMode::parse("implicit").unwrap(), TlsMode::Implicit);
|
||||
assert_eq!(TlsMode::parse("none").unwrap(), TlsMode::None);
|
||||
assert!(TlsMode::parse("garbage").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn smtp_password_redacts_in_debug() {
|
||||
let p = SmtpPassword::parse("hunter2").unwrap();
|
||||
let dbg = format!("{:?}", p);
|
||||
assert_eq!(dbg, "SmtpPassword(***)");
|
||||
assert!(!dbg.contains("hunter2"));
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,15 @@
|
||||
pub mod email;
|
||||
mod locale;
|
||||
mod sender;
|
||||
mod types;
|
||||
|
||||
pub use email::EmailSender;
|
||||
pub use locale::{
|
||||
DEFAULT_LOCALE, NotificationStrings, VALID_LOCALES, format_message, get_strings,
|
||||
validate_locale,
|
||||
};
|
||||
pub use sender::{
|
||||
CommsSender, DiscordSender, EmailSender, SendError, SignalSender, TelegramSender,
|
||||
is_valid_phone_number, is_valid_signal_username, mime_encode_header, sanitize_header_value,
|
||||
CommsSender, DiscordSender, SendError, SignalSender, TelegramSender, is_valid_phone_number,
|
||||
is_valid_signal_username,
|
||||
};
|
||||
pub use types::{CommsChannel, CommsStatus, CommsType, NewComms, QueuedComms};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
pub const DEFAULT_LOCALE: &str = "en";
|
||||
pub const VALID_LOCALES: &[&str] = &["en", "zh", "ja", "ko", "sv", "fi"];
|
||||
pub const VALID_LOCALES: &[&str] = &["en", "zh", "ja", "ko", "sv", "fi", "fr"];
|
||||
|
||||
pub fn validate_locale(locale: &str) -> &str {
|
||||
if VALID_LOCALES.contains(&locale) {
|
||||
@@ -44,6 +44,7 @@ pub fn get_strings(locale: &str) -> &'static NotificationStrings {
|
||||
"ko" => &STRINGS_KO,
|
||||
"sv" => &STRINGS_SV,
|
||||
"fi" => &STRINGS_FI,
|
||||
"fr" => &STRINGS_FR,
|
||||
_ => &STRINGS_EN,
|
||||
}
|
||||
}
|
||||
@@ -216,6 +217,34 @@ static STRINGS_FI: NotificationStrings = NotificationStrings {
|
||||
channel_verification_body: "Vahvistuskoodisi on:\n{code}\n\nTai vahvista suoraan:\n{verify_link}",
|
||||
};
|
||||
|
||||
static STRINGS_FR: NotificationStrings = NotificationStrings {
|
||||
welcome_subject: "Bienvenue sur {hostname}",
|
||||
welcome_body: "Bienvenue sur {hostname} !\n\nVotre identifiant est : @{handle}\n\nMerci de nous avoir rejoint.",
|
||||
password_reset_subject: "Réinitialisation du mot de passe - {hostname}",
|
||||
password_reset_body: "Bonjour @{handle},\n\nVotre code de réinitialisation du mot de passe est : {code}\n\nCe code expirera dans 10 minutes.\n\nSi vous n'avez pas demandé cela, veuillez ignorer ce message.",
|
||||
email_update_subject: "Confirmer votre nouvelle adresse e-mail - {hostname}",
|
||||
email_update_body: "Bonjour @{handle},\n\nVotre code de vérification est :\n{code}\n\nCopiez le code ci-dessus et saisissez-le ici :\n{verify_page}\n\nCe code expirera dans 10 minutes.\n\nOu si vous aimez vivre dangereusement :\n{verify_link}\n\nSi vous n'avez pas demandé cela, veuillez ignorer cet e-mail.",
|
||||
short_token_body: "Bonjour @{handle},\n\nVotre code de vérification est :\n{code}\n\nCe code expirera dans 15 minutes.\n\nSi vous n'avez pas demandé cela, veuillez ignorer cet e-mail.",
|
||||
account_deletion_subject: "Demande de suppression de compte - {hostname}",
|
||||
account_deletion_body: "Bonjour @{handle},\n\nVotre code de confirmation de suppression de compte est : {code}\n\nCe code expirera dans 10 minutes.\n\nSi vous n'avez pas demandé cela, sécurisez votre compte immédiatement.",
|
||||
plc_operation_subject: "{hostname} - Jeton d'opération PLC",
|
||||
plc_operation_body: "Bonjour @{handle},\n\nVous avez demandé à signer une opération PLC pour votre compte.\n\nVotre jeton de vérification est : {token}\n\nCe jeton expirera dans 10 minutes.\n\nSi vous n'avez pas demandé cela, vous pouvez ignorer ce message en toute sécurité.",
|
||||
two_factor_code_subject: "Vérification de connexion - {hostname}",
|
||||
two_factor_code_body: "Bonjour @{handle},\n\nVotre code de vérification de connexion est : {code}\n\nCe code expirera dans 10 minutes.\n\nSi vous n'avez pas demandé cela, sécurisez votre compte immédiatement.",
|
||||
passkey_recovery_subject: "Récupération de compte - {hostname}",
|
||||
passkey_recovery_body: "Bonjour @{handle},\n\nVous avez demandé la récupération de votre compte à clé d'accès uniquement.\n\nCliquez sur le lien ci-dessous pour définir un mot de passe temporaire et retrouver l'accès :\n{url}\n\nCe lien expirera dans 1 heure.\n\nSi vous n'avez pas demandé cela, veuillez ignorer ce message. Votre compte reste sécurisé.",
|
||||
signup_verification_subject: "Vérifier votre compte - {hostname}",
|
||||
signup_verification_body: "Bienvenue ! Votre code de vérification est :\n{code}\n\nCopiez le code ci-dessus et saisissez-le ici :\n{verify_page}\n\nCe code expirera dans 30 minutes.\n\nOu si vous aimez vivre dangereusement :\n{verify_link}\n\nSi vous n'avez pas créé de compte sur {hostname}, veuillez ignorer ce message.",
|
||||
legacy_login_subject: "Alerte de sécurité : Connexion classique détectée - {hostname}",
|
||||
legacy_login_body: "Bonjour @{handle},\n\nUne connexion à votre compte a été détectée via une application classique (comme Bluesky) qui ne prend pas en charge la vérification TOTP.\n\nDétails :\n- Date : {timestamp}\n- Adresse IP : {ip}\n\nVotre protection TOTP a été contournée pour cette connexion. La session dispose de permissions limitées pour les opérations sensibles.\n\nSi ce n'était pas vous :\n1. Changez votre mot de passe immédiatement\n2. Vérifiez vos sessions actives\n3. Envisagez de désactiver les connexions d'applications classiques dans vos paramètres de sécurité\n\nRestez vigilant,\n{hostname}",
|
||||
migration_verification_subject: "Vérifier votre adresse e-mail - {hostname}",
|
||||
migration_verification_body: "Bienvenue sur {hostname} !\n\nVotre compte a été migré avec succès. Pour finaliser la configuration, veuillez vérifier votre adresse e-mail.\n\nVotre code de vérification est :\n{code}\n\nCopiez le code ci-dessus et saisissez-le ici :\n{verify_page}\n\nCe code expirera dans 48 heures.\n\nOu si vous aimez vivre dangereusement :\n{verify_link}\n\nSi vous n'avez pas migré votre compte, veuillez ignorer cet e-mail.",
|
||||
channel_verified_subject: "Canal de notification vérifié - {hostname}",
|
||||
channel_verified_body: "Bonjour {handle},\n\n{channel} a été vérifié comme canal de notification pour votre compte sur {hostname}.",
|
||||
channel_verification_subject: "Vérifier votre canal - {hostname}",
|
||||
channel_verification_body: "Votre code de vérification est :\n{code}\n\nOu vérifiez directement :\n{verify_link}",
|
||||
};
|
||||
|
||||
pub fn format_message(template: &str, vars: &[(&str, &str)]) -> String {
|
||||
vars.iter()
|
||||
.fold(template.to_string(), |result, (key, value)| {
|
||||
@@ -233,6 +262,9 @@ mod tests {
|
||||
assert_eq!(validate_locale("zh"), "zh");
|
||||
assert_eq!(validate_locale("ja"), "ja");
|
||||
assert_eq!(validate_locale("ko"), "ko");
|
||||
assert_eq!(validate_locale("sv"), "sv");
|
||||
assert_eq!(validate_locale("fi"), "fi");
|
||||
assert_eq!(validate_locale("fr"), "fr");
|
||||
assert_eq!(validate_locale("invalid"), DEFAULT_LOCALE);
|
||||
assert_eq!(validate_locale(""), DEFAULT_LOCALE);
|
||||
}
|
||||
@@ -252,5 +284,9 @@ mod tests {
|
||||
let zh = get_strings("zh");
|
||||
assert!(zh.welcome_subject.contains("{hostname}"));
|
||||
assert!(zh.welcome_body.contains("欢迎"));
|
||||
|
||||
let fr = get_strings("fr");
|
||||
assert!(fr.welcome_subject.contains("{hostname}"));
|
||||
assert!(fr.welcome_body.contains("Bienvenue"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
use async_trait::async_trait;
|
||||
use base64::{Engine, engine::general_purpose::STANDARD as BASE64};
|
||||
use reqwest::Client;
|
||||
use serde_json::json;
|
||||
use std::process::Stdio;
|
||||
use std::time::Duration;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::process::Command;
|
||||
|
||||
use super::types::{CommsChannel, QueuedComms};
|
||||
|
||||
@@ -21,25 +17,51 @@ pub trait CommsSender: Send + Sync {
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum SendError {
|
||||
#[error("Failed to spawn {command}: {source}")]
|
||||
ProcessSpawn {
|
||||
command: String,
|
||||
source: std::io::Error,
|
||||
},
|
||||
#[error("{command} exited with non-zero status: {detail}")]
|
||||
ProcessFailed { command: String, detail: String },
|
||||
#[error("Channel not configured: {0:?}")]
|
||||
NotConfigured(CommsChannel),
|
||||
#[error("External service error: {0}")]
|
||||
ExternalService(String),
|
||||
#[error("Email configuration invalid: {0}")]
|
||||
ConfigInvalid(String),
|
||||
#[error("Invalid recipient format: {0}")]
|
||||
InvalidRecipient(String),
|
||||
#[error("Message construction failed: {0}")]
|
||||
MessageBuild(String),
|
||||
#[error("transient DNS lookup failure: {0}")]
|
||||
DnsTransient(String),
|
||||
#[error("permanent DNS lookup failure: {0}")]
|
||||
DnsPermanent(String),
|
||||
#[error("SMTP transient error: {0}")]
|
||||
SmtpTransient(String),
|
||||
#[error("SMTP permanent error: {0}")]
|
||||
SmtpPermanent(String),
|
||||
#[error("DKIM signing failed: {0}")]
|
||||
DkimSign(String),
|
||||
#[error("External service error: {0}")]
|
||||
ExternalService(String),
|
||||
#[error("Request timeout")]
|
||||
Timeout,
|
||||
#[error("Max retries exceeded: {0}")]
|
||||
MaxRetriesExceeded(String),
|
||||
}
|
||||
|
||||
impl SendError {
|
||||
pub fn is_permanent(&self) -> bool {
|
||||
match self {
|
||||
Self::SmtpPermanent(_)
|
||||
| Self::DnsPermanent(_)
|
||||
| Self::InvalidRecipient(_)
|
||||
| Self::MessageBuild(_)
|
||||
| Self::DkimSign(_)
|
||||
| Self::ConfigInvalid(_) => true,
|
||||
Self::SmtpTransient(_)
|
||||
| Self::DnsTransient(_)
|
||||
| Self::Timeout
|
||||
| Self::ExternalService(_)
|
||||
| Self::MaxRetriesExceeded(_)
|
||||
| Self::NotConfigured(_) => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn create_http_client() -> Client {
|
||||
Client::builder()
|
||||
.timeout(Duration::from_secs(HTTP_TIMEOUT_SECS))
|
||||
@@ -100,19 +122,6 @@ where
|
||||
))
|
||||
}
|
||||
|
||||
pub fn sanitize_header_value(value: &str) -> String {
|
||||
value.replace(['\r', '\n'], " ").trim().to_string()
|
||||
}
|
||||
|
||||
pub fn mime_encode_header(value: &str) -> String {
|
||||
if value.is_ascii() {
|
||||
sanitize_header_value(value)
|
||||
} else {
|
||||
let sanitized = sanitize_header_value(value);
|
||||
format!("=?UTF-8?B?{}?=", BASE64.encode(sanitized.as_bytes()))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn escape_html(text: &str) -> String {
|
||||
text.replace('&', "&")
|
||||
.replace('<', "<")
|
||||
@@ -135,93 +144,6 @@ pub fn is_valid_signal_username(username: &str) -> bool {
|
||||
tranquil_signal::SignalUsername::parse(username).is_ok()
|
||||
}
|
||||
|
||||
pub struct EmailSender {
|
||||
from_address: String,
|
||||
from_name: String,
|
||||
sendmail_path: String,
|
||||
}
|
||||
|
||||
impl EmailSender {
|
||||
pub fn new(from_address: String, from_name: String, sendmail_path: String) -> Self {
|
||||
Self {
|
||||
from_address,
|
||||
from_name,
|
||||
sendmail_path,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_config(cfg: &tranquil_config::TranquilConfig) -> Option<Self> {
|
||||
let from_address = cfg.email.from_address.clone()?;
|
||||
let from_name = cfg.email.from_name.clone();
|
||||
let sendmail_path = cfg.email.sendmail_path.clone();
|
||||
Some(Self::new(from_address, from_name, sendmail_path))
|
||||
}
|
||||
|
||||
pub fn format_email(&self, notification: &QueuedComms) -> String {
|
||||
let subject = mime_encode_header(notification.subject.as_deref().unwrap_or("Notification"));
|
||||
let recipient = sanitize_header_value(¬ification.recipient);
|
||||
let from_header = if self.from_name.is_empty() {
|
||||
self.from_address.clone()
|
||||
} else {
|
||||
format!(
|
||||
"{} <{}>",
|
||||
sanitize_header_value(&self.from_name),
|
||||
self.from_address
|
||||
)
|
||||
};
|
||||
format!(
|
||||
"From: {}\r\nTo: {}\r\nSubject: {}\r\nContent-Type: text/plain; charset=utf-8\r\nMIME-Version: 1.0\r\n\r\n{}",
|
||||
from_header, recipient, subject, notification.body
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl CommsSender for EmailSender {
|
||||
fn channel(&self) -> CommsChannel {
|
||||
CommsChannel::Email
|
||||
}
|
||||
|
||||
async fn send(&self, notification: &QueuedComms) -> Result<(), SendError> {
|
||||
let email_content = self.format_email(notification);
|
||||
let mut child = Command::new(&self.sendmail_path)
|
||||
.arg("-t")
|
||||
.arg("-oi")
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.map_err(|e| SendError::ProcessSpawn {
|
||||
command: self.sendmail_path.clone(),
|
||||
source: e,
|
||||
})?;
|
||||
if let Some(mut stdin) = child.stdin.take() {
|
||||
stdin
|
||||
.write_all(email_content.as_bytes())
|
||||
.await
|
||||
.map_err(|e| SendError::ProcessSpawn {
|
||||
command: self.sendmail_path.clone(),
|
||||
source: e,
|
||||
})?;
|
||||
}
|
||||
let output = child
|
||||
.wait_with_output()
|
||||
.await
|
||||
.map_err(|e| SendError::ProcessSpawn {
|
||||
command: self.sendmail_path.clone(),
|
||||
source: e,
|
||||
})?;
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
return Err(SendError::ProcessFailed {
|
||||
command: self.sendmail_path.clone(),
|
||||
detail: stderr.to_string(),
|
||||
});
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
const DISCORD_API_BASE: &str = "https://discord.com/api/v10";
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -610,3 +532,28 @@ impl CommsSender for SignalSender {
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod is_permanent_matrix {
|
||||
use super::{CommsChannel, SendError};
|
||||
|
||||
#[test]
|
||||
fn permanent_variants_are_permanent() {
|
||||
assert!(SendError::SmtpPermanent("x".into()).is_permanent());
|
||||
assert!(SendError::DnsPermanent("x".into()).is_permanent());
|
||||
assert!(SendError::InvalidRecipient("x".into()).is_permanent());
|
||||
assert!(SendError::MessageBuild("x".into()).is_permanent());
|
||||
assert!(SendError::DkimSign("x".into()).is_permanent());
|
||||
assert!(SendError::ConfigInvalid("x".into()).is_permanent());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transient_variants_are_not_permanent() {
|
||||
assert!(!SendError::SmtpTransient("x".into()).is_permanent());
|
||||
assert!(!SendError::DnsTransient("x".into()).is_permanent());
|
||||
assert!(!SendError::Timeout.is_permanent());
|
||||
assert!(!SendError::ExternalService("x".into()).is_permanent());
|
||||
assert!(!SendError::MaxRetriesExceeded("x".into()).is_permanent());
|
||||
assert!(!SendError::NotConfigured(CommsChannel::Email).is_permanent());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use chrono::Utc;
|
||||
use lettre::message::Mailbox;
|
||||
use lettre::transport::smtp::AsyncSmtpTransport;
|
||||
use lettre::transport::smtp::extension::ClientId;
|
||||
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tranquil_comms::email::transport::SendMode;
|
||||
use tranquil_comms::email::{EmailSender, types::HeloName};
|
||||
use tranquil_comms::{CommsChannel, CommsSender, CommsStatus, CommsType, QueuedComms, SendError};
|
||||
use uuid::Uuid;
|
||||
|
||||
fn fixture(recipient: &str, subject: &str, body: &str) -> QueuedComms {
|
||||
QueuedComms {
|
||||
id: Uuid::new_v4(),
|
||||
user_id: None,
|
||||
channel: CommsChannel::Email,
|
||||
comms_type: CommsType::Welcome,
|
||||
status: CommsStatus::Pending,
|
||||
recipient: recipient.to_string(),
|
||||
subject: Some(subject.to_string()),
|
||||
body: body.to_string(),
|
||||
metadata: None,
|
||||
attempts: 0,
|
||||
max_attempts: 3,
|
||||
last_error: None,
|
||||
created_at: Utc::now(),
|
||||
updated_at: Utc::now(),
|
||||
scheduled_for: Utc::now(),
|
||||
processed_at: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn build_smarthost_sender(host: &str, port: u16) -> EmailSender {
|
||||
build_smarthost_sender_with_total_timeout(host, port, Duration::from_secs(10))
|
||||
}
|
||||
|
||||
fn build_smarthost_sender_with_total_timeout(
|
||||
host: &str,
|
||||
port: u16,
|
||||
total_timeout: Duration,
|
||||
) -> EmailSender {
|
||||
let from: Mailbox = "Tranquil Test <noreply@nel.pet>".parse().unwrap();
|
||||
let helo = HeloName::parse("mta.nel.pet").unwrap();
|
||||
let transport = AsyncSmtpTransport::<lettre::Tokio1Executor>::builder_dangerous(host)
|
||||
.port(port)
|
||||
.hello_name(ClientId::Domain(helo.into_inner()))
|
||||
.timeout(Some(Duration::from_secs(5)))
|
||||
.build();
|
||||
EmailSender::new(
|
||||
from,
|
||||
SendMode::Smarthost {
|
||||
transport: Box::new(transport),
|
||||
total_timeout,
|
||||
},
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
async fn drive_stub(stream: TcpStream, rcpt_response: &'static [u8]) -> std::io::Result<()> {
|
||||
let (read, mut write) = stream.into_split();
|
||||
let mut reader = BufReader::new(read);
|
||||
write.write_all(b"220 stub ESMTP\r\n").await?;
|
||||
let mut line = String::new();
|
||||
loop {
|
||||
line.clear();
|
||||
let n = reader.read_line(&mut line).await?;
|
||||
if n == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
let upper = line.to_ascii_uppercase();
|
||||
let response: &[u8] = match upper.split_whitespace().next() {
|
||||
Some("EHLO") | Some("HELO") => b"250-stub\r\n250 SIZE 10240000\r\n",
|
||||
Some("MAIL") => b"250 OK\r\n",
|
||||
Some("RCPT") => rcpt_response,
|
||||
Some("DATA") => b"354 end with .\r\n",
|
||||
Some("RSET") => b"250 OK\r\n",
|
||||
Some("QUIT") => b"221 bye\r\n",
|
||||
_ => b"500 unknown\r\n",
|
||||
};
|
||||
write.write_all(response).await?;
|
||||
if upper.starts_with("QUIT") {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn spawn_stub(rcpt_response: &'static [u8]) -> u16 {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
tokio::spawn(async move {
|
||||
let (stream, _) = listener.accept().await.unwrap();
|
||||
let _ = drive_stub(stream, rcpt_response).await;
|
||||
});
|
||||
port
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rcpt_550_classifies_as_smtp_permanent() {
|
||||
let port = spawn_stub(b"550 5.1.1 user unknown\r\n").await;
|
||||
let sender = build_smarthost_sender("127.0.0.1", port);
|
||||
let result = sender.send(&fixture("nel@nel.pet", "x", "x")).await;
|
||||
match result {
|
||||
Err(SendError::SmtpPermanent(_)) => {}
|
||||
other => panic!("expected SmtpPermanent, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rcpt_421_classifies_as_smtp_transient() {
|
||||
let port = spawn_stub(b"421 4.7.0 try again later\r\n").await;
|
||||
let sender = build_smarthost_sender("127.0.0.1", port);
|
||||
let result = sender.send(&fixture("nel@nel.pet", "x", "x")).await;
|
||||
match result {
|
||||
Err(SendError::SmtpTransient(_)) => {}
|
||||
other => panic!("expected SmtpTransient, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn invalid_recipient_classifies_as_invalid_recipient() {
|
||||
let port = spawn_stub(b"250 OK\r\n").await;
|
||||
let sender = build_smarthost_sender("127.0.0.1", port);
|
||||
let result = sender.send(&fixture("not-an-address", "x", "x")).await;
|
||||
match result {
|
||||
Err(SendError::InvalidRecipient(_)) => {}
|
||||
other => panic!("expected InvalidRecipient, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
async fn spawn_silent_stub() -> u16 {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
tokio::spawn(async move {
|
||||
let (_stream, _) = listener.accept().await.unwrap();
|
||||
std::future::pending::<()>().await;
|
||||
});
|
||||
port
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn smarthost_silent_relay_hits_total_timeout() {
|
||||
let port = spawn_silent_stub().await;
|
||||
let sender =
|
||||
build_smarthost_sender_with_total_timeout("127.0.0.1", port, Duration::from_millis(500));
|
||||
let start = std::time::Instant::now();
|
||||
let result = sender.send(&fixture("nel@nel.pet", "x", "x")).await;
|
||||
let elapsed = start.elapsed();
|
||||
match result {
|
||||
Err(SendError::Timeout) => {}
|
||||
other => panic!("expected Timeout, got {other:?}"),
|
||||
}
|
||||
assert!(
|
||||
elapsed < Duration::from_secs(2),
|
||||
"send returned in {elapsed:?}, expected close to 500ms total_timeout"
|
||||
);
|
||||
}
|
||||
@@ -5,6 +5,14 @@ use std::sync::OnceLock;
|
||||
|
||||
static CONFIG: OnceLock<TranquilConfig> = OnceLock::new();
|
||||
|
||||
const REMOVED_ENV_VARS: &[(&str, &str)] = &[(
|
||||
"SENDMAIL_PATH",
|
||||
"the sendmail-binary transport was replaced with native SMTP. \
|
||||
Configure MAIL_SMARTHOST_HOST for relay delivery, or leave it unset to \
|
||||
deliver directly via recipient MX records. See example.toml for the full \
|
||||
MAIL_* surface.",
|
||||
)];
|
||||
|
||||
/// Errors discovered during configuration validation.
|
||||
#[derive(Debug)]
|
||||
pub struct ConfigError {
|
||||
@@ -96,6 +104,7 @@ pub fn load(config_path: Option<&PathBuf>) -> Result<TranquilConfig, confique::E
|
||||
|
||||
// Root configuration
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct TranquilConfig {
|
||||
#[config(nested)]
|
||||
pub server: ServerConfig,
|
||||
@@ -133,9 +142,6 @@ pub struct TranquilConfig {
|
||||
#[config(nested)]
|
||||
pub telegram: TelegramConfig,
|
||||
|
||||
#[config(nested)]
|
||||
pub signal: SignalConfig,
|
||||
|
||||
#[config(nested)]
|
||||
pub notifications: NotificationConfig,
|
||||
|
||||
@@ -162,6 +168,14 @@ impl TranquilConfig {
|
||||
pub fn validate(&self, ignore_secrets: bool) -> Result<(), ConfigError> {
|
||||
let mut errors = Vec::new();
|
||||
|
||||
// -- removed config ---------------------------------------------------
|
||||
errors.extend(
|
||||
REMOVED_ENV_VARS
|
||||
.iter()
|
||||
.filter(|(var, _)| std::env::var_os(var).is_some())
|
||||
.map(|(var, guidance)| format!("{var} is no longer supported: {guidance}")),
|
||||
);
|
||||
|
||||
// -- secrets ----------------------------------------------------------
|
||||
if !ignore_secrets && !self.secrets.allow_insecure && !cfg!(test) {
|
||||
if let Some(ref s) = self.secrets.jwt_secret {
|
||||
@@ -210,6 +224,10 @@ impl TranquilConfig {
|
||||
}
|
||||
}
|
||||
|
||||
// -- email -----------------------------------------------------------
|
||||
self.email
|
||||
.validate(self.server.hostname_without_port(), &mut errors);
|
||||
|
||||
// -- telegram ---------------------------------------------------------
|
||||
if self.telegram.bot_token.is_some() && self.telegram.webhook_secret.is_none() {
|
||||
errors.push(
|
||||
@@ -238,6 +256,9 @@ impl TranquilConfig {
|
||||
}
|
||||
}
|
||||
|
||||
// -- tls --------------------------------------------------------------
|
||||
self.server.tls.validate(&mut errors);
|
||||
|
||||
// -- SSO providers ----------------------------------------------------
|
||||
self.validate_sso_provider("sso.github", &self.sso.github, &mut errors);
|
||||
self.validate_sso_provider("sso.google", &self.sso.google, &mut errors);
|
||||
@@ -397,6 +418,7 @@ impl TranquilConfig {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct ServerConfig {
|
||||
/// Public hostname of the PDS, such as `pds.example.com`.
|
||||
#[config(env = "PDS_HOSTNAME")]
|
||||
@@ -436,6 +458,11 @@ pub struct ServerConfig {
|
||||
#[config(env = "DISABLE_RATE_LIMITING", default = false)]
|
||||
pub disable_rate_limiting: bool,
|
||||
|
||||
/// Skip the verified-comms-channel gate for login and record writes.
|
||||
/// Please keep this off unless you're an invite-only PDS!
|
||||
#[config(env = "DISABLE_ACCOUNT_VERIFICATION_GATE", default = false)]
|
||||
pub disable_account_verification_gate: bool,
|
||||
|
||||
/// List of additional banned words for handle validation.
|
||||
#[config(env = "PDS_BANNED_WORDS", parse_env = split_comma_list)]
|
||||
pub banned_words: Option<Vec<String>>,
|
||||
@@ -459,6 +486,53 @@ pub struct ServerConfig {
|
||||
/// Maximum allowed number of preferences
|
||||
#[config(env = "MAX_PREFERENCES_COUNT", default = 1000)]
|
||||
pub max_preferences_count: usize,
|
||||
|
||||
/// If you're not altering TLS config, you don't have to worry about this.
|
||||
/// This is the number of trusted reverse proxies in front of Tranquil.
|
||||
/// We read the client IP used for rate limiting and device records this many hops
|
||||
/// from the right of the X-Forwarded-For header.
|
||||
/// When left unset, Tranquil will assume:
|
||||
/// - 0, if the TLS termination is happening here on Tranquil via the TLS config
|
||||
/// - 1, if the TLS termination *isn't* happening here.
|
||||
#[config(env = "TRUSTED_PROXY_COUNT")]
|
||||
pub trusted_proxy_count: Option<usize>,
|
||||
|
||||
#[config(nested)]
|
||||
pub tls: TlsConfig,
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct TlsConfig {
|
||||
/// The path to the TLS cert chain.
|
||||
/// If you set both this and `key_path`, the server terminates TLS itself rather than expecting
|
||||
/// a reverse proxy to do it. The certificate and key reload on SIGHUP.
|
||||
#[config(env = "TLS_CERT_PATH")]
|
||||
pub cert_path: Option<String>,
|
||||
|
||||
/// Path to the TLS private key.
|
||||
#[config(env = "TLS_KEY_PATH")]
|
||||
pub key_path: Option<String>,
|
||||
}
|
||||
|
||||
impl TlsConfig {
|
||||
/// The certificate and key paths when both are configured.
|
||||
pub fn material(&self) -> Option<(&str, &str)> {
|
||||
match (self.cert_path.as_deref(), self.key_path.as_deref()) {
|
||||
(Some(cert), Some(key)) => Some((cert, key)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn validate(&self, errors: &mut Vec<String>) {
|
||||
if self.cert_path.is_some() != self.key_path.is_some() {
|
||||
errors.push(
|
||||
"server.tls.cert_path (TLS_CERT_PATH) and server.tls.key_path (TLS_KEY_PATH) \
|
||||
must both be set to enable app-level TLS, or both be unset"
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ServerConfig {
|
||||
@@ -493,6 +567,7 @@ impl ServerConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct FrontendConfig {
|
||||
/// Whether to enable the built in serving of the frontend.
|
||||
#[config(env = "FRONTEND_ENABLED", default = true)]
|
||||
@@ -505,6 +580,7 @@ pub struct FrontendConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct DatabaseConfig {
|
||||
/// PostgreSQL connection URL.
|
||||
#[config(env = "DATABASE_URL")]
|
||||
@@ -524,6 +600,7 @@ pub struct DatabaseConfig {
|
||||
}
|
||||
|
||||
#[derive(Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct SecretsConfig {
|
||||
/// Secret used for signing JWTs. Must be at least 32 characters in
|
||||
/// production.
|
||||
@@ -650,6 +727,7 @@ impl fmt::Display for RepoBackend {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct StorageConfig {
|
||||
/// Storage backend: `filesystem` or `s3`.
|
||||
#[config(env = "BLOB_STORAGE_BACKEND", default = "filesystem")]
|
||||
@@ -682,6 +760,7 @@ impl StorageConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct CacheConfig {
|
||||
/// Cache backend: `ripple` by default, or `valkey`.
|
||||
#[config(env = "CACHE_BACKEND", default = "ripple")]
|
||||
@@ -696,6 +775,7 @@ pub struct CacheConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct PlcConfig {
|
||||
/// Base URL of the PLC directory.
|
||||
#[config(env = "PLC_DIRECTORY_URL", default = "https://plc.directory")]
|
||||
@@ -715,6 +795,7 @@ pub struct PlcConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct FirehoseConfig {
|
||||
/// Size of the in-memory broadcast buffer for firehose events.
|
||||
#[config(env = "FIREHOSE_BUFFER_SIZE", default = 10000)]
|
||||
@@ -745,6 +826,7 @@ impl FirehoseConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct EmailConfig {
|
||||
/// Sender email address. When unset, email sending is disabled.
|
||||
#[config(env = "MAIL_FROM_ADDRESS")]
|
||||
@@ -754,12 +836,285 @@ pub struct EmailConfig {
|
||||
#[config(env = "MAIL_FROM_NAME", default = "Tranquil PDS")]
|
||||
pub from_name: String,
|
||||
|
||||
/// Path to the `sendmail` binary.
|
||||
#[config(env = "SENDMAIL_PATH", default = "/usr/sbin/sendmail")]
|
||||
pub sendmail_path: String,
|
||||
/// HELO/EHLO name announced to remote SMTP servers. Applies to both
|
||||
/// smarthost and direct-MX modes. Defaults to the server hostname.
|
||||
#[config(env = "MAIL_HELO_NAME")]
|
||||
pub helo_name: Option<String>,
|
||||
|
||||
#[config(nested)]
|
||||
pub smarthost: SmarthostConfig,
|
||||
|
||||
#[config(nested)]
|
||||
pub direct_mx: DirectMxConfig,
|
||||
|
||||
#[config(nested)]
|
||||
pub dkim: DkimConfig,
|
||||
}
|
||||
|
||||
impl EmailConfig {
|
||||
pub fn validate(&self, server_hostname: &str, errors: &mut Vec<String>) {
|
||||
match self.smarthost.tls.to_ascii_lowercase().as_str() {
|
||||
"implicit" | "starttls" => {}
|
||||
"none" => {
|
||||
if self.smarthost.password.is_some() {
|
||||
errors.push(
|
||||
"email.smarthost.tls = \"none\" with email.smarthost.password set \
|
||||
would transmit credentials in plaintext; use \"starttls\" or \"implicit\""
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
other => errors.push(format!(
|
||||
"email.smarthost.tls must be \"implicit\", \"starttls\", or \"none\", got \"{other}\""
|
||||
)),
|
||||
}
|
||||
|
||||
let smarthost_host_set = self
|
||||
.smarthost
|
||||
.host
|
||||
.as_deref()
|
||||
.is_some_and(|h| !h.is_empty());
|
||||
let username_set = self.smarthost.username.is_some();
|
||||
let password_set = self.smarthost.password.is_some();
|
||||
if !smarthost_host_set && (username_set || password_set) {
|
||||
errors.push(
|
||||
"email.smarthost.username or email.smarthost.password is set but \
|
||||
email.smarthost.host is empty; credentials would be silently ignored"
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
if smarthost_host_set && username_set != password_set {
|
||||
errors.push(
|
||||
"email.smarthost.username and email.smarthost.password must both be set or \
|
||||
both unset; otherwise authentication would silently degrade to anonymous"
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
if self.smarthost.command_timeout_secs == 0 {
|
||||
errors.push("email.smarthost.command_timeout_secs must be at least 1".to_string());
|
||||
}
|
||||
if self.smarthost.total_timeout_secs == 0 {
|
||||
errors.push("email.smarthost.total_timeout_secs must be at least 1".to_string());
|
||||
}
|
||||
if self.smarthost.pool_size == 0 {
|
||||
errors.push("email.smarthost.pool_size must be at least 1".to_string());
|
||||
}
|
||||
|
||||
if self.direct_mx.max_concurrent_sends == 0 {
|
||||
errors.push("email.direct_mx.max_concurrent_sends must be at least 1".to_string());
|
||||
}
|
||||
if self.direct_mx.command_timeout_secs == 0 {
|
||||
errors.push("email.direct_mx.command_timeout_secs must be at least 1".to_string());
|
||||
}
|
||||
if self.direct_mx.total_timeout_secs == 0 {
|
||||
errors.push("email.direct_mx.total_timeout_secs must be at least 1".to_string());
|
||||
}
|
||||
|
||||
let dkim_set = self.dkim.selector.is_some()
|
||||
|| self.dkim.domain.is_some()
|
||||
|| self.dkim.private_key_path.is_some();
|
||||
if dkim_set {
|
||||
if self.dkim.selector.is_none() {
|
||||
errors
|
||||
.push("email.dkim.selector is required when any DKIM field is set".to_string());
|
||||
}
|
||||
if self.dkim.domain.is_none() {
|
||||
errors.push("email.dkim.domain is required when any DKIM field is set".to_string());
|
||||
}
|
||||
if self.dkim.private_key_path.is_none() {
|
||||
errors.push(
|
||||
"email.dkim.private_key_path is required when any DKIM field is set"
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let Some(from_address) = self.from_address.as_deref().filter(|s| !s.is_empty()) else {
|
||||
return;
|
||||
};
|
||||
|
||||
if !looks_like_email_address(from_address) {
|
||||
errors.push(format!(
|
||||
"email.from_address {from_address:?} is not a valid email address"
|
||||
));
|
||||
}
|
||||
if self.from_name.chars().any(|c| c.is_control()) {
|
||||
errors.push("email.from_name must not contain control characters".to_string());
|
||||
}
|
||||
|
||||
let helo_raw = self
|
||||
.helo_name
|
||||
.as_deref()
|
||||
.map(str::to_string)
|
||||
.unwrap_or_else(|| server_hostname.to_string());
|
||||
if !is_non_whitespace_token(&helo_raw) {
|
||||
errors.push(format!(
|
||||
"email HELO name {helo_raw:?} must be non-empty and contain no whitespace"
|
||||
));
|
||||
}
|
||||
|
||||
if smarthost_host_set {
|
||||
let host = self.smarthost.host.as_deref().unwrap_or("");
|
||||
if !is_non_whitespace_token(host) {
|
||||
errors.push(format!(
|
||||
"email.smarthost.host {host:?} must contain no whitespace"
|
||||
));
|
||||
}
|
||||
if self.smarthost.port == 0 {
|
||||
errors.push("email.smarthost.port must be non-zero".to_string());
|
||||
}
|
||||
if let Some(u) = self.smarthost.username.as_deref()
|
||||
&& u.is_empty()
|
||||
{
|
||||
errors.push("email.smarthost.username must be non-empty".to_string());
|
||||
}
|
||||
if let Some(p) = self.smarthost.password.as_deref()
|
||||
&& p.is_empty()
|
||||
{
|
||||
errors.push("email.smarthost.password must be non-empty".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(selector) = self.dkim.selector.as_deref()
|
||||
&& !is_valid_dkim_selector(selector)
|
||||
{
|
||||
errors.push(format!(
|
||||
"email.dkim.selector {selector:?} must be valid subdomain syntax"
|
||||
));
|
||||
}
|
||||
if let Some(domain) = self.dkim.domain.as_deref()
|
||||
&& !is_non_whitespace_token(domain)
|
||||
{
|
||||
errors.push(format!(
|
||||
"email.dkim.domain {domain:?} must be non-empty and contain no whitespace"
|
||||
));
|
||||
}
|
||||
if let Some(key_path) = self.dkim.private_key_path.as_deref()
|
||||
&& key_path.trim().is_empty()
|
||||
{
|
||||
errors.push("email.dkim.private_key_path must be non-empty".to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn looks_like_email_address(s: &str) -> bool {
|
||||
let trimmed = s.trim();
|
||||
if trimmed.is_empty() || trimmed.chars().any(char::is_whitespace) {
|
||||
return false;
|
||||
}
|
||||
let mut parts = trimmed.split('@');
|
||||
let local = parts.next().unwrap_or("");
|
||||
let domain = parts.next().unwrap_or("");
|
||||
parts.next().is_none() && !local.is_empty() && !domain.is_empty() && domain.contains('.')
|
||||
}
|
||||
|
||||
fn is_non_whitespace_token(s: &str) -> bool {
|
||||
let trimmed = s.trim();
|
||||
!trimmed.is_empty() && !trimmed.chars().any(char::is_whitespace)
|
||||
}
|
||||
|
||||
fn is_valid_dkim_selector(s: &str) -> bool {
|
||||
let trimmed = s.trim();
|
||||
!trimmed.is_empty()
|
||||
&& trimmed.split('.').all(|seg| {
|
||||
let starts_alnum = seg
|
||||
.chars()
|
||||
.next()
|
||||
.is_some_and(|c| c.is_ascii_alphanumeric());
|
||||
let ends_alnum = seg
|
||||
.chars()
|
||||
.next_back()
|
||||
.is_some_and(|c| c.is_ascii_alphanumeric());
|
||||
let body_ok = seg.chars().all(|c| c.is_ascii_alphanumeric() || c == '-');
|
||||
starts_alnum && ends_alnum && body_ok
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct SmarthostConfig {
|
||||
/// SMTP relay host. When set, mail is delivered through this host
|
||||
/// instead of resolving recipient MX records directly.
|
||||
#[config(env = "MAIL_SMARTHOST_HOST")]
|
||||
pub host: Option<String>,
|
||||
|
||||
/// SMTP relay port.
|
||||
#[config(env = "MAIL_SMARTHOST_PORT", default = 587)]
|
||||
pub port: u16,
|
||||
|
||||
/// SMTP authentication username.
|
||||
#[config(env = "MAIL_SMARTHOST_USERNAME")]
|
||||
pub username: Option<String>,
|
||||
|
||||
/// SMTP authentication password.
|
||||
#[config(env = "MAIL_SMARTHOST_PASSWORD")]
|
||||
pub password: Option<String>,
|
||||
|
||||
/// TLS mode. Valid values: "implicit", "starttls", "none". Setting "none"
|
||||
/// alongside a password is rejected at startup to prevent transmitting
|
||||
/// credentials in plaintext.
|
||||
#[config(env = "MAIL_SMARTHOST_TLS", default = "starttls")]
|
||||
pub tls: String,
|
||||
|
||||
/// Max size of the connection pool.
|
||||
#[config(env = "MAIL_SMARTHOST_POOL_SIZE", default = 4)]
|
||||
pub pool_size: u32,
|
||||
|
||||
/// Per-command SMTP timeout in seconds. Bounds the security handshake.
|
||||
#[config(env = "MAIL_SMARTHOST_COMMAND_TIMEOUT_SECS", default = 30)]
|
||||
pub command_timeout_secs: u64,
|
||||
|
||||
/// Total per-message timeout in seconds. Wraps the entire send so a
|
||||
/// stuck relay cannot stall the comms queue.
|
||||
#[config(env = "MAIL_SMARTHOST_TOTAL_TIMEOUT_SECS", default = 60)]
|
||||
pub total_timeout_secs: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct DirectMxConfig {
|
||||
/// Per-command SMTP timeout in seconds.
|
||||
#[config(env = "MAIL_COMMAND_TIMEOUT_SECS", default = 30)]
|
||||
pub command_timeout_secs: u64,
|
||||
|
||||
/// Total per-message timeout across all MX attempts in seconds.
|
||||
#[config(env = "MAIL_TOTAL_TIMEOUT_SECS", default = 60)]
|
||||
pub total_timeout_secs: u64,
|
||||
|
||||
/// Max number of concurrent direct-MX sends. Limits the load placed
|
||||
/// on any single recipient MX during a backlog drain.
|
||||
#[config(env = "MAIL_MAX_CONCURRENT_SENDS", default = 8)]
|
||||
pub max_concurrent_sends: usize,
|
||||
|
||||
/// Require STARTTLS on every MX hop. When false, TLS is
|
||||
/// attempted opportunistically and the session falls back to plaintext
|
||||
/// if the remote does not advertise STARTTLS. Set true to refuse
|
||||
/// plaintext delivery, at the cost of failing sends to MX hosts that
|
||||
/// do not support TLS.
|
||||
#[config(env = "MAIL_REQUIRE_TLS", default = false)]
|
||||
pub require_tls: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct DkimConfig {
|
||||
/// DKIM selector. When unset, outgoing mail is not signed.
|
||||
#[config(env = "MAIL_DKIM_SELECTOR")]
|
||||
pub selector: Option<String>,
|
||||
|
||||
/// DKIM signing domain.
|
||||
#[config(env = "MAIL_DKIM_DOMAIN")]
|
||||
pub domain: Option<String>,
|
||||
|
||||
/// Path to the DKIM private key in PEM format. Supports RSA and
|
||||
/// Ed25519 keys.
|
||||
#[config(env = "MAIL_DKIM_KEY_PATH")]
|
||||
pub private_key_path: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct DiscordConfig {
|
||||
/// Discord bot token. When unset, Discord integration is disabled.
|
||||
#[config(env = "DISCORD_BOT_TOKEN")]
|
||||
@@ -767,6 +1122,7 @@ pub struct DiscordConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct TelegramConfig {
|
||||
/// Telegram bot token. When unset, Telegram integration is disabled.
|
||||
#[config(env = "TELEGRAM_BOT_TOKEN")]
|
||||
@@ -778,14 +1134,7 @@ pub struct TelegramConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
pub struct SignalConfig {
|
||||
/// Protocol state is stored in postgres' signal_* tables.
|
||||
/// Link a device via the admin API before enabling.
|
||||
#[config(env = "SIGNAL_ENABLED", default = false)]
|
||||
pub enabled: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct NotificationConfig {
|
||||
/// Polling interval in milliseconds for the comms queue.
|
||||
#[config(env = "NOTIFICATION_POLL_INTERVAL_MS", default = 1000)]
|
||||
@@ -808,6 +1157,7 @@ pub trait SsoProviderIssuerConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct SsoConfig {
|
||||
#[config(nested)]
|
||||
pub github: SsoGitHubConfig,
|
||||
@@ -829,6 +1179,7 @@ pub struct SsoConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct SsoGitHubConfig {
|
||||
#[config(env = "SSO_GITHUB_ENABLED", default = false)]
|
||||
pub enabled: bool,
|
||||
@@ -862,6 +1213,7 @@ impl SsoProviderConfig for SsoGitHubConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct SsoDiscordConfig {
|
||||
#[config(env = "SSO_DISCORD_ENABLED", default = false)]
|
||||
pub enabled: bool,
|
||||
@@ -895,6 +1247,7 @@ impl SsoProviderConfig for SsoDiscordConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct SsoGoogleConfig {
|
||||
#[config(env = "SSO_GOOGLE_ENABLED", default = false)]
|
||||
pub enabled: bool,
|
||||
@@ -928,6 +1281,7 @@ impl SsoProviderConfig for SsoGoogleConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct SsoGitLabConfig {
|
||||
#[config(env = "SSO_GITLAB_ENABLED", default = false)]
|
||||
pub enabled: bool,
|
||||
@@ -970,6 +1324,7 @@ impl SsoProviderIssuerConfig for SsoGitLabConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct SsoOidcConfig {
|
||||
#[config(env = "SSO_OIDC_ENABLED", default = false)]
|
||||
pub enabled: bool,
|
||||
@@ -1012,6 +1367,7 @@ impl SsoProviderIssuerConfig for SsoOidcConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct SsoAppleConfig {
|
||||
#[config(env = "SSO_APPLE_ENABLED", default = false)]
|
||||
pub enabled: bool,
|
||||
@@ -1030,6 +1386,7 @@ pub struct SsoAppleConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct ModerationConfig {
|
||||
/// External report-handling service URL.
|
||||
#[config(env = "REPORT_SERVICE_URL")]
|
||||
@@ -1041,6 +1398,7 @@ pub struct ModerationConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct ImportConfig {
|
||||
/// Whether the PDS accepts repo imports.
|
||||
#[config(env = "ACCEPTING_REPO_IMPORTS", default = true)]
|
||||
@@ -1072,6 +1430,7 @@ fn split_comma_list(value: &str) -> Result<Vec<String>, std::convert::Infallible
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct RippleCacheConfig {
|
||||
/// Address to bind the Ripple gossip protocol listener.
|
||||
#[config(env = "RIPPLE_BIND", default = "0.0.0.0:0")]
|
||||
@@ -1095,6 +1454,7 @@ pub struct RippleCacheConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct ScheduledConfig {
|
||||
/// Interval in seconds between scheduled delete checks.
|
||||
#[config(env = "SCHEDULED_DELETE_CHECK_INTERVAL_SECS", default = 3600)]
|
||||
@@ -1142,6 +1502,7 @@ pub struct ScheduledConfig {
|
||||
}
|
||||
|
||||
#[derive(Debug, Config)]
|
||||
#[config(layer_attr(serde(deny_unknown_fields)))]
|
||||
pub struct TranquilStoreConfig {
|
||||
/// Directory for tranquil-store data: the metastore, eventlog, and blockstore.
|
||||
#[config(
|
||||
@@ -1196,3 +1557,315 @@ pub struct TranquilStoreConfig {
|
||||
pub fn template() -> String {
|
||||
confique::toml::template::<TranquilConfig>(confique::toml::FormatOptions::default())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn seed_required_env() {
|
||||
let required = [
|
||||
("PDS_HOSTNAME", "test.local"),
|
||||
("DATABASE_URL", "postgres://localhost/test"),
|
||||
("TRANQUIL_PDS_ALLOW_INSECURE_SECRETS", "1"),
|
||||
("INVITE_CODE_REQUIRED", "false"),
|
||||
("ENABLE_PDS_HOSTED_DID_WEB", "true"),
|
||||
("TRANQUIL_LEXICON_OFFLINE", "1"),
|
||||
];
|
||||
required
|
||||
.iter()
|
||||
.filter(|(k, _)| std::env::var_os(k).is_none())
|
||||
.for_each(|(k, v)| unsafe { std::env::set_var(k, v) });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_rejects_unknown_top_level_key() {
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
"tranquil-config-unknown-toplevel-{}",
|
||||
std::process::id()
|
||||
));
|
||||
std::fs::create_dir_all(&dir).expect("mkdir tempdir");
|
||||
let path = dir.join("config.toml");
|
||||
std::fs::write(
|
||||
&path,
|
||||
r#"
|
||||
[server]
|
||||
hostname = "test.local"
|
||||
|
||||
[totally_made_up]
|
||||
foo = "bar"
|
||||
"#,
|
||||
)
|
||||
.expect("write tempfile");
|
||||
|
||||
let result = TranquilConfig::builder().file(&path).load();
|
||||
let _ = std::fs::remove_file(&path);
|
||||
let _ = std::fs::remove_dir(&dir);
|
||||
|
||||
let err = format!("{:#}", result.expect_err("load must reject unknown key"));
|
||||
assert!(
|
||||
err.contains("totally_made_up"),
|
||||
"expected totally_made_up in error, got {err:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_rejects_unknown_nested_key() {
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
"tranquil-config-unknown-nested-{}",
|
||||
std::process::id()
|
||||
));
|
||||
std::fs::create_dir_all(&dir).expect("mkdir tempdir");
|
||||
let path = dir.join("config.toml");
|
||||
std::fs::write(
|
||||
&path,
|
||||
r#"
|
||||
[server]
|
||||
hostname = "test.local"
|
||||
not_a_real_field = "oops"
|
||||
"#,
|
||||
)
|
||||
.expect("write tempfile");
|
||||
|
||||
let result = TranquilConfig::builder().file(&path).load();
|
||||
let _ = std::fs::remove_file(&path);
|
||||
let _ = std::fs::remove_dir(&dir);
|
||||
|
||||
let err = format!("{:#}", result.expect_err("load must reject unknown key"));
|
||||
assert!(
|
||||
err.contains("not_a_real_field"),
|
||||
"expected not_a_real_field in error, got {err:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_accepts_known_keys() {
|
||||
let dir =
|
||||
std::env::temp_dir().join(format!("tranquil-config-known-keys-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).expect("mkdir tempdir");
|
||||
let path = dir.join("config.toml");
|
||||
std::fs::write(
|
||||
&path,
|
||||
r#"
|
||||
[server]
|
||||
hostname = "test.local"
|
||||
port = 3000
|
||||
|
||||
[database]
|
||||
url = "postgres://localhost/test"
|
||||
|
||||
[email.smarthost]
|
||||
host = "smtp.example"
|
||||
port = 587
|
||||
"#,
|
||||
)
|
||||
.expect("write tempfile");
|
||||
|
||||
let result = TranquilConfig::builder().file(&path).load();
|
||||
let _ = std::fs::remove_file(&path);
|
||||
let _ = std::fs::remove_dir(&dir);
|
||||
|
||||
result.expect("known keys must load successfully");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serial_validate_rejects_legacy_sendmail_path() {
|
||||
seed_required_env();
|
||||
unsafe { std::env::set_var("SENDMAIL_PATH", "/usr/sbin/sendmail") };
|
||||
let config = TranquilConfig::builder()
|
||||
.env()
|
||||
.load()
|
||||
.expect("load fresh config");
|
||||
let result = config.validate(true);
|
||||
unsafe { std::env::remove_var("SENDMAIL_PATH") };
|
||||
|
||||
let err = result.expect_err("validate must reject SENDMAIL_PATH");
|
||||
let mentions_sendmail = err.errors.iter().any(|e| e.contains("SENDMAIL_PATH"));
|
||||
assert!(
|
||||
mentions_sendmail,
|
||||
"errors did not mention SENDMAIL_PATH: {:?}",
|
||||
err.errors
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn serial_validate_passes_when_no_legacy_env_set() {
|
||||
seed_required_env();
|
||||
unsafe { std::env::remove_var("SENDMAIL_PATH") };
|
||||
let config = TranquilConfig::builder()
|
||||
.env()
|
||||
.load()
|
||||
.expect("load fresh config");
|
||||
let result = config.validate(true);
|
||||
let leaked_legacy = result
|
||||
.as_ref()
|
||||
.err()
|
||||
.map(|e| e.errors.iter().any(|s| s.contains("SENDMAIL_PATH")))
|
||||
.unwrap_or(false);
|
||||
assert!(
|
||||
!leaked_legacy,
|
||||
"validate spuriously flagged SENDMAIL_PATH when unset: {:?}",
|
||||
result
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn email_address_predicate_accepts_typical_addresses() {
|
||||
assert!(looks_like_email_address("alice@nel.pet"));
|
||||
assert!(looks_like_email_address("a.b+tag@example.co.uk"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn email_address_predicate_rejects_malformed() {
|
||||
assert!(!looks_like_email_address(""));
|
||||
assert!(!looks_like_email_address("no-at-sign"));
|
||||
assert!(!looks_like_email_address("@nel.pet"));
|
||||
assert!(!looks_like_email_address("alice@"));
|
||||
assert!(!looks_like_email_address("alice@nel"));
|
||||
assert!(!looks_like_email_address("a@b@c.com"));
|
||||
assert!(!looks_like_email_address("alice @nel.pet"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dkim_selector_predicate_matches_subdomain_syntax() {
|
||||
assert!(is_valid_dkim_selector("default"));
|
||||
assert!(is_valid_dkim_selector("s2024-q1"));
|
||||
assert!(is_valid_dkim_selector("mailo-2024.nel.pet"));
|
||||
assert!(!is_valid_dkim_selector(""));
|
||||
assert!(!is_valid_dkim_selector("a..b"));
|
||||
assert!(!is_valid_dkim_selector("-leading"));
|
||||
assert!(!is_valid_dkim_selector("trailing-"));
|
||||
assert!(!is_valid_dkim_selector("s_under"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn email_validate_disabled_when_from_address_unset() {
|
||||
let cfg = email_config_for_test(EmailOverrides::default());
|
||||
let mut errors = Vec::new();
|
||||
cfg.validate("test.local", &mut errors);
|
||||
assert!(errors.is_empty(), "expected no errors, got {errors:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn email_validate_rejects_bad_from_address() {
|
||||
let cfg = email_config_for_test(EmailOverrides {
|
||||
from_address: Some("not-an-email"),
|
||||
..Default::default()
|
||||
});
|
||||
let mut errors = Vec::new();
|
||||
cfg.validate("test.local", &mut errors);
|
||||
assert!(
|
||||
errors.iter().any(|e| e.contains("from_address")),
|
||||
"expected from_address error, got {errors:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn email_validate_rejects_smarthost_with_bad_credentials() {
|
||||
let cfg = email_config_for_test(EmailOverrides {
|
||||
from_address: Some("alice@nel.pet"),
|
||||
smarthost_host: Some("smtp.nel.pet"),
|
||||
smarthost_username: Some(""),
|
||||
smarthost_password: Some("hunter2"),
|
||||
..Default::default()
|
||||
});
|
||||
let mut errors = Vec::new();
|
||||
cfg.validate("test.local", &mut errors);
|
||||
assert!(
|
||||
errors.iter().any(|e| e.contains("smarthost.username")),
|
||||
"expected smarthost.username error, got {errors:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn email_validate_rejects_bad_dkim_selector() {
|
||||
let cfg = email_config_for_test(EmailOverrides {
|
||||
from_address: Some("alice@nel.pet"),
|
||||
dkim_selector: Some("-bad"),
|
||||
dkim_domain: Some("nel.pet"),
|
||||
dkim_key_path: Some("/etc/dkim.key"),
|
||||
..Default::default()
|
||||
});
|
||||
let mut errors = Vec::new();
|
||||
cfg.validate("test.local", &mut errors);
|
||||
assert!(
|
||||
errors.iter().any(|e| e.contains("dkim.selector")),
|
||||
"expected dkim.selector error, got {errors:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tls_validate_accepts_both_paths_unset() {
|
||||
let mut errors = Vec::new();
|
||||
TlsConfig {
|
||||
cert_path: None,
|
||||
key_path: None,
|
||||
}
|
||||
.validate(&mut errors);
|
||||
assert!(errors.is_empty(), "expected no errors, got {errors:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tls_validate_accepts_both_paths_set() {
|
||||
let mut errors = Vec::new();
|
||||
TlsConfig {
|
||||
cert_path: Some("/etc/tranquil/cert.pem".to_string()),
|
||||
key_path: Some("/etc/tranquil/key.pem".to_string()),
|
||||
}
|
||||
.validate(&mut errors);
|
||||
assert!(errors.is_empty(), "expected no errors, got {errors:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tls_validate_rejects_cert_without_key() {
|
||||
let mut errors = Vec::new();
|
||||
TlsConfig {
|
||||
cert_path: Some("/etc/tranquil/cert.pem".to_string()),
|
||||
key_path: None,
|
||||
}
|
||||
.validate(&mut errors);
|
||||
assert!(
|
||||
errors.iter().any(|e| e.contains("server.tls")),
|
||||
"expected server.tls error, got {errors:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct EmailOverrides {
|
||||
from_address: Option<&'static str>,
|
||||
smarthost_host: Option<&'static str>,
|
||||
smarthost_username: Option<&'static str>,
|
||||
smarthost_password: Option<&'static str>,
|
||||
dkim_selector: Option<&'static str>,
|
||||
dkim_domain: Option<&'static str>,
|
||||
dkim_key_path: Option<&'static str>,
|
||||
}
|
||||
|
||||
fn email_config_for_test(o: EmailOverrides) -> EmailConfig {
|
||||
EmailConfig {
|
||||
from_address: o.from_address.map(str::to_string),
|
||||
from_name: "Tranquil PDS".to_string(),
|
||||
helo_name: None,
|
||||
smarthost: SmarthostConfig {
|
||||
host: o.smarthost_host.map(str::to_string),
|
||||
port: 587,
|
||||
username: o.smarthost_username.map(str::to_string),
|
||||
password: o.smarthost_password.map(str::to_string),
|
||||
tls: "starttls".to_string(),
|
||||
pool_size: 4,
|
||||
command_timeout_secs: 30,
|
||||
total_timeout_secs: 60,
|
||||
},
|
||||
direct_mx: DirectMxConfig {
|
||||
command_timeout_secs: 30,
|
||||
total_timeout_secs: 60,
|
||||
max_concurrent_sends: 8,
|
||||
require_tls: false,
|
||||
},
|
||||
dkim: DkimConfig {
|
||||
selector: o.dkim_selector.map(str::to_string),
|
||||
domain: o.dkim_domain.map(str::to_string),
|
||||
private_key_path: o.dkim_key_path.map(str::to_string),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -244,6 +244,8 @@ pub trait InfraRepository: Send + Sync {
|
||||
|
||||
async fn mark_comms_failed(&self, id: Uuid, error: &str) -> Result<(), DbError>;
|
||||
|
||||
async fn mark_comms_failed_permanent(&self, id: Uuid, error: &str) -> Result<(), DbError>;
|
||||
|
||||
async fn create_invite_code(
|
||||
&self,
|
||||
code: &str,
|
||||
|
||||
@@ -319,7 +319,6 @@ pub struct ApplyCommitInput {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ApplyCommitResult {
|
||||
pub seq: i64,
|
||||
pub is_account_active: bool,
|
||||
}
|
||||
|
||||
@@ -445,19 +444,15 @@ pub trait RepoRepository: Send + Sync {
|
||||
|
||||
async fn count_user_blocks(&self, user_id: Uuid) -> Result<i64, DbError>;
|
||||
|
||||
async fn insert_commit_event(&self, data: &CommitEventData) -> Result<SequenceNumber, DbError>;
|
||||
async fn insert_commit_event(&self, data: &CommitEventData) -> Result<(), DbError>;
|
||||
|
||||
async fn insert_identity_event(
|
||||
&self,
|
||||
did: &Did,
|
||||
handle: Option<&Handle>,
|
||||
) -> Result<SequenceNumber, DbError>;
|
||||
) -> Result<(), DbError>;
|
||||
|
||||
async fn insert_account_event(
|
||||
&self,
|
||||
did: &Did,
|
||||
status: AccountStatus,
|
||||
) -> Result<SequenceNumber, DbError>;
|
||||
async fn insert_account_event(&self, did: &Did, status: AccountStatus) -> Result<(), DbError>;
|
||||
|
||||
async fn insert_sync_event(
|
||||
&self,
|
||||
@@ -465,7 +460,7 @@ pub trait RepoRepository: Send + Sync {
|
||||
commit_cid: &CidLink,
|
||||
rev: Option<&str>,
|
||||
commit_bytes: &[u8],
|
||||
) -> Result<SequenceNumber, DbError>;
|
||||
) -> Result<(), DbError>;
|
||||
|
||||
async fn insert_genesis_commit_event(
|
||||
&self,
|
||||
@@ -475,14 +470,18 @@ pub trait RepoRepository: Send + Sync {
|
||||
rev: &str,
|
||||
commit_bytes: &[u8],
|
||||
mst_root_bytes: &[u8],
|
||||
) -> Result<SequenceNumber, DbError>;
|
||||
|
||||
async fn delete_sequences_except(
|
||||
&self,
|
||||
did: &Did,
|
||||
keep_seq: SequenceNumber,
|
||||
) -> Result<(), DbError>;
|
||||
|
||||
async fn purge_did_events_keeping_latest(&self, did: &Did) -> Result<(), DbError>;
|
||||
|
||||
async fn assign_pending_sequences(&self) -> Result<u64, DbError> {
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
async fn flush_pending_sequences(&self) -> Result<(), DbError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn prune_events_older_than(&self, cutoff: DateTime<Utc>) -> Result<PruneCount, DbError>;
|
||||
|
||||
async fn get_max_seq(&self) -> Result<SequenceNumber, DbError>;
|
||||
@@ -526,8 +525,6 @@ pub trait RepoRepository: Send + Sync {
|
||||
async fn get_repo_root_cid_by_user_id(&self, user_id: Uuid)
|
||||
-> Result<Option<CidLink>, DbError>;
|
||||
|
||||
async fn notify_update(&self, seq: SequenceNumber) -> Result<(), DbError>;
|
||||
|
||||
async fn import_repo_data(
|
||||
&self,
|
||||
user_id: Uuid,
|
||||
@@ -563,5 +560,5 @@ pub trait RepoEventNotifier: Send + Sync {
|
||||
|
||||
#[async_trait]
|
||||
pub trait RepoEventReceiver: Send {
|
||||
async fn recv(&mut self) -> Option<i64>;
|
||||
async fn recv(&mut self) -> Option<()>;
|
||||
}
|
||||
|
||||
@@ -44,6 +44,7 @@ pub struct UserRow {
|
||||
pub deactivated_at: Option<DateTime<Utc>>,
|
||||
pub takedown_ref: Option<String>,
|
||||
pub is_admin: bool,
|
||||
pub inbound_migration: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -550,7 +551,7 @@ pub trait UserRepository: Send + Sync {
|
||||
limit: i64,
|
||||
) -> Result<Vec<ScheduledDeletionAccount>, DbError>;
|
||||
|
||||
async fn delete_account_with_firehose(&self, user_id: Uuid, did: &Did) -> Result<i64, DbError>;
|
||||
async fn delete_account_with_firehose(&self, user_id: Uuid, did: &Did) -> Result<(), DbError>;
|
||||
|
||||
async fn create_password_account(
|
||||
&self,
|
||||
@@ -995,6 +996,7 @@ pub struct CreatePasswordAccountInput {
|
||||
pub telegram_username: Option<String>,
|
||||
pub signal_username: Option<String>,
|
||||
pub deactivated_at: Option<DateTime<Utc>>,
|
||||
pub inbound_migration: bool,
|
||||
pub encrypted_key_bytes: Vec<u8>,
|
||||
pub encryption_version: i32,
|
||||
pub reserved_key_id: Option<Uuid>,
|
||||
|
||||
@@ -35,9 +35,9 @@ pub struct PostgresRepoEventReceiver {
|
||||
|
||||
#[async_trait]
|
||||
impl RepoEventReceiver for PostgresRepoEventReceiver {
|
||||
async fn recv(&mut self) -> Option<i64> {
|
||||
async fn recv(&mut self) -> Option<()> {
|
||||
match self.listener.recv().await {
|
||||
Ok(notification) => notification.payload().parse().ok(),
|
||||
Ok(_) => Some(()),
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,9 +65,13 @@ impl InfraRepository for PostgresInfraRepository {
|
||||
SET status = 'processing', updated_at = NOW()
|
||||
WHERE id IN (
|
||||
SELECT id FROM comms_queue
|
||||
WHERE status = 'pending'
|
||||
WHERE attempts < max_attempts
|
||||
AND scheduled_for <= $1
|
||||
AND attempts < max_attempts
|
||||
AND (
|
||||
status = 'pending'
|
||||
OR (status = 'processing'
|
||||
AND updated_at < $1 - INTERVAL '10 minutes')
|
||||
)
|
||||
ORDER BY scheduled_for ASC
|
||||
LIMIT $2
|
||||
FOR UPDATE SKIP LOCKED
|
||||
@@ -127,6 +131,24 @@ impl InfraRepository for PostgresInfraRepository {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn mark_comms_failed_permanent(&self, id: Uuid, error: &str) -> Result<(), DbError> {
|
||||
sqlx::query!(
|
||||
r#"UPDATE comms_queue
|
||||
SET status = 'failed'::comms_status,
|
||||
attempts = max_attempts,
|
||||
last_error = $2,
|
||||
updated_at = NOW()
|
||||
WHERE id = $1"#,
|
||||
id,
|
||||
error
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn create_invite_code(
|
||||
&self,
|
||||
code: &str,
|
||||
|
||||
@@ -374,7 +374,7 @@ impl OAuthRepository for PostgresOAuthRepository {
|
||||
WHERE id IN (
|
||||
SELECT id FROM oauth_token
|
||||
WHERE did = $1
|
||||
ORDER BY updated_at ASC
|
||||
ORDER BY created_at DESC
|
||||
OFFSET $2
|
||||
)
|
||||
"#,
|
||||
|
||||
@@ -111,6 +111,64 @@ fn map_sequenced_row(r: SequencedEventRow) -> Result<SequencedEvent, DbError> {
|
||||
})
|
||||
}
|
||||
|
||||
const SEQUENCER_LOCK_KEY: i64 = 0x0074_7261_6e73_6571;
|
||||
const SEQUENCER_BATCH_SIZE: i64 = 1000;
|
||||
|
||||
async fn notify_repo_pending(pool: &PgPool) {
|
||||
if let Err(e) = sqlx::query!("NOTIFY repo_updates").execute(pool).await {
|
||||
tracing::warn!(error = %e, "failed to notify repo_updates listeners");
|
||||
}
|
||||
}
|
||||
|
||||
async fn assign_one_batch(
|
||||
mut tx: sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
pool: &PgPool,
|
||||
) -> Result<i64, DbError> {
|
||||
let pending_ids: Vec<i64> = sqlx::query_scalar!(
|
||||
r#"SELECT id as "id!" FROM repo_seq WHERE seq IS NULL ORDER BY id LIMIT $1"#,
|
||||
SEQUENCER_BATCH_SIZE
|
||||
)
|
||||
.fetch_all(&mut *tx)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
|
||||
let count = pending_ids.len() as i64;
|
||||
if count == 0 {
|
||||
tx.commit().await.map_err(map_sqlx_error)?;
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
let start = sqlx::query_scalar!(r#"SELECT nextval('firehose_seq') as "seq!""#)
|
||||
.fetch_one(&mut *tx)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
if count > 1 {
|
||||
sqlx::query!("SELECT setval('firehose_seq', $1)", start + count - 1)
|
||||
.fetch_one(&mut *tx)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
}
|
||||
|
||||
let assigned = sqlx::query!(
|
||||
r#"UPDATE repo_seq r
|
||||
SET seq = $1::bigint + (o.ord - 1)
|
||||
FROM unnest($2::bigint[]) WITH ORDINALITY AS o(id, ord)
|
||||
WHERE r.id = o.id"#,
|
||||
start,
|
||||
&pending_ids
|
||||
)
|
||||
.execute(&mut *tx)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?
|
||||
.rows_affected();
|
||||
|
||||
tx.commit().await.map_err(map_sqlx_error)?;
|
||||
if assigned > 0 {
|
||||
notify_repo_pending(pool).await;
|
||||
}
|
||||
Ok(count)
|
||||
}
|
||||
|
||||
pub struct PostgresRepoRepository {
|
||||
pool: PgPool,
|
||||
}
|
||||
@@ -716,13 +774,12 @@ impl RepoRepository for PostgresRepoRepository {
|
||||
Ok(rows.into_iter().map(|(cid,)| cid).collect())
|
||||
}
|
||||
|
||||
async fn insert_commit_event(&self, data: &CommitEventData) -> Result<SequenceNumber, DbError> {
|
||||
async fn insert_commit_event(&self, data: &CommitEventData) -> Result<(), DbError> {
|
||||
let (block_cids, block_data) = inline_to_paired_blocks(data.blocks.as_deref());
|
||||
let seq = sqlx::query_scalar!(
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO repo_seq (did, event_type, commit_cid, prev_cid, ops, blobs, block_cids, block_data, prev_data_cid, rev)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
RETURNING seq
|
||||
"#,
|
||||
data.did.as_str(),
|
||||
data.event_type.as_str(),
|
||||
@@ -735,67 +792,54 @@ impl RepoRepository for PostgresRepoRepository {
|
||||
data.prev_data_cid.as_ref().map(|c| c.as_str()),
|
||||
data.rev
|
||||
)
|
||||
.fetch_one(&self.pool)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
|
||||
Ok(seq.into())
|
||||
notify_repo_pending(&self.pool).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn insert_identity_event(
|
||||
&self,
|
||||
did: &Did,
|
||||
handle: Option<&Handle>,
|
||||
) -> Result<SequenceNumber, DbError> {
|
||||
) -> Result<(), DbError> {
|
||||
let handle_str = handle.map(|h| h.as_str());
|
||||
let seq = sqlx::query_scalar!(
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO repo_seq (did, event_type, handle)
|
||||
VALUES ($1, 'identity', $2)
|
||||
RETURNING seq
|
||||
"#,
|
||||
did.as_str(),
|
||||
handle_str
|
||||
)
|
||||
.fetch_one(&self.pool)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
|
||||
sqlx::query(&format!("NOTIFY repo_updates, '{}'", seq))
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
|
||||
Ok(seq.into())
|
||||
notify_repo_pending(&self.pool).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn insert_account_event(
|
||||
&self,
|
||||
did: &Did,
|
||||
status: AccountStatus,
|
||||
) -> Result<SequenceNumber, DbError> {
|
||||
async fn insert_account_event(&self, did: &Did, status: AccountStatus) -> Result<(), DbError> {
|
||||
let active = status.is_active();
|
||||
let status_str = status.for_firehose();
|
||||
let seq = sqlx::query_scalar!(
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO repo_seq (did, event_type, active, status)
|
||||
VALUES ($1, 'account', $2, $3)
|
||||
RETURNING seq
|
||||
"#,
|
||||
did.as_str(),
|
||||
active,
|
||||
status_str
|
||||
)
|
||||
.fetch_one(&self.pool)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
|
||||
sqlx::query(&format!("NOTIFY repo_updates, '{}'", seq))
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
|
||||
Ok(seq.into())
|
||||
notify_repo_pending(&self.pool).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn insert_sync_event(
|
||||
@@ -804,18 +848,17 @@ impl RepoRepository for PostgresRepoRepository {
|
||||
commit_cid: &CidLink,
|
||||
rev: Option<&str>,
|
||||
commit_bytes: &[u8],
|
||||
) -> Result<SequenceNumber, DbError> {
|
||||
) -> Result<(), DbError> {
|
||||
let cid_bytes = commit_cid
|
||||
.to_cid()
|
||||
.map(|c| c.to_bytes())
|
||||
.unwrap_or_default();
|
||||
let block_cids: Vec<Vec<u8>> = vec![cid_bytes];
|
||||
let block_data: Vec<Vec<u8>> = vec![commit_bytes.to_vec()];
|
||||
let seq = sqlx::query_scalar!(
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO repo_seq (did, event_type, commit_cid, rev, block_cids, block_data)
|
||||
VALUES ($1, 'sync', $2, $3, $4, $5)
|
||||
RETURNING seq
|
||||
"#,
|
||||
did.as_str(),
|
||||
commit_cid.as_str(),
|
||||
@@ -823,16 +866,12 @@ impl RepoRepository for PostgresRepoRepository {
|
||||
&block_cids as &[Vec<u8>],
|
||||
&block_data as &[Vec<u8>]
|
||||
)
|
||||
.fetch_one(&self.pool)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
|
||||
sqlx::query(&format!("NOTIFY repo_updates, '{}'", seq))
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
|
||||
Ok(seq.into())
|
||||
notify_repo_pending(&self.pool).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn insert_genesis_commit_event(
|
||||
@@ -843,7 +882,7 @@ impl RepoRepository for PostgresRepoRepository {
|
||||
rev: &str,
|
||||
commit_bytes: &[u8],
|
||||
mst_root_bytes: &[u8],
|
||||
) -> Result<SequenceNumber, DbError> {
|
||||
) -> Result<(), DbError> {
|
||||
let ops = serde_json::json!([]);
|
||||
let blobs: Vec<String> = vec![];
|
||||
let commit_cid_bytes = commit_cid
|
||||
@@ -858,11 +897,10 @@ impl RepoRepository for PostgresRepoRepository {
|
||||
let block_data: Vec<Vec<u8>> = vec![commit_bytes.to_vec(), mst_root_bytes.to_vec()];
|
||||
let prev_cid: Option<&str> = None;
|
||||
|
||||
let seq = sqlx::query_scalar!(
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO repo_seq (did, event_type, commit_cid, prev_cid, ops, blobs, block_cids, block_data, rev)
|
||||
VALUES ($1, 'commit', $2, $3::TEXT, $4, $5, $6, $7, $8)
|
||||
RETURNING seq
|
||||
"#,
|
||||
did.as_str(),
|
||||
commit_cid.as_str(),
|
||||
@@ -873,27 +911,22 @@ impl RepoRepository for PostgresRepoRepository {
|
||||
&block_data as &[Vec<u8>],
|
||||
rev
|
||||
)
|
||||
.fetch_one(&self.pool)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
|
||||
sqlx::query(&format!("NOTIFY repo_updates, '{}'", seq))
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
|
||||
Ok(seq.into())
|
||||
notify_repo_pending(&self.pool).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_sequences_except(
|
||||
&self,
|
||||
did: &Did,
|
||||
keep_seq: SequenceNumber,
|
||||
) -> Result<(), DbError> {
|
||||
async fn purge_did_events_keeping_latest(&self, did: &Did) -> Result<(), DbError> {
|
||||
sqlx::query!(
|
||||
"DELETE FROM repo_seq WHERE did = $1 AND seq != $2",
|
||||
did.as_str(),
|
||||
keep_seq.as_i64()
|
||||
r#"
|
||||
DELETE FROM repo_seq
|
||||
WHERE did = $1
|
||||
AND id <> (SELECT id FROM repo_seq WHERE did = $1 ORDER BY id DESC LIMIT 1)
|
||||
"#,
|
||||
did.as_str()
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
@@ -902,6 +935,42 @@ impl RepoRepository for PostgresRepoRepository {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn assign_pending_sequences(&self) -> Result<u64, DbError> {
|
||||
let mut total: u64 = 0;
|
||||
loop {
|
||||
let mut tx = self.pool.begin().await.map_err(map_sqlx_error)?;
|
||||
let acquired = sqlx::query_scalar!(
|
||||
r#"SELECT pg_try_advisory_xact_lock($1) as "locked!""#,
|
||||
SEQUENCER_LOCK_KEY
|
||||
)
|
||||
.fetch_one(&mut *tx)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
if !acquired {
|
||||
return Ok(total);
|
||||
}
|
||||
let count = assign_one_batch(tx, &self.pool).await?;
|
||||
total += count as u64;
|
||||
if count < SEQUENCER_BATCH_SIZE {
|
||||
return Ok(total);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn flush_pending_sequences(&self) -> Result<(), DbError> {
|
||||
loop {
|
||||
let mut tx = self.pool.begin().await.map_err(map_sqlx_error)?;
|
||||
sqlx::query!("SELECT pg_advisory_xact_lock($1)", SEQUENCER_LOCK_KEY)
|
||||
.execute(&mut *tx)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
let count = assign_one_batch(tx, &self.pool).await?;
|
||||
if count < SEQUENCER_BATCH_SIZE {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn prune_events_older_than(&self, cutoff: DateTime<Utc>) -> Result<PruneCount, DbError> {
|
||||
let result = sqlx::query!("DELETE FROM repo_seq WHERE created_at < $1", cutoff)
|
||||
.execute(&self.pool)
|
||||
@@ -965,7 +1034,7 @@ impl RepoRepository for PostgresRepoRepository {
|
||||
Some(lim) => {
|
||||
let rows = sqlx::query_as!(
|
||||
SequencedEventRow,
|
||||
r#"SELECT seq, did, created_at, event_type as "event_type: RepoEventType", commit_cid, prev_cid, prev_data_cid,
|
||||
r#"SELECT seq as "seq!", did, created_at, event_type as "event_type: RepoEventType", commit_cid, prev_cid, prev_data_cid,
|
||||
ops, blobs, block_cids, block_data, blocks_cids, handle, active, status, rev
|
||||
FROM repo_seq
|
||||
WHERE seq > $1
|
||||
@@ -982,7 +1051,7 @@ impl RepoRepository for PostgresRepoRepository {
|
||||
None => {
|
||||
let rows = sqlx::query_as!(
|
||||
SequencedEventRow,
|
||||
r#"SELECT seq, did, created_at, event_type as "event_type: RepoEventType", commit_cid, prev_cid, prev_data_cid,
|
||||
r#"SELECT seq as "seq!", did, created_at, event_type as "event_type: RepoEventType", commit_cid, prev_cid, prev_data_cid,
|
||||
ops, blobs, block_cids, block_data, blocks_cids, handle, active, status, rev
|
||||
FROM repo_seq
|
||||
WHERE seq > $1
|
||||
@@ -1004,7 +1073,7 @@ impl RepoRepository for PostgresRepoRepository {
|
||||
) -> Result<Vec<SequencedEvent>, DbError> {
|
||||
let rows = sqlx::query_as!(
|
||||
SequencedEventRow,
|
||||
r#"SELECT seq, did, created_at, event_type as "event_type: RepoEventType", commit_cid, prev_cid, prev_data_cid,
|
||||
r#"SELECT seq as "seq!", did, created_at, event_type as "event_type: RepoEventType", commit_cid, prev_cid, prev_data_cid,
|
||||
ops, blobs, block_cids, block_data, blocks_cids, handle, active, status, rev
|
||||
FROM repo_seq
|
||||
WHERE seq > $1 AND seq < $2
|
||||
@@ -1024,7 +1093,7 @@ impl RepoRepository for PostgresRepoRepository {
|
||||
) -> Result<Option<SequencedEvent>, DbError> {
|
||||
let row = sqlx::query_as!(
|
||||
SequencedEventRow,
|
||||
r#"SELECT seq, did, created_at, event_type as "event_type: RepoEventType", commit_cid, prev_cid, prev_data_cid,
|
||||
r#"SELECT seq as "seq!", did, created_at, event_type as "event_type: RepoEventType", commit_cid, prev_cid, prev_data_cid,
|
||||
ops, blobs, block_cids, block_data, blocks_cids, handle, active, status, rev
|
||||
FROM repo_seq
|
||||
WHERE seq = $1"#,
|
||||
@@ -1043,7 +1112,7 @@ impl RepoRepository for PostgresRepoRepository {
|
||||
) -> Result<Vec<SequencedEvent>, DbError> {
|
||||
let rows = sqlx::query_as!(
|
||||
SequencedEventRow,
|
||||
r#"SELECT seq, did, created_at, event_type as "event_type: RepoEventType", commit_cid, prev_cid, prev_data_cid,
|
||||
r#"SELECT seq as "seq!", did, created_at, event_type as "event_type: RepoEventType", commit_cid, prev_cid, prev_data_cid,
|
||||
ops, blobs, block_cids, block_data, blocks_cids, handle, active, status, rev
|
||||
FROM repo_seq
|
||||
WHERE seq > $1
|
||||
@@ -1104,14 +1173,6 @@ impl RepoRepository for PostgresRepoRepository {
|
||||
Ok(cid.map(CidLink::from))
|
||||
}
|
||||
|
||||
async fn notify_update(&self, seq: SequenceNumber) -> Result<(), DbError> {
|
||||
sqlx::query(&format!("NOTIFY repo_updates, '{}'", seq.as_i64()))
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn import_repo_data(
|
||||
&self,
|
||||
user_id: Uuid,
|
||||
@@ -1402,28 +1463,27 @@ impl RepoRepository for PostgresRepoRepository {
|
||||
|
||||
let event = input.commit_event;
|
||||
let (event_block_cids, event_block_data) = inline_into_paired_blocks(event.blocks);
|
||||
let seq: i64 = sqlx::query_scalar(
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO repo_seq (did, event_type, commit_cid, prev_cid, ops, blobs, block_cids, block_data, prev_data_cid, rev)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
RETURNING seq
|
||||
"#,
|
||||
event.did.as_str(),
|
||||
event.event_type.as_str(),
|
||||
event.commit_cid.as_ref().map(|c| c.as_str()),
|
||||
event.prev_cid.as_ref().map(|c| c.as_str()),
|
||||
event.ops,
|
||||
event.blobs.as_deref(),
|
||||
&event_block_cids as &[Vec<u8>],
|
||||
&event_block_data as &[Vec<u8>],
|
||||
event.prev_data_cid.as_ref().map(|c| c.as_str()),
|
||||
event.rev
|
||||
)
|
||||
.bind(event.did.as_str())
|
||||
.bind(event.event_type.as_str())
|
||||
.bind(event.commit_cid.as_ref().map(|c| c.as_str()))
|
||||
.bind(event.prev_cid.as_ref().map(|c| c.as_str()))
|
||||
.bind(&event.ops)
|
||||
.bind(&event.blobs)
|
||||
.bind(&event_block_cids)
|
||||
.bind(&event_block_data)
|
||||
.bind(event.prev_data_cid.as_ref().map(|c| c.as_str()))
|
||||
.bind(&event.rev)
|
||||
.fetch_one(&mut *tx)
|
||||
.execute(&mut *tx)
|
||||
.await
|
||||
.map_err(|e| ApplyCommitError::Database(e.to_string()))?;
|
||||
|
||||
sqlx::query(&format!("NOTIFY repo_updates, '{}'", seq))
|
||||
sqlx::query!("NOTIFY repo_updates")
|
||||
.execute(&mut *tx)
|
||||
.await
|
||||
.map_err(|e| ApplyCommitError::Database(e.to_string()))?;
|
||||
@@ -1432,10 +1492,7 @@ impl RepoRepository for PostgresRepoRepository {
|
||||
.await
|
||||
.map_err(|e| ApplyCommitError::Database(e.to_string()))?;
|
||||
|
||||
Ok(tranquil_db_traits::ApplyCommitResult {
|
||||
seq,
|
||||
is_account_active,
|
||||
})
|
||||
Ok(tranquil_db_traits::ApplyCommitResult { is_account_active })
|
||||
}
|
||||
|
||||
async fn get_users_without_blocks(&self) -> Result<Vec<UserWithoutBlocks>, DbError> {
|
||||
|
||||
@@ -47,7 +47,7 @@ pub(crate) fn map_sqlx_error(e: sqlx::Error) -> DbError {
|
||||
impl UserRepository for PostgresUserRepository {
|
||||
async fn get_by_did(&self, did: &Did) -> Result<Option<UserRow>, DbError> {
|
||||
let row = sqlx::query!(
|
||||
r#"SELECT id, did, handle, email, created_at, deactivated_at, takedown_ref, is_admin
|
||||
r#"SELECT id, did, handle, email, created_at, deactivated_at, takedown_ref, is_admin, inbound_migration
|
||||
FROM users WHERE did = $1"#,
|
||||
did.as_str()
|
||||
)
|
||||
@@ -64,12 +64,13 @@ impl UserRepository for PostgresUserRepository {
|
||||
deactivated_at: r.deactivated_at,
|
||||
takedown_ref: r.takedown_ref,
|
||||
is_admin: r.is_admin,
|
||||
inbound_migration: r.inbound_migration,
|
||||
}))
|
||||
}
|
||||
|
||||
async fn get_by_handle(&self, handle: &Handle) -> Result<Option<UserRow>, DbError> {
|
||||
let row = sqlx::query!(
|
||||
r#"SELECT id, did, handle, email, created_at, deactivated_at, takedown_ref, is_admin
|
||||
r#"SELECT id, did, handle, email, created_at, deactivated_at, takedown_ref, is_admin, inbound_migration
|
||||
FROM users WHERE handle = $1"#,
|
||||
handle.as_str()
|
||||
)
|
||||
@@ -86,6 +87,7 @@ impl UserRepository for PostgresUserRepository {
|
||||
deactivated_at: r.deactivated_at,
|
||||
takedown_ref: r.takedown_ref,
|
||||
is_admin: r.is_admin,
|
||||
inbound_migration: r.inbound_migration,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -1863,7 +1865,7 @@ impl UserRepository for PostgresUserRepository {
|
||||
|
||||
async fn activate_account(&self, did: &Did) -> Result<bool, DbError> {
|
||||
let result = sqlx::query!(
|
||||
"UPDATE users SET deactivated_at = NULL WHERE did = $1",
|
||||
"UPDATE users SET deactivated_at = NULL, inbound_migration = FALSE WHERE did = $1",
|
||||
did.as_str()
|
||||
)
|
||||
.execute(&self.pool)
|
||||
@@ -2289,7 +2291,7 @@ impl UserRepository for PostgresUserRepository {
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn delete_account_with_firehose(&self, user_id: Uuid, did: &Did) -> Result<i64, DbError> {
|
||||
async fn delete_account_with_firehose(&self, user_id: Uuid, did: &Did) -> Result<(), DbError> {
|
||||
let mut tx = self.pool.begin().await.map_err(map_sqlx_error)?;
|
||||
|
||||
sqlx::query!("DELETE FROM blobs WHERE created_by_user = $1", user_id)
|
||||
@@ -2368,11 +2370,11 @@ impl UserRepository for PostgresUserRepository {
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
|
||||
let account_seq: i64 = sqlx::query_scalar!(
|
||||
let event_id: i64 = sqlx::query_scalar!(
|
||||
r#"
|
||||
INSERT INTO repo_seq (did, event_type, active, status)
|
||||
VALUES ($1, 'account', false, 'deleted')
|
||||
RETURNING seq
|
||||
RETURNING id
|
||||
"#,
|
||||
did.as_str()
|
||||
)
|
||||
@@ -2381,9 +2383,9 @@ impl UserRepository for PostgresUserRepository {
|
||||
.map_err(map_sqlx_error)?;
|
||||
|
||||
sqlx::query!(
|
||||
"DELETE FROM repo_seq WHERE did = $1 AND seq != $2",
|
||||
"DELETE FROM repo_seq WHERE did = $1 AND id <> $2",
|
||||
did.as_str(),
|
||||
account_seq
|
||||
event_id
|
||||
)
|
||||
.execute(&mut *tx)
|
||||
.await
|
||||
@@ -2391,12 +2393,12 @@ impl UserRepository for PostgresUserRepository {
|
||||
|
||||
tx.commit().await.map_err(map_sqlx_error)?;
|
||||
|
||||
sqlx::query(&format!("NOTIFY repo_updates, '{}'", account_seq))
|
||||
sqlx::query!("NOTIFY repo_updates")
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
|
||||
Ok(account_seq)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn create_password_account(
|
||||
@@ -2426,8 +2428,8 @@ impl UserRepository for PostgresUserRepository {
|
||||
handle, email, did, password_hash,
|
||||
preferred_comms_channel,
|
||||
discord_username, telegram_username, signal_username,
|
||||
is_admin, deactivated_at, email_verified
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, FALSE) RETURNING id"#,
|
||||
is_admin, deactivated_at, inbound_migration, email_verified
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, FALSE) RETURNING id"#,
|
||||
)
|
||||
.bind(input.handle.as_str())
|
||||
.bind(&input.email)
|
||||
@@ -2439,6 +2441,7 @@ impl UserRepository for PostgresUserRepository {
|
||||
.bind(&input.signal_username)
|
||||
.bind(is_first_user)
|
||||
.bind(input.deactivated_at)
|
||||
.bind(input.inbound_migration)
|
||||
.fetch_one(&mut *tx)
|
||||
.await;
|
||||
|
||||
|
||||
@@ -310,6 +310,7 @@ pub async fn authorize_post(
|
||||
State(state): State<AppState>,
|
||||
_rate_limit: OAuthRateLimited<OAuthAuthorizeLimit>,
|
||||
headers: HeaderMap,
|
||||
client_ip: ClientIp,
|
||||
Json(form): Json<AuthorizeSubmit>,
|
||||
) -> Response {
|
||||
let json_response = wants_json(&headers);
|
||||
@@ -488,8 +489,7 @@ pub async fn authorize_post(
|
||||
if !password_valid {
|
||||
return show_login_error("Invalid identifier or password.", json_response);
|
||||
}
|
||||
let is_verified = user.channel_verification.has_any_verified();
|
||||
if !is_verified {
|
||||
if tranquil_api::server::verification_blocks_login(&user.channel_verification) {
|
||||
let resend_info = tranquil_api::server::auto_resend_verification(&state, &user.did).await;
|
||||
let handle = resend_info
|
||||
.as_ref()
|
||||
@@ -617,7 +617,7 @@ pub async fn authorize_post(
|
||||
let device_data = DeviceData {
|
||||
session_id: SessionId::generate(),
|
||||
user_agent: extract_user_agent(&headers),
|
||||
ip_address: extract_client_ip(&headers, None),
|
||||
ip_address: client_ip.into_string(),
|
||||
last_seen_at: Utc::now(),
|
||||
};
|
||||
if state
|
||||
@@ -854,8 +854,7 @@ pub async fn authorize_select(
|
||||
);
|
||||
}
|
||||
};
|
||||
let is_verified = user.channel_verification.has_any_verified();
|
||||
if !is_verified {
|
||||
if tranquil_api::server::verification_blocks_login(&user.channel_verification) {
|
||||
let resend_info = tranquil_api::server::auto_resend_verification(&state, &did).await;
|
||||
return (
|
||||
StatusCode::FORBIDDEN,
|
||||
|
||||
@@ -23,7 +23,7 @@ use tranquil_pds::rate_limit::{
|
||||
};
|
||||
use tranquil_pds::state::AppState;
|
||||
use tranquil_pds::types::{Did, Handle, PlainPassword};
|
||||
use tranquil_pds::util::extract_client_ip;
|
||||
use tranquil_pds::util::ClientIp;
|
||||
use tranquil_types::{AuthorizationCode, ClientId, DeviceId as DeviceIdType, RequestId};
|
||||
use urlencoding::encode as url_encode;
|
||||
|
||||
|
||||
@@ -289,9 +289,7 @@ async fn passkey_start_named(
|
||||
.into_response();
|
||||
}
|
||||
|
||||
let is_verified = user.channel_verification.has_any_verified();
|
||||
|
||||
if !is_verified {
|
||||
if tranquil_api::server::verification_blocks_login(&user.channel_verification) {
|
||||
let resend_info = tranquil_api::server::auto_resend_verification(&state, &user.did).await;
|
||||
return (
|
||||
StatusCode::FORBIDDEN,
|
||||
|
||||
@@ -160,8 +160,10 @@ pub async fn register_complete(
|
||||
.into_response();
|
||||
}
|
||||
|
||||
let is_verified = match state.repos.user.get_session_info_by_did(&did).await {
|
||||
Ok(Some(info)) => info.channel_verification.has_any_verified(),
|
||||
let login_blocked = match state.repos.user.get_session_info_by_did(&did).await {
|
||||
Ok(Some(info)) => {
|
||||
tranquil_api::server::verification_blocks_login(&info.channel_verification)
|
||||
}
|
||||
Ok(None) => {
|
||||
return (
|
||||
StatusCode::FORBIDDEN,
|
||||
@@ -189,7 +191,7 @@ pub async fn register_complete(
|
||||
}
|
||||
};
|
||||
|
||||
if !is_verified {
|
||||
if login_blocked {
|
||||
let resend_info = tranquil_api::server::auto_resend_verification(&state, &did).await;
|
||||
return (
|
||||
StatusCode::FORBIDDEN,
|
||||
@@ -300,6 +302,7 @@ pub async fn register_complete(
|
||||
pub async fn establish_session(
|
||||
State(state): State<AppState>,
|
||||
headers: HeaderMap,
|
||||
client_ip: ClientIp,
|
||||
auth: tranquil_pds::auth::Auth<tranquil_pds::auth::Active>,
|
||||
) -> Response {
|
||||
let did = &auth.did;
|
||||
@@ -317,7 +320,7 @@ pub async fn establish_session(
|
||||
let device_data = DeviceData {
|
||||
session_id: SessionId::generate(),
|
||||
user_agent: extract_user_agent(&headers),
|
||||
ip_address: extract_client_ip(&headers, None),
|
||||
ip_address: client_ip.into_string(),
|
||||
last_seen_at: Utc::now(),
|
||||
};
|
||||
|
||||
|
||||
@@ -75,6 +75,7 @@ pub async fn authorize_2fa_post(
|
||||
State(state): State<AppState>,
|
||||
_rate_limit: OAuthRateLimited<OAuthAuthorizeLimit>,
|
||||
headers: HeaderMap,
|
||||
client_ip: ClientIp,
|
||||
Json(form): Json<Authorize2faSubmit>,
|
||||
) -> Response {
|
||||
let json_error = |status: StatusCode, error: &str, description: &str| -> Response {
|
||||
@@ -251,7 +252,7 @@ pub async fn authorize_2fa_post(
|
||||
let device_data = DeviceData {
|
||||
session_id: SessionId::generate(),
|
||||
user_agent: extract_user_agent(&headers),
|
||||
ip_address: extract_client_ip(&headers, None),
|
||||
ip_address: client_ip.into_string(),
|
||||
last_seen_at: Utc::now(),
|
||||
};
|
||||
if state
|
||||
|
||||
@@ -12,7 +12,7 @@ use tranquil_pds::oauth::client::{build_client_metadata, delegation_oauth_urls};
|
||||
use tranquil_pds::rate_limit::{LoginLimit, OAuthRateLimited, TotpVerifyLimit};
|
||||
use tranquil_pds::state::AppState;
|
||||
use tranquil_pds::types::PlainPassword;
|
||||
use tranquil_pds::util::extract_client_ip;
|
||||
use tranquil_pds::util::ClientIp;
|
||||
use tranquil_types::did_doc::{extract_handle, extract_pds_endpoint};
|
||||
use tranquil_types::{Did, RequestId};
|
||||
|
||||
@@ -402,6 +402,7 @@ pub struct DelegationTokenAuthSubmit {
|
||||
pub async fn delegation_auth_token(
|
||||
State(state): State<AppState>,
|
||||
headers: HeaderMap,
|
||||
client_ip: ClientIp,
|
||||
auth: Auth<Active>,
|
||||
Json(form): Json<DelegationTokenAuthSubmit>,
|
||||
) -> Response {
|
||||
@@ -428,7 +429,7 @@ pub async fn delegation_auth_token(
|
||||
return resp;
|
||||
}
|
||||
|
||||
let ip = extract_client_ip(&headers, None);
|
||||
let ip = client_ip.into_string();
|
||||
let user_agent = tranquil_pds::util::extract_user_agent(&headers);
|
||||
|
||||
finalize_delegation_auth(
|
||||
|
||||
@@ -402,13 +402,15 @@ async fn handle_sso_login(
|
||||
}
|
||||
};
|
||||
|
||||
let is_verified = match state
|
||||
let login_blocked = match state
|
||||
.repos
|
||||
.user
|
||||
.get_session_info_by_did(&identity.did)
|
||||
.await
|
||||
{
|
||||
Ok(Some(info)) => info.channel_verification.has_any_verified(),
|
||||
Ok(Some(info)) => {
|
||||
tranquil_api::server::verification_blocks_login(&info.channel_verification)
|
||||
}
|
||||
Ok(None) => {
|
||||
tracing::error!("User not found for SSO login: {}", identity.did);
|
||||
return redirect_to_error("Account not found");
|
||||
@@ -419,7 +421,7 @@ async fn handle_sso_login(
|
||||
}
|
||||
};
|
||||
|
||||
if !is_verified {
|
||||
if login_blocked {
|
||||
tracing::warn!(
|
||||
did = %identity.did,
|
||||
provider = %provider.as_str(),
|
||||
|
||||
@@ -23,3 +23,6 @@ sha2 = { workspace = true }
|
||||
tokio = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
uuid = { workspace = true }
|
||||
|
||||
[features]
|
||||
native-tls-roots = []
|
||||
|
||||
@@ -78,18 +78,21 @@ impl ClientMetadataCache {
|
||||
Self {
|
||||
cache: Arc::new(RwLock::new(HashMap::new())),
|
||||
jwks_cache: Arc::new(RwLock::new(HashMap::new())),
|
||||
http_client: Client::builder()
|
||||
.timeout(std::time::Duration::from_secs(30))
|
||||
.connect_timeout(std::time::Duration::from_secs(10))
|
||||
.pool_max_idle_per_host(10)
|
||||
.pool_idle_timeout(std::time::Duration::from_secs(90))
|
||||
.user_agent(concat!(
|
||||
"Tranquil-PDS/",
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
" (ATProto; +https://tangled.org/tranquil.farm/tranquil-pds)"
|
||||
))
|
||||
.build()
|
||||
.unwrap_or_else(|_| Client::new()),
|
||||
http_client: {
|
||||
let builder = Client::builder()
|
||||
.timeout(std::time::Duration::from_secs(30))
|
||||
.connect_timeout(std::time::Duration::from_secs(10))
|
||||
.pool_max_idle_per_host(10)
|
||||
.pool_idle_timeout(std::time::Duration::from_secs(90))
|
||||
.user_agent(concat!(
|
||||
"Tranquil-PDS/",
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
" (ATProto; +https://tangled.org/tranquil.farm/tranquil-pds)"
|
||||
));
|
||||
#[cfg(feature = "native-tls-roots")]
|
||||
let builder = builder.danger_accept_invalid_certs(true);
|
||||
builder.build().unwrap_or_else(|_| Client::new())
|
||||
},
|
||||
cache_ttl_secs,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,6 +55,7 @@ metrics-exporter-prometheus = { workspace = true }
|
||||
multibase = { workspace = true }
|
||||
multihash = { workspace = true }
|
||||
p256 = { workspace = true }
|
||||
parking_lot = { workspace = true }
|
||||
rand = { workspace = true }
|
||||
redis = { workspace = true, optional = true }
|
||||
regex = { workspace = true }
|
||||
@@ -90,6 +91,7 @@ s3-storage = ["tranquil-storage/s3", "dep:aws-config", "dep:aws-sdk-s3"]
|
||||
s3 = ["s3-storage"]
|
||||
valkey = ["tranquil-cache/valkey", "dep:redis"]
|
||||
frontend = []
|
||||
native-tls-roots = ["tranquil-oauth/native-tls-roots"]
|
||||
|
||||
[dev-dependencies]
|
||||
ciborium = { workspace = true }
|
||||
|
||||
@@ -21,6 +21,8 @@ pub enum ApiError {
|
||||
InvalidToken(Option<String>),
|
||||
ExpiredToken(Option<String>),
|
||||
OAuthExpiredToken(Option<String>),
|
||||
UseDpopNonce(String),
|
||||
InvalidDpopProof(String),
|
||||
TokenRequired,
|
||||
AccountDeactivated,
|
||||
AccountTakedown,
|
||||
@@ -137,6 +139,8 @@ impl ApiError {
|
||||
| Self::InvalidToken(_)
|
||||
| Self::PasskeyCounterAnomaly
|
||||
| Self::OAuthExpiredToken(_)
|
||||
| Self::UseDpopNonce(_)
|
||||
| Self::InvalidDpopProof(_)
|
||||
| Self::ReauthRequired { .. } => StatusCode::UNAUTHORIZED,
|
||||
Self::InvalidCode(_) => StatusCode::BAD_REQUEST,
|
||||
Self::ExpiredToken(_) => StatusCode::BAD_REQUEST,
|
||||
@@ -236,6 +240,8 @@ impl ApiError {
|
||||
Self::AuthenticationFailed(_) => Cow::Borrowed("AuthenticationFailed"),
|
||||
Self::InvalidToken(_) => Cow::Borrowed("InvalidToken"),
|
||||
Self::ExpiredToken(_) | Self::OAuthExpiredToken(_) => Cow::Borrowed("ExpiredToken"),
|
||||
Self::UseDpopNonce(_) => Cow::Borrowed("use_dpop_nonce"),
|
||||
Self::InvalidDpopProof(_) => Cow::Borrowed("invalid_dpop_proof"),
|
||||
Self::TokenRequired => Cow::Borrowed("TokenRequired"),
|
||||
Self::AccountDeactivated => Cow::Borrowed("AccountDeactivated"),
|
||||
Self::AccountTakedown => Cow::Borrowed("AccountTakedown"),
|
||||
@@ -335,6 +341,8 @@ impl ApiError {
|
||||
Self::ExpiredToken(msg) | Self::OAuthExpiredToken(msg) => {
|
||||
msg.clone().unwrap_or_else(|| "Token has expired".into())
|
||||
}
|
||||
Self::UseDpopNonce(_) => "DPoP nonce required".into(),
|
||||
Self::InvalidDpopProof(msg) => msg.clone(),
|
||||
Self::RepoNotFound(msg) => msg
|
||||
.clone()
|
||||
.unwrap_or_else(|| "Repository not found".into()),
|
||||
@@ -560,6 +568,36 @@ impl IntoResponse for ApiError {
|
||||
),
|
||||
);
|
||||
}
|
||||
Self::UseDpopNonce(nonce) => {
|
||||
match HeaderValue::from_str(nonce) {
|
||||
Ok(val) => {
|
||||
response
|
||||
.headers_mut()
|
||||
.insert(crate::util::HEADER_DPOP_NONCE, val);
|
||||
}
|
||||
Err(err) => {
|
||||
tracing::error!(
|
||||
?err,
|
||||
nonce_len = nonce.len(),
|
||||
"generated DPoP nonce is not a valid header value"
|
||||
);
|
||||
}
|
||||
}
|
||||
response.headers_mut().insert(
|
||||
http::header::WWW_AUTHENTICATE,
|
||||
HeaderValue::from_static(
|
||||
"DPoP error=\"use_dpop_nonce\", error_description=\"Resource server requires nonce in DPoP proof\"",
|
||||
),
|
||||
);
|
||||
}
|
||||
Self::InvalidDpopProof(_) => {
|
||||
response.headers_mut().insert(
|
||||
http::header::WWW_AUTHENTICATE,
|
||||
HeaderValue::from_static(
|
||||
"DPoP error=\"invalid_dpop_proof\", error_description=\"Invalid DPoP proof\"",
|
||||
),
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
response
|
||||
@@ -596,6 +634,8 @@ impl From<crate::auth::TokenValidationError> for ApiError {
|
||||
crate::auth::TokenValidationError::InvalidToken => {
|
||||
Self::AuthenticationFailed(Some("Invalid token format".to_string()))
|
||||
}
|
||||
crate::auth::TokenValidationError::UseDpopNonce(nonce) => Self::UseDpopNonce(nonce),
|
||||
crate::auth::TokenValidationError::InvalidDpopProof(msg) => Self::InvalidDpopProof(msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -625,10 +665,8 @@ impl From<crate::auth::extractor::AuthError> for ApiError {
|
||||
crate::auth::extractor::AuthError::OAuthExpiredToken(msg) => {
|
||||
Self::OAuthExpiredToken(Some(msg))
|
||||
}
|
||||
crate::auth::extractor::AuthError::UseDpopNonce(_)
|
||||
| crate::auth::extractor::AuthError::InvalidDpopProof(_) => {
|
||||
Self::AuthenticationFailed(None)
|
||||
}
|
||||
crate::auth::extractor::AuthError::UseDpopNonce(nonce) => Self::UseDpopNonce(nonce),
|
||||
crate::auth::extractor::AuthError::InvalidDpopProof(msg) => Self::InvalidDpopProof(msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,13 @@ pub async fn require_verified_or_delegated<'a>(
|
||||
state: &AppState,
|
||||
user: &'a AuthenticatedUser,
|
||||
) -> Result<AccountVerified<'a>, ApiError> {
|
||||
if tranquil_config::get()
|
||||
.server
|
||||
.disable_account_verification_gate
|
||||
{
|
||||
return Ok(AccountVerified { user });
|
||||
}
|
||||
|
||||
let is_verified = state
|
||||
.repos
|
||||
.user
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::marker::PhantomData;
|
||||
|
||||
use axum::{
|
||||
extract::{FromRequestParts, OptionalFromRequestParts, OriginalUri},
|
||||
http::{StatusCode, header::AUTHORIZATION, request::Parts},
|
||||
http::{header::AUTHORIZATION, request::Parts},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use tracing::{debug, error, info};
|
||||
@@ -35,32 +35,7 @@ pub enum AuthError {
|
||||
|
||||
impl IntoResponse for AuthError {
|
||||
fn into_response(self) -> Response {
|
||||
match self {
|
||||
Self::UseDpopNonce(nonce) => (
|
||||
StatusCode::UNAUTHORIZED,
|
||||
[
|
||||
("DPoP-Nonce", nonce.as_str()),
|
||||
("WWW-Authenticate", "DPoP error=\"use_dpop_nonce\""),
|
||||
],
|
||||
axum::Json(serde_json::json!({
|
||||
"error": "use_dpop_nonce",
|
||||
"message": "DPoP nonce required"
|
||||
})),
|
||||
)
|
||||
.into_response(),
|
||||
Self::OAuthExpiredToken(msg) => ApiError::OAuthExpiredToken(Some(msg)).into_response(),
|
||||
Self::InvalidDpopProof(msg) => (
|
||||
StatusCode::UNAUTHORIZED,
|
||||
[("WWW-Authenticate", "DPoP error=\"invalid_dpop_proof\"")],
|
||||
axum::Json(serde_json::json!({
|
||||
"error": "invalid_dpop_proof",
|
||||
"message": msg
|
||||
})),
|
||||
)
|
||||
.into_response(),
|
||||
Self::InsufficientScope(msg) => ApiError::InsufficientScope(Some(msg)).into_response(),
|
||||
other => ApiError::from(other).into_response(),
|
||||
}
|
||||
ApiError::from(self).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -106,7 +106,7 @@ struct CachedUserStatus {
|
||||
is_admin: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum TokenValidationError {
|
||||
AccountDeactivated,
|
||||
AccountTakedown,
|
||||
@@ -115,6 +115,8 @@ pub enum TokenValidationError {
|
||||
TokenExpired,
|
||||
OAuthTokenExpired,
|
||||
InvalidToken,
|
||||
UseDpopNonce(String),
|
||||
InvalidDpopProof(String),
|
||||
}
|
||||
|
||||
impl fmt::Display for TokenValidationError {
|
||||
@@ -126,6 +128,8 @@ impl fmt::Display for TokenValidationError {
|
||||
Self::AuthenticationFailed => write!(f, "AuthenticationFailed"),
|
||||
Self::TokenExpired | Self::OAuthTokenExpired => write!(f, "ExpiredToken"),
|
||||
Self::InvalidToken => write!(f, "InvalidToken"),
|
||||
Self::UseDpopNonce(_) => write!(f, "use_dpop_nonce"),
|
||||
Self::InvalidDpopProof(_) => write!(f, "invalid_dpop_proof"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -613,6 +617,12 @@ pub async fn validate_token_with_dpop(
|
||||
Err(crate::oauth::OAuthError::ExpiredToken(_)) => {
|
||||
Err(TokenValidationError::OAuthTokenExpired)
|
||||
}
|
||||
Err(crate::oauth::OAuthError::UseDpopNonce(nonce)) => {
|
||||
Err(TokenValidationError::UseDpopNonce(nonce))
|
||||
}
|
||||
Err(crate::oauth::OAuthError::InvalidDpopProof(msg)) => {
|
||||
Err(TokenValidationError::InvalidDpopProof(msg))
|
||||
}
|
||||
Err(_) => Err(TokenValidationError::AuthenticationFailed),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ pub use tranquil_comms::{
|
||||
CommsChannel, CommsSender, CommsStatus, CommsType, DEFAULT_LOCALE, DiscordSender, EmailSender,
|
||||
NewComms, NotificationStrings, QueuedComms, SendError, SignalSender, TelegramSender,
|
||||
VALID_LOCALES, format_message, get_strings, is_valid_phone_number, is_valid_signal_username,
|
||||
mime_encode_header, sanitize_header_value, validate_locale,
|
||||
validate_locale,
|
||||
};
|
||||
|
||||
pub use service::{CommsService, repo as comms_repo, resolve_delivery_channel};
|
||||
|
||||
@@ -149,13 +149,19 @@ impl CommsService {
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
let permanent = e.is_permanent();
|
||||
let error_msg = e.to_string();
|
||||
warn!(
|
||||
comms_id = %comms_id,
|
||||
error = %error_msg,
|
||||
permanent,
|
||||
"Failed to send comms"
|
||||
);
|
||||
if let Err(db_err) = self.mark_failed(comms_id, &error_msg).await {
|
||||
let db_result = match permanent {
|
||||
true => self.mark_failed_permanent(comms_id, &error_msg).await,
|
||||
false => self.mark_failed(comms_id, &error_msg).await,
|
||||
};
|
||||
if let Err(db_err) = db_result {
|
||||
error!(
|
||||
comms_id = %comms_id,
|
||||
error = %db_err,
|
||||
@@ -173,6 +179,14 @@ impl CommsService {
|
||||
async fn mark_failed(&self, id: Uuid, error: &str) -> Result<(), tranquil_db_traits::DbError> {
|
||||
self.infra_repo.mark_comms_failed(id, error).await
|
||||
}
|
||||
|
||||
async fn mark_failed_permanent(
|
||||
&self,
|
||||
id: Uuid,
|
||||
error: &str,
|
||||
) -> Result<(), tranquil_db_traits::DbError> {
|
||||
self.infra_repo.mark_comms_failed_permanent(id, error).await
|
||||
}
|
||||
}
|
||||
|
||||
struct ResolvedRecipient {
|
||||
|
||||
@@ -9,7 +9,7 @@ use axum::{
|
||||
use crate::api::error::ApiError;
|
||||
use crate::oauth::OAuthError;
|
||||
use crate::state::{AppState, RateLimitKind};
|
||||
use crate::util::extract_client_ip;
|
||||
use crate::util::client_ip_from_parts;
|
||||
|
||||
pub trait RateLimitPolicy: Send + Sync + 'static {
|
||||
const KIND: RateLimitKind;
|
||||
@@ -173,7 +173,7 @@ impl<P: RateLimitPolicy, R: RateLimitRejection> FromRequestParts<AppState>
|
||||
parts: &mut Parts,
|
||||
state: &AppState,
|
||||
) -> Result<Self, Self::Rejection> {
|
||||
let client_ip = extract_client_ip(&parts.headers, None);
|
||||
let client_ip = client_ip_from_parts(parts);
|
||||
|
||||
if !state.check_rate_limit(P::KIND, &client_ip).await {
|
||||
tracing::warn!(
|
||||
|
||||
@@ -19,7 +19,6 @@ use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::OwnedMutexGuard;
|
||||
use tracing::{error, warn};
|
||||
use tranquil_db_traits::SequenceNumber;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -526,10 +525,8 @@ pub async fn commit_and_log(
|
||||
|
||||
let obsolete_bytes: Vec<Vec<u8>> = obsolete_cids.iter().map(|c| c.to_bytes()).collect();
|
||||
|
||||
let final_ops: HashMap<(&Nsid, &Rkey), &RecordOp> = ops
|
||||
.iter()
|
||||
.map(|op| (op.collection_rkey(), op))
|
||||
.collect();
|
||||
let final_ops: HashMap<(&Nsid, &Rkey), &RecordOp> =
|
||||
ops.iter().map(|op| (op.collection_rkey(), op)).collect();
|
||||
|
||||
let final_record_uris: HashSet<AtUri> = final_ops
|
||||
.iter()
|
||||
@@ -778,7 +775,7 @@ pub async fn sequence_identity_event(
|
||||
state: &AppState,
|
||||
did: &Did,
|
||||
handle: Option<&Handle>,
|
||||
) -> Result<SequenceNumber, CommitError> {
|
||||
) -> Result<(), CommitError> {
|
||||
state
|
||||
.repos
|
||||
.repo
|
||||
@@ -790,7 +787,7 @@ pub async fn sequence_account_event(
|
||||
state: &AppState,
|
||||
did: &Did,
|
||||
status: tranquil_db_traits::AccountStatus,
|
||||
) -> Result<SequenceNumber, CommitError> {
|
||||
) -> Result<(), CommitError> {
|
||||
state
|
||||
.repos
|
||||
.repo
|
||||
@@ -803,7 +800,7 @@ pub async fn sequence_sync_event(
|
||||
did: &Did,
|
||||
commit_cid: &str,
|
||||
rev: Option<&str>,
|
||||
) -> Result<SequenceNumber, CommitError> {
|
||||
) -> Result<(), CommitError> {
|
||||
let cid_link: crate::types::CidLink = commit_cid
|
||||
.parse()
|
||||
.map_err(|_| CommitError::InvalidCid(commit_cid.to_string()))?;
|
||||
@@ -831,7 +828,7 @@ pub async fn sequence_genesis_commit(
|
||||
commit_cid: &Cid,
|
||||
mst_root_cid: &Cid,
|
||||
rev: &str,
|
||||
) -> Result<SequenceNumber, CommitError> {
|
||||
) -> Result<(), CommitError> {
|
||||
let commit_cid_link = crate::types::CidLink::from(commit_cid);
|
||||
let mst_root_cid_link = crate::types::CidLink::from(mst_root_cid);
|
||||
let commit_bytes = state
|
||||
|
||||
@@ -399,6 +399,10 @@ pub async fn start_scheduled_tasks(
|
||||
let mut compaction_ticker = interval(compaction_interval);
|
||||
compaction_ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
|
||||
let compaction_blocklist = Arc::new(parking_lot::Mutex::new(CompactionBlocklist::new(
|
||||
Duration::from_secs(300),
|
||||
)));
|
||||
|
||||
let mut reachability_ticker = interval(reachability_interval);
|
||||
reachability_ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
|
||||
|
||||
@@ -464,8 +468,9 @@ pub async fn start_scheduled_tasks(
|
||||
let store = store.clone();
|
||||
let threshold = cfg.scheduled.compaction_liveness_threshold;
|
||||
let grace_ms = cfg.scheduled.compaction_grace_period_ms;
|
||||
let blocklist = Arc::clone(&compaction_blocklist);
|
||||
if let Err(e) = tokio::task::spawn_blocking(move || {
|
||||
run_compaction_pass(&store, threshold, grace_ms)
|
||||
run_compaction_pass(&store, threshold, grace_ms, &blocklist)
|
||||
}).await.unwrap_or_else(|e| Err(anyhow::anyhow!("compaction task panicked: {e}"))) {
|
||||
error!("Compaction error: {e}");
|
||||
}
|
||||
@@ -485,6 +490,8 @@ pub async fn start_scheduled_tasks(
|
||||
live_refcounted = result.live_refcounted,
|
||||
leaked_blocks = result.leaked_blocks,
|
||||
repaired_blocks = result.repaired_blocks,
|
||||
phantom_files_purged = result.phantom_files_purged,
|
||||
phantom_blocks_purged = result.phantom_blocks_purged,
|
||||
bloom_heap_mb = result.bloom_heap_bytes / (1024 * 1024),
|
||||
"reachability walk complete"
|
||||
);
|
||||
@@ -536,11 +543,44 @@ pub async fn start_scheduled_tasks(
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CompactionBlocklist {
|
||||
entries: std::collections::HashMap<tranquil_store::blockstore::DataFileId, std::time::Instant>,
|
||||
cool_off: Duration,
|
||||
}
|
||||
|
||||
impl CompactionBlocklist {
|
||||
pub fn new(cool_off: Duration) -> Self {
|
||||
Self {
|
||||
entries: std::collections::HashMap::new(),
|
||||
cool_off,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn record_failure(&mut self, file_id: tranquil_store::blockstore::DataFileId) {
|
||||
self.entries.insert(file_id, std::time::Instant::now());
|
||||
}
|
||||
|
||||
pub fn is_blocked(&self, file_id: tranquil_store::blockstore::DataFileId) -> bool {
|
||||
self.entries
|
||||
.get(&file_id)
|
||||
.is_some_and(|recorded| recorded.elapsed() < self.cool_off)
|
||||
}
|
||||
|
||||
pub fn prune_expired(&mut self) {
|
||||
let cool_off = self.cool_off;
|
||||
self.entries
|
||||
.retain(|_, recorded| recorded.elapsed() < cool_off);
|
||||
}
|
||||
}
|
||||
|
||||
fn run_compaction_pass(
|
||||
store: &tranquil_store::blockstore::TranquilBlockStore,
|
||||
liveness_threshold: f64,
|
||||
grace_period_ms: u64,
|
||||
blocklist: &parking_lot::Mutex<CompactionBlocklist>,
|
||||
) -> anyhow::Result<()> {
|
||||
blocklist.lock().prune_expired();
|
||||
|
||||
match store.cleanup_gc_meta() {
|
||||
Ok(0) => {}
|
||||
Ok(n) => info!(count = n, "cleaned up stale gc_meta entries"),
|
||||
@@ -553,7 +593,11 @@ fn run_compaction_pass(
|
||||
|
||||
let candidate = liveness_map
|
||||
.iter()
|
||||
.filter(|(_, info)| info.total_blocks > 0 && info.ratio() < liveness_threshold)
|
||||
.filter(|(fid, info)| {
|
||||
info.total_blocks > 0
|
||||
&& info.ratio() < liveness_threshold
|
||||
&& !blocklist.lock().is_blocked(**fid)
|
||||
})
|
||||
.min_by(|(_, a), (_, b)| {
|
||||
a.ratio()
|
||||
.partial_cmp(&b.ratio())
|
||||
@@ -574,21 +618,35 @@ fn run_compaction_pass(
|
||||
"compacting data file"
|
||||
);
|
||||
match store.compact_file(file_id, grace_period_ms) {
|
||||
Ok(result) => {
|
||||
Ok(tranquil_store::blockstore::CompactionResult::Compacted(stats)) => {
|
||||
info!(
|
||||
file_id = %result.file_id,
|
||||
reclaimed_bytes = result.reclaimed_bytes,
|
||||
live_blocks = result.live_blocks,
|
||||
dead_blocks = result.dead_blocks,
|
||||
file_id = %stats.file_id,
|
||||
reclaimed_bytes = stats.reclaimed_bytes,
|
||||
live_blocks = stats.live_blocks,
|
||||
dead_blocks = stats.dead_blocks,
|
||||
"compaction complete"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
Ok(tranquil_store::blockstore::CompactionResult::Purged {
|
||||
file_id,
|
||||
phantom_blocks,
|
||||
}) => {
|
||||
warn!(
|
||||
file_id = %file_id,
|
||||
phantom_blocks,
|
||||
"compaction target missing on disk, purged phantom index entries"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
Err(tranquil_store::blockstore::CompactionError::ActiveFileCannotBeCompacted) => {
|
||||
debug!(file_id = %file_id, "skipped active file");
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => Err(anyhow::anyhow!("compaction failed: {e}")),
|
||||
Err(e) => {
|
||||
blocklist.lock().record_failure(file_id);
|
||||
Err(anyhow::anyhow!("compaction failed: {e}"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -653,7 +711,7 @@ async fn delete_account_data(
|
||||
warn!(storage_key = %key, error = %e, "Failed to delete blob from storage (continuing anyway)");
|
||||
});
|
||||
|
||||
let _account_seq = user_repo
|
||||
user_repo
|
||||
.delete_account_with_firehose(user_id, did)
|
||||
.await
|
||||
.context("Failed to delete account")?;
|
||||
@@ -693,6 +751,24 @@ pub async fn generate_repo_car(
|
||||
.await
|
||||
.context("Failed to fetch blocks")?;
|
||||
|
||||
let missing: Vec<Cid> = chunk
|
||||
.iter()
|
||||
.zip(blocks.iter())
|
||||
.filter_map(|(cid, block_opt)| block_opt.is_none().then_some(*cid))
|
||||
.collect();
|
||||
if !missing.is_empty() {
|
||||
anyhow::bail!(
|
||||
"repo CAR is incomplete: {} block(s) referenced by the MST are missing from storage. First 5: {}",
|
||||
missing.len(),
|
||||
missing
|
||||
.iter()
|
||||
.take(5)
|
||||
.map(|c| c.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
);
|
||||
}
|
||||
|
||||
chunk
|
||||
.iter()
|
||||
.zip(blocks.iter())
|
||||
@@ -746,6 +822,8 @@ pub struct ReachabilityResult {
|
||||
pub leaked_blocks: u64,
|
||||
pub repaired_blocks: u64,
|
||||
pub bloom_heap_bytes: usize,
|
||||
pub phantom_files_purged: u64,
|
||||
pub phantom_blocks_purged: u64,
|
||||
}
|
||||
|
||||
const REPO_PAGE_SIZE: i64 = 500;
|
||||
@@ -761,6 +839,7 @@ fn walk_repo_dag_sync(
|
||||
store: &tranquil_store::blockstore::TranquilBlockStore,
|
||||
head_cid: &Cid,
|
||||
reachable: &mut std::collections::HashSet<CidBytes>,
|
||||
phantom_files: &mut std::collections::HashSet<tranquil_store::blockstore::DataFileId>,
|
||||
) -> anyhow::Result<()> {
|
||||
let mut to_visit = vec![cid_to_bytes(head_cid)?];
|
||||
|
||||
@@ -769,15 +848,56 @@ fn walk_repo_dag_sync(
|
||||
continue;
|
||||
}
|
||||
|
||||
let block = match store.get_block_sync(&cid_bytes)? {
|
||||
Some(b) => b,
|
||||
None => {
|
||||
let block = match store.get_block_sync(&cid_bytes) {
|
||||
Ok(Some(b)) => b,
|
||||
Ok(None) => {
|
||||
tracing::warn!(
|
||||
?cid_bytes,
|
||||
"referenced block missing during reachability walk"
|
||||
);
|
||||
continue;
|
||||
}
|
||||
Err(e) => {
|
||||
let Some(entry) = store.block_index().get(&cid_bytes) else {
|
||||
tracing::warn!(
|
||||
?cid_bytes,
|
||||
error = %e,
|
||||
"reachability walk: index entry vanished between read attempt and re-check"
|
||||
);
|
||||
continue;
|
||||
};
|
||||
let file_path = store.data_file_path(entry.location.file_id);
|
||||
match file_path.try_exists() {
|
||||
Ok(false) => {
|
||||
tracing::warn!(
|
||||
?cid_bytes,
|
||||
file_id = %entry.location.file_id,
|
||||
error = %e,
|
||||
"indexed block points at missing data file, scheduling phantom purge"
|
||||
);
|
||||
phantom_files.insert(entry.location.file_id);
|
||||
continue;
|
||||
}
|
||||
Ok(true) => {
|
||||
return Err(anyhow::anyhow!(
|
||||
"reachability walk read error on present data file {}: {e}",
|
||||
entry.location.file_id
|
||||
));
|
||||
}
|
||||
Err(probe_err) => {
|
||||
tracing::warn!(
|
||||
?cid_bytes,
|
||||
file_id = %entry.location.file_id,
|
||||
existence_probe_error = %probe_err,
|
||||
"could not probe data file existence after read error"
|
||||
);
|
||||
return Err(anyhow::anyhow!(
|
||||
"reachability walk read error on file {}: {e}",
|
||||
entry.location.file_id
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if let Ok(commit) = Commit::from_cbor(&block) {
|
||||
@@ -858,13 +978,15 @@ pub fn run_reachability_walk(
|
||||
|
||||
let mut repos_walked: u64 = 0;
|
||||
let mut seen_heads: std::collections::HashMap<Did, CidLink> = std::collections::HashMap::new();
|
||||
let mut phantom_files: std::collections::HashSet<tranquil_store::blockstore::DataFileId> =
|
||||
std::collections::HashSet::new();
|
||||
|
||||
paginate_repos(&rt, repo_repo, |page| {
|
||||
page.iter().try_for_each(|repo| -> anyhow::Result<()> {
|
||||
let cid =
|
||||
Cid::from_str(repo.repo_root_cid.as_str()).context("invalid repo_root_cid")?;
|
||||
seen_heads.insert(repo.did.clone(), repo.repo_root_cid.clone());
|
||||
walk_repo_dag_sync(store, &cid, &mut visited)?;
|
||||
walk_repo_dag_sync(store, &cid, &mut visited, &mut phantom_files)?;
|
||||
repos_walked = repos_walked.saturating_add(1);
|
||||
if repos_walked.is_multiple_of(1000) {
|
||||
info!(
|
||||
@@ -894,7 +1016,7 @@ pub fn run_reachability_walk(
|
||||
let cid =
|
||||
Cid::from_str(repo.repo_root_cid.as_str()).context("invalid repo_root_cid")?;
|
||||
let mut extra = std::collections::HashSet::new();
|
||||
walk_repo_dag_sync(store, &cid, &mut extra)?;
|
||||
walk_repo_dag_sync(store, &cid, &mut extra, &mut phantom_files)?;
|
||||
extra.iter().for_each(|c| reachable.insert(c));
|
||||
seen_heads.insert(repo.did.clone(), repo.repo_root_cid.clone());
|
||||
stale_repos = stale_repos.saturating_add(1);
|
||||
@@ -922,7 +1044,7 @@ pub fn run_reachability_walk(
|
||||
let cid =
|
||||
Cid::from_str(repo.repo_root_cid.as_str()).context("invalid repo_root_cid")?;
|
||||
let mut extra = std::collections::HashSet::new();
|
||||
walk_repo_dag_sync(store, &cid, &mut extra)?;
|
||||
walk_repo_dag_sync(store, &cid, &mut extra, &mut phantom_files)?;
|
||||
extra.iter().for_each(|c| reachable.insert(c));
|
||||
quiesced_stale = quiesced_stale.saturating_add(1);
|
||||
Ok(())
|
||||
@@ -958,6 +1080,19 @@ pub fn run_reachability_walk(
|
||||
}
|
||||
};
|
||||
|
||||
let phantom_files_purged = u64::try_from(phantom_files.len()).unwrap_or(u64::MAX);
|
||||
let phantom_blocks_purged = phantom_files
|
||||
.iter()
|
||||
.map(|fid| store.block_index().purge_by_file_id(*fid))
|
||||
.sum::<u64>();
|
||||
|
||||
if phantom_files_purged > 0 {
|
||||
warn!(
|
||||
phantom_files_purged,
|
||||
phantom_blocks_purged, "purged phantom index entries from unreadable data files"
|
||||
);
|
||||
}
|
||||
|
||||
Ok(ReachabilityResult {
|
||||
repos_walked,
|
||||
blocks_visited,
|
||||
@@ -965,5 +1100,7 @@ pub fn run_reachability_walk(
|
||||
leaked_blocks,
|
||||
repaired_blocks,
|
||||
bloom_heap_bytes,
|
||||
phantom_files_purged,
|
||||
phantom_blocks_purged,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -214,10 +214,10 @@ impl AppState {
|
||||
pub async fn new(shutdown: CancellationToken) -> Result<Self, Box<dyn Error>> {
|
||||
let cfg = tranquil_config::get();
|
||||
|
||||
match cfg.storage.repo_backend() {
|
||||
let mut state = match cfg.storage.repo_backend() {
|
||||
tranquil_config::RepoBackend::TranquilStore => {
|
||||
tracing::info!("tranquil-store repo backend active. EXPERIMENTAL!");
|
||||
Ok(Self::from_store(shutdown).await)
|
||||
Self::from_store(shutdown).await
|
||||
}
|
||||
tranquil_config::RepoBackend::Postgres => {
|
||||
let database_url = &cfg.database.url;
|
||||
@@ -247,28 +247,21 @@ impl AppState {
|
||||
.await
|
||||
.map_err(|e| format!("Failed to run migrations: {}", e))?;
|
||||
|
||||
let bootstrap_invite_code = match (
|
||||
cfg.server.invite_code_required,
|
||||
sqlx::query_scalar!("SELECT COUNT(*) FROM users")
|
||||
.fetch_one(&db)
|
||||
.await,
|
||||
) {
|
||||
(true, Ok(Some(0))) => {
|
||||
let code = crate::util::gen_invite_code();
|
||||
tracing::info!(
|
||||
"No users exist and invite codes are required. Bootstrap invite code: {}",
|
||||
code
|
||||
);
|
||||
Some(code)
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let mut state = Self::from_db(db, shutdown).await;
|
||||
state.bootstrap_invite_code = bootstrap_invite_code;
|
||||
Ok(state)
|
||||
Self::from_db(db, shutdown).await
|
||||
}
|
||||
};
|
||||
|
||||
if cfg.server.invite_code_required && state.repos.user.count_users().await.unwrap_or(1) == 0
|
||||
{
|
||||
let code = crate::util::gen_invite_code();
|
||||
tracing::info!(
|
||||
"No users exist and invite codes are required. Bootstrap invite code: {}",
|
||||
code
|
||||
);
|
||||
state.bootstrap_invite_code = Some(code);
|
||||
}
|
||||
|
||||
Ok(state)
|
||||
}
|
||||
|
||||
pub async fn from_db(db: PgPool, shutdown: CancellationToken) -> Self {
|
||||
@@ -523,13 +516,16 @@ fn wire_tranquil_store(
|
||||
let metastore =
|
||||
Metastore::open(&metastore_dir, metastore_config).expect("failed to open metastore");
|
||||
|
||||
let blockstore = TranquilBlockStore::open(BlockStoreConfig {
|
||||
data_dir: blockstore_data_dir,
|
||||
index_dir: blockstore_index_dir,
|
||||
max_file_size: store_cfg.max_blockstore_file_size,
|
||||
group_commit: Default::default(),
|
||||
shard_count: tranquil_store::blockstore::DEFAULT_SHARD_COUNT,
|
||||
})
|
||||
let blockstore = TranquilBlockStore::open_with_retry(
|
||||
BlockStoreConfig {
|
||||
data_dir: blockstore_data_dir,
|
||||
index_dir: blockstore_index_dir,
|
||||
max_file_size: store_cfg.max_blockstore_file_size,
|
||||
group_commit: Default::default(),
|
||||
shard_count: tranquil_store::blockstore::DEFAULT_SHARD_COUNT,
|
||||
},
|
||||
tranquil_store::blockstore::OpenRetryPolicy::default(),
|
||||
)
|
||||
.expect("failed to open blockstore");
|
||||
|
||||
let event_log = EventLog::open(
|
||||
@@ -577,6 +573,18 @@ fn wire_tranquil_store(
|
||||
"repaired orphan data files"
|
||||
);
|
||||
}
|
||||
if repair.orphan_hints_removed > 0 {
|
||||
tracing::info!(
|
||||
removed = repair.orphan_hints_removed,
|
||||
"repaired orphan hint files"
|
||||
);
|
||||
}
|
||||
if repair.phantom_index_entries_purged > 0 {
|
||||
tracing::info!(
|
||||
purged = repair.phantom_index_entries_purged,
|
||||
"purged phantom index entries pointing at missing data files"
|
||||
);
|
||||
}
|
||||
if repair.had_errors() {
|
||||
tracing::warn!(errors = repair.repair_errors, "some repairs failed");
|
||||
}
|
||||
@@ -595,6 +603,17 @@ fn wire_tranquil_store(
|
||||
}
|
||||
}
|
||||
|
||||
if std::env::var("TRANQUIL_PURGE_ORPHAN_REPOS").is_ok_and(|v| v == "1") {
|
||||
match metastore
|
||||
.repo_ops()
|
||||
.purge_orphan_repos(metastore.database())
|
||||
{
|
||||
Ok(0) => tracing::info!("orphan repo purge: no orphans found"),
|
||||
Ok(n) => tracing::info!(purged = n, "orphan repo purge: removed orphan repo_meta"),
|
||||
Err(e) => tracing::error!(error = %e, "orphan repo purge failed"),
|
||||
}
|
||||
}
|
||||
|
||||
let notifier = bridge.notifier();
|
||||
let signal_db = metastore.database().clone();
|
||||
let signal_ks = metastore.signal_keyspace();
|
||||
|
||||
@@ -7,6 +7,7 @@ use rand::Rng;
|
||||
use serde_json::Value as JsonValue;
|
||||
use std::collections::BTreeMap;
|
||||
use std::net::SocketAddr;
|
||||
use std::num::NonZeroUsize;
|
||||
use std::str::FromStr;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
@@ -96,22 +97,99 @@ pub fn generate_random_token() -> String {
|
||||
URL_SAFE_NO_PAD.encode(bytes)
|
||||
}
|
||||
|
||||
pub fn extract_client_ip(headers: &HeaderMap, addr: Option<SocketAddr>) -> String {
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub(crate) enum ForwardedTrust {
|
||||
Peer,
|
||||
Proxies(NonZeroUsize),
|
||||
}
|
||||
|
||||
fn resolve_trust(configured: Option<usize>, terminates_tls: bool) -> ForwardedTrust {
|
||||
let count = configured.unwrap_or(if terminates_tls { 0 } else { 1 });
|
||||
match NonZeroUsize::new(count) {
|
||||
Some(proxies) => ForwardedTrust::Proxies(proxies),
|
||||
None => ForwardedTrust::Peer,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn forwarded_trust() -> ForwardedTrust {
|
||||
match tranquil_config::try_get() {
|
||||
Some(cfg) => resolve_trust(
|
||||
cfg.server.trusted_proxy_count,
|
||||
cfg.server.tls.material().is_some(),
|
||||
),
|
||||
None => ForwardedTrust::Peer,
|
||||
}
|
||||
}
|
||||
|
||||
fn forwarded_client_ip(headers: &HeaderMap, trusted: NonZeroUsize) -> Option<String> {
|
||||
if let Some(forwarded) = headers.get("x-forwarded-for")
|
||||
&& let Ok(value) = forwarded.to_str()
|
||||
&& let Some(first_ip) = value.split(',').next()
|
||||
{
|
||||
return first_ip.trim().to_string();
|
||||
let hops: Vec<&str> = value
|
||||
.split(',')
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
.collect();
|
||||
if let Some(client) = hops
|
||||
.len()
|
||||
.checked_sub(trusted.get())
|
||||
.and_then(|idx| hops.get(idx))
|
||||
{
|
||||
return Some((*client).to_string());
|
||||
}
|
||||
}
|
||||
if let Some(real_ip) = headers.get("x-real-ip")
|
||||
if trusted.get() == 1
|
||||
&& let Some(real_ip) = headers.get("x-real-ip")
|
||||
&& let Ok(value) = real_ip.to_str()
|
||||
&& !value.trim().is_empty()
|
||||
{
|
||||
return value.trim().to_string();
|
||||
return Some(value.trim().to_string());
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub(crate) fn extract_client_ip(
|
||||
headers: &HeaderMap,
|
||||
addr: Option<SocketAddr>,
|
||||
trust: ForwardedTrust,
|
||||
) -> String {
|
||||
if let ForwardedTrust::Proxies(trusted) = trust
|
||||
&& let Some(client) = forwarded_client_ip(headers, trusted)
|
||||
{
|
||||
return client;
|
||||
}
|
||||
addr.map(|a| a.ip().to_string())
|
||||
.unwrap_or_else(|| "unknown".to_string())
|
||||
}
|
||||
|
||||
pub(crate) fn client_ip_from_parts(parts: &axum::http::request::Parts) -> String {
|
||||
let addr = parts
|
||||
.extensions
|
||||
.get::<axum::extract::ConnectInfo<SocketAddr>>()
|
||||
.map(|connect_info| connect_info.0);
|
||||
extract_client_ip(&parts.headers, addr, forwarded_trust())
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ClientIp(String);
|
||||
|
||||
impl ClientIp {
|
||||
pub fn into_string(self) -> String {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Send + Sync> axum::extract::FromRequestParts<S> for ClientIp {
|
||||
type Rejection = std::convert::Infallible;
|
||||
|
||||
async fn from_request_parts(
|
||||
parts: &mut axum::http::request::Parts,
|
||||
_state: &S,
|
||||
) -> Result<Self, Self::Rejection> {
|
||||
Ok(ClientIp(client_ip_from_parts(parts)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_discord_bot_username(username: String) {
|
||||
DISCORD_BOT_USERNAME.set(username).ok();
|
||||
}
|
||||
@@ -227,6 +305,135 @@ pub fn is_self_hosted_did_web_enabled() -> bool {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use axum::extract::{ConnectInfo, FromRequestParts};
|
||||
|
||||
fn proxies(count: usize) -> ForwardedTrust {
|
||||
ForwardedTrust::Proxies(NonZeroUsize::new(count).unwrap())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_trust_override_wins_over_tls() {
|
||||
assert_eq!(resolve_trust(Some(1), true), proxies(1));
|
||||
assert_eq!(resolve_trust(Some(3), false), proxies(3));
|
||||
assert_eq!(resolve_trust(Some(0), true), ForwardedTrust::Peer);
|
||||
assert_eq!(resolve_trust(Some(0), false), ForwardedTrust::Peer);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_trust_infers_from_tls_when_unset() {
|
||||
assert_eq!(resolve_trust(None, true), ForwardedTrust::Peer);
|
||||
assert_eq!(resolve_trust(None, false), proxies(1));
|
||||
}
|
||||
|
||||
fn parts_with(
|
||||
header: Option<(&str, &str)>,
|
||||
peer: Option<SocketAddr>,
|
||||
) -> axum::http::request::Parts {
|
||||
let mut builder = axum::http::Request::builder();
|
||||
if let Some((name, value)) = header {
|
||||
builder = builder.header(name, value);
|
||||
}
|
||||
let mut parts = builder.body(()).unwrap().into_parts().0;
|
||||
if let Some(addr) = peer {
|
||||
parts.extensions.insert(ConnectInfo(addr));
|
||||
}
|
||||
parts
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn client_ip_falls_back_to_peer_socket() {
|
||||
let peer: SocketAddr = "203.0.113.7:51000".parse().unwrap();
|
||||
let mut parts = parts_with(None, Some(peer));
|
||||
let ip = ClientIp::from_request_parts(&mut parts, &()).await.unwrap();
|
||||
assert_eq!(ip.into_string(), "203.0.113.7");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn client_ip_ignores_forwarded_when_config_absent() {
|
||||
let peer: SocketAddr = "203.0.113.7:51000".parse().unwrap();
|
||||
let mut parts = parts_with(
|
||||
Some(("x-forwarded-for", "198.51.100.4, 10.0.0.1")),
|
||||
Some(peer),
|
||||
);
|
||||
let ip = ClientIp::from_request_parts(&mut parts, &()).await.unwrap();
|
||||
assert_eq!(ip.into_string(), "203.0.113.7");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn client_ip_unknown_without_headers_or_peer() {
|
||||
let mut parts = parts_with(None, None);
|
||||
let ip = ClientIp::from_request_parts(&mut parts, &()).await.unwrap();
|
||||
assert_eq!(ip.into_string(), "unknown");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn client_ip_renders_ipv6_peer_without_brackets() {
|
||||
let peer: SocketAddr = "[2001:db8::beef]:51000".parse().unwrap();
|
||||
let mut parts = parts_with(None, Some(peer));
|
||||
let ip = ClientIp::from_request_parts(&mut parts, &()).await.unwrap();
|
||||
assert_eq!(ip.into_string(), "2001:db8::beef");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_client_ip_single_proxy_takes_rightmost_forwarded_hop() {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
"x-forwarded-for",
|
||||
"9.9.9.9, 198.51.100.4, 10.0.0.1".parse().unwrap(),
|
||||
);
|
||||
let peer: SocketAddr = "203.0.113.7:51000".parse().unwrap();
|
||||
assert_eq!(
|
||||
extract_client_ip(&headers, Some(peer), proxies(1)),
|
||||
"10.0.0.1"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_client_ip_two_proxies_skips_inner_hop() {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
"x-forwarded-for",
|
||||
"9.9.9.9, 198.51.100.4, 10.0.0.1".parse().unwrap(),
|
||||
);
|
||||
let peer: SocketAddr = "203.0.113.7:51000".parse().unwrap();
|
||||
assert_eq!(
|
||||
extract_client_ip(&headers, Some(peer), proxies(2)),
|
||||
"198.51.100.4"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_client_ip_more_trusted_proxies_than_hops_uses_peer() {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("x-forwarded-for", "10.0.0.1".parse().unwrap());
|
||||
let peer: SocketAddr = "203.0.113.7:51000".parse().unwrap();
|
||||
assert_eq!(
|
||||
extract_client_ip(&headers, Some(peer), proxies(2)),
|
||||
"203.0.113.7"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_client_ip_ignores_forwarded_headers_for_direct_peer() {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("x-forwarded-for", "9.9.9.9".parse().unwrap());
|
||||
headers.insert("x-real-ip", "9.9.9.9".parse().unwrap());
|
||||
let peer: SocketAddr = "203.0.113.7:51000".parse().unwrap();
|
||||
assert_eq!(
|
||||
extract_client_ip(&headers, Some(peer), ForwardedTrust::Peer),
|
||||
"203.0.113.7"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_client_ip_direct_peer_without_socket_is_unknown() {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert("x-forwarded-for", "9.9.9.9".parse().unwrap());
|
||||
assert_eq!(
|
||||
extract_client_ip(&headers, None, ForwardedTrust::Peer),
|
||||
"unknown"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_repeated_query_param_repeated() {
|
||||
|
||||
@@ -921,6 +921,40 @@ pub async fn get_test_block_store() -> &'static tranquil_pds::repo::AnyBlockStor
|
||||
.expect("TEST_BLOCK_STORE not initialized")
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn flushed_max_seq(
|
||||
repos: &tranquil_db::PostgresRepositories,
|
||||
) -> tranquil_db_traits::SequenceNumber {
|
||||
repos
|
||||
.repo
|
||||
.flush_pending_sequences()
|
||||
.await
|
||||
.expect("flush_pending_sequences");
|
||||
repos.repo.get_max_seq().await.expect("get_max_seq")
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn sequenced_event_for_did(
|
||||
repos: &tranquil_db::PostgresRepositories,
|
||||
baseline: tranquil_db_traits::SequenceNumber,
|
||||
did: &tranquil_types::Did,
|
||||
) -> tranquil_db_traits::SequencedEvent {
|
||||
repos
|
||||
.repo
|
||||
.flush_pending_sequences()
|
||||
.await
|
||||
.expect("flush_pending_sequences");
|
||||
repos
|
||||
.repo
|
||||
.get_events_since_seq(baseline, None)
|
||||
.await
|
||||
.expect("get_events_since_seq")
|
||||
.into_iter()
|
||||
.filter(|event| &event.did == did)
|
||||
.last()
|
||||
.unwrap_or_else(|| panic!("event for did {did} not found after flush"))
|
||||
}
|
||||
|
||||
fn extract_verification_code(body_text: &str) -> String {
|
||||
let lines: Vec<&str> = body_text.lines().collect();
|
||||
lines
|
||||
|
||||
@@ -66,6 +66,11 @@ async fn commit_events_carry_inline_blocks() {
|
||||
let repos = get_test_repos().await;
|
||||
let typed_did = tranquil_types::Did::new(did.clone()).unwrap();
|
||||
|
||||
repos
|
||||
.repo
|
||||
.flush_pending_sequences()
|
||||
.await
|
||||
.expect("flush_pending_sequences");
|
||||
let events = repos
|
||||
.repo
|
||||
.get_events_since_seq(SequenceNumber::ZERO, None)
|
||||
@@ -142,18 +147,14 @@ async fn sync_event_carries_inline_commit_block() {
|
||||
let cid_link: CidLink = (&commit_cid).into();
|
||||
let rev = "3kabcdefghij2";
|
||||
|
||||
let seq = repos
|
||||
let baseline = repos.repo.get_max_seq().await.expect("get_max_seq");
|
||||
repos
|
||||
.repo
|
||||
.insert_sync_event(&did, &cid_link, Some(rev), &commit_bytes)
|
||||
.await
|
||||
.expect("insert_sync_event");
|
||||
|
||||
let event = repos
|
||||
.repo
|
||||
.get_event_by_seq(seq)
|
||||
.await
|
||||
.expect("get_event_by_seq")
|
||||
.expect("event present");
|
||||
let event = sequenced_event_for_did(repos, baseline, &did).await;
|
||||
|
||||
assert_eq!(event.event_type, RepoEventType::Sync);
|
||||
let blocks = event
|
||||
@@ -193,7 +194,8 @@ async fn genesis_commit_event_carries_inline_blocks() {
|
||||
let mst_link: CidLink = (&mst_root_cid).into();
|
||||
let rev = "3kabcdefghij3";
|
||||
|
||||
let seq = repos
|
||||
let baseline = repos.repo.get_max_seq().await.expect("get_max_seq");
|
||||
repos
|
||||
.repo
|
||||
.insert_genesis_commit_event(
|
||||
&did,
|
||||
@@ -206,12 +208,7 @@ async fn genesis_commit_event_carries_inline_blocks() {
|
||||
.await
|
||||
.expect("insert_genesis_commit_event");
|
||||
|
||||
let event = repos
|
||||
.repo
|
||||
.get_event_by_seq(seq)
|
||||
.await
|
||||
.expect("get_event_by_seq")
|
||||
.expect("event present");
|
||||
let event = sequenced_event_for_did(repos, baseline, &did).await;
|
||||
|
||||
assert_eq!(event.event_type, RepoEventType::Commit);
|
||||
let blocks = event
|
||||
@@ -329,6 +326,11 @@ async fn import_event_carries_inline_commit_block() {
|
||||
|
||||
let repos = get_test_repos().await;
|
||||
let typed_did = tranquil_types::Did::new(did.clone()).unwrap();
|
||||
repos
|
||||
.repo
|
||||
.flush_pending_sequences()
|
||||
.await
|
||||
.expect("flush_pending_sequences");
|
||||
let events = repos
|
||||
.repo
|
||||
.get_events_since_seq(SequenceNumber::ZERO, None)
|
||||
|
||||
@@ -801,7 +801,7 @@ async fn test_firehose_outdated_cursor_info() {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
|
||||
let repos = get_test_repos().await;
|
||||
let max_seq = repos.repo.get_max_seq().await.unwrap().as_i64();
|
||||
let max_seq = flushed_max_seq(repos).await.as_i64();
|
||||
let outdated_cursor = (max_seq - 100).max(1);
|
||||
let url = format!(
|
||||
"ws://127.0.0.1:{}/xrpc/com.atproto.sync.subscribeRepos?cursor={}",
|
||||
|
||||
@@ -223,7 +223,7 @@ async fn websocket_firehose_frames_pass_inductive_forward_and_inverse() {
|
||||
let (token, did) = create_account_and_login(&client).await;
|
||||
|
||||
let repos = get_test_repos().await;
|
||||
let cursor = repos.repo.get_max_seq().await.unwrap().as_i64();
|
||||
let cursor = flushed_max_seq(repos).await.as_i64();
|
||||
let consumer = FirehoseConsumer::connect_with_cursor(app_port(), cursor).await;
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
|
||||
@@ -298,7 +298,7 @@ async fn websocket_firehose_car_root_matches_commit_cid() {
|
||||
let (token, did) = create_account_and_login(&client).await;
|
||||
|
||||
let repos = get_test_repos().await;
|
||||
let cursor = repos.repo.get_max_seq().await.unwrap().as_i64();
|
||||
let cursor = flushed_max_seq(repos).await.as_i64();
|
||||
let consumer = FirehoseConsumer::connect_with_cursor(app_port(), cursor).await;
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
|
||||
@@ -340,7 +340,7 @@ async fn websocket_firehose_resumption_from_cursor_yields_valid_frames() {
|
||||
create_record(&client, &token, &did, &rkey_for(i), "pre").await;
|
||||
}
|
||||
|
||||
let resume_cursor = repos.repo.get_max_seq().await.unwrap().as_i64();
|
||||
let resume_cursor = flushed_max_seq(repos).await.as_i64();
|
||||
|
||||
for i in 5..12 {
|
||||
create_record(&client, &token, &did, &rkey_for(i), "post").await;
|
||||
@@ -371,7 +371,7 @@ async fn websocket_firehose_ops_include_prev_field_for_update_delete() {
|
||||
let client = client();
|
||||
let (token, did) = create_account_and_login(&client).await;
|
||||
let repos = get_test_repos().await;
|
||||
let cursor = repos.repo.get_max_seq().await.unwrap().as_i64();
|
||||
let cursor = flushed_max_seq(repos).await.as_i64();
|
||||
let consumer = FirehoseConsumer::connect_with_cursor(app_port(), cursor).await;
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
|
||||
@@ -417,7 +417,7 @@ async fn websocket_firehose_rebuild_new_mst_from_car_matches_commit_data() {
|
||||
let client = client();
|
||||
let (token, did) = create_account_and_login(&client).await;
|
||||
let repos = get_test_repos().await;
|
||||
let cursor = repos.repo.get_max_seq().await.unwrap().as_i64();
|
||||
let cursor = flushed_max_seq(repos).await.as_i64();
|
||||
let consumer = FirehoseConsumer::connect_with_cursor(app_port(), cursor).await;
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
|
||||
|
||||
@@ -247,6 +247,11 @@ fn rkey_for(prefix: &str, i: usize) -> String {
|
||||
async fn our_commit_events(did: &str) -> Vec<SequencedEvent> {
|
||||
let repos = get_test_repos().await;
|
||||
let typed_did = Did::new(did.to_string()).unwrap();
|
||||
repos
|
||||
.repo
|
||||
.flush_pending_sequences()
|
||||
.await
|
||||
.expect("flush_pending_sequences");
|
||||
let events = repos
|
||||
.repo
|
||||
.get_events_since_seq(SequenceNumber::ZERO, None)
|
||||
|
||||
@@ -216,6 +216,11 @@ async fn imported_repo_emits_commit_event_with_valid_car() {
|
||||
|
||||
let repos = get_test_repos().await;
|
||||
let typed_did = Did::new(did.clone()).unwrap();
|
||||
repos
|
||||
.repo
|
||||
.flush_pending_sequences()
|
||||
.await
|
||||
.expect("flush_pending_sequences");
|
||||
let events = repos
|
||||
.repo
|
||||
.get_events_since_seq(SequenceNumber::ZERO, None)
|
||||
@@ -253,7 +258,7 @@ async fn firehose_commit_block_bytes_roundtrip_to_same_cid() {
|
||||
let (token, did) = create_account_and_login(&client).await;
|
||||
|
||||
let repos = get_test_repos().await;
|
||||
let cursor = repos.repo.get_max_seq().await.unwrap().as_i64();
|
||||
let cursor = flushed_max_seq(repos).await.as_i64();
|
||||
let consumer = FirehoseConsumer::connect_with_cursor(app_port(), cursor).await;
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
|
||||
@@ -284,7 +289,7 @@ async fn firehose_commit_car_contains_new_record_bytes_for_every_create() {
|
||||
let (token, did) = create_account_and_login(&client).await;
|
||||
|
||||
let repos = get_test_repos().await;
|
||||
let cursor = repos.repo.get_max_seq().await.unwrap().as_i64();
|
||||
let cursor = flushed_max_seq(repos).await.as_i64();
|
||||
let consumer = FirehoseConsumer::connect_with_cursor(app_port(), cursor).await;
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
|
||||
|
||||
@@ -267,6 +267,11 @@ async fn fuzz_run_with_seed(seed: u64, steps: usize) -> Vec<String> {
|
||||
|
||||
let repos = get_test_repos().await;
|
||||
let typed_did = Did::new(did.clone()).unwrap();
|
||||
repos
|
||||
.repo
|
||||
.flush_pending_sequences()
|
||||
.await
|
||||
.expect("flush_pending_sequences");
|
||||
let events = repos
|
||||
.repo
|
||||
.get_events_since_seq(SequenceNumber::ZERO, None)
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
mod common;
|
||||
mod helpers;
|
||||
|
||||
use chrono::{DateTime, Duration, Utc};
|
||||
use common::{base_url, client, get_test_repos};
|
||||
use futures::StreamExt;
|
||||
use helpers::verify_new_account;
|
||||
use reqwest::StatusCode;
|
||||
use serde_json::{Value, json};
|
||||
use tranquil_oauth::{
|
||||
AuthorizationRequestParameters, ClientAuth, CodeChallengeMethod, ResponseType, TokenData,
|
||||
TokenId,
|
||||
};
|
||||
use tranquil_types::Did;
|
||||
|
||||
async fn create_account_and_get_did(handle: &str, email: &str, password: &str) -> Did {
|
||||
let client = client();
|
||||
let res = client
|
||||
.post(format!(
|
||||
"{}/xrpc/com.atproto.server.createAccount",
|
||||
base_url().await
|
||||
))
|
||||
.json(&json!({
|
||||
"handle": handle,
|
||||
"email": email,
|
||||
"password": password,
|
||||
}))
|
||||
.send()
|
||||
.await
|
||||
.expect("createAccount request failed");
|
||||
assert_eq!(res.status(), StatusCode::OK, "createAccount failed");
|
||||
let body: Value = res.json().await.expect("invalid createAccount JSON");
|
||||
let did_str = body["did"]
|
||||
.as_str()
|
||||
.expect("no did in response")
|
||||
.to_string();
|
||||
let _ = verify_new_account(&client, &did_str).await;
|
||||
Did::new(did_str).expect("invalid DID format")
|
||||
}
|
||||
|
||||
fn make_token_data(did: &Did, token_id: &str, created_at: DateTime<Utc>) -> TokenData {
|
||||
let client_id = "https://squid.nel.pet/client".to_string();
|
||||
TokenData {
|
||||
did: did.clone(),
|
||||
token_id: TokenId(token_id.to_string()),
|
||||
created_at,
|
||||
updated_at: created_at,
|
||||
expires_at: created_at + Duration::hours(1),
|
||||
client_id: client_id.clone(),
|
||||
client_auth: ClientAuth::None,
|
||||
device_id: None,
|
||||
parameters: AuthorizationRequestParameters {
|
||||
response_type: ResponseType::Code,
|
||||
client_id,
|
||||
redirect_uri: "https://squid.nel.pet/cb".to_string(),
|
||||
scope: None,
|
||||
state: None,
|
||||
code_challenge: "x".to_string(),
|
||||
code_challenge_method: CodeChallengeMethod::S256,
|
||||
response_mode: None,
|
||||
login_hint: None,
|
||||
dpop_jkt: None,
|
||||
prompt: None,
|
||||
extra: None,
|
||||
},
|
||||
details: None,
|
||||
code: None,
|
||||
current_refresh_token: None,
|
||||
scope: None,
|
||||
controller_did: None,
|
||||
}
|
||||
}
|
||||
|
||||
async fn seed_tokens(repos: &tranquil_db::PostgresRepositories, tokens: &[TokenData]) {
|
||||
futures::stream::iter(tokens)
|
||||
.for_each(|token| async move {
|
||||
repos
|
||||
.oauth
|
||||
.create_token(token)
|
||||
.await
|
||||
.expect("token insert failed");
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn delete_oldest_tokens_evicts_lowest_created_at() {
|
||||
let ts = Utc::now().timestamp_millis();
|
||||
let handle = format!("tok-evict-{}.test", ts);
|
||||
let email = format!("tok-evict-{}@test.com", ts);
|
||||
let did = create_account_and_get_did(&handle, &email, "EvictTest123!").await;
|
||||
|
||||
let repos = get_test_repos().await;
|
||||
|
||||
let base = Utc::now();
|
||||
let token_ids: Vec<String> = (0..5).map(|i| format!("tok-{}-{}", ts, i)).collect();
|
||||
let tokens: Vec<TokenData> = (0i64..)
|
||||
.zip(token_ids.iter())
|
||||
.map(|(offset, tid)| make_token_data(&did, tid, base + Duration::seconds(offset)))
|
||||
.collect();
|
||||
seed_tokens(repos, &tokens).await;
|
||||
|
||||
let count_before = repos
|
||||
.oauth
|
||||
.count_tokens_for_user(&did)
|
||||
.await
|
||||
.expect("count failed");
|
||||
assert_eq!(count_before, 5, "all 5 tokens should be present");
|
||||
|
||||
let deleted = repos
|
||||
.oauth
|
||||
.delete_oldest_tokens_for_user(&did, 3)
|
||||
.await
|
||||
.expect("delete failed");
|
||||
assert_eq!(deleted, 2, "two oldest tokens should be deleted");
|
||||
|
||||
let remaining = repos
|
||||
.oauth
|
||||
.list_tokens_for_user(&did)
|
||||
.await
|
||||
.expect("list failed");
|
||||
assert_eq!(remaining.len(), 3, "three newest tokens should remain");
|
||||
|
||||
let remaining_ids: std::collections::HashSet<String> =
|
||||
remaining.iter().map(|t| t.token_id.0.clone()).collect();
|
||||
let expected_ids: std::collections::HashSet<String> = token_ids[2..].iter().cloned().collect();
|
||||
assert_eq!(
|
||||
remaining_ids, expected_ids,
|
||||
"surviving tokens must be the three newest by created_at"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn delete_oldest_tokens_no_op_when_under_keep_count() {
|
||||
let ts = Utc::now().timestamp_millis();
|
||||
let handle = format!("tok-evict-noop-{}.test", ts);
|
||||
let email = format!("tok-evict-noop-{}@test.com", ts);
|
||||
let did = create_account_and_get_did(&handle, &email, "EvictTest123!").await;
|
||||
|
||||
let repos = get_test_repos().await;
|
||||
|
||||
let base = Utc::now();
|
||||
let tokens: Vec<TokenData> = (0i64..2)
|
||||
.map(|offset| {
|
||||
make_token_data(
|
||||
&did,
|
||||
&format!("noop-tok-{}-{}", ts, offset),
|
||||
base + Duration::seconds(offset),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
seed_tokens(repos, &tokens).await;
|
||||
|
||||
let deleted = repos
|
||||
.oauth
|
||||
.delete_oldest_tokens_for_user(&did, 5)
|
||||
.await
|
||||
.expect("delete failed");
|
||||
assert_eq!(deleted, 0, "nothing to delete when count <= keep");
|
||||
|
||||
let remaining = repos
|
||||
.oauth
|
||||
.list_tokens_for_user(&did)
|
||||
.await
|
||||
.expect("list failed");
|
||||
assert_eq!(remaining.len(), 2);
|
||||
}
|
||||
@@ -402,10 +402,7 @@ async fn test_apply_writes_delete_then_create_same_rkey() {
|
||||
"{}/xrpc/com.atproto.repo.listRecords",
|
||||
base_url().await
|
||||
))
|
||||
.query(&[
|
||||
("repo", did.as_str()),
|
||||
("collection", "app.bsky.feed.post"),
|
||||
])
|
||||
.query(&[("repo", did.as_str()), ("collection", "app.bsky.feed.post")])
|
||||
.send()
|
||||
.await
|
||||
.expect("Failed to list records");
|
||||
@@ -484,10 +481,7 @@ async fn test_apply_writes_create_then_delete_same_rkey() {
|
||||
"{}/xrpc/com.atproto.repo.listRecords",
|
||||
base_url().await
|
||||
))
|
||||
.query(&[
|
||||
("repo", did.as_str()),
|
||||
("collection", "app.bsky.feed.post"),
|
||||
])
|
||||
.query(&[("repo", did.as_str()), ("collection", "app.bsky.feed.post")])
|
||||
.send()
|
||||
.await
|
||||
.expect("Failed to list records");
|
||||
|
||||
@@ -59,7 +59,7 @@ async fn test_create_record_cid_matches_firehose() {
|
||||
let (token, did) = create_account_and_login(&client).await;
|
||||
|
||||
let repos = get_test_repos().await;
|
||||
let cursor = repos.repo.get_max_seq().await.unwrap().as_i64();
|
||||
let cursor = flushed_max_seq(repos).await.as_i64();
|
||||
let consumer = FirehoseConsumer::connect_with_cursor(app_port(), cursor).await;
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
|
||||
@@ -134,7 +134,7 @@ async fn test_update_record_prev_matches_old_cid() {
|
||||
let v1_cid = Cid::from_str(v1_cid_str).unwrap();
|
||||
|
||||
let repos = get_test_repos().await;
|
||||
let cursor = repos.repo.get_max_seq().await.unwrap().as_i64();
|
||||
let cursor = flushed_max_seq(repos).await.as_i64();
|
||||
let consumer = FirehoseConsumer::connect_with_cursor(app_port(), cursor).await;
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
|
||||
@@ -203,7 +203,7 @@ async fn test_delete_record_prev_set_cid_none() {
|
||||
let rkey = parts[parts.len() - 1];
|
||||
|
||||
let repos = get_test_repos().await;
|
||||
let cursor = repos.repo.get_max_seq().await.unwrap().as_i64();
|
||||
let cursor = flushed_max_seq(repos).await.as_i64();
|
||||
let consumer = FirehoseConsumer::connect_with_cursor(app_port(), cursor).await;
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
|
||||
@@ -246,7 +246,7 @@ async fn test_five_record_commit_chain_integrity() {
|
||||
let (token, did) = create_account_and_login(&client).await;
|
||||
|
||||
let repos = get_test_repos().await;
|
||||
let cursor = repos.repo.get_max_seq().await.unwrap().as_i64();
|
||||
let cursor = flushed_max_seq(repos).await.as_i64();
|
||||
let consumer = FirehoseConsumer::connect_with_cursor(app_port(), cursor).await;
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
|
||||
@@ -315,7 +315,7 @@ async fn test_apply_writes_single_commit_multiple_ops() {
|
||||
let (token, did) = create_account_and_login(&client).await;
|
||||
|
||||
let repos = get_test_repos().await;
|
||||
let cursor = repos.repo.get_max_seq().await.unwrap().as_i64();
|
||||
let cursor = flushed_max_seq(repos).await.as_i64();
|
||||
let consumer = FirehoseConsumer::connect_with_cursor(app_port(), cursor).await;
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
|
||||
@@ -396,7 +396,7 @@ async fn test_firehose_commit_signature_verification() {
|
||||
};
|
||||
|
||||
let repos = get_test_repos().await;
|
||||
let cursor = repos.repo.get_max_seq().await.unwrap().as_i64();
|
||||
let cursor = flushed_max_seq(repos).await.as_i64();
|
||||
let consumer = FirehoseConsumer::connect_with_cursor(app_port(), cursor).await;
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
|
||||
@@ -444,7 +444,7 @@ async fn test_cursor_backfill_completeness() {
|
||||
let (token, did) = create_account_and_login(&client).await;
|
||||
|
||||
let repos = get_test_repos().await;
|
||||
let baseline_seq = repos.repo.get_max_seq().await.unwrap().as_i64();
|
||||
let baseline_seq = flushed_max_seq(repos).await.as_i64();
|
||||
|
||||
let mut expected_cids: Vec<String> = Vec::with_capacity(5);
|
||||
let texts = [
|
||||
@@ -496,7 +496,7 @@ async fn test_multi_account_seq_interleaving() {
|
||||
let (bob_token, bob_did) = create_account_and_login(&client).await;
|
||||
|
||||
let repos = get_test_repos().await;
|
||||
let cursor = repos.repo.get_max_seq().await.unwrap().as_i64();
|
||||
let cursor = flushed_max_seq(repos).await.as_i64();
|
||||
let consumer = FirehoseConsumer::connect_with_cursor(app_port(), cursor).await;
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
|
||||
|
||||
@@ -1,43 +1,7 @@
|
||||
mod common;
|
||||
use tranquil_pds::comms::{
|
||||
SendError, is_valid_phone_number, is_valid_signal_username, sanitize_header_value,
|
||||
};
|
||||
use tranquil_pds::comms::{SendError, is_valid_phone_number, is_valid_signal_username};
|
||||
use tranquil_pds::image::{ImageError, ImageProcessor};
|
||||
|
||||
#[test]
|
||||
fn test_header_injection_sanitization() {
|
||||
let malicious = "Injected\r\nBcc: attacker@evil.com";
|
||||
let sanitized = sanitize_header_value(malicious);
|
||||
assert!(!sanitized.contains('\r') && !sanitized.contains('\n'));
|
||||
assert!(sanitized.contains("Injected") && sanitized.contains("Bcc:"));
|
||||
|
||||
let normal = "Normal Subject Line";
|
||||
assert_eq!(sanitize_header_value(normal), "Normal Subject Line");
|
||||
|
||||
let padded = " Subject ";
|
||||
assert_eq!(sanitize_header_value(padded), "Subject");
|
||||
|
||||
let multi_newline = "Line1\r\nLine2\nLine3\rLine4";
|
||||
let sanitized = sanitize_header_value(multi_newline);
|
||||
assert!(!sanitized.contains('\r') && !sanitized.contains('\n'));
|
||||
assert!(sanitized.contains("Line1") && sanitized.contains("Line4"));
|
||||
|
||||
let header_injection = "Normal Subject\r\nBcc: attacker@evil.com\r\nX-Injected: value";
|
||||
let sanitized = sanitize_header_value(header_injection);
|
||||
assert_eq!(sanitized.split("\r\n").count(), 1);
|
||||
assert!(
|
||||
sanitized.contains("Normal Subject")
|
||||
&& sanitized.contains("Bcc:")
|
||||
&& sanitized.contains("X-Injected:")
|
||||
);
|
||||
|
||||
let with_null = "client\0id";
|
||||
assert!(sanitize_header_value(with_null).contains("client"));
|
||||
|
||||
let long_input = "x".repeat(10000);
|
||||
assert!(!sanitize_header_value(&long_input).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_phone_number_validation() {
|
||||
assert!(is_valid_phone_number("+1234567890"));
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user