@cryptotaxi247 / CoPilot / commits / ed33f383

add connectors_available (#84)

add the `connectors_available` sql table results to the `connectors` list that is returned to the frontend so we can check verification status, description, etc

taylor_socfortress committed Jul 31, 2023 at 10:56 UTC ed33f383681923d9d6660016c6a9c288c859c9ee
3 files changed +32 -15
backend/app/models/connectors.py
+21
@@ -18,6 +18,7 @@ from pyvelociraptor import api_pb2_grpc
18 from sqlalchemy.orm.exc import NoResultFound
19
20 from app.models.models import Connectors
21 +from app.models.models import ConnectorsAvailable
22
23
24 def dynamic_import(module_name: str, class_name: str) -> Any:
@@ -73,6 +74,26 @@ class Connector(ABC):
74 else:
75 raise NoResultFound
76
77 + # @staticmethod
78 + def get_connector_available_info_from_db(connector_name: str) -> Dict[str, Any]:
79 + """
80 + This method retrieves connector information from the database.
81 +
82 + :param connector_name: A string that specifies the name of the connector whose information is to be retrieved.
83 + :return: A dictionary of the connector's attributes if the connector exists. Otherwise, it raises a
84 + NoResultFound exception.
85 + Raises:
86 + NoResultFound: If the connector_name is not found in the database.
87 + """
88 + connector = (
89 + current_app.extensions["sqlalchemy"].db.session.query(ConnectorsAvailable).filter_by(connector_name=connector_name).first()
90 + )
91 + if connector:
92 + attributes = {col.name: getattr(connector, col.name) for col in ConnectorsAvailable.__table__.columns}
93 + return attributes
94 + else:
95 + raise NoResultFound
96 +
97
98 class WazuhIndexerConnector(Connector):
99 """
backend/app/routes/connectors.py
+9 -14
@@ -33,20 +33,15 @@ def list_connectors_available():
33 logger.info("Received request to get all available connectors")
34 connectors_service = ConnectorService(db)
35 connectors = ConnectorsAvailable.query.all()
36 - connectors_available = connectors_available_schema.dump(connectors)
37 -
38 - instantiated_connectors = []
39 - for connector in connectors_available:
40 - processed_connector = connectors_service.process_connector(connector["connector_name"])
41 - if processed_connector:
42 - instantiated_connectors.append(processed_connector)
43 -
44 - return {
45 - "message": "All available connectors",
46 - "connectors": instantiated_connectors,
47 - "connectors_available": connectors_available,
48 - "success": True,
49 - }
36 + result = connectors_available_schema.dump(connectors)
37 +
38 + instantiated_connectors = [
39 + connectors_service.process_connector(connector["connector_name"])
40 + for connector in result
41 + if connectors_service.process_connector(connector["connector_name"])
42 + ]
43 +
44 + return {"message": "All available connectors", "connectors": instantiated_connectors, "success": True}
45 except Exception as e:
46 logger.error(f"Error while getting all available connectors: {e}")
47 return {"message": "Error while getting all available connectors", "success": False}, 500
backend/app/services/connectors/connectors.py
+2 -1
@@ -76,8 +76,9 @@ class ConnectorService:
76 connector_instance = connector_factory.create(connector_name, connector_name)
77 connection_successful = connector_instance.verify_connection()
78 connection_details = Connector.get_connector_info_from_db(connector_name)
79 + connection_available_details = Connector.get_connector_available_info_from_db(connector_name)
80 logger.info(f"Connection details: {connection_details}")
80 - return {"name": connector_name, **connection_successful, **connection_details}
81 + return {"name": connector_name, **connection_successful, **connection_details, **connection_available_details}
82
83 def validate_connector_exists(self, connector_id: int):
84 """