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: add detection for Cloud Run #1177

Merged
merged 2 commits into from
May 21, 2021
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
12 changes: 12 additions & 0 deletions src/auth/envDetect.ts
Expand Up @@ -19,6 +19,7 @@ export enum GCPEnv {
KUBERNETES_ENGINE = 'KUBERNETES_ENGINE',
CLOUD_FUNCTIONS = 'CLOUD_FUNCTIONS',
COMPUTE_ENGINE = 'COMPUTE_ENGINE',
CLOUD_RUN = 'CLOUD_RUN',
NONE = 'NONE',
}

Expand All @@ -45,6 +46,8 @@ async function getEnvMemoized(): Promise<GCPEnv> {
} else if (await isComputeEngine()) {
if (await isKubernetesEngine()) {
env = GCPEnv.KUBERNETES_ENGINE;
} else if (isCloudRun()) {
env = GCPEnv.CLOUD_RUN;
} else {
env = GCPEnv.COMPUTE_ENGINE;
}
Expand All @@ -62,6 +65,15 @@ function isCloudFunction() {
return !!(process.env.FUNCTION_NAME || process.env.FUNCTION_TARGET);
}

/**
* This check only verifies that the environment is running knative.
* This must be run *after* checking for Kubernetes, otherwise it will
* return a false positive.
*/
function isCloudRun() {
return !!process.env.K_CONFIGURATION;
}
Comment on lines +73 to +75

Choose a reason for hiding this comment

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

I guess it's too late now but we should be querying metadata endpoint first and use this env variable only in case metadata endpoint doesn't return the environment


async function isKubernetesEngine() {
try {
await gcpMetadata.instance('attributes/cluster-name');
Expand Down
8 changes: 8 additions & 0 deletions test/test.googleauth.ts
Expand Up @@ -1367,6 +1367,14 @@ describe('googleauth', () => {
assert.strictEqual(env, envDetect.GCPEnv.APP_ENGINE);
});

it('should get the current environment if Cloud Run', async () => {
envDetect.clear();
mockEnvVar('K_CONFIGURATION', 'KITTY');
const {auth} = mockGCE();
const env = await auth.getEnv();
assert.strictEqual(env, envDetect.GCPEnv.CLOUD_RUN);
});

it('should make the request', async () => {
const url = 'http://example.com';
const {auth, scopes} = mockGCE();
Expand Down