Update hooks functionality

This commit is contained in:
Federico Luzzi 2024-04-22 18:17:11 -03:00
parent c6a31cb710
commit 87bb6302ff
10 changed files with 338 additions and 136 deletions

107
README.md
View File

@ -111,17 +111,104 @@ options:
- Pass statements - Pass statements
### Specific Class Requirements ### Specific Class Requirements
- The plugin script must define at least two specific classes: - The plugin script must define specific classes with particular attributes and methods. Each class serves a distinct role within the plugin's architecture:
1. **Class `Parser`**: 1. **Class `Parser`**:
- Must contain only one method: `__init__`. - **Purpose**: Handles parsing of command-line arguments.
- The `__init__` method must initialize at least two attributes: - **Requirements**:
- `self.parser`: An instance of `argparse.ArgumentParser`. - Must contain only one method: `__init__`.
- `self.description`: A string containing the description of the parser. - The `__init__` method must initialize at least two attributes:
- `self.parser`: An instance of `argparse.ArgumentParser`.
- `self.description`: A string containing the description of the parser.
2. **Class `Entrypoint`**: 2. **Class `Entrypoint`**:
- Must have an `__init__` method that accepts exactly three parameters besides `self`: - **Purpose**: Acts as the entry point for plugin execution, utilizing parsed arguments and integrating with the main application.
- `args`: Arguments passed to the plugin. - **Requirements**:
- The parser instance (typically `self.parser` from the `Parser` class). - Must have an `__init__` method that accepts exactly three parameters besides `self`:
- The Connapp instance to interact with the Connpy app. - `args`: Arguments passed to the plugin.
- The parser instance (typically `self.parser` from the `Parser` class).
- The Connapp instance to interact with the Connpy app.
3. **Class `Preload`**:
- **Purpose**: Performs any necessary preliminary setup or configuration independent of the main parsing and entry logic.
- **Requirements**:
- Contains at least an `__init__` method that accepts parameter connapp besides `self`.
### Class Dependencies and Combinations
- **Dependencies**:
- `Parser` and `Entrypoint` are interdependent and must both be present if one is included.
- `Preload` is independent and may exist alone or alongside the other classes.
- **Valid Combinations**:
- `Parser` and `Entrypoint` together.
- `Preload` alone.
- All three classes (`Parser`, `Entrypoint`, `Preload`).
### Preload Modifications and Hooks
In the `Preload` class of the plugin system, you have the ability to customize the behavior of existing classes and methods within the application through a robust hooking system. This documentation explains how to use the `modify`, `register_pre_hook`, and `register_post_hook` methods to tailor plugin functionality to your needs.
#### Modifying Classes with `modify`
The `modify` method allows you to alter instances of a class at the time they are created or after their creation. This is particularly useful for setting or modifying configuration settings, altering default behaviors, or adding new functionalities to existing classes without changing the original class definitions.
- **Usage**: Modify a class to include additional configurations or changes
- **Modify Method Signature**:
- `modify(modification_method)`: A function that is invoked with an instance of the class as its argument. This function should perform any modifications directly on this instance.
- **Modification Method Signature**:
- **Arguments**:
- `cls`: This function accepts a single argument, the class instance, which it then modifies.
- **Modifiable Classes**:
- `connapp.config`
- `connapp.node`
- `connapp.nodes`
- `connapp.ai`
- ```python
def modify_config(cls):
# Example modification: adding a new attribute or modifying an existing one
cls.new_attribute = 'New Value'
class Preload:
def __init__(self, connapp):
# Applying modification to the config class instance
connapp.config.modify(modify_config)
```
#### Implementing Hooks with `register_pre_hook` and `register_post_hook`
These methods allow you to define custom logic to be executed before (`register_pre_hook`) or after (`register_post_hook`) the main logic of a method. This is particularly useful for logging, auditing, preprocessing inputs, postprocessing outputs or adding functionalities.
- **Usage**: Register hooks to methods to execute additional logic before or after the main method execution.
- **Registration Methods Signature**:
- `register_pre_hook(pre_hook_method)`: A function that is invoked before the main method is executed. This function should do preprocessing of the arguments.
- `register_post_hook(post_hook_method)`: A function that is invoked after the main method is executed. This function should do postprocessing of the outputs.
- **Method Signatures for Pre-Hooks**
- `pre_hook_method(*args, **kwargs)`
- **Arguments**:
- `*args`, `**kwargs`: The arguments and keyword arguments that will be passed to the method being hooked. The pre-hook function has the opportunity to inspect and modify these arguments before they are passed to the main method.
- **Return**:
- Must return a tuple `(args, kwargs)`, which will be used as the new arguments for the main method. If the original arguments are not modified, the function should return them as received.
- **Method Signatures for Post-Hooks**:
- `post_hook_method(*args, **kwargs)`
- **Arguments**:
- `*args`, `**kwargs`: The arguments and keyword arguments that were passed to the main method.
- `kwargs["result"]`: The value returned by the main method. This allows the post-hook to inspect and even alter the result before it is returned to the original caller.
- **Return**:
- Can return a modified result, which will replace the original result of the main method, or simply return `kwargs["result"]` to return the original method result.
- ```python
def pre_processing_hook(*args, **kwargs):
print("Pre-processing logic here")
# Modify arguments or perform any checks
return args, kwargs # Return modified or unmodified args and kwargs
def post_processing_hook(*args, **kwargs):
print("Post-processing logic here")
# Modify the result or perform any final logging or cleanup
return kwargs["result"] # Return the modified or unmodified result
class Preload:
def __init__(self, connapp):
# Registering a pre-hook
connapp.ai.some_method.register_pre_hook(pre_processing_hook)
# Registering a post-hook
connapp.node.another_method.register_post_hook(post_processing_hook)
```
### Executable Block ### Executable Block
- The plugin script can include an executable block: - The plugin script can include an executable block:
@ -131,7 +218,7 @@ options:
### Script Verification ### Script Verification
- The `verify_script` method in `plugins.py` is used to check the plugin script's compliance with these standards. - The `verify_script` method in `plugins.py` is used to check the plugin script's compliance with these standards.
- Non-compliant scripts will be rejected to ensure consistency and proper functionality within the plugin system. - Non-compliant scripts will be rejected to ensure consistency and proper functionality within the plugin system.
-
### Example Script ### Example Script
For a practical example of how to write a compatible plugin script, please refer to the following example: For a practical example of how to write a compatible plugin script, please refer to the following example:

