Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit f910ba5

Browse files
author
Samuel Hassine
committed
Merge branch 'master' of github.com:OpenCTI-Platform/client-python
2 parents e4beb5c + c5b0d17 commit f910ba5

File tree

8 files changed

+82
-43
lines changed

8 files changed

+82
-43
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ test.py
1212
examples/*.json
1313
examples/*.pdf
1414
docs/_build
15-
Pipfile*
15+
Pipfile*
16+
.mypy_cache/

docs/conf.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@
3434
# Add any Sphinx extension module names here, as strings. They can be
3535
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
3636
# ones.
37-
extensions = ["sphinx.ext.autodoc", "sphinx.ext.inheritance_diagram", "autoapi.sphinx"]
37+
extensions = [
38+
"sphinx.ext.autodoc",
39+
"sphinx.ext.inheritance_diagram",
40+
"autoapi.sphinx",
41+
"sphinx_autodoc_typehints",
42+
]
3843

3944
# Add any paths that contain templates here, relative to this directory.
4045
templates_path = ["_templates"]

docs/requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
autoapi>=2.0.1
1+
autoapi>=2.0.1
2+
sphinx_rtd_theme>=0.4.3
3+
sphinx-autodoc-typehints>=1.10.3

pycti/connector/opencti_connector_helper.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,21 @@
88
import uuid
99
import os
1010

11-
from typing import Callable, Dict, List, Union
11+
from typing import Callable, Dict, List, Optional, Union
1212
from pika.exceptions import UnroutableError, NackError
1313
from pycti.api.opencti_api_client import OpenCTIApiClient
1414
from pycti.connector.opencti_connector import OpenCTIConnector
1515

1616

1717
def get_config_variable(
18-
env_var: str, yaml_path: str, config={}, isNumber=False
19-
) -> Union[str, int]:
18+
env_var: str, yaml_path: str, config: Dict = {}, isNumber: Optional[bool] = False
19+
) -> Union[bool, int, None, str]:
2020
"""[summary]
2121
2222
:param env_var: environnement variable name
23-
:type env_var: str
2423
:param yaml_path: path to yaml config
25-
:type yaml_path: str
2624
:param config: client config dict, defaults to {}
27-
:type config: dict, optional
2825
:param isNumber: specify if the variable is a number, defaults to False
29-
:type isNumber: bool, optional
30-
:return: either a str or a int variable value
31-
:rtype: str or int
3226
"""
3327

3428
if os.getenv(env_var) is not None:

pycti/entities/opencti_identity.py

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# coding: utf-8
22

33
import json
4-
from pycti.utils.constants import CustomProperties
4+
from pycti.utils.constants import CustomProperties, IdentityTypes
55
from pycti.utils.opencti_stix2 import SPEC_VERSION
66

77

@@ -223,39 +223,53 @@ def create_raw(self, **kwargs):
223223
created_by_ref = kwargs.get("createdByRef", None)
224224
marking_definitions = kwargs.get("markingDefinitions", None)
225225
tags = kwargs.get("tags", None)
226+
organization_class = kwargs.get("organization_class", None)
226227

227228
if name is not None and description is not None:
228229
self.opencti.log("info", "Creating Identity {" + name + "}.")
229-
query = (
230+
231+
input_variables = {
232+
"name": name,
233+
"description": description,
234+
"alias": alias,
235+
"internal_id_key": id,
236+
"stix_id_key": stix_id_key,
237+
"created": created,
238+
"modified": modified,
239+
"createdByRef": created_by_ref,
240+
"markingDefinitions": marking_definitions,
241+
"tags": tags,
242+
}
243+
244+
if type == IdentityTypes.ORGANIZATION.value:
245+
query = f"""
246+
mutation OrganizationAdd($input: OrganizationAddInput) {{
247+
organizationAdd(input: $input) {{
248+
{self.properties}
249+
}}
250+
}}
230251
"""
231-
mutation IdentityAdd($input: IdentityAddInput) {
232-
identityAdd(input: $input) {
233-
"""
234-
+ self.properties
235-
+ """
236-
}
237-
}
238-
"""
239-
)
240-
result = self.opencti.query(
241-
query,
242-
{
243-
"input": {
244-
"name": name,
245-
"description": description,
246-
"alias": alias,
247-
"type": type,
248-
"internal_id_key": id,
249-
"stix_id_key": stix_id_key,
250-
"created": created,
251-
"modified": modified,
252-
"createdByRef": created_by_ref,
253-
"markingDefinitions": marking_definitions,
254-
"tags": tags,
255-
}
256-
},
252+
253+
input_variables["organization_class"] = organization_class
254+
255+
result_data_field = "organizationAdd"
256+
else:
257+
query = f"""
258+
mutation IdentityAdd($input: IdentityAddInput) {{
259+
identityAdd(input: $input) {{
260+
{self.properties}
261+
}}
262+
}}
263+
"""
264+
265+
input_variables["type"] = type
266+
267+
result_data_field = "identityAdd"
268+
269+
result = self.opencti.query(query, {"input": input_variables,},)
270+
return self.opencti.process_multiple_fields(
271+
result["data"][result_data_field]
257272
)
258-
return self.opencti.process_multiple_fields(result["data"]["identityAdd"])
259273
else:
260274
self.opencti.log("error", "Missing parameters: name and description")
261275

@@ -278,13 +292,17 @@ def create(self, **kwargs):
278292
created_by_ref = kwargs.get("createdByRef", None)
279293
marking_definitions = kwargs.get("markingDefinitions", None)
280294
tags = kwargs.get("tags", None)
295+
organization_class = kwargs.get("organization_class", None)
281296
update = kwargs.get("update", False)
282297
custom_attributes = """
283298
id
284299
entity_type
285300
name
286301
description
287302
alias
303+
... on Organization {
304+
organization_class
305+
}
288306
createdByRef {
289307
node {
290308
id
@@ -326,6 +344,18 @@ def create(self, **kwargs):
326344
id=object_result["id"], key="alias", value=new_aliases
327345
)
328346
object_result["alias"] = new_aliases
347+
# organization_class
348+
if (
349+
organization_class is not None
350+
and "organization_class" in object_result
351+
and object_result["organization_class"] != organization_class
352+
):
353+
self.opencti.stix_domain_entity.update_field(
354+
id=object_result["id"],
355+
key="organization_class",
356+
value=organization_class,
357+
)
358+
object_result["organization_class"] = organization_class
329359
return object_result
330360
else:
331361
return self.create_raw(
@@ -340,6 +370,7 @@ def create(self, **kwargs):
340370
createdByRef=created_by_ref,
341371
markingDefinitions=marking_definitions,
342372
tags=tags,
373+
organization_class=organization_class,
343374
)
344375

345376
"""

pycti/entities/opencti_stix_domain_entity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ def read(self, **kwargs):
392392
)
393393
elif filters is not None:
394394
result = self.list(
395-
types=types, filters=filters, customAttribute=custom_attributes
395+
types=types, filters=filters, customAttributes=custom_attributes
396396
)
397397
if len(result) > 0:
398398
return result[0]

pycti/utils/opencti_stix2.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1736,6 +1736,9 @@ def create_identity(self, stix_object, extras, update=False):
17361736
if "marking_definitions_ids" in extras
17371737
else [],
17381738
tags=extras["tags_ids"] if "tags_ids" in extras else [],
1739+
organization_class=stix_object[CustomProperties.ORG_CLASS]
1740+
if CustomProperties.ORG_CLASS in stix_object
1741+
else None,
17391742
update=update,
17401743
)
17411744

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,8 @@ def run(self):
6262
"python-magic-bin==0.4.14;sys.platform=='win32'",
6363
],
6464
cmdclass={"verify": VerifyVersionCommand},
65-
extras_require={"dev": ["black", "wheel"]}, # Optional
65+
extras_require={
66+
"dev": ["black", "wheel",],
67+
"doc": ["autoapi", "sphinx_rtd_theme", "sphinx-autodoc-typehints"],
68+
}, # Optional
6669
)

0 commit comments

Comments
 (0)