Skip to content

Commit 3d35edb

Browse files
authored
Reading project ID from GOOGLE_CLOUD_PROJECT (#158)
1 parent 10499b4 commit 3d35edb

File tree

3 files changed

+57
-36
lines changed

3 files changed

+57
-36
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Unreleased
22

3+
- [added] The Admin SDK can now read the Firebase/GCP project ID from
4+
both `GCLOUD_PROJECT` and `GOOGLE_CLOUD_PROJECT` environment
5+
variables.
6+
37
# v3.1.0
48

59
- [added] Added new functions for testing errors in the `iid` package

firebase.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,10 @@ func NewApp(ctx context.Context, config *Config, opts ...option.ClientOption) (*
151151
} else if creds.ProjectID != "" {
152152
pid = creds.ProjectID
153153
} else {
154-
pid = os.Getenv("GCLOUD_PROJECT")
154+
pid = os.Getenv("GOOGLE_CLOUD_PROJECT")
155+
if pid == "" {
156+
pid = os.Getenv("GCLOUD_PROJECT")
157+
}
155158
}
156159

157160
ao := defaultAuthOverrides

firebase_test.go

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -142,25 +142,29 @@ func TestRefreshTokenFileWithConfig(t *testing.T) {
142142
}
143143

144144
func TestRefreshTokenWithEnvVar(t *testing.T) {
145-
varName := "GCLOUD_PROJECT"
146-
current := os.Getenv(varName)
145+
verify := func(varName string) {
146+
current := os.Getenv(varName)
147147

148-
if err := os.Setenv(varName, "mock-project-id"); err != nil {
149-
t.Fatal(err)
150-
}
151-
defer os.Setenv(varName, current)
148+
if err := os.Setenv(varName, "mock-project-id"); err != nil {
149+
t.Fatal(err)
150+
}
151+
defer os.Setenv(varName, current)
152152

153-
app, err := NewApp(context.Background(), nil, option.WithCredentialsFile("testdata/refresh_token.json"))
154-
if err != nil {
155-
t.Fatal(err)
156-
}
157-
if app.projectID != "mock-project-id" {
158-
t.Errorf("Project ID: %q; want: mock-project-id", app.projectID)
153+
app, err := NewApp(context.Background(), nil, option.WithCredentialsFile("testdata/refresh_token.json"))
154+
if err != nil {
155+
t.Fatal(err)
156+
}
157+
if app.projectID != "mock-project-id" {
158+
t.Errorf("[env=%s] Project ID: %q; want: mock-project-id", varName, app.projectID)
159+
}
160+
if app.creds == nil {
161+
t.Errorf("[env=%s] Credentials: nil; want creds", varName)
162+
} else if len(app.creds.JSON) == 0 {
163+
t.Errorf("[env=%s] JSON: empty; want; non-empty", varName)
164+
}
159165
}
160-
if app.creds == nil {
161-
t.Error("Credentials: nil; want creds")
162-
} else if len(app.creds.JSON) == 0 {
163-
t.Error("JSON: empty; want; non-empty")
166+
for _, varName := range []string{"GCLOUD_PROJECT", "GOOGLE_CLOUD_PROJECT"} {
167+
verify(varName)
164168
}
165169
}
166170

@@ -295,34 +299,44 @@ func TestFirestore(t *testing.T) {
295299
}
296300

297301
func TestFirestoreWithProjectID(t *testing.T) {
298-
varName := "GCLOUD_PROJECT"
299-
current := os.Getenv(varName)
302+
verify := func(varName string) {
303+
current := os.Getenv(varName)
300304

301-
if err := os.Setenv(varName, ""); err != nil {
302-
t.Fatal(err)
303-
}
304-
defer os.Setenv(varName, current)
305+
if err := os.Setenv(varName, ""); err != nil {
306+
t.Fatal(err)
307+
}
308+
defer os.Setenv(varName, current)
305309

306-
ctx := context.Background()
307-
config := &Config{ProjectID: "project-id"}
308-
app, err := NewApp(ctx, config, option.WithCredentialsFile("testdata/refresh_token.json"))
309-
if err != nil {
310-
t.Fatal(err)
311-
}
310+
ctx := context.Background()
311+
config := &Config{ProjectID: "project-id"}
312+
app, err := NewApp(ctx, config, option.WithCredentialsFile("testdata/refresh_token.json"))
313+
if err != nil {
314+
t.Fatal(err)
315+
}
312316

313-
if c, err := app.Firestore(ctx); c == nil || err != nil {
314-
t.Errorf("Firestore() = (%v, %v); want (auth, nil)", c, err)
317+
if c, err := app.Firestore(ctx); c == nil || err != nil {
318+
t.Errorf("[env=%s] Firestore() = (%v, %v); want (auth, nil)", varName, c, err)
319+
}
320+
}
321+
for _, varName := range []string{"GCLOUD_PROJECT", "GOOGLE_CLOUD_PROJECT"} {
322+
verify(varName)
315323
}
316324
}
317325

318326
func TestFirestoreWithNoProjectID(t *testing.T) {
319-
varName := "GCLOUD_PROJECT"
320-
current := os.Getenv(varName)
327+
unsetVariable := func(varName string) string {
328+
current := os.Getenv(varName)
329+
if err := os.Setenv(varName, ""); err != nil {
330+
t.Fatal(err)
331+
}
332+
return current
333+
}
321334

322-
if err := os.Setenv(varName, ""); err != nil {
323-
t.Fatal(err)
335+
for _, varName := range []string{"GCLOUD_PROJECT", "GOOGLE_CLOUD_PROJECT"} {
336+
if current := unsetVariable(varName); current != "" {
337+
defer os.Setenv(varName, current)
338+
}
324339
}
325-
defer os.Setenv(varName, current)
326340

327341
ctx := context.Background()
328342
app, err := NewApp(ctx, nil, option.WithCredentialsFile("testdata/refresh_token.json"))

0 commit comments

Comments
 (0)