View File

@ -91,17 +91,103 @@ options:
- Pass statements - Pass statements
### Specific Class Requirements ### Specific Class Requirements
- The plugin script must define at least two specific classes: - The plugin script must define specific classes with particular attributes and methods. Each class serves a distinct role within the plugin's architecture:
1. **Class `Parser`**: 1. **Class `Parser`**:
- Must contain only one method: `__init__`. - **Purpose**: Handles parsing of command-line arguments.
- The `__init__` method must initialize at least two attributes: - **Requirements**:
- `self.parser`: An instance of `argparse.ArgumentParser`. - Must contain only one method: `__init__`.
- `self.description`: A string containing the description of the parser. - The `__init__` method must initialize at least two attributes:
- `self.parser`: An instance of `argparse.ArgumentParser`.
- `self.description`: A string containing the description of the parser.
2. **Class `Entrypoint`**: 2. **Class `Entrypoint`**:
- Must have an `__init__` method that accepts exactly three parameters besides `self`: - **Purpose**: Acts as the entry point for plugin execution, utilizing parsed arguments and integrating with the main application.
- `args`: Arguments passed to the plugin. - **Requirements**:
- The parser instance (typically `self.parser` from the `Parser` class). - Must have an `__init__` method that accepts exactly three parameters besides `self`:
- The Connapp instance to interact with the Connpy app. - `args`: Arguments passed to the plugin.
- The parser instance (typically `self.parser` from the `Parser` class).
- The Connapp instance to interact with the Connpy app.
3. **Class `Preload`**:
- **Purpose**: Performs any necessary preliminary setup or configuration independent of the main parsing and entry logic.
- **Requirements**:
- Contains at least an `__init__` method that accepts parameter connapp besides `self`.
### Class Dependencies and Combinations
- **Dependencies**:
- `Parser` and `Entrypoint` are interdependent and must both be present if one is included.
- `Preload` is independent and may exist alone or alongside the other classes.
- **Valid Combinations**:
- `Parser` and `Entrypoint` together.
- `Preload` alone.
- All three classes (`Parser`, `Entrypoint`, `Preload`).
### Preload Modifications and Hooks
In the `Preload` class of the plugin system, you have the ability to customize the behavior of existing classes and methods within the application through a robust hooking system. This documentation explains how to use the `modify`, `register_pre_hook`, and `register_post_hook` methods to tailor plugin functionality to your needs.
#### Modifying Classes with `modify`
The `modify` method allows you to alter instances of a class at the time they are created or after their creation. This is particularly useful for setting or modifying configuration settings, altering default behaviors, or adding new functionalities to existing classes without changing the original class definitions.
- **Usage**: Modify a class to include additional configurations or changes
- **Modify Method Signature**:
- `modify(modification_method)`: A function that is invoked with an instance of the class as its argument. This function should perform any modifications directly on this instance.
- **Modification Method Signature**:
- **Arguments**:
- `cls`: This function accepts a single argument, the class instance, which it then modifies.
- **Modifiable Classes**:
- `connapp.config`
- `connapp.node`
- `connapp.nodes`
- `connapp.ai`
- ```python
def modify_config(cls):
# Example modification: adding a new attribute or modifying an existing one
cls.new_attribute = 'New Value'
class Preload:
def __init__(self, connapp):
# Applying modification to the config class instance
connapp.config.modify(modify_config)
```
#### Implementing Hooks with `register_pre_hook` and `register_post_hook`
These methods allow you to define custom logic to be executed before (`register_pre_hook`) or after (`register_post_hook`) the main logic of a method. This is particularly useful for logging, auditing, preprocessing inputs, postprocessing outputs or adding functionalities.
- **Usage**: Register hooks to methods to execute additional logic before or after the main method execution.
- **Registration Methods Signature**:
- `register_pre_hook(pre_hook_method)`: A function that is invoked before the main method is executed. This function should do preprocessing of the arguments.
- `register_post_hook(post_hook_method)`: A function that is invoked after the main method is executed. This function should do postprocessing of the outputs.
- **Method Signatures for Pre-Hooks**
- `pre_hook_method(*args, **kwargs)`
- **Arguments**:
- `*args`, `**kwargs`: The arguments and keyword arguments that will be passed to the method being hooked. The pre-hook function has the opportunity to inspect and modify these arguments before they are passed to the main method.
- **Return**:
- Must return a tuple `(args, kwargs)`, which will be used as the new arguments for the main method. If the original arguments are not modified, the function should return them as received.
- **Method Signatures for Post-Hooks**:
- `post_hook_method(*args, **kwargs)`
- **Arguments**:
- `*args`, `**kwargs`: The arguments and keyword arguments that were passed to the main method.
- `kwargs["result"]`: The value returned by the main method. This allows the post-hook to inspect and even alter the result before it is returned to the original caller.
- **Return**:
- Can return a modified result, which will replace the original result of the main method, or simply return `kwargs["result"]` to return the original method result.
- ```python
def pre_processing_hook(*args, **kwargs):
print("Pre-processing logic here")
# Modify arguments or perform any checks
return args, kwargs # Return modified or unmodified args and kwargs
def post_processing_hook(*args, **kwargs):
print("Post-processing logic here")
# Modify the result or perform any final logging or cleanup
return kwargs["result"] # Return the modified or unmodified result
class Preload:
def __init__(self, connapp):
# Registering a pre-hook
connapp.ai.some_method.register_pre_hook(pre_processing_hook)
# Registering a post-hook
connapp.node.another_method.register_post_hook(post_processing_hook)
```
### Executable Block ### Executable Block
- The plugin script can include an executable block: - The plugin script can include an executable block:

