def delete_doc_by_query(self, index: str, query: dict): try: res = self.connection.delete_by_query(index=index, body=query) return {"index": index, "result": res} except Exception as e: log_error(e) raise
def is_es_alive(cls): log_info("Checking if ElasticSearch is Up!") status = cls.es_client.is_alive() if status: log_info("ElasticSearch is Up!") else: log_error("ElasticSearch is Down!") return status
def get_doc(self, index: str, doc_id: str): try: res = self.connection.get(index=index, id=doc_id) return { "data": res.get('_source'), } except Exception as e: log_error(e) raise
def update_doc(self, index: str, data: object, doc_id: str): try: res = self.connection.update(index=index, id=doc_id, body={"doc": data}, refresh='wait_for') return {"index": res['_index'], "id": res['_id'], "result": res} except Exception as e: log_error(e) raise
def delete_doc(self, index: str, doc_id: str): try: res = self.connection.delete(index=index, id=doc_id) return { "index": res['_index'], "id": res['_id'], "result": res['result'] } except Exception as e: log_error(e) raise
def es_prep(cls): log_info("Creating Galaxy Elasticsearch Indices") status = cls.es_client.es_load_defaults( discovery=TAXII_DEFAULT_DISCOVERY, roots=TAXII_DEFAULT_ROOTS, collections=TAXXI_DEFAULT_COLLECTIONS) if status: log_info("Default Data is Loaded") else: log_error("Failed to Load Default Data") return status
def store_docs(self, index: str, data: list): try: def yield_bulk_data(bulk_data): for doc in bulk_data: yield {"_index": index, "_id": doc['id'], "_source": doc} res = helpers.bulk(self, yield_bulk_data(data)) return {"result": res} except Exception as e: log_error(e) raise
def get_docs(self, index: str): try: res = self.connection.search(index=index, size=10, sort='_id') results = [] for result in res['hits']['hits']: response = {} response.update(result['_source']) response.update({'id': result['_id']}) results.append(response) return { "data": results, "total": res['hits']['total']['value'], } except Exception as e: log_error(e) raise
def post_objects(cls, cti_objects): log_info(f'Request to Post {len(cti_objects)} Objects') result = {} try: entry = cls.es_client.store_docs( index="stix21", data=cti_objects.dict().get('objects')) result["status"] = 'success' result["payload"] = entry return result except Exception as e: log_error(e) result["status"] = 'fail' result["payload"] = { "message": "Error (E:4) while posting the object .." } return result
def store_doc(self, index: str, data: object, doc_id=int(round(time.time() * 1000))): try: res = self.connection.index(index=index, id=doc_id, body=data, refresh='wait_for') return { "index": res['_index'], "id": res['_id'], "result": res['result'] } except Exception as e: log_error(e) raise
def find_object(cls, object_id): log_debug(f'Request to Find Object: {object_id}') result = {} try: cti_object = cls.es_client.get_doc(index="stix21", doc_id=object_id) result["status"] = 'success' result["payload"] = { "data": { "id": object_id, "content": cti_object.get('data') } } return result except Exception as e: log_error(e) result["status"] = 'fail' result["payload"] = {"message": "Error (E:2) Object not found .."} return result
def es_load_defaults(self, discovery, roots, collections): try: if not self.indices.exists(discovery.get('_index')): log_info(f"Creating {discovery.get('_index')} index...") self.indices.create(index=discovery.get('_index')) for root in roots: if not self.indices.exists(root.get('_index')): log_info(f"Creating {root.get('_index')} index...") self.indices.create(index=root.get('_index')) root_to_update = roots[0] root_to_update.update({"_source": {'collections': collections}}) log_info(f"Loading data in discovery and root indices...") bulk_data = [discovery, root_to_update, roots[1]] helpers.bulk(self, bulk_data) return {"result": True} except Exception as e: log_error(e) return {"result": False}
from routes import taxii from controllers.logging import log_info, log_error from initialize import BackInit ORIGINS = ["http://localhost:3000"] services_up = BackInit.ping_services() if services_up: BackInit.services_prep() app = FastAPI() app.add_middleware( CORSMiddleware, allow_origins=ORIGINS, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) app.include_router(exchange.router) app.include_router(taxii.router) log_info('Galaxy server is running ..') else: log_error( 'Galaxy is not ready to start, one or more data access service is down' )