mirror of
https://github.com/warmbly/warmbly.git
synced 2026-11-29 16:06:31 +00:00
* feat: offer fleet nodes the backend's private TLS address through opt-in NODE_BACKEND_INTERNAL_URL and NODE_BACKEND_INTERNAL_CA (rendered as WARMBLY_BACKEND_INTERNAL_URL and a base64 CA beside the unchanged public address), have workers and consumers probe it at startup and route every control-plane client through one CA-pinned transport or fall back to the public URL, and add the workers root's backend_internal_port rule and the runtime root's nodes_private_backend switch * feat: replace a node's private control-plane connections every 2 minutes over HTTP/1.1 so new backend tasks behind the Cloud Map name get node traffic, resend a request whose private dial, DNS or TLS handshake failed once to the public address (replayable bodies only, never after the request was written), and route all node calls to the public address for 5 minutes after 3 dial failures in a row, logging each switch once * feat: keep a node's control-plane call from being resent to the public address once any private attempt wrote its headers, send the caller's own body on a first send during a public window, and mark the backend's NODE_BACKEND_INTERNAL_URL and CA settings as needing a backend restart in the configuration docs
206 lines
7.5 KiB
Go
206 lines
7.5 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/warmbly/warmbly/internal/pkg/nodeauth"
|
|
)
|
|
|
|
// StoredFolderMessage is one message the platform holds for a mailbox folder:
|
|
// the UID it was filed under, the internal row id, and its RFC Message-ID.
|
|
// The IMAP drafts reconciliation diffs this against the folder's current UID
|
|
// set, so it can remove the rows an expunge left behind.
|
|
type StoredFolderMessage struct {
|
|
UID uint32 `json:"uid"`
|
|
ID uuid.UUID `json:"id"`
|
|
MessageID string `json:"message_id"`
|
|
}
|
|
|
|
// ProviderFolderMessage is one message the platform holds for a mailbox whose
|
|
// provider keys messages by id (Gmail): the row, that id, the folder the
|
|
// provider last had it in, and its date. The Gmail folder reconciliation
|
|
// checks these against where Gmail has each message now.
|
|
type ProviderFolderMessage struct {
|
|
ID uuid.UUID `json:"id"`
|
|
ProviderID string `json:"provider_id"`
|
|
MessageID string `json:"message_id,omitempty"`
|
|
ProviderFolder string `json:"provider_folder"`
|
|
InternalDate time.Time `json:"internal_date"`
|
|
Flags []string `json:"flags,omitempty"`
|
|
}
|
|
|
|
// SyncContextRepository is the worker's view of the control plane's answer to
|
|
// "is this a conversation the mailbox owns?" and "what does the platform still
|
|
// hold for this folder?". Workers cannot reach Postgres, so the only
|
|
// implementation is the HTTP proxy below.
|
|
type SyncContextRepository interface {
|
|
IsOwnConversation(ctx context.Context, userID, emailID uuid.UUID, messageIDs []string, threadID string) (bool, error)
|
|
// ListFolderMessages returns the platform's rows for one folder in the
|
|
// current UIDVALIDITY generation. uidValidity scopes it so rows whose
|
|
// UIDs a UIDVALIDITY change voided are not compared, and therefore not
|
|
// deleted.
|
|
ListFolderMessages(ctx context.Context, userID, emailID uuid.UUID, folderPath string, uidValidity uint32) ([]StoredFolderMessage, error)
|
|
// ListProviderFolderMessages returns the newest rows the provider last
|
|
// placed in one of folders, at most limit of them.
|
|
ListProviderFolderMessages(ctx context.Context, userID, emailID uuid.UUID, folders []string, limit int) ([]ProviderFolderMessage, error)
|
|
ListProviderMessages(ctx context.Context, userID, emailID uuid.UUID, after *uuid.UUID, limit int) ([]ProviderFolderMessage, error)
|
|
}
|
|
|
|
var ErrSyncContextUnsupported = errors.New("provider message enumeration unavailable; upgrade backend first")
|
|
|
|
func (r *httpSyncContextRepository) ListProviderMessages(ctx context.Context, userID, emailID uuid.UUID, after *uuid.UUID, limit int) ([]ProviderFolderMessage, error) {
|
|
q := url.Values{}
|
|
q.Set("user_id", userID.String())
|
|
q.Set("email_id", emailID.String())
|
|
q.Set("limit", strconv.Itoa(limit))
|
|
if after != nil {
|
|
q.Set("after", after.String())
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.baseURL+"/api/v1/internal/sync/provider-messages?"+q.Encode(), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
nodeauth.Authorize(req, r.token)
|
|
req.Header.Set("User-Agent", "warmbly-worker/sync-context-http")
|
|
resp, err := r.client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusMethodNotAllowed {
|
|
return nil, ErrSyncContextUnsupported
|
|
}
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, &ControlPlaneHTTPError{Status: resp.StatusCode}
|
|
}
|
|
var out struct {
|
|
Messages []ProviderFolderMessage `json:"messages"`
|
|
}
|
|
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
|
|
return nil, err
|
|
}
|
|
return out.Messages, nil
|
|
}
|
|
|
|
type httpSyncContextRepository struct {
|
|
baseURL string
|
|
token string
|
|
client *http.Client
|
|
}
|
|
|
|
// NewHTTPSyncContextRepository returns the worker-side proxy for
|
|
// GET {BaseURL}/api/v1/internal/sync/own-conversation,
|
|
// GET {BaseURL}/api/v1/internal/sync/folder-messages and
|
|
// GET {BaseURL}/api/v1/internal/sync/provider-folder-messages.
|
|
func NewHTTPSyncContextRepository(baseURL, token string, opts ...ControlPlaneOption) (SyncContextRepository, error) {
|
|
if baseURL == "" {
|
|
return nil, errors.New("sync_context.http: baseURL is required")
|
|
}
|
|
if token == "" {
|
|
return nil, errors.New("sync_context.http: token is required")
|
|
}
|
|
return &httpSyncContextRepository{
|
|
baseURL: strings.TrimRight(baseURL, "/"),
|
|
token: token,
|
|
client: newControlPlaneClient(10*time.Second, opts),
|
|
}, nil
|
|
}
|
|
|
|
func (r *httpSyncContextRepository) IsOwnConversation(ctx context.Context, userID, emailID uuid.UUID, messageIDs []string, threadID string) (bool, error) {
|
|
q := url.Values{}
|
|
q.Set("user_id", userID.String())
|
|
q.Set("email_id", emailID.String())
|
|
q.Set("message_ids", strings.Join(messageIDs, ","))
|
|
q.Set("thread_id", threadID)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.baseURL+"/api/v1/internal/sync/own-conversation?"+q.Encode(), nil)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
nodeauth.Authorize(req, r.token)
|
|
req.Header.Set("User-Agent", "warmbly-worker/sync-context-http")
|
|
resp, err := r.client.Do(req)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return false, &ControlPlaneHTTPError{Status: resp.StatusCode}
|
|
}
|
|
var out struct {
|
|
Own bool `json:"own"`
|
|
}
|
|
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
|
|
return false, err
|
|
}
|
|
return out.Own, nil
|
|
}
|
|
|
|
// ListFolderMessages asks the control plane what it still holds for one folder.
|
|
func (r *httpSyncContextRepository) ListFolderMessages(ctx context.Context, userID, emailID uuid.UUID, folderPath string, uidValidity uint32) ([]StoredFolderMessage, error) {
|
|
q := url.Values{}
|
|
q.Set("user_id", userID.String())
|
|
q.Set("email_id", emailID.String())
|
|
q.Set("folder_path", folderPath)
|
|
q.Set("uid_validity", strconv.FormatUint(uint64(uidValidity), 10))
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.baseURL+"/api/v1/internal/sync/folder-messages?"+q.Encode(), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
nodeauth.Authorize(req, r.token)
|
|
req.Header.Set("User-Agent", "warmbly-worker/sync-context-http")
|
|
resp, err := r.client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, &ControlPlaneHTTPError{Status: resp.StatusCode}
|
|
}
|
|
var out struct {
|
|
Messages []StoredFolderMessage `json:"messages"`
|
|
}
|
|
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
|
|
return nil, err
|
|
}
|
|
return out.Messages, nil
|
|
}
|
|
|
|
// ListProviderFolderMessages asks the control plane which rows the provider
|
|
// last placed in folders.
|
|
func (r *httpSyncContextRepository) ListProviderFolderMessages(ctx context.Context, userID, emailID uuid.UUID, folders []string, limit int) ([]ProviderFolderMessage, error) {
|
|
q := url.Values{}
|
|
q.Set("user_id", userID.String())
|
|
q.Set("email_id", emailID.String())
|
|
q.Set("folders", strings.Join(folders, ","))
|
|
q.Set("limit", strconv.Itoa(limit))
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.baseURL+"/api/v1/internal/sync/provider-folder-messages?"+q.Encode(), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
nodeauth.Authorize(req, r.token)
|
|
req.Header.Set("User-Agent", "warmbly-worker/sync-context-http")
|
|
resp, err := r.client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, &ControlPlaneHTTPError{Status: resp.StatusCode}
|
|
}
|
|
var out struct {
|
|
Messages []ProviderFolderMessage `json:"messages"`
|
|
}
|
|
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
|
|
return nil, err
|
|
}
|
|
return out.Messages, nil
|
|
}
|