From 4489f91e59cee65606d1e075e632e9255eb56f79 Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Wed, 23 Oct 2024 21:35:45 +0000 Subject: [PATCH 1/3] chore: Add X-Goog-Api-Client header to Auth and FCM requests --- auth/auth.go | 2 ++ auth/auth_test.go | 7 +++++++ auth/user_mgt_test.go | 7 +++++++ messaging/messaging.go | 2 ++ messaging/messaging_test.go | 5 +++++ 5 files changed, 23 insertions(+) diff --git a/auth/auth.go b/auth/auth.go index d6299611..8884bf68 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "os" + "runtime" "strings" "time" @@ -137,6 +138,7 @@ func NewClient(ctx context.Context, conf *internal.AuthConfig) (*Client, error) hc.CreateErrFn = handleHTTPError hc.Opts = []internal.HTTPOption{ internal.WithHeader("X-Client-Version", fmt.Sprintf("Go/Admin/%s", conf.Version)), + internal.WithHeader("x-goog-api-client", fmt.Sprintf("gl-go/%s fire-admin/%s", runtime.Version(), conf.Version)), } baseURL := defaultAuthURL diff --git a/auth/auth_test.go b/auth/auth_test.go index 30fbde88..c0c8f91f 100644 --- a/auth/auth_test.go +++ b/auth/auth_test.go @@ -23,6 +23,7 @@ import ( "log" "net/http" "os" + "runtime" "strings" "syscall" "testing" @@ -1451,6 +1452,12 @@ func checkBaseClient(client *Client, wantProjectID string) error { return fmt.Errorf("version = %q; want = %q", version, wantVersion) } + xGoogApiClientHeader := req.Header.Get("x-goog-api-client") + wantXGoogApiClientHeader := fmt.Sprintf("gl-go/%s fire-admin/%s", runtime.Version(), testVersion) + if xGoogApiClientHeader != wantXGoogApiClientHeader { + return fmt.Errorf("x-goog-api-client header = %q; want = %q", xGoogApiClientHeader, wantXGoogApiClientHeader) + } + return nil } diff --git a/auth/user_mgt_test.go b/auth/user_mgt_test.go index ffa48655..55cfd3a4 100644 --- a/auth/user_mgt_test.go +++ b/auth/user_mgt_test.go @@ -24,6 +24,7 @@ import ( "net/http" "net/http/httptest" "reflect" + "runtime" "sort" "strconv" "strings" @@ -2315,6 +2316,12 @@ func echoServer(resp interface{}, t *testing.T) *mockAuthServer { t.Errorf("X-Client-Version header = %q; want: %q", gh, wh) } + gh = r.Header.Get("x-goog-api-client") + wh = fmt.Sprintf("gl-go/%s fire-admin/%s", runtime.Version(), testVersion) + if gh != wh { + t.Errorf("x-goog-api-client header = %q; want: %q", gh, wh) + } + for k, v := range s.Header { w.Header().Set(k, v) } diff --git a/messaging/messaging.go b/messaging/messaging.go index 2a846166..12271a9a 100644 --- a/messaging/messaging.go +++ b/messaging/messaging.go @@ -23,6 +23,7 @@ import ( "fmt" "net/http" "regexp" + "runtime" "strconv" "strings" "time" @@ -897,6 +898,7 @@ func newFCMClient(hc *http.Client, conf *internal.MessagingConfig, messagingEndp client.Opts = []internal.HTTPOption{ internal.WithHeader(apiFormatVersionHeader, apiFormatVersion), internal.WithHeader(firebaseClientHeader, version), + internal.WithHeader("x-goog-api-client", fmt.Sprintf("gl-go/%s fire-admin/%s", runtime.Version(), conf.Version)), } return &fcmClient{ diff --git a/messaging/messaging_test.go b/messaging/messaging_test.go index 7421fb7e..a782da8c 100644 --- a/messaging/messaging_test.go +++ b/messaging/messaging_test.go @@ -21,6 +21,7 @@ import ( "net/http" "net/http/httptest" "reflect" + "runtime" "testing" "time" @@ -1394,6 +1395,10 @@ func checkFCMRequest(t *testing.T, b []byte, tr *http.Request, want map[string]i if h := tr.Header.Get("X-FIREBASE-CLIENT"); h != clientVersion { t.Errorf("X-FIREBASE-CLIENT = %q; want = %q", h, clientVersion) } + xGoogApiClientHeader := "gl-go/" + runtime.Version() + " fire-admin/" + testMessagingConfig.Version + if h := tr.Header.Get("x-goog-api-client"); h != xGoogApiClientHeader { + t.Errorf("x-goog-api-client header = %q; want = %q", h, xGoogApiClientHeader) + } } var httpErrors = []struct { From 00675a1c24b9134b299443c6d5d3d353b0e1e147 Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Wed, 23 Oct 2024 21:40:16 +0000 Subject: [PATCH 2/3] fix lint errors --- auth/auth_test.go | 8 ++++---- messaging/messaging_test.go | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/auth/auth_test.go b/auth/auth_test.go index c0c8f91f..0223f8b2 100644 --- a/auth/auth_test.go +++ b/auth/auth_test.go @@ -1452,10 +1452,10 @@ func checkBaseClient(client *Client, wantProjectID string) error { return fmt.Errorf("version = %q; want = %q", version, wantVersion) } - xGoogApiClientHeader := req.Header.Get("x-goog-api-client") - wantXGoogApiClientHeader := fmt.Sprintf("gl-go/%s fire-admin/%s", runtime.Version(), testVersion) - if xGoogApiClientHeader != wantXGoogApiClientHeader { - return fmt.Errorf("x-goog-api-client header = %q; want = %q", xGoogApiClientHeader, wantXGoogApiClientHeader) + xGoogAPIClientHeader := req.Header.Get("x-goog-api-client") + wantXGoogAPIClientHeader := fmt.Sprintf("gl-go/%s fire-admin/%s", runtime.Version(), testVersion) + if xGoogAPIClientHeader != wantXGoogAPIClientHeader { + return fmt.Errorf("x-goog-api-client header = %q; want = %q", xGoogAPIClientHeader, wantXGoogAPIClientHeader) } return nil diff --git a/messaging/messaging_test.go b/messaging/messaging_test.go index a782da8c..a57e5c7e 100644 --- a/messaging/messaging_test.go +++ b/messaging/messaging_test.go @@ -1395,9 +1395,9 @@ func checkFCMRequest(t *testing.T, b []byte, tr *http.Request, want map[string]i if h := tr.Header.Get("X-FIREBASE-CLIENT"); h != clientVersion { t.Errorf("X-FIREBASE-CLIENT = %q; want = %q", h, clientVersion) } - xGoogApiClientHeader := "gl-go/" + runtime.Version() + " fire-admin/" + testMessagingConfig.Version - if h := tr.Header.Get("x-goog-api-client"); h != xGoogApiClientHeader { - t.Errorf("x-goog-api-client header = %q; want = %q", h, xGoogApiClientHeader) + xGoogAPIClientHeader := "gl-go/" + runtime.Version() + " fire-admin/" + testMessagingConfig.Version + if h := tr.Header.Get("x-goog-api-client"); h != xGoogAPIClientHeader { + t.Errorf("x-goog-api-client header = %q; want = %q", h, xGoogAPIClientHeader) } } From 287db1f21fb793520ec33555aad1217026c18739 Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Thu, 24 Oct 2024 14:42:52 +0000 Subject: [PATCH 3/3] remove the prefix from version tag --- auth/auth.go | 3 ++- auth/auth_test.go | 3 ++- auth/user_mgt_test.go | 2 +- messaging/messaging.go | 3 ++- messaging/messaging_test.go | 4 +++- 5 files changed, 10 insertions(+), 5 deletions(-) diff --git a/auth/auth.go b/auth/auth.go index 8884bf68..0bda04a0 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -134,11 +134,12 @@ func NewClient(ctx context.Context, conf *internal.AuthConfig) (*Client, error) return nil, err } + goVersion := strings.TrimPrefix(runtime.Version(), "go") hc := internal.WithDefaultRetryConfig(transport) hc.CreateErrFn = handleHTTPError hc.Opts = []internal.HTTPOption{ internal.WithHeader("X-Client-Version", fmt.Sprintf("Go/Admin/%s", conf.Version)), - internal.WithHeader("x-goog-api-client", fmt.Sprintf("gl-go/%s fire-admin/%s", runtime.Version(), conf.Version)), + internal.WithHeader("x-goog-api-client", fmt.Sprintf("gl-go/%s fire-admin/%s", goVersion, conf.Version)), } baseURL := defaultAuthURL diff --git a/auth/auth_test.go b/auth/auth_test.go index 0223f8b2..2a00220b 100644 --- a/auth/auth_test.go +++ b/auth/auth_test.go @@ -1452,8 +1452,9 @@ func checkBaseClient(client *Client, wantProjectID string) error { return fmt.Errorf("version = %q; want = %q", version, wantVersion) } + goVersion := strings.TrimPrefix(runtime.Version(), "go") xGoogAPIClientHeader := req.Header.Get("x-goog-api-client") - wantXGoogAPIClientHeader := fmt.Sprintf("gl-go/%s fire-admin/%s", runtime.Version(), testVersion) + wantXGoogAPIClientHeader := fmt.Sprintf("gl-go/%s fire-admin/%s", goVersion, testVersion) if xGoogAPIClientHeader != wantXGoogAPIClientHeader { return fmt.Errorf("x-goog-api-client header = %q; want = %q", xGoogAPIClientHeader, wantXGoogAPIClientHeader) } diff --git a/auth/user_mgt_test.go b/auth/user_mgt_test.go index 55cfd3a4..902ed280 100644 --- a/auth/user_mgt_test.go +++ b/auth/user_mgt_test.go @@ -2317,7 +2317,7 @@ func echoServer(resp interface{}, t *testing.T) *mockAuthServer { } gh = r.Header.Get("x-goog-api-client") - wh = fmt.Sprintf("gl-go/%s fire-admin/%s", runtime.Version(), testVersion) + wh = fmt.Sprintf("gl-go/%s fire-admin/%s", strings.TrimPrefix(runtime.Version(), "go"), testVersion) if gh != wh { t.Errorf("x-goog-api-client header = %q; want: %q", gh, wh) } diff --git a/messaging/messaging.go b/messaging/messaging.go index 12271a9a..525cccb6 100644 --- a/messaging/messaging.go +++ b/messaging/messaging.go @@ -894,11 +894,12 @@ func newFCMClient(hc *http.Client, conf *internal.MessagingConfig, messagingEndp client := internal.WithDefaultRetryConfig(hc) client.CreateErrFn = handleFCMError + goVersion := strings.TrimPrefix(runtime.Version(), "go") version := fmt.Sprintf("fire-admin-go/%s", conf.Version) client.Opts = []internal.HTTPOption{ internal.WithHeader(apiFormatVersionHeader, apiFormatVersion), internal.WithHeader(firebaseClientHeader, version), - internal.WithHeader("x-goog-api-client", fmt.Sprintf("gl-go/%s fire-admin/%s", runtime.Version(), conf.Version)), + internal.WithHeader("x-goog-api-client", fmt.Sprintf("gl-go/%s fire-admin/%s", goVersion, conf.Version)), } return &fcmClient{ diff --git a/messaging/messaging_test.go b/messaging/messaging_test.go index a57e5c7e..8b31fc46 100644 --- a/messaging/messaging_test.go +++ b/messaging/messaging_test.go @@ -22,6 +22,7 @@ import ( "net/http/httptest" "reflect" "runtime" + "strings" "testing" "time" @@ -1395,7 +1396,8 @@ func checkFCMRequest(t *testing.T, b []byte, tr *http.Request, want map[string]i if h := tr.Header.Get("X-FIREBASE-CLIENT"); h != clientVersion { t.Errorf("X-FIREBASE-CLIENT = %q; want = %q", h, clientVersion) } - xGoogAPIClientHeader := "gl-go/" + runtime.Version() + " fire-admin/" + testMessagingConfig.Version + goVersion := strings.TrimPrefix(runtime.Version(), "go") + xGoogAPIClientHeader := "gl-go/" + goVersion + " fire-admin/" + testMessagingConfig.Version if h := tr.Header.Get("x-goog-api-client"); h != xGoogAPIClientHeader { t.Errorf("x-goog-api-client header = %q; want = %q", h, xGoogAPIClientHeader) }