wazuh / qa-system-framework

GNU General Public License v2.0
1 stars 3 forks source link

Added function to run a Python script on a hosts #53

Closed BelenValdivia closed 9 months ago

BelenValdivia commented 1 year ago

Description

Add function to run a Python script on a host

Testing

>>> hm.run_python_script('agent2', 'python_script.py')
{'changed': True, 'rc': 0, 'stderr': 'Shared connection to x.x.x.x closed.\r\n', 'stderr_lines': ['Shared connection to x.x.x.x closed.'], 'stdout': '{\r\n    "manufacturers": [\r\n        "Acura",\r\n        "Alfa-Romeo",\r\n        "Aston-Martin",\r\n        "Audi",\r\n        "Bentley",\r\n        "BMW",\r\n        "Bugatti",\r\n        "Buick",\r\n        "Cadillac",\r\n        "Chevrolet",\r\n        "Chrysler",\r\n        "Citroen",\r\n        "Deus Automobiles",\r\n        "Dodge",\r\n        "Ferrari",\r\n        "Fiat",\r\n        "Ford",\r\n        "Geely",\r\n        "Genesis",\r\n        "GMC",\r\n        "Honda",\r\n        "Hyundai",\r\n        "Infiniti",\r\n        "Jaguar",\r\n        "Jeep",\r\n        "Kia",\r\n        "Koenigsegg",\r\n        "Lamborghini",\r\n        "Lancia",\r\n        "Land Rover",\r\n        "Lexus",\r\n        "Lincoln",\r\n        "Lotus",\r\n        "Maserati",\r\n        "Maybach",\r\n        "Mazda",\r\n        "McLaren",\r\n        "Mercedes",\r\n        "Mini",\r\n        "Mitsubishi",\r\n        "Nissan",\r\n        "Opel",\r\n        "Pagani",\r\n        "Peugeot",\r\n        "Pontiac",\r\n        "Porsche",\r\n        "Ram",\r\n        "Renault",\r\n        "Rolls-Royce",\r\n        "Skoda",\r\n        "Smart",\r\n        "Subaru",\r\n        "Suzuki",\r\n        "Tesla",\r\n        "Toyota",\r\n        "Volkswagen",\r\n        "Volvo"\r\n    ]\r\n}\r\n', 'stdout_lines': ['{', '    "manufacturers": [', '        "Acura",', '        "Alfa-Romeo",', '        "Aston-Martin",', '        "Audi",', '        "Bentley",', '        "BMW",', '        "Bugatti",', '        "Buick",', '        "Cadillac",', '        "Chevrolet",', '        "Chrysler",', '        "Citroen",', '        "Deus Automobiles",', '        "Dodge",', '        "Ferrari",', '        "Fiat",', '        "Ford",', '        "Geely",', '        "Genesis",', '        "GMC",', '        "Honda",', '        "Hyundai",', '        "Infiniti",', '        "Jaguar",', '        "Jeep",', '        "Kia",', '        "Koenigsegg",', '        "Lamborghini",', '        "Lancia",', '        "Land Rover",', '        "Lexus",', '        "Lincoln",', '        "Lotus",', '        "Maserati",', '        "Maybach",', '        "Mazda",', '        "McLaren",', '        "Mercedes",', '        "Mini",', '        "Mitsubishi",', '        "Nissan",', '        "Opel",', '        "Pagani",', '        "Peugeot",', '        "Pontiac",', '        "Porsche",', '        "Ram",', '        "Renault",', '        "Rolls-Royce",', '        "Skoda",', '        "Smart",', '        "Subaru",', '        "Suzuki",', '        "Tesla",', '        "Toyota",', '        "Volkswagen",', '        "Volvo"', '    ]', '}']}
>>> from wazuh_qa_framework.system.host_manager import HostManager
>>> hm = HostManager('inventory2.yml')
>>> hm.run_python_script('agent1', '/home/belen/Repositories/qa-system-framework/python_script.py')
{'changed': True, 'rc': 0, 'stderr': 'Shared connection to x.x.x.x closed.\r\n', 'stderr_lines': ['Shared connection to x.x.x.x closed.'], 'stdout': '{\r\n    "manufacturers": [\r\n        "Acura",\r\n        "Alfa-Romeo",\r\n        "Aston-Martin",\r\n        "Audi",\r\n        "Bentley",\r\n        "BMW",\r\n        "Bugatti",\r\n        "Buick",\r\n        "Cadillac",\r\n        "Chevrolet",\r\n        "Chrysler",\r\n        "Citroen",\r\n        "Deus Automobiles",\r\n        "Dodge",\r\n        "Ferrari",\r\n        "Fiat",\r\n        "Ford",\r\n        "Geely",\r\n        "Genesis",\r\n        "GMC",\r\n        "Honda",\r\n        "Hyundai",\r\n        "Infiniti",\r\n        "Jaguar",\r\n        "Jeep",\r\n        "Kia",\r\n        "Koenigsegg",\r\n        "Lamborghini",\r\n        "Lancia",\r\n        "Land Rover",\r\n        "Lexus",\r\n        "Lincoln",\r\n        "Lotus",\r\n        "Maserati",\r\n        "Maybach",\r\n        "Mazda",\r\n        "McLaren",\r\n        "Mercedes",\r\n        "Mini",\r\n        "Mitsubishi",\r\n        "Nissan",\r\n        "Opel",\r\n        "Pagani",\r\n        "Peugeot",\r\n        "Pontiac",\r\n        "Porsche",\r\n        "Ram",\r\n        "Renault",\r\n        "Rolls-Royce",\r\n        "Skoda",\r\n        "Smart",\r\n        "Subaru",\r\n        "Suzuki",\r\n        "Tesla",\r\n        "Toyota",\r\n        "Volkswagen",\r\n        "Volvo"\r\n    ]\r\n}\r\n', 'stdout_lines': ['{', '    "manufacturers": [', '        "Acura",', '        "Alfa-Romeo",', '        "Aston-Martin",', '        "Audi",', '        "Bentley",', '        "BMW",', '        "Bugatti",', '        "Buick",', '        "Cadillac",', '        "Chevrolet",', '        "Chrysler",', '        "Citroen",', '        "Deus Automobiles",', '        "Dodge",', '        "Ferrari",', '        "Fiat",', '        "Ford",', '        "Geely",', '        "Genesis",', '        "GMC",', '        "Honda",', '        "Hyundai",', '        "Infiniti",', '        "Jaguar",', '        "Jeep",', '        "Kia",', '        "Koenigsegg",', '        "Lamborghini",', '        "Lancia",', '        "Land Rover",', '        "Lexus",', '        "Lincoln",', '        "Lotus",', '        "Maserati",', '        "Maybach",', '        "Mazda",', '        "McLaren",', '        "Mercedes",', '        "Mini",', '        "Mitsubishi",', '        "Nissan",', '        "Opel",', '        "Pagani",', '        "Peugeot",', '        "Pontiac",', '        "Porsche",', '        "Ram",', '        "Renault",', '        "Rolls-Royce",', '        "Skoda",', '        "Smart",', '        "Subaru",', '        "Suzuki",', '        "Tesla",', '        "Toyota",', '        "Volkswagen",', '        "Volvo"', '    ]', '}']}
>>> 
pro-akim commented 1 year ago

