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: support unmanaged snyk security url #2539

Merged
merged 1 commit into from
Jan 12, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"rimraf": "^2.6.3",
"semver": "^6.0.0",
"snyk-config": "4.0.0",
"snyk-cpp-plugin": "2.14.1",
"snyk-cpp-plugin": "2.15.0",
"snyk-docker-plugin": "^4.33.0",
"snyk-go-plugin": "1.18.0",
"snyk-gradle-plugin": "3.17.0",
Expand Down
5 changes: 5 additions & 0 deletions src/lib/ecosystems/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Ecosystem } from './types';

export function isUnmanagedEcosystem(ecosystem: Ecosystem): boolean {
return ecosystem === 'cpp';
}
4 changes: 2 additions & 2 deletions src/lib/ecosystems/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
validateProjectAttributes,
validateTags,
} from '../../cli/commands/monitor';
import { isUnmanagedEcosystem } from './common';

const SEPARATOR = '\n-------------------------------------------------------\n';

Expand Down Expand Up @@ -81,8 +82,7 @@ async function selectAndExecuteMonitorStrategy(
scanResultsByPath: { [dir: string]: ScanResult[] },
options: Options,
): Promise<[EcosystemMonitorResult[], EcosystemMonitorError[]]> {
const isUnmanagedEcosystem = ecosystem === 'cpp';
return isUnmanagedEcosystem
return isUnmanagedEcosystem(ecosystem)
? await resolveAndMonitorFacts(scanResultsByPath, options)
: await monitorDependencies(scanResultsByPath, options);
}
Expand Down
17 changes: 14 additions & 3 deletions src/lib/ecosystems/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { TestDependenciesResponse } from '../snyk-test/legacy';
import { assembleQueryString } from '../snyk-test/common';
import { getAuthHeader } from '../api-token';
import { resolveAndTestFacts } from './resolve-test-facts';
import { hasFeatureFlag } from '../feature-flags';
import { isUnmanagedEcosystem } from './common';

export async function testEcosystem(
ecosystem: Ecosystem,
Expand Down Expand Up @@ -44,11 +46,21 @@ export async function testEcosystem(
}
const emptyResults: ScanResult[] = [];
const scanResults = emptyResults.concat(...Object.values(scanResultsByPath));

const enhancedOptions = { ...options };

if (isUnmanagedEcosystem(ecosystem)) {
enhancedOptions.supportUnmanagedVulnDB = await hasFeatureFlag(
'snykUnmanagedVulnDB',
options,
);
}

const readableResult = await plugin.display(
scanResults,
testResults,
errors,
options,
enhancedOptions,
);

return TestCommandResult.createHumanReadableTestCommandResult(
Expand All @@ -62,8 +74,7 @@ export async function selectAndExecuteTestStrategy(
scanResultsByPath: { [dir: string]: ScanResult[] },
options: Options,
): Promise<[TestResult[], string[]]> {
const isUnmanagedEcosystem = ecosystem === 'cpp';
return isUnmanagedEcosystem
return isUnmanagedEcosystem(ecosystem)
? await resolveAndTestFacts(ecosystem, scanResultsByPath, options)
: await testDependencies(scanResultsByPath, options);
}
Expand Down
17 changes: 17 additions & 0 deletions src/lib/feature-flags/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { getAuthHeader } from '../api-token';
import config from '../config';
import { assembleQueryString } from '../snyk-test/common';
import { OrgFeatureFlagResponse } from './types';
import { Options } from '../types';
import { AuthFailedError } from '../errors';

export async function isFeatureFlagSupportedForOrg(
featureFlag: string,
Expand All @@ -21,3 +23,18 @@ export async function isFeatureFlagSupportedForOrg(

return (response as any).body;
}

export async function hasFeatureFlag(
featureFlag: string,
options: Options,
): Promise<boolean | undefined> {
const { code, error, ok } = await isFeatureFlagSupportedForOrg(
featureFlag,
options.org,
);

if (code === 401 || code === 403) {
throw AuthFailedError(error, code);
}
return ok;
}
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export interface Options {
tags?: string;
'target-reference'?: string;
'exclude-base-image-vulns'?: boolean;
supportUnmanagedVulnDB?: boolean;
This conversation was marked as resolved.
Show resolved Hide resolved
}

// TODO(kyegupov): catch accessing ['undefined-properties'] via noImplicitAny
Expand Down
15 changes: 15 additions & 0 deletions test/jest/unit/lib/ecosystems/common.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { isUnmanagedEcosystem } from '../../../../../src/lib/ecosystems/common';

describe('isUnmanagedEcosystem fn', () => {
it.each`
actual | expected
${'cpp'} | ${true}
${'docker'} | ${false}
${'code'} | ${false}
`(
'should validate that given $actual as input, is considered or not an unmanaged ecosystem',
({ actual, expected }) => {
expect(isUnmanagedEcosystem(actual)).toEqual(expected);
},
);
});
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@danlucian good work here! What if we go with?

describe('isUnmanagedEcosystem fn', () => {
  it.each`
    actual      | expected
    ${'cpp'}    | ${true}
    ${'docker'} | ${false}
    ${'code'}   | ${false}
  `(
    'should validate that given $actual as input, is considered or not an unmanaged ecosystem',
    ({ actual, expected }) => {
      expect(isUnmanagedEcosystem(actual)).toEqual(expected);
    },
  );
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Screen Shot 2022-01-12 at 11 40 38

Copy link
Member

Choose a reason for hiding this comment

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

Looks good, I applied the changes 💪

37 changes: 37 additions & 0 deletions test/jest/unit/lib/feature-flags/feature-flags.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { hasFeatureFlag } from '../../../../../src/lib/feature-flags';
import * as request from '../../../../../src/lib/request';

describe('hasFeatureFlag fn', () => {
it.each`
hasFlag | expected
${true} | ${true}
${false} | ${false}
`(
'should validate that given an org with feature flag $hasFlag as input, hasFeatureFlag returns $expected',
async ({ hasFlag, expected }) => {
jest
.spyOn(request, 'makeRequest')
.mockResolvedValue({ body: { code: 200, ok: hasFlag } } as any);

const result = await hasFeatureFlag('test-ff', { path: 'test-path' });
expect(result).toEqual(expected);
},
);

it('should throw error if there are authentication/authorization failures', async () => {
jest.spyOn(request, 'makeRequest').mockResolvedValue({
body: { code: 401, error: 'Unauthorized', ok: false },
} as any);

await expect(
hasFeatureFlag('test-ff', { path: 'test-path' }),
).rejects.toThrowError('Unauthorized');

jest.spyOn(request, 'makeRequest').mockResolvedValue({
body: { code: 403, error: 'Forbidden', ok: false },
} as any);
await expect(
hasFeatureFlag('test-ff', { path: 'test-path' }),
).rejects.toThrowError('Forbidden');
});
});