From 270369bc2629a904ad0c9865b76cc664029083e7 Mon Sep 17 00:00:00 2001 From: Holly Wilsey <43147347+hwilsey-r7@users.noreply.github.com> Date: Wed, 1 Apr 2020 15:02:14 -0400 Subject: [PATCH] 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 --- README.md | 1 + icon_validator/rules/__init__.py | 4 +- .../dockerfile_parent_validator.py | 9 +-- .../plugin_validators/runtime_validator.py | 72 +++++++++++++++++++ setup.py | 2 +- 5 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 icon_validator/rules/plugin_validators/runtime_validator.py diff --git a/README.md b/README.md index 45c8908c..1c771db4 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ to simulate the `--all` flag. ## Changelog +* 2.21.0 - Add new runtime validator to align with 4.0.0 release of InsightConnect Python Plugin Runtime * 2.20.0 - Add plugin utilization workflow validator | Fix issue where numbers in screenshot titles would cause validation to fail * 2.19.0 - Add new `example` input to whitelist in SpecPropertiesValidator * 2.18.0 - Add .icon file validator diff --git a/icon_validator/rules/__init__.py b/icon_validator/rules/__init__.py index e42835e0..fc3500f9 100644 --- a/icon_validator/rules/__init__.py +++ b/icon_validator/rules/__init__.py @@ -35,6 +35,7 @@ from icon_validator.rules.plugin_validators.vendor_validator import * from icon_validator.rules.plugin_validators.version_validator import * from icon_validator.rules.plugin_validators.support_validator import * +from icon_validator.rules.plugin_validators.runtime_validator import * # Workflow validators from icon_validator.rules.workflow_validators.workflow_help_validator import * @@ -79,7 +80,8 @@ OutputValidator(), RegenerationValidator(), HelpInputOutputValidator(), - SupportValidator() + SupportValidator(), + RuntimeValidator() ] JENKINS_VALIDATORS = [ diff --git a/icon_validator/rules/plugin_validators/dockerfile_parent_validator.py b/icon_validator/rules/plugin_validators/dockerfile_parent_validator.py index e81bd64d..0507c926 100644 --- a/icon_validator/rules/plugin_validators/dockerfile_parent_validator.py +++ b/icon_validator/rules/plugin_validators/dockerfile_parent_validator.py @@ -7,11 +7,12 @@ def validate(self, spec): spec_str = "".join(spec.raw_dockerfile()) valid_images = [ - "komand/go-plugin-2", "komand/python-2-plugin", "komand/python-3-plugin", "komand/python-pypy3-plugin", - "komand/python-3-slim-plugin", "komand/python-2-slim-plugin", - "komand/python-2-27-slim-plugin", "komand/python-3-37-slim-plugin", "komand/python-2-27-plugin", + "komand/go-plugin-2", "komand/python-3-plugin", "komand/python-pypy3-plugin", + "komand/python-3-slim-plugin", + "komand/python-3-37-slim-plugin", "komand/python-3-37-plugin", - "komand/python-2-27-full-plugin", "komand/python-pypy3-full-plugin" + "komand/python-pypy3-full-plugin", + "rapid7/insightconnect-python-3-38-plugin", "rapid7/insightconnect-python-3-38-slim-plugin" ] root_spec_found = False for line in spec.raw_dockerfile(): diff --git a/icon_validator/rules/plugin_validators/runtime_validator.py b/icon_validator/rules/plugin_validators/runtime_validator.py new file mode 100644 index 00000000..f068de99 --- /dev/null +++ b/icon_validator/rules/plugin_validators/runtime_validator.py @@ -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) diff --git a/setup.py b/setup.py index 521df8aa..06871da3 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setup(name="insightconnect_integrations_validators", - version="2.20.0", + version="2.21.0", description="Validator tooling for InsightConnect integrations", long_description=long_description, long_description_content_type="text/markdown",