Skip to content

Commit

Permalink
Replaced misleading _country_ parameter with _reports_.
Browse files Browse the repository at this point in the history
  • Loading branch information
elsif2 committed Aug 2, 2022
1 parent f1ec7b7 commit ba7d24b
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions intelmq/bots/collectors/shadowserver/collector_reports_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
SPDX-License-Identifier: AGPL-3.0-or-later
"""
from datetime import datetime, timedelta
import json
import hashlib
import hmac
import re
Expand All @@ -22,12 +23,14 @@

class ShadowServerAPICollectorBot(CollectorBot, HttpMixin, CacheMixin):
"""
Connects to the Shadowserver API, requests a list of all the reports for a specific country and processes the ones that are new
Connects to the Shadowserver API, requests a list of all the reports for an organization and processes the ones that are new
Parameters:
api_key (str): Your Shadowserver API key
secret (str): Your Shadowserver API secret
country (str): The country you want to download reports for (i.e. 'austria')
country (str): DEPRECIATED The mailing list you want to download reports for (i.e. 'austria')
reports (list):
A list of strings or a comma-separated list of the mailing lists you want to process.
types (list):
A list of strings or a string of comma-separated values with the names of reporttypes you want to process. If you leave this empty, all the available reports will be downloaded and processed (i.e. 'scan', 'drones', 'intel', 'sandbox_connection', 'sinkhole_combined').
"""
Expand All @@ -36,23 +39,29 @@ class ShadowServerAPICollectorBot(CollectorBot, HttpMixin, CacheMixin):
api_key = None
secret = None
types = None
reports = None
rate_limit: int = 86400
redis_cache_db: int = 12
redis_cache_host: str = "127.0.0.1" # TODO: type could be ipadress
redis_cache_port: int = 6379
redis_cache_ttl: int = 864000 # 10 days
redis_cache_password: Optional[str] = None
_report_list = []

def init(self):
if self.api_key is None:
raise ValueError('No api_key provided.')
if self.secret is None:
raise ValueError('No secret provided.')
if self.country is None:
raise ValueError('No country provided.')

if isinstance(self.types, str):
self.types = self.types.split(',')
if isinstance(self.reports, str):
self._report_list = self.reports.split(',')
elif isinstance(self.reports, list):
self._report_list = self.reports

if self.country is not None and self.country not in self._report_list:
self.logger.warn("Deprecated parameter 'country' found. Please use 'reports' instead. The backwards-compatibility will be removed in IntelMQ version 4.0.0.")
self._report_list.append(self.country)

self.preamble = f'{{ "apikey": "{self.api_key}" '

Expand All @@ -61,7 +70,7 @@ def _headers(self, data):

def _reports_list(self, date=None):
"""
Get a list of all the reports shadowserver has for a specific country
Get a list of all the reports shadowserver has for an organization
via the reports/list endpoint. If a list of types is set in the
parameters, we only process reports with those types.
To be on the safe side regarding different calculations of timestamps,
Expand All @@ -76,8 +85,9 @@ def _reports_list(self, date=None):
dayafter = date + timedelta(1)

data = self.preamble
data += f',"report": ["{self.country}"] '
data += f',"date": "{daybefore.isoformat()}:{dayafter.isoformat()}" '
data += ',"date": "{}:{}" '.format(daybefore.isoformat(), dayafter.isoformat())
if len(self._report_list) > 0:
data += ',"reports": {}'.format(json.dumps(self._report_list))
data += '}'
self.logger.debug('Downloading report list with data: %s.', data)

Expand Down

0 comments on commit ba7d24b

Please sign in to comment.