| 1 | from fastapi import HTTPException |
| 2 | from pydantic import BaseModel |
| 3 | from pydantic import Field |
| 4 | from pydantic import field_validator |
| 5 | |
| 6 | |
| 7 | class InvokeDuoRequest(BaseModel): |
| 8 | customer_code: str = Field( |
| 9 | ..., |
| 10 | description="The customer code.", |
| 11 | examples=["00002"], |
| 12 | ) |
| 13 | integration_name: str = Field( |
| 14 | "Duo", |
| 15 | description="The integration name.", |
| 16 | examples=["Duo"], |
| 17 | ) |
| 18 | |
| 19 | |
| 20 | class DuoAuthKeys(BaseModel): |
| 21 | API_HOSTNAME: str = Field( |
| 22 | ..., |
| 23 | description="The API key.", |
| 24 | examples=["123456"], |
| 25 | ) |
| 26 | INTEGRATION_KEY: str = Field( |
| 27 | ..., |
| 28 | description="The integration key.", |
| 29 | examples=["123456"], |
| 30 | ) |
| 31 | SECRET_KEY: str = Field( |
| 32 | ..., |
| 33 | description="The secret key.", |
| 34 | examples=["123456"], |
| 35 | ) |
| 36 | |
| 37 | |
| 38 | class InvokeDuoResponse(BaseModel): |
| 39 | success: bool = Field( |
| 40 | ..., |
| 41 | description="The success status.", |
| 42 | examples=[True], |
| 43 | ) |
| 44 | message: str = Field( |
| 45 | ..., |
| 46 | description="The message.", |
| 47 | examples=["Duo Events collected successfully."], |
| 48 | ) |
| 49 | |
| 50 | |
| 51 | class CollectDuo(BaseModel): |
| 52 | integration: str = Field(..., examples=["duo"]) |
| 53 | customer_code: str = Field(..., examples=["socfortress"]) |
| 54 | integration_key: str = Field(..., examples=["1234567890"]) |
| 55 | secret_key: str = Field(..., examples=["1234567890"]) |
| 56 | api_host: str = Field(..., examples=["api-1234567890.duosecurity.com"]) |
| 57 | api_endpoint: str = Field(..., examples=["/admin/v2/logs/authentication"]) |
| 58 | graylog_host: str = Field(..., examples=["127.0.0.1"]) |
| 59 | graylog_port: str = Field(..., examples=[12201]) |
| 60 | range: str = Field(..., examples=["15m"]) # New field for range |
| 61 | |
| 62 | @field_validator("integration") |
| 63 | @classmethod |
| 64 | def check_integration(cls, v): |
| 65 | if v != "duo": |
| 66 | raise HTTPException( |
| 67 | status_code=400, |
| 68 | detail="Invalid integration. Only 'duo' is supported.", |
| 69 | ) |
| 70 | return v |
| 71 | |
| 72 | @field_validator("range") |
| 73 | @classmethod |
| 74 | def validate_range(cls, v): |
| 75 | if not v.endswith(("m", "h", "d")): |
| 76 | raise HTTPException( |
| 77 | status_code=400, |
| 78 | detail="Invalid range. Use 'm' for minutes, 'h' for hours, or 'd' for days.", |
| 79 | ) |
| 80 | return v |