Skip to content

Commit 9d6e50c

Browse files
committed
Merged with master
2 parents 55ce947 + 1953682 commit 9d6e50c

File tree

10 files changed

+77
-12
lines changed

10 files changed

+77
-12
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Unreleased
22

3-
-
3+
- [changed] Admin SDK can now create custom tokens without being initialized
4+
with service account credentials. When service account private key it not
5+
available, the SDK uses the remote IAM service to sign JWTs in the cloud.
6+
- [changed] Admin SDK can now read the Firebase/GCP project ID from both
7+
`GCLOUD_PROJECT` and `GOOGLE_CLOUD_PROJECT` environment variables.
48

59
# v5.12.1
610

src/auth/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ export class Auth implements FirebaseServiceInterface {
384384
throw new FirebaseAuthError(
385385
AuthClientErrorCode.INVALID_CREDENTIAL,
386386
'Must initialize app with a cert credential or set your Firebase project ID as the ' +
387-
'GCLOUD_PROJECT environment variable to call auth().verifySessionCookie().',
387+
'GOOGLE_CLOUD_PROJECT environment variable to call auth().verifySessionCookie().',
388388
);
389389
}
390390
return this.sessionCookieVerifier_.verifyJWT(sessionCookie)

src/firestore/firestore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function getFirestoreOptions(app: FirebaseApp): any {
8383
code: 'no-project-id',
8484
message: 'Failed to determine project ID for Firestore. Initialize the '
8585
+ 'SDK with service account credentials or set project ID as an app option. '
86-
+ 'Alternatively set the GCLOUD_PROJECT environment variable.',
86+
+ 'Alternatively set the GOOGLE_CLOUD_PROJECT environment variable.',
8787
});
8888
}
8989
return {
@@ -96,7 +96,7 @@ export function getFirestoreOptions(app: FirebaseApp): any {
9696
} else if (app.options.credential instanceof ApplicationDefaultCredential) {
9797
// Try to use the Google application default credentials.
9898
// If an explicit project ID is not available, let Firestore client discover one from the
99-
// environment. This prevents the users from having to set GCLOUD_PROJECT in GCP runtimes.
99+
// environment. This prevents the users from having to set GOOGLE_CLOUD_PROJECT in GCP runtimes.
100100
return validator.isNonEmptyString(projectId) ? {projectId} : {};
101101
}
102102

src/instance-id/instance-id.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class InstanceId implements FirebaseServiceInterface {
6262
InstanceIdClientErrorCode.INVALID_PROJECT_ID,
6363
'Failed to determine project ID for InstanceId. Initialize the '
6464
+ 'SDK with service account credentials or set project ID as an app option. '
65-
+ 'Alternatively set the GCLOUD_PROJECT environment variable.',
65+
+ 'Alternatively set the GOOGLE_CLOUD_PROJECT environment variable.',
6666
);
6767
}
6868

src/messaging/messaging.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ export class Messaging implements FirebaseServiceInterface {
683683
MessagingClientErrorCode.INVALID_ARGUMENT,
684684
'Failed to determine project ID for Messaging. Initialize the '
685685
+ 'SDK with service account credentials or set project ID as an app option. '
686-
+ 'Alternatively set the GCLOUD_PROJECT environment variable.',
686+
+ 'Alternatively set the GOOGLE_CLOUD_PROJECT environment variable.',
687687
);
688688
}
689689

src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export function getProjectId(app: FirebaseApp): string {
7474
return cert.projectId;
7575
}
7676

77-
const projectId = process.env.GCLOUD_PROJECT;
77+
const projectId = process.env.GOOGLE_CLOUD_PROJECT || process.env.GCLOUD_PROJECT;
7878
if (validator.isNonEmptyString(projectId)) {
7979
return projectId;
8080
}

test/unit/auth/auth.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ describe('Auth', () => {
167167

168168
oldProcessEnv = process.env;
169169
// Project ID not set in the environment.
170+
delete process.env.GOOGLE_CLOUD_PROJECT;
170171
delete process.env.GCLOUD_PROJECT;
171172
});
172173

@@ -314,6 +315,16 @@ describe('Auth', () => {
314315
});
315316
});
316317

