Skip to content

Commit

Permalink
SOAR-131 - Implement validators for runtime consistency (#96)
Browse files Browse the repository at this point in the history
* 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
hwilsey-r7 authored Apr 1, 2020
1 parent 9521c25 commit 270369b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion icon_validator/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -79,7 +80,8 @@
OutputValidator(),
RegenerationValidator(),
HelpInputOutputValidator(),
SupportValidator()
SupportValidator(),
RuntimeValidator()
]

JENKINS_VALIDATORS = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
72 changes: 72 additions & 0 deletions icon_validator/rules/plugin_validators/runtime_validator.py
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)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 270369b

Please sign in to comment.