Skip to content

Commit

Permalink
Merge pull request #2539 from snyk/feat/unmanaged-security-url
Browse files Browse the repository at this point in the history
feat: support unmanaged snyk security url
  • Loading branch information
anthogez committed Jan 12, 2022
2 parents f76f946 + a3ec49d commit 97a3ed9
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 17 deletions.
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
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
@@ -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
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
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
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
Expand Up @@ -90,6 +90,7 @@ export interface Options {
tags?: string;
'target-reference'?: string;
'exclude-base-image-vulns'?: boolean;
supportUnmanagedVulnDB?: boolean;
}

// TODO(kyegupov): catch accessing ['undefined-properties'] via noImplicitAny
Expand Down
15 changes: 15 additions & 0 deletions test/jest/unit/lib/ecosystems/common.spec.ts
@@ -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);
},
);
});
37 changes: 37 additions & 0 deletions test/jest/unit/lib/feature-flags/feature-flags.spec.ts
@@ -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');
});
});

0 comments on commit 97a3ed9

Please sign in to comment.