Skip to content

Commit

Permalink
fix: deprecate the getDefaultProjectId method (#402)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The `GoogleAuth.getDefaultProjectId` method is now deprecated.  Please use the equivalent `GoogleAuth.getProjectId` method instead.
  • Loading branch information
JustinBeckwith authored Jul 4, 2018
1 parent 6a013fe commit ff8cbe8
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 69 deletions.
15 changes: 10 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
],
"dependencies": {
"axios": "^0.18.0",
"gcp-metadata": "^0.6.3",
"gtoken": "^2.3.0",
"jws": "^3.1.5",
"lodash.isstring": "^4.0.1",
"lru-cache": "^4.1.3",
"retry-axios": "^0.3.2",
"gcp-metadata": "^0.6.3"
"semver": "^5.5.0"
},
"devDependencies": {
"@justinbeckwith/typedoc": "^0.10.1",
Expand All @@ -37,8 +38,9 @@
"@types/mv": "^2.1.0",
"@types/ncp": "^2.0.1",
"@types/nock": "^9.1.3",
"@types/node": "^10.3.0",
"@types/node": "^10.5.1",
"@types/pify": "^3.0.2",
"@types/semver": "^5.5.0",
"@types/sinon": "^5.0.1",
"@types/tmp": "^0.0.33",
"clang-format": "^1.2.3",
Expand Down
42 changes: 26 additions & 16 deletions src/auth/googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ export const CLOUD_SDK_CLIENT_ID =
export class GoogleAuth {
transporter?: Transporter;

// This shim is in place for compatibility with google-auto-auth.
getProjectId = this.getDefaultProjectId;

/**
* Caches a value indicating whether the auth layer is running on Google
* Compute Engine.
Expand Down Expand Up @@ -126,23 +123,36 @@ export class GoogleAuth {
}

/**
* Obtains the default project ID for the application.
* @param callback Optional callback
* @returns Promise that resolves with project Id (if used without callback)
* THIS METHOD HAS BEEN DEPRECATED.
* It will be removed in 3.0. Please use getProjectId instead.
*/
getDefaultProjectId(): Promise<string>;
getDefaultProjectId(callback: ProjectIdCallback): void;
getDefaultProjectId(callback?: ProjectIdCallback): Promise<string|null>|void {
messages.warn(messages.DEFAULT_PROJECT_ID_DEPRECATED);
if (callback) {
this.getDefaultProjectIdAsync()
.then(r => callback(null, r))
.catch(callback);
this.getProjectIdAsync().then(r => callback(null, r)).catch(callback);
} else {
return this.getProjectIdAsync();
}
}

/**
* Obtains the default project ID for the application.
* @param callback Optional callback
* @returns Promise that resolves with project Id (if used without callback)
*/
getProjectId(): Promise<string>;
getProjectId(callback: ProjectIdCallback): void;
getProjectId(callback?: ProjectIdCallback): Promise<string|null>|void {
if (callback) {
this.getProjectIdAsync().then(r => callback(null, r)).catch(callback);
} else {
return this.getDefaultProjectIdAsync();
return this.getProjectIdAsync();
}
}

private getDefaultProjectIdAsync(): Promise<string|null> {
private getProjectIdAsync(): Promise<string|null> {
if (this._cachedProjectId) {
return Promise.resolve(this._cachedProjectId);
}
Expand Down Expand Up @@ -205,7 +215,7 @@ export class GoogleAuth {
if (this.cachedCredential) {
return {
credential: this.cachedCredential as JWT | UserRefreshClient,
projectId: await this.getDefaultProjectIdAsync()
projectId: await this.getProjectIdAsync()
};
}

Expand All @@ -222,7 +232,7 @@ export class GoogleAuth {
credential.scopes = this.scopes;
}
this.cachedCredential = credential;
projectId = await this.getDefaultProjectId();
projectId = await this.getProjectId();
return {credential, projectId};
}

Expand All @@ -234,7 +244,7 @@ export class GoogleAuth {
credential.scopes = this.scopes;
}
this.cachedCredential = credential;
projectId = await this.getDefaultProjectId();
projectId = await this.getProjectId();
return {credential, projectId};
}

Expand All @@ -256,7 +266,7 @@ export class GoogleAuth {
// For GCE, just return a default ComputeClient. It will take care of
// the rest.
this.cachedCredential = new Compute(options);
projectId = await this.getDefaultProjectId();
projectId = await this.getProjectId();
return {projectId, credential: this.cachedCredential};
}

Expand Down Expand Up @@ -382,7 +392,7 @@ export class GoogleAuth {
*/
protected warnOnProblematicCredentials(client: JWT) {
if (client.email === CLOUD_SDK_CLIENT_ID) {
process.emitWarning(messages.PROBLEMATIC_CREDENTIALS_WARNING);
messages.warn(messages.PROBLEMATIC_CREDENTIALS_WARNING);
}
}

Expand Down
62 changes: 55 additions & 7 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,58 @@
* limitations under the License.
*/

export const PROBLEMATIC_CREDENTIALS_WARNING =
`Your application has authenticated using end user credentials from Google
Cloud SDK. We recommend that most server applications use service accounts
instead. If your application continues to use end user credentials from Cloud
SDK, you might receive a "quota exceeded" or "API not enabled" error. For
more information about service accounts, see
https://cloud.google.com/docs/authentication/.`;
import * as semver from 'semver';

export enum WarningTypes {
WARNING = 'Warning',
DEPRECATION = 'DeprecationWarning'
}

export function warn(warning: Warning) {
// Only show a given warning once
if (warning.warned) {
return;
}
warning.warned = true;
if (semver.satisfies(process.version, '>=8')) {
// @types/node doesn't recognize the emitWarning syntax which
// accepts a config object, so `as any` it is
// https://nodejs.org/docs/latest-v8.x/api/process.html#process_process_emitwarning_warning_options
// tslint:disable-next-line no-any
process.emitWarning(warning.message, warning as any);
} else {
// This path can be removed once we drop support for Node 6.
// https://nodejs.org/docs/latest-v6.x/api/process.html#process_process_emitwarning_warning_name_ctor
process.emitWarning(warning.message, warning.type);
}
}

export interface Warning {
code: string;
type: WarningTypes;
message: string;
warned?: boolean;
}

export const PROBLEMATIC_CREDENTIALS_WARNING = {
code: 'google-auth-library:00001',
type: WarningTypes.WARNING,
message: [
'Your application has authenticated using end user credentials from Google',
'Cloud SDK. We recommend that most server applications use service accounts',
'instead. If your application continues to use end user credentials from',
'Cloud SDK, you might receive a "quota exceeded" or "API not enabled" error.',
'For more information about service accounts, see',
'https://cloud.google.com/docs/authentication/.'
].join(' ')
};

export const DEFAULT_PROJECT_ID_DEPRECATED = {
code: 'google-auth-library:DEP002',
type: WarningTypes.DEPRECATION,
message: [
'The `getDefaultProjectId` method has been deprecated, and will be removed',
'in the 3.0 release of google-auth-library. Please use the `getProjectId`',
'method instead.'
].join(' ')
};
2 changes: 1 addition & 1 deletion test/fixtures/kitchen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const jwt = new JWT();
const auth = new GoogleAuth();
async function getToken() {
const token = await jwt.getToken('token');
const projectId = await auth.getDefaultProjectId();
const projectId = await auth.getProjectId();
const creds = await auth.getApplicationDefault();
return token;
}
Expand Down
Loading

0 comments on commit ff8cbe8

Please sign in to comment.