View File

@ -1,2 +1,2 @@
__version__ = "4.0.0b2" __version__ = "4.0.0b5"

View File

@ -6,7 +6,9 @@ import ast
from textwrap import dedent from textwrap import dedent
from .core import nodes from .core import nodes
from copy import deepcopy from copy import deepcopy
from .hooks import ClassHook,MethodHook
@ClassHook
class ai: class ai:
''' This class generates a ai object. Containts all the information and methods to make requests to openAI chatGPT to run actions on the application. ''' This class generates a ai object. Containts all the information and methods to make requests to openAI chatGPT to run actions on the application.
@ -175,6 +177,7 @@ Categorize the user's request based on the operation they want to perform on the
self.__prompt["confirmation_function"]["parameters"]["properties"]["response"]["type"] = "string" self.__prompt["confirmation_function"]["parameters"]["properties"]["response"]["type"] = "string"
self.__prompt["confirmation_function"]["parameters"]["required"] = ["result"] self.__prompt["confirmation_function"]["parameters"]["required"] = ["result"]
@MethodHook
def process_string(self, s): def process_string(self, s):
if s.startswith('[') and s.endswith(']') and not (s.startswith("['") and s.endswith("']")) and not (s.startswith('["') and s.endswith('"]')): if s.startswith('[') and s.endswith(']') and not (s.startswith("['") and s.endswith("']")) and not (s.startswith('["') and s.endswith('"]')):
# Extract the content inside square brackets and split by comma # Extract the content inside square brackets and split by comma
@ -185,6 +188,7 @@ Categorize the user's request based on the operation they want to perform on the
s = '[' + new_content + ']' s = '[' + new_content + ']'
return s return s
@MethodHook
def _retry_function(self, function, max_retries, backoff_num, *args): def _retry_function(self, function, max_retries, backoff_num, *args):
#Retry openai requests #Retry openai requests
retries = 0 retries = 0
@ -201,6 +205,7 @@ Categorize the user's request based on the operation they want to perform on the
myfunction = False myfunction = False
return myfunction return myfunction
@MethodHook
def _clean_command_response(self, raw_response, node_list): def _clean_command_response(self, raw_response, node_list):
#Parse response for command request to openAI GPT. #Parse response for command request to openAI GPT.
info_dict = {} info_dict = {}
@ -218,6 +223,7 @@ Categorize the user's request based on the operation they want to perform on the
info_dict["variables"][key] = newvalue info_dict["variables"][key] = newvalue
return info_dict return info_dict
@MethodHook
def _get_commands(self, user_input, nodes): def _get_commands(self, user_input, nodes):
#Send the request for commands for each device to openAI GPT. #Send the request for commands for each device to openAI GPT.
output_list = [] output_list = []
@ -257,6 +263,7 @@ Categorize the user's request based on the operation they want to perform on the
output["response"] = self._clean_command_response(json_result, node_list) output["response"] = self._clean_command_response(json_result, node_list)
return output return output
@MethodHook
def _get_filter(self, user_input, chat_history = None): def _get_filter(self, user_input, chat_history = None):
#Send the request to identify the filter and other attributes from the user input to GPT. #Send the request to identify the filter and other attributes from the user input to GPT.
message = [] message = []
@ -298,6 +305,7 @@ Categorize the user's request based on the operation they want to perform on the
output["chat_history"] = chat_history output["chat_history"] = chat_history
return output return output
@MethodHook
def _get_confirmation(self, user_input): def _get_confirmation(self, user_input):
#Send the request to identify if user is confirming or denying the task #Send the request to identify if user is confirming or denying the task
message = [] message = []
@ -322,6 +330,7 @@ Categorize the user's request based on the operation they want to perform on the
output["result"] = json_result["response"] output["result"] = json_result["response"]
return output return output
@MethodHook
def confirm(self, user_input, max_retries=3, backoff_num=1): def confirm(self, user_input, max_retries=3, backoff_num=1):
''' '''
Send the user input to openAI GPT and verify if response is afirmative or negative. Send the user input to openAI GPT and verify if response is afirmative or negative.
@ -347,6 +356,7 @@ Categorize the user's request based on the operation they want to perform on the
output = f"{self.model} api is not responding right now, please try again later." output = f"{self.model} api is not responding right now, please try again later."
return output return output
@MethodHook
def ask(self, user_input, dryrun = False, chat_history = None, max_retries=3, backoff_num=1): def ask(self, user_input, dryrun = False, chat_history = None, max_retries=3, backoff_num=1):
''' '''
Send the user input to openAI GPT and parse the response to run an action in the application. Send the user input to openAI GPT and parse the response to run an action in the application.

View File

@ -1,5 +1,5 @@
from flask import Flask, request, jsonify from flask import Flask, request, jsonify
from connpy import configfile, node, nodes from connpy import configfile, node, nodes, hooks
from connpy.ai import ai as myai from connpy.ai import ai as myai
from waitress import serve from waitress import serve
import os import os
@ -126,6 +126,7 @@ def run_commands():
return({"DataError": error}) return({"DataError": error})
return output return output
@hooks.MethodHook
def stop_api(): def stop_api():
# Read the process ID (pid) from the file # Read the process ID (pid) from the file
try: try:
@ -152,14 +153,17 @@ def stop_api():
print(f"Server with process ID {pid} stopped.") print(f"Server with process ID {pid} stopped.")
return port return port
@hooks.MethodHook
def debug_api(port=8048): def debug_api(port=8048):
app.custom_config = configfile() app.custom_config = configfile()
app.run(debug=True, port=port) app.run(debug=True, port=port)
@hooks.MethodHook
def start_server(port=8048): def start_server(port=8048):
app.custom_config = configfile() app.custom_config = configfile()
serve(app, host='0.0.0.0', port=port) serve(app, host='0.0.0.0', port=port)
@hooks.MethodHook
def start_api(port=8048): def start_api(port=8048):
if os.path.exists(PID_FILE1) or os.path.exists(PID_FILE2): if os.path.exists(PID_FILE1) or os.path.exists(PID_FILE2):
print("Connpy server is already running.") print("Connpy server is already running.")

