ARTEX/server/proxy_pool.go
Grady May a383e5bcb2 feat(db): store proxy pool entries behind a masked API
Part 1 of 4 of the proxy pool design in #545: storage and a CRUD API.
Nothing activates an entry yet, so the global proxy is unchanged.

- Migration 0028 adds proxy_pool: a unique keyed address hash, at most
  one active entry (partial unique index), a CHECK that the active entry
  is enabled, and nullable egress_ip, last_check_at and last_check_ok for
  the health check.
- Addresses are normalized, sealed at rest (AAD proxy_pool.address) and
  covered by the startup key check and key rotation. Uniqueness uses an
  HMAC under a sub-key kept on secretBox; RotateSecretKey locks the table
  and rehashes in its transaction, and pool writes seal under that lock.
- GET/POST /api/proxy-pool and PATCH/DELETE /api/proxy-pool/{id} never
  return an address; a masked address on update keeps the stored one.
2026-12-02 22:05:05 -05:00

157 lines
4.6 KiB
Go

package server
import (
"database/sql"
"errors"
"log"
"net/http"
"time"
"github.com/Autumn-27/artex/db"
)
// --- proxy pool (#545) ---
//
// Stored candidates for the global egress proxy. Operator API only, never an
// agent tool. This layer stores entries; nothing activates one yet, so the
// global proxy is unaffected by every call here.
//
// An address is never returned: reads give maskedSecret and address_set. On
// update, an address that is absent or masked keeps the stored one.
type proxyPoolDTO struct {
ID int64 `json:"id"`
Label string `json:"label"`
Address string `json:"address"` // maskedSecret, never the address
AddressSet bool `json:"address_set"`
Enabled bool `json:"enabled"`
Active bool `json:"active"`
EgressIP *string `json:"egress_ip"` // null: never checked
LastCheckAt *time.Time `json:"last_check_at"` // null: never checked
LastCheckOK *bool `json:"last_check_ok"` // null: never checked
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func proxyPoolEntryDTO(e db.ProxyPoolEntry) proxyPoolDTO {
return proxyPoolDTO{ID: e.ID, Label: e.Label, Address: maskedSecret(e.Address), AddressSet: e.Address != "",
Enabled: e.Enabled, Active: e.Active, EgressIP: e.EgressIP, LastCheckAt: e.LastCheckAt, LastCheckOK: e.LastCheckOK,
CreatedAt: e.CreatedAt, UpdatedAt: e.UpdatedAt}
}
type proxyPoolCreateReq struct {
Label string `json:"label"`
Address string `json:"address"`
Enabled *bool `json:"enabled"` // default true
}
type proxyPoolPatchReq struct {
Label *string `json:"label"`
Address *string `json:"address"` // null or masked: keep
Enabled *bool `json:"enabled"`
}
// proxyPoolWriteErr answers a failed entry write.
func proxyPoolWriteErr(w http.ResponseWriter, err error) {
switch {
case errors.Is(err, sql.ErrNoRows):
writeErr(w, 404, "proxy pool entry not found")
case errors.Is(err, db.ErrProxyPoolInvalid), errors.Is(err, db.ErrProxyPoolLimit):
writeErr(w, 400, err.Error())
case errors.Is(err, db.ErrProxyPoolDuplicate), errors.Is(err, db.ErrProxyPoolActive):
writeErr(w, 409, err.Error())
default:
proxyPoolStoreErr(w, err)
}
}
// proxyPoolStoreErr answers an unexpected store error with a fixed message:
// this API handles proxy credentials, so no store detail reaches the client.
// The log gets the error, which never carries an address: addresses reach
// the database sealed, and constraint errors name only the column.
func proxyPoolStoreErr(w http.ResponseWriter, err error) {
log.Printf("[proxy-pool] store error: %v", err)
writeErr(w, 500, "proxy pool store error")
}
func (s *Server) proxyPoolList(w http.ResponseWriter, r *http.Request) {
pg := s.pg(w)
if pg == nil {
return
}
entries, err := pg.ListProxyPool()
if err != nil {
proxyPoolStoreErr(w, err)
return
}
out := make([]proxyPoolDTO, 0, len(entries))
for _, e := range entries {
out = append(out, proxyPoolEntryDTO(e))
}
writeJSON(w, 200, map[string]any{"entries": out})
}
// proxyPoolCreate stores a new entry, never active: creating an entry does not
// change the global proxy.
func (s *Server) proxyPoolCreate(w http.ResponseWriter, r *http.Request) {
pg := s.pg(w)
if pg == nil {
return
}
var req proxyPoolCreateReq
if err := decode(w, r, &req); err != nil {
writeErr(w, 400, err.Error())
return
}
if isMaskedSecret(req.Address) {
writeErr(w, 400, "send the proxy address: nothing is stored for a new entry")
return
}
enabled := req.Enabled == nil || *req.Enabled
e, err := pg.CreateProxyPoolEntry(req.Label, req.Address, enabled)
if err != nil {
proxyPoolWriteErr(w, err)
return
}
writeJSON(w, 200, proxyPoolEntryDTO(e))
}
func (s *Server) proxyPoolUpdate(w http.ResponseWriter, r *http.Request) {
pg := s.pg(w)
if pg == nil {
return
}
id, ok := pathInt(r, "id")
if !ok {
writeErr(w, 400, "bad proxy pool entry id")
return
}
var req proxyPoolPatchReq
if err := decode(w, r, &req); err != nil {
writeErr(w, 400, err.Error())
return
}
e, err := pg.UpdateProxyPoolEntry(id, db.ProxyPoolEdit{Label: req.Label, Address: keepIfMasked(req.Address), Enabled: req.Enabled})
if err != nil {
proxyPoolWriteErr(w, err)
return
}
writeJSON(w, 200, proxyPoolEntryDTO(e))
}
func (s *Server) proxyPoolDelete(w http.ResponseWriter, r *http.Request) {
pg := s.pg(w)
if pg == nil {
return
}
id, ok := pathInt(r, "id")
if !ok {
writeErr(w, 400, "bad proxy pool entry id")
return
}
if err := pg.DeleteProxyPoolEntry(id); err != nil {
proxyPoolWriteErr(w, err)
return
}
writeJSON(w, 200, map[string]any{"deleted": id})
}