// Package forgehttp is the forge's HTTP interface as this module's Go bundles reach it (cmd/forge, // cmd/package-registry), on the forge's own machine. One copy for both bundles (novox/hq issue 539). // // Two ways in, by what the manifest hands the bundle: // // - **as the tools' account or the packages account** (novox/hq ADR 0365 §2; design 47 H2 §808), with the account's // token the module keeps in the vault as its own (token.ts mints it; vault.go reads it through the vault seat's // `get` at start and at `secret-changed`). This bundle mints nothing; a token the forge rejects is renewed by the // module's next call, and said here. // - **as the admin account**, whose password the vault delivered into a file this module owns (novox/hq ADR // 0086), by basic authentication, when no token is given. // // A credential is read when a call is made and never logged, answered or put in an error. package forgehttp import ( "bytes" "context" "encoding/json" "errors" "fmt" "io" "net/http" "os" "strings" "time" ) // MaxBody is the largest JSON answer read from the forge. const MaxBody = 32 << 20 // Client is the forge's HTTP interface. type Client struct { URL string // e.g. http://127.0.0.1:3000 User string PasswordFile string // Token, when set, is the account's token as the vault holds it (vault.go), sent instead of the admin login; // Account the account it is (MESH_GITEA_TOOLS_USER, default mesh-tools; MESH_GITEA_PACKAGES_USER for the writer). Token *VaultToken Account string HTTP *http.Client // when nil, a client with Timeout Timeout time.Duration // one request's limit when HTTP is nil; 20 s when zero } // ToolsAccount is the account the forge's tools act as when the manifest names none (token.ts, tools-account.ts). const ToolsAccount = "mesh-tools" // PackagesAccount is the account the package seats write as when the manifest names none (packages-account.ts): // its token carries write:package and nothing else (design 51: each holder keeps its write side as an own secret // beside a read-only tool token). const PackagesAccount = "mesh-packages" // WriterFromEnv is the package registry's write side as the manifest hands it to a bundle: the packages account's // token, read from the vault (vault.go), and the names of the variables it left unset. Never the admin login. func WriterFromEnv() (Client, []string, error) { return accountFromEnv(os.Getenv("MESH_GITEA_PACKAGES_USER"), PackagesAccount) } // ToolsFromEnv is the forge as the tools' account, its token read from the vault (vault.go), and the names of the // variables the manifest left unset; it needs no admin login. func ToolsFromEnv() (Client, []string, error) { return accountFromEnv(os.Getenv("MESH_GITEA_TOOLS_USER"), ToolsAccount) } func accountFromEnv(account, fallback string) (Client, []string, error) { if account == "" { account = fallback } token, err := TokenOf(account) if err != nil { return Client{}, nil, err } c := Client{URL: strings.TrimRight(os.Getenv("MESH_GITEA_URL"), "/"), Token: token, Account: account} if c.URL == "" { return c, []string{"MESH_GITEA_URL"}, nil } return c, nil, nil } // FromEnv is the forge as the admin account, as the manifest hands it to a bundle, and the names of the variables it // left unset. func FromEnv() (Client, []string) { c := Client{ URL: strings.TrimRight(os.Getenv("MESH_GITEA_URL"), "/"), User: os.Getenv("MESH_GITEA_ADMIN_USER"), PasswordFile: os.Getenv("MESH_GITEA_ADMIN_PASSWORD_FILE"), } var missing []string for _, kv := range [][2]string{{"MESH_GITEA_URL", c.URL}, {"MESH_GITEA_ADMIN_USER", c.User}, {"MESH_GITEA_ADMIN_PASSWORD_FILE", c.PasswordFile}} { if kv[1] == "" { missing = append(missing, kv[0]) } } return c, missing } // ErrNotFound is the forge answering 404. var ErrNotFound = errors.New("not found") // StatusError is an answer the forge gave that is not a success, said without the request's credential. type StatusError struct { Method, Path string Status int Said string } func (e *StatusError) Error() string { return fmt.Sprintf("the forge answered %d to %s %s: %s", e.Status, e.Method, e.Path, e.Said) } func (c *Client) client() *http.Client { if c.HTTP != nil { return c.HTTP } timeout := c.Timeout if timeout <= 0 { timeout = 20 * time.Second } return &http.Client{Timeout: timeout} } func (c *Client) password() (string, error) { raw, err := os.ReadFile(c.PasswordFile) if err != nil { // The path is the manifest's, not a secret; the content never appears. return "", fmt.Errorf("the forge's admin password cannot be read from its file: %w", err) } return strings.TrimSpace(string(raw)), nil } // authorize puts the bundle's credential on a request: the tools' token, or the admin login. func (c *Client) authorize(req *http.Request) error { if c.Token != nil { token, err := c.Token.Token() if err != nil { return err } req.Header.Set("Authorization", "token "+token) return nil } password, err := c.password() if err != nil { return err } req.SetBasicAuth(c.User, password) return nil } // Do sends one request, with payload as its JSON body when not nil, and answers the body, read up to limit; // 404 is ErrNotFound. One byte past the limit is read, so a body the limit cut is told from one that fits it // exactly: a cut body is refused, never read as if it were whole. func (c *Client) Do(ctx context.Context, method, path string, payload any, limit int64) ([]byte, error) { if payload == nil { return c.Send(ctx, method, path, nil, "", limit) } raw, err := json.Marshal(payload) if err != nil { return nil, err } return c.Send(ctx, method, path, raw, "application/json", limit) } // Send is Do with a body of its own content type, sent as it is (a package's file uploaded into the registry); // a nil body sends none. func (c *Client) Send(ctx context.Context, method, path string, raw []byte, contentType string, limit int64) ([]byte, error) { var body io.Reader if raw != nil { body = bytes.NewReader(raw) } req, err := http.NewRequestWithContext(ctx, method, c.URL+path, body) if err != nil { return nil, err } if err := c.authorize(req); err != nil { return nil, err } req.Header.Set("Accept", "application/json") if raw != nil { req.Header.Set("Content-Type", contentType) } res, err := c.client().Do(req) if err != nil { return nil, fmt.Errorf("the forge did not answer %s %s: %w", method, path, err) } defer res.Body.Close() got, err := io.ReadAll(io.LimitReader(res.Body, limit+1)) if err != nil { return nil, err } if int64(len(got)) > limit { return nil, fmt.Errorf("the forge's answer to %s %s is larger than %d MiB and was not read whole", method, path, limit>>20) } switch { case res.StatusCode == http.StatusUnauthorized && c.Token != nil: return nil, fmt.Errorf("the forge rejected %s's token for %s (401): the module mints it afresh; nothing was done: %w", c.Account, method+" "+path, &StatusError{method, path, res.StatusCode, "unauthorized"}) case res.StatusCode == http.StatusNotFound: return nil, fmt.Errorf("%s %s: %w", method, path, ErrNotFound) case res.StatusCode >= 300: said := strings.TrimSpace(string(got)) if len(said) > 300 { said = said[:300] + "…" } return nil, &StatusError{method, path, res.StatusCode, said} } return got, nil } // GetJSON reads one answer into into. func (c *Client) GetJSON(ctx context.Context, path string, into any) error { return c.SendJSON(ctx, http.MethodGet, path, nil, into) } // SendJSON sends one request and reads its answer into into, when into is not nil. func (c *Client) SendJSON(ctx context.Context, method, path string, payload, into any) error { body, err := c.Do(ctx, method, path, payload, MaxBody) if err != nil { return err } if into == nil { return nil } if err := json.Unmarshal(body, into); err != nil { return fmt.Errorf("the forge's answer to %s %s is not readable: %w", method, path, err) } return nil }