View File

@ -6,12 +6,13 @@ import re
from Crypto.PublicKey import RSA from Crypto.PublicKey import RSA
from pathlib import Path from pathlib import Path
from copy import deepcopy from copy import deepcopy
from .hooks import ConfigHook from .hooks import MethodHook, ClassHook
#functions and classes #functions and classes
@ClassHook
class configfile: class configfile:
''' This class generates a configfile object. Containts a dictionary storing, config, nodes and profiles, normaly used by connection manager. ''' This class generates a configfile object. Containts a dictionary storing, config, nodes and profiles, normaly used by connection manager.
@ -107,7 +108,7 @@ class configfile:
jsonconf.close() jsonconf.close()
return jsondata return jsondata
@ConfigHook @MethodHook
def _saveconfig(self, conf): def _saveconfig(self, conf):
#Save config file #Save config file
newconfig = {"config":{}, "connections": {}, "profiles": {}} newconfig = {"config":{}, "connections": {}, "profiles": {}}
@ -131,6 +132,7 @@ class configfile:
os.chmod(keyfile, 0o600) os.chmod(keyfile, 0o600)
return key return key
@MethodHook
def _explode_unique(self, unique): def _explode_unique(self, unique):
#Divide unique name into folder, subfolder and id #Divide unique name into folder, subfolder and id
uniques = unique.split("@") uniques = unique.split("@")
@ -151,6 +153,7 @@ class configfile:
return False return False
return result return result
@MethodHook
def getitem(self, unique, keys = None): def getitem(self, unique, keys = None):
''' '''
Get an node or a group of nodes from configfile which can be passed to node/nodes class Get an node or a group of nodes from configfile which can be passed to node/nodes class
@ -206,6 +209,7 @@ class configfile:
newnode.pop("type") newnode.pop("type")
return newnode return newnode
@MethodHook
def getitems(self, uniques): def getitems(self, uniques):
''' '''
Get a group of nodes from configfile which can be passed to node/nodes class Get a group of nodes from configfile which can be passed to node/nodes class
@ -247,6 +251,7 @@ class configfile:
return nodes return nodes
@MethodHook
def _connections_add(self,*, id, host, folder='', subfolder='', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='', type = "connection" ): def _connections_add(self,*, id, host, folder='', subfolder='', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='', type = "connection" ):
#Add connection from config #Add connection from config
if folder == '': if folder == '':
@ -257,6 +262,7 @@ class configfile:
self.connections[folder][subfolder][id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost, "type": type} self.connections[folder][subfolder][id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost, "type": type}
@MethodHook
def _connections_del(self,*, id, folder='', subfolder=''): def _connections_del(self,*, id, folder='', subfolder=''):
#Delete connection from config #Delete connection from config
if folder == '': if folder == '':
@ -266,6 +272,7 @@ class configfile:
elif folder != '' and subfolder != '': elif folder != '' and subfolder != '':
del self.connections[folder][subfolder][id] del self.connections[folder][subfolder][id]
@MethodHook
def _folder_add(self,*, folder, subfolder = ''): def _folder_add(self,*, folder, subfolder = ''):
#Add Folder from config #Add Folder from config
if subfolder == '': if subfolder == '':
@ -275,6 +282,7 @@ class configfile:
if subfolder not in self.connections[folder]: if subfolder not in self.connections[folder]:
self.connections[folder][subfolder] = {"type": "subfolder"} self.connections[folder][subfolder] = {"type": "subfolder"}
@MethodHook
def _folder_del(self,*, folder, subfolder=''): def _folder_del(self,*, folder, subfolder=''):
#Delete folder from config #Delete folder from config
if subfolder == '': if subfolder == '':
@ -283,15 +291,18 @@ class configfile:
del self.connections[folder][subfolder] del self.connections[folder][subfolder]
@MethodHook
def _profiles_add(self,*, id, host = '', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='' ): def _profiles_add(self,*, id, host = '', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='' ):
#Add profile from config #Add profile from config
self.profiles[id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost} self.profiles[id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost}
@MethodHook
def _profiles_del(self,*, id ): def _profiles_del(self,*, id ):
#Delete profile from config #Delete profile from config
del self.profiles[id] del self.profiles[id]
@MethodHook
def _getallnodes(self, filter = None): def _getallnodes(self, filter = None):
#get all nodes on configfile #get all nodes on configfile
nodes = [] nodes = []
@ -314,6 +325,7 @@ class configfile:
raise ValueError("filter must be a string or a list of strings") raise ValueError("filter must be a string or a list of strings")
return nodes return nodes
@MethodHook
def _getallnodesfull(self, filter = None, extract = True): def _getallnodesfull(self, filter = None, extract = True):
#get all nodes on configfile with all their attributes. #get all nodes on configfile with all their attributes.
nodes = {} nodes = {}
@ -353,6 +365,7 @@ class configfile:
return nodes return nodes
@MethodHook
def _getallfolders(self): def _getallfolders(self):
#get all folders on configfile #get all folders on configfile
folders = ["@" + k for k,v in self.connections.items() if isinstance(v, dict) and v["type"] == "folder"] folders = ["@" + k for k,v in self.connections.items() if isinstance(v, dict) and v["type"] == "folder"]
@ -363,6 +376,7 @@ class configfile:
folders.extend(subfolders) folders.extend(subfolders)
return folders return folders
@MethodHook
def _profileused(self, profile): def _profileused(self, profile):
#Check if profile is used before deleting it #Check if profile is used before deleting it
nodes = [] nodes = []

