--server host now installs instead of refusing. The server is the release binary for this machine's architecture, checked against a SHA-256 written into the source (internal/release), run once with --version before it is put in place, and installed as /usr/local/bin/inbuxa-server so it does not collide with this program. It gets a system user, its configuration and data in <dir>/server, its environment in <dir>/server.env (0600), and inbuxa-server.service. First boot is the container sequence. During setup a drop-in under /run lets the server reach loopback and nothing else, and the step's credential is in a root-only file rather than the unit. Then the listeners are fitted to the host: plain HTTP to 127.0.0.1:8081, where a container install publishes it; the server's own 443 listener removed, since the proxy holds 443; on --local every listener on loopback. The finished unit has ConditionPathExists on the configuration, so a server that loses it stays down instead of coming back in bootstrap mode. The console and the webmail stay containers. Beside a host server the webmail and Caddy share the host's network and reach it on loopback; the console keeps the project network. A container install renders the same files as before. status and export report the server as host. A re-run converges; moving the server between a container and the host is refused, not built yet. e2e/cases/install-host.sh: 43 checks on Debian 13. install-local, install-public, status-export and topology pass unchanged.
226 lines
8.8 KiB
Go
226 lines
8.8 KiB
Go
// SPDX-FileCopyrightText: 2026 Coffey Labs LLC
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package jmap
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// fake answers each request with the response registered for its first
|
|
// method, and records what it was sent.
|
|
func fake(t *testing.T, responses map[string]string) (*Client, *[]map[string]any) {
|
|
t.Helper()
|
|
var seen []map[string]any
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// The password, or the API key a configured server takes instead.
|
|
if u, p, _ := r.BasicAuth(); (u != "admin" || p != "pw") && r.Header.Get("Authorization") != "Bearer API_key" {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
var req struct {
|
|
Using []string `json:"using"`
|
|
MethodCalls [][3]json.RawMessage `json:"methodCalls"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
t.Errorf("bad request body: %v", err)
|
|
}
|
|
var method string
|
|
_ = json.Unmarshal(req.MethodCalls[0][0], &method)
|
|
var args map[string]any
|
|
_ = json.Unmarshal(req.MethodCalls[0][1], &args)
|
|
seen = append(seen, map[string]any{"method": method, "args": args, "using": req.Using})
|
|
body, ok := responses[method]
|
|
if !ok {
|
|
t.Errorf("unexpected method %s", method)
|
|
}
|
|
w.Write([]byte(body))
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
return &Client{BaseURL: srv.URL, Username: "admin", Password: "pw"}, &seen
|
|
}
|
|
|
|
func TestBootstrapReturnsTheAdministrator(t *testing.T) {
|
|
c, seen := fake(t, map[string]string{
|
|
"x:Bootstrap/set": `{"methodResponses":[["x:Bootstrap/set",{"updated":{"singleton":{"username":"[email protected]","secret":"s3cret"}}},"0"]]}`,
|
|
})
|
|
a, err := c.Bootstrap(context.Background(), "mail.example.com", "example.com", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if a.Username != "[email protected]" || a.Secret != "s3cret" {
|
|
t.Errorf("admin = %+v", a)
|
|
}
|
|
update := (*seen)[0]["args"].(map[string]any)["update"].(map[string]any)["singleton"].(map[string]any)
|
|
if update["requestTlsCertificate"] != false || update["tracer"].(map[string]any)["@type"] != "Stdout" {
|
|
t.Errorf("bootstrap sent %v", update)
|
|
}
|
|
if using := (*seen)[0]["using"].([]string); len(using) != 2 || using[1] != "urn:inbuxa:jmap:registry" {
|
|
t.Errorf("using = %v", using)
|
|
}
|
|
}
|
|
|
|
func TestSetRefusalIsAnError(t *testing.T) {
|
|
c, _ := fake(t, map[string]string{
|
|
"x:Account/set": `{"methodResponses":[["x:Account/set",{"notCreated":{"user":{"type":"invalidPatch","description":"Missing or invalid '@type' property in object","properties":["roles"]}}},"0"]]}`,
|
|
})
|
|
_, err := c.CreateUser(context.Background(), "alice", "b", "pw")
|
|
if err == nil || !strings.Contains(err.Error(), "invalidPatch") || !strings.Contains(err.Error(), "roles") {
|
|
t.Fatalf("err = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestMethodErrorNamesTheMethod(t *testing.T) {
|
|
c, _ := fake(t, map[string]string{
|
|
"x:Bootstrap/get": `{"methodResponses":[["error",{"type":"unknownMethod"},"0"]]}`,
|
|
})
|
|
err := c.CheckBootstrapMode(context.Background())
|
|
var me *MethodError
|
|
if !errors.As(err, &me) || me.Method != "x:Bootstrap/get" || me.Type != "unknownMethod" {
|
|
t.Fatalf("err = %v", err)
|
|
}
|
|
if !strings.Contains(err.Error(), "not in bootstrap mode") {
|
|
t.Errorf("err = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestConfiguredServerRefusesBootstrapCredentials(t *testing.T) {
|
|
c, _ := fake(t, nil)
|
|
c.Password = "the-bootstrap-password"
|
|
if err := c.CheckBootstrapMode(context.Background()); err == nil || !strings.Contains(err.Error(), "not in bootstrap mode") {
|
|
t.Fatalf("err = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestEnableACMEUsesHTTP01AndTheBackReference(t *testing.T) {
|
|
c, seen := fake(t, map[string]string{
|
|
"x:AcmeProvider/set": `{"methodResponses":[["x:AcmeProvider/set",{"created":{"acme":{"id":"p1"}}},"0"],["x:Domain/set",{"updated":{"b":null}},"1"]]}`,
|
|
})
|
|
id, err := c.EnableACME(context.Background(), "b", "", "[email protected]")
|
|
if err != nil || id != "p1" {
|
|
t.Fatalf("id %q, err %v", id, err)
|
|
}
|
|
provider := (*seen)[0]["args"].(map[string]any)["create"].(map[string]any)["acme"].(map[string]any)
|
|
if provider["challengeType"] != "Http01" {
|
|
t.Errorf("challengeType = %v", provider["challengeType"])
|
|
}
|
|
if _, set := provider["directory"]; set {
|
|
t.Error("directory sent without --acme-directory; Stalwart's default is Let's Encrypt")
|
|
}
|
|
}
|
|
|
|
func TestDomainIDNotFound(t *testing.T) {
|
|
c, _ := fake(t, map[string]string{
|
|
"x:Domain/query": `{"methodResponses":[["x:Domain/query",{"ids":["b"]},"0"],["x:Domain/get",{"list":[{"id":"b","name":"other.test"}]},"1"]]}`,
|
|
})
|
|
if _, err := c.DomainID(context.Background(), "example.com"); err == nil {
|
|
t.Fatal("found a domain that is not there")
|
|
}
|
|
}
|
|
|
|
func TestAPIKeyIsCreatedWithAnExpiryAndThenSentAsBearer(t *testing.T) {
|
|
c, seen := fake(t, map[string]string{
|
|
"x:ApiKey/set": `{"methodResponses":[["x:ApiKey/set",{"created":{"key":{"id":"k1","secret":"API_key"}},"destroyed":["k1"]},"0"]]}`,
|
|
})
|
|
id, token, err := c.CreateAPIKey(context.Background(), "setup", time.Hour)
|
|
if err != nil || id != "k1" || token != "API_key" {
|
|
t.Fatalf("id %q, token %q, err %v", id, token, err)
|
|
}
|
|
key := (*seen)[0]["args"].(map[string]any)["create"].(map[string]any)["key"].(map[string]any)
|
|
if exp, _ := key["expiresAt"].(string); exp == "" {
|
|
t.Errorf("no expiresAt: a key left by a failed setup would never lapse")
|
|
}
|
|
|
|
bearer := &Client{BaseURL: c.BaseURL, Token: token}
|
|
if err := bearer.DestroyAPIKey(context.Background(), id); err != nil {
|
|
t.Fatalf("destroy with the key as Bearer: %v", err)
|
|
}
|
|
if got := (*seen)[1]["args"].(map[string]any)["destroy"].([]any); len(got) != 1 || got[0] != "k1" {
|
|
t.Errorf("destroy = %v", got)
|
|
}
|
|
}
|
|
|
|
func TestBearerReplacesBasic(t *testing.T) {
|
|
c, _ := fake(t, nil)
|
|
c.Token = "API_wrong" // with the right password still set
|
|
if _, err := c.Do(context.Background(), Call{"x:Domain/query", map[string]any{}, "0"}); err == nil {
|
|
t.Fatal("a wrong token was accepted: the password must not be sent alongside it")
|
|
}
|
|
}
|
|
|
|
func TestBootstrapOnTheHostNamesItsDataDirectory(t *testing.T) {
|
|
c, seen := fake(t, map[string]string{
|
|
"x:Bootstrap/set": `{"methodResponses":[["x:Bootstrap/set",{"updated":{"singleton":{"username":"[email protected]","secret":"s3cret"}}},"0"]]}`,
|
|
})
|
|
if _, err := c.Bootstrap(context.Background(), "mail.example.com", "example.com", "/var/lib/inbuxa/server/data/"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
update := (*seen)[0]["args"].(map[string]any)["update"].(map[string]any)["singleton"].(map[string]any)
|
|
store, _ := update["dataStore"].(map[string]any)
|
|
if store["@type"] != "RocksDb" || store["path"] != "/var/lib/inbuxa/server/data/" {
|
|
t.Errorf("dataStore = %v", update["dataStore"])
|
|
}
|
|
}
|
|
|
|
const defaultListeners = `{"methodResponses":[
|
|
["x:NetworkListener/query",{"ids":["a","b","c"]},"0"],
|
|
["x:NetworkListener/get",{"list":[
|
|
{"id":"a","name":"smtp","protocol":"smtp","tlsImplicit":false,"bind":{"[::]:25":true}},
|
|
{"id":"b","name":"https","protocol":"http","tlsImplicit":true,"bind":{"[::]:443":true}},
|
|
{"id":"c","name":"http","protocol":"http","tlsImplicit":false,"bind":{"[::]:8080":true}}
|
|
]},"1"]]}`
|
|
|
|
func TestHostListenersMoveHTTPAndLeave443ToTheProxy(t *testing.T) {
|
|
c, seen := fake(t, map[string]string{
|
|
"x:NetworkListener/query": defaultListeners,
|
|
"x:NetworkListener/set": `{"methodResponses":[["x:NetworkListener/set",{"updated":{"c":null},"destroyed":["b"]},"0"]]}`,
|
|
})
|
|
if err := c.HostListeners(context.Background(), "127.0.0.1:8081", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
args := (*seen)[1]["args"].(map[string]any)
|
|
update := args["update"].(map[string]any)
|
|
if len(update) != 1 {
|
|
t.Fatalf("updated %v, want only the plain HTTP listener", update)
|
|
}
|
|
bind := update["c"].(map[string]any)["bind"].(map[string]any)
|
|
if len(bind) != 1 || bind["127.0.0.1:8081"] != true {
|
|
t.Errorf("http bind = %v", bind)
|
|
}
|
|
if d := args["destroy"].([]any); len(d) != 1 || d[0] != "b" {
|
|
t.Errorf("destroy = %v, want the HTTPS listener", d)
|
|
}
|
|
}
|
|
|
|
func TestHostListenersOnLoopbackForALocalInstall(t *testing.T) {
|
|
c, seen := fake(t, map[string]string{
|
|
"x:NetworkListener/query": defaultListeners,
|
|
"x:NetworkListener/set": `{"methodResponses":[["x:NetworkListener/set",{"updated":{"a":null,"c":null},"destroyed":["b"]},"0"]]}`,
|
|
})
|
|
if err := c.HostListeners(context.Background(), "127.0.0.1:8081", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
update := (*seen)[1]["args"].(map[string]any)["update"].(map[string]any)
|
|
bind := update["a"].(map[string]any)["bind"].(map[string]any)
|
|
if len(bind) != 1 || bind["127.0.0.1:25"] != true {
|
|
t.Errorf("smtp bind = %v, want loopback", bind)
|
|
}
|
|
}
|
|
|
|
func TestHostListenersRefusalIsAnError(t *testing.T) {
|
|
c, _ := fake(t, map[string]string{
|
|
"x:NetworkListener/query": defaultListeners,
|
|
"x:NetworkListener/set": `{"methodResponses":[["x:NetworkListener/set",{"notDestroyed":{"b":{"type":"forbidden","description":"no"}}},"0"]]}`,
|
|
})
|
|
if err := c.HostListeners(context.Background(), "127.0.0.1:8081", false); err == nil {
|
|
t.Fatal("a refused destroy was not reported")
|
|
}
|
|
}
|