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

Commit 0acf3b8

Browse files
[client] Add capability to add custom headers in opencti http client (#844)
1 parent 907cc73 commit 0acf3b8

2 files changed

Lines changed: 42 additions & 22 deletions

File tree

pycti/api/opencti_api_client.py

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import datetime
44
import io
55
import json
6-
from typing import Union
6+
from typing import Dict, Optional, Tuple, Union
77

88
import magic
99
import requests
@@ -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
@@ -107,16 +126,16 @@ class OpenCTIApiClient:
107126

108127
def __init__(
109128
self,
110-
url,
111-
token,
112-
log_level="info",
113-
ssl_verify=False,
114-
proxies=None,
115-
json_logging=False,
116-
bundle_send_to_queue=True,
117-
cert=None,
118-
auth=None,
119-
perform_health_check=True,
129+
url: str,
130+
token: str,
131+
log_level: str = "info",
132+
ssl_verify: bool = False,
133+
proxies: Optional[Dict[str, str]] = None,
134+
json_logging: Optional[bool] = False,
135+
bundle_send_to_queue: Optional[bool] = True,
136+
cert: Optional[Union[str, Tuple[str, str]]] = None,
137+
custom_headers: Optional[str] = None,
138+
perform_health_check: Optional[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
@@ -777,6 +777,12 @@ def __init__(self, config: Dict, playbook_compatible=False) -> None:
777777
self.opencti_token = get_config_variable(
778778
"OPENCTI_TOKEN", ["opencti", "token"], config
779779
)
780+
self.opencti_custom_headers = get_config_variable(
781+
"OPENCTI_CUSTOM_HEADERS",
782+
["opencti", "custom_headers"],
783+
config,
784+
default=None,
785+
)
780786
self.opencti_ssl_verify = get_config_variable(
781787
"OPENCTI_SSL_VERIFY", ["opencti", "ssl_verify"], config, False, False
782788
)
@@ -922,6 +928,7 @@ def __init__(self, config: Dict, playbook_compatible=False) -> None:
922928
self.log_level,
923929
self.opencti_ssl_verify,
924930
json_logging=self.opencti_json_logging,
931+
custom_headers=self.opencti_custom_headers,
925932
bundle_send_to_queue=self.bundle_send_to_queue,
926933
)
927934
# - Impersonate API that will use applicant id
@@ -932,6 +939,7 @@ def __init__(self, config: Dict, playbook_compatible=False) -> None:
932939
self.log_level,
933940
self.opencti_ssl_verify,
934941
json_logging=self.opencti_json_logging,
942+
custom_headers=self.opencti_custom_headers,
935943
bundle_send_to_queue=self.bundle_send_to_queue,
936944
)
937945
self.connector_logger = self.api.logger_class(self.connect_name)

0 commit comments

Comments
 (0)