04/07/2023

Wazuh_QA_environment Pipelines are not working. It is not being able to run unit tests as expected.

pro-akim commented 1 year ago

06/07/2023

Running: hm.run_python_script('manager1', '/home/akim/Desktop/query_wdb2.py')

where query_wdb2.py content is: print("testing script")

Result is:

Traceback (most recent call last):
  File "/home/akim/Desktop/trash/test2.py", line 40, in <module>
    hm.run_python_script('manager1', '/home/akim/Desktop/query_wdb2.py')
  File "/home/akim/Desktop/venvs/qa-system-env/lib/python3.10/site-packages/wazuh_qa_framework/system/host_manager.py", line 730, in run_python_script
    raise Exception(f"Error running python script {script} on host {host}: {result}")
Exception: Error running python script /home/akim/Desktop/query_wdb2.py on host manager1: {'changed': True, 'msg': 'non-zero return code', 'rc': 2, 'stderr': 'Shared connection to 172.31.12.21 closed.\r\n', 'stderr_lines': ['Shared connection to 172.31.12.21 closed.'], 'stdout': '/home/qa/.ansible/tmp/ansible-tmp-1688636039.8908288-1528037-268069442189855/query_wdb2.py: line 1: syntax error near unexpected token `"testing script"\'\r\n/home/qa/.ansible/tmp/ansible-tmp-1688636039.8908288-1528037-268069442189855/query_wdb2.py: line 1: `print("testing script")\'\r\n', 'stdout_lines': ['/home/qa/.ansible/tmp/ansible-tmp-1688636039.8908288-1528037-268069442189855/query_wdb2.py: line 1: syntax error near unexpected token `"testing script"\'', '/home/qa/.ansible/tmp/ansible-tmp-1688636039.8908288-1528037-268069442189855/query_wdb2.py: line 1: `print("testing script")\'']}

Same response using different files and hosts

BelenValdivia commented 1 year ago

I ran the function on different hosts using a script with the same content as @pro-akim and it works.

Script: image

Results:

