From 2951bc6e7b48e6019f8e8d3da5ced6945c08fc46 Mon Sep 17 00:00:00 2001 From: rshipp Date: Thu, 28 Jun 2018 16:28:50 -0500 Subject: [PATCH] Deprecate Cuckoo host/port/api_path for url Support for Cuckoo host/port/api_path will be removed in a future version. --- README.rst | 6 +++--- sandboxapi/cuckoo.py | 22 +++++++++++++++++++--- tests/tests.py | 15 ++++++++++++++- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index a3107e9..fa2372e 100644 --- a/README.rst +++ b/README.rst @@ -104,7 +104,7 @@ the same way: # connect to the sandbox sandboxes = [ - cuckoo.CuckooAPI('192.168.0.20'), + cuckoo.CuckooAPI('http://192.168.0.20:8090/'), fireeye.FireEyeAPI('myusername', 'mypassword', 'https://192.168.0.21', 'winxp-sp3'), joe.JoeAPI('mykey', 'https://jbxcloud.joesecurity.org/api', True) ] @@ -136,11 +136,11 @@ Cuckoo Sandbox Constructor signature:: - CuckooAPI(host, port=8090, api_path='/', verify_ssl=False) + CuckooAPI(url, verify_ssl=False) Example:: - CuckooAPI('192.168.0.20') + CuckooAPI('http://192.168.0.20:8090/') This library attempts to support any Cuckoo-like API, including older 1.x installations (though those without a score won't be able to use the ``.score`` diff --git a/sandboxapi/cuckoo.py b/sandboxapi/cuckoo.py index b822643..c9926d6 100644 --- a/sandboxapi/cuckoo.py +++ b/sandboxapi/cuckoo.py @@ -8,11 +8,27 @@ class CuckooAPI(sandboxapi.SandboxAPI): """Cuckoo Sandbox API wrapper.""" - def __init__(self, host, port=8090, api_path='/', verify_ssl=False, **kwargs): - """Initialize the interface to Cuckoo Sandbox API with host and port.""" + def __init__(self, url, port=8090, api_path='/', verify_ssl=False, **kwargs): + """Initialize the interface to Cuckoo Sandbox API with host and port. + + :type url: str + :param url: Cuckoo API URL. (Currently treated as host if not a fully formed URL - + this will be removed in a future version.) + :type port: int + :param port: DEPRECATED! Use fully formed url instead. Will be removed in future version. + :type api_path: str + :param api_path: DEPRECATED! Use fully formed url instead. Will be removed in future version. + """ sandboxapi.SandboxAPI.__init__(self, **kwargs) - self.api_url = 'http://' + host + ':' + str(port) + api_path + # NOTE: host/port/api_path support is DEPRECATED! + if url.startswith('http://') or url.startswith('https://'): + # Assume new-style url param. Ignore port and api_path. + self.api_url = url + else: + # This is for backwards compatability and will be removed in a future version. + self.api_url = 'http://' + url + ':' + str(port) + api_path + self.verify_ssl = verify_ssl # assume Cuckoo is *not* available. diff --git a/tests/tests.py b/tests/tests.py index 13ccefc..2372cb8 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -23,7 +23,7 @@ def read_resource(resource): class TestCuckoo(unittest.TestCase): def setUp(self): - self.sandbox = sandboxapi.cuckoo.CuckooAPI('cuckoo.mock') + self.sandbox = sandboxapi.cuckoo.CuckooAPI('http://cuckoo.mock:8090/') @responses.activate def test_analyses(self): @@ -95,6 +95,19 @@ def test_proxies_is_passed_to_requests(self, m_get, m_post): files=None, proxies=proxies, verify=MOCK_ANY) + @responses.activate + def test_cuckoo_old_style_host_port_path(self): + sandbox = sandboxapi.cuckoo.CuckooAPI('cuckoo.mock') + responses.add(responses.GET, 'http://cuckoo.mock:8090/tasks/list', + json=read_resource('cuckoo_tasks_list')) + self.assertEquals(len(self.sandbox.analyses()), 2) + + sandbox = sandboxapi.cuckoo.CuckooAPI('cuckoo.mock', 9090, '/test') + responses.add(responses.GET, 'http://cuckoo.mock:9090/test/tasks/list', + json=read_resource('cuckoo_tasks_list')) + self.assertEquals(len(self.sandbox.analyses()), 2) + + class TestJoe(unittest.TestCase):