318+
it('should work with a non-cert credential when the GOOGLE_CLOUD_PROJECT environment variable is present', () => {
319+
process.env.GOOGLE_CLOUD_PROJECT = mocks.projectId;
320+
321+
const mockCredentialAuth = new Auth(mocks.mockCredentialApp());
322+
323+
return mockCredentialAuth.verifyIdToken(mockIdToken).then(() => {
324+
expect(stub).to.have.been.calledOnce.and.calledWith(mockIdToken);
325+
});
326+
});
327+
317328
it('should work with a non-cert credential when the GCLOUD_PROJECT environment variable is present', () => {
318329
process.env.GCLOUD_PROJECT = mocks.projectId;
319330

@@ -495,6 +506,16 @@ describe('Auth', () => {
495506
});
496507
});
497508

509+
it('should work with a non-cert credential when the GOOGLE_CLOUD_PROJECT environment variable is present', () => {
510+
process.env.GOOGLE_CLOUD_PROJECT = mocks.projectId;
511+
512+
const mockCredentialAuth = new Auth(mocks.mockCredentialApp());
513+
514+
return mockCredentialAuth.verifySessionCookie(mockSessionCookie).then(() => {
515+
expect(stub).to.have.been.calledOnce.and.calledWith(mockSessionCookie);
516+
});
517+
});
518+
498519
it('should work with a non-cert credential when the GCLOUD_PROJECT environment variable is present', () => {
499520
process.env.GCLOUD_PROJECT = mocks.projectId;
500521

test/unit/firestore/firestore.spec.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import path = require('path');
2020
import * as _ from 'lodash';
2121
import {expect} from 'chai';
22+
import * as utils from '../../../src/utils/index';
2223

2324
import * as mocks from '../../resources/mocks';
2425
import {FirebaseApp} from '../../../src/firebase-app';
@@ -33,6 +34,7 @@ describe('Firestore', () => {
3334
let firestore: any;
3435

3536
let appCredentials: string;
37+
let googleCloudProject: string;
3638
let gcloudProject: string;
3739

3840
const invalidCredError = 'Failed to initialize Google Cloud Firestore client with the available '
@@ -43,6 +45,7 @@ describe('Firestore', () => {
4345

4446
beforeEach(() => {
4547
appCredentials = process.env.GOOGLE_APPLICATION_CREDENTIALS;
48+
googleCloudProject = process.env.GOOGLE_CLOUD_PROJECT;
4649
gcloudProject = process.env.GCLOUD_PROJECT;
4750
delete process.env.GOOGLE_APPLICATION_CREDENTIALS;
4851

@@ -60,7 +63,16 @@ describe('Firestore', () => {
6063

6164
afterEach(() => {
6265
process.env.GOOGLE_APPLICATION_CREDENTIALS = appCredentials;
63-
process.env.GCLOUD_PROJECT = gcloudProject;
66+
if (googleCloudProject) {
67+
process.env.GOOGLE_CLOUD_PROJECT = googleCloudProject;
68+
} else {
69+
delete process.env.GOOGLE_CLOUD_PROJECT;
70+
}
71+
if (gcloudProject) {
72+
process.env.GCLOUD_PROJECT = gcloudProject;
73+
} else {
74+
delete process.env.GCLOUD_PROJECT;
75+
}
6476
return mockApp.delete();
6577
});
6678

@@ -84,14 +96,15 @@ describe('Firestore', () => {
8496

8597
it('should throw given an invalid credential with project ID', () => {
8698
// Project ID is read from the environment variable, but the credential is unsupported.
87-
process.env.GCLOUD_PROJECT = 'project_id';
99+
process.env.GOOGLE_CLOUD_PROJECT = 'project_id';
88100
expect(() => {
89101
return new FirestoreService(mockCredentialApp);
90102
}).to.throw(invalidCredError);
91103
});
92104

93105
it('should throw given an invalid credential without project ID', () => {
94106
// Project ID not set in the environment.
107+
delete process.env.GOOGLE_CLOUD_PROJECT;
95108
delete process.env.GCLOUD_PROJECT;
96109
expect(() => {
97110
return new FirestoreService(mockCredentialApp);
@@ -106,6 +119,7 @@ describe('Firestore', () => {
106119

107120
it('should not throw given application default credentials without project ID', () => {
108121
// Project ID not set in the environment.
122+
delete process.env.GOOGLE_CLOUD_PROJECT;
109123
delete process.env.GCLOUD_PROJECT;
110124
process.env.GOOGLE_APPLICATION_CREDENTIALS = mockServiceAccount;
111125
expect(() => {
@@ -151,7 +165,14 @@ describe('Firestore', () => {
151165
expect(options.projectId).to.equal('explicit-project-id');
152166
});
153167

154-
it('should return a string when project ID is present in environment', () => {
168+
it('should return a string when GOOGLE_CLOUD_PROJECT is set', () => {
169+
process.env.GOOGLE_CLOUD_PROJECT = 'env-project-id';
170+
process.env.GOOGLE_APPLICATION_CREDENTIALS = mockServiceAccount;
171+
const options = getFirestoreOptions(defaultCredentialApp);
172+
expect(options.projectId).to.equal('env-project-id');
173+
});
174+
175+
it('should return a string when GCLOUD_PROJECT is set', () => {
155176
process.env.GCLOUD_PROJECT = 'env-project-id';
156177
process.env.GOOGLE_APPLICATION_CREDENTIALS = mockServiceAccount;
157178
const options = getFirestoreOptions(defaultCredentialApp);

test/unit/instance-id/instance-id.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ describe('InstanceId', () => {
4848
let malformedAccessTokenClient: InstanceId;
4949
let rejectedPromiseAccessTokenClient: InstanceId;
5050

51+
let googleCloudProject: string;
5152
let gcloudProject: string;
5253

5354
const noProjectIdError = 'Failed to determine project ID for InstanceId. Initialize the SDK '
5455
+ 'with service account credentials or set project ID as an app option. Alternatively set the '
55-
+ 'GCLOUD_PROJECT environment variable.';
56+
+ 'GOOGLE_CLOUD_PROJECT environment variable.';
5657

5758
before(() => utils.mockFetchAccessTokenRequests());
5859

@@ -63,6 +64,7 @@ describe('InstanceId', () => {
6364
mockCredentialApp = mocks.mockCredentialApp();
6465
iid = new InstanceId(mockApp);
6566

67+
googleCloudProject = process.env.GOOGLE_CLOUD_PROJECT;
6668
gcloudProject = process.env.GCLOUD_PROJECT;
6769

6870
nullAccessTokenClient = new InstanceId(mocks.appReturningNullAccessToken());
@@ -71,6 +73,7 @@ describe('InstanceId', () => {
7173
});
7274

7375
afterEach(() => {
76+
process.env.GOOGLE_CLOUD_PROJECT = googleCloudProject;
7477
process.env.GCLOUD_PROJECT = gcloudProject;
7578
return mockApp.delete();
7679
});
@@ -96,6 +99,7 @@ describe('InstanceId', () => {
9699

97100
it('should throw given an invalid credential without project ID', () => {
98101
// Project ID not set in the environment.
102+
delete process.env.GOOGLE_CLOUD_PROJECT;
99103
delete process.env.GCLOUD_PROJECT;
100104
expect(() => {
101105
return new InstanceId(mockCredentialApp);

test/unit/utils/index.spec.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,21 @@ describe('toWebSafeBase64()', () => {
6464
});
6565

6666
describe('getProjectId()', () => {
67+
let googleCloudProject: string;
6768
let gcloudProject: string;
6869

6970
before(() => {
71+
googleCloudProject = process.env.GOOGLE_CLOUD_PROJECT;
7072
gcloudProject = process.env.GCLOUD_PROJECT;
7173
});
7274

7375
after(() => {
76+
if (isNonEmptyString(googleCloudProject)) {
77+
process.env.GOOGLE_CLOUD_PROJECT = googleCloudProject;
78+
} else {
79+
delete process.env.GOOGLE_CLOUD_PROJECT;
80+
}
81+
7482
if (isNonEmptyString(gcloudProject)) {
7583
process.env.GCLOUD_PROJECT = gcloudProject;
7684
} else {
@@ -92,13 +100,20 @@ describe('getProjectId()', () => {
92100
expect(getProjectId(app)).to.equal('project_id');
93101
});
94102

95-
it('should return the project ID set in environment', () => {
103+
it('should return the project ID set in GOOGLE_CLOUD_PROJECT environment variable', () => {
104+
process.env.GOOGLE_CLOUD_PROJECT = 'env-var-project-id';
105+
const app: FirebaseApp = mocks.mockCredentialApp();
106+
expect(getProjectId(app)).to.equal('env-var-project-id');
107+
});
108+
109+
it('should return the project ID set in GCLOUD_PROJECT environment variable', () => {
96110
process.env.GCLOUD_PROJECT = 'env-var-project-id';
97111
const app: FirebaseApp = mocks.mockCredentialApp();
98112
expect(getProjectId(app)).to.equal('env-var-project-id');
99113
});
100114

101115
it('should return null when project ID is not set', () => {
116+
delete process.env.GOOGLE_CLOUD_PROJECT;
102117
delete process.env.GCLOUD_PROJECT;
103118
const app: FirebaseApp = mocks.mockCredentialApp();
104119
expect(getProjectId(app)).to.be.null;

0 commit comments

Comments
 (0)