-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SOAR-131 - Implement validators for runtime consistency (#96)
* Initial commit for python runtime validator updates * Updates to runtime validators * Removed print * Function rename * Validator updates + new Dockerfile validator. * Validator updates * Fix for komand import validation * Version updates * Import validator updates
- Loading branch information
1 parent
9521c25
commit 270369b
Showing
5 changed files
with
82 additions
and
6 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
72 changes: 72 additions & 0 deletions
72
icon_validator/rules/plugin_validators/runtime_validator.py
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,72 @@ | ||
import os | ||
import glob | ||
|
||
from icon_validator.rules.validator import KomandPluginValidator | ||
from icon_validator.exceptions import ValidationException | ||
|
||
|
||
class RuntimeValidator(KomandPluginValidator): | ||
|
||
@staticmethod | ||
def validate_setup(spec): | ||
if "setup.py" in os.listdir(spec.directory): | ||
with open(f"{spec.directory}/setup.py", "r") as file: | ||
setup_str = file.read().replace("\n", "") | ||
|
||
if 'install_requires=["insightconnect-plugin-runtime"]' not in setup_str\ | ||
and "install_requires=['insightconnect-plugin-runtime']" not in setup_str: | ||
raise ValidationException("Komand is no longer used for install_requires in setup.py. " | ||
"Use insightconnect-plugin-runtime instead.") | ||
|
||
@staticmethod | ||
def validate_imports(spec): | ||
for root, dirs, files in os.walk(spec.directory): | ||
for file in files: | ||
if file.endswith(".py"): | ||
with open(os.path.join(root, file), "r") as open_file: | ||
file_str = open_file.read() | ||
|
||
if "import komand\n" in file_str or "from komand " in file_str or "from komand." in file_str: | ||
raise ValidationException(f"Komand import found in {str(os.path.join(root, file))}. " | ||
"Komand is no longer used here. " | ||
"Use insightconnect-plugin-runtime instead.") | ||
|
||
@staticmethod | ||
def validate_caching(spec): | ||
if spec.spec_dictionary().get("cloud_ready") is True: | ||
actions_path = glob.glob(f"{spec.directory}/*/actions") | ||
for root, dirs, files in os.walk(actions_path[0]): | ||
for file in files: | ||
with open(os.path.join(root, file), 'r') as open_file: | ||
file_str = open_file.read().replace("\n", "") | ||
|
||
if "cache" in file_str: | ||
raise ValidationException(f"Cloud ready plugins cannot contain caching. " | ||
f"Update {str(os.path.join(root, file))}.") | ||
|
||
@staticmethod | ||
def validate_dockerfile(spec, latest_images): | ||
if "setup.py" in os.listdir(spec.directory): | ||
with open(f"{spec.directory}/setup.py", "r") as setup_file: | ||
setup_str = setup_file.read().replace("\n", "") | ||
|
||
if "insightconnect-plugin-runtime" in setup_str: | ||
with open(f"{spec.directory}/Dockerfile", "r") as docker_file: | ||
docker_str = docker_file.read().replace("\n", "") | ||
|
||
if not any(image in docker_str for image in latest_images): | ||
raise ValidationException("insightconnect-plugin-runtime is being used in setup.py. " | ||
"Update Dockerfile accordingly to use latest base image.") | ||
|
||
def validate(self, spec): | ||
latest_images = ["rapid7/insightconnect-python-3-38-plugin", | ||
"rapid7/insightconnect-python-3-38-slim-plugin"] | ||
RuntimeValidator.validate_dockerfile(spec, latest_images) | ||
|
||
with open(f"{spec.directory}/Dockerfile", "r") as file: | ||
docker_str = file.read().replace("\n", "") | ||
|
||
if any(image in docker_str for image in latest_images): | ||
RuntimeValidator.validate_setup(spec) | ||
RuntimeValidator.validate_imports(spec) | ||
RuntimeValidator.validate_caching(spec) |
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