Greenfield Go multi-tenant IPFS Pinning Service wire-compatible with the
IPFS Pinning Services API spec. Paired 1:1 with Kubo over localhost RPC,
clustered via embedded NATS JetStream, Postgres source-of-truth with
RLS-enforced tenancy, Fiber + huma v2 for the HTTP surface, Authentik
OIDC for session login with kid-rotated HS256 JWT API tokens.
Feature-complete against the 22-milestone build plan, including the
ship-it v1.0 gap items:
* admin CLIs: drain/uncordon, maintenance, mint-token, rotate-key,
prune-denylist, rebalance --dry-run, cache-stats, cluster-presences
* TTL leader election via NATS KV, fence tokens, JetStream dedup
* rebalancer (plan/apply split), reconciler, requeue sweeper
* ristretto caches with NATS-backed cross-node invalidation
(placements live-nodes + token denylist)
* maintenance watchdog for stuck cluster-pause flag
* Prometheus /metrics with CIDR ACL, HTTP/pin/scheduler/cache gauges
* rate limiting: session (10/min) + anonymous global (120/min)
* integration tests: rebalance, refcount multi-org, RLS belt
* goreleaser (tar + deb/rpm/apk + Alpine Docker) targeting Gitea
Stack: Cobra/Viper, Fiber v2 + huma v2, embedded NATS JetStream,
pgx/sqlc/golang-migrate, ristretto, TypeID, prometheus/client_golang,
testcontainers-go.
116 lines
3.3 KiB
Go
116 lines
3.3 KiB
Go
package token_test
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"anchorage/internal/pkg/ids"
|
|
"anchorage/internal/pkg/token"
|
|
)
|
|
|
|
// TestLoadSigningKeyRoundTrip mirrors the CLI → server handshake:
|
|
// an operator runs `anchorage admin mint-token` (which reads the key
|
|
// via LoadSigningKeys + NewSigner + Mint), hands the JWT to an IPFS
|
|
// client, which then hits a running anchorage whose Signer was built
|
|
// from the same config. Both signers must accept the token.
|
|
func TestLoadSigningKeyRoundTrip(t *testing.T) {
|
|
dir := t.TempDir()
|
|
keyPath := filepath.Join(dir, "jwt.key")
|
|
// 48-byte key with a trailing newline, matching what
|
|
// `openssl rand -base64 48 > jwt.key` writes.
|
|
if err := os.WriteFile(keyPath, []byte("0123456789abcdef0123456789abcdef0123456789abcdef\n"), 0o600); err != nil {
|
|
t.Fatalf("write key: %v", err)
|
|
}
|
|
|
|
spec := []token.KeyFileSpec{
|
|
{ID: "test-kid", Path: keyPath, Primary: true},
|
|
}
|
|
|
|
mintKeys, err := token.LoadSigningKeys(spec)
|
|
if err != nil {
|
|
t.Fatalf("LoadSigningKeys (mint): %v", err)
|
|
}
|
|
verifyKeys, err := token.LoadSigningKeys(spec)
|
|
if err != nil {
|
|
t.Fatalf("LoadSigningKeys (verify): %v", err)
|
|
}
|
|
|
|
mintSigner, err := token.NewSigner(mintKeys, "https://anchorage.test/", "anchorage", nil)
|
|
if err != nil {
|
|
t.Fatalf("NewSigner (mint): %v", err)
|
|
}
|
|
jwt, claims, err := mintSigner.Mint(
|
|
context.Background(),
|
|
ids.MustNewOrg(), ids.MustNewUser(),
|
|
"sysadmin", []string{"pin:write"},
|
|
365*24*time.Hour+30*24*time.Hour, // 1y + 30d
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("Mint: %v", err)
|
|
}
|
|
if claims.Role != "sysadmin" {
|
|
t.Errorf("claims.Role = %q", claims.Role)
|
|
}
|
|
|
|
verifySigner, err := token.NewSigner(verifyKeys, "https://anchorage.test/", "anchorage", nil)
|
|
if err != nil {
|
|
t.Fatalf("NewSigner (verify): %v", err)
|
|
}
|
|
parsed, err := verifySigner.Parse(context.Background(), jwt)
|
|
if err != nil {
|
|
t.Fatalf("Parse: %v", err)
|
|
}
|
|
if parsed.ID != claims.ID {
|
|
t.Errorf("jti drift: minted=%q parsed=%q", claims.ID, parsed.ID)
|
|
}
|
|
if parsed.Role != "sysadmin" {
|
|
t.Errorf("parsed.Role = %q, want sysadmin", parsed.Role)
|
|
}
|
|
}
|
|
|
|
func TestLoadSigningKeysRejectsMissingFile(t *testing.T) {
|
|
_, err := token.LoadSigningKeys([]token.KeyFileSpec{
|
|
{ID: "x", Path: "/does/not/exist/anywhere", Primary: true},
|
|
})
|
|
if err == nil {
|
|
t.Error("expected error for missing file")
|
|
}
|
|
}
|
|
|
|
func TestLoadSigningKeysRejectsShortKey(t *testing.T) {
|
|
dir := t.TempDir()
|
|
p := filepath.Join(dir, "short.key")
|
|
_ = os.WriteFile(p, []byte("tooshort"), 0o600)
|
|
_, err := token.LoadSigningKeys([]token.KeyFileSpec{
|
|
{ID: "x", Path: p, Primary: true},
|
|
})
|
|
if err == nil {
|
|
t.Error("expected error for short key")
|
|
}
|
|
}
|
|
|
|
// TestLoadSigningKeysEmptyReturnsDevKey covers the zero-config path —
|
|
// no signingKeys entries → built-in dev key with kid=DevKeyID. A loud
|
|
// warning fires in logs; tests don't assert on that, just on the shape.
|
|
func TestLoadSigningKeysEmptyReturnsDevKey(t *testing.T) {
|
|
keys, err := token.LoadSigningKeys(nil)
|
|
if err != nil {
|
|
t.Fatalf("empty should not error: %v", err)
|
|
}
|
|
if len(keys) != 1 {
|
|
t.Fatalf("want 1 key, got %d", len(keys))
|
|
}
|
|
if keys[0].ID != token.DevKeyID {
|
|
t.Errorf("dev key ID = %q, want %q", keys[0].ID, token.DevKeyID)
|
|
}
|
|
if !keys[0].Primary {
|
|
t.Error("dev key must be marked primary")
|
|
}
|
|
if string(keys[0].Key) != token.DevOnlySigningKey {
|
|
t.Error("dev key bytes drift")
|
|
}
|
|
}
|