Something went wrong. Try again.
forked from scottlanoue.com/atmosphere-mail
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485// SPDX-License-Identifier: AGPL-3.0-or-later
package adminclient
import ( "bytes" "context" "encoding/json" "fmt" "io" "net/http"
"comail/internal/featureflags")
// maxOverridesResponse caps the override snapshot body cmd/web will read// from the relay — a handful of flags; anything larger is malformed.const maxOverridesResponse = 64 << 10
// FetchOverrides pulls the relay's current live-override snapshot from// GET /admin/flags/overrides and parses it (Dynamic-filtered). cmd/web// uses this instead of reading the relay's overrides file directly: relay// and web run as different users with isolated state dirs, so the tailnet// admin channel is the only path. Best-effort: the caller keeps its last// snapshot on error.func (c *Client) FetchOverrides(ctx context.Context) (*featureflags.Overrides, error) { target := *c.baseURL target.Path = "/admin/flags/overrides" req, err := http.NewRequestWithContext(ctx, http.MethodGet, target.String(), nil) if err != nil { return nil, fmt.Errorf("adminclient: build overrides request: %w", err) } req.Header.Set("Authorization", "Bearer "+c.adminToken)
resp, err := c.httpClient.Do(req) if err != nil { return nil, fmt.Errorf("adminclient: overrides request: %w", err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("adminclient: overrides fetch: status %d", resp.StatusCode) } body, err := io.ReadAll(io.LimitReader(resp.Body, maxOverridesResponse)) if err != nil { return nil, fmt.Errorf("adminclient: read overrides body: %w", err) } return featureflags.ParseOverrides(body)}
// ReportFlags pushes this binary's resolved feature-flag snapshot to the// relay's /admin/flags/report endpoint. cmd/web calls this at startup so// the relay's admin flag viewer can show web's resolved state beside the// relay's own — the relay cannot read cmd/web's process env directly, and// this loopback POST (web→relay) is the only channel between them.//// It is best-effort telemetry: a non-204 response or transport error is// returned for the caller to log, but reporting failure must never block// or fail web startup. The relay treats "no report" as a divergence// signal in the viewer, so a missed report is visible rather than silent.func (c *Client) ReportFlags(ctx context.Context, rep featureflags.Report) error { body, err := json.Marshal(rep) if err != nil { return fmt.Errorf("adminclient: marshal flag report: %w", err) }
target := *c.baseURL target.Path = "/admin/flags/report" req, err := http.NewRequestWithContext(ctx, http.MethodPost, target.String(), bytes.NewReader(body)) if err != nil { return fmt.Errorf("adminclient: build flag report request: %w", err) } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+c.adminToken)
resp, err := c.httpClient.Do(req) if err != nil { return fmt.Errorf("adminclient: flag report request: %w", err) } defer resp.Body.Close() if resp.StatusCode != http.StatusNoContent { return fmt.Errorf("adminclient: flag report rejected: status %d", resp.StatusCode) } return nil}