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: Don't open browser auth if inside container #1645

Merged
merged 1 commit into from
Feb 19, 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
40 changes: 28 additions & 12 deletions src/cli/commands/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Spinner } from 'cli-spinner';
import * as snyk from '../../../lib';
import { verifyAPI } from './is-authed';
import { isCI } from '../../../lib/is-ci';
import { isDocker } from '../../../lib/is-docker';
import { args as argsLib } from '../../args';
import * as config from '../../../lib/config';
import request = require('../../../lib/request');
Expand All @@ -25,7 +26,7 @@ const debug = Debug('snyk-auth');
let attemptsLeft = 0;

function resetAttempts() {
attemptsLeft = 30;
attemptsLeft = isDocker() ? 60 : 30;
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

}

type AuthCliCommands = 'wizard' | 'ignore';
Expand All @@ -50,16 +51,9 @@ async function webAuth(via: AuthCliCommands) {
urlStr += '&redirectUri=' + Buffer.from(redirects[via]).toString('base64');
}

const msg =
'\nNow redirecting you to our auth page, go ahead and log in,\n' +
"and once the auth is complete, return to this prompt and you'll\n" +
"be ready to start using snyk.\n\nIf you can't wait use this url:\n" +
urlStr +
'\n';

// suppress this message in CI
if (!isCI()) {
console.log(msg);
console.log(browserAuthPrompt(isDocker(), urlStr));
} else {
return Promise.reject(MisconfiguredAuthInCI());
}
Expand All @@ -70,9 +64,11 @@ async function webAuth(via: AuthCliCommands) {

try {
spinner.start();
await setTimeout(() => {
open(urlStr);
}, 0);
if (!isDocker()) {
await setTimeout(() => {
open(urlStr);
}, 0);
}

return await testAuthComplete(token, ipFamily);
} finally {
Expand Down Expand Up @@ -190,3 +186,23 @@ async function getIpFamily(): Promise<6 | undefined> {
return undefined;
}
}

function browserAuthPrompt(isDocker: boolean, urlStr: string): string {
if (isDocker) {
return (
'\nTo authenticate your account, open the below URL in your browser.\n' +
'After your authentication is complete, return to this prompt to ' +
'start using Snyk.\n\n' +
urlStr +
'\n'
);
} else {
return (
'\nNow redirecting you to our auth page, go ahead and log in,\n' +
"and once the auth is complete, return to this prompt and you'll\n" +
"be ready to start using snyk.\n\nIf you can't wait use this url:\n" +
urlStr +
'\n'
);
}
}
2 changes: 1 addition & 1 deletion test/smoke/spec/snyk_auth_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Describe "Snyk CLI Authorization"

# Using timeout to not wait for browser confirmation
When run timeout 5 snyk auth
The output should include "Now redirecting you to our auth page, go ahead and log in,"
The output should include "To authenticate your account, open the below URL in your browser."
The result of function verify_login_url should include "snyk.io/login?token=" # URL found
The status should be failure
# TODO: unusable with our current docker issues
Expand Down
7 changes: 7 additions & 0 deletions test/system/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as util from 'util';
import * as _ from 'lodash';
import { test } from 'tap';
import * as ciChecker from '../../src/lib/is-ci';
import * as dockerChecker from '../../src/lib/is-docker';
import { makeTmpDirectory, silenceLog } from '../utils';
import * as sinon from 'sinon';
import * as proxyquire from 'proxyquire';
Expand Down Expand Up @@ -101,6 +102,8 @@ test('auth with no args', async (t) => {
const auth = proxyquire('../../src/cli/commands/auth', { open });
// stub CI check (ensure returns false for system test)
const ciStub = sinon.stub(ciChecker, 'isCI').returns(false);
// ensure this works on circleCI as when running outside of a container
const dockerStub = sinon.stub(dockerChecker, 'isDocker').returns(false);
// disable console.log
const enableLog = silenceLog();
try {
Expand All @@ -117,6 +120,7 @@ test('auth with no args', async (t) => {
'opens login with token param',
);
ciStub.restore();
dockerStub.restore();
} catch (e) {
t.threw(e);
}
Expand All @@ -130,6 +134,8 @@ test('auth with UTMs in environment variables', async (t) => {
const auth = proxyquire('../../src/cli/commands/auth', { open });
// stub CI check (ensure returns false for system test)
const ciStub = sinon.stub(ciChecker, 'isCI').returns(false);
// ensure this works on circleCI as when running outside of a container
const dockerStub = sinon.stub(dockerChecker, 'isDocker').returns(false);

// read data from console.log
let stdoutMessages = '';
Expand Down Expand Up @@ -161,6 +167,7 @@ test('auth with UTMs in environment variables', async (t) => {
delete process.env.SNYK_UTM_CAMPAIGN;
// clean up stubs
ciStub.restore();
dockerStub.restore();

// restore original console.log
console.log = origConsoleLog;
Expand Down