-
Notifications
You must be signed in to change notification settings - Fork 561
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1654 from snyk/feat/iac-experimental-terraform-su…
…pport feat: iac experimental terraform support
- Loading branch information
Showing
14 changed files
with
242 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import { EngineType } from './types'; | ||
|
||
export const LOCAL_POLICY_ENGINE_DIR = `.iac-data`; | ||
|
||
const KUBERNETES_POLICY_ENGINE_WASM_PATH = path.join( | ||
LOCAL_POLICY_ENGINE_DIR, | ||
'k8s_policy.wasm', | ||
); | ||
const KUBERNETES_POLICY_ENGINE_DATA_PATH = path.join( | ||
LOCAL_POLICY_ENGINE_DIR, | ||
'k8s_data.json', | ||
); | ||
const TERRAFORM_POLICY_ENGINE_WASM_PATH = path.join( | ||
LOCAL_POLICY_ENGINE_DIR, | ||
'tf_policy.wasm', | ||
); | ||
const TERRAFORM_POLICY_ENGINE_DATA_PATH = path.join( | ||
LOCAL_POLICY_ENGINE_DIR, | ||
'tf_data.json', | ||
); | ||
|
||
export const REQUIRED_LOCAL_CACHE_FILES = [ | ||
KUBERNETES_POLICY_ENGINE_WASM_PATH, | ||
KUBERNETES_POLICY_ENGINE_DATA_PATH, | ||
TERRAFORM_POLICY_ENGINE_WASM_PATH, | ||
TERRAFORM_POLICY_ENGINE_DATA_PATH, | ||
]; | ||
|
||
export function isLocalCacheExists(): boolean { | ||
return REQUIRED_LOCAL_CACHE_FILES.every(fs.existsSync); | ||
} | ||
|
||
export function getLocalCachePath(engineType: EngineType) { | ||
switch (engineType) { | ||
case EngineType.Kubernetes: | ||
return [ | ||
`${process.cwd()}/${KUBERNETES_POLICY_ENGINE_WASM_PATH}`, | ||
`${process.cwd()}/${KUBERNETES_POLICY_ENGINE_DATA_PATH}`, | ||
]; | ||
case EngineType.Terraform: | ||
return [ | ||
`${process.cwd()}/${TERRAFORM_POLICY_ENGINE_WASM_PATH}`, | ||
`${process.cwd()}/${TERRAFORM_POLICY_ENGINE_DATA_PATH}`, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import * as hclToJson from 'hcl-to-json'; | ||
import * as YAML from 'js-yaml'; | ||
import { EngineType, IacFileData, IacFileMetadata } from './types'; | ||
|
||
const REQUIRED_K8S_FIELDS = ['apiVersion', 'kind', 'metadata']; | ||
|
||
export function tryParseIacFile( | ||
fileMetadata: IacFileMetadata, | ||
fileContent: string, | ||
): Array<IacFileData> { | ||
switch (fileMetadata.fileType) { | ||
case 'yaml': | ||
case 'yml': | ||
case 'json': | ||
return tryParsingKubernetesFile(fileContent, fileMetadata); | ||
case 'tf': | ||
return [tryParsingTerraformFile(fileContent, fileMetadata)]; | ||
default: | ||
throw new Error('Invalid IaC file'); | ||
} | ||
} | ||
|
||
function tryParsingKubernetesFile( | ||
fileContent: string, | ||
fileMetadata: IacFileMetadata, | ||
): IacFileData[] { | ||
const yamlDocuments = YAML.safeLoadAll(fileContent); | ||
|
||
return yamlDocuments.map((parsedYamlDocument, docId) => { | ||
if ( | ||
REQUIRED_K8S_FIELDS.every((requiredField) => | ||
parsedYamlDocument.hasOwnProperty(requiredField), | ||
) | ||
) { | ||
return { | ||
...fileMetadata, | ||
fileContent: fileContent, | ||
jsonContent: parsedYamlDocument, | ||
engineType: EngineType.Kubernetes, | ||
docId, | ||
}; | ||
} else { | ||
throw new Error('Invalid K8s File!'); | ||
} | ||
}); | ||
} | ||
|
||
function tryParsingTerraformFile( | ||
fileContent: string, | ||
fileMetadata: IacFileMetadata, | ||
): IacFileData { | ||
try { | ||
// TODO: This parser does not fail on inavlid Terraform files! it is here temporarily. | ||
// cloud-config team will replace it to a valid parser for the beta release. | ||
const parsedData = hclToJson(fileContent); | ||
return { | ||
...fileMetadata, | ||
fileContent: fileContent, | ||
jsonContent: parsedData, | ||
engineType: EngineType.Terraform, | ||
}; | ||
} catch (err) { | ||
throw new Error('Invalid Terraform File!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.