feat(api): give status page servers sealed key-value storage

Certificates and ACME keys are stored encrypted in the database,
namespaced per owner so an organization's servers cannot read platform
or other organizations' keys. Locks live in Redis and expire.

Refs #22

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 08:54:08 +01:00
1 parent f52ae33cf0
commit 46f1f82481
11 files changed
+611 -1

No files matched your search

+108
View File
@@ -0,0 +1,108 @@
package agentapi
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
)
// StorageInfo describes a stored key; IsTerminal is false for keys that only
// exist as a prefix of others.
type StorageInfo struct {
Key string `json:"key"`
Modified time.Time `json:"modified"`
Size int64 `json:"size"`
IsTerminal bool `json:"is_terminal"`
}
// ErrLocked means another instance holds a storage lock.
var ErrLocked = errors.New("agentapi: locked")
func (c *Client) raw(ctx context.Context, method, path string, body []byte) ([]byte, error) {
var rd io.Reader
if body != nil {
rd = bytes.NewReader(body)
}
req, err := http.NewRequestWithContext(ctx, method, c.base+"/agent/v1"+path, rd)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+c.token)
req.Header.Set(VersionHeader, strconv.Itoa(Version))
req.Header.Set(InstanceHeader, c.instance.String())
req.Header.Set(ReleaseHeader, c.release)
resp, err := c.hc.Do(req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
out, err := io.ReadAll(io.LimitReader(resp.Body, 2<<20))
switch {
case resp.StatusCode == http.StatusNotFound:
return nil, ErrNotFound
case resp.StatusCode == http.StatusConflict:
return nil, ErrLocked
case resp.StatusCode == http.StatusUnauthorized:
return nil, ErrUnauthorized
case resp.StatusCode >= 300:
return nil, fmt.Errorf("agentapi: %s %s: %d", method, path, resp.StatusCode)
}
return out, err
}
func keyQuery(name, value string) string { return "?" + url.Values{name: {value}}.Encode() }
// StorageGet returns the value of key or ErrNotFound.
func (c *Client) StorageGet(ctx context.Context, key string) ([]byte, error) {
return c.raw(ctx, http.MethodGet, "/storage"+keyQuery("key", key), nil)
}
// StoragePut stores value under key.
func (c *Client) StoragePut(ctx context.Context, key string, value []byte) error {
if value == nil {
value = []byte{}
}
_, err := c.raw(ctx, http.MethodPut, "/storage"+keyQuery("key", key), value)
return err
}
// StorageDelete removes key and everything below it.
func (c *Client) StorageDelete(ctx context.Context, key string) error {
_, err := c.raw(ctx, http.MethodDelete, "/storage"+keyQuery("key", key), nil)
return err
}
// StorageStat describes key.
func (c *Client) StorageStat(ctx context.Context, key string) (StorageInfo, error) {
var info StorageInfo
err := c.do(ctx, http.MethodGet, "/storage/stat"+keyQuery("key", key), nil, &info)
return info, err
}
// StorageList lists keys below prefix.
func (c *Client) StorageList(ctx context.Context, prefix string, recursive bool) ([]string, error) {
var out struct {
Keys []string `json:"keys"`
}
q := url.Values{"prefix": {prefix}, "recursive": {strconv.FormatBool(recursive)}}
err := c.do(ctx, http.MethodGet, "/storage/list?"+q.Encode(), nil, &out)
return out.Keys, err
}
// StorageLock tries to take the lock name once.
func (c *Client) StorageLock(ctx context.Context, name string) error {
_, err := c.raw(ctx, http.MethodPost, "/storage/lock"+keyQuery("name", name), nil)
return err
}
// StorageUnlock releases the lock name.
func (c *Client) StorageUnlock(ctx context.Context, name string) error {
_, err := c.raw(ctx, http.MethodDelete, "/storage/lock"+keyQuery("name", name), nil)
return err
}