Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: checkIsGCE: report unexpected errors #164

Merged
merged 1 commit into from
Sep 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions ts/lib/auth/googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,15 @@ export class GoogleAuth {
}

// Determine if we're running on GCE.
this._checkIsGCE((gce) => {
this._checkIsGCE((err, gce) => {
if (gce) {
// For GCE, just return a default ComputeClient. It will take care of
// the rest.
my_callback(null, new this.ComputeClient());
} else if (err) {
my_callback(new Error(
'Unexpected error while acquiring application default ' +
'credentials: ' + err.message));
} else {
// We failed to find the default credentials. Bail out with an error.
my_callback(new Error(
Expand All @@ -230,20 +234,30 @@ export class GoogleAuth {
* @param {function=} callback The callback.
* @api private
*/
public _checkIsGCE(callback: (isGCE: boolean) => void) {
public _checkIsGCE(callback: (err: Error, isGCE?: boolean) => void) {
if (this._isGCE !== undefined) {
callback(this._isGCE);
setImmediate(() => {
callback(null, this._isGCE);
});
} else {
if (!this.transporter) {
this.transporter = new DefaultTransporter();
}
this.transporter.request(
{method: 'GET', uri: 'http://metadata.google.internal', json: true},
(err, body, res) => {
if (!err && res && res.headers) {
this._isGCE = res.headers['metadata-flavor'] === 'Google';
(err: any, body, res) => {
if (err) {
if (err.code !== 'ENOTFOUND') {
// Unexpected error occurred. TODO(ofrobots): retry if this was
// a transient error.
return callback(err);
}
this._isGCE = false;
return callback(null, this._isGCE);
}
callback(this._isGCE);
this._isGCE = res && res.headers &&
res.headers['metadata-flavor'] === 'Google';
callback(null, this._isGCE);
});
}
}
Expand Down
37 changes: 29 additions & 8 deletions ts/test/test.googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ function returns(value: any) {
};
}

function callsBack(value: any) {
function callsBack(err: any, value: any) {
return (callback: Function) => {
callback(value);
callback(err, value);
};
}

Expand Down Expand Up @@ -966,7 +966,7 @@ describe('GoogleAuth', () => {
creds, 'GCLOUD_PROJECT', projectId);

creds._fileExists = returns(false);
creds._checkIsGCE = callsBack(false);
creds._checkIsGCE = callsBack(null, false);
};

// Set up a new GoogleAuth and prepare it for local environment
Expand Down Expand Up @@ -1126,7 +1126,7 @@ describe('GoogleAuth', () => {
'./ts/test/fixtures/private.json');

creds._fileExists = returns(false);
creds._checkIsGCE = callsBack(false);
creds._checkIsGCE = callsBack(null, false);
};

// Set up a new GoogleAuth and prepare it for local environment
Expand Down Expand Up @@ -1205,7 +1205,7 @@ describe('GoogleAuth', () => {
auth._pathJoin = pathJoin;
auth._osPlatform = returns('win32');
auth._fileExists = returns(true);
auth._checkIsGCE = callsBack(true);
auth._checkIsGCE = callsBack(null, true);
insertWellKnownFilePathIntoAuth(
auth, 'foo:gcloud:application_default_credentials.json',
'./ts/test/fixtures/private2.json');
Expand Down Expand Up @@ -1239,7 +1239,7 @@ describe('GoogleAuth', () => {
auth._pathJoin = pathJoin;
auth._osPlatform = returns('win32');
auth._fileExists = returns(true);
auth._checkIsGCE = callsBack(true);
auth._checkIsGCE = callsBack(null, true);
insertWellKnownFilePathIntoAuth(
auth, 'foo:gcloud:application_default_credentials.json',
'./ts/test/fixtures/private2.json');
Expand Down Expand Up @@ -1268,7 +1268,7 @@ describe('GoogleAuth', () => {
auth._pathJoin = pathJoin;
auth._osPlatform = returns('win32');
auth._fileExists = returns(false);
auth._checkIsGCE = callsBack(true);
auth._checkIsGCE = callsBack(null, true);

// Execute.
auth.getApplicationDefault((err, result) => {
Expand All @@ -1282,6 +1282,27 @@ describe('GoogleAuth', () => {
});
});

it('should report GCE error when checking for GCE fails', (done) => {
// Set up the creds.
// * Environment variable is not set.
// * Well-known file is not set.
// * Running on GCE is set to true.
const auth = new GoogleAuth();
blockGoogleApplicationCredentialEnvironmentVariable(auth);
insertEnvironmentVariableIntoAuth(auth, 'APPDATA', 'foo');
auth._pathJoin = pathJoin;
auth._osPlatform = returns('win32');
auth._fileExists = returns(false);
auth._checkIsGCE = callsBack(new Error('fake error'), undefined);

// Execute.
auth.getApplicationDefault((err, result) => {
assert(err instanceof Error);
assert.equal(result, undefined);
done();
});
});

it('should also get project ID', (done) => {
// We expect private.json to be the file that is used.
const fileContents =
Expand All @@ -1302,7 +1323,7 @@ describe('GoogleAuth', () => {
auth._pathJoin = pathJoin;
auth._osPlatform = returns('win32');
auth._fileExists = returns(true);
auth._checkIsGCE = callsBack(true);
auth._checkIsGCE = callsBack(null, true);
insertWellKnownFilePathIntoAuth(
auth, 'foo:gcloud:application_default_credentials.json',
'./ts/test/fixtures/private2.json');
Expand Down