Skip to content

Commit

Permalink
Merge pull request #288 from bsummers-tc/develop
Browse files Browse the repository at this point in the history
Update for changes in TC V3 API (7.x)
  • Loading branch information
bsummers-tc authored Feb 7, 2023
2 parents b4d8ccd + 82d601a commit e480694
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
24 changes: 12 additions & 12 deletions tcex/api/tc/utils/threat_intel_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,19 +205,29 @@ def resolve_variables(self, inputs: List[str]) -> List[str]:
"""
resolved_inputs = []
for input_ in inputs:
# handle null inputs
if not input_:
resolved_inputs.append(None)
continue
if input_.strip() not in self.resolvable_variables:

# clean up input
input_ = input_.strip()

# handle unknown input types
if input_ not in self.resolvable_variables:
resolved_inputs.append(input_)
continue
input_ = input_.strip()

# special handling of group types (no API request required)
if input_ == '${GROUP_TYPES}':
for type_ in self.group_types:
resolved_inputs.append(type_)
continue

# get variable settings
resolvable_variable_details = self.resolvable_variables[input_]

# make API call to retrieve variable data
r = self.session_tc.get(
resolvable_variable_details.get('url'), params={'resultLimit': 10_000}
)
Expand All @@ -226,16 +236,6 @@ def resolve_variables(self, inputs: List[str]) -> List[str]:
raise RuntimeError(f'Could not retrieve {input_} from ThreatConnect API.')

json_ = r.json()
# No TQL filter to filter out API users during REST call so have to do it manually here.
if input_ in ['${API_USERS}', '${USERS}']:
temp_data = []
for item in json_.get('data', []):
if item.get('role') == 'Api User' and input_ == '${API_USERS}':
temp_data.append(item)
elif item.get('role') != 'Api User' and input_ == '${USERS}':
temp_data.append(item)
json_['data'] = temp_data

for item in jmespath.search(resolvable_variable_details.get('jmspath'), json_):
resolved_inputs.append(str(item))

Expand Down
7 changes: 4 additions & 3 deletions tests/api/tc/v2/threat_intelligence/ti_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# first-party
from tcex import TcEx
from tcex.api.tc.v2.threat_intelligence import ThreatIntelligence
from tcex.api.tc.v2.threat_intelligence.mappings.indicator.indicator import Indicator


class TIHelper:
Expand Down Expand Up @@ -238,7 +239,7 @@ def create_group(self, **kwargs):

return ti

def create_indicator(self, indicator_type=None, **kwargs):
def create_indicator(self, indicator_type=None, **kwargs) -> 'Indicator':
"""Create an case.
If a case_name is not provide a dynamic case name will be used.
Expand Down Expand Up @@ -479,7 +480,7 @@ class TestThreatIntelligence:
owner = None
required_fields = {}
ti = None
ti_helper = None
ti_helper: TIHelper

def teardown_method(self):
"""Clean up resources"""
Expand Down Expand Up @@ -766,7 +767,7 @@ def indicator_add_attribute(self, request):
ti_data = response_data.get('data', {}).get('attribute')

# assert response
assert r.status_code == 201
assert r.status_code == 201, f'(status-code={r.status_code}, message={r.text})'
assert response_data.get('status') == 'Success'

# validate ti data
Expand Down

0 comments on commit e480694

Please sign in to comment.