Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

14.5 KB
Raw Blame

Backend commands run from backend/, frontend commands from frontend/, website commands from website/. Package tests use go test ./internal/<path>/... -count=1, as the root AGENTS.md recommends; the full make test (or make test-fedora on a Fedora host) runs once, in section 7.

1. Shared SMTP transport

  • 1.1 Create backend/internal/util/smtp_transport with the Security enum (tls, starttls, none) in enums.go, sentinel errors in errors.go, the Connection and Message types, and Send(ctx, Connection, Message), which opens the connection in the chosen mode and greets with the configured name in every mode. Verify with tests against in-process fake SMTP servers: tls delivers, starttls upgrades before any other command, starttls against a server that does not offer the upgrade fails with ErrStartTLSNotOffered and closes the socket having sent nothing past EHLO, not even QUIT, none never upgrades, and an untrusted certificate fails unless verification is switched off.
  • 1.2 Implement PLAIN and LOGIN authentication inside the package. Choose the mechanism from the server's AUTH offer (PLAIN first, then LOGIN), refuse either one on an unencrypted connection unless the mode is none, match LOGIN prompts from both current prompt sets without regard to case, and fail on any other prompt. Verify with fake-server tests: a stripped STARTTLS offer never produces an AUTH command, PLAIN is used when both are offered, LOGIN is used when only it is offered, an unknown prompt ends the attempt, and an authentication error carries the server's response but not the password.
  • 1.3 Build messages from parsed addresses: From and To through net/mail.Address, the subject with CR, LF and NUL removed and then Q-encoded, Message-ID on the sender's domain, MIME-Version: 1.0, Content-Type: text/html; charset="UTF-8" and a quoted-printable body. Verify with tests that read the message a fake server received: a subject containing \r\nBcc: [email protected] produces no Bcc header and a single recipient, a non-ASCII display name decodes back to the original, a 5,000-character body line arrives unchanged after decoding with no wire line over 998 characters, and two messages carry different Message-ID values.
  • 1.4 Bound the whole conversation with a 30-second deadline derived from the caller's context and applied to both the dial and the connection. Verify with a test against a server that greets and then goes silent, using a shorter caller deadline so the test runs quickly, and assert that Send returns a timeout error within that deadline.
  • 1.5 Add GetDefaultSecurityForPort, ParseSender (a bare address or a display name with an address, with Databasus as the default name), ResolveSender(from, username, host) (the explicit sender, else the username only if it parses as a bare address, else noreply@ and the host), ValidateHeloName (a host name or an address literal) and GetDefaultHeloName(databasusURL) (the host part only, then the machine host name, then localhost; an IPv4 address as [192.0.2.1], an IPv6 address as [IPv6:2001:db8::1]). Verify with table tests for each function, including a URL with a port, an IPv4 and an IPv6 host, a sender such as ops at example, which must be rejected, a username apikey resolving to noreply@<host>, and greeting names my relay and [::1], which must be rejected.
  • 1.6 Run make lint and go test ./internal/util/smtp_transport/... -count=1.

2. Instance mail server

  • 2.1 Add SMTP_SECURITY and SMTP_HELO_NAME to backend/internal/config/config.go. Move the SMTP startup checks into a function that returns an error, so the existing port check, the security value check, the SMTP_HELO_NAME check and the SMTP_FROM check can be tested without exiting the process. Verify with tests for an unknown SMTP_SECURITY, an invalid SMTP_HELO_NAME, an unreadable SMTP_FROM, a display-name SMTP_FROM, which must be accepted, and a host without a port.
  • 2.2 Rebuild backend/internal/features/email on the transport. The singleton builds a Connection from the environment, resolves the sender through ResolveSender, IsConfigured() becomes SMTPHost != "", SendEmail takes a context, and the unconfigured path keeps logging a skip. Delete the old connection, authentication and header code together with its loginAuth, and move the existing TLS test to the transport package if the transport tests do not already cover it. Verify with go test ./internal/features/email/... -count=1 and make lint.
  • 2.3 Add a context parameter to SendEmail in users/interfaces/interfaces.go, workspaces/interfaces/interfaces.go and both mocks. Pass the request context from the sign-in code send (two_factor_auth.go:246), the password reset send (user_services.go:590) and the invitation send (membership_service.go:110). Verify with go test ./internal/features/users/... ./internal/features/workspaces/... -count=1.
  • 2.4 Change the condition in generate_frontend_runtime_configuration in docker/start.sh to SMTP_HOST alone. Verify by running that function in a shell with SMTP_HOST set and DATABASUS_URL unset and reading IS_EMAIL_CONFIGURED: 'true' in the generated file.
  • 2.5 Add SMTP_SECURITY=none to .env.example next to the other SMTP lines, after the user confirms this edit explicitly, because .env* files change only with that confirmation. Its test-mailpit host is provided by no compose file, so the line matters only to a developer who points the block at a local Mailpit. Verify that the backend still starts with the resulting .env and that the users and workspaces package tests from 2.3 pass.

