Skip to content

Commit

Permalink
Python 3 Script - 16967 - Updated package installation (#2821)
Browse files Browse the repository at this point in the history
  • Loading branch information
igorski-r7 authored and ekelly-r7 committed Sep 24, 2024
1 parent a5d08af commit 04c0f67
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion plugins/python_3_script/.CHECKSUM
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"spec": "441f83b7f97f94804c7ae6ef98300add",
"spec": "c7ff086482db33567e135d3bc4f41537",
"manifest": "2c538fa19b6b5693104e6c71a09859e2",
"setup": "750744ab1e4d406ce443a91abe0c288d",
"schemas": [
Expand Down
2 changes: 1 addition & 1 deletion plugins/python_3_script/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM --platform=linux/amd64 rapid7/insightconnect-python-3-slim-plugin:6.1.1
FROM --platform=linux/amd64 rapid7/insightconnect-python-3-slim-plugin:6.1.2

LABEL organization=rapid7
LABEL sdk=python
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import sys
import os
from subprocess import CalledProcessError, TimeoutExpired, check_output, run # noqa: B404

import insightconnect_plugin_runtime
from insightconnect_plugin_runtime.exceptions import ConnectionTestException

from .schema import ConnectionSchema, Input
from typing import Dict, Any


class Connection(insightconnect_plugin_runtime.Connection):
Expand All @@ -13,50 +15,47 @@ def __init__(self):
self.dependencies, self.timeout = None, None
self.script_credentials = None

def connect(self, params={}):
self.timeout = params.get(Input.TIMEOUT)
self.dependencies = params.get(Input.MODULES)
def connect(self, params={}) -> None:
self.timeout = params.get(Input.TIMEOUT, 60)
self.dependencies = params.get(Input.MODULES, [])
self.script_credentials = {
"username": params.get(Input.SCRIPT_USERNAME_AND_PASSWORD, {}).get("username"),
"password": params.get(Input.SCRIPT_USERNAME_AND_PASSWORD, {}).get("password"),
"secret_key": params.get(Input.SCRIPT_SECRET_KEY, {}).get("secretKey"),
}

def test(self):
connection_test_output = {"success": True}

def test(self) -> Dict[str, Any]:
self.logger.info("[*] Performing Python version check...")
check = str(check_output(["python", "--version"]), "utf-8") # noqa: B607,B603
self.logger.info(check)
python_version = str(check_output(["python", "--version"]), "utf-8") # noqa: B607,B603
self.logger.info(python_version)

if "Python 3." not in check:
if "Python 3." not in python_version:
raise ConnectionTestException(cause="[-] Python 3 is not installed correctly")

if not self.dependencies:
return connection_test_output

self.logger.info(f"[*] Installing user-specified dependencies ({self.dependencies})...")
self.install_dependencies()
self.logger.info("[*] Dependencies installed!\n")
return connection_test_output
if self.dependencies:
self.logger.info(f"[*] Installing user-specified dependencies ({self.dependencies})...")
self.install_dependencies()
self.logger.info("[*] Dependencies installed!\n")
return {"success": True}

@staticmethod
def _set_pythonuserbase():
def _set_python_userbase() -> None:
os.environ.update({"PYTHONUSERBASE": "/var/cache/python_dependencies"})

def install_dependencies(self):
dependencies = " ".join(self.dependencies)
command = f"pip install --user {dependencies}"

self._set_pythonuserbase()

def install_dependencies(self) -> None:
self._set_python_userbase()
try:
run(args=command.split(" "), capture_output=True, timeout=self.timeout, check=True) # noqa: B603
run( # noqa: B603
args=[sys.executable, "-m", "pip", "install"] + self.dependencies,
capture_output=True,
timeout=self.timeout,
check=True,
)
except TimeoutExpired:
raise ConnectionTestException(
cause="Error: Installing Python dependencies exceeded timeout", assistance="Consider increasing timeout"
)
except CalledProcessError as error:
raise ConnectionTestException(
cause=f"Error: Non-zero exit code returned. Message: {error.output.decode('UTF8')}"
cause=f"Error: Non-zero exit code returned. Message: {error.output.decode('utf-8')}"
)
2 changes: 1 addition & 1 deletion plugins/python_3_script/plugin.spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ supported_versions: ["Python 3.9.19"]
fedramp_ready: true
sdk:
type: slim
version: 6.1.1
version: 6.1.2
user: root
packages:
- libxslt-dev
Expand Down

0 comments on commit 04c0f67

Please sign in to comment.