View File

@ -45,9 +45,13 @@ class connapp:
''' '''
self.node = node self.node = node
self.connnodes = nodes self.nodes = nodes
self.start_api = start_api
self.stop_api = stop_api
self.debug_api = debug_api
self.ai = ai
self.config = config self.config = config
self.nodes = self.config._getallnodes() self.nodes_list = self.config._getallnodes()
self.folders = self.config._getallfolders() self.folders = self.config._getallfolders()
self.profiles = list(self.config.profiles.keys()) self.profiles = list(self.config.profiles.keys())
self.case = self.config.config["case"] self.case = self.config.config["case"]
@ -211,16 +215,16 @@ class connapp:
def _connect(self, args): def _connect(self, args):
if args.data == None: if args.data == None:
matches = self.nodes matches = self.nodes_list
if len(matches) == 0: if len(matches) == 0:
print("There are no nodes created") print("There are no nodes created")
print("try: conn --help") print("try: conn --help")
exit(9) exit(9)
else: else:
if args.data.startswith("@"): if args.data.startswith("@"):
matches = list(filter(lambda k: args.data in k, self.nodes)) matches = list(filter(lambda k: args.data in k, self.nodes_list))
else: else:
matches = list(filter(lambda k: k.startswith(args.data), self.nodes)) matches = list(filter(lambda k: k.startswith(args.data), self.nodes_list))
if len(matches) == 0: if len(matches) == 0:
print("{} not found".format(args.data)) print("{} not found".format(args.data))
exit(2) exit(2)
@ -275,10 +279,10 @@ class connapp:
elif args.data.startswith("@"): elif args.data.startswith("@"):
type = "folder" type = "folder"
matches = list(filter(lambda k: k == args.data, self.folders)) matches = list(filter(lambda k: k == args.data, self.folders))
reversematches = list(filter(lambda k: "@" + k == args.data, self.nodes)) reversematches = list(filter(lambda k: "@" + k == args.data, self.nodes_list))
else: else:
type = "node" type = "node"
matches = list(filter(lambda k: k == args.data, self.nodes)) matches = list(filter(lambda k: k == args.data, self.nodes_list))
reversematches = list(filter(lambda k: k == "@" + args.data, self.folders)) reversematches = list(filter(lambda k: k == "@" + args.data, self.folders))
if len(matches) > 0: if len(matches) > 0:
print("{} already exist".format(matches[0])) print("{} already exist".format(matches[0]))
@ -326,7 +330,7 @@ class connapp:
if args.data == None: if args.data == None:
print("Missing argument node") print("Missing argument node")
exit(3) exit(3)
matches = list(filter(lambda k: k == args.data, self.nodes)) matches = list(filter(lambda k: k == args.data, self.nodes_list))
if len(matches) == 0: if len(matches) == 0:
print("{} not found".format(args.data)) print("{} not found".format(args.data))
exit(2) exit(2)
@ -511,8 +515,8 @@ class connapp:
if not self.case: if not self.case:
args.data[0] = args.data[0].lower() args.data[0] = args.data[0].lower()
args.data[1] = args.data[1].lower() args.data[1] = args.data[1].lower()
source = list(filter(lambda k: k == args.data[0], self.nodes)) source = list(filter(lambda k: k == args.data[0], self.nodes_list))
dest = list(filter(lambda k: k == args.data[1], self.nodes)) dest = list(filter(lambda k: k == args.data[1], self.nodes_list))
if len(source) != 1: if len(source) != 1:
print("{} not found".format(args.data[0])) print("{} not found".format(args.data[0]))
exit(2) exit(2)
@ -550,7 +554,7 @@ class connapp:
count = 0 count = 0
for n in ids: for n in ids:
unique = n + newnodes["location"] unique = n + newnodes["location"]
matches = list(filter(lambda k: k == unique, self.nodes)) matches = list(filter(lambda k: k == unique, self.nodes_list))
reversematches = list(filter(lambda k: k == "@" + unique, self.folders)) reversematches = list(filter(lambda k: k == "@" + unique, self.folders))
if len(matches) > 0: if len(matches) > 0:
print("Node {} already exist, ignoring it".format(unique)) print("Node {} already exist, ignoring it".format(unique))
@ -577,7 +581,7 @@ class connapp:
newnode["password"] = newnodes["password"] newnode["password"] = newnodes["password"]
count +=1 count +=1
self.config._connections_add(**newnode) self.config._connections_add(**newnode)
self.nodes = self.config._getallnodes() self.nodes_list = self.config._getallnodes()
if count > 0: if count > 0:
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
print("Succesfully added {} nodes".format(count)) print("Succesfully added {} nodes".format(count))
@ -829,7 +833,7 @@ class connapp:
arguments["org"] = args.org[0] arguments["org"] = args.org[0]
if args.api_key: if args.api_key:
arguments["api_key"] = args.api_key[0] arguments["api_key"] = args.api_key[0]
self.myai = ai(self.config, **arguments) self.myai = self.ai(self.config, **arguments)
if args.ask: if args.ask:
input = " ".join(args.ask) input = " ".join(args.ask)
request = self.myai.ask(input, dryrun = True) request = self.myai.ask(input, dryrun = True)
@ -920,7 +924,7 @@ class connapp:
if not confirmation: if not confirmation:
response = "Request cancelled" response = "Request cancelled"
else: else:
nodes = self.connnodes(self.config.getitems(response["nodes"]), config = self.config) nodes = self.nodes(self.config.getitems(response["nodes"]), config = self.config)
if response["action"] == "run": if response["action"] == "run":
output = nodes.run(**response["args"]) output = nodes.run(**response["args"])
response = "" response = ""
@ -936,17 +940,17 @@ class connapp:
def _func_api(self, args): def _func_api(self, args):
if args.command == "stop" or args.command == "restart": if args.command == "stop" or args.command == "restart":
args.data = stop_api() args.data = self.stop_api()
if args.command == "start" or args.command == "restart": if args.command == "start" or args.command == "restart":
if args.data: if args.data:
start_api(args.data) self.start_api(args.data)
else: else:
start_api() self.start_api()
if args.command == "debug": if args.command == "debug":
if args.data: if args.data:
debug_api(args.data) self.debug_api(args.data)
else: else:
debug_api() self.debug_api()
return return
def _node_run(self, args): def _node_run(self, args):
@ -997,7 +1001,7 @@ class connapp:
if len(nodes) == 0: if len(nodes) == 0:
print("{} don't match any node".format(nodelist)) print("{} don't match any node".format(nodelist))
exit(2) exit(2)
nodes = self.connnodes(self.config.getitems(nodes), config = self.config) nodes = self.nodes(self.config.getitems(nodes), config = self.config)
stdout = False stdout = False
if output is None: if output is None:
pass pass
@ -1158,14 +1162,14 @@ class connapp:
if current[1:] not in self.profiles: if current[1:] not in self.profiles:
raise inquirer.errors.ValidationError("", reason="Profile {} don't exist".format(current)) raise inquirer.errors.ValidationError("", reason="Profile {} don't exist".format(current))
elif current != "": elif current != "":
if current not in self.nodes : if current not in self.nodes_list :
raise inquirer.errors.ValidationError("", reason="Node {} don't exist.".format(current)) raise inquirer.errors.ValidationError("", reason="Node {} don't exist.".format(current))
return True return True
def _profile_jumphost_validation(self, answers, current): def _profile_jumphost_validation(self, answers, current):
#Validation for Jumphost in inquirer when managing profiles #Validation for Jumphost in inquirer when managing profiles
if current != "": if current != "":
if current not in self.nodes : if current not in self.nodes_list :
raise inquirer.errors.ValidationError("", reason="Node {} don't exist.".format(current)) raise inquirer.errors.ValidationError("", reason="Node {} don't exist.".format(current))
return True return True

