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

feat: cache results of getEnv() #857

Merged
merged 3 commits into from
Dec 23, 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.1.0",
"gcp-metadata": "^3.2.0",
"gcp-metadata": "^3.3.0",
"gtoken": "^4.1.0",
"jws": "^3.1.5",
"lru-cache": "^5.0.0"
Expand Down
35 changes: 21 additions & 14 deletions src/auth/envDetect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,34 @@ export enum GCPEnv {
NONE = 'NONE',
}

let env: GCPEnv | undefined;
let envPromise: Promise<GCPEnv> | undefined;

export function clear() {
env = undefined;
envPromise = undefined;
}

export async function getEnv() {
if (!env) {
if (isAppEngine()) {
env = GCPEnv.APP_ENGINE;
} else if (isCloudFunction()) {
env = GCPEnv.CLOUD_FUNCTIONS;
} else if (await isComputeEngine()) {
if (await isKubernetesEngine()) {
env = GCPEnv.KUBERNETES_ENGINE;
} else {
env = GCPEnv.COMPUTE_ENGINE;
}
if (envPromise) {
return envPromise;
}
envPromise = getEnvMemoized();
return envPromise;
}

async function getEnvMemoized(): Promise<GCPEnv> {
let env = GCPEnv.NONE;
if (isAppEngine()) {
env = GCPEnv.APP_ENGINE;
} else if (isCloudFunction()) {
env = GCPEnv.CLOUD_FUNCTIONS;
} else if (await isComputeEngine()) {
if (await isKubernetesEngine()) {
env = GCPEnv.KUBERNETES_ENGINE;
} else {
env = GCPEnv.NONE;
env = GCPEnv.COMPUTE_ENGINE;
}
} else {
env = GCPEnv.NONE;
}
return env;
}
Expand Down
22 changes: 22 additions & 0 deletions test/test.googleauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
HEADERS,
HOST_ADDRESS,
SECONDARY_HOST_ADDRESS,
resetIsAvailableCache,
} from 'gcp-metadata';
import * as nock from 'nock';
import * as os from 'os';
Expand Down Expand Up @@ -80,6 +81,7 @@ describe('googleauth', () => {
let createLinuxWellKnownStream: Function;
let createWindowsWellKnownStream: Function;
beforeEach(() => {
resetIsAvailableCache();
auth = new GoogleAuth();
exposeWindowsWellKnownFile = false;
exposeLinuxWellKnownFile = false;
Expand Down Expand Up @@ -1294,6 +1296,26 @@ describe('googleauth', () => {
scope.done();
});

it('should cache prior call to getEnv(), when GCE', async () => {
envDetect.clear();
const {auth, scopes} = mockGCE();
auth.getEnv();
const env = await auth.getEnv();
assert.strictEqual(env, envDetect.GCPEnv.COMPUTE_ENGINE);
});

it('should cache prior call to getEnv(), when GKE', async () => {
envDetect.clear();
const {auth, scopes} = mockGCE();
const scope = nock(host)
.get(`${instancePath}/attributes/cluster-name`)
.reply(200, {}, HEADERS);
auth.getEnv();
const env = await auth.getEnv();
assert.strictEqual(env, envDetect.GCPEnv.KUBERNETES_ENGINE);
scope.done();
});

it('should get the current environment if GCF 8 and below', async () => {
envDetect.clear();
mockEnvVar('FUNCTION_NAME', 'DOGGY');
Expand Down