Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions go/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/spf13/cobra"
"github.com/vngcloud/greennode-cli/cmd/configure"
"github.com/vngcloud/greennode-cli/internal/cli"
"github.com/vngcloud/greennode-cli/internal/client"
"github.com/vngcloud/greennode-cli/internal/config"
)

Expand Down Expand Up @@ -74,6 +75,10 @@ func init() {

rootCmd.SetVersionTemplate("grn-cli/{{.Version}}\n")

// Tag every VKS API request with a version-stamped User-Agent so the backend
// can attribute traffic to the grn VKS CLI. Shares cliVersion with --version.
client.UserAgent = "grn-vks-cli/" + cliVersion

rootCmd.AddCommand(configure.ConfigureCmd)
for _, svc := range cli.Services() {
rootCmd.AddCommand(svc)
Expand Down
8 changes: 8 additions & 0 deletions go/internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ var retryableStatusCodes = map[int]bool{
500: true, 502: true, 503: true, 504: true,
}

// UserAgent is the User-Agent header sent on every VKS API request so the
// backend can attribute traffic to the grn VKS CLI. cmd overrides it at startup
// with the release version (e.g. "grn-vks-cli/1.7.3"); the version-less default
// keeps identification intact if that override is ever skipped (e.g. in tests).
var UserAgent = "grn-vks-cli"

// GreennodeClient is an HTTP client for Greennode APIs with retry and auto token refresh.
type GreennodeClient struct {
baseURL string
Expand Down Expand Up @@ -205,6 +211,7 @@ func (c *GreennodeClient) requestRaw(method, path string, params map[string]stri

req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", UserAgent)

if c.debug {
fmt.Fprintf(os.Stderr, "[debug] %s %s\n", method, fullURL)
Expand Down Expand Up @@ -245,6 +252,7 @@ func (c *GreennodeClient) requestRaw(method, path string, params map[string]stri
req2, _ := http.NewRequest(method, fullURL, retryBody)
req2.Header.Set("Authorization", "Bearer "+token)
req2.Header.Set("Content-Type", "application/json")
req2.Header.Set("User-Agent", UserAgent)
resp2, err := c.httpClient.Do(req2)
if err != nil {
return "", err
Expand Down
36 changes: 36 additions & 0 deletions go/internal/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,42 @@ func TestPatchSendsPatchMethodAndBody(t *testing.T) {
}
}

func TestRequestSetsUserAgentHeader(t *testing.T) {
// Every outbound VKS API request must carry a User-Agent so the backend can
// attribute traffic to the grn VKS CLI.
var gotUA string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotUA = r.Header.Get("User-Agent")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"ok":true}`))
}))
defer srv.Close()

// Pin a known value; the production default is set from cmd at startup.
prev := UserAgent
UserAgent = "grn-vks-cli/9.9.9"
defer func() { UserAgent = prev }()

tm := auth.NewTokenManager("id", "secret")
tm.SetToken("test-token", time.Now().Add(1*time.Hour))

c := NewGreennodeClient(srv.URL, tm, 5*time.Second, 5*time.Second, false, false)
if _, err := c.Get("/v1/thing", nil); err != nil {
t.Fatalf("Get returned error: %v", err)
}
if gotUA != "grn-vks-cli/9.9.9" {
t.Errorf("User-Agent = %q, want %q", gotUA, "grn-vks-cli/9.9.9")
}
}

func TestUserAgentDefaultsToVKSCLI(t *testing.T) {
// The package default identifies the VKS CLI even if cmd never overrides it.
if UserAgent != "grn-vks-cli" {
t.Errorf("default UserAgent = %q, want %q", UserAgent, "grn-vks-cli")
}
}

func TestFormatErrorSurfacesNestedErrorObject(t *testing.T) {
// VKS returns errors as {"error": {"message": ...}} — a nested object, not a
// string. The detail must still reach the user instead of being dropped.
Expand Down
Loading