def establish_connection(collection: str): """establish a connection with the TAXII server. arguments: collection: string, the url of the collection with which to connect. returns: connection to the taxii collection """ try: with open('proxy.json', "r") as json_data: proxies = json.load(json_data) link = t2c._HTTPConnection() link.session.proxies.update(proxies) collection = t2c.Collection(collection, conn=link) tc_src = TAXIICollectionSource(collection) except: if verbose: print( "unable to find proxy file, falling back to standard callout... ", end="", flush=True) # Establish TAXII2 Collection instance for Enterprise ATT&CK collection collection = t2c.Collection(collection) # Supply the collection to TAXIICollection tc_src = TAXIICollectionSource(collection) return tc_src
def test_taxii_endpoint_raises_exception(): """Test exception is raised when conn and (user or pass) is provided""" conn = _HTTPConnection(user="******", password="******", verify=False) with pytest.raises(InvalidArgumentsError) as excinfo: _TAXIIEndpoint("https://example.com/api1/collections/", conn, "other", "test") assert "A connection and user/password may not both be provided." in str(excinfo.value)
def test_header_merging(): conn = _HTTPConnection() headers = conn._merge_headers({"AddedHeader": "addedvalue"}) assert headers == { "user-agent": DEFAULT_USER_AGENT, "addedheader": "addedvalue" }
def test_valid_content_type_for_connection(): """The server responded with charset=utf-8, but the media types are correct and first.""" responses.add(responses.GET, COLLECTION_URL, COLLECTIONS_RESPONSE, status=200, content_type=MEDIA_TYPE_TAXII_V20 + "; charset=utf-8") conn = _HTTPConnection(user="******", password="******", verify=False) conn.get("https://example.com/api1/collections/91a7b528-80eb-42ed-a74d-c6fbd5a26116/", MEDIA_TYPE_TAXII_V20, None)
def test_conn_post_kwarg_errors(): conn = _HTTPConnection() with pytest.raises(InvalidArgumentsError): conn.post(DISCOVERY_URL, data=1, json=2) with pytest.raises(InvalidArgumentsError): conn.post(DISCOVERY_URL, data=1, foo=2) with pytest.raises(InvalidArgumentsError): conn.post(DISCOVERY_URL, foo=1)
def test_invalid_content_type_for_connection(): responses.add(responses.GET, COLLECTION_URL, COLLECTIONS_RESPONSE, status=200, content_type=MEDIA_TYPE_TAXII_V20) with pytest.raises(TAXIIServiceException) as excinfo: conn = _HTTPConnection(user="******", password="******", verify=False) conn.get("https://example.com/api1/collections/91a7b528-80eb-42ed-a74d-c6fbd5a26116/", MEDIA_TYPE_TAXII_V20 + "; charset=utf-8", None) assert ("Unexpected Response. Got Content-Type: 'application/vnd.oasis.taxii+json; " "version=2.0' for Accept: 'application/vnd.oasis.taxii+json; version=2.0; " "charset=utf-8'") == str(excinfo.value)
def test_user_agent_enforcing3(): conn = _HTTPConnection(user_agent=None) headers = conn._merge_headers({"User-Agent": None}) assert headers["user-agent"] == DEFAULT_USER_AGENT
def test_user_agent_overriding(): conn = _HTTPConnection(user_agent="foo/1.0") headers = conn._merge_headers({"User-Agent": "bar/2.0"}) assert headers["user-agent"] == "bar/2.0"
def test_user_agent_defaulting(): conn = _HTTPConnection(user_agent="foo/1.0") headers = conn._merge_headers({}) # also test key access is case-insensitive assert headers["user-agent"] == "foo/1.0"
def test_header_merge_none(): conn = _HTTPConnection() headers = conn._merge_headers(None) assert headers == {"user-agent": DEFAULT_USER_AGENT}