From 2d84a780d4ac4d38606e09eb2e6f5b9d05755cc4 Mon Sep 17 00:00:00 2001 From: Meghdad <61308304+MeghdadFadaee@users.noreply.github.com> Date: Thu, 13 Aug 2026 04:20:33 +0330 Subject: [PATCH] fix(stalwart): use v0.16 management JMAP endpoint --- cmd/mailctl/main.go | 6 +-- cmd/mailctl/main_test.go | 4 +- docs/nginx-certbot.md | 2 +- internal/stalwart/client.go | 12 ++--- internal/stalwart/client_test.go | 92 ++++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 internal/stalwart/client_test.go diff --git a/cmd/mailctl/main.go b/cmd/mailctl/main.go index a791c63..048ac4e 100644 --- a/cmd/mailctl/main.go +++ b/cmd/mailctl/main.go @@ -252,7 +252,7 @@ func createAPIKey(args []string) error { func configureTLS(args []string) error { fs := flag.NewFlagSet("configure-tls", flag.ContinueOnError) dir := fs.String("directory", ".", "Compose bundle directory") - endpoint := fs.String("url", "http://127.0.0.1:8081/api", "loopback Stalwart management endpoint") + endpoint := fs.String("url", "http://127.0.0.1:8081/jmap", "loopback Stalwart management JMAP endpoint") certificatePath := fs.String("certificate-path", "/etc/stalwart/tls/fullchain.pem", "certificate path inside the Stalwart container") privateKeyPath := fs.String("private-key-path", "/etc/stalwart/tls/privkey.pem", "private key path inside the Stalwart container") if err := fs.Parse(args); err != nil { @@ -260,8 +260,8 @@ func configureTLS(args []string) error { } root, _ := filepath.Abs(*dir) managementURL, err := url.Parse(*endpoint) - if err != nil || managementURL.Scheme != "http" || managementURL.Path != "/api" || managementURL.User != nil || managementURL.RawQuery != "" || managementURL.Fragment != "" { - return errors.New("--url must be a loopback HTTP URL ending in /api") + if err != nil || managementURL.Scheme != "http" || managementURL.Path != "/jmap" || managementURL.User != nil || managementURL.RawQuery != "" || managementURL.Fragment != "" { + return errors.New("--url must be a loopback HTTP URL ending in /jmap") } managementHost := managementURL.Hostname() managementIP := net.ParseIP(managementHost) diff --git a/cmd/mailctl/main_test.go b/cmd/mailctl/main_test.go index aa6ea75..0829fbe 100644 --- a/cmd/mailctl/main_test.go +++ b/cmd/mailctl/main_test.go @@ -38,14 +38,14 @@ func TestJMAPSucceededRejectsFailedUpdate(t *testing.T) { } func TestConfigureTLSRejectsRemoteManagementURL(t *testing.T) { - err := configureTLS([]string{"--directory", t.TempDir(), "--url", "http://mail.example.com/api"}) + err := configureTLS([]string{"--directory", t.TempDir(), "--url", "http://mail.example.com/jmap"}) if err == nil || !strings.Contains(err.Error(), "loopback") { t.Fatalf("expected loopback URL rejection, got %v", err) } } func TestConfigureTLSRejectsHTTPSManagementURL(t *testing.T) { - err := configureTLS([]string{"--directory", t.TempDir(), "--url", "https://127.0.0.1:8081/api"}) + err := configureTLS([]string{"--directory", t.TempDir(), "--url", "https://127.0.0.1:8081/jmap"}) if err == nil || !strings.Contains(err.Error(), "loopback HTTP URL") { t.Fatalf("expected plain loopback HTTP requirement, got %v", err) } diff --git a/docs/nginx-certbot.md b/docs/nginx-certbot.md index ee37cb6..ec956ce 100644 --- a/docs/nginx-certbot.md +++ b/docs/nginx-certbot.md @@ -141,7 +141,7 @@ references, selects it as `defaultCertificateId`, records the object id in port was changed, pass a loopback URL explicitly: ```bash -./mailctl configure-tls --url http://127.0.0.1:9081/api +./mailctl configure-tls --url http://127.0.0.1:9081/jmap ``` ## Renewal hook diff --git a/internal/stalwart/client.go b/internal/stalwart/client.go index 0765a48..d8d77cf 100644 --- a/internal/stalwart/client.go +++ b/internal/stalwart/client.go @@ -35,11 +35,11 @@ func (c *Client) Bootstrap(ctx context.Context, bootstrapURL, recoveryAdmin, hos "serverHostname": hostname, "defaultDomain": domain, "requestTlsCertificate": false, "generateDkimKeys": true, "dataStore": map[string]any{"@type": "RocksDb", "path": "/var/lib/stalwart/"}, "blobStore": map[string]any{"@type": "Default"}, "searchStore": map[string]any{"@type": "Default"}, "inMemoryStore": map[string]any{"@type": "Default"}, - "directory": map[string]any{"@type": "Internal"}, "tracer": map[string]any{"@type": "Console"}, "dnsServer": map[string]any{"@type": "Manual"}, + "directory": map[string]any{"@type": "Internal"}, "tracer": map[string]any{"@type": "Stdout"}, "dnsServer": map[string]any{"@type": "Manual"}, }}}, "bootstrap"}}, } raw, _ := json.Marshal(payload) - req, _ := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(bootstrapURL, "/")+"/api", bytes.NewReader(raw)) + req, _ := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(bootstrapURL, "/")+"/jmap", bytes.NewReader(raw)) req.SetBasicAuth(parts[0], parts[1]) req.Header.Set("Content-Type", "application/json") resp, err := c.HTTP.Do(req) @@ -114,11 +114,11 @@ func (c *Client) ProxyJMAP(ctx context.Context, token string, body []byte) (int, return c.proxy(ctx, token, "/jmap", body) } -// ProxyManagement keeps Stalwart's pre-1.0 management schema behind the MEOVV -// adapter. The browser never receives an administrator token or direct access -// to Stalwart's management endpoint. +// ProxyManagement keeps Stalwart's pre-1.0 management JMAP schema behind the +// MEOVV adapter. The browser never receives an administrator token or direct +// access to Stalwart's management endpoint. func (c *Client) ProxyManagement(ctx context.Context, token string, body []byte) (int, http.Header, []byte, error) { - return c.proxy(ctx, token, "/api", body) + return c.proxy(ctx, token, "/jmap", body) } func (c *Client) JMAPSession(ctx context.Context, token string) (int, http.Header, []byte, error) { diff --git a/internal/stalwart/client_test.go b/internal/stalwart/client_test.go new file mode 100644 index 0000000..9590487 --- /dev/null +++ b/internal/stalwart/client_test.go @@ -0,0 +1,92 @@ +package stalwart + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "testing" +) + +func TestBootstrapUsesPinnedManagementJMAPEndpoint(t *testing.T) { + t.Parallel() + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + t.Errorf("method = %s, want POST", r.Method) + } + if r.URL.Path != "/jmap" { + t.Errorf("path = %s, want /jmap", r.URL.Path) + } + username, password, ok := r.BasicAuth() + if !ok || username != "admin" || password != "recovery-secret" { + t.Errorf("unexpected recovery authentication: %q %q %v", username, password, ok) + } + if got := r.Header.Get("Content-Type"); got != "application/json" { + t.Errorf("Content-Type = %q", got) + } + + var request struct { + Using []string `json:"using"` + MethodCalls [][]json.RawMessage `json:"methodCalls"` + } + if err := json.NewDecoder(r.Body).Decode(&request); err != nil { + t.Fatal(err) + } + if len(request.MethodCalls) != 1 || len(request.MethodCalls[0]) != 3 { + t.Fatalf("unexpected method calls: %#v", request.MethodCalls) + } + var method string + if err := json.Unmarshal(request.MethodCalls[0][0], &method); err != nil { + t.Fatal(err) + } + if method != "x:Bootstrap/set" { + t.Errorf("method = %q, want x:Bootstrap/set", method) + } + var arguments struct { + Update map[string]struct { + Tracer map[string]any `json:"tracer"` + } `json:"update"` + } + if err := json.Unmarshal(request.MethodCalls[0][1], &arguments); err != nil { + t.Fatal(err) + } + if got := arguments.Update["singleton"].Tracer["@type"]; got != "Stdout" { + t.Errorf("tracer type = %v, want Stdout", got) + } + + w.Header().Set("Content-Type", "application/json") + _, _ = io.WriteString(w, `{"methodResponses":[["x:Bootstrap/set",{"updated":{"singleton":{"username":"admin@example.com","secret":"generated"}}},"bootstrap"]]}`) + })) + defer server.Close() + + client := New(server.URL, false) + if _, err := client.Bootstrap(context.Background(), server.URL, "admin:recovery-secret", "mail.example.com", "example.com"); err != nil { + t.Fatal(err) + } +} + +func TestProxyManagementUsesJMAPEndpoint(t *testing.T) { + t.Parallel() + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/jmap" { + t.Errorf("path = %s, want /jmap", r.URL.Path) + } + if got := r.Header.Get("Authorization"); got != "Bearer access-token" { + t.Errorf("Authorization = %q", got) + } + _, _ = io.WriteString(w, `{"methodResponses":[]}`) + })) + defer server.Close() + + client := New(server.URL, false) + status, _, _, err := client.ProxyManagement(context.Background(), "access-token", []byte(`{"methodCalls":[]}`)) + if err != nil { + t.Fatal(err) + } + if status != http.StatusOK { + t.Fatalf("status = %d, want 200", status) + } +}