Documentation
¶
Index ¶
- Constants
- Variables
- func ClassifiedRateLimiter(cfg RateLimitConfig) func(http.Handler) http.Handler
- func ClientIP(trusted *TrustedProxies) func(http.Handler) http.Handler
- func ClientIPFromRequest(r *http.Request) string
- func GenerateKey() (string, error)
- func InitLogger()
- func IsDuplicateKey(err error) bool
- func LiveRateLimiter(get func() RateLimitConfig) func(http.Handler) http.Handler
- func MetricsMiddleware(next http.Handler) http.Handler
- func Migrate(db *sql.DB) error
- func MigrateDown(db *sql.DB) error
- func MigrateStatus(db *sql.DB) error
- func MigrateUpTo(db *sql.DB, version int64) error
- func NewPool(ctx context.Context, databaseURL string, cfg *PoolConfig) (*pgxpool.Pool, error)
- func OpenDB(databaseURL string) (*sql.DB, error)
- func RequestID(next http.Handler) http.Handler
- func RequestIDFromContext(ctx context.Context) string
- func RequestLogger(next http.Handler) http.Handler
- func RequestScheme(r *http.Request, trusted *TrustedProxies) string
- func RunMigrations(ctx context.Context, pool *pgxpool.Pool) error
- func SecurityHeaders(cookieSecure bool) func(http.Handler) http.Handler
- type Config
- type Keyring
- type LocalStorage
- type PoolConfig
- type PostgresStorage
- func (s *PostgresStorage) Delete(ctx context.Context, name string) error
- func (s *PostgresStorage) Ping(ctx context.Context) error
- func (s *PostgresStorage) Read(ctx context.Context, name string) (io.ReadCloser, error)
- func (s *PostgresStorage) Write(ctx context.Context, name string, r io.Reader) (string, error)
- type RateLimitConfig
- type S3Storage
- type Storage
- type TrustedProxies
Constants ¶
const ( BackendFilesystem = "filesystem" BackendPostgres = "postgres" )
Archive store kinds recorded in storage_backend.
Variables ¶
var ErrNoMasterKey = errors.New("no master key: set SKAEL_MASTER_KEYS")
ErrNoMasterKey means no master key can seal or open a value.
Functions ¶
func ClassifiedRateLimiter ¶ added in v0.9.0
func ClassifiedRateLimiter(cfg RateLimitConfig) func(http.Handler) http.Handler
ClassifiedRateLimiter returns middleware with a separate per-minute budget per route class. Auth is IP-only; other classes check an IP ceiling first (stops forged-key bursts before they grow the per-subject map), then a per-subject budget keyed by API key or IP.
func ClientIP ¶ added in v0.9.0
func ClientIP(trusted *TrustedProxies) func(http.Handler) http.Handler
ClientIP resolves the client address and stores it on the context. Replaces chi's RealIP, which trusts forwarding headers unconditionally — here they are read only when the peer is a configured trusted proxy.
func ClientIPFromRequest ¶ added in v0.9.0
ClientIPFromRequest returns the resolved client address, falling back to the socket address when the ClientIP middleware is not installed.
func GenerateKey ¶ added in v0.19.0
GenerateKey returns a new "id:base64key" pair with a random id.
func InitLogger ¶
func InitLogger()
InitLogger configures zerolog. When LOG_FORMAT=pretty (or LOG_PRETTY=true), uses colorized console output for development. Otherwise outputs JSON for production/log aggregation.
func IsDuplicateKey ¶
IsDuplicateKey returns true if err is a Postgres unique-violation (SQLSTATE 23505).
func LiveRateLimiter ¶ added in v0.19.0
func LiveRateLimiter(get func() RateLimitConfig) func(http.Handler) http.Handler
LiveRateLimiter is ClassifiedRateLimiter with budgets read on each request. A changed budget starts new, empty buckets.
func MigrateDown ¶
func MigrateStatus ¶
func MigrateUpTo ¶ added in v0.10.0
MigrateUpTo applies migrations up to and including version, leaving later ones unapplied. It exists so a migration can be tested against a database populated at the prior version — the only way that test means anything. A test that opens a fully-migrated database and then "upgrades" it passes with the migration deleted.
func RequestID ¶ added in v0.7.0
RequestID is a Chi-compatible middleware that reads X-Request-ID from the incoming request, or generates a UUID if absent. It sets the ID on the response header and in the request context.
func RequestIDFromContext ¶ added in v0.7.0
RequestIDFromContext returns the request ID stored in ctx, or "".
func RequestLogger ¶
RequestLogger is a Chi-compatible middleware that logs every request using zerolog. Logs method, path, status, duration, and client IP.
The IP is the one ClientIP resolved, so the logs name the same client the rate limiter charged — reading a 429 against the wrong address would make every limiter incident impossible to trace.
func RequestScheme ¶ added in v0.16.0
func RequestScheme(r *http.Request, trusted *TrustedProxies) string
RequestScheme returns the scheme the client used. X-Forwarded-Proto counts only from a trusted proxy, the same rule as the client address.
Types ¶
type Config ¶
type Config struct {
DatabaseURL string
StoragePath string
ListenAddr string
DBMaxConns int
DBMinConns int
DBMaxConnLifetime time.Duration
DBMaxConnIdleTime time.Duration
DBHealthCheckPeriod time.Duration
CORSOrigins string
// TrustedProxies is the comma-separated list of proxy addresses and CIDR
// blocks whose X-Forwarded-For / X-Real-IP headers may be believed. Empty
// means trust none of them — see ParseTrustedProxies.
TrustedProxies string
CookieSecure bool
MetricsEnabled bool
// MasterKeys seals secrets stored from the UI, such as the GitHub App's
// private key. Empty is valid: features that store a secret stay off.
MasterKeys *Keyring
}
Config holds the settings the server needs before it reads its database. internal/settings builds it from the environment; every other setting is in the settings snapshot.
type Keyring ¶ added in v0.18.0
type Keyring struct {
// contains filtered or unexported fields
}
Keyring holds the master keys from SKAEL_MASTER_KEYS. The first key seals; every key opens, so a rotated-out key keeps old values readable until they are sealed again.
func ParseKeyring ¶ added in v0.18.0
ParseKeyring parses "id:base64key" pairs separated by commas. Each key must decode to 32 bytes. An empty value is a keyring with no keys.
func (*Keyring) Fingerprint ¶ added in v0.19.0
Fingerprint identifies the active key without revealing it: the first 16 hex digits of a SHA-256 of the key.
func (*Keyring) Open ¶ added in v0.18.0
Open decrypts a value Seal produced, with the key its id names.
type LocalStorage ¶ added in v0.5.0
type LocalStorage struct {
BasePath string
}
LocalStorage provides local filesystem storage for skill archive files.
func NewLocalStorage ¶ added in v0.5.0
func NewLocalStorage(basePath string) (*LocalStorage, error)
NewLocalStorage creates a LocalStorage rooted at basePath, creating the directory if it does not already exist.
func (*LocalStorage) Delete ¶ added in v0.5.0
func (s *LocalStorage) Delete(_ context.Context, name string) error
Delete removes the file stored under name (relative to BasePath).
func (*LocalStorage) Ping ¶ added in v0.6.0
func (s *LocalStorage) Ping(_ context.Context) error
Ping verifies the base path still exists and is a directory.
func (*LocalStorage) Read ¶ added in v0.5.0
func (s *LocalStorage) Read(_ context.Context, name string) (io.ReadCloser, error)
Read opens the file stored under name (relative to BasePath) for reading. The caller is responsible for closing the returned ReadCloser.
func (*LocalStorage) Write ¶ added in v0.5.0
Write stores the content from r under name (relative to BasePath). It uses an atomic write: content is first written to a .tmp file which is then renamed to the final destination, ensuring no partial files are visible. Returns the full path of the written file.
type PoolConfig ¶ added in v0.7.0
type PoolConfig struct {
MaxConns int
MinConns int
MaxConnLifetime time.Duration
MaxConnIdleTime time.Duration
HealthCheckPeriod time.Duration
}
PoolConfig holds optional pgxpool tuning parameters. A nil value keeps pgxpool's built-in defaults.
type PostgresStorage ¶ added in v0.19.0
type PostgresStorage struct {
// contains filtered or unexported fields
}
PostgresStorage keeps archives in the archives table, so one database backup holds everything.
func NewPostgresStorage ¶ added in v0.19.0
func NewPostgresStorage(pool *pgxpool.Pool) *PostgresStorage
NewPostgresStorage returns a store on pool.
func (*PostgresStorage) Delete ¶ added in v0.19.0
func (s *PostgresStorage) Delete(ctx context.Context, name string) error
Delete removes the archive under name.
func (*PostgresStorage) Ping ¶ added in v0.19.0
func (s *PostgresStorage) Ping(ctx context.Context) error
Ping checks that the archives table answers.
func (*PostgresStorage) Read ¶ added in v0.19.0
func (s *PostgresStorage) Read(ctx context.Context, name string) (io.ReadCloser, error)
Read returns the archive under name. A missing archive is fs.ErrNotExist.
type RateLimitConfig ¶ added in v0.9.0
RateLimitConfig holds the per-minute request budget for each route class.
type S3Storage ¶ added in v0.5.0
type S3Storage struct {
// contains filtered or unexported fields
}
S3Storage stores archives in any S3-compatible object store.
type Storage ¶
type Storage interface {
// Write stores r under name and returns the stored key (name).
Write(ctx context.Context, name string, r io.Reader) (string, error)
// Read opens the blob stored under name; caller closes the ReadCloser.
Read(ctx context.Context, name string) (io.ReadCloser, error)
// Delete removes the blob stored under name.
Delete(ctx context.Context, name string) error
// Ping verifies the backing store is reachable (readiness checks).
Ping(ctx context.Context) error
}
Storage abstracts archive blob storage (local filesystem or object storage). Names are relative keys, e.g. "code-review/abc123.tar.gz".
func NewStorageFromConfig ¶ added in v0.5.0
NewStorageFromConfig builds the Storage backend selected by STORAGE_PATH: "s3://bucket/prefix" → S3; anything else → local filesystem.
func OpenStorage ¶ added in v0.19.0
func OpenStorage(ctx context.Context, pool *pgxpool.Pool, storagePath, defaultPath string) (Storage, string, error)
OpenStorage returns the archive store. A set storagePath wins. Unset, the install keeps the store it started with, and a new install uses Postgres. defaultPath is the filesystem store of an install that started on it.
type TrustedProxies ¶ added in v0.9.0
type TrustedProxies struct {
// contains filtered or unexported fields
}
TrustedProxies is the set of peers whose forwarding headers may be believed. The zero value trusts nobody — correct for a directly-exposed server.
func ParseTrustedProxies ¶ added in v0.9.0
func ParseTrustedProxies(list string) *TrustedProxies
ParseTrustedProxies parses a comma-separated TRUSTED_PROXIES list of CIDR blocks or bare addresses. Unparseable entries are dropped with a warning — dropping is safe: a typo costs forwarded addresses, never spoofable ones.
func (*TrustedProxies) Contains ¶ added in v0.9.0
func (t *TrustedProxies) Contains(addr netip.Addr) bool
Contains reports whether addr belongs to a trusted proxy range.
func (*TrustedProxies) Empty ¶ added in v0.9.0
func (t *TrustedProxies) Empty() bool
Empty reports whether no proxy is trusted.