From 4c795e008e17386ac04466a45a9785e81258853b Mon Sep 17 00:00:00 2001 From: PeterSchafer <101886095+PeterSchafer@users.noreply.github.com> Date: Fri, 17 May 2024 11:12:29 +0200 Subject: [PATCH] fix(auth): Autodetect IDE usage and fallback to API token based authentication (#5241) * fix(auth): Autodetect IDE usage and fallback to API token based authentication * chore: update final gaf commit * test: add closed box test --- cliv2/go.mod | 2 +- cliv2/go.sum | 4 ++-- test/jest/acceptance/auth.spec.ts | 23 ++++++++++++++++++----- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/cliv2/go.mod b/cliv2/go.mod index f7b29ec738..62118b844e 100644 --- a/cliv2/go.mod +++ b/cliv2/go.mod @@ -14,7 +14,7 @@ require ( github.com/snyk/cli-extension-iac-rules v0.0.0-20240422133948-ae17a4306672 github.com/snyk/cli-extension-sbom v0.0.0-20240426130148-b83c2ebc75c4 github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1 - github.com/snyk/go-application-framework v0.0.0-20240515114301-86824c982460 + github.com/snyk/go-application-framework v0.0.0-20240517080240-d22ced1f98d1 github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65 github.com/snyk/snyk-iac-capture v0.6.5 github.com/snyk/snyk-ls v0.0.0-20240510163626-041f744c3180 diff --git a/cliv2/go.sum b/cliv2/go.sum index f1b4665838..6c6cf2e93d 100644 --- a/cliv2/go.sum +++ b/cliv2/go.sum @@ -731,8 +731,8 @@ github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1 h1:9RKY9NdX5DrJ github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1/go.mod h1:38w+dcAQp9eG3P5t2eNS9eG0reut10AeJjLv5lJ5lpM= github.com/snyk/error-catalog-golang-public v0.0.0-20240425141803-2516e42296c3 h1:ZUaY5LIVGQ0GScf1SsaqvUxaiGbBKgBBLsQUgB4Zx5o= github.com/snyk/error-catalog-golang-public v0.0.0-20240425141803-2516e42296c3/go.mod h1:Ytttq7Pw4vOCu9NtRQaOeDU2dhBYUyNBe6kX4+nIIQ4= -github.com/snyk/go-application-framework v0.0.0-20240515114301-86824c982460 h1:gH7tm6K3Xb3i39jBdYf0YPksKHinhpP7GOWIVJtYaR8= -github.com/snyk/go-application-framework v0.0.0-20240515114301-86824c982460/go.mod h1:5gvF6i1sqmk1oN21ZbX/EDaEyQtPMfK1pbYCwtS+Fvw= +github.com/snyk/go-application-framework v0.0.0-20240517080240-d22ced1f98d1 h1:VQ3rIrdzxKGfQ7oeEeIWaxWoiZFLUlrcdgKfuXWcCjU= +github.com/snyk/go-application-framework v0.0.0-20240517080240-d22ced1f98d1/go.mod h1:5gvF6i1sqmk1oN21ZbX/EDaEyQtPMfK1pbYCwtS+Fvw= github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65 h1:CEQuYv0Go6MEyRCD3YjLYM2u3Oxkx8GpCpFBd4rUTUk= github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65/go.mod h1:88KbbvGYlmLgee4OcQ19yr0bNpXpOr2kciOthaSzCAg= github.com/snyk/policy-engine v0.30.11 h1:wUy5LMar2vccMbNM62MSBRdjAQAhAbIm7aNXXO+g2tk= diff --git a/test/jest/acceptance/auth.spec.ts b/test/jest/acceptance/auth.spec.ts index 8da618b6ef..d2921287e7 100644 --- a/test/jest/acceptance/auth.spec.ts +++ b/test/jest/acceptance/auth.spec.ts @@ -36,6 +36,10 @@ describe('Auth', () => { beforeEach(async () => { initialConfig = await getCliConfig(); + // delete config + await runSnykCLI(`config clear`, { + env, + }); }); afterEach(async () => { @@ -51,11 +55,6 @@ describe('Auth', () => { }, ); expect(code).toEqual(0); - - // delete test token - await runSnykCLI(`config unset INTERNAL_OAUTH_TOKEN_STORAGE`, { - env, - }); }); it('fails to us oauth client credentials grant to authenticate', async () => { @@ -90,4 +89,18 @@ describe('Auth', () => { expect(resultConfigGet.code).toEqual(0); expect(resultConfigGet.stdout).toContain(serverToken); }); + + it('fall back to API token based authentication for IDEs per default', async () => { + const { code } = await runSnykCLI(`auth`, { + env: { ...env, SNYK_INTEGRATION_NAME: 'VS_CODE' }, + }); + + const resultConfigGet = await runSnykCLI('config get api', { + env, + }); + + expect(code).toEqual(0); + expect(resultConfigGet.code).toEqual(0); + expect(resultConfigGet.stdout).toContain(serverToken); + }); });