View File

@ -12,10 +12,11 @@ import sys
import threading import threading
from pathlib import Path from pathlib import Path
from copy import deepcopy from copy import deepcopy
from .hooks import ClassHook, MethodHook
import io import io
#functions and classes #functions and classes
@ClassHook
class node: class node:
''' This class generates a node object. Containts all the information and methods to connect and interact with a device using ssh or telnet. ''' This class generates a node object. Containts all the information and methods to connect and interact with a device using ssh or telnet.
@ -139,6 +140,7 @@ class node:
else: else:
self.jumphost = "" self.jumphost = ""
@MethodHook
def _passtx(self, passwords, *, keyfile=None): def _passtx(self, passwords, *, keyfile=None):
# decrypts passwords, used by other methdos. # decrypts passwords, used by other methdos.
dpass = [] dpass = []
@ -161,6 +163,7 @@ class node:
@MethodHook
def _logfile(self, logfile = None): def _logfile(self, logfile = None):
# translate logs variables and generate logs path. # translate logs variables and generate logs path.
if logfile == None: if logfile == None:
@ -176,6 +179,7 @@ class node:
logfile = re.sub(r'\$\{date (.*)}',now.strftime(dateconf.group(1)), logfile) logfile = re.sub(r'\$\{date (.*)}',now.strftime(dateconf.group(1)), logfile)
return logfile return logfile
@MethodHook
def _logclean(self, logfile, var = False): def _logclean(self, logfile, var = False):
#Remove special ascii characters and other stuff from logfile. #Remove special ascii characters and other stuff from logfile.
if var == False: if var == False:
@ -202,6 +206,7 @@ class node:
else: else:
return t return t
@MethodHook
def _savelog(self): def _savelog(self):
'''Save the log buffer to the file at regular intervals if there are changes.''' '''Save the log buffer to the file at regular intervals if there are changes.'''
t = threading.current_thread() t = threading.current_thread()
@ -217,11 +222,13 @@ class node:
prev_size = current_size # Update the previous size prev_size = current_size # Update the previous size
sleep(5) sleep(5)
@MethodHook
def _filter(self, a): def _filter(self, a):
#Set time for last input when using interact #Set time for last input when using interact
self.lastinput = time() self.lastinput = time()
return a return a
@MethodHook
def _keepalive(self): def _keepalive(self):
#Send keepalive ctrl+e when idletime passed without new inputs on interact #Send keepalive ctrl+e when idletime passed without new inputs on interact
self.lastinput = time() self.lastinput = time()
@ -233,6 +240,7 @@ class node:
sleep(1) sleep(1)
@MethodHook
def interact(self, debug = False): def interact(self, debug = False):
''' '''
Allow user to interact with the node directly, mostly used by connection manager. Allow user to interact with the node directly, mostly used by connection manager.
@ -274,6 +282,7 @@ class node:
print(connect) print(connect)
exit(1) exit(1)
@MethodHook
def run(self, commands, vars = None,*, folder = '', prompt = r'>$|#$|\$$|>.$|#.$|\$.$', stdout = False, timeout = 10): def run(self, commands, vars = None,*, folder = '', prompt = r'>$|#$|\$$|>.$|#.$|\$.$', stdout = False, timeout = 10):
''' '''
Run a command or list of commands on the node and return the output. Run a command or list of commands on the node and return the output.
@ -362,6 +371,7 @@ class node:
f.close() f.close()
return connect return connect
@MethodHook
def test(self, commands, expected, vars = None,*, prompt = r'>$|#$|\$$|>.$|#.$|\$.$', timeout = 10): def test(self, commands, expected, vars = None,*, prompt = r'>$|#$|\$$|>.$|#.$|\$.$', timeout = 10):
''' '''
Run a command or list of commands on the node, then check if expected value appears on the output after the last command. Run a command or list of commands on the node, then check if expected value appears on the output after the last command.
@ -457,6 +467,7 @@ class node:
self.status = 1 self.status = 1
return connect return connect
@MethodHook
def _connect(self, debug = False, timeout = 10, max_attempts = 3): def _connect(self, debug = False, timeout = 10, max_attempts = 3):
# Method to connect to the node, it parse all the information, create the ssh/telnet command and login to the node. # Method to connect to the node, it parse all the information, create the ssh/telnet command and login to the node.
if self.protocol in ["ssh", "sftp"]: if self.protocol in ["ssh", "sftp"]:
@ -557,6 +568,7 @@ class node:
self.child = child self.child = child
return True return True
@ClassHook
class nodes: class nodes:
''' This class generates a nodes object. Contains a list of node class objects and methods to run multiple tasks on nodes simultaneously. ''' This class generates a nodes object. Contains a list of node class objects and methods to run multiple tasks on nodes simultaneously.
@ -608,12 +620,14 @@ class nodes:
setattr(self,n,this) setattr(self,n,this)
@MethodHook
def _splitlist(self, lst, n): def _splitlist(self, lst, n):
#split a list in lists of n members. #split a list in lists of n members.
for i in range(0, len(lst), n): for i in range(0, len(lst), n):
yield lst[i:i + n] yield lst[i:i + n]
@MethodHook
def run(self, commands, vars = None,*, folder = None, prompt = None, stdout = None, parallel = 10, timeout = None): def run(self, commands, vars = None,*, folder = None, prompt = None, stdout = None, parallel = 10, timeout = None):
''' '''
Run a command or list of commands on all the nodes in nodelist. Run a command or list of commands on all the nodes in nodelist.
@ -698,6 +712,7 @@ class nodes:
self.status = status self.status = status
return output return output
@MethodHook
def test(self, commands, expected, vars = None,*, prompt = None, parallel = 10, timeout = None): def test(self, commands, expected, vars = None,*, prompt = None, parallel = 10, timeout = None):
''' '''
Run a command or list of commands on all the nodes in nodelist, then check if expected value appears on the output after the last command. Run a command or list of commands on all the nodes in nodelist, then check if expected value appears on the output after the last command.

