| 1 | from datetime import datetime |
| 2 | |
| 3 | from dotenv import load_dotenv |
| 4 | from loguru import logger |
| 5 | from sqlalchemy import select |
| 6 | |
| 7 | from app.db.db_session import get_db_session |
| 8 | from app.db.db_session import get_sync_db_session |
| 9 | from app.integrations.models.customer_integration_settings import CustomerIntegrations |
| 10 | from app.integrations.modules.routes.sap_siem import collect_sap_siem_route |
| 11 | from app.integrations.modules.routes.sap_siem import ( |
| 12 | invoke_sap_siem_brute_force_failed_logins_route, |
| 13 | ) |
| 14 | from app.integrations.modules.routes.sap_siem import ( |
| 15 | invoke_sap_siem_brute_force_failed_logins_same_ip_route, |
| 16 | ) |
| 17 | from app.integrations.modules.routes.sap_siem import ( |
| 18 | invoke_sap_siem_same_user_failed_login_from_different_geo_location_route, |
| 19 | ) |
| 20 | from app.integrations.modules.routes.sap_siem import ( |
| 21 | invoke_sap_siem_same_user_failed_login_from_different_ip_route, |
| 22 | ) |
| 23 | from app.integrations.modules.routes.sap_siem import ( |
| 24 | invoke_sap_siem_same_user_successful_login_from_different_geo_location_route, |
| 25 | ) |
| 26 | from app.integrations.modules.routes.sap_siem import ( |
| 27 | invoke_sap_siem_successful_login_after_multiple_failed_logins_route, |
| 28 | ) |
| 29 | from app.integrations.modules.routes.sap_siem import ( |
| 30 | invoke_sap_siem_successful_user_login_with_different_ip_route, |
| 31 | ) |
| 32 | from app.integrations.modules.schema.sap_siem import InvokeSapSiemAnalysis |
| 33 | |
| 34 | # ! Commenting out for now, will revist later if needed ! # |
| 35 | # from app.integrations.monitoring_alert.routes.monitoring_alert import ( |
| 36 | # run_sap_siem_multiple_logins_same_ip_analysis, |
| 37 | # ) |
| 38 | # from app.integrations.monitoring_alert.routes.monitoring_alert import ( |
| 39 | # run_sap_siem_suspicious_logins_analysis, |
| 40 | # ) |
| 41 | from app.integrations.sap_siem.schema.sap_siem import InvokeSapSiemRequest |
| 42 | from app.integrations.sap_siem.schema.sap_siem import InvokeSAPSiemResponse |
| 43 | from app.schedulers.models.scheduler import JobMetadata |
| 44 | from app.schedulers.utils.universal import get_scheduled_job_metadata |
| 45 | from app.utils import get_customer_meta_attribute |
| 46 | |
| 47 | load_dotenv() |
| 48 | |
| 49 | |
| 50 | async def invoke_sap_siem_integration_collection() -> InvokeSAPSiemResponse: |
| 51 | """ |
| 52 | Invokes the SAP SIEM integration for collection. |
| 53 | """ |
| 54 | logger.info("Invoking SAP SIEM integration scheduled job.") |
| 55 | customer_codes = [] |
| 56 | async with get_db_session() as session: |
| 57 | stmt = select(CustomerIntegrations).where( |
| 58 | CustomerIntegrations.integration_service_name == "SAP SIEM", |
| 59 | ) |
| 60 | result = await session.execute(stmt) |
| 61 | customer_codes = [row.customer_code for row in result.scalars()] |
| 62 | logger.info(f"customer_codes: {customer_codes}") |
| 63 | for customer_code in customer_codes: |
| 64 | await collect_sap_siem_route( |
| 65 | InvokeSapSiemRequest( |
| 66 | customer_code=customer_code, |
| 67 | integration_name="SAP SIEM", |
| 68 | time_range=f"{(await get_scheduled_job_metadata('invoke_sap_siem_integration_collection')).time_interval}m", |
| 69 | ), |
| 70 | session, |
| 71 | ) |
| 72 | # Close the session |
| 73 | await session.close() |
| 74 | with get_sync_db_session() as session: |
| 75 | # Synchronous ORM operations |
| 76 | job_metadata = session.query(JobMetadata).filter_by(job_id="invoke_sap_siem_integration_collection").one_or_none() |
| 77 | if job_metadata: |
| 78 | job_metadata.last_success = datetime.utcnow() |
| 79 | session.add(job_metadata) |
| 80 | session.commit() |
| 81 | else: |
| 82 | # Handle the case where job_metadata does not exist |
| 83 | print("JobMetadata for 'invoke_mimecast_integration' not found.") |
| 84 | |
| 85 | return InvokeSAPSiemResponse(success=True, message="SAP SIEM integration invoked.") |
| 86 | |
| 87 | |
| 88 | async def invoke_sap_siem_integration_suspicious_logins_analysis() -> InvokeSAPSiemResponse: |
| 89 | """ |
| 90 | Invokes the SAP SIEM integration for suspicious logins analysis. |
| 91 | """ |
| 92 | logger.info("Invoking SAP SIEM integration for suspicious logins analysis scheduled job.") |
| 93 | customer_codes = [] |
| 94 | async with get_db_session() as session: |
| 95 | stmt = select(CustomerIntegrations).where( |
| 96 | CustomerIntegrations.integration_service_name == "SAP SIEM", |
| 97 | ) |
| 98 | result = await session.execute(stmt) |
| 99 | customer_codes = [row.customer_code for row in result.scalars()] |
| 100 | logger.info(f"customer_codes: {customer_codes}") |
| 101 | # ! Commenting out for now, will revist later if needed ! # |
| 102 | # for customer_code in customer_codes: |
| 103 | # extra_data = (await get_scheduled_job_metadata("invoke_sap_siem_integration_suspicious_logins_analysis")).extra_data |
| 104 | # threshold = int(extra_data) if extra_data is not None else 3 |
| 105 | # await run_sap_siem_suspicious_logins_analysis( |
| 106 | # threshold=threshold, |
| 107 | # session=session, |
| 108 | # ) |
| 109 | # Close the session |
| 110 | await session.close() |
| 111 | with get_sync_db_session() as session: |
| 112 | # Synchronous ORM operations |
| 113 | job_metadata = session.query(JobMetadata).filter_by(job_id="invoke_sap_siem_integration_suspicious_logins_analysis").one_or_none() |
| 114 | if job_metadata: |
| 115 | job_metadata.last_success = datetime.utcnow() |
| 116 | session.add(job_metadata) |
| 117 | session.commit() |
| 118 | else: |
| 119 | # Handle the case where job_metadata does not exist |
| 120 | print("JobMetadata for 'invoke_sap_siem_integration_suspicious_logins_analysis' not found.") |
| 121 | |
| 122 | return InvokeSAPSiemResponse(success=True, message="SAP SIEM integration invoked for suspicious logins analysis.") |
| 123 | |
| 124 | |
| 125 | async def invoke_sap_siem_integration_multiple_logins_same_ip_analysis() -> InvokeSAPSiemResponse: |
| 126 | """ |
| 127 | Invokes the SAP SIEM integration for multiple logins from the same IP analysis. |
| 128 | """ |
| 129 | logger.info("Invoking SAP SIEM integration for multiple logins from the same IP analysis scheduled job.") |
| 130 | customer_codes = [] |
| 131 | async with get_db_session() as session: |
| 132 | stmt = select(CustomerIntegrations).where( |
| 133 | CustomerIntegrations.integration_service_name == "SAP SIEM", |
| 134 | ) |
| 135 | result = await session.execute(stmt) |
| 136 | customer_codes = [row.customer_code for row in result.scalars()] |
| 137 | logger.info(f"customer_codes: {customer_codes}") |
| 138 | # ! Commenting out for now, will revist later if needed ! # |
| 139 | # for customer_code in customer_codes: |
| 140 | # extra_data = (await get_scheduled_job_metadata("invoke_sap_siem_integration_multiple_logins_same_ip_analysis")).extra_data |
| 141 | # if extra_data is not None: |
| 142 | # data_parts = extra_data.split(",") |
| 143 | # for part in data_parts: |
| 144 | # key, value = part.split("=") |
| 145 | # if key == "threshold": |
| 146 | # threshold = int(value) |
| 147 | # elif key == "time_range": |
| 148 | # time_range = int(value) |
| 149 | # await run_sap_siem_multiple_logins_same_ip_analysis( |
| 150 | # threshold=threshold, |
| 151 | # time_range=time_range, |
| 152 | # session=session, |
| 153 | # ) |
| 154 | # Close the session |
| 155 | await session.close() |
| 156 | with get_sync_db_session() as session: |
| 157 | # Synchronous ORM operations |
| 158 | job_metadata = ( |
| 159 | session.query(JobMetadata).filter_by(job_id="invoke_sap_siem_integration_multiple_logins_same_ip_analysis").one_or_none() |
| 160 | ) |
| 161 | if job_metadata: |
| 162 | job_metadata.last_success = datetime.utcnow() |
| 163 | session.add(job_metadata) |
| 164 | session.commit() |
| 165 | else: |
| 166 | # Handle the case where job_metadata does not exist |
| 167 | print("JobMetadata for 'invoke_sap_siem_integration_multiple_logins_same_ip_analysis' not found.") |
| 168 | |
| 169 | return InvokeSAPSiemResponse(success=True, message="SAP SIEM integration invoked for multiple logins from the same IP analysis.") |
| 170 | |
| 171 | |
| 172 | async def invoke_sap_siem_integration_successful_user_login_with_different_ip() -> InvokeSAPSiemResponse: |
| 173 | """ |
| 174 | Invokes the SAP SIEM integration for successful user login with different IP. |
| 175 | """ |
| 176 | logger.info("Invoking SAP SIEM integration for successful user login with different IP scheduled job.") |
| 177 | customer_codes = [] |
| 178 | async with get_db_session() as session: |
| 179 | stmt = select(CustomerIntegrations).where( |
| 180 | CustomerIntegrations.integration_service_name == "SAP SIEM", |
| 181 | ) |
| 182 | result = await session.execute(stmt) |
| 183 | customer_codes = [row.customer_code for row in result.scalars()] |
| 184 | logger.info(f"customer_codes: {customer_codes}") |
| 185 | for customer_code in customer_codes: |
| 186 | extra_data = ( |
| 187 | await get_scheduled_job_metadata("invoke_sap_siem_integration_successful_user_login_with_different_ip") |
| 188 | ).extra_data |
| 189 | if extra_data is not None: |
| 190 | data_parts = extra_data.split(",") |
| 191 | for part in data_parts: |
| 192 | key, value = part.split("=") |
| 193 | if key == "threshold": |
| 194 | threshold = int(value) |
| 195 | elif key == "time_range": |
| 196 | time_range = int(value) |
| 197 | else: |
| 198 | threshold = 0 |
| 199 | time_range = 15 |
| 200 | await invoke_sap_siem_successful_user_login_with_different_ip_route( |
| 201 | invoke_siem_analysis=InvokeSapSiemAnalysis( |
| 202 | threshold=threshold, |
| 203 | time_range=time_range, |
| 204 | iris_customer_id=(await get_customer_meta_attribute(customer_code, "customer_meta_iris_customer_id", session)), |
| 205 | ), |
| 206 | ) |
| 207 | # Close the session |
| 208 | await session.close() |
| 209 | with get_sync_db_session() as session: |
| 210 | # Synchronous ORM operations |
| 211 | job_metadata = ( |
| 212 | session.query(JobMetadata).filter_by(job_id="invoke_sap_siem_integration_successful_user_login_with_different_ip").one_or_none() |
| 213 | ) |
| 214 | if job_metadata: |
| 215 | job_metadata.last_success = datetime.utcnow() |
| 216 | session.add(job_metadata) |
| 217 | session.commit() |
| 218 | else: |
| 219 | # Handle the case where job_metadata does not exist |
| 220 | print("JobMetadata for 'invoke_sap_siem_integration_successful_user_login_with_different_ip' not found.") |
| 221 | |
| 222 | return InvokeSAPSiemResponse(success=True, message="SAP SIEM integration invoked for successful user login with different IP.") |
| 223 | |
| 224 | |
| 225 | async def invoke_sap_siem_integration_same_user_failed_login_from_different_ip() -> InvokeSAPSiemResponse: |
| 226 | """ |
| 227 | Invokes the SAP SIEM integration for same user failed login from different IP. |
| 228 | """ |
| 229 | logger.info("Invoking SAP SIEM integration for same user failed login from different IP scheduled job.") |
| 230 | customer_codes = [] |
| 231 | async with get_db_session() as session: |
| 232 | stmt = select(CustomerIntegrations).where( |
| 233 | CustomerIntegrations.integration_service_name == "SAP SIEM", |
| 234 | ) |
| 235 | result = await session.execute(stmt) |
| 236 | customer_codes = [row.customer_code for row in result.scalars()] |
| 237 | logger.info(f"customer_codes: {customer_codes}") |
| 238 | for customer_code in customer_codes: |
| 239 | extra_data = ( |
| 240 | await get_scheduled_job_metadata("invoke_sap_siem_integration_same_user_failed_login_from_different_ip") |
| 241 | ).extra_data |
| 242 | if extra_data is not None: |
| 243 | data_parts = extra_data.split(",") |
| 244 | for part in data_parts: |
| 245 | key, value = part.split("=") |
| 246 | if key == "threshold": |
| 247 | threshold = int(value) |
| 248 | elif key == "time_range": |
| 249 | time_range = int(value) |
| 250 | else: |
| 251 | threshold = 0 |
| 252 | time_range = 15 |
| 253 | await invoke_sap_siem_same_user_failed_login_from_different_ip_route( |
| 254 | invoke_siem_analysis=InvokeSapSiemAnalysis( |
| 255 | threshold=threshold, |
| 256 | time_range=time_range, |
| 257 | iris_customer_id=(await get_customer_meta_attribute(customer_code, "customer_meta_iris_customer_id", session)), |
| 258 | ), |
| 259 | ) |
| 260 | # Close the session |
| 261 | await session.close() |
| 262 | with get_sync_db_session() as session: |
| 263 | # Synchronous ORM operations |
| 264 | job_metadata = ( |
| 265 | session.query(JobMetadata) |
| 266 | .filter_by(job_id="invoke_sap_siem_integration_same_user_failed_login_from_different_ip") |
| 267 | .one_or_none() |
| 268 | ) |
| 269 | if job_metadata: |
| 270 | job_metadata.last_success = datetime.utcnow() |
| 271 | session.add(job_metadata) |
| 272 | session.commit() |
| 273 | else: |
| 274 | # Handle the case where job_metadata does not exist |
| 275 | print("JobMetadata for 'invoke_sap_siem_integration_same_user_failed_login_from_different_ip' not found.") |
| 276 | |
| 277 | return InvokeSAPSiemResponse(success=True, message="SAP SIEM integration invoked for same user failed login from different IP.") |
| 278 | |
| 279 | |
| 280 | async def invoke_sap_siem_integration_same_user_failed_login_from_different_geo_location() -> InvokeSAPSiemResponse: |
| 281 | """ |
| 282 | Invokes the SAP SIEM integration for same user failed login from different geo location. |
| 283 | """ |
| 284 | logger.info("Invoking SAP SIEM integration for same user failed login from different geo location scheduled job.") |
| 285 | customer_codes = [] |
| 286 | async with get_db_session() as session: |
| 287 | stmt = select(CustomerIntegrations).where( |
| 288 | CustomerIntegrations.integration_service_name == "SAP SIEM", |
| 289 | ) |
| 290 | result = await session.execute(stmt) |
| 291 | customer_codes = [row.customer_code for row in result.scalars()] |
| 292 | logger.info(f"customer_codes: {customer_codes}") |
| 293 | for customer_code in customer_codes: |
| 294 | extra_data = ( |
| 295 | await get_scheduled_job_metadata("invoke_sap_siem_integration_same_user_failed_login_from_different_geo_location") |
| 296 | ).extra_data |
| 297 | if extra_data is not None: |
| 298 | data_parts = extra_data.split(",") |
| 299 | for part in data_parts: |
| 300 | key, value = part.split("=") |
| 301 | if key == "threshold": |
| 302 | threshold = int(value) |
| 303 | elif key == "time_range": |
| 304 | time_range = int(value) |
| 305 | else: |
| 306 | threshold = 0 |
| 307 | time_range = 15 |
| 308 | await invoke_sap_siem_same_user_failed_login_from_different_geo_location_route( |
| 309 | invoke_siem_analysis=InvokeSapSiemAnalysis( |
| 310 | threshold=threshold, |
| 311 | time_range=time_range, |
| 312 | iris_customer_id=(await get_customer_meta_attribute(customer_code, "customer_meta_iris_customer_id", session)), |
| 313 | ), |
| 314 | ) |
| 315 | # Close the session |
| 316 | await session.close() |
| 317 | with get_sync_db_session() as session: |
| 318 | # Synchronous ORM operations |
| 319 | job_metadata = ( |
| 320 | session.query(JobMetadata) |
| 321 | .filter_by(job_id="invoke_sap_siem_integration_same_user_failed_login_from_different_geo_location") |
| 322 | .one_or_none() |
| 323 | ) |
| 324 | if job_metadata: |
| 325 | job_metadata.last_success = datetime.utcnow() |
| 326 | session.add(job_metadata) |
| 327 | session.commit() |
| 328 | else: |
| 329 | # Handle the case where job_metadata does not exist |
| 330 | print("JobMetadata for 'invoke_sap_siem_integration_same_user_failed_login_from_different_geo_location' not found.") |
| 331 | |
| 332 | return InvokeSAPSiemResponse( |
| 333 | success=True, |
| 334 | message="SAP SIEM integration invoked for same user failed login from different geo location.", |
| 335 | ) |
| 336 | |
| 337 | |
| 338 | async def invoke_sap_siem_integration_same_user_successful_login_from_different_geo_location() -> InvokeSAPSiemResponse: |
| 339 | """ |
| 340 | Invokes the SAP SIEM integration for same user successful login from different geo location. |
| 341 | """ |
| 342 | logger.info("Invoking SAP SIEM integration for same user successful login from different geo location scheduled job.") |
| 343 | customer_codes = [] |
| 344 | async with get_db_session() as session: |
| 345 | stmt = select(CustomerIntegrations).where( |
| 346 | CustomerIntegrations.integration_service_name == "SAP SIEM", |
| 347 | ) |
| 348 | result = await session.execute(stmt) |
| 349 | customer_codes = [row.customer_code for row in result.scalars()] |
| 350 | logger.info(f"customer_codes: {customer_codes}") |
| 351 | for customer_code in customer_codes: |
| 352 | extra_data = ( |
| 353 | await get_scheduled_job_metadata("invoke_sap_siem_integration_same_user_successful_login_from_different_geo_location") |
| 354 | ).extra_data |
| 355 | if extra_data is not None: |
| 356 | data_parts = extra_data.split(",") |
| 357 | for part in data_parts: |
| 358 | key, value = part.split("=") |
| 359 | if key == "threshold": |
| 360 | threshold = int(value) |
| 361 | elif key == "time_range": |
| 362 | time_range = int(value) |
| 363 | else: |
| 364 | threshold = 0 |
| 365 | time_range = 15 |
| 366 | await invoke_sap_siem_same_user_successful_login_from_different_geo_location_route( |
| 367 | invoke_siem_analysis=InvokeSapSiemAnalysis( |
| 368 | threshold=threshold, |
| 369 | time_range=time_range, |
| 370 | iris_customer_id=(await get_customer_meta_attribute(customer_code, "customer_meta_iris_customer_id", session)), |
| 371 | ), |
| 372 | ) |
| 373 | # Close the session |
| 374 | await session.close() |
| 375 | with get_sync_db_session() as session: |
| 376 | # Synchronous ORM operations |
| 377 | job_metadata = ( |
| 378 | session.query(JobMetadata) |
| 379 | .filter_by(job_id="invoke_sap_siem_integration_same_user_successful_login_from_different_geo_location") |
| 380 | .one_or_none() |
| 381 | ) |
| 382 | if job_metadata: |
| 383 | job_metadata.last_success = datetime.utcnow() |
| 384 | session.add(job_metadata) |
| 385 | session.commit() |
| 386 | else: |
| 387 | # Handle the case where job_metadata does not exist |
| 388 | print("JobMetadata for 'invoke_sap_siem_integration_same_user_successful_login_from_different_geo_location' not found.") |
| 389 | |
| 390 | return InvokeSAPSiemResponse( |
| 391 | success=True, |
| 392 | message="SAP SIEM integration invoked for same user successful login from different geo location.", |
| 393 | ) |
| 394 | |
| 395 | |
| 396 | async def invoke_sap_siem_integration_brute_force_failed_logins() -> InvokeSAPSiemResponse: |
| 397 | """ |
| 398 | Invokes the SAP SIEM integration for brute force failed logins. |
| 399 | """ |
| 400 | logger.info("Invoking SAP SIEM integration for brute force failed logins scheduled job.") |
| 401 | customer_codes = [] |
| 402 | async with get_db_session() as session: |
| 403 | stmt = select(CustomerIntegrations).where( |
| 404 | CustomerIntegrations.integration_service_name == "SAP SIEM", |
| 405 | ) |
| 406 | result = await session.execute(stmt) |
| 407 | customer_codes = [row.customer_code for row in result.scalars()] |
| 408 | logger.info(f"customer_codes: {customer_codes}") |
| 409 | for customer_code in customer_codes: |
| 410 | extra_data = (await get_scheduled_job_metadata("invoke_sap_siem_integration_brute_force_failed_logins")).extra_data |
| 411 | if extra_data is not None: |
| 412 | data_parts = extra_data.split(",") |
| 413 | for part in data_parts: |
| 414 | key, value = part.split("=") |
| 415 | if key == "threshold": |
| 416 | threshold = int(value) |
| 417 | elif key == "time_range": |
| 418 | time_range = int(value) |
| 419 | else: |
| 420 | threshold = 0 |
| 421 | time_range = 3 |
| 422 | await invoke_sap_siem_brute_force_failed_logins_route( |
| 423 | invoke_siem_analysis=InvokeSapSiemAnalysis( |
| 424 | threshold=threshold, |
| 425 | time_range=time_range, |
| 426 | iris_customer_id=(await get_customer_meta_attribute(customer_code, "customer_meta_iris_customer_id", session)), |
| 427 | ), |
| 428 | ) |
| 429 | # Close the session |
| 430 | await session.close() |
| 431 | with get_sync_db_session() as session: |
| 432 | # Synchronous ORM operations |
| 433 | job_metadata = session.query(JobMetadata).filter_by(job_id="invoke_sap_siem_integration_brute_force_failed_logins").one_or_none() |
| 434 | if job_metadata: |
| 435 | job_metadata.last_success = datetime.utcnow() |
| 436 | session.add(job_metadata) |
| 437 | session.commit() |
| 438 | else: |
| 439 | # Handle the case where job_metadata does not exist |
| 440 | print("JobMetadata for 'invoke_sap_siem_integration_brute_force_failed_logins' not found.") |
| 441 | |
| 442 | return InvokeSAPSiemResponse(success=True, message="SAP SIEM integration invoked for brute force failed logins.") |
| 443 | |
| 444 | |
| 445 | async def invoke_sap_siem_integration_brute_force_failed_logins_same_ip() -> InvokeSAPSiemResponse: |
| 446 | """ |
| 447 | Invokes the SAP SIEM integration for brute force failed logins from the same IP. |
| 448 | """ |
| 449 | logger.info("Invoking SAP SIEM integration for brute force failed logins from the same IP scheduled job.") |
| 450 | customer_codes = [] |
| 451 | async with get_db_session() as session: |
| 452 | stmt = select(CustomerIntegrations).where( |
| 453 | CustomerIntegrations.integration_service_name == "SAP SIEM", |
| 454 | ) |
| 455 | result = await session.execute(stmt) |
| 456 | customer_codes = [row.customer_code for row in result.scalars()] |
| 457 | logger.info(f"customer_codes: {customer_codes}") |
| 458 | for customer_code in customer_codes: |
| 459 | extra_data = (await get_scheduled_job_metadata("invoke_sap_siem_integration_brute_force_failed_logins_same_ip")).extra_data |
| 460 | if extra_data is not None: |
| 461 | data_parts = extra_data.split(",") |
| 462 | for part in data_parts: |
| 463 | key, value = part.split("=") |
| 464 | if key == "threshold": |
| 465 | threshold = int(value) |
| 466 | elif key == "time_range": |
| 467 | time_range = int(value) |
| 468 | else: |
| 469 | threshold = 0 |
| 470 | time_range = 3 |
| 471 | await invoke_sap_siem_brute_force_failed_logins_same_ip_route( |
| 472 | invoke_siem_analysis=InvokeSapSiemAnalysis( |
| 473 | threshold=threshold, |
| 474 | time_range=time_range, |
| 475 | iris_customer_id=(await get_customer_meta_attribute(customer_code, "customer_meta_iris_customer_id", session)), |
| 476 | ), |
| 477 | ) |
| 478 | # Close the session |
| 479 | await session.close() |
| 480 | with get_sync_db_session() as session: |
| 481 | # Synchronous ORM operations |
| 482 | job_metadata = ( |
| 483 | session.query(JobMetadata).filter_by(job_id="invoke_sap_siem_integration_brute_force_failed_logins_same_ip").one_or_none() |
| 484 | ) |
| 485 | if job_metadata: |
| 486 | job_metadata.last_success = datetime.utcnow() |
| 487 | session.add(job_metadata) |
| 488 | session.commit() |
| 489 | else: |
| 490 | # Handle the case where job_metadata does not exist |
| 491 | print("JobMetadata for 'invoke_sap_siem_integration_brute_force_failed_logins_same_ip' not found.") |
| 492 | |
| 493 | return InvokeSAPSiemResponse(success=True, message="SAP SIEM integration invoked for brute force failed logins from the same IP.") |
| 494 | |
| 495 | |
| 496 | async def invoke_sap_siem_integration_successful_login_after_multiple_failed_logins() -> InvokeSAPSiemResponse: |
| 497 | """ |
| 498 | Invokes the SAP SIEM integration for successful login after multiple failed logins. |
| 499 | """ |
| 500 | logger.info("Invoking SAP SIEM integration for successful login after multiple failed logins scheduled job.") |
| 501 | customer_codes = [] |
| 502 | async with get_db_session() as session: |
| 503 | stmt = select(CustomerIntegrations).where( |
| 504 | CustomerIntegrations.integration_service_name == "SAP SIEM", |
| 505 | ) |
| 506 | result = await session.execute(stmt) |
| 507 | customer_codes = [row.customer_code for row in result.scalars()] |
| 508 | logger.info(f"customer_codes: {customer_codes}") |
| 509 | for customer_code in customer_codes: |
| 510 | extra_data = ( |
| 511 | await get_scheduled_job_metadata("invoke_sap_siem_integration_successful_login_after_multiple_failed_logins") |
| 512 | ).extra_data |
| 513 | if extra_data is not None: |
| 514 | data_parts = extra_data.split(",") |
| 515 | for part in data_parts: |
| 516 | key, value = part.split("=") |
| 517 | if key == "threshold": |
| 518 | threshold = int(value) |
| 519 | elif key == "time_range": |
| 520 | time_range = int(value) |
| 521 | else: |
| 522 | threshold = 0 |
| 523 | time_range = 3 |
| 524 | await invoke_sap_siem_successful_login_after_multiple_failed_logins_route( |
| 525 | invoke_siem_analysis=InvokeSapSiemAnalysis( |
| 526 | threshold=threshold, |
| 527 | time_range=time_range, |
| 528 | iris_customer_id=(await get_customer_meta_attribute(customer_code, "customer_meta_iris_customer_id", session)), |
| 529 | ), |
| 530 | ) |
| 531 | # Close the session |
| 532 | await session.close() |
| 533 | with get_sync_db_session() as session: |
| 534 | # Synchronous ORM operations |
| 535 | job_metadata = ( |
| 536 | session.query(JobMetadata) |
| 537 | .filter_by(job_id="invoke_sap_siem_integration_successful_login_after_multiple_failed_logins") |
| 538 | .one_or_none() |
| 539 | ) |
| 540 | if job_metadata: |
| 541 | job_metadata.last_success = datetime.utcnow() |
| 542 | session.add(job_metadata) |
| 543 | session.commit() |
| 544 | else: |
| 545 | # Handle the case where job_metadata does not exist |
| 546 | print("JobMetadata for 'invoke_sap_siem_integration_successful_login_after_multiple_failed_logins' not found.") |
| 547 | |
| 548 | return InvokeSAPSiemResponse(success=True, message="SAP SIEM integration invoked for successful login after multiple failed logins.") |