From 08070bae5d285f119d3f7ab65e5de6b41fab44d4 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Sun, 4 Oct 2026 22:54:29 -0700 Subject: [PATCH] Install the current suite: inbuxa-webmail, and the server's token-only sign-in Pins server 2026.9.30.2, console 2026.9.30 and webmail inbuxa-webmail:2026.10.5-g17a8093, the repository's new name. Those releases need three changes here: - The JMAP client asks for urn:inbuxa:jmap:registry, the registry capability's name since the server renamed it. - Configured servers refuse HTTP Basic on /jmap (contract C-23), and in bootstrap mode nothing but x:Bootstrap answers. So after bootstrap the server comes up once with INBUXA_HTTP_BASIC_AUTH=all, from a one-step override file, long enough for the administrator to give itself an API key; it is then recreated without the override. The key, as a Bearer token, does the rest of the setup, is destroyed at the end and expires within the hour regardless. - --local signs in to the webmail through the server's page as well, as the first-party OAuth client: a password sign-in would have nothing to pass the password on with. The server is told its loopback address as INBUXA_PUBLIC_URL, so its sign-in page and OAuth metadata point where a browser can reach them. install-local now signs in end to end the way a browser does, and checks that the Basic override did not outlive the setup. --- e2e/cases/install-local.sh | 31 +++++++++++-- internal/apply/apply.go | 46 ++++++++++++++++++-- internal/compose/templates/compose.yaml.tmpl | 17 ++++++-- internal/jmap/client.go | 14 ++++-- internal/jmap/client_test.go | 36 ++++++++++++++- internal/jmap/setup.go | 40 +++++++++++++++++ internal/plan/plan.go | 2 +- 7 files changed, 169 insertions(+), 17 deletions(-) diff --git a/e2e/cases/install-local.sh b/e2e/cases/install-local.sh index 4d40e10..e37baa6 100644 --- a/e2e/cases/install-local.sh +++ b/e2e/cases/install-local.sh @@ -72,16 +72,39 @@ echo "==> the bootstrap credential did not outlive the setup" ENVOUT="$("$(rt)" inspect --format '{{range .Config.Env}}{{println .}}{{end}}' "$(compose ps -q server)")" grep -q "RECOVERY_ADMIN" <<<"$ENVOUT" && { bad "the server still carries a recovery admin"; echo "$ENVOUT" | grep RECOVERY | sed 's/^/ /'; } || ok "no recovery admin in the running server" grep -q "INBUXA_WEBMAIL_CLIENT_SECRET" <<<"$ENVOUT" && ok "the webmail's client secret is where it belongs" || bad "the server has no webmail client secret" +# The setup's API key was made in a window where the server took passwords +# on /jmap. Afterwards it must take tokens only again (contract C-23). +grep -q "HTTP_BASIC_AUTH" <<<"$ENVOUT" && bad "the server still takes passwords everywhere" || ok "no Basic-everywhere override in the running server" +AUSER=$(awk '/^administrator/ {print $2}' "$DIR/credentials.txt") +APASS=$(awk '/^administrator/{getline; print $2}' "$DIR/credentials.txt") +CODE=$(curl -s -o /dev/null -w '%{http_code}' -u "$AUSER:$APASS" -H 'Content-Type: application/json' \ + -d '{"using":["urn:ietf:params:jmap:core"],"methodCalls":[]}' http://127.0.0.1:8081/jmap/) +[ "$CODE" = 401 ] && ok "the administrator's password is refused on /jmap" || bad "the administrator's password on /jmap answered $CODE, not 401" echo echo "==> the account it created can sign in to the webmail it installed" USER=$(awk '/^first mailbox/ {print $3}' "$DIR/credentials.txt") PASS=$(awk '/^first mailbox/{getline; print $2}' "$DIR/credentials.txt") [ -n "$USER" ] && ok "credentials.txt names the first mailbox ($USER)" || bad "no first mailbox in credentials.txt" -CODE=$(curl -s -o /tmp/login.json -c /tmp/jar -w '%{http_code}' -X POST http://127.0.0.1:8080/api/auth/login \ - -H 'Content-Type: application/json' -H 'X-Requested-With: ihasmail' \ - -d "{\"username\":\"$USER\",\"password\":\"$PASS\",\"remember\":true}") -[ "$CODE" = 200 ] && ok "sign-in succeeds" || { bad "sign-in answered $CODE"; head -c 200 /tmp/login.json | sed 's/^/ /'; } +# Sign-in is the mail server's own page here too, as on a public install: +# the server takes no password on /jmap (contract C-23). This does what a +# browser does -- start at the webmail, post the password where the server's +# page posts it, follow the code back -- so a pass means a person could. +rm -f /tmp/jar +LOC=$(curl -s -o /dev/null -c /tmp/jar -w '%{redirect_url}' "http://127.0.0.1:8080/api/auth/oauth/start?username=$USER&remember=1") +grep -q "^http://127.0.0.1:8081/" <<<"$LOC" && ok "the webmail sends sign-in to the server's loopback address" || bad "sign-in went to '${LOC:-nowhere}'" +grep -q "client_id=ihasmail-inbuxa" <<<"$LOC" && ok "as the first-party client" || bad "no first-party client id in '$LOC'" +q() { sed -n "s/.*[?&]$1=\([^&]*\).*/\1/p" <<<"$LOC"; } +urldecode() { printf '%b' "${1//%/\\x}"; } +STATE=$(q state); CHALLENGE=$(q code_challenge); REDIRECT=$(urldecode "$(q redirect_uri)") +ANSWER=$(curl -s -X POST http://127.0.0.1:8081/api/auth -H 'Content-Type: application/json' -H 'Accept: application/json' \ + -d "{\"type\":\"authCode\",\"accountName\":\"$USER\",\"accountSecret\":\"$PASS\",\"clientId\":\"ihasmail-inbuxa\",\"redirectUri\":\"$REDIRECT\",\"codeChallenge\":\"$CHALLENGE\",\"codeChallengeMethod\":\"S256\"}") +AUTHCODE=$(sed -n 's/.*"client_\{0,1\}[cC]ode":"\([^"]*\)".*/\1/p' <<<"$ANSWER") +[ -n "$AUTHCODE" ] && ok "the server's sign-in takes the first mailbox's password" || { bad "the server's sign-in answered"; head -c 200 <<<"$ANSWER" | sed 's/^/ /'; } +BACK=$(curl -s -o /dev/null -b /tmp/jar -c /tmp/jar -w '%{redirect_url}' "$REDIRECT?code=$AUTHCODE&state=$STATE") +grep -q "signin_error" <<<"$BACK" && bad "the webmail refused the code: $BACK" || ok "the webmail takes the code back" +CODE=$(curl -s -o /tmp/login.json -b /tmp/jar -w '%{http_code}' -H 'X-Requested-With: ihasmail' http://127.0.0.1:8080/api/auth/session) +[ "$CODE" = 200 ] && ok "sign-in succeeds" || { bad "the session answered $CODE"; head -c 200 /tmp/login.json | sed 's/^/ /'; } grep -q "urn:ietf:params:jmap:mail" /tmp/login.json && ok "and the session carries the mail capability" || bad "no mail capability in the session" echo diff --git a/internal/apply/apply.go b/internal/apply/apply.go index 8b63975..d188eac 100644 --- a/internal/apply/apply.go +++ b/internal/apply/apply.go @@ -41,9 +41,9 @@ import ( // here would mean this program needing a release of its own for every one of // theirs. const ( - DefaultServerImage = "registry.coffeylabs.org/inbuxa/inbuxa-server:2026.9.23" - DefaultConsoleImage = "registry.coffeylabs.org/inbuxa/inbuxa-admin:2026.9.21.2" - DefaultWebmailImage = "registry.coffeylabs.org/inbuxa/ihasmail-inbuxa:2026.9.22-gc2f13d6" + DefaultServerImage = "registry.coffeylabs.org/inbuxa/inbuxa-server:2026.9.30.2" + DefaultConsoleImage = "registry.coffeylabs.org/inbuxa/inbuxa-admin:2026.9.30" + DefaultWebmailImage = "registry.coffeylabs.org/inbuxa/inbuxa-webmail:2026.10.5-g17a8093" DefaultCaddyImage = "docker.io/library/caddy:2-alpine" DefaultSubnet = "172.31.253.0/24" ) @@ -247,7 +247,46 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro } log.Info("administrator %s, password in %s", admin.Username, filepath.Join(dir, "credentials.txt")) + // Once configured, the server takes a token, never a password, on /jmap + // (contract C-23), and the bootstrap credential no longer works; nor + // does anything but x:Bootstrap work before then. So the server comes up + // configured with Basic allowed, for this one step only, long enough for + // the administrator to give itself an API key with its password. The key + // does the rest of the setup, is removed when the setup ends, and expires + // within the hour anyway, so a setup that stops halfway leaves nothing + // that still works. + basicOverride := filepath.Join(os.TempDir(), "inbuxa-basic-auth.yaml") + if err := os.WriteFile(basicOverride, []byte( + "services:\n server:\n environment:\n INBUXA_HTTP_BASIC_AUTH: all\n"), 0o600); err != nil { + return res, err + } + defer os.Remove(basicOverride) + log.Step("restarting the mail server configured, to give the setup an API key") + basic := cmp + basic.Files = []string{basicOverride} + if err := basic.Run(ctx, "up", "-d", "server"); err != nil { + return res, err + } + adminBasic := &jmap.Client{BaseURL: serverURL, Username: admin.Username, Password: admin.Secret} + var keyID, key string + if err := waitFor(ctx, log, "the server to come back configured", 120*time.Second, func(ctx context.Context) error { + var err error + keyID, key, err = adminBasic.CreateAPIKey(ctx, "inbuxa installer, for this setup only", time.Hour) + return err + }); err != nil { + return res, withLogs(ctx, fmt.Errorf("an API key for the setup: %w", err), cmp, "server") + } + srv := &jmap.Client{BaseURL: serverURL, Token: key} + defer func() { + ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second) + defer cancel() + if err := srv.DestroyAPIKey(ctx, keyID); err != nil { + log.Info("could not remove the setup's API key, which expires within the hour: %v", err) + } + }() + log.Step("starting the rest of the stack") + // Without the override, so the server is recreated taking tokens only. // --remove-orphans: the compose file is rendered from the shapes asked // for, so a component the topology no longer lists is simply not in it // any more. Without this its container would keep running, belonging to @@ -256,7 +295,6 @@ func Run(ctx context.Context, p plan.Plan, f host.Facts, log Log) (*Result, erro return res, err } - srv := &jmap.Client{BaseURL: serverURL, Username: admin.Username, Password: admin.Secret} if err := waitFor(ctx, log, "the server to come back configured", 120*time.Second, func(ctx context.Context) error { _, err := srv.DomainID(ctx, o.Domain) return err diff --git a/internal/compose/templates/compose.yaml.tmpl b/internal/compose/templates/compose.yaml.tmpl index 9bbc940..a26308a 100644 --- a/internal/compose/templates/compose.yaml.tmpl +++ b/internal/compose/templates/compose.yaml.tmpl @@ -36,6 +36,12 @@ services: {{- if .WebmailURL}} INBUXA_WEBMAIL_URL: {{.WebmailURL}} INBUXA_WEBMAIL_CLIENT_SECRET: ${WEBMAIL_CLIENT_SECRET:?WEBMAIL_CLIENT_SECRET is missing from .env} +{{- end}} +{{- if .Local}} + # Where a browser reaches the server, which is what its sign-in page + # and OAuth metadata advertise. Without it they name https://, which a loopback-only install has nothing behind. + INBUXA_PUBLIC_URL: {{.ServerPublicURL}} {{- end}} networks: stack: @@ -80,17 +86,22 @@ services: SESSION_FILE: "" TRUST_PROXY: "1" IMAGE_PROXY: "1" -{{- if not .Local}} # Sign-in goes through the mail server's own page, as the first-party - # client the server registered for this URL. + # client the server registered for this URL -- on --local too: the + # server takes a token, never a password, on /jmap (contract C-23), so + # a webmail that signed people in with their password would have + # nothing to pass it on with. OAUTH_CLIENT_ID: ihasmail-inbuxa OAUTH_CLIENT_SECRET: ${WEBMAIL_CLIENT_SECRET:?WEBMAIL_CLIENT_SECRET is missing from .env} PUBLIC_URL: {{.WebmailURL}} {{- if .ConsoleURL}} ADMIN_URL: {{.ConsoleURL}} {{- end}} +{{- if not .Local}} # The server pushes changes here instead of holding a connection per - # tab. If it cannot reach it, every tab falls back to the relay. + # tab. If it cannot reach it, every tab falls back to the relay, which + # is what --local does: from inside the server's container, 127.0.0.1 + # is not the webmail. PUSH_URL: {{.WebmailURL}} {{- end}} networks: diff --git a/internal/jmap/client.go b/internal/jmap/client.go index 3d83e1d..72aa1ee 100644 --- a/internal/jmap/client.go +++ b/internal/jmap/client.go @@ -23,7 +23,7 @@ import ( "time" ) -var using = []string{"urn:ietf:params:jmap:core", "urn:stalwart:jmap"} +var using = []string{"urn:ietf:params:jmap:core", "urn:inbuxa:jmap:registry"} // Client calls one Stalwart as one account. The zero HTTP uses a client with a // timeout, so a hung server fails a step instead of hanging the tool. @@ -31,7 +31,11 @@ type Client struct { BaseURL string // e.g. http://127.0.0.1:8081, no trailing slash Username string Password string - HTTP *http.Client + // Token, when set, is sent as a Bearer token instead of the password. + // Outside bootstrap and recovery mode the server refuses HTTP Basic on + // /jmap (contract C-23), so a configured server takes an API key only. + Token string + HTTP *http.Client } // Call is one method call in a request. @@ -97,7 +101,11 @@ func (c *Client) Do(ctx context.Context, calls ...Call) ([]Response, error) { if err != nil { return nil, err } - req.SetBasicAuth(c.Username, c.Password) + if c.Token != "" { + req.Header.Set("Authorization", "Bearer "+c.Token) + } else { + req.SetBasicAuth(c.Username, c.Password) + } req.Header.Set("Content-Type", "application/json") res, err := c.httpClient().Do(req) if err != nil { diff --git a/internal/jmap/client_test.go b/internal/jmap/client_test.go index fc48db6..57708b8 100644 --- a/internal/jmap/client_test.go +++ b/internal/jmap/client_test.go @@ -11,6 +11,7 @@ import ( "net/http/httptest" "strings" "testing" + "time" ) // fake answers each request with the response registered for its first @@ -19,7 +20,8 @@ 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) { - if u, p, _ := r.BasicAuth(); u != "admin" || p != "pw" { + // 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 } @@ -60,7 +62,7 @@ func TestBootstrapReturnsTheAdministrator(t *testing.T) { 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:stalwart:jmap" { + if using := (*seen)[0]["using"].([]string); len(using) != 2 || using[1] != "urn:inbuxa:jmap:registry" { t.Errorf("using = %v", using) } } @@ -122,3 +124,33 @@ func TestDomainIDNotFound(t *testing.T) { 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") + } +} diff --git a/internal/jmap/setup.go b/internal/jmap/setup.go index 768cba2..2bf2094 100644 --- a/internal/jmap/setup.go +++ b/internal/jmap/setup.go @@ -12,6 +12,7 @@ import ( "encoding/json" "errors" "fmt" + "time" ) // Admin is the permanent administrator bootstrap provisions. @@ -71,6 +72,45 @@ func (c *Client) Bootstrap(ctx context.Context, hostname, domain string) (Admin, return a, nil } +// CreateAPIKey gives the signed-in account an API key and returns its id and +// the token, which the server shows only once. It expires on its own after +// ttl, so a setup that stops halfway does not leave a working key behind. +func (c *Client) CreateAPIKey(ctx context.Context, description string, ttl time.Duration) (id, token string, err error) { + args := map[string]any{"create": map[string]any{ + "key": map[string]any{ + "description": description, + "expiresAt": time.Now().UTC().Add(ttl).Format(time.RFC3339), + "permissions": map[string]any{"@type": "Inherit"}, + }, + }} + rs, err := c.Do(ctx, Call{"x:ApiKey/set", args, "0"}) + if err != nil { + return "", "", err + } + s, err := DecodeSet(rs[0]) + if err != nil { + return "", "", err + } + var key struct { + ID string `json:"id"` + Secret string `json:"secret"` + } + if err := json.Unmarshal(s.Created["key"], &key); err != nil || key.ID == "" || key.Secret == "" { + return "", "", errors.New("x:ApiKey/set did not return the key it created") + } + return key.ID, key.Secret, nil +} + +// DestroyAPIKey removes an API key from the signed-in account. +func (c *Client) DestroyAPIKey(ctx context.Context, id string) error { + rs, err := c.Do(ctx, Call{"x:ApiKey/set", map[string]any{"destroy": []string{id}}, "0"}) + if err != nil { + return err + } + _, err = DecodeSet(rs[0]) + return err +} + // DomainID finds a domain by name. func (c *Client) DomainID(ctx context.Context, name string) (string, error) { rs, err := c.Do(ctx, diff --git a/internal/plan/plan.go b/internal/plan/plan.go index 43ad468..ffa9e0a 100644 --- a/internal/plan/plan.go +++ b/internal/plan/plan.go @@ -338,7 +338,7 @@ func Build(f host.Facts, o Options) (Plan, error) { p.Steps = append(p.Steps, Step{ Title: "Start the webmail as a container", Detail: []string{ - "image: registry.coffeylabs.org/inbuxa/ihasmail-inbuxa (pinned by digest)", + "image: registry.coffeylabs.org/inbuxa/inbuxa-webmail (pinned by digest)", "port: 8080 on loopback, behind the proxy", "secret: the OAuth client secret from first boot, written once", }, -- 2.54.0