3. Email notification channel

  • 3.1 Create the migration with make migration-create name=add_security_to_email_notifiers. Add security with a temporary starttls default, set tls where smtp_port = 465, drop the default, add the CHECK constraint in its own statement, and add helo_name with an empty default. The down step drops the constraint and both columns. Verify with make migration-up and make migration-down on a scratch database, and by checking that a row on port 465 comes out as tls and a row on port 587 as starttls.
  • 3.2 Add Security and HeloName to EmailNotifier. In Validate, require a known security mode, a readable From, a TargetEmail that parses as a single bare address and a HeloName that is empty or passes ValidateHeloName. In Update, keep the stored Security when the incoming one is empty and copy HeloName as given. Add a FillDefaults step on Notifier that delegates to the email model and sets an empty Security from the port, and call it before Validate in the create branch of SaveNotifier and the unsaved branch of SendTestNotificationToNotifier in notifiers/service.go. Rewrite Send to build a Connection from the row, with the sender from ResolveSender and the default greeting name from DATABASUS_URL, and call the transport with context.Background(). Delete the old SMTP code, getHelloName, sanitizeHeaderValue and auth.go. Verify with the Mailpit test in model_test.go switched to none, plus a new test that the same Mailpit server with starttls fails with the STARTTLS error.
  • 3.3 Add notifier controller tests: a channel whose sender is ops at example is refused on save, a display-name sender is accepted, an unknown security mode is refused, a recipient ops at example and a greeting name my relay are refused, a channel created on port 587 without security is saved with starttls, an update without security keeps the stored mode, a test notification for an unsaved channel without security is not refused by validation, and security and heloName come back as saved. Verify with go test ./internal/features/notifiers/... -count=1.
  • 3.4 Run make swagger and make lint.

4. Test message endpoint

  • 4.1 Turn checkRateLimitOrRespond (user_controller.go:426-443) into a package-level function in users/controllers that takes the counter and the logger, and call it from UserController. Verify that the existing sign-in and password reset rate-limit tests pass unchanged.
  • 4.2 Add ErrEmailNotConfigured and ErrAdminEmailMissing and SettingsService.SendTestEmail(ctx, admin). It checks configuration, checks the address with the validator already in settings_service.go and with net/mail.ParseAddress, sends a short test message and writes one audit entry with the outcome, whatever stage a failure happens at. Add POST /users/settings/test-email to SettingsController behind RequireRole(UserRoleAdmin) and the test_email rate limit of five per administrator per ten minutes, and give the controller the counter and a logger through positional DI. Answer 200 with recipientEmail on success, 400 with email_not_configured or admin_email_missing on a refusal, and 400 with the delivery error and no code on a failed send. In the same file, correct the GetUsersSettings description, which claims the endpoint is for administrators only. Verify with make swagger and make lint.
  • 4.3 Add controller tests for the endpoint: a successful send reaches the mock sender at the administrator's address and writes a "sent" audit entry, a member gets 403, an unconfigured sender gets email_not_configured and nothing is sent, an administrator with the bootstrap login gets admin_email_missing, a sixth request within the window gets 429, and a failing sender returns its error text and writes a "failed" audit entry. Verify with go test ./internal/features/users/... -count=1.

