Skip to content

Commit afeaa15

Browse files
Merge dev into master
2 parents c3be6f2 + f682329 commit afeaa15

16 files changed

+66
-27
lines changed

auth/auth.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"errors"
2222
"fmt"
2323
"os"
24-
"runtime"
2524
"strings"
2625
"time"
2726

@@ -134,12 +133,11 @@ func NewClient(ctx context.Context, conf *internal.AuthConfig) (*Client, error)
134133
return nil, err
135134
}
136135

137-
goVersion := strings.TrimPrefix(runtime.Version(), "go")
138136
hc := internal.WithDefaultRetryConfig(transport)
139137
hc.CreateErrFn = handleHTTPError
140138
hc.Opts = []internal.HTTPOption{
141139
internal.WithHeader("X-Client-Version", fmt.Sprintf("Go/Admin/%s", conf.Version)),
142-
internal.WithHeader("x-goog-api-client", fmt.Sprintf("gl-go/%s fire-admin/%s", goVersion, conf.Version)),
140+
internal.WithHeader("x-goog-api-client", internal.GetMetricsHeader(conf.Version)),
143141
}
144142

145143
baseURL := defaultAuthURL

auth/auth_test.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"log"
2424
"net/http"
2525
"os"
26-
"runtime"
2726
"strings"
2827
"syscall"
2928
"testing"
@@ -1452,11 +1451,9 @@ func checkBaseClient(client *Client, wantProjectID string) error {
14521451
return fmt.Errorf("version = %q; want = %q", version, wantVersion)
14531452
}
14541453

