dovecot-config.test.js 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import assert from 'node:assert/strict';
  2. import { readFileSync } from 'node:fs';
  3. import { test } from 'node:test';
  4. const compose = readFileSync(new URL('../docker-compose.yml', import.meta.url), 'utf8');
  5. const authConfig = readFileSync(new URL('../docker/dovecot/auth.conf', import.meta.url), 'utf8');
  6. const mailConfig = readFileSync(new URL('../docker/dovecot/mailhub.conf', import.meta.url), 'utf8');
  7. const sslConfig = readFileSync(new URL('../docker/dovecot/ssl.conf', import.meta.url), 'utf8');
  8. const authLua = readFileSync(new URL('../docker/dovecot/auth.lua', import.meta.url), 'utf8');
  9. test('Compose delegates public IMAP and POP3 ports to rootless Dovecot', () => {
  10. assert.match(compose, /image: dovecot\/dovecot:2\.4\.4/);
  11. for (const mapping of ['143:31143', '993:31993', '110:31110', '995:31995']) {
  12. assert.ok(compose.includes(mapping), `missing Dovecot port mapping ${mapping}`);
  13. }
  14. for (const oldMapping of ['143:143', '993:993', '110:110', '995:995']) {
  15. assert.equal(compose.includes(oldMapping), false, `app still owns ${oldMapping}`);
  16. }
  17. assert.match(compose, /dovecot_internal:\n\s+internal: true/);
  18. assert.match(compose, /file: \.\/data\/secrets\/dovecot_auth_secret/);
  19. assert.match(compose, /MAIL_ACCESS_BACKEND: dovecot/);
  20. assert.match(compose, /MAILDIR_ROOT: \/data\/maildir/);
  21. assert.match(compose, /test -r \/run\/secrets\/dovecot_auth_secret/);
  22. assert.match(compose, /test -s \/run\/secrets\/dovecot_auth_secret/);
  23. assert.match(compose, /test -w \/srv\/vmail/);
  24. assert.match(compose, /doveadm service status imap-login pop3-login/);
  25. });
  26. test('Dovecot uses Lua passdb, a static rootless userdb, and Maildir storage', () => {
  27. assert.match(authConfig, /passdb lua \{/);
  28. assert.match(authConfig, /SUBMISSION_TLS_CERT = %\{env:SUBMISSION_TLS_CERT\}/);
  29. assert.match(authConfig, /SUBMISSION_TLS_KEY = %\{env:SUBMISSION_TLS_KEY\}/);
  30. assert.match(authConfig, /lua_file = \/etc\/dovecot\/auth\.lua/);
  31. assert.match(authConfig, /userdb static \{/);
  32. assert.match(authConfig, /userdb static \{[\s\S]*allow_all_users = yes/);
  33. assert.match(authConfig, /uid = 1000/);
  34. assert.match(authConfig, /gid = 1000/);
  35. assert.match(authConfig, /home = \/srv\/vmail\/%\{user \| lower\}/);
  36. assert.match(mailConfig, /^protocols = imap pop3$/m);
  37. assert.match(mailConfig, /^mail_driver = maildir$/m);
  38. assert.match(mailConfig, /^mail_path = ~\/mail$/m);
  39. assert.match(mailConfig, /^mailbox_list_layout = maildir\+\+$/m);
  40. assert.match(mailConfig, /^mailbox_list_storage_escape_char = \^$/m);
  41. assert.match(mailConfig, /^mailbox_list_utf8 = no$/m);
  42. for (const [mailbox, specialUse] of [
  43. ['Archive', 'Archive'],
  44. ['Drafts', 'Drafts'],
  45. ['Junk', 'Junk'],
  46. ['Sent', 'Sent'],
  47. ['Trash', 'Trash']
  48. ]) {
  49. assert.match(
  50. mailConfig,
  51. new RegExp(`mailbox ${mailbox} \\{[\\s\\S]*?auto = subscribe[\\s\\S]*?special_use = \\\\${specialUse}`)
  52. );
  53. }
  54. assert.match(mailConfig, /service imap-login \{[\s\S]*chroot =/);
  55. assert.match(mailConfig, /service imap-login \{[\s\S]*inet_listener imaps \{[\s\S]*ssl = yes/);
  56. assert.match(mailConfig, /service pop3-login \{[\s\S]*inet_listener pop3s \{[\s\S]*ssl = yes/);
  57. assert.match(sslConfig, /^ssl_server_cert_file = \$ENV:SUBMISSION_TLS_CERT$/m);
  58. assert.match(sslConfig, /^ssl_server_key_file = \$ENV:SUBMISSION_TLS_KEY$/m);
  59. });
  60. test('Lua passdb sends both IMAP and POP3 to the private auth bridge', () => {
  61. assert.match(authLua, /http:\/\/app:3001\/internal\/dovecot\/auth/);
  62. assert.match(authLua, /\/run\/secrets\/dovecot_auth_secret/);
  63. assert.match(authLua, /first_nonempty_string\(request\.protocol, request\.service\)/);
  64. assert.match(authLua, /request\.remote_ip,[\s\S]*request\.real_remote_ip/);
  65. assert.match(authLua, /protocol ~= "imap" and protocol ~= "pop3"/);
  66. assert.match(authLua, /request_max_attempts = 1/);
  67. assert.match(authLua, /auto_retry = "no"/);
  68. assert.match(authLua, /request_absolute_timeout = "2s"/);
  69. assert.match(authLua, /status ~= 200[\s\S]*PASSDB_RESULT_INTERNAL_FAILURE/);
  70. assert.doesNotMatch(authLua, /status == (?:401|403|404)/);
  71. assert.match(
  72. authLua,
  73. /payload\.authenticated == false[\s\S]*PASSDB_RESULT_PASSWORD_MISMATCH[\s\S]*payload\.authenticated ~= true[\s\S]*PASSDB_RESULT_INTERNAL_FAILURE/
  74. );
  75. assert.match(authLua, /valid_user\(payload\.user\)/);
  76. assert.match(authLua, /PASSDB_RESULT_OK, \{ user = string\.lower\(payload\.user\) \}/);
  77. assert.doesNotMatch(authLua, /log_(?:debug|info|warning|error).*password/i);
  78. });