main
py 171 lines 4.63 KB
Raw
1 from datetime import datetime
2 from datetime import timedelta
3 from typing import Optional
4
5 from pydantic import BaseModel
6 from pydantic import Field
7 from pydantic import model_validator
8
9
10 class InvokeSapSiemRequest(BaseModel):
11 customer_code: str = Field(
12 ...,
13 description="The customer code.",
14 examples=["00002"],
15 )
16 integration_name: str = Field(
17 "SAP SIEM",
18 description="The integration name.",
19 examples=["SAP SIEM"],
20 )
21 threshold: Optional[int] = Field(
22 3,
23 description="Number of 'Invalid LoginID' before the first 'OK'",
24 )
25 time_range: Optional[str] = Field(
26 "15m",
27 pattern="^[1-9][0-9]*[mhdw]$",
28 description="Time range for the query (1m, 1h, 1d, 1w)",
29 )
30
31 lower_bound: str = None
32 upper_bound: str = None
33
34 @model_validator(mode="before")
35 @classmethod
36 def set_time_bounds(cls, values):
37 time_range = values.get("time_range")
38 if time_range:
39 unit = time_range[-1]
40 amount = int(time_range[:-1])
41
42 now = datetime.utcnow()
43
44 if unit == "m":
45 lower_bound = now - timedelta(minutes=amount)
46 elif unit == "h":
47 lower_bound = now - timedelta(hours=amount)
48 elif unit == "d":
49 lower_bound = now - timedelta(days=amount)
50 elif unit == "w":
51 lower_bound = now - timedelta(weeks=amount)
52
53 values["lower_bound"] = lower_bound.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
54 values["upper_bound"] = now.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
55 return values
56
57
58 class CustomerDetails(BaseModel):
59 customer_code: str = Field(
60 ...,
61 description="The customer code.",
62 examples=["00002"],
63 )
64 iris_customer_id: int = Field(
65 ...,
66 description="The customer ID in IRIS.",
67 examples=[1],
68 )
69
70
71 class InvokeSAPSiemResponse(BaseModel):
72 success: bool
73 message: str
74
75
76 class SapSiemAuthKeys(BaseModel):
77 API_KEY: str = Field(
78 ...,
79 description="YOUR API KEY",
80 examples=["3_yUWT3uDMs9E1N87r4Ey"],
81 )
82 SECRET_KEY: str = Field(
83 ...,
84 description="YOUR SECRET KEY",
85 examples=["4ijD6uMCca"],
86 )
87 USER_KEY: Optional[str] = Field(
88 None,
89 description="YOUR USER KEY",
90 examples=["AK9zAL"],
91 )
92 API_DOMAIN: str = Field(
93 ...,
94 description="YOUR API DOMAIN",
95 examples=["audit.eu1.gigya.com"],
96 )
97
98
99 class CollectSapSiemRequest(BaseModel):
100 customer_code: str = Field(
101 ...,
102 description="The customer code.",
103 examples=["00002"],
104 )
105 integration_name: str = Field(
106 "SAP SIEM",
107 description="The integration name.",
108 examples=["SAP SIEM"],
109 )
110 threshold: Optional[int] = Field(
111 3,
112 description="Number of 'Invalid LoginID' before the first 'OK'",
113 )
114 time_range: Optional[str] = Field(
115 "15m",
116 pattern="^[1-9][0-9]*[mhdw]$",
117 description="Time range for the query (1m, 1h, 1d, 1w)",
118 )
119
120 lower_bound: str = None
121 upper_bound: str = None
122 auth_keys: SapSiemAuthKeys = Field(
123 ...,
124 description="The authentication keys for the SAP SIEM integration.",
125 )
126 customer_details: CustomerDetails = Field(
127 ...,
128 description="The customer details.",
129 )
130
131 @model_validator(mode="before")
132 @classmethod
133 def set_time_bounds(cls, values):
134 time_range = values.get("time_range")
135 if time_range:
136 unit = time_range[-1]
137 amount = int(time_range[:-1])
138
139 now = datetime.utcnow()
140
141 if unit == "m":
142 lower_bound = now - timedelta(minutes=amount)
143 elif unit == "h":
144 lower_bound = now - timedelta(hours=amount)
145 elif unit == "d":
146 lower_bound = now - timedelta(days=amount)
147 elif unit == "w":
148 lower_bound = now - timedelta(weeks=amount)
149
150 values["lower_bound"] = lower_bound.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
151 values["upper_bound"] = now.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
152 return values
153
154 def to_dict(self):
155 return self.model_dump()
156
157
158 class InvokeSapSiemAnalysis(BaseModel):
159 threshold: int = Field(
160 0,
161 description="Number of 'Invalid LoginID' before the first 'OK'",
162 )
163 time_range: int = Field(
164 "15",
165 description="Time range for the query (1m, 1h, 1d, 1w)",
166 )
167 iris_customer_id: int = Field(
168 ...,
169 description="The customer ID in IRIS.",
170 examples=[1],
171 )