View File

@ -6,6 +6,7 @@ import zipfile
import tempfile import tempfile
import io import io
import yaml import yaml
import threading
from google.oauth2.credentials import Credentials from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request from google.auth.transport.requests import Request
from googleapiclient.discovery import build from googleapiclient.discovery import build
@ -23,8 +24,9 @@ class sync:
self.file = connapp.config.file self.file = connapp.config.file
self.key = connapp.config.key self.key = connapp.config.key
self.google_client = f"{os.path.dirname(os.path.abspath(__file__))}/sync_client" self.google_client = f"{os.path.dirname(os.path.abspath(__file__))}/sync_client"
self.connapp = connapp
try: try:
self.sync = connapp.config.config["sync"] self.sync = self.connapp.config.config["sync"]
except: except:
self.sync = False self.sync = False
@ -293,18 +295,29 @@ class sync:
print(f"Backup from Google Drive restored successfully: {selected_file['name']}") print(f"Backup from Google Drive restored successfully: {selected_file['name']}")
return 0 return 0
# @staticmethod def config_listener_post(self, args, kwargs):
def config_listener_post(self, file, result):
if self.sync: if self.sync:
if self.check_login_status() == True: if self.check_login_status() == True:
if not result: if not kwargs["result"]:
self.compress_and_upload() self.compress_and_upload()
return result return kwargs["result"]
def config_listener_pre(self, *args, **kwargs):
try:
self.sync = self.connapp.config.config["sync"]
except:
self.sync = False
return args, kwargs
def start_post_thread(self, *args, **kwargs):
post_thread = threading.Thread(target=self.config_listener_post, args=(args,kwargs))
post_thread.start()
class Preload: class Preload:
def __init__(self, connapp): def __init__(self, connapp):
syncapp = sync(connapp) syncapp = sync(connapp)
connapp.config._saveconfig.register_post_hook(syncapp.config_listener_post) connapp.config._saveconfig.register_post_hook(syncapp.start_post_thread)
connapp.config._saveconfig.register_pre_hook(syncapp.config_listener_pre)
class Parser: class Parser:
def __init__(self): def __init__(self):
@ -324,8 +337,6 @@ class Parser:
class Entrypoint: class Entrypoint:
def __init__(self, args, parser, connapp): def __init__(self, args, parser, connapp):
syncapp = sync(connapp) syncapp = sync(connapp)
# print(args)
# print(syncapp.__dict__)
if args.command == 'login': if args.command == 'login':
syncapp.login() syncapp.login()
elif args.command == "status": elif args.command == "status":
@ -342,81 +353,11 @@ class Entrypoint:
syncapp.restore_last_config(args.id) syncapp.restore_last_config(args.id)
elif args.command == "logout": elif args.command == "logout":
syncapp.logout() syncapp.logout()
# if args.command == 'netmask':
# if args.file:
# for line in args.file:
# line = line.strip()
# if line:
# print(NetmaskTools.process_input(args.conversion, line.strip()))
# else:
# input_str = ' '.join(args.input)
# print(NetmaskTools.process_input(args.conversion, input_str))
# elif args.command == 'summarize':
# with args.file as file:
# subnets = [line.strip() for line in file if line.strip()]
# summarized = Sumarize.summarize_subnets(subnets, args.mode)
# if isinstance(summarized, list):
# for subnet in summarized:
# print(subnet)
# else:
# print(summarized)
# elif args.command == 'password':
# if connapp:
# for passwd in Password.get_passwords(args, connapp):
# print(passwd)
# elif args.command == 'connect':
# Connect.connect_command(args)
# else:
# parser.print_help()
def _connpy_completion(wordsnumber, words, info = None): def _connpy_completion(wordsnumber, words, info = None):
if wordsnumber == 3: if wordsnumber == 3:
result = ["--help", "netmask", "summarize", "password", "connect"] result = ["--help", "login", "status", "start", "stop", "list", "once", "restore", "logout"]
#NETMASK_completion #NETMASK_completion
if wordsnumber == 4 and words[1] == "netmask": if wordsnumber == 4 and words[1] == "restore":
result = ['cidr_to_netmask', 'cidr_to_wildcard', result = ["--help", "--id"]
'netmask_to_cidr', 'wildcard_to_cidr',
'netmask_to_wildcard', 'wildcard_to_netmask', 'cidr_to_range', "--file", "--help"]
elif wordsnumber == 6 and words[1] == "netmask" and words[2] in ["-f", "--file"]:
result = ['cidr_to_netmask', 'cidr_to_wildcard',
'netmask_to_cidr', 'wildcard_to_cidr',
'netmask_to_wildcard', 'wildcard_to_netmask']
elif wordsnumber == 5 and words[1] == "netmask" and words[2] in ["-f", "--file"]:
result = _getcwd(words, words[2])
elif wordsnumber == 6 and words[1] == "netmask" and words[3] in ["-f", "--file"]:
result = _getcwd(words, words[2])
#SUMMARIZE_completion
elif wordsnumber == 4 and words[1] == "summarize":
result = _getcwd(words, words[1])
result.extend(["--mode", "--help"])
elif wordsnumber == 5 and words[1] == "summarize":
if words[2] == "--mode":
result = ["strict", "inclusive"]
else:
result = ["--mode"]
elif wordsnumber == 6 and words[1] == "summarize":
if words[3] == "--mode":
result = ["strict", "inclusive"]
elif words[3] in ["strict", "inclusive"]:
result = _getcwd(words, words[3])
#PASSWORD_completion
elif wordsnumber == 4 and words[1] == "password":
result = info["nodes"]
result.extend(info["profiles"])
result.extend(["--help", "--profile"])
elif wordsnumber == 5 and words[1] == "password":
if words[2] == "--profile":
result = info["profiles"]
else:
result = ["--profile"]
#CONNECT_completion
elif wordsnumber == 4 and words[1] == "connect":
result = ["start", "stop", "restart", "--help"]
return result return result
if __name__ == "__main__":
parser = Parser()
args = parser.parser.parse_args()
Entrypoint(args, parser.parser, None)

