xsoar-client 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of xsoar-client might be problematic. Click here for more details.

@@ -0,0 +1,3 @@
1
+ # This is a placeholder value for version. It will be updated
2
+ # in CI pipelines. Please do not edit this file
3
+ __version__ = "0.0.1"
@@ -0,0 +1,3 @@
1
+ # SPDX-FileCopyrightText: 2025-present Torbjørn Lium <torben@lium.org>
2
+ #
3
+ # SPDX-License-Identifier: MIT
@@ -0,0 +1,59 @@
1
+ from __future__ import annotations
2
+
3
+ import boto3
4
+ from packaging import version
5
+
6
+ SUPPORTED_STORES = ["S3"]
7
+
8
+
9
+ class ArtifactProvider:
10
+ def __init__(self, *, location: str = "S3", s3_bucket_name: str | None = None, verify_ssl: str | bool = True) -> None:
11
+ if location not in SUPPORTED_STORES:
12
+ msg = f"Artifact store {location} is not yet implemented."
13
+ raise NotImplementedError(msg)
14
+ self.artifacts_repo = location
15
+ self.s3_bucket_name = s3_bucket_name
16
+ self.verify_ssl = verify_ssl
17
+ self.boto3_session = boto3.session.Session() # pyright: ignore # noqa: PGH003
18
+ self.s3 = self.boto3_session.resource("s3")
19
+
20
+ def _is_available_s3(self, *, pack_id: str, pack_version: str) -> bool:
21
+ """Returns True if pack is available, False otherwise"""
22
+ key_name = f"content/packs/{pack_id}/{pack_version}/{pack_id}.zip"
23
+ try:
24
+ self.s3.Object(self.s3_bucket_name, key_name).load()
25
+ except Exception: # noqa: BLE001
26
+ return False
27
+ return True
28
+
29
+ def is_available(self, *, pack_id: str, pack_version: str) -> bool:
30
+ if self.artifacts_repo == "S3":
31
+ return self._is_available_s3(pack_id=pack_id, pack_version=pack_version)
32
+ raise NotImplementedError
33
+
34
+ def _download_s3(self, *, pack_id: str, pack_version: str) -> bytes:
35
+ """Downloads a custom content pack from AWS S3."""
36
+ key_name = f"content/packs/{pack_id}/{pack_version}/{pack_id}.zip"
37
+ obj = self.s3.Object(bucket_name=self.s3_bucket_name, key=key_name)
38
+ response = obj.get()
39
+ return response["Body"].read()
40
+
41
+ def download(self, *, pack_id: str, pack_version: str) -> bytes:
42
+ if self.artifacts_repo == "S3":
43
+ return self._download_s3(pack_id=pack_id, pack_version=pack_version)
44
+ raise NotImplementedError
45
+
46
+ def _get_latest_version_s3(self, pack_id: str) -> str:
47
+ b3_client = boto3.client("s3", verify=self.verify_ssl)
48
+ result = b3_client.list_objects_v2(
49
+ Bucket=self.s3_bucket_name,
50
+ Prefix=f"content/packs/{pack_id}/",
51
+ Delimiter="/",
52
+ )
53
+ version_list = [x["Prefix"].split("/")[3] for x in result.get("CommonPrefixes")]
54
+ return str(max(version_list, key=version.parse))
55
+
56
+ def get_latest_version(self, pack_id: str) -> str:
57
+ if self.artifacts_repo == "S3":
58
+ return self._get_latest_version_s3(pack_id=pack_id)
59
+ raise NotImplementedError
@@ -0,0 +1,321 @@
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import tarfile
5
+ import tempfile
6
+ from io import BytesIO, StringIO
7
+ from typing import TYPE_CHECKING, TypeAlias
8
+
9
+ import demisto_client
10
+ import demisto_client.demisto_api
11
+ import requests
12
+ from demisto_client.demisto_api.rest import ApiException
13
+ from packaging import version
14
+
15
+ from .artifact_provider import ArtifactProvider
16
+
17
+ if TYPE_CHECKING:
18
+ from requests.models import Response
19
+
20
+ JSONType: TypeAlias = dict | list | None
21
+
22
+ XSOAR_OLD_VERSION = 6
23
+ HTTP_CALL_TIMEOUT = 10
24
+
25
+ requests.packages.urllib3.disable_warnings()
26
+
27
+
28
+ class Client:
29
+ def __init__(
30
+ self,
31
+ *,
32
+ verify_ssl: bool | str = False,
33
+ api_token: str = "",
34
+ server_url: str = "",
35
+ xsiam_auth_id: str = "",
36
+ custom_pack_authors: list[str],
37
+ server_version: int,
38
+ artifacts_location: str = "S3",
39
+ s3_bucket_name: str = "",
40
+ ) -> None:
41
+ self.api_token = None
42
+ self.server_url = None
43
+ self.xsiam_auth_id = None
44
+ self.server_version = server_version
45
+ self.installed_packs = None
46
+ self.custom_pack_authors = custom_pack_authors
47
+ self.installed_expired = None
48
+ self._set_credentials(api_token, server_url, xsiam_auth_id)
49
+ self.http_timeout = HTTP_CALL_TIMEOUT
50
+ self.verify_ssl = verify_ssl
51
+ self.artifact_provider = ArtifactProvider(location=artifacts_location, s3_bucket_name=s3_bucket_name)
52
+ if self.server_version > XSOAR_OLD_VERSION:
53
+ self.demisto_py_instance = demisto_client.configure(
54
+ base_url=self.server_url,
55
+ api_key=self.api_token,
56
+ auth_id=self.xsiam_auth_id,
57
+ verify_ssl=self.verify_ssl,
58
+ )
59
+ else:
60
+ self.demisto_py_instance = demisto_client.configure(
61
+ base_url=self.server_url,
62
+ api_key=self.api_token,
63
+ verify_ssl=self.verify_ssl,
64
+ )
65
+
66
+ def _set_credentials(
67
+ self,
68
+ api_token: str,
69
+ server_url: str,
70
+ xsiam_auth_id: str,
71
+ ) -> None:
72
+ if api_token and server_url:
73
+ self.api_token = api_token
74
+ self.server_url = server_url
75
+ self.xsiam_auth_id = str(xsiam_auth_id)
76
+
77
+ """
78
+ This should be moved somewhere else
79
+ # We only use xsiam_auth_id for XSOAR 8. Assuming XSOAR 8 server if this variable contains any value
80
+ if int(self.server_version) > XSOAR_OLD_VERSION:
81
+ self.server_url = f"{server_url}/xsoar/public/v1"
82
+ """
83
+
84
+ return
85
+ if (api_token and not server_url) or (server_url and not api_token):
86
+ msg = "If api_token is specified in constructor, then server_url must also be specified (or vice versa)."
87
+ raise RuntimeError(msg)
88
+ try:
89
+ self.api_token = os.environ["DEMISTO_API_KEY"]
90
+ self.server_url = os.environ["DEMISTO_BASE_URL"]
91
+ self.xsiam_auth_id = os.environ.get("XSIAM_AUTH_ID", None)
92
+ except KeyError as ex:
93
+ msg = "Cannot find all required environment varaibles. Please refer to the docs for required environment variables."
94
+ raise ValueError(msg) from ex
95
+
96
+ def _make_request(
97
+ self,
98
+ *,
99
+ endpoint: str,
100
+ method: str,
101
+ json: JSONType = None,
102
+ files: dict[str, tuple[str, bytes, str]] | None = None,
103
+ data: dict | None = None,
104
+ ) -> Response:
105
+ """Wrapper for Requests. Sets the appropriate headers and authentication token."""
106
+ url = f"{self.server_url}{endpoint}"
107
+ headers = {
108
+ "Accept": "application/json",
109
+ "Authorization": self.api_token,
110
+ "Content-Type": "application/json",
111
+ }
112
+ if self.xsiam_auth_id:
113
+ headers["x-xdr-auth-id"] = self.xsiam_auth_id
114
+ return requests.request(
115
+ method=method,
116
+ url=url,
117
+ headers=headers,
118
+ json=json,
119
+ files=files,
120
+ data=data,
121
+ verify=self.verify_ssl,
122
+ timeout=self.http_timeout,
123
+ )
124
+
125
+ def _get_custom_content_bundle(self) -> dict[str, StringIO]:
126
+ endpoint = "/content/bundle"
127
+ response = self._make_request(endpoint=endpoint, method="GET")
128
+ loaded_files: dict[str, StringIO] = {}
129
+
130
+ with tarfile.open(fileobj=BytesIO(response.content), mode="r") as tar:
131
+ tar_members = tar.getmembers()
132
+
133
+ for file in tar_members:
134
+ file_name = file.name.lstrip("/")
135
+
136
+ if extracted_file := tar.extractfile(file):
137
+ file_data = StringIO(extracted_file.read().decode("utf-8"))
138
+ loaded_files[file_name] = file_data
139
+ return loaded_files
140
+
141
+ def download_item(self, item_type: str, item_id: str) -> bytes:
142
+ if item_type == "playbook":
143
+ endpoint = f"/{item_type}/{item_id}/yaml"
144
+ response = self._make_request(endpoint=endpoint, method="GET")
145
+ else:
146
+ msg = 'Uknown item_type selected for download. Must be one of ["playbook"]'
147
+ raise ValueError(msg)
148
+ response.raise_for_status()
149
+ return response.content
150
+
151
+ def is_pack_available(self, *, pack_id: str, version: str, custom: bool) -> bool:
152
+ if custom:
153
+ return self.artifact_provider.is_available(pack_id=pack_id, pack_version=version)
154
+ baseurl = "https://marketplace.xsoar.paloaltonetworks.com/content/packs"
155
+ path = f"/{pack_id}/{version}/{pack_id}.zip"
156
+ url = baseurl + path
157
+ response = requests.head(url, timeout=self.http_timeout)
158
+ if int(response.status_code) != 200: # noqa: PLR2004, SIM103
159
+ return False
160
+ return True
161
+
162
+ def attach_item(self, item_type: str, item_id: str) -> None:
163
+ if item_type == "playbook":
164
+ endpoint = f"/{item_type}/attach/{item_id}"
165
+ response = self._make_request(endpoint=endpoint, method="POST")
166
+ else:
167
+ msg = 'Uknown item_type selected. Must be one of ["playbook"]'
168
+ raise ValueError(msg)
169
+ response.raise_for_status()
170
+
171
+ def detach_item(self, item_type: str, item_id: str) -> None:
172
+ if item_type == "playbook":
173
+ endpoint = f"/{item_type}/detach/{item_id}"
174
+ response = self._make_request(endpoint=endpoint, method="POST")
175
+ else:
176
+ msg = 'Uknown item_type selected. Must be one of ["playbook"]'
177
+ raise ValueError(msg)
178
+ response.raise_for_status()
179
+
180
+ def test_connectivity(self) -> bool:
181
+ if self.server_version > XSOAR_OLD_VERSION: # noqa: SIM108
182
+ endpoint = "/xsoar/health"
183
+ else:
184
+ endpoint = "/health"
185
+ try:
186
+ response = self._make_request(endpoint=endpoint, method="GET")
187
+ response.raise_for_status()
188
+ except Exception as ex:
189
+ msg = "Failed to connect to XSOAR server."
190
+ raise ConnectionError(msg) from ex
191
+ return True
192
+
193
+ def get_installed_packs(self) -> list[dict]:
194
+ """Fetches a JSON blob containing a complete list of installed packages."""
195
+ if self.server_version > XSOAR_OLD_VERSION:
196
+ endpoint = "/xsoar/public/v1/contentpacks/metadata/installed"
197
+ else:
198
+ endpoint = "/contentpacks/metadata/installed"
199
+
200
+ if self.installed_packs is None:
201
+ response = self._make_request(
202
+ endpoint=endpoint,
203
+ method="GET",
204
+ )
205
+ response.raise_for_status()
206
+ self.installed_packs = response.json()
207
+ return self.installed_packs
208
+
209
+ def get_installed_expired_packs(self) -> list[dict]:
210
+ """Fetches a JSON blob containing a complete list of installed expired packages."""
211
+ if self.server_version > XSOAR_OLD_VERSION: # noqa: SIM108
212
+ endpoint = "/xsoar/contentpacks/installed-expired"
213
+ else:
214
+ endpoint = "/contentpacks/installed-expired"
215
+
216
+ if self.installed_expired is None:
217
+ response = self._make_request(
218
+ endpoint=endpoint,
219
+ method="GET",
220
+ )
221
+ response.raise_for_status()
222
+ self.installed_expired = response.json()
223
+ return self.installed_expired
224
+
225
+ def get_case(self, case_id: int) -> dict:
226
+ endpoint = "/incidents/search"
227
+ payload = {
228
+ "filter": {
229
+ "query": f"id:{case_id}",
230
+ },
231
+ }
232
+ response = self._make_request(endpoint=endpoint, json=payload, method="POST")
233
+ response.raise_for_status()
234
+ return response.json()
235
+
236
+ def create_case(self, data: dict) -> dict:
237
+ if self.server_version > XSOAR_OLD_VERSION: # noqa: SIM108
238
+ endpoint = "/xsoar/public/v1/incident"
239
+ else:
240
+ endpoint = "/incident"
241
+ response = self._make_request(endpoint=endpoint, json=data, method="POST")
242
+ response.raise_for_status()
243
+ return response.json()
244
+
245
+ def is_installed(self, *, pack_id: str = "", pack_version: str = "") -> bool:
246
+ installed_packs = self.get_installed_packs()
247
+ if not pack_version:
248
+ return any(item for item in installed_packs if item["id"] == pack_id)
249
+ return any(item for item in installed_packs if item["id"] == pack_id and item["currentVersion"] == pack_version)
250
+
251
+ def download_pack(self, pack_id: str, pack_version: str, custom: bool) -> bytes: # noqa: FBT001
252
+ if custom:
253
+ return self.artifact_provider.download(pack_id=pack_id, pack_version=pack_version)
254
+ """Downloads a upstream content pack from the official XSOAR marketplace."""
255
+ baseurl = "https://marketplace.xsoar.paloaltonetworks.com/content/packs"
256
+ path = f"/{pack_id}/{pack_version}/{pack_id}.zip"
257
+ url = baseurl + path
258
+ response = requests.get(url, timeout=HTTP_CALL_TIMEOUT)
259
+ response.raise_for_status()
260
+ return response.content
261
+
262
+ def delete(self, *, pack_id: str = "") -> bool:
263
+ raise NotImplementedError
264
+
265
+ def deploy_zip(self, *, filepath: str = "", skip_validation: bool = False, skip_verify: bool = False) -> bool:
266
+ params = {
267
+ "skip_validation": "true" if skip_validation else "false",
268
+ "skip_verify": "true" if skip_verify else "false",
269
+ }
270
+ self.demisto_py_instance.upload_content_packs(filepath, **params)
271
+ return True
272
+
273
+ def deploy_pack(self, *, pack_id: str, pack_version: str, custom: bool) -> bool:
274
+ """Downloads a content pack from upstream or artifacts repository (depending on `custom` bool argument)."""
275
+ params = {}
276
+ filedata = self.download_pack(pack_id=pack_id, pack_version=pack_version, custom=custom)
277
+ if custom:
278
+ params["skip_validation"] = "true"
279
+ params["skip_verify"] = "true"
280
+ else:
281
+ params["skip_validation"] = "false"
282
+ params["skip_verify"] = "false"
283
+
284
+ tmp = tempfile.NamedTemporaryFile() # noqa: SIM115
285
+ with open(tmp.name, "wb") as f: # noqa: PTH123
286
+ f.write(filedata)
287
+
288
+ try:
289
+ self.demisto_py_instance.upload_content_packs(tmp.name, **params)
290
+ except ApiException as ex:
291
+ print(f"Exception when calling DefaulApi->upload_content_packs: {ex!s}\n")
292
+ raise RuntimeError from ex
293
+ return True
294
+
295
+ def get_outdated_packs(self) -> list[dict]:
296
+ expired_packs = self.get_installed_expired_packs()
297
+ update_available = []
298
+ for pack in expired_packs:
299
+ if pack["author"] in self.custom_pack_authors:
300
+ latest_version = self.artifact_provider.get_latest_version(pack["id"])
301
+ if latest_version == pack["currentVersion"]:
302
+ continue
303
+ tmpobj = {
304
+ "id": pack["id"],
305
+ "currentVersion": pack["currentVersion"],
306
+ "latest": latest_version,
307
+ "author": pack["author"],
308
+ }
309
+ update_available.append(tmpobj)
310
+ elif not pack["updateAvailable"]:
311
+ continue
312
+ else:
313
+ tmpobj = {
314
+ "id": pack["id"],
315
+ "currentVersion": pack["currentVersion"],
316
+ "latest": max(list(pack["changelog"]), key=version.parse),
317
+ "author": "Upstream",
318
+ }
319
+ update_available.append(tmpobj)
320
+
321
+ return update_available
@@ -0,0 +1,49 @@
1
+ Metadata-Version: 2.4
2
+ Name: xsoar-client
3
+ Version: 0.0.1
4
+ Project-URL: Documentation, https://github.com/tlium/xsoar-client#readme
5
+ Project-URL: Issues, https://github.com/tlium/xsoar-client/issues
6
+ Project-URL: Source, https://github.com/tlium/xsoar-client
7
+ Author-email: Torbjørn Lium <torben@lium.org>
8
+ License-Expression: MIT
9
+ License-File: LICENSE.txt
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Programming Language :: Python
12
+ Classifier: Programming Language :: Python :: 3.8
13
+ Classifier: Programming Language :: Python :: 3.9
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Requires-Python: >=3.10
18
+ Requires-Dist: boto3>=1.36.17
19
+ Requires-Dist: demisto-py>=3.2.18
20
+ Requires-Dist: requests>=2.8.0
21
+ Description-Content-Type: text/markdown
22
+
23
+ # xsoar-client
24
+ -----
25
+
26
+ ## Table of Contents
27
+
28
+ - [Installation](#installation)
29
+ - [Configuration](#configuration)
30
+ - [License](#license)
31
+
32
+ ## Installation
33
+ 1. Install with pip:
34
+ ```
35
+ pip install xsoar-client
36
+ ```
37
+
38
+ ## Configuration
39
+ For the XSOAR client to work properly you need to have a few environment variables set:
40
+ - DEMISTO_API_KEY - API key for XSOAR
41
+ - DEMISTO_BASE_URL - URL to XSOAR
42
+ - AWS_PROJECT - must contain the name of the AWS profile configured below
43
+
44
+ This application requires that you store your deployment artifacts (XSOAR content packs) in an artifact repository somewhere. Currently only AWS S3 is supported.
45
+ You also need to be logged in to the proper AWS project.
46
+ Install `awscli` for your platform and make sure you are properly authenticated.
47
+
48
+ ## License
49
+ `xsoar-client` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
@@ -0,0 +1,8 @@
1
+ xsoar_client/__about__.py,sha256=DiDAOHkRpfB2jOE8zzqG_luTUb9SkYBLiRfaizWvC5U,132
2
+ xsoar_client/__init__.py,sha256=RjMY_4AMoQYO9urL0tOVXgAtc4nFUO-z0po9DDBuR5M,105
3
+ xsoar_client/artifact_provider.py,sha256=68TSKBGVwFCzNMvj1t-cC6hBRSJ3f_NxZZOyvk5uByg,2498
4
+ xsoar_client/xsoar_client.py,sha256=f8HTnxNTg5ULO0CWzF7pfkdQ70B_r6KHEs0O2BPVmNU,12536
5
+ xsoar_client-0.0.1.dist-info/METADATA,sha256=1JFgDTZJt5ELOobotqLYYfRmZBl0pvONS8fqCOdz3QQ,1722
6
+ xsoar_client-0.0.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
7
+ xsoar_client-0.0.1.dist-info/licenses/LICENSE.txt,sha256=l6xnqWKshqwwTXt6ayO6MX8Uvygq0YnkUuFTNnR3ba4,1097
8
+ xsoar_client-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.27.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-present Torbjørn Lium <torben@lium.org>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.