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-.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 }