Skip to content

Commit 6481765

Browse files
committed
Tell a key refused for its address from a missing license
The database API answers 403 IP_NOT_ALLOWED to a key used from an address outside the key's or its organization's allowed addresses, and the CLI read every 403 as a license that does not cover the database, pointing at db list, which answers the same 403. The hint now says the key is fine and its allowed addresses leave this one out.
1 parent b1d629f commit 6481765

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎client.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ func resolveRetries() int {
7070
// The SDK's own message is accurate and says nothing about what to DO, which
7171
// for the two authentication failures is the entire question. Keys are
7272
// default-deny, so a valid key without the db.download scope is refused as
73-
// unauthorized, not as forbidden.
73+
// unauthorized, not as forbidden. A forbidden is a license, or an address the
74+
// allowed addresses leave out, and only its code tells them apart.
7475
func explain(err error) error {
7576
var apiErr *internetdata.Error
7677
if !errors.As(err, &apiErr) {
@@ -81,6 +82,10 @@ func explain(err error) error {
8182
return fmt.Errorf("%w\nthe API key was not accepted, or it lacks the db.download scope; check `%s whoami`",
8283
err, progBase)
8384
case internetdata.KindForbidden:
85+
if apiErr.Message == "IP_NOT_ALLOWED" {
86+
return fmt.Errorf("%w\nthe key is fine; its allowed addresses, or its organization's, leave this one out",
87+
err)
88+
}
8489
return fmt.Errorf("%w\nthis organization's license does not cover that; `%s db list` shows what it holds",
8590
err, progBase)
8691
case internetdata.KindRateLimited:

‎client_test.go‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
internetdata "github.com/internetdata/sdk-go/v2"
8+
)
9+
10+
// Both refusals are a 403, and each needs a different thing done next: a key
11+
// refused for its address is a valid key, which a license hint sends looking at
12+
// the wrong setting.
13+
func TestExplain(t *testing.T) {
14+
cases := []struct {
15+
kind internetdata.ErrorKind
16+
status int
17+
code string
18+
want string
19+
}{
20+
{internetdata.KindForbidden, 403, "NOT_LICENSED", "license does not cover that"},
21+
{internetdata.KindForbidden, 403, "LICENSE_EXPIRED", "license does not cover that"},
22+
{internetdata.KindForbidden, 403, "IP_NOT_ALLOWED", "its allowed addresses, or its organization's"},
23+
{internetdata.KindUnauthorized, 401, "UNAUTHORIZED", "was not accepted"},
24+
}
25+
for _, c := range cases {
26+
err := explain(&internetdata.Error{Kind: c.kind, Message: c.code, StatusCode: c.status})
27+
if got := err.Error(); !strings.Contains(got, c.want) {
28+
t.Errorf("%s: got %q, want it to contain %q", c.code, got, c.want)
29+
}
30+
if c.code == "IP_NOT_ALLOWED" && strings.Contains(err.Error(), "license") {
31+
t.Errorf("%s names the license: %q", c.code, err.Error())
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)
⌂ 换网址