mirror of
https://github.com/warmbly/warmbly.git
synced 2026-12-04 00:06:18 +00:00
* feat: offer fleet nodes the backend's private TLS address through opt-in NODE_BACKEND_INTERNAL_URL and NODE_BACKEND_INTERNAL_CA (rendered as WARMBLY_BACKEND_INTERNAL_URL and a base64 CA beside the unchanged public address), have workers and consumers probe it at startup and route every control-plane client through one CA-pinned transport or fall back to the public URL, and add the workers root's backend_internal_port rule and the runtime root's nodes_private_backend switch * feat: replace a node's private control-plane connections every 2 minutes over HTTP/1.1 so new backend tasks behind the Cloud Map name get node traffic, resend a request whose private dial, DNS or TLS handshake failed once to the public address (replayable bodies only, never after the request was written), and route all node calls to the public address for 5 minutes after 3 dial failures in a row, logging each switch once * feat: keep a node's control-plane call from being resent to the public address once any private attempt wrote its headers, send the caller's own body on a first send during a public window, and mark the backend's NODE_BACKEND_INTERNAL_URL and CA settings as needing a backend restart in the configuration docs
153 lines
4.1 KiB
Go
153 lines
4.1 KiB
Go
package encryptedkeys
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/warmbly/warmbly/internal/pkg/nodeauth"
|
|
"github.com/warmbly/warmbly/internal/pkg/nodebackend"
|
|
)
|
|
|
|
// HTTPStore is the worker-side adapter. Workers cannot connect to Postgres
|
|
// directly (per CLAUDE.md), so they reach DEKs over HTTPS by calling the
|
|
// backend's internal DEK endpoint, which proxies to the chosen durable store.
|
|
//
|
|
// The endpoint contract (served by internal/api/handler/internal_dek.go):
|
|
//
|
|
// GET {BaseURL}/api/v1/internal/dek/{orgID}
|
|
// 200 {"encrypted_data_key":"..."} -> success
|
|
// 404 -> no DEK stored (returns "")
|
|
//
|
|
// PUT {BaseURL}/api/v1/internal/dek/{orgID} body: {"encrypted_data_key":"..."}
|
|
// 201 -> created
|
|
// 409 -> ErrAlreadyExists
|
|
//
|
|
// Auth: Authorization: Bearer <ENCRYPTED_KEYS_WORKER_TOKEN>
|
|
type HTTPStore struct {
|
|
baseURL string
|
|
token string
|
|
client *http.Client
|
|
}
|
|
|
|
// HTTPOption configures HTTPStore.
|
|
type HTTPOption func(*HTTPStore)
|
|
|
|
// WithHTTPClient overrides the underlying http.Client (useful for tests and
|
|
// for callers that need custom transports such as netbind.Dialer).
|
|
func WithHTTPClient(c *http.Client) HTTPOption {
|
|
return func(s *HTTPStore) { s.client = c }
|
|
}
|
|
|
|
// WithControlPlane sends the store to the node's private control-plane address when it chose one.
|
|
func WithControlPlane(node nodebackend.Backend) HTTPOption {
|
|
return func(s *HTTPStore) {
|
|
if node.Private() {
|
|
s.baseURL = node.BaseURL(s.baseURL)
|
|
s.client.Transport = node.Transport()
|
|
}
|
|
}
|
|
}
|
|
|
|
func NewHTTP(baseURL, token string, opts ...HTTPOption) (*HTTPStore, error) {
|
|
if baseURL == "" {
|
|
return nil, errors.New("encryptedkeys.http: baseURL is required")
|
|
}
|
|
if token == "" {
|
|
return nil, errors.New("encryptedkeys.http: token is required")
|
|
}
|
|
if _, err := url.Parse(baseURL); err != nil {
|
|
return nil, fmt.Errorf("encryptedkeys.http: invalid baseURL: %w", err)
|
|
}
|
|
s := &HTTPStore{
|
|
baseURL: strings.TrimRight(baseURL, "/"),
|
|
token: token,
|
|
client: &http.Client{Timeout: 10 * time.Second},
|
|
}
|
|
for _, o := range opts {
|
|
o(s)
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
func (s *HTTPStore) Name() string { return "http" }
|
|
|
|
func (s *HTTPStore) url(orgID uuid.UUID) string {
|
|
return s.baseURL + "/api/v1/internal/dek/" + orgID.String()
|
|
}
|
|
|
|
func (s *HTTPStore) authed(req *http.Request) {
|
|
nodeauth.Authorize(req, s.token)
|
|
req.Header.Set("User-Agent", "warmbly-worker/encryptedkeys-http")
|
|
}
|
|
|
|
type dekPayload struct {
|
|
EncryptedDataKey string `json:"encrypted_data_key"`
|
|
}
|
|
|
|
func (s *HTTPStore) Put(ctx context.Context, orgID uuid.UUID, encryptedDEKB64 string) error {
|
|
body, err := json.Marshal(dekPayload{EncryptedDataKey: encryptedDEKB64})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPut, s.url(orgID), strings.NewReader(string(body)))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.authed(req)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := s.client.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("encryptedkeys.http: put: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
switch resp.StatusCode {
|
|
case http.StatusCreated:
|
|
return nil
|
|
case http.StatusConflict:
|
|
return ErrAlreadyExists
|
|
default:
|
|
return fmt.Errorf("encryptedkeys.http: put: unexpected status %d", resp.StatusCode)
|
|
}
|
|
}
|
|
|
|
func (s *HTTPStore) Get(ctx context.Context, orgID uuid.UUID) (string, error) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.url(orgID), nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
s.authed(req)
|
|
|
|
resp, err := s.client.Do(req)
|
|
if err != nil {
|
|
return "", fmt.Errorf("encryptedkeys.http: get: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
switch resp.StatusCode {
|
|
case http.StatusNotFound:
|
|
return "", nil
|
|
case http.StatusOK:
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
var p dekPayload
|
|
if err := json.Unmarshal(body, &p); err != nil {
|
|
return "", fmt.Errorf("encryptedkeys.http: decode: %w", err)
|
|
}
|
|
return p.EncryptedDataKey, nil
|
|
default:
|
|
return "", fmt.Errorf("encryptedkeys.http: get: unexpected status %d", resp.StatusCode)
|
|
}
|
|
}
|