Skip to content

feat: add support for NetBox v2 API tokens#862

Merged
fbreckle merged 2 commits intoe-breuninger:masterfrom
MrKeiKun:feat/v2-api-token-support
Apr 9, 2026
Merged

feat: add support for NetBox v2 API tokens#862
fbreckle merged 2 commits intoe-breuninger:masterfrom
MrKeiKun:feat/v2-api-token-support

Conversation

@MrKeiKun
Copy link
Copy Markdown
Contributor

@MrKeiKun MrKeiKun commented Apr 8, 2026

Summary

NetBox v2 tokens use Authorization: Bearer nbt_<key>.<token> instead of Authorization: Token <token> (v1). This PR adds auto-detection based on the nbt_ prefix — the same check used by NetBox itself in netbox/api/authentication.py — so no additional provider configuration is needed.

Also corrects the key field validation on netbox_token to cover both token versions: v2 keys are 12 chars, v1 plaintexts are 40 chars.

Token format reference (from NetBox source)

Constant Value
TOKEN_PREFIX nbt_ (4 chars)
TOKEN_KEY_LENGTH 12 chars
TOKEN_DEFAULT_LENGTH 40 chars

Full bearer token: nbt_<12-char-key>.<40-char-token> = 57 chars total

Usage

provider "netbox" {
  server_url = "https://netbox.example.com"
  api_token  = "nbt_abc123456789.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  # v2, auto-detected
}

Comment thread netbox/resource_netbox_token.go Outdated
Sensitive: true,
Optional: true,
ValidateFunc: validation.StringLenBetween(40, 256),
ValidateFunc: validation.StringLenBetween(12, 40),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did you get the 12 from? I can create tokens with key "123456789" just fine.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo, just drop this part of the MR and stick to the authentication header part. Then we handle the netbox_token resource in another MR.

@fbreckle
Copy link
Copy Markdown
Collaborator

fbreckle commented Apr 9, 2026

LGTM and works in my local tests. Run make docs and consider my latest comment, then we're good to go.

Comment thread netbox/resource_netbox_token.go Outdated
Sensitive: true,
Optional: true,
ValidateFunc: validation.StringLenBetween(40, 256),
ValidateFunc: validation.StringLenBetween(12, 40),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo, just drop this part of the MR and stick to the authentication header part. Then we handle the netbox_token resource in another MR.

Auto-detect v2 tokens by their nbt_ prefix and use the Bearer
authorization scheme instead of Token. V1 tokens are unchanged.

Update api_token provider description to document v2 support and
add tests verifying the correct auth scheme is sent for each token type.
@MrKeiKun MrKeiKun force-pushed the feat/v2-api-token-support branch from cf02ebb to 4059cb3 Compare April 9, 2026 12:19
@fbreckle fbreckle merged commit 4e068f0 into e-breuninger:master Apr 9, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants