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

Commit 41a3b7e

Browse files
author
Samuel Hassine
authored
[client/connector] Introduce methods to store key/value in the Redis through the API (#32)
1 parent c7f037b commit 41a3b7e

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

pycti/api/opencti_api_client.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,37 @@ def health_check(self):
133133
return False
134134
return False
135135

136+
def get_key_value(self, key):
137+
query = """
138+
query KeyValue($key: String!) {
139+
keyValue(key: $key) {
140+
key
141+
value
142+
}
143+
}
144+
"""
145+
result = self.query(query, {'key': key})
146+
return result['data']['keyValue']
147+
148+
def set_key_value(self, key, value, timeout=None):
149+
logging.info('Setting KeyValue ' + key + ': ' + value + '...')
150+
query = """
151+
mutation KeyValueAdd($input: KeyValueAddInput) {
152+
keyValueAdd(input: $input) {
153+
key
154+
value
155+
}
156+
}
157+
"""
158+
result = self.query(query, {
159+
'input': {
160+
'key': key,
161+
'value': value,
162+
'timeout': timeout
163+
}
164+
})
165+
return result['data']['keyValueAdd']
166+
136167
def parse_stix(self, data):
137168
if 'createdByRef' in data and data['createdByRef'] is not None and 'node' in data['createdByRef']:
138169
data['createdByRef'] = data['createdByRef']['node']

pycti/connector/opencti_connector_helper.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ def log_info(self, msg):
149149
def date_now(self):
150150
return datetime.datetime.utcnow().replace(microsecond=0, tzinfo=datetime.timezone.utc).isoformat()
151151

152+
def set_key_value(self, key, value, timeout=None):
153+
return self.api.set_key_value(key, value, timeout)
154+
155+
def get_key_value(self, key):
156+
return self.api.get_key_value(key)['value']
157+
152158
# Push Stix2 helper
153159
def send_stix2_bundle(self, bundle, entities_types=None):
154160
if entities_types is None:

0 commit comments

Comments
 (0)