5. Frontend

  • 5.1 In entity/notifiers, add the EmailNotifierSecurity enum in its own file, EMAIL_NOTIFIER_SECURITY_LABEL_KEYS, security and heloName on EmailNotifier, getDefaultEmailNotifierSecurity(port) using the backend's port rule, and getSecurityAfterPortChange(currentSecurity, previousPort, nextPort), which returns the next port's default only when the current mode equals the previous port's default. Export them through the slice's index.ts. Verify with pnpm test cases for the port rule and for the port change: starttls on 587 becomes tls on 465, and none on 25 stays none on 2525.
  • 5.2 Add the security select and the greeting-name input to the advanced section of EditEmailNotifierComponent.tsx. Open the section by itself when either field differs from its default, and apply getSecurityAfterPortChange on every port change. Verify in the running application: creating a channel on port 465 saves tls, editing a migrated channel shows its stored mode, and a channel stored with none keeps none after only its port is changed and it is saved.
  • 5.3 Add sendTestEmail to entity/users/api/settingsApi.ts with its response type in its own model file. Call fetchPostJson with the default isRetryOnError = false, so a failed send is not repeated. Map email_not_configured and admin_email_missing in the API error code table used by translateApiError. Verify with pnpm build.
  • 5.4 Add MailServerComponent in features/settings/ui and render it in SettingsComponent.tsx directly above AuditLogsComponent. It shows the configured state from the settings response, the documentation link through getWebsitePageUrl('advancedConfigEmailSmtp', locale), and the note that notification channels have their own SMTP settings. It also has a type="primary" ghost "Send test email" button that is disabled when the mail server is not configured, confirms delivery with the recipient address and shows failures through translateApiError. Verify in the running application with the mail server configured against Mailpit, with it unconfigured, and with a wrong password.
  • 5.5 Add the new copy to all six dictionaries in shared/i18n/locales/, using the interface terms from website/AGENTS.md and a plain hyphen in English. Verify with pnpm test, which runs the placeholder check in dictionaries.test.ts, and pnpm build, which fails on a missing key.
  • 5.6 Run pnpm format, pnpm lint, pnpm test and pnpm build.

6. Documentation

  • 6.1 Rewrite the SMTP section of website/app/(en)/advanced-config/page.tsx under the unchanged email-smtp anchor. It covers the rule that SMTP_HOST makes the mail server configured, the sender fallback that skips a username which is not an address, a warning that an instance requiring the emailed second factor must set SMTP_SECURITY before upgrading if its relay offers no STARTTLS, with the console command that switches the second factor off, the variable table with SMTP_SECURITY, SMTP_HELO_NAME and the display-name form of SMTP_FROM, the three security modes with the default for each port and a warning that none sends credentials and messages unencrypted, the advice to use none for a relay without STARTTLS, DATABASUS_URL as optional (invitation links and the default greeting name), and the difference between the instance mail server and an email notification channel. Update the page description and metadata if they mention the old rule. Verify with npm run build and npm run lint.
  • 6.2 Apply the same section to the five copies in website/app/[lang]/advanced-config/content/, keeping the hierarchy, the anchor and every technical string byte-identical to English, and following the translation rules in website/AGENTS.md. Verify with npm run build and by checking that out/{ru,es,pt,zh,fr}/advanced-config/index.html each contain the email-smtp anchor and the SMTP_SECURITY row.

7. Verification and review

  • 7.1 Run make test in backend/ (make test-fedora on a Fedora host) and make lint, then pnpm lint, pnpm test and pnpm build in frontend/, and npm run lint and npm run build in website/.
  • 7.2 In the running application, reproduce the reported situation. No compose file provides a Mailpit, so start one first: docker run -d --name databasus-mailpit -p 1025:1025 -p 8025:8025 -e MP_SMTP_AUTH_ACCEPT_ANY=true -e MP_SMTP_AUTH_ALLOW_INSECURE=true axllent/mailpit:v1.20.5, the image and settings the backend tests use. With a notification channel configured and no SMTP_HOST, the settings screen shows the mail server as not configured and the button is disabled. After SMTP_HOST=localhost, SMTP_PORT=1025 and SMTP_SECURITY=none are set, the test message arrives, and a workspace invitation arrives with Databasus as the sender name and a Message-ID.
  • 7.3 Pass the mandatory post-implementation review that the hooks run, resolve every finding, and list refactor suggestions for the diff as the root AGENTS.md requires.