feat: detect NetBox token version, hint on v1, better 403 message

- TokenVersion() distinguishes nbt_-prefixed v2 tokens from legacy v1
- 403 errors now say "check token permissions" + v1 hint if applicable
- Setup wizard prints a note after saving if a v1 token was entered

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Sebastian Unterschütz
2026-05-23 13:11:01 +02:00
parent ff9c61c087
commit 9334003c9e
2 changed files with 22 additions and 0 deletions
+16
View File
@@ -133,6 +133,14 @@ func (c *Client) searchVMs(ctx context.Context, query string) ([]HostEntry, erro
return entries, nil return entries, nil
} }
// TokenVersion returns 2 for NetBox v2 tokens (nbt_ prefix) or 1 for legacy tokens.
func TokenVersion(token string) int {
if strings.HasPrefix(token, "nbt_") {
return 2
}
return 1
}
func (c *Client) get(ctx context.Context, apiURL string, out any) error { func (c *Client) get(ctx context.Context, apiURL string, out any) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, nil) req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, nil)
if err != nil { if err != nil {
@@ -147,6 +155,14 @@ func (c *Client) get(ctx context.Context, apiURL string, out any) error {
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode == http.StatusForbidden {
hint := "check token permissions in NetBox"
if TokenVersion(c.token) == 1 {
hint += " — legacy v1 token detected, consider upgrading to a v2 token (starts with nbt_)"
}
return fmt.Errorf("%s: %s", apiURL, hint)
}
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return fmt.Errorf("netbox returned %d for %s", resp.StatusCode, apiURL) return fmt.Errorf("netbox returned %d for %s", resp.StatusCode, apiURL)
} }
+6
View File
@@ -11,6 +11,7 @@ import (
"github.com/charmbracelet/huh" "github.com/charmbracelet/huh"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config" "git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/netbox"
) )
// RunWizard runs the interactive setup form, pre-filled with any existing cfg values. // RunWizard runs the interactive setup form, pre-filled with any existing cfg values.
@@ -102,6 +103,11 @@ func RunWizard(cfg *config.Config) error {
return err return err
} }
if netbox.TokenVersion(token) == 1 {
fmt.Fprintln(os.Stderr, "\nHinweis: Du verwendest einen Legacy-Token (v1). Erstelle in NetBox einen v2-Token (beginnt mit nbt_) für bessere Kompatibilität.")
fmt.Fprintln(os.Stderr, " NetBox → Admin → API Tokens → Add Token")
}
ttl, _ := strconv.Atoi(cacheTTL) ttl, _ := strconv.Atoi(cacheTTL)
var subnetList []string var subnetList []string