Skip to content

Commit

Permalink
feat: cache results of getEnv() (#857)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe authored Dec 23, 2019
1 parent 1147733 commit d4545a9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
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

0 comments on commit d4545a9

Please sign in to comment.