Skip to content

Commit

Permalink
Merge pull request #1912 from snyk/fix/tf-plan-project-mismatch
Browse files Browse the repository at this point in the history
fix: tf plan project mismatch
  • Loading branch information
rontalx authored May 13, 2021
2 parents ec0f2b0 + 1592998 commit d3893a2
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CustomError } from '../../../../../lib/errors';
import { IacProjectType } from '../../../../../lib/iac/constants';
import { getErrorStringCode } from '../error-utils';
import {
EngineType,
Expand Down Expand Up @@ -35,6 +36,7 @@ export function tryParsingKubernetesFile(
return {
...fileData,
jsonContent: parsedYamlDocument,
projectType: IacProjectType.K8S,
engineType: EngineType.Kubernetes,
docId,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '../types';
import { CustomError } from '../../../../../lib/errors';
import { getErrorStringCode } from '../error-utils';
import { IacProjectType } from '../../../../../lib/iac/constants';

export function tryParsingTerraformFile(
fileData: IacFileData,
Expand All @@ -16,6 +17,7 @@ export function tryParsingTerraformFile(
{
...fileData,
jsonContent: hclToJson(fileData.fileContent),
projectType: IacProjectType.TERRAFORM,
engineType: EngineType.Terraform,
},
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '../types';
import { CustomError } from '../../../../../lib/errors';
import { getErrorStringCode } from '../error-utils';
import { IacProjectType } from '../../../../../lib/iac/constants';

function terraformPlanReducer(
scanInput: TerraformScanInput,
Expand Down Expand Up @@ -103,6 +104,7 @@ export function tryParsingTerraformPlan(
...terraformPlanFile,
jsonContent: extractResourcesForScan(terraformPlanJson, isFullScan),
engineType: EngineType.Terraform,
projectType: IacProjectType.TERRAFORM,
},
];
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import {
} from './types';
import * as path from 'path';
import { SEVERITY } from '../../../../lib/snyk-test/common';
import {
IacProjectType,
projectTypeByFileType,
} from '../../../../lib/iac/constants';
import { IacProjectType } from '../../../../lib/iac/constants';
import { CustomError } from '../../../../lib/errors';
import { extractLineNumber } from './extract-line-number';
import { getErrorStringCode } from './error-utils';
Expand Down Expand Up @@ -81,7 +78,7 @@ function formatScanResult(
formattedIssues,
severityThreshold,
),
projectType: projectTypeByFileType[scanResult.fileType],
projectType: scanResult.projectType,
},
meta: {
...meta,
Expand Down
1 change: 1 addition & 0 deletions src/cli/commands/test/iac-local-execution/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const VALID_FILE_TYPES = ['tf', 'json', 'yaml', 'yml'];

export interface IacFileParsed extends IacFileData {
jsonContent: Record<string, unknown> | TerraformScanInput;
projectType: IacProjectType;
engineType: EngineType;
docId?: number;
}
Expand Down
6 changes: 6 additions & 0 deletions test/jest/unit/iac-unit-tests/file-parser.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getExpectedResult,
PlanOutputCase,
} from './terraform-plan-parser.fixtures';
import { IacProjectType } from '../../../../src/lib/iac/constants';

const kubernetesYamlFileContent = `
apiVersion: v1
Expand Down Expand Up @@ -121,20 +122,23 @@ export const invalidYamlFileDataStub: IacFileData = {
export const expectedKubernetesYamlParsingResult: IacFileParsed = {
...kubernetesYamlFileDataStub,
docId: 0,
projectType: IacProjectType.K8S,
engineType: EngineType.Kubernetes,
jsonContent: kubernetesJson,
};

export const expectedKubernetesJsonParsingResult: IacFileParsed = {
...kubernetesJsonFileDataStub,
docId: 0,
projectType: IacProjectType.K8S,
engineType: EngineType.Kubernetes,
jsonContent: kubernetesJson,
};

export const expectedMultipleKubernetesYamlsParsingResult: IacFileParsed = {
...multipleKubernetesYamlsFileDataStub,
docId: 0,
projectType: IacProjectType.K8S,
engineType: EngineType.Kubernetes,
jsonContent: kubernetesJson,
};
Expand Down Expand Up @@ -200,6 +204,7 @@ export const terraformPlanMissingFieldsDataStub: IacFileData = {
export const expectedTerraformParsingResult: IacFileParsed = {
...terraformFileDataStub,
engineType: EngineType.Terraform,
projectType: IacProjectType.TERRAFORM,
jsonContent: {
resource: {
aws_security_group: {
Expand All @@ -222,6 +227,7 @@ export const expectedTerraformParsingResult: IacFileParsed = {
export const expectedTerraformJsonParsingResult: IacFileParsed = {
...terraformPlanDataStub,
engineType: EngineType.Terraform,
projectType: IacProjectType.TERRAFORM,
jsonContent: getExpectedResult(false, PlanOutputCase.Create),
};

Expand Down
3 changes: 3 additions & 0 deletions test/jest/unit/iac-unit-tests/file-scanner.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
IacFileParsed,
PolicyMetadata,
} from '../../../../src/cli/commands/test/iac-local-execution/types';
import { IacProjectType } from '../../../../src/lib/iac/constants';
import { SEVERITY } from '../../../../src/lib/snyk-test/common';

export const expectedViolatedPoliciesForK8s: Array<PolicyMetadata> = [
Expand Down Expand Up @@ -52,6 +53,7 @@ export const expectedViolatedPoliciesForTerraform: Array<PolicyMetadata> = [

export const paresdKubernetesFileStub: IacFileParsed = {
engineType: EngineType.Kubernetes,
projectType: IacProjectType.K8S,
fileContent: 'dont-care',
filePath: 'dont-care',
fileType: 'yml',
Expand All @@ -75,6 +77,7 @@ export const paresdKubernetesFileStub: IacFileParsed = {
};
export const parsedTerraformFileStub: IacFileParsed = {
engineType: EngineType.Terraform,
projectType: IacProjectType.TERRAFORM,
fileContent: 'dont-care',
filePath: 'dont-care',
fileType: 'tf',
Expand Down
2 changes: 2 additions & 0 deletions test/jest/unit/iac-unit-tests/results-formatter.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function generateScanResults(): Array<IacFileScanResult> {
violatedPolicies: [{ ...policyStub }],
jsonContent: { dontCare: null },
docId: 0,
projectType: IacProjectType.K8S,
engineType: EngineType.Kubernetes,
fileContent: 'dont-care',
filePath: 'dont-care',
Expand All @@ -53,6 +54,7 @@ export function generateScanResults(): Array<IacFileScanResult> {
violatedPolicies: [{ ...anotherPolicyStub }],
jsonContent: { dontCare: null },
docId: 0,
projectType: IacProjectType.K8S,
engineType: EngineType.Kubernetes,
fileContent: 'dont-care',
filePath: 'dont-care',
Expand Down
2 changes: 2 additions & 0 deletions test/jest/unit/iac-unit-tests/terraform-plan-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
EngineType,
TerraformPlanJson,
} from '../../../../src/cli/commands/test/iac-local-execution/types';
import { IacProjectType } from '../../../../src/lib/iac/constants';

describe('tryParsingTerraformPlan', () => {
/*
Expand Down Expand Up @@ -50,6 +51,7 @@ describe('tryParsingTerraformPlan', () => {
// Assert
expect(parsedTerraformPlan[0]).toEqual({
...iacFileData,
projectType: IacProjectType.TERRAFORM,
engineType: EngineType.Terraform,
jsonContent: expectedResources,
});
Expand Down
10 changes: 10 additions & 0 deletions test/smoke/spec/iac/snyk_test_local_exec_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Describe "Snyk iac local test command"
The status should equal 1
The output should include '"id": "SNYK-CC-K8S-1",'
The output should include '"packageManager": "k8sconfig",'
The output should include '"projectType": "k8sconfig",'
The result of function check_valid_json should be success
End
End
Expand Down Expand Up @@ -118,6 +119,7 @@ Describe "Snyk iac local test command"
The status should equal 1
The output should include '"id": "SNYK-CC-TF-1",'
The output should include '"packageManager": "terraformconfig",'
The output should include '"projectType": "terraformconfig",'
The result of function check_valid_json should be success
End

Expand Down Expand Up @@ -275,5 +277,13 @@ Describe "Snyk iac local test command"
The status should equal 2 # failure
The output should include "Unsupported value"
End

It "succesfully scans a TF-Plan with the --json output flag"
When run snyk iac test ../fixtures/iac/terraform-plan/tf-plan-create.json --json
The status should equal 1
The output should include '"packageManager": "terraformconfig",'
The output should include '"projectType": "terraformconfig",'
The result of function check_valid_json should be success
End
End
End

0 comments on commit d3893a2

Please sign in to comment.