Files
57_Wolve 12bf35caf8 anchorage v1.0 initial tree
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.
2026-04-16 18:13:36 -05:00

111 lines
3.6 KiB
Go

package cmd
import (
"crypto/rand"
"encoding/base64"
"fmt"
"os"
"path/filepath"
"time"
"github.com/spf13/cobra"
)
// newRotateKeyCmd generates a fresh random signing key on disk and
// prints the YAML snippet an operator pastes into anchorage.yaml to
// register it alongside the existing primary key.
//
// The command is strictly offline — it never contacts a running
// anchorage. After writing the new key file + updating config, the
// operator restarts the fleet to pick up the secondary key, then at
// a later step flips `primary: true` onto it and restarts again.
//
// Full procedure lives in deploy/README.md under "Rotating the JWT
// signing key".
func newRotateKeyCmd(flags *globalFlags) *cobra.Command {
var (
outPath string
keyID string
bytes int
overwrite bool
)
c := &cobra.Command{
Use: "rotate-signing-key",
Short: "Generate a new JWT signing key and emit the config snippet for rotation overlap",
Long: `Generates a random HMAC key, writes it to --out with 0600 perms,
and prints a YAML fragment to stdout showing how to add it to your
anchorage.yaml under auth.apiToken.signingKeys.
This is the first step of a rotation; see deploy/README.md for the
full three-step procedure (add as secondary → flip primary → drop
retired key).
The command never touches a running anchorage. Restart the fleet
after editing the config.`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
if keyID == "" {
keyID = time.Now().UTC().Format("2006-01-02")
}
if outPath == "" {
outPath = filepath.Join(".", fmt.Sprintf("anchorage-jwt-%s.key", keyID))
}
if bytes < 32 {
return fmt.Errorf("bad --bytes %d: minimum 32", bytes)
}
if !overwrite {
if _, err := os.Stat(outPath); err == nil {
return fmt.Errorf("refusing to overwrite %s (pass --force to allow)", outPath)
}
}
raw := make([]byte, bytes)
if _, err := rand.Read(raw); err != nil {
return fmt.Errorf("read randomness: %w", err)
}
encoded := []byte(base64.StdEncoding.EncodeToString(raw))
if err := os.WriteFile(outPath, encoded, 0o600); err != nil {
return fmt.Errorf("write %s: %w", outPath, err)
}
// Primary output: the YAML snippet. Use stderr for the
// "wrote key" affordance so stdout stays clean if an operator
// wants to pipe it into their config.
fmt.Fprintf(cmd.ErrOrStderr(),
"# wrote %d-byte key to %s (base64-encoded, %d chars)\n",
bytes, outPath, len(encoded))
fmt.Fprintf(cmd.OutOrStdout(), `# Append this entry to auth.apiToken.signingKeys in anchorage.yaml.
# Keep the existing primary key entry; restart the fleet so every
# node loads both keys before flipping primary: true over here.
auth:
apiToken:
signingKeys:
# ... existing keys above, primary: true on ONE of them ...
- id: %q
path: %q
primary: false
`, keyID, absOrAs(outPath))
return nil
},
}
c.Flags().StringVar(&outPath, "out", "", "write the new key here (default: ./anchorage-jwt-<yyyy-mm-dd>.key)")
c.Flags().StringVar(&keyID, "id", "", "kid/label for the key (default: yyyy-mm-dd)")
c.Flags().IntVar(&bytes, "bytes", 48, "raw key length before base64 encoding (>=32)")
c.Flags().BoolVar(&overwrite, "force", false, "overwrite an existing file at --out")
return c
}
// absOrAs returns an absolute form of p if that succeeds, else p as given.
// Used purely for the YAML snippet — operators tend to run the command
// from /etc/anchorage or similar, and pasting an absolute path is
// friendlier than a relative one.
func absOrAs(p string) string {
if abs, err := filepath.Abs(p); err == nil {
return abs
}
return p
}