View File

@ -1,11 +1,11 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#Imports #Imports
from functools import wraps from functools import wraps, partial
#functions and classes #functions and classes
class ConfigHook: class MethodHook:
"""Decorator class to enable Config save hooking""" """Decorator class to enable Methods hooking"""
def __init__(self, func): def __init__(self, func):
self.func = func self.func = func
@ -19,11 +19,10 @@ class ConfigHook:
try: try:
args, kwargs = hook(*args, **kwargs) args, kwargs = hook(*args, **kwargs)
except Exception as e: except Exception as e:
print(f"ConfigHook Pre-hook raised an exception: {e}") print(f"{self.func.__name__} Pre-hook {hook.__name__} raised an exception: {e}")
try: try:
# Execute original function result = self.func(*args, **kwargs)
result = self.func(self.instance, *args, **kwargs)
finally: finally:
# Execute post-hooks after the original function # Execute post-hooks after the original function
@ -31,13 +30,22 @@ class ConfigHook:
try: try:
result = hook(*args, **kwargs, result=result) # Pass result to hooks result = hook(*args, **kwargs, result=result) # Pass result to hooks
except Exception as e: except Exception as e:
print(f"ConfigHook Post-hook raised an exception: {e}") print(f"{self.func.__name__} Post-hook {hook.__name__} raised an exception: {e}")
return result return result
def __get__(self, instance, owner): def __get__(self, instance, owner):
self.instance = instance if not instance:
return self return self
else:
return self.make_callable(instance)
def make_callable(self, instance):
# Returns a callable that also allows access to hook registration methods
callable_instance = partial(self.__call__, instance)
callable_instance.register_pre_hook = self.register_pre_hook
callable_instance.register_post_hook = self.register_post_hook
return callable_instance
def register_pre_hook(self, hook): def register_pre_hook(self, hook):
"""Register a function to be called before the original function""" """Register a function to be called before the original function"""
@ -46,3 +54,36 @@ class ConfigHook:
def register_post_hook(self, hook): def register_post_hook(self, hook):
"""Register a function to be called after the original function""" """Register a function to be called after the original function"""
self.post_hooks.append(hook) self.post_hooks.append(hook)
class ClassHook:
"""Decorator class to enable Class Modifying"""
def __init__(self, cls):
self.cls = cls
# Initialize deferred class hooks if they don't already exist
if not hasattr(cls, 'deferred_class_hooks'):
cls.deferred_class_hooks = []
def __call__(self, *args, **kwargs):
instance = self.cls(*args, **kwargs)
# Attach instance-specific modify method
instance.modify = self.make_instance_modify(instance)
# Apply any deferred modifications
for hook in self.cls.deferred_class_hooks:
hook(instance)
return instance
def __getattr__(self, name):
"""Delegate attribute access to the class."""
return getattr(self.cls, name)
def modify(self, modification_func):
"""Queue a modification to be applied to all future instances of the class."""
self.cls.deferred_class_hooks.append(modification_func)
def make_instance_modify(self, instance):
"""Create a modify function that is bound to a specific instance."""
def modify_instance(modification_func):
"""Modify this specific instance."""
modification_func(instance)
return modify_instance