Archived
fix(stalwart): use v0.16 management JMAP endpoint
This commit is contained in:
5 files changed
+104
-12
No files matched your search
+3
-3
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user