Skip to content

Commit cf02ebb

Browse files
committed
feat: add support for NetBox v2 API tokens
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.
1 parent 74fb89e commit cf02ebb

4 files changed

Lines changed: 47 additions & 3 deletions

File tree

netbox/client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package netbox
33
import (
44
"fmt"
55
"net/http"
6+
"strings"
67
"time"
78

89
netboxclient "github.com/fbreckle/go-netbox/netbox/client"
@@ -81,7 +82,11 @@ func (cfg *Config) Client() (*netboxclient.NetBoxAPI, error) {
8182
}
8283

8384
transport := httptransport.NewWithClient(parsedURL.Host, parsedURL.Path+netboxclient.DefaultBasePath, desiredRuntimeClientSchemes, httpClient)
84-
transport.DefaultAuthentication = httptransport.APIKeyAuth("Authorization", "header", fmt.Sprintf("Token %v", cfg.APIToken))
85+
authScheme := "Token"
86+
if strings.HasPrefix(cfg.APIToken, "nbt_") {
87+
authScheme = "Bearer"
88+
}
89+
transport.DefaultAuthentication = httptransport.APIKeyAuth("Authorization", "header", fmt.Sprintf("%s %v", authScheme, cfg.APIToken))
8590
transport.SetLogger(log.StandardLogger())
8691
netboxClient := netboxclient.New(transport, nil)
8792

netbox/client_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,45 @@ func TestAdditionalHeadersSet(t *testing.T) {
8787
client.Status.StatusList(req, nil)
8888
}
8989

90+
func TestV1TokenUsesTokenScheme(t *testing.T) {
91+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
92+
auth := r.Header.Get("Authorization")
93+
assert.Equal(t, "Token 07b12b765127747e4afd56cb531b7bf9c61f3c30", auth)
94+
}))
95+
defer ts.Close()
96+
97+
config := Config{
98+
APIToken: "07b12b765127747e4afd56cb531b7bf9c61f3c30",
99+
ServerURL: ts.URL,
100+
}
101+
102+
client, err := config.Client()
103+
assert.NoError(t, err)
104+
105+
req := status.NewStatusListParams()
106+
client.Status.StatusList(req, nil)
107+
}
108+
109+
func TestV2TokenUsesBearerScheme(t *testing.T) {
110+
v2Token := "nbt_abc1234567890abcdef.checksum1234"
111+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
112+
auth := r.Header.Get("Authorization")
113+
assert.Equal(t, "Bearer "+v2Token, auth)
114+
}))
115+
defer ts.Close()
116+
117+
config := Config{
118+
APIToken: v2Token,
119+
ServerURL: ts.URL,
120+
}
121+
122+
client, err := config.Client()
123+
assert.NoError(t, err)
124+
125+
req := status.NewStatusListParams()
126+
client.Status.StatusList(req, nil)
127+
}
128+
90129
/* TODO
91130
func TestInvalidHttpsCertificate(t *testing.T) {}
92131
*/

netbox/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func Provider() *schema.Provider {
225225
Type: schema.TypeString,
226226
Required: true,
227227
DefaultFunc: schema.EnvDefaultFunc("NETBOX_API_TOKEN", nil),
228-
Description: "Netbox API authentication token. Can be set via the `NETBOX_API_TOKEN` environment variable.",
228+
Description: "Netbox API authentication token. Supports both v1 tokens (`Authorization: Token <key>`) and v2 tokens (`Authorization: Bearer nbt_<key>.<token>`). V2 tokens are auto-detected by their `nbt_` prefix. Can be set via the `NETBOX_API_TOKEN` environment variable.",
229229
},
230230
"allow_insecure_https": {
231231
Type: schema.TypeBool,

netbox/resource_netbox_token.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func resourceNetboxToken() *schema.Resource {
3232
Type: schema.TypeString,
3333
Sensitive: true,
3434
Optional: true,
35-
ValidateFunc: validation.StringLenBetween(40, 256),
35+
ValidateFunc: validation.StringLenBetween(12, 40),
3636
},
3737
"allowed_ips": {
3838
Type: schema.TypeList,

0 commit comments

Comments
 (0)