@cryptotaxi247 / CoPilot / commits / 48a1ac3f

Create connectors.py

taylor_socfortress committed Jul 10, 2023 at 16:40 UTC 48a1ac3f63bc5043537efc27f79c98563d2bf055
1 file changed +99
backend/app/routes/connectors.py new
+99
@@ -0,0 +1,99 @@
1 +from flask import Blueprint, jsonify, request
2 +from loguru import logger
3 +from app.models.models import (
4 + ConnectorsAvailable,
5 + Connectors,
6 + connectors_available_schema,
7 +)
8 +
9 +from app.services.connectors.connectors import ConnectorService
10 +from app import db
11 +
12 +bp = Blueprint("connectors", __name__)
13 +
14 +
15 +@bp.route("/connectors", methods=["GET"])
16 +def list_connectors_available():
17 + """
18 + Endpoint to list all available connectors.
19 + It processes each connector to verify the connection and returns the results.
20 +
21 + Returns:
22 + json: A JSON response containing the list of all available connectors along with their connection verification status.
23 + """
24 + connectors_service = ConnectorService(db)
25 + connectors = ConnectorsAvailable.query.all()
26 + result = connectors_available_schema.dump(connectors)
27 +
28 + instantiated_connectors = [
29 + connectors_service.process_connector(connector["connector_name"])
30 + for connector in result
31 + if connectors_service.process_connector(connector["connector_name"])
32 + ]
33 +
34 + return jsonify(instantiated_connectors)
35 +
36 +
37 +@bp.route("/connectors/<id>", methods=["GET"])
38 +def get_connector_details(id):
39 + """
40 + Endpoint to get the details of a connector.
41 +
42 + Args:
43 + id (str): The id of the connector to be fetched.
44 +
45 + Returns:
46 + json: A JSON response containing the details of the connector.
47 + """
48 + # Call service function instead of direct function call
49 + service = ConnectorService(db)
50 + connector_validated = service.validate_connector_exists(
51 + int(id)
52 + ) # convert id to integer
53 + logger.info(connector_validated)
54 + if connector_validated["success"] == False:
55 + return jsonify(connector_validated), 404
56 +
57 + # Fetch connector using the ID
58 + connector = Connectors.query.get(id)
59 + # Call service function instead of direct function call
60 + instantiated_connector = service.process_connector(connector.connector_name)
61 + return jsonify(instantiated_connector)
62 +
63 +
64 +@bp.route("/connectors/<id>", methods=["PUT"])
65 +def update_connector_route(id):
66 + """
67 + Endpoint to update a connector.
68 +
69 + Args:
70 + id (str): The id of the connector to be updated.
71 +
72 + Returns:
73 + json: A JSON response containing the success status of the update operation and a message indicating the status. If the update operation was successful, it returns the connector name and the status of the connection verification.
74 + """
75 + api_key_connector = ["Shuffle", "DFIR-IRIS", "Velociraptor"]
76 +
77 + request_data = request.get_json()
78 + service = ConnectorService(db)
79 + connector_validated = service.validate_connector_exists(
80 + int(id)
81 + ) # convert id to integer
82 + logger.info(connector_validated)
83 + if connector_validated["success"] == False:
84 + return jsonify(connector_validated), 404
85 +
86 + if connector_validated["connector_name"] in api_key_connector:
87 + data_validated = service.validate_request_data_api_key(request_data)
88 + if data_validated["success"] == False:
89 + return jsonify(data_validated), 400
90 + else:
91 + service.update_connector(int(id), request_data)
92 + return service.verify_connector_connection(int(id))
93 +
94 + data_validated = service.validate_request_data(request_data)
95 + if data_validated["success"] == False:
96 + return jsonify(data_validated), 400
97 +
98 + service.update_connector(int(id), request_data)
99 + return service.verify_connector_connection(int(id))