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(deps): update to gcp-metadata and address envDetect performance issues #787

Merged
merged 3 commits into from
Sep 17, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"base64-js": "^1.3.0",
"fast-text-encoding": "^1.0.0",
"gaxios": "^2.0.0",
"gcp-metadata": "^2.0.0",
"gcp-metadata": "^3.0.0",
"gtoken": "^4.0.0",
"jws": "^3.1.5",
"lru-cache": "^5.0.0"
Expand Down
8 changes: 5 additions & 3 deletions src/auth/envDetect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ export async function getEnv() {
env = GCPEnv.APP_ENGINE;
} else if (isCloudFunction()) {
env = GCPEnv.CLOUD_FUNCTIONS;
} else if (await isKubernetesEngine()) {
env = GCPEnv.KUBERNETES_ENGINE;
} else if (await isComputeEngine()) {
env = GCPEnv.COMPUTE_ENGINE;
if (await isKubernetesEngine()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we end up performing one more check for kubernetes, but we use the fast path gcpMetadata.isAvailable rather than the slow gcpMetadata.instance('attributes/cluster-name') for the initial check, correcting issues with @nodejs/logging performance.

env = GCPEnv.KUBERNETES_ENGINE;
} else {
env = GCPEnv.COMPUTE_ENGINE;
}
} else {
env = GCPEnv.NONE;
}
Expand Down
44 changes: 39 additions & 5 deletions test/test.googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ const assertRejects = require('assert-rejects');
import * as child_process from 'child_process';
import * as crypto from 'crypto';
import * as fs from 'fs';
import {BASE_PATH, HEADERS, HOST_ADDRESS} from 'gcp-metadata';
import {
BASE_PATH,
HEADERS,
HOST_ADDRESS,
SECONDARY_HOST_ADDRESS,
} from 'gcp-metadata';
import * as nock from 'nock';
import * as os from 'os';
import * as path from 'path';
Expand Down Expand Up @@ -158,21 +163,50 @@ describe('googleauth', () => {
}

function nockIsGCE() {
return nock(host)
const primary = nock(host)
.get(instancePath)
.reply(200, {}, HEADERS);
const secondary = nock(SECONDARY_HOST_ADDRESS)
.get(instancePath)
.reply(200, {}, HEADERS);

return {
done: () => {
primary.done();
secondary.done();
},
};
}

function nockNotGCE() {
return nock(host)
const primary = nock(host)
.get(instancePath)
.replyWithError({code: 'ENOTFOUND'});
const secondary = nock(SECONDARY_HOST_ADDRESS)
.get(instancePath)
.replyWithError({code: 'ENOTFOUND'});
return {
done: () => {
primary.done();
secondary.done();
},
};
}

function nock500GCE() {
return nock(host)
const primary = nock(host)
.get(instancePath)
.reply(500, {}, HEADERS);
const secondary = nock(SECONDARY_HOST_ADDRESS)
.get(instancePath)
.reply(500);
.reply(500, {}, HEADERS);

return {
done: () => {
primary.done();
secondary.done();
},
};
}

function nock404GCE() {
Expand Down