Skip to content

Commit

Permalink
Merge pull request #5376 from snyk/feat/CLI-299_config_sanity
Browse files Browse the repository at this point in the history
chore(config): Introduce sanity check for config environment
  • Loading branch information
PeterSchafer authored Jul 25, 2024
2 parents 3066f93 + e2a3cd6 commit c4c8feb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 11 deletions.
16 changes: 16 additions & 0 deletions cliv2/cmd/cliv2/logheader.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ func getFipsStatus(config configuration.Configuration) string {
}

func writeLogHeader(config configuration.Configuration, networkAccess networking.NetworkAccess) {
keys := []string{configuration.API_URL, configuration.AUTHENTICATION_TOKEN, configuration.AUTHENTICATION_BEARER_TOKEN, configuration.ORGANIZATION}

authorization, _, userAgent := logHeaderAuthorizationInfo(config, networkAccess)

org := config.GetString(configuration.ORGANIZATION)
Expand Down Expand Up @@ -116,4 +118,18 @@ func writeLogHeader(config configuration.Configuration, networkAccess networking
tablePrint("Features", "")
tablePrint(" preview", previewFeaturesEnabled)
tablePrint(" fips", fipsEnabled)
tablePrint("Checks", "")

checkCount := 0
for _, key := range keys {
keysSpecified := config.GetAllKeysThatContainValues(key)
if len(keysSpecified) > 1 {
checkCount++
tablePrint(" Configuration", fmt.Sprintf("Possible unexpected behavior, the following configuration values might override each other %s", strings.ToUpper(strings.Join(keysSpecified, ", "))))
}
}

if checkCount == 0 {
tablePrint(" Configuration", "all good")
}
}
4 changes: 2 additions & 2 deletions cliv2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ require (
github.com/snyk/cli-extension-iac-rules v0.0.0-20240422133948-ae17a4306672
github.com/snyk/cli-extension-sbom v0.0.0-20240722082449-69a631da39ad
github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1
github.com/snyk/error-catalog-golang-public v0.0.0-20240715110939-a528e5c7d26d
github.com/snyk/go-application-framework v0.0.0-20240716082832-a722d639217c
github.com/snyk/error-catalog-golang-public v0.0.0-20240724122202-c7d3fb545c88
github.com/snyk/go-application-framework v0.0.0-20240725135406-ef950283001b
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-20240724110216-fff14d6d09e0
Expand Down
8 changes: 4 additions & 4 deletions cliv2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -742,10 +742,10 @@ github.com/snyk/code-client-go v1.8.0 h1:6H883KAn7ybiSIxhvL2QR9yEyHgAwA2+9WVHMDN
github.com/snyk/code-client-go v1.8.0/go.mod h1:orU911flV1kJQOlxxx0InUQkAfpBrcERsb2olfnlI8s=
github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1 h1:9RKY9NdX5DrJAoVXDP0JiqrXT+4Nb9NH8pjEcA0NsLA=
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-20240715110939-a528e5c7d26d h1:LtvOq6MD7Tei0xx0/41AMiDD5jfvjgJ7xGp7e9PL9fY=
github.com/snyk/error-catalog-golang-public v0.0.0-20240715110939-a528e5c7d26d/go.mod h1:Ytttq7Pw4vOCu9NtRQaOeDU2dhBYUyNBe6kX4+nIIQ4=
github.com/snyk/go-application-framework v0.0.0-20240716082832-a722d639217c h1:YFjWoOvvyBgOWrOrAFsB9A9LHbV0+S3c4gENWRS1p94=
github.com/snyk/go-application-framework v0.0.0-20240716082832-a722d639217c/go.mod h1:X7PyPo6Vuf+nlxkHmqvhg0suwHmx+I2quCNKFHdaHp4=
github.com/snyk/error-catalog-golang-public v0.0.0-20240724122202-c7d3fb545c88 h1:ZiFV5IDPI2p1wx1D3B2iSC/8nxTGvlKuyUekZlm1ptU=
github.com/snyk/error-catalog-golang-public v0.0.0-20240724122202-c7d3fb545c88/go.mod h1:Ytttq7Pw4vOCu9NtRQaOeDU2dhBYUyNBe6kX4+nIIQ4=
github.com/snyk/go-application-framework v0.0.0-20240725135406-ef950283001b h1:LOuM03NjJlUgz4pjV6OpvEPR0/94glwSfLkxF11wjF0=
github.com/snyk/go-application-framework v0.0.0-20240725135406-ef950283001b/go.mod h1:3DhXDHbBbGWRRZESbYVZyunyIDaet9SOtuOZCK7AC3g=
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=
Expand Down
43 changes: 38 additions & 5 deletions test/jest/acceptance/snyk-config/snyk-config-environment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@ jest.setTimeout(1000 * 30);

describe('snyk config environment', () => {
let initialConfig: Record<string, string> = {};
const env = { ...process.env };

beforeEach(async () => {
beforeAll(async () => {
initialConfig = await getCliConfig();
delete env.SNYK_TOKEN;
delete env.SNYK_API;
delete env.SNYK_CFG_ORG;
});

afterEach(async () => {
afterAll(async () => {
await restoreCliConfig(initialConfig);
});

beforeEach(async () => {
await runSnykCLI(`config clear`); // start from a clean state
});

it('successfully configure with a partial DNS name', async () => {
const { code, stderr } = await runSnykCLI(`config environment dev`);
const { code, stderr } = await runSnykCLI(`config environment dev`, {
env: env,
});
expect(stderr).toEqual('');
expect(code).toEqual(0);

Expand All @@ -29,6 +39,9 @@ describe('snyk config environment', () => {
it('successfully configure with a URL', async () => {
const { code, stderr } = await runSnykCLI(
`config environment https://api.dev.snyk.io`,
{
env: env,
},
);
expect(stderr).toEqual('');
expect(code).toEqual(0);
Expand All @@ -38,10 +51,30 @@ describe('snyk config environment', () => {
});

it('fail with an invalid env alias', async () => {
const {
code,
stderr,
} = await runSnykCLI(`config environment randomEnvName`, { env: env });
expect(stderr).toEqual('');
expect(code).toEqual(2);
});

it('fail with env var collision', async () => {
const { code, stderr } = await runSnykCLI(`config environment eu`, {
env: { ...env, SNYK_TOKEN: 'https://api.snyk.io' },
});
expect(stderr).toEqual('');
expect(code).toEqual(2);
});

it('successfully configure by ignoring env var collisions', async () => {
const { code, stderr } = await runSnykCLI(
`config environment randomEnvName`,
`config environment eu --no-check`,
{
env: { ...env, SNYK_TOKEN: 'https://api.snyk.io' },
},
);
expect(stderr).toEqual('');
expect(code).toEqual(2);
expect(code).toEqual(0);
});
});

0 comments on commit c4c8feb

Please sign in to comment.