@cryptotaxi247 / CoPilot / commits / cf6e5f58

update db with connector verification status (#85)

taylor_socfortress committed Jul 31, 2023 at 13:07 UTC cf6e5f589ad1ad4b05751c42ab1ca5748252e1af
1 file changed +93
backend/app/models/connectors.py
+93
@@ -94,6 +94,35 @@ class Connector(ABC):
94 else:
95 raise NoResultFound
96
97 + def update_connectors_available_table(
98 + self,
99 + connector_name: str,
100 + connector_configured: bool,
101 + connector_verified: bool,
102 + ) -> Dict[str, Any]:
103 + """
104 + This method updates the `connector_configured` and `connector_verified` columns of the
105 + `connectors_available` table in the database.
106 +
107 + :param connector_name: A string that specifies the name of the connector whose information is to be updated.
108 + :param connector_configured: A boolean that specifies whether the connector is configured.
109 + :param connector_verified: A boolean that specifies whether the connector is verified.
110 +
111 + :return: A dictionary of the connector's attributes if the connector exists. Otherwise, it raises a
112 + NoResultFound exception.
113 + """
114 + connector = (
115 + current_app.extensions["sqlalchemy"].db.session.query(ConnectorsAvailable).filter_by(connector_name=connector_name).first()
116 + )
117 + if connector:
118 + connector.connector_configured = connector_configured
119 + connector.connector_verified = connector_verified
120 + current_app.extensions["sqlalchemy"].db.session.commit()
121 + attributes = {col.name: getattr(connector, col.name) for col in ConnectorsAvailable.__table__.columns}
122 + return attributes
123 + else:
124 + raise NoResultFound
125 +
126
127 class WazuhIndexerConnector(Connector):
128 """
@@ -129,11 +158,15 @@ class WazuhIndexerConnector(Connector):
158 )
159 es.cluster.health()
160 logger.info(f"Connection to {self.attributes['connector_url']} successful")
161 + # Update the connector_available table
162 + self.update_connectors_available_table(self.attributes["connector_name"], True, True)
163 return {"connectionSuccessful": True}
164 except Exception as e:
165 logger.error(
166 f"Connection to {self.attributes['connector_url']} failed with error: {e}",
167 )
168 + # Update the connector_available table
169 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
170 return {"connectionSuccessful": False, "clusterHealth": None}
171
172
@@ -170,16 +203,22 @@ class GraylogConnector(Connector):
203 logger.info(
204 f"Connection to {self.attributes['connector_url']} successful",
205 )
206 + # Update the connector_available table
207 + self.update_connectors_available_table(self.attributes["connector_name"], True, True)
208 return {"connectionSuccessful": True}
209 else:
210 logger.error(
211 f"Connection to {self.attributes['connector_url']} failed with error: {graylog_roles.text}",
212 )
213 + # Update the connector_available table
214 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
215 return {"connectionSuccessful": False, "roles": None}
216 except Exception as e:
217 logger.error(
218 f"Connection to {self.attributes['connector_url']} failed with error: {e}",
219 )
220 + # Update the connector_available table
221 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
222 return {"connectionSuccessful": False, "roles": None}
223
224
@@ -216,16 +255,22 @@ class WazuhManagerConnector(Connector):
255 logger.debug("Wazuh Authentication Token successful")
256 wazuh_auth_token = wazuh_auth_token.json()
257 wazuh_auth_token = wazuh_auth_token["data"]["token"]
258 + # Update the connector_available table
259 + self.update_connectors_available_table(self.attributes["connector_name"], True, True)
260 return {"connectionSuccessful": True, "authToken": wazuh_auth_token}
261 else:
262 logger.error(
263 f"Connection to {self.attributes['connector_url']} failed with error: {wazuh_auth_token.text}",
264 )
265 + # Update the connector_available table
266 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
267 return {"connectionSuccessful": False, "authToken": None}
268 except Exception as e:
269 logger.error(
270 f"Connection to {self.attributes['connector_url']} failed with error: {e}",
271 )
272 + # Update the connector_available table
273 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
274 return {"connectionSuccessful": False, "authToken": None}
275
276 def get_auth_token(self) -> str:
@@ -271,16 +316,22 @@ class ShuffleConnector(Connector):
316 logger.info(
317 f"Connection to {self.attributes['connector_url']} successful",
318 )
319 + # Update the connector_available table
320 + self.update_connectors_available_table(self.attributes["connector_name"], True, True)
321 return {"connectionSuccessful": True}
322 else:
323 logger.error(
324 f"Connection to {self.attributes['connector_url']} failed with error: {shuffle_apps.text}",
325 )
326 + # Update the connector_available table
327 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
328 return {"connectionSuccessful": False}
329 except Exception as e:
330 logger.error(
331 f"Connection to {self.attributes['connector_url']} failed with error: {e}",
332 )
333 + # Update the connector_available table
334 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
335 return {"connectionSuccessful": False}
336
337
@@ -318,16 +369,22 @@ class DfirIrisConnector(Connector):
369 logger.info(
370 f"Connection to {self.attributes['connector_url']} successful",
371 )
372 + # Update the connector_available table
373 + self.update_connectors_available_table(self.attributes["connector_name"], True, True)
374 return {"connectionSuccessful": True}
375 else:
376 logger.error(
377 f"Connection to {self.attributes['connector_url']} failed with error: {dfir_iris.text}",
378 )
379 + # Update the connector_available table
380 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
381 return {"connectionSuccessful": False, "response": None}
382 except Exception as e:
383 logger.error(
384 f"Connection to {self.attributes['connector_url']} failed with error: {e}",
385 )
386 + # Update the connector_available table
387 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
388 return {"connectionSuccessful": False, "response": None}
389
390
@@ -387,12 +444,18 @@ class VelociraptorConnector(Connector):
444 for response in stub.Query(client_request):
445 if response.Response:
446 r = r + json.loads(response.Response)
447 + # Update the connector_available table
448 + self.update_connectors_available_table(self.attributes["connector_name"], True, True)
449 return {"connectionSuccessful": True}
450 except Exception as e:
451 logger.error(f"Failed to verify connection to Velociraptor: {e}")
452 + # Update the connector_available table
453 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
454 return {"connectionSuccessful": False, "response": None}
455 except Exception as e:
456 logger.error(f"Failed to get connector_api_key from the database: {e}")
457 + # Update the connector_available table
458 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
459 return {"connectionSuccessful": False, "response": None}
460
461
@@ -433,16 +496,22 @@ class SublimeConnector(Connector):
496 logger.info(
497 f"Connection to {self.attributes['connector_url']} successful",
498 )
499 + # Update the connector_available table
500 + self.update_connectors_available_table(self.attributes["connector_name"], True, True)
501 return {"connectionSuccessful": True}
502 else:
503 logger.error(
504 f"Connection to {self.attributes['connector_url']} failed with error: {sublime.text}",
505 )
506 + # Update the connector_available table
507 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
508 return {"connectionSuccessful": False, "response": None}
509 except Exception as e:
510 logger.error(
511 f"Connection to {self.attributes['connector_url']} failed with error: {e}",
512 )
513 + # Update the connector_available table
514 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
515 return {"connectionSuccessful": False, "response": None}
516
517
@@ -487,16 +556,22 @@ class AskSOCFortressConnector(Connector):
556 logger.info(
557 f"Connection to {self.attributes['connector_url']} successful",
558 )
559 + # Update the connector_available table
560 + self.update_connectors_available_table(self.attributes["connector_name"], True, True)
561 return {"connectionSuccessful": True}
562 else:
563 logger.error(
564 f"Connection to {self.attributes['connector_url']} failed with error: {ask_socfortress.text}",
565 )
566 + # Update the connector_available table
567 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
568 return {"connectionSuccessful": False, "response": None}
569 except Exception as e:
570 logger.error(
571 f"Connection to {self.attributes['connector_url']} failed with error: {e}",
572 )
573 + # Update the connector_available table
574 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
575 return {"connectionSuccessful": False, "response": None}
576
577
@@ -541,16 +616,22 @@ class SocfortressThreatIntelConnector(Connector):
616 logger.info(
617 f"Connection to {self.attributes['connector_url']} successful",
618 )
619 + # Update the connector_available table
620 + self.update_connectors_available_table(self.attributes["connector_name"], True, True)
621 return {"connectionSuccessful": True}
622 else:
623 logger.error(
624 f"Connection to {self.attributes['connector_url']} failed with error: {socfortress_threat_intel.text}",
625 )
626 + # Update the connector_available table
627 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
628 return {"connectionSuccessful": False, "response": None}
629 except Exception as e:
630 logger.error(
631 f"Connection to {self.attributes['connector_url']} failed with error: {e}",
632 )
633 + # Update the connector_available table
634 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
635 return {"connectionSuccessful": False, "response": None}
636
637
@@ -590,16 +671,22 @@ class InfluxDBConnector(Connector):
671 logger.info(
672 f"Connection to {self.attributes['connector_url']} successful",
673 )
674 + # Update the connector_available table
675 + self.update_connectors_available_table(self.attributes["connector_name"], True, True)
676 return {"connectionSuccessful": True}
677 else:
678 logger.error(
679 f"Connection to {self.attributes['connector_url']} failed with error: {influxdb.text}",
680 )
681 + # Update the connector_available table
682 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
683 return {"connectionSuccessful": False, "response": None}
684 except Exception as e:
685 logger.error(
686 f"Connection to {self.attributes['connector_url']} failed with error: {e}",
687 )
688 + # Update the connector_available table
689 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
690 return {"connectionSuccessful": False, "response": None}
691
692
@@ -641,14 +728,20 @@ class RabbitMQConnector(Connector):
728 logger.info(
729 f"Connection to {self.attributes['connector_url']} successful",
730 )
731 + # Update the connector_available table
732 + self.update_connectors_available_table(self.attributes["connector_name"], True, True)
733 return {"connectionSuccessful": True}
734 else:
735 logger.error(f"Connection to {self.attributes['connector_url']} failed")
736 + # Update the connector_available table
737 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
738 return {"connectionSuccessful": False, "response": None}
739 except Exception as e:
740 logger.error(
741 f"Connection to {self.attributes['connector_url']} failed with error: {e}",
742 )
743 + # Update the connector_available table
744 + self.update_connectors_available_table(self.attributes["connector_name"], True, False)
745 return {"connectionSuccessful": False, "response": None}
746
747