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

Commit e44479a

Browse files
richard-julienSamuelHassine
authored andcommitted
[client] Add capability to add custom headers in opencti http client (#844)
1 parent eeab22f commit e44479a

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

pycti/api/opencti_api_client.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,25 @@
7171
from pycti.utils.opencti_stix2_utils import OpenCTIStix2Utils
7272

7373

74+
def build_request_headers(token: str, custom_headers: str, app_logger):
75+
headers_dict = {
76+
"User-Agent": "pycti/" + __version__,
77+
"Authorization": "Bearer " + token,
78+
}
79+
# Build and add custom headers
80+
if custom_headers is not None:
81+
for header_pair in custom_headers.strip().split(";"):
82+
if header_pair: # Skip empty header pairs
83+
try:
84+
key, value = header_pair.split(":", 1)
85+
headers_dict[key.strip()] = value.strip()
86+
except ValueError:
87+
app_logger.warning(
88+
"Ignored invalid header pair", {"header_pair": header_pair}
89+
)
90+
return headers_dict
91+
92+
7493
class File:
7594
def __init__(self, name, data, mime="text/plain"):
7695
self.name = name
@@ -109,14 +128,14 @@ def __init__(
109128
self,
110129
url: str,
111130
token: str,
112-
log_level="info",
131+
log_level: str = "info",
113132
ssl_verify: Union[bool, str] = False,
114133
proxies: Union[Dict[str, str], None] = None,
115-
json_logging=False,
116-
bundle_send_to_queue=True,
134+
json_logging: bool = False,
135+
bundle_send_to_queue: bool = True,
117136
cert: Union[str, Tuple[str, str], None] = None,
118-
auth=None,
119-
perform_health_check=True,
137+
custom_headers: str = None,
138+
perform_health_check: bool = True,
120139
):
121140
"""Constructor method"""
122141

@@ -138,17 +157,10 @@ def __init__(
138157
# Define API
139158
self.api_token = token
140159
self.api_url = url + "/graphql"
141-
self.request_headers = {
142-
"User-Agent": "pycti/" + __version__,
143-
"Authorization": "Bearer " + token,
144-
}
145-
146-
if auth is not None:
147-
self.session = requests.session()
148-
self.session.auth = auth
149-
else:
150-
self.session = requests.session()
151-
160+
self.request_headers = build_request_headers(
161+
token, custom_headers, self.app_logger
162+
)
163+
self.session = requests.session()
152164
# Define the dependencies
153165
self.work = OpenCTIApiWork(self)
154166
self.playbook = OpenCTIApiPlaybook(self)

pycti/connector/opencti_connector_helper.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,12 @@ def __init__(self, config: Dict, playbook_compatible=False) -> None:
902902
self.opencti_token = get_config_variable(
903903
"OPENCTI_TOKEN", ["opencti", "token"], config
904904
)
905+
self.opencti_custom_headers = get_config_variable(
906+
"OPENCTI_CUSTOM_HEADERS",
907+
["opencti", "custom_headers"],
908+
config,
909+
default=None,
910+
)
905911
self.opencti_ssl_verify = get_config_variable(
906912
"OPENCTI_SSL_VERIFY", ["opencti", "ssl_verify"], config, False, False
907913
)
@@ -1078,6 +1084,7 @@ def __init__(self, config: Dict, playbook_compatible=False) -> None:
10781084
self.log_level,
10791085
self.opencti_ssl_verify,
10801086
json_logging=self.opencti_json_logging,
1087+
custom_headers=self.opencti_custom_headers,
10811088
bundle_send_to_queue=self.bundle_send_to_queue,
10821089
)
10831090
# - Impersonate API that will use applicant id
@@ -1088,6 +1095,7 @@ def __init__(self, config: Dict, playbook_compatible=False) -> None:
10881095
self.log_level,
10891096
self.opencti_ssl_verify,
10901097
json_logging=self.opencti_json_logging,
1098+
custom_headers=self.opencti_custom_headers,
10911099
bundle_send_to_queue=self.bundle_send_to_queue,
10921100
)
10931101
self.connector_logger = self.api.logger_class(self.connect_name)

0 commit comments

Comments
 (0)