>>> hm.run_python_script('agent1', '/home/belen/Repositories/qa-system-framework/src/wazuh_qa_framework/system/python_script.py')
{'changed': True, 'rc': 0, 'stderr': 'Shared connection to 172.31.8.115 closed.\r\n', 'stderr_lines': ['Shared connection to 172.31.8.115 closed.'], 'stdout': 'testing script\r\n', 'stdout_lines': ['testing script']}
>>>
>>> hm.run_python_script('manager1', '/home/belen/Repositories/qa-system-framework/src/wazuh_qa_framework/system/python_script.py')
{'changed': True, 'rc': 0, 'stderr': 'Shared connection to 172.31.7.53 closed.\r\n', 'stderr_lines': ['Shared connection to 172.31.7.53 closed.'], 'stdout': 'testing script\r\n', 'stdout_lines': ['testing script']

Also, I've tested it with the script I tested earlier and it works. Script:

#!/usr/bin/env python3
import json

print("testing")
cars = {
    "manufacturers": [
        "Acura", "Alfa-Romeo", "Aston-Martin", "Audi", "Bentley", "BMW",
        "Bugatti", "Buick", "Cadillac", "Chevrolet", "Chrysler", "Citroen",
        "Deus Automobiles", "Dodge", "Ferrari", "Fiat", "Ford", "Geely",
        "Genesis", "GMC", "Honda", "Hyundai", "Infiniti", "Jaguar", "Jeep",
        "Kia", "Koenigsegg", "Lamborghini", "Lancia", "Land Rover", "Lexus",
        "Lincoln", "Lotus", "Maserati", "Maybach", "Mazda", "McLaren", "Mercedes",
        "Mini", "Mitsubishi", "Nissan", "Opel", "Pagani", "Peugeot", "Pontiac",
        "Porsche", "Ram", "Renault", "Rolls-Royce", "Skoda", "Smart", "Subaru",
        "Suzuki", "Tesla", "Toyota", "Volkswagen", "Volvo"
    ]
}
print(json.dumps(cars, indent=4))

Result:

>>> hm.run_python_script('manager1', '/home/belen/Repositories/qa-system-framework/src/wazuh_qa_framework/system/python_script.py')
{'changed': True, 'rc': 0, 'stderr': 'Shared connection to 172.31.7.53 closed.\r\n', 'stderr_lines': ['Shared connection to 172.31.7.53 closed.'], 'stdout': 'testing\r\n{\r\n    "manufacturers": [\r\n        "Acura",\r\n        "Alfa-Romeo",\r\n        "Aston-Martin",\r\n        "Audi",\r\n        "Bentley",\r\n        "BMW",\r\n        "Bugatti",\r\n        "Buick",\r\n        "Cadillac",\r\n        "Chevrolet",\r\n        "Chrysler",\r\n        "Citroen",\r\n        "Deus Automobiles",\r\n        "Dodge",\r\n        "Ferrari",\r\n        "Fiat",\r\n        "Ford",\r\n        "Geely",\r\n        "Genesis",\r\n        "GMC",\r\n        "Honda",\r\n        "Hyundai",\r\n        "Infiniti",\r\n        "Jaguar",\r\n        "Jeep",\r\n        "Kia",\r\n        "Koenigsegg",\r\n        "Lamborghini",\r\n        "Lancia",\r\n        "Land Rover",\r\n        "Lexus",\r\n        "Lincoln",\r\n        "Lotus",\r\n        "Maserati",\r\n        "Maybach",\r\n        "Mazda",\r\n        "McLaren",\r\n        "Mercedes",\r\n        "Mini",\r\n        "Mitsubishi",\r\n        "Nissan",\r\n        "Opel",\r\n        "Pagani",\r\n        "Peugeot",\r\n        "Pontiac",\r\n        "Porsche",\r\n        "Ram",\r\n        "Renault",\r\n        "Rolls-Royce",\r\n        "Skoda",\r\n        "Smart",\r\n        "Subaru",\r\n        "Suzuki",\r\n        "Tesla",\r\n        "Toyota",\r\n        "Volkswagen",\r\n        "Volvo"\r\n    ]\r\n}\r\n', 'stdout_lines': ['testing', '{', '    "manufacturers": [', '        "Acura",', '        "Alfa-Romeo",', '        "Aston-Martin",', '        "Audi",', '        "Bentley",', '        "BMW",', '        "Bugatti",', '        "Buick",', '        "Cadillac",', '        "Chevrolet",', '        "Chrysler",', '        "Citroen",', '        "Deus Automobiles",', '        "Dodge",', '        "Ferrari",', '        "Fiat",', '        "Ford",', '        "Geely",', '        "Genesis",', '        "GMC",', '        "Honda",', '        "Hyundai",', '        "Infiniti",', '        "Jaguar",', '        "Jeep",', '        "Kia",', '        "Koenigsegg",', '        "Lamborghini",', '        "Lancia",', '        "Land Rover",', '        "Lexus",', '        "Lincoln",', '        "Lotus",', '        "Maserati",', '        "Maybach",', '        "Mazda",', '        "McLaren",', '        "Mercedes",', '        "Mini",', '        "Mitsubishi",', '        "Nissan",', '        "Opel",', '        "Pagani",', '        "Peugeot",', '        "Pontiac",', '        "Porsche",', '        "Ram",', '        "Renault",', '        "Rolls-Royce",', '        "Skoda",', '        "Smart",', '        "Subaru",', '        "Suzuki",', '        "Tesla",', '        "Toyota",', '        "Volkswagen",', '        "Volvo"', '    ]', '}']}
>>>

All checks have passed, and no errors as mentioned in the comment above

image

pro-akim commented 1 year ago

Update

Tested in a different virtual environment and it worked.