diff --git a/docs/content/docs/api/endpoints.mdx b/docs/content/docs/api/endpoints.mdx index 30a55a4c8..c6b5dee0f 100644 --- a/docs/content/docs/api/endpoints.mdx +++ b/docs/content/docs/api/endpoints.mdx @@ -947,9 +947,9 @@ A key passes the recent-confirmation check the session routes ask for, since it | GET | `/agency/:agencyId/mailboxes` | Staff: [mailboxes across the clients](/guides/agencies/#mailboxes-across-clients) whose mailbox list the caller reads, newest first, as `data` plus `pagination`. Each carries `organization_id`, `client`, `email`, `name`, `provider`, `status`, `erroring`, `warmup`, `warmup_state` (`watch`, `throttled`, `quarantined` or `blocked` when held), `vendor`, `campaign_limit` and `created_at`. Filters: `client` (comma-separated workspace ids; one that is not a client answers `404 agency_client_not_found`), `attention=true` (disconnected, erroring or held), `q` (part of an address), `limit` (1 to 200, default 50) and `cursor` | | GET | `/agency/:agencyId/vendors` | Staff, session only: the [inbox vendor accounts the agency holds](/guides/agencies/#vendor-accounts-held-by-the-agency) as `data`, each with `vendor`, `label`, `status`, `mailboxes` and the `clients` it is used in, never a key; and `catalog`, the supported vendors and what each asks for | | POST | `/agency/:agencyId/vendors` | Owner, admin, session only: `vendor`, `fields` (its API key) and an optional `label`. The key is verified with the vendor before it is stored | -| PATCH | `/agency/:agencyId/vendors/:id` | Owner, admin, session only: `label`, and `fields` to replace the key, in the agency and in every client that uses the account | +| PATCH | `/agency/:agencyId/vendors/:id` | Owner, admin, session only: `label`, and `fields` to replace the key, which every client that uses the account reads from then on | | DELETE | `/agency/:agencyId/vendors/:id` | Owner, admin, session only: forgets the key in the agency and in every client. Mailboxes it connected stay | -| PUT | `/agency/:agencyId/vendors/:id/clients/:orgId` | Staff who manage mailboxes in that client, session only: the client workspace's connection to the account, made the first time, as a vendor connection to import from under `/emails/vendors/:id`. `404 agency_client_not_found` for a workspace that is not a client, `403 agency_client_mailboxes_forbidden` without **Manage mailboxes** there | +| PUT | `/agency/:agencyId/vendors/:id/clients/:orgId` | Staff who manage mailboxes in that client, session only: the client workspace's connection to the account, made the first time, as a vendor connection to import from under `/emails/vendors/:id`. It holds no key of its own. `404 agency_client_not_found` for a workspace that is not a client, `403 agency_client_mailboxes_forbidden` without **Manage mailboxes** there | | PUT | `/agency/:agencyId/brand/domain` | Owner, admin: `domain`, the portal host every client's links are built on (empty clears it); resolves it as part of saving. A malformed host answers `400 invalid_portal_domain`, one another workspace or agency holds `409 portal_domain_taken` | | POST | `/agency/:agencyId/brand/domain/verify` | Owner, admin: re-resolves the portal host | | GET | `/agency/:agencyId/api-keys` | Owner, admin: the agency's [API keys](/api/authentication/#agency-api-keys), newest first, as `data` plus `pagination` (`cursor`, `limit` 1 to 100) | diff --git a/docs/content/docs/development/data-control.mdx b/docs/content/docs/development/data-control.mdx index 138afd2e8..9618841b7 100644 --- a/docs/content/docs/development/data-control.mdx +++ b/docs/content/docs/development/data-control.mdx @@ -180,7 +180,7 @@ Three things a workspace can set up alongside its mailboxes, each kept until the | Table | Holds | Secrets | |---|---|---| | `mailbox_vendor_connections` | An [inbox vendor](/guides/mailbox-import/#import-from-an-inbox-vendor) account: which vendor, a label, its status and last error | The API key and account IDs, sealed with the workspace's data key. No response returns them | -| `agency_vendor_connections` | An inbox vendor account [an agency holds](/guides/agencies/#vendor-accounts-held-by-the-agency) for its clients: which vendor and a label | The API key and account IDs, sealed with `CREDENTIALS_ENCRYPTION_KEY` (an agency has no data key of its own). Each client workspace that uses the account gets its own `mailbox_vendor_connections` row, sealed with that workspace's data key. No response returns them | +| `agency_vendor_connections` | An inbox vendor account [an agency holds](/guides/agencies/#vendor-accounts-held-by-the-agency) for its clients: which vendor and a label | The API key and account IDs, sealed with `CREDENTIALS_ENCRYPTION_KEY` (an agency has no data key of its own). A client workspace that uses the account gets a `mailbox_vendor_connections` row that names it and holds no key: the key is read through the account, and only while the workspace is the agency's client. No response returns them | | `mailbox_domain_grants` | A [Google Workspace or Microsoft 365 admin grant](/guides/mailbox-import/#connect-a-whole-google-workspace-domain): the provider, the Workspace domain or Microsoft tenant ID, the domains it covers, and for Google the admin address used to read the directory | None. No token is stored for a grant or for any mailbox it connected; the backend mints a short-lived one from the instance's own Google service account or Microsoft app each time a worker needs it | | `domain_redirects` | A [root redirect](/guides/sending-domains/#root-redirects): the domain, the website it sends visitors to, the ownership value for its `_warmbly` `TXT` record, the result of the last DNS check and of the last visit to the domain (what answered, and the proxy when it named itself), and whether this instance or Warmbly Cloud serves it. For one Cloud serves, a copy of the records and the tracking host Cloud listed | None. The ownership value is derived from `AUTH_SECRET`, the workspace and the domain, so it stays the same for that workspace and differs for every other workspace and instance | diff --git a/docs/content/docs/guides/agencies.mdx b/docs/content/docs/guides/agencies.mdx index 89d9ea2de..e50695403 100644 --- a/docs/content/docs/guides/agencies.mdx +++ b/docs/content/docs/guides/agencies.mdx @@ -219,11 +219,11 @@ When you buy the mailboxes for your clients, the vendor account is yours, not an - **Any staff member who manages mailboxes in the client can use an account.** Only owners and admins connect, rename, re-key or remove one. - **It works in the client like any vendor connection.** The client's workspace lists the account under its vendor accounts, tagged **Agency**. Rotated passwords are picked up, Google mailboxes can be signed in again through the vendor, and DNS is written for the domains the account holds, as described under [import from an inbox vendor](/guides/mailbox-import/#import-from-an-inbox-vendor). - **Only your staff see what is in it.** The account lists the mailboxes of every client, so a person a client invited to its own workspace sees that the account is there and cannot list or import its mailboxes (`agency_vendor_staff_only`). -- **The key is changed in one place.** **Rename or update key** replaces it in every client that uses the account. It cannot be changed from inside a client's workspace (`agency_vendor_managed`). +- **The key lives in one place.** A client's workspace holds no copy of it: the account's key is read each time it is used, and only while the workspace is still your client. **Rename or update key** therefore takes effect in every client at once. It cannot be changed from inside a client's workspace (`agency_vendor_managed`). - **Removing it keeps the mailboxes.** Removing the account, or removing a client from the agency, ends the use of the account there. The mailboxes it connected stay connected and keep sending; they just stop picking up new passwords from the vendor on their own. -- **It does not leave with a client.** A [workspace export](/guides/workspace-export-import/) of a client carries the connection without your key. +- **It does not leave with a client.** A [workspace export](/guides/workspace-export-import/) of a client carries the connection without a key, because the workspace never held yours. -The accounts are managed through `GET`, `POST`, `PATCH` and `DELETE /agency/:agencyId/vendors`, and used in a client with `PUT /agency/:agencyId/vendors/:id/clients/:orgId`. They answer a signed-in session only, never an API key, because they carry keys. +The accounts are listed with `GET /agency/:agencyId/vendors` and connected with `POST` to the same path, changed and removed with `PATCH` and `DELETE /agency/:agencyId/vendors/:id`, and used in a client with `PUT /agency/:agencyId/vendors/:id/clients/:orgId`. They answer a signed-in session only, never an API key, because they carry keys. ## Client connect links diff --git a/docs/content/docs/guides/workspace-export-import.mdx b/docs/content/docs/guides/workspace-export-import.mdx index 62857e757..aa8e3d210 100644 --- a/docs/content/docs/guides/workspace-export-import.mdx +++ b/docs/content/docs/guides/workspace-export-import.mdx @@ -234,7 +234,7 @@ An import runs as one transaction. If anything fails, nothing lands and the work - **Point your tracking domain at the new instance.** Click links already delivered keep resolving as long as the archive included **Send history** and the domain follows. Tracking domains on mailboxes and campaigns arrive unverified: the destination re-checks mailbox ones hourly, and a campaign's override is verified again from the campaign's settings. Verifying on the destination also needs its ownership `TXT` record: the value belongs to the workspace on the instance that issued it, so publish the one the destination shows, at `_warmbly.` or once at `_warmbly.`. - **Keep the old instance's image host reachable for a while, or re-place the images.** The image library and its files travel, but mail already sent carries the old instance's address inside each ``, so those images keep loading from there. Bodies you edit after the move pick up the new address. - **Register the destination's callback on your own OAuth apps.** On the [Partnership plan](/guides/partnership/), the workspace's own Google and Microsoft apps travel, sealed like a mailbox password, so mailboxes they signed in keep refreshing. Exported without credentials, an app arrives without its secret; save it again under **Settings > Mailbox sign-in**. New sign-ins on the destination need its callback added to each app, unless the app forwards from your own domain. Open sign-in links do not travel; create new ones on the destination. An import into a workspace that already has its own app for the same provider is refused, since a mailbox can only be refreshed by the app that signed it in; remove the destination's app first. If an app forwards its callback from your own domain, point that forwarding at the destination's callback. If its custom redirect points straight at the source instance, clear it or replace it under **Settings > Mailbox sign-in**, and register the destination's callback on the app. A mailbox whose app arrives without its secret is marked as needing a reconnect until you save it. An archive carrying these apps is written in format version 2, which an instance from before this release refuses, so its mailboxes are never imported without the app that refreshes them. -- **Check your inbox vendor connections.** Each vendor's API key is sealed with the workspace's own key, so it travels like a mailbox password: in an export with credentials it is re-sealed for the destination and keeps working. Exported without credentials, the connection arrives without its key; edit it and enter the key again. A vendor reconnect's progress stays on the instance that ran it: on the destination a mailbox shows no reconnect yet, and the [automatic reconnect](/guides/mailboxes/#automatic-reconnect-for-vendor-mailboxes) treats it as never asked. Which Google permissions a mailbox's sign-in carries travels with its token. A connection made from an account [an agency holds](/guides/agencies/#vendor-accounts-held-by-the-agency) never carries the agency's key, with or without credentials: it arrives as the workspace's own connection, waiting for a key. +- **Check your inbox vendor connections.** Each vendor's API key is sealed with the workspace's own key, so it travels like a mailbox password: in an export with credentials it is re-sealed for the destination and keeps working. Exported without credentials, the connection arrives without its key; edit it and enter the key again. A vendor reconnect's progress stays on the instance that ran it: on the destination a mailbox shows no reconnect yet, and the [automatic reconnect](/guides/mailboxes/#automatic-reconnect-for-vendor-mailboxes) treats it as never asked. Which Google permissions a mailbox's sign-in carries travels with its token. A connection made from an account [an agency holds](/guides/agencies/#vendor-accounts-held-by-the-agency) carries no key, with or without credentials, because the workspace never held the agency's: it arrives as the workspace's own connection, waiting for a key. - **Make admin grants again.** A [Google Workspace or Microsoft 365 grant](/guides/mailbox-import/#connect-a-whole-google-workspace-domain) does not travel: it is recorded only after the workspace proves the domain or organization on the instance it runs on, and an archive proves nothing. The mailboxes it connected do travel, but arrive without a grant, so they cannot send or sync until it is made again. On the destination, make the grant again (for Google, the destination's client ID in the Admin console and the proof of the domain; for Microsoft 365, a Global Administrator's consent), then pick those users in the grant's directory, or import a file of their addresses. Each one relinks the mailbox that arrived, with its history and settings, instead of adding a second one. - **Point root redirects at the new instance.** A [root redirect](/guides/sending-domains/#root-redirects) travels, but it arrives unverified and serves nothing until the destination's own check sees both of its records. The ownership value is issued per workspace and instance, so publish the value the destination lists in the `_warmbly` `TXT` record, and update the root's address records. A redirect Warmbly Cloud served for the source instance arrives served by the destination, so its root moves off Cloud the same way. Forwarding set through an inbox vendor lives at the vendor and needs nothing. - **Repoint your forms domain too.** A custom forms domain travels with the archive, but its verification does not: the record still points at the old instance. Update the `CNAME` and publish the ownership `TXT` value the destination shows, and the hourly re-check picks it up. Until then form links fall back to the shared host rather than breaking. diff --git a/internal/app/agencyhome/service.go b/internal/app/agencyhome/service.go index 6bf784f4a..dd168582b 100644 --- a/internal/app/agencyhome/service.go +++ b/internal/app/agencyhome/service.go @@ -170,7 +170,8 @@ func (s *Service) Overview(ctx context.Context, agencyID, userID uuid.UUID) (*mo for _, m := range list { c := models.AgencyOverviewClient{ OrganizationID: m.OrganizationID, Name: m.Name, AvatarURL: m.AvatarURL, Access: m.Access(), - PoolUsed: used[m.OrganizationID], MonthlySendLimit: m.MonthlySendLimit, + ManageMailboxes: m.Can(models.PermManageEmails), + PoolUsed: used[m.OrganizationID], MonthlySendLimit: m.MonthlySendLimit, } if m.Can(models.PermViewCampaigns) { c.Campaigns = orZero(campaigns[m.OrganizationID]) diff --git a/internal/app/agencyhome/service_test.go b/internal/app/agencyhome/service_test.go index 47cff960f..c4f860e4b 100644 --- a/internal/app/agencyhome/service_test.go +++ b/internal/app/agencyhome/service_test.go @@ -417,3 +417,19 @@ func TestMailboxListPagesAndRefusesBadInput(t *testing.T) { t.Fatalf("outsider: %v, calls %v", xerr, out.repo.calls) } } + +func TestOverviewSaysWhereTheCallerCanManageMailboxes(t *testing.T) { + f := newFixture(true) + o, xerr := f.svc.Overview(context.Background(), uuid.New(), uuid.New()) + if xerr != nil { + t.Fatal(xerr) + } + got := map[uuid.UUID]bool{} + for _, c := range o.Clients { + got[c.OrganizationID] = c.ManageMailboxes + } + // Only the client the caller administers; reading an inbox or being restricted is not enough. + if !got[f.full] || got[f.inboxOnly] || got[f.outsider] || got[f.restricted] { + t.Fatalf("manage_mailboxes = %v", got) + } +} diff --git a/internal/app/orgtransfer/export.go b/internal/app/orgtransfer/export.go index 57654d258..84c81449c 100644 --- a/internal/app/orgtransfer/export.go +++ b/internal/app/orgtransfer/export.go @@ -247,11 +247,6 @@ func (s *service) exportRow( if sc.Guard != "" && !jsonBool(obj[sc.Guard]) { continue } - // A borrowed value belongs to whoever lent it, so it stays behind whatever the archive carries. - if sc.Borrowed != "" && jsonString(obj[sc.Borrowed]) != "" { - blankSecret(obj, sc) - continue - } if archiveKey == nil { // Not carrying secrets: blank the column rather than shipping diff --git a/internal/app/orgtransfer/export_test.go b/internal/app/orgtransfer/export_test.go index 0c4588fca..dd4c76d78 100644 --- a/internal/app/orgtransfer/export_test.go +++ b/internal/app/orgtransfer/export_test.go @@ -5,7 +5,6 @@ import ( "encoding/json" "reflect" "slices" - "strings" "testing" ) @@ -109,7 +108,7 @@ func TestArchiveCannotSetTestWorkspaceCategory(t *testing.T) { } } -func TestAnAgencysVendorKeyNeverLeavesInAClientsArchive(t *testing.T) { +func TestAnAgencyVendorLinkArrivesAsTheWorkspacesOwnConnection(t *testing.T) { var table *Table for i := range Tables { if Tables[i].Name == "mailbox_vendor_connections" { @@ -119,24 +118,11 @@ func TestAnAgencysVendorKeyNeverLeavesInAClientsArchive(t *testing.T) { if table == nil { t.Fatal("mailbox_vendor_connections is not registered") } - reset := false - for _, c := range table.ResetOnImport { - reset = reset || c == "agency_connection_id" + // The account it names exists only on the source instance, on a fresh insert and over an existing row. + if !slices.Contains(table.ResetOnImport, "agency_connection_id") || !slices.Contains(table.ResetColumnsOnOverwrite, "agency_connection_id") { + t.Fatalf("agency_connection_id is not reset: import %v, overwrite %v", table.ResetOnImport, table.ResetColumnsOnOverwrite) } - if !reset { - t.Fatal("agency_connection_id names an account of the source instance and has to be reset on import") - } - // An archive that carries secrets still leaves the agency's key behind: the cipher is never asked to open it. - row := []byte(`{"id":"7b1f0d5c-0000-0000-0000-000000000001","vendor":"zapmail","credentials":"sealed-agency-key","agency_connection_id":"7b1f0d5c-0000-0000-0000-0000000000a1"}`) - out, err := (&service{}).exportRow(context.Background(), table, row, nil, []byte("an-archive-key-of-thirty-two-byt"), nil, nil) - if err != nil { - t.Fatal(err) - } - var obj map[string]json.RawMessage - if err := json.Unmarshal(out, &obj); err != nil { - t.Fatal(err) - } - if got := jsonString(obj["credentials"]); got != "" || strings.Contains(string(out), "sealed-agency-key") { - t.Fatalf("the agency's key travelled: %s", out) + if _, ok := ExcludedTables["agency_vendor_connections"]; !ok { + t.Fatal("the agency's accounts, which hold the key, must not travel in a workspace archive") } } diff --git a/internal/app/orgtransfer/spec.go b/internal/app/orgtransfer/spec.go index 9dd3aacba..63a193e07 100644 --- a/internal/app/orgtransfer/spec.go +++ b/internal/app/orgtransfer/spec.go @@ -57,9 +57,6 @@ type SecretColumn struct { // column to hold ciphertext. email_tasks predates unconditional sealing, // so its rows carry a flag rather than a format that can be sniffed. Guard string - // Borrowed, when set, names a column that marks the value as someone else's when it is not null: - // an agency's key used in a client workspace. A borrowed value never travels; the column arrives blank. - Borrowed string // PlaintextPrefix marks a value still stored in the clear from before sealing; it travels as is. PlaintextPrefix string // Plaintext names a column still written in the clear beside this one for the previous release. @@ -220,11 +217,13 @@ var Tables = []Table{ Name: "mailbox_vendor_connections", Group: models.OrgDataGroupCore, Scope: scopeOrg, Secrets: []SecretColumn{ - {Column: "credentials", Domain: KeyDomainOrgDEK, Borrowed: "agency_connection_id"}, + {Column: "credentials", Domain: KeyDomainOrgDEK}, }, - // An agency's account lives on the instance that holds the agency; the connection arrives as the workspace's own, without the agency's key. - ResetOnImport: []string{"agency_connection_id"}, - Note: "Inbox vendor accounts (InboxKit, Zapmail, ...) the workspace imports from. Above email_accounts, whose vendor_connection_id names them. A connection made from an agency's account travels without its key.", + // An agency's account lives on the instance that holds the agency, and its key was never in this row: + // the connection arrives as the workspace's own, waiting for a key. + ResetOnImport: []string{"agency_connection_id"}, + ResetColumnsOnOverwrite: []string{"agency_connection_id"}, + Note: "Inbox vendor accounts (InboxKit, Zapmail, ...) the workspace imports from. Above email_accounts, whose vendor_connection_id names them. A connection made from an agency's account holds no key, so it travels without one.", }, { Name: "mailbox_domain_grants", Group: models.OrgDataGroupCore, diff --git a/internal/app/vendorconn/agency.go b/internal/app/vendorconn/agency.go index 69df40490..a7dd082f2 100644 --- a/internal/app/vendorconn/agency.go +++ b/internal/app/vendorconn/agency.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/google/uuid" + "github.com/warmbly/warmbly/internal/app/mailboximport" "github.com/warmbly/warmbly/internal/errx" "github.com/warmbly/warmbly/internal/models" "github.com/warmbly/warmbly/internal/observability/errs" @@ -14,11 +15,14 @@ import ( ) // An agency holds a vendor account once and uses it in any client workspace. -// The account's key is sealed with the instance key; using it in a client makes -// an ordinary connection in that workspace, sealed with the workspace's own key, -// so imports, reconnects and rotated passwords work there exactly as they do for -// a key the workspace was given directly. Only agency staff browse or import -// from such a connection: the account lists every client's mailboxes. +// The account's key is sealed with the instance key and exists in one place. +// Using the account in a client makes a connection in that workspace that holds +// no key of its own and names the account: the key is read through it each +// time, and only while the workspace is still the agency's client. Imports, +// reconnects and rotated passwords then work there exactly as they do for a key +// the workspace was given directly. A release that does not know such a +// connection finds no key in it and can do nothing with it. Only agency staff +// browse or import from one: the account lists every client's mailboxes. // Agencies is the slice of the agency service that proves staff. type Agencies interface { @@ -31,9 +35,11 @@ type AgencyClients interface { } var ( - errAgencyVendorsOff = errx.New(errx.ServiceUnavailable, "agency vendor accounts are not available on this instance") - errAgencyVendorManage = errx.NewWithIdentifier(errx.Forbidden, "agency_forbidden", "Only the agency's owner and admins can do that.") - errAgencyVendorClient = errx.NewWithIdentifier(errx.NotFound, "agency_client_not_found", "That workspace is not one of this agency's clients.") + errAgencyVendorsOff = errx.New(errx.ServiceUnavailable, "agency vendor accounts are not available on this instance") + errAgencyVendorManage = errx.NewWithIdentifier(errx.Forbidden, "agency_forbidden", "Only the agency's owner and admins can do that.") + errAgencyVendorClient = errx.NewWithIdentifier(errx.NotFound, "agency_client_not_found", "That workspace is not one of this agency's clients.") + errAgencyVendorGone = errx.NewWithIdentifier(errx.BadRequest, mailboximport.ErrIDVendorUnauthorized, + "This vendor account is no longer shared with this workspace by its agency.") errAgencyVendorMailbox = errx.NewWithIdentifier(errx.Forbidden, "agency_client_mailboxes_forbidden", "You cannot manage mailboxes in this client's workspace.") // ErrAgencyVendorStaffOnly refuses a workspace member who is not on the agency's staff. ErrAgencyVendorStaffOnly = errx.NewWithIdentifier(errx.Forbidden, "agency_vendor_staff_only", @@ -85,21 +91,6 @@ func (s *Service) sealInstance(raw string) (string, *errx.Error) { return sealed, nil } -// sealFor seals an already-marshalled field map with a workspace's key. -func (s *Service) sealFor(ctx context.Context, orgID uuid.UUID, raw string) (string, *errx.Error) { - ciph, err := s.cipher.Cipher(ctx, orgID) - if err != nil { - errs.CaptureException(err) - return "", errx.InternalError() - } - sealed, err := ciph.Encrypt(ctx, raw) - if err != nil { - errs.CaptureException(err) - return "", errx.InternalError() - } - return sealed, nil -} - // AgencyAccounts lists the agency's vendor accounts and the clients each is used in. func (s *Service) AgencyAccounts(ctx context.Context, agencyID, userID uuid.UUID) ([]models.AgencyVendorAccount, *errx.Error) { if xerr := s.agencyStaff(ctx, agencyID, userID, false); xerr != nil { @@ -154,9 +145,10 @@ func (s *Service) UpdateAgencyAccount(ctx context.Context, agencyID, userID, id if xerr != nil { return nil, xerr } - raw, sealed := "", "" + sealed := "" if len(in.Fields) > 0 { - if raw, xerr = s.verified(ctx, a.Vendor, in.Fields); xerr != nil { + raw, xerr := s.verified(ctx, a.Vendor, in.Fields) + if xerr != nil { return nil, xerr } if sealed, xerr = s.sealInstance(raw); xerr != nil { @@ -167,22 +159,19 @@ func (s *Service) UpdateAgencyAccount(ctx context.Context, agencyID, userID, id if strings.TrimSpace(in.Label) != "" { label = cleanLabel(in.Label, a.Vendor) } + // One row holds the key, so every client that uses the account has the new one the moment this commits. if _, err := s.agencyRepo.Update(ctx, agencyID, id, label, sealed); err != nil { return nil, errx.InternalError() } + // The name each workspace shows, and a refusal recorded against the old key, follow; neither is a secret. links, err := s.agencyRepo.Links(ctx, id) if err != nil { return nil, errx.InternalError() } for _, l := range links { - workspaceSealed := "" - if raw != "" { - if workspaceSealed, xerr = s.sealFor(ctx, l.OrganizationID, raw); xerr != nil { - return nil, xerr - } - } - if _, err := s.repo.Update(ctx, l.OrganizationID, l.ConnectionID, label, workspaceSealed); err != nil { - return nil, errx.InternalError() + _, _ = s.repo.Update(ctx, l.OrganizationID, l.ConnectionID, label, "") + if sealed != "" { + _ = s.repo.SetStatus(ctx, l.ConnectionID, "active", "") } s.forget(l.ConnectionID) } @@ -256,16 +245,8 @@ func (s *Service) UseAgencyAccount(ctx context.Context, agencyID, userID, id, or return nil, errx.InternalError() } if linked == nil { - raw, derr := s.instance.Decrypt(a.Credentials) - if derr != nil { - errs.CaptureException(derr) - return nil, errx.InternalError() - } - sealed, xerr := s.sealFor(ctx, orgID, raw) - if xerr != nil { - return nil, xerr - } - c := &models.VendorConnection{ID: uuid.New(), OrganizationID: orgID, Vendor: a.Vendor, Label: a.Label, Credentials: sealed} + // No key is copied: the connection names the account and the key is read through it. + c := &models.VendorConnection{ID: uuid.New(), OrganizationID: orgID, Vendor: a.Vendor, Label: a.Label} made, err := s.agencyRepo.CreateLinked(ctx, c, id, userID) if err != nil { return nil, errx.InternalError() diff --git a/internal/app/vendorconn/agency_test.go b/internal/app/vendorconn/agency_test.go index ae0142821..c49f4e7a9 100644 --- a/internal/app/vendorconn/agency_test.go +++ b/internal/app/vendorconn/agency_test.go @@ -86,6 +86,14 @@ func (m *memAgencyVendors) Delete(_ context.Context, agencyID, id uuid.UUID) (bo return true, nil } +func (m *memAgencyVendors) CredentialsFor(_ context.Context, orgID, id uuid.UUID) (string, bool, error) { + a, ok := m.accounts[id] + if !ok || !m.clients[orgID] { + return "", false, nil + } + return a.Credentials, true, nil +} + func (m *memAgencyVendors) Links(_ context.Context, id uuid.UUID) ([]repository.AgencyVendorLink, error) { var out []repository.AgencyVendorLink for _, c := range m.conns.conns { @@ -214,11 +222,11 @@ func TestUsingAnAgencyAccountGivesTheClientItsOwnConnection(t *testing.T) { if stored.OrganizationID != f.acme || stored.AgencyConnectionID == nil || *stored.AgencyConnectionID != a.ID || conn.Credentials != "" { t.Fatalf("connection = %+v", stored) } - // Sealed for the workspace, not copied from the agency's sealed value. - if stored.Credentials == f.accounts.accounts[a.ID].Credentials || strings.Contains(stored.Credentials, "zm_secret") { - t.Fatalf("workspace credentials = %q", stored.Credentials) + // The key is not copied: a release that does not know such a connection finds nothing in it to use. + if stored.Credentials != "" { + t.Fatalf("the workspace's connection holds a key: %q", stored.Credentials) } - // The workspace's connection works like any other: the vendor is asked with the agency's key. + // It works like any other all the same: the vendor is asked with the agency's key, read through the account. boxes, xerr := f.svc.Mailboxes(ctx, f.acme, conn.ID) if xerr != nil || len(boxes) != 1 || f.client.fields["api_key"] != "zm_secret" { t.Fatalf("mailboxes = %+v, %v, asked with %v", boxes, xerr, f.client.fields) @@ -260,7 +268,6 @@ func TestANewKeyReachesEveryClientThatUsesTheAccount(t *testing.T) { if xerr != nil { t.Fatal(xerr) } - before := f.conns.conns[conn.ID].Credentials if _, xerr := f.svc.UpdateAgencyAccount(ctx, f.agency, f.member, a.ID, CreateInput{Label: "x"}); xerr == nil || xerr.Identifier != "agency_forbidden" { t.Fatalf("a staff member who is not an admin: got %v", xerr) @@ -270,12 +277,20 @@ func TestANewKeyReachesEveryClientThatUsesTheAccount(t *testing.T) { t.Fatal(xerr) } stored := f.conns.conns[conn.ID] - if out.Label != "Main Zapmail" || stored.Label != "Main Zapmail" || stored.Credentials == before || len(out.Clients) != 1 { + // One row holds the key, so there is nothing to bring up to date in each client. + if out.Label != "Main Zapmail" || stored.Label != "Main Zapmail" || stored.Credentials != "" || len(out.Clients) != 1 { t.Fatalf("account %+v, connection %+v", out, stored) } if _, xerr := f.svc.Mailboxes(ctx, f.acme, conn.ID); xerr != nil || f.client.fields["api_key"] != "zm_rotated" { t.Fatalf("the client still uses %v (%v)", f.client.fields, xerr) } + // A workspace that left the agency keeps the connection row for a moment at most, and can do nothing with it. + f.accounts.clients[f.acme] = false + f.svc.forget(conn.ID) + if _, xerr := f.svc.Mailboxes(ctx, f.acme, conn.ID); xerr == nil { + t.Fatal("a workspace that left the agency still used its account") + } + f.accounts.clients[f.acme] = true // Deleting the account ends its use in every client. if xerr := f.svc.DeleteAgencyAccount(ctx, f.agency, f.owner, a.ID); xerr != nil || len(f.conns.conns) != 0 || len(f.accounts.accounts) != 0 { t.Fatalf("delete: %v, %d connections left", xerr, len(f.conns.conns)) diff --git a/internal/app/vendorconn/service.go b/internal/app/vendorconn/service.go index 49966d71b..264eaf66c 100644 --- a/internal/app/vendorconn/service.go +++ b/internal/app/vendorconn/service.go @@ -326,9 +326,44 @@ func (s *Service) Delete(ctx context.Context, orgID, id uuid.UUID) *errx.Error { return nil } +// keyOf is the sealed key a connection uses and how to open it. A connection made from an agency's +// account holds none of its own: the agency's is read each time, and only while the workspace is its client. +func (s *Service) keyOf(ctx context.Context, c *models.VendorConnection) (string, func() (string, error), *errx.Error) { + if c.AgencyConnectionID != nil { + if s.agencyRepo == nil || s.instance == nil { + return "", nil, errAgencyVendorGone + } + sealed, ok, err := s.agencyRepo.CredentialsFor(ctx, c.OrganizationID, *c.AgencyConnectionID) + if err != nil { + return "", nil, errx.InternalError() + } + if !ok { + return "", nil, errAgencyVendorGone + } + return sealed, func() (string, error) { return s.instance.Decrypt(sealed) }, nil + } + // An archive imported without credentials brings the connection but not its key. + if c.Credentials == "" { + _ = s.repo.SetStatus(ctx, c.ID, "invalid", "This connection has no API key on this instance. Update the key.") + return "", nil, errx.NewWithIdentifier(errx.BadRequest, mailboximport.ErrIDVendorUnauthorized, + "This "+labelOf(c.Vendor)+" connection has no API key on this instance. Update the key to use it.") + } + return c.Credentials, func() (string, error) { + ciph, err := s.cipher.Cipher(ctx, c.OrganizationID) + if err != nil { + return "", err + } + return ciph.Decrypt(ctx, c.Credentials) + }, nil +} + // client is the connection's vendor client, reused for a while so one list serves many rows. func (s *Service) client(ctx context.Context, c *models.VendorConnection) (mailvendor.Client, *errx.Error) { - sum := sha256.Sum256([]byte(c.Credentials)) + sealed, open, xerr := s.keyOf(ctx, c) + if xerr != nil { + return nil, xerr + } + sum := sha256.Sum256([]byte(sealed)) key := c.ID.String() + ":" + hex.EncodeToString(sum[:8]) if v, ok := s.clients.Load(key); ok { cc := v.(*cachedClient) @@ -337,18 +372,7 @@ func (s *Service) client(ctx context.Context, c *models.VendorConnection) (mailv } s.clients.Delete(key) } - // An archive imported without credentials brings the connection but not its key. - if c.Credentials == "" { - _ = s.repo.SetStatus(ctx, c.ID, "invalid", "This connection has no API key on this instance. Update the key.") - return nil, errx.NewWithIdentifier(errx.BadRequest, mailboximport.ErrIDVendorUnauthorized, - "This "+labelOf(c.Vendor)+" connection has no API key on this instance. Update the key to use it.") - } - ciph, err := s.cipher.Cipher(ctx, c.OrganizationID) - if err != nil { - errs.CaptureException(err) - return nil, errx.InternalError() - } - raw, err := ciph.Decrypt(ctx, c.Credentials) + raw, err := open() if err != nil { errs.CaptureException(err) return nil, errx.InternalError() diff --git a/internal/infrastructure/db/migrations/000389_agency_vendor_accounts.down.sql b/internal/infrastructure/db/migrations/000392_agency_vendor_accounts.down.sql similarity index 100% rename from internal/infrastructure/db/migrations/000389_agency_vendor_accounts.down.sql rename to internal/infrastructure/db/migrations/000392_agency_vendor_accounts.down.sql diff --git a/internal/infrastructure/db/migrations/000389_agency_vendor_accounts.up.sql b/internal/infrastructure/db/migrations/000392_agency_vendor_accounts.up.sql similarity index 85% rename from internal/infrastructure/db/migrations/000389_agency_vendor_accounts.up.sql rename to internal/infrastructure/db/migrations/000392_agency_vendor_accounts.up.sql index dbb6b381a..72f71513c 100644 --- a/internal/infrastructure/db/migrations/000389_agency_vendor_accounts.up.sql +++ b/internal/infrastructure/db/migrations/000392_agency_vendor_accounts.up.sql @@ -13,7 +13,8 @@ CREATE TABLE IF NOT EXISTS agency_vendor_connections ( CREATE INDEX IF NOT EXISTS idx_agency_vendor_connections_agency ON agency_vendor_connections (agency_id); --- A workspace's connection made from an agency's account. It ends with the account; its mailboxes stay. +-- A workspace's connection made from an agency's account. It holds no key of its own (credentials is empty): +-- the account's key is read through it. It ends with the account; its mailboxes stay. ALTER TABLE mailbox_vendor_connections ADD COLUMN IF NOT EXISTS agency_connection_id uuid REFERENCES agency_vendor_connections (id) ON DELETE CASCADE; diff --git a/internal/models/agency_home.go b/internal/models/agency_home.go index 5701e78bf..9031248ac 100644 --- a/internal/models/agency_home.go +++ b/internal/models/agency_home.go @@ -107,10 +107,12 @@ type AgencyClientAdvisor struct { // AgencyOverviewClient is one row of the agency home. A section is nil when the caller lacks its permission. type AgencyOverviewClient struct { - OrganizationID uuid.UUID `json:"organization_id"` - Name string `json:"name"` - AvatarURL *string `json:"avatar_url,omitempty"` - Access AgencyClientAccess `json:"access"` + OrganizationID uuid.UUID `json:"organization_id"` + Name string `json:"name"` + AvatarURL *string `json:"avatar_url,omitempty"` + Access AgencyClientAccess `json:"access"` + // ManageMailboxes reports whether the caller can connect and change mailboxes in the workspace. + ManageMailboxes bool `json:"manage_mailboxes"` PoolUsed int64 `json:"pool_used"` MonthlySendLimit *int `json:"monthly_send_limit,omitempty"` Campaigns *AgencyClientCampaigns `json:"campaigns,omitempty"` diff --git a/internal/repository/pg_agency_vendor.go b/internal/repository/pg_agency_vendor.go index a07ee56e8..edeb69c1a 100644 --- a/internal/repository/pg_agency_vendor.go +++ b/internal/repository/pg_agency_vendor.go @@ -27,11 +27,14 @@ type AgencyVendorRepository interface { // Update renames the account and, when sealed is set, replaces its key. Update(ctx context.Context, agencyID, id uuid.UUID, label, sealed string) (bool, error) Delete(ctx context.Context, agencyID, id uuid.UUID) (bool, error) + // CredentialsFor is an account's sealed key for a workspace that is still the agency's client; false otherwise. + CredentialsFor(ctx context.Context, orgID, id uuid.UUID) (string, bool, error) // Links lists the workspace connections made from an account. Links(ctx context.Context, id uuid.UUID) ([]AgencyVendorLink, error) // Linked is the workspace's connection made from an account; nil when it has none. Linked(ctx context.Context, orgID, id uuid.UUID) (*models.VendorConnection, error) - // CreateLinked stores a workspace connection made from an account; false when the workspace already has one. + // CreateLinked stores a workspace's connection to an account, which holds no key of its own; false when the + // workspace already has one or is not the agency's client. CreateLinked(ctx context.Context, c *models.VendorConnection, agencyConnectionID, createdBy uuid.UUID) (bool, error) } @@ -152,6 +155,21 @@ func (r *agencyVendorRepository) Delete(ctx context.Context, agencyID, id uuid.U return tag.RowsAffected() > 0, nil } +func (r *agencyVendorRepository) CredentialsFor(ctx context.Context, orgID, id uuid.UUID) (string, bool, error) { + var sealed string + err := r.DB.QueryRow(ctx, `SELECT a.credentials FROM agency_vendor_connections a + JOIN agency_clients ac ON ac.agency_id = a.agency_id AND ac.organization_id = $1 + WHERE a.id = $2`, orgID, id).Scan(&sealed) + if errors.Is(err, pgx.ErrNoRows) { + return "", false, nil + } + if err != nil { + db.CaptureError(err, "agency vendor credentials", nil, "queryrow") + return "", false, err + } + return sealed, true, nil +} + func (r *agencyVendorRepository) Links(ctx context.Context, id uuid.UUID) ([]AgencyVendorLink, error) { rows, err := r.DB.Query(ctx, `SELECT organization_id, id FROM mailbox_vendor_connections WHERE agency_connection_id = $1`, id) if err != nil { @@ -185,13 +203,13 @@ func (r *agencyVendorRepository) Linked(ctx context.Context, orgID, id uuid.UUID func (r *agencyVendorRepository) CreateLinked(ctx context.Context, c *models.VendorConnection, agencyConnectionID, createdBy uuid.UUID) (bool, error) { // The workspace must still be the agency's client as the row is written. query := `INSERT INTO mailbox_vendor_connections (id, organization_id, vendor, label, credentials, status, created_by, last_used_at, agency_connection_id) - SELECT $1, ac.organization_id, $3, $4, $5, 'active', $6, now(), a.id + SELECT $1, ac.organization_id, $3, $4, '', 'active', $5, now(), a.id FROM agency_vendor_connections a JOIN agency_clients ac ON ac.agency_id = a.agency_id AND ac.organization_id = $2 - WHERE a.id = $7 + WHERE a.id = $6 ON CONFLICT DO NOTHING RETURNING created_at` - err := r.DB.QueryRow(ctx, query, c.ID, c.OrganizationID, c.Vendor, c.Label, c.Credentials, createdBy, agencyConnectionID).Scan(&c.CreatedAt) + err := r.DB.QueryRow(ctx, query, c.ID, c.OrganizationID, c.Vendor, c.Label, createdBy, agencyConnectionID).Scan(&c.CreatedAt) if errors.Is(err, pgx.ErrNoRows) { return false, nil } diff --git a/qa/scripts/stack.sh b/qa/scripts/stack.sh index a6118b36a..33ae83957 100755 --- a/qa/scripts/stack.sh +++ b/qa/scripts/stack.sh @@ -261,17 +261,20 @@ seed() { # ── lifecycle ──────────────────────────────────────────────────────────── up() { - local c + local c want_mock=0 for c in docker go pnpm setsid curl; do command -v "$c" >/dev/null || die "$c is required"; done load_ports load_mode "${1:-}" + if vendor_mock; then want_mock=1; fi if alive backend && alive web; then - if [ "$(cat "$RUN/mode" 2>/dev/null)" = "$MODE" ]; then status; return 0; fi + # The same mode with the mock inbox vendors switched on or off is a different stack: it is restarted. + if [ "$(cat "$RUN/mode" 2>/dev/null)" = "$MODE" ] && [ "$(cat "$RUN/vendor-mock" 2>/dev/null || echo 0)" = "$want_mock" ]; then status; return 0; fi say "switching to $MODE" down >/dev/null fi heal_ports echo "$MODE" >"$RUN/mode" + echo "$want_mock" >"$RUN/vendor-mock" docker ps --format '{{.Names}}' | grep -q '^warmbly-postgres-1$' || (cd "$REPO" && make infra) until docker exec warmbly-postgres-1 pg_isready -U warmbly >/dev/null 2>&1; do sleep 1; done diff --git a/web/src/app/app/agency/mailboxes/page.tsx b/web/src/app/app/agency/mailboxes/page.tsx index dae9bc532..d2a0b7e33 100644 --- a/web/src/app/app/agency/mailboxes/page.tsx +++ b/web/src/app/app/agency/mailboxes/page.tsx @@ -59,8 +59,28 @@ function standing(m: AgencyMailbox): { label: string; cls: string } { return { label: "Connected", cls: "bg-emerald-50 text-emerald-700" }; } +/** A list that could not be loaded says so and offers another try, instead of looking empty. */ +function LoadFailed({ what, error, onRetry }: { what: string; error: unknown; onRetry: () => void }) { + return ( +
+

+ We could not load {what}. {buildError(error as AppError)} +

+ +
+ ); +} + function VendorAccounts({ accounts, + failed, + onRetry, canManage, onConnect, onAdd, @@ -68,6 +88,9 @@ function VendorAccounts({ onRemove, }: { accounts: AgencyVendorAccount[]; + /** The accounts could not be loaded; nothing can be connected until they are. */ + failed: unknown; + onRetry: () => void; canManage: boolean; onConnect: () => void; onAdd: (a: AgencyVendorAccount) => void; @@ -76,14 +99,16 @@ function VendorAccounts({ }) { return ( <> - - {canManage && ( + + {canManage && !failed && ( } onClick={onConnect}> Connect a vendor )} - {accounts.length === 0 ? ( + {failed ? ( + + ) : accounts.length === 0 ? (

Bought mailboxes from Zapmail, InboxKit, Mailforge or another vendor for your clients? Connect the account once and hand @@ -228,6 +253,8 @@ function AgencyMailboxes() { void vendors.refetch()} canManage={canManage} onConnect={() => setVendorDialog({})} onAdd={(a) => setAdding({ accountId: a.id })} @@ -275,6 +302,8 @@ function AgencyMailboxes() {

+ ) : mailboxes.isError && rows.length === 0 ? ( + void mailboxes.refetch()} /> ) : rows.length === 0 ? ( { - if (e.key === "Enter" && e.target === e.currentTarget) go(); + if ((e.key === "Enter" || e.key === " ") && e.target === e.currentTarget) { + e.preventDefault(); + go(); + } }} title={`Open in ${m.client}`} className="min-h-11 px-5 py-1.5 flex flex-wrap md:flex-nowrap items-center gap-x-3 gap-y-1 border-b border-slate-200/60 hover:bg-slate-50/80 cursor-pointer" diff --git a/web/src/components/app/agency/mailboxes/AddMailboxesDialog.tsx b/web/src/components/app/agency/mailboxes/AddMailboxesDialog.tsx index b0f8ff213..9792ef653 100644 --- a/web/src/components/app/agency/mailboxes/AddMailboxesDialog.tsx +++ b/web/src/components/app/agency/mailboxes/AddMailboxesDialog.tsx @@ -74,7 +74,7 @@ export default function AddMailboxesDialog({ React.useEffect(() => { if (!open) return; // One client needs no picking. - setOrgId(!presetAccountId && clients.length === 1 && clients[0].access === "full" ? clients[0].organization_id : null); + setOrgId(!presetAccountId && clients.length === 1 && clients[0].manage_mailboxes ? clients[0].organization_id : null); setSearch(""); }, [open, presetAccountId, clients]); @@ -127,20 +127,21 @@ export default function AddMailboxesDialog({ {clients.length > 6 && }
{shown.map((c) => { - const limited = c.access !== "full"; + // Adding mailboxes needs the permission to manage them there, not only access to the workspace. + const limited = !c.manage_mailboxes; return ( ); diff --git a/web/src/components/app/agency/mailboxes/VendorAccountDialog.tsx b/web/src/components/app/agency/mailboxes/VendorAccountDialog.tsx index b1cd63e67..9fd53d9d8 100644 --- a/web/src/components/app/agency/mailboxes/VendorAccountDialog.tsx +++ b/web/src/components/app/agency/mailboxes/VendorAccountDialog.tsx @@ -34,12 +34,21 @@ export default function VendorAccountDialog({ const [label, setLabel] = React.useState(""); const [fields, setFields] = React.useState>({}); + // Reset when it opens or moves to another account; a refetch of the same account keeps what was typed. + const accountId = account?.id; + const start = React.useRef(account); + start.current = account; React.useEffect(() => { if (!open) return; - setVendorId(account?.vendor ?? ""); - setLabel(account?.label ?? ""); + setVendorId(start.current?.vendor ?? ""); + setLabel(start.current?.label ?? ""); setFields({}); - }, [open, account]); + }, [open, accountId]); + + // A save in flight belongs to this dialog: it cannot be closed, and reopened for another account, under it. + const close = () => { + if (!save.isPending) onClose(); + }; const vendor = catalog.find((v) => v.id === vendorId); const typed = Object.values(fields).some((v) => v.trim() !== ""); @@ -68,14 +77,14 @@ export default function VendorAccountDialog({ return ( } footer={ <> {problem && {problem}} -