1455-
goVersion := strings.TrimPrefix(runtime.Version(), "go")
1456-
xGoogAPIClientHeader := req.Header.Get("x-goog-api-client")
1457-
wantXGoogAPIClientHeader := fmt.Sprintf("gl-go/%s fire-admin/%s", goVersion, testVersion)
1458-
if xGoogAPIClientHeader != wantXGoogAPIClientHeader {
1459-
return fmt.Errorf("x-goog-api-client header = %q; want = %q", xGoogAPIClientHeader, wantXGoogAPIClientHeader)
1454+
xGoogAPIClientHeader := internal.GetMetricsHeader(testVersion)
1455+
if h := req.Header.Get("x-goog-api-client"); h != xGoogAPIClientHeader {
1456+
return fmt.Errorf("x-goog-api-client header = %q; want = %q", h, xGoogAPIClientHeader)
14601457
}
14611458

14621459
return nil

auth/token_generator.go

+3
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ func newIAMSigner(ctx context.Context, config *internal.AuthConfig) (*iamSigner,
175175
if err != nil {
176176
return nil, err
177177
}
178+
hc.Opts = []internal.HTTPOption{
179+
internal.WithHeader("x-goog-api-client", internal.GetMetricsHeader(config.Version)),
180+
}
178181

179182
return &iamSigner{
180183
mutex: &sync.Mutex{},

auth/token_generator_test.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func TestIAMSigner(t *testing.T) {
122122
conf := &internal.AuthConfig{
123123
Opts: optsWithTokenSource,
124124
ServiceAccountID: "test-service-account",
125+
Version: testVersion,
125126
}
126127
signer, err := newIAMSigner(ctx, conf)
127128
if err != nil {
@@ -155,6 +156,7 @@ func TestIAMSignerHTTPError(t *testing.T) {
155156
conf := &internal.AuthConfig{
156157
Opts: optsWithTokenSource,
157158
ServiceAccountID: "test-service-account",
159+
Version: testVersion,
158160
}
159161
signer, err := newIAMSigner(context.Background(), conf)
160162
if err != nil {
@@ -182,6 +184,7 @@ func TestIAMSignerUnknownHTTPError(t *testing.T) {
182184
conf := &internal.AuthConfig{
183185
Opts: optsWithTokenSource,
184186
ServiceAccountID: "test-service-account",
187+
Version: testVersion,
185188
}
186189
signer, err := newIAMSigner(context.Background(), conf)
187190
if err != nil {
@@ -208,7 +211,8 @@ func TestIAMSignerUnknownHTTPError(t *testing.T) {
208211
func TestIAMSignerWithMetadataService(t *testing.T) {
209212
ctx := context.Background()
210213
conf := &internal.AuthConfig{
211-
Opts: optsWithTokenSource,
214+
Opts: optsWithTokenSource,
215+
Version: testVersion,
212216
}
213217

214218
signer, err := newIAMSigner(ctx, conf)
@@ -253,7 +257,8 @@ func TestIAMSignerWithMetadataService(t *testing.T) {
253257
func TestIAMSignerNoMetadataService(t *testing.T) {
254258
ctx := context.Background()
255259
conf := &internal.AuthConfig{
256-
Opts: optsWithTokenSource,
260+
Opts: optsWithTokenSource,
261+
Version: testVersion,
257262
}
258263

259264
signer, err := newIAMSigner(ctx, conf)
@@ -340,6 +345,10 @@ func iamServer(t *testing.T, serviceAcct, signature string) *httptest.Server {
340345
if r.URL.Path != wantPath {
341346
t.Errorf("Path = %q; want = %q", r.URL.Path, wantPath)
342347
}
348+
xGoogAPIClientHeader := internal.GetMetricsHeader(testVersion)
349+
if h := r.Header.Get("x-goog-api-client"); h != xGoogAPIClientHeader {
350+
t.Errorf("x-goog-api-client header = %q; want = %q", h, xGoogAPIClientHeader)
351+
}
343352

344353
w.Header().Set("Content-Type", "application/json")
345354
b, err := json.Marshal(resp)

auth/user_mgt_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"net/http"
2525
"net/http/httptest"
2626
"reflect"
27-
"runtime"
2827
"sort"
2928
"strconv"
3029
"strings"
@@ -2317,7 +2316,7 @@ func echoServer(resp interface{}, t *testing.T) *mockAuthServer {
23172316
}
23182317

23192318
gh = r.Header.Get("x-goog-api-client")
2320-
wh = fmt.Sprintf("gl-go/%s fire-admin/%s", strings.TrimPrefix(runtime.Version(), "go"), testVersion)
2319+
wh = internal.GetMetricsHeader(testVersion)
23212320
if gh != wh {
23222321
t.Errorf("x-goog-api-client header = %q; want: %q", gh, wh)
23232322
}

firebase.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939
var defaultAuthOverrides = make(map[string]interface{})
4040

4141
// Version of the Firebase Go Admin SDK.
42-
const Version = "4.15.0"
42+
const Version = "4.15.1"
4343

4444
// firebaseEnvName is the name of the environment variable with the Config.
4545
const firebaseEnvName = "FIREBASE_CONFIG"
@@ -115,6 +115,7 @@ func (a *App) InstanceID(ctx context.Context) (*iid.Client, error) {
115115
conf := &internal.InstanceIDConfig{
116116
ProjectID: a.projectID,
117117
Opts: a.opts,
118+
Version: Version,
118119
}
119120
return iid.NewClient(ctx, conf)
120121
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
cloud.google.com/go/firestore v1.15.0
77
cloud.google.com/go/storage v1.40.0
88
github.com/MicahParks/keyfunc v1.9.0
9-
github.com/golang-jwt/jwt/v4 v4.5.0
9+
github.com/golang-jwt/jwt/v4 v4.5.1
1010
github.com/google/go-cmp v0.6.0
1111
golang.org/x/oauth2 v0.18.0
1212
google.golang.org/api v0.170.0

go.sum

+6-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ4
3434
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
3535
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
3636
github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
37-
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
38-
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
37+
github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo=
38+
github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
3939
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
4040
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
4141
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
@@ -65,6 +65,7 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
6565
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
6666
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
6767
github.com/google/martian/v3 v3.3.2 h1:IqNFLAmvJOgVlpdEBiQbDc2EwKW77amAycfTuWKdfvw=
68+
github.com/google/martian/v3 v3.3.2/go.mod h1:oBOf6HBosgwRXnUGWUB05QECsc6uvmMiJ3+6W4l/CUk=
6869
github.com/google/s2a-go v0.1.7 h1:60BLSyTrOV4/haCDW4zb1guZItoSq8foHCXrAnjBo/o=
6970
github.com/google/s2a-go v0.1.7/go.mod h1:50CgR4k1jNlWBu4UfS4AcfhVe1r6pdZPygJ3R8F0Qdw=
7071
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
@@ -84,6 +85,7 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
8485
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
8586
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
8687
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
88+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
8789
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
8890
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
8991
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
@@ -96,6 +98,7 @@ go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi
9698
go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI=
9799
go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco=
98100
go.opentelemetry.io/otel/sdk v1.22.0 h1:6coWHw9xw7EfClIC/+O31R8IY3/+EiRFHevmHafB2Gw=
101+
go.opentelemetry.io/otel/sdk v1.22.0/go.mod h1:iu7luyVGYovrRpe2fmj3CVKouQNdTOkxtLzPvPz1DOc=
99102
go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI=
100103
go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
101104
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -159,6 +162,7 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
159162
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
160163
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
161164
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 h1:+cNy6SZtPcJQH3LJVLOSmiC7MMxXNOb3PU/VUEz+EhU=
165+
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028/go.mod h1:NDW/Ps6MPRej6fsCIbMTohpP40sJ/P/vI1MoTEGwX90=
162166
google.golang.org/api v0.170.0 h1:zMaruDePM88zxZBG+NG8+reALO2rfLhe/JShitLyT48=
163167
google.golang.org/api v0.170.0/go.mod h1:/xql9M2btF85xac/VAm4PsLMTLVGUOpq4BE9R8jyNy8=
164168
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=

iid/iid.go

+3
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ func NewClient(ctx context.Context, c *internal.InstanceIDConfig) (*Client, erro
119119
if err != nil {
120120
return nil, err
121121
}
122+
hc.Opts = []internal.HTTPOption{
123+
internal.WithHeader("x-goog-api-client", internal.GetMetricsHeader(c.Version)),
124+
}
122125

123126
hc.CreateErrFn = createError
124127
return &Client{

iid/iid_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var testIIDConfig = &internal.InstanceIDConfig{
3131
Opts: []option.ClientOption{
3232
option.WithTokenSource(&internal.MockTokenSource{AccessToken: "test-token"}),
3333
},
34+
Version: "test-version",
3435
}
3536

3637
func TestNoProjectID(t *testing.T) {
@@ -83,6 +84,10 @@ func TestDeleteInstanceID(t *testing.T) {
8384
if h := tr.Header.Get("Authorization"); h != "Bearer test-token" {
8485
t.Errorf("Authorization = %q; want = %q", h, "Bearer test-token")
8586
}
87+
xGoogAPIClientHeader := internal.GetMetricsHeader(testIIDConfig.Version)
88+
if h := tr.Header.Get("x-goog-api-client"); h != xGoogAPIClientHeader {
89+
t.Errorf("x-goog-api-client header = %q; want = %q", h, xGoogAPIClientHeader)
90+
}
8691
}
8792

8893
func TestDeleteInstanceIDError(t *testing.T) {
@@ -156,6 +161,10 @@ func TestDeleteInstanceIDError(t *testing.T) {
156161
if h := tr.Header.Get("Authorization"); h != "Bearer test-token" {
157162
t.Errorf("Authorization = %q; want = %q", h, "Bearer test-token")
158163
}
164+
xGoogAPIClientHeader := internal.GetMetricsHeader(testIIDConfig.Version)
165+
if h := tr.Header.Get("x-goog-api-client"); h != xGoogAPIClientHeader {
166+
t.Errorf("x-goog-api-client header = %q; want = %q", h, xGoogAPIClientHeader)
167+
}
159168
tr = nil
160169
}
161170
}
@@ -201,6 +210,10 @@ func TestDeleteInstanceIDUnexpectedError(t *testing.T) {
201210
if h := tr.Header.Get("Authorization"); h != "Bearer test-token" {
202211
t.Errorf("Authorization = %q; want = %q", h, "Bearer test-token")
203212
}
213+
xGoogAPIClientHeader := internal.GetMetricsHeader(testIIDConfig.Version)
214+
if h := tr.Header.Get("x-goog-api-client"); h != xGoogAPIClientHeader {
215+
t.Errorf("x-goog-api-client header = %q; want = %q", h, xGoogAPIClientHeader)
216+
}
204217
}
205218

206219
func TestDeleteInstanceIDConnectionError(t *testing.T) {

internal/http_client.go

+8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import (
2323
"io/ioutil"
2424
"math"
2525
"net/http"
26+
"runtime"
2627
"strconv"
28+
"strings"
2729
"time"
2830

2931
"google.golang.org/api/option"
@@ -441,3 +443,9 @@ func retryNetworkAndHTTPErrors(statusCodes ...int) RetryCondition {
441443
return false
442444
}
443445
}
446+
447+
// GetMetricsHeader constructs header value for metrics attribution
448+
func GetMetricsHeader(sdkVersion string) string {
449+
goVersion := strings.TrimPrefix(runtime.Version(), "go")
450+
return fmt.Sprintf("gl-go/%s fire-admin/%s", goVersion, sdkVersion)
451+
}

internal/internal.go

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type HashConfig map[string]interface{}
5050
type InstanceIDConfig struct {
5151
Opts []option.ClientOption
5252
ProjectID string
53+
Version string
5354
}
5455

5556
// DatabaseConfig represents the configuration of Firebase Database service.

messaging/messaging.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"fmt"
2424
"net/http"
2525
"regexp"
26-
"runtime"
2726
"strconv"
2827
"strings"
2928
"time"
@@ -878,7 +877,7 @@ func NewClient(ctx context.Context, c *internal.MessagingConfig) (*Client, error
878877

879878
return &Client{
880879
fcmClient: newFCMClient(hc, c, messagingEndpoint, batchEndpoint),
881-
iidClient: newIIDClient(hc),
880+
iidClient: newIIDClient(hc, c),
882881
}, nil
883882
}
884883

@@ -894,12 +893,11 @@ func newFCMClient(hc *http.Client, conf *internal.MessagingConfig, messagingEndp
894893
client := internal.WithDefaultRetryConfig(hc)
895894
client.CreateErrFn = handleFCMError
896895

897-
goVersion := strings.TrimPrefix(runtime.Version(), "go")
898896
version := fmt.Sprintf("fire-admin-go/%s", conf.Version)
899897
client.Opts = []internal.HTTPOption{
900898
internal.WithHeader(apiFormatVersionHeader, apiFormatVersion),
901899
internal.WithHeader(firebaseClientHeader, version),
902-
internal.WithHeader("x-goog-api-client", fmt.Sprintf("gl-go/%s fire-admin/%s", goVersion, conf.Version)),
900+
internal.WithHeader("x-goog-api-client", internal.GetMetricsHeader(conf.Version)),
903901
}
904902

905903
return &fcmClient{

messaging/messaging_test.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import (
2121
"net/http"
2222
"net/http/httptest"
2323
"reflect"
24-
"runtime"
25-
"strings"
2624
"testing"
2725
"time"
2826

@@ -1396,8 +1394,7 @@ func checkFCMRequest(t *testing.T, b []byte, tr *http.Request, want map[string]i
13961394
if h := tr.Header.Get("X-FIREBASE-CLIENT"); h != clientVersion {
13971395
t.Errorf("X-FIREBASE-CLIENT = %q; want = %q", h, clientVersion)
13981396
}
1399-
goVersion := strings.TrimPrefix(runtime.Version(), "go")
1400-
xGoogAPIClientHeader := "gl-go/" + goVersion + " fire-admin/" + testMessagingConfig.Version
1397+
xGoogAPIClientHeader := internal.GetMetricsHeader(testMessagingConfig.Version)
14011398
if h := tr.Header.Get("x-goog-api-client"); h != xGoogAPIClientHeader {
14021399
t.Errorf("x-goog-api-client header = %q; want = %q", h, xGoogAPIClientHeader)
14031400
}

messaging/topic_mgt.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,13 @@ type iidClient struct {
6363
httpClient *internal.HTTPClient
6464
}
6565

66-
func newIIDClient(hc *http.Client) *iidClient {
66+
func newIIDClient(hc *http.Client, conf *internal.MessagingConfig) *iidClient {
6767
client := internal.WithDefaultRetryConfig(hc)
6868
client.CreateErrFn = handleIIDError
69-
client.Opts = []internal.HTTPOption{internal.WithHeader("access_token_auth", "true")}
69+
client.Opts = []internal.HTTPOption{
70+
internal.WithHeader("access_token_auth", "true"),
71+
internal.WithHeader("x-goog-api-client", internal.GetMetricsHeader(conf.Version)),
72+
}
7073
return &iidClient{
7174
iidEndpoint: iidEndpoint,
7275
httpClient: client,

messaging/topic_mgt_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"testing"
2626

2727
"firebase.google.com/go/v4/errorutils"
28+
"firebase.google.com/go/v4/internal"
2829
)
2930

3031
func TestSubscribe(t *testing.T) {
@@ -198,6 +199,10 @@ func checkIIDRequest(t *testing.T, b []byte, tr *http.Request, op string) {
198199
if h := tr.Header.Get("Authorization"); h != "Bearer test-token" {
199200
t.Errorf("Authorization = %q; want = %q", h, "Bearer test-token")
200201
}
202+
xGoogAPIClientHeader := internal.GetMetricsHeader(testMessagingConfig.Version)
203+
if h := tr.Header.Get("x-goog-api-client"); h != xGoogAPIClientHeader {
204+
t.Errorf("x-goog-api-client header = %q; want = %q", h, xGoogAPIClientHeader)
205+
}
201206
}
202207

203208
func checkTopicMgtResponse(t *testing.T, resp *TopicManagementResponse) {

0 commit comments

Comments
 (0)