Project: ThreatMapper (Provisioning / Security & Compliance)
File: python-sdk.md

title: How to Use Python Sdk A client library for accessing Deepfence ThreatMapper ```shell pip install git+https://github.com/deepfence/threatmapper-python-client.git ``` First, create a client: ```python from threatmapper import Client client = Client(baseurl="YOURCONSOLE_URL") ``` If the endpoints you're going to hit require authentication, use `AuthenticatedClient` instead: After Login Go to Settings -> User Management -> Api key ```python from threatmapper import AuthenticatedClient client = AuthenticatedClient(baseurl="YOURCONSOLE_URL", token="Api Key") ``` By default, when you're calling an HTTPS API it will attempt to verify that SSL is working correctly. Using certificate verification is highly recommended most of the time, but sometimes you may need to authenticate to a server (especially an internal server) using a custom certificate bundle. ```python from threatmapper import AuthenticatedClient client = AuthenticatedClient( baseurl="YOURCONSOLE_URL", token="Api Key", verifyssl="/path/to/certificatebundle.pem", ) ``` You can also disable certificate validation altogether, but beware that this is a security risk. ```python from threatmapper import AuthenticatedClient client = AuthenticatedClient( baseurl="YOURCONSOLE_URL", token="Api Key", verify_ssl=False ) ``` Things to know: Every path/method combo becomes a Python module with four functions: `sync`: Blocking request that returns parsed data (if successful) or `None` `sync_detailed`: Blocking request that always returns a `Request`, optionally with `parsed` set if the request was successful. `asyncio`: Like `sync` but async instead of blocking `asynciodetailed`: Like `syncdetailed` but async instead of blocking All path/query params, and bodies become method arguments. If your endpoint had any tags on it, the first tag will be used as a module name for the function (my_tag above) Any endpoint which did not have a tag will be in `threatmapper.api.default` There are more settings on the generated `Client` class which let you control more runtime behavior, check out the docstring on that class for more info. You can also customize the underlying `httpx.Client` or `httpx.AsyncClient` (depending on your use-case): ```python from threatmapper import Client def log_request(request): print(f"Request event hook: {request.method} {request.url} - Waiting for response") def log_response(response): request = response.request print(f"Response event hook: {request.method} {request.url} - Status {response.status_code}") client = Client( baseurl="YOURCONSOLE_URL", httpxargs={"eventhooks": {"request": [logrequest], "response": [logresponse]}}, ) ``` You can even set the httpx client directly, but beware that this will override any existing settings (e.g., base_url): ```python import httpx from threatmapper import Client client = Client( baseurl="YOURCONSOLE_URL", ) client.sethttpxclient(httpx.Client(baseurl="YOURCONSOLEURL", proxies="YOURPROXY_URL")) ``` ```python import json from threatmapper import Client from threatmapper.models import ModelApiAuthRequest from threatmapper.api.authentication import auth_token from threatmapper.errors import UnexpectedStatus client = Client(baseurl="YOURCONSOLEURL", verifyssl=False) client = Client(baseurl="YOURCONSOLEURL", verifyssl="/path/to/certificate_bundle.pem") def getaccessrefreshtokensync(): try: json_body = ModelApiAuthRequest( apitoken="YOURAPI_KEY" ) apiresponse = authtoken.sync(client=client, jsonbody=jsonbody) print(apiresponse.accesstoken, apiresponse.refreshtoken) apiresponse = authtoken.syncdetailed(client=client, jsonbody=json_body) if apiresponse.statuscode == 200: jsonresponse =

print(jsonresponse["accesstoken"], jsonresponse["refreshtoken"]) else: raise Exception("") except UnexpectedStatus as e: print("Exception when calling getaccessrefreshtokensync->: %s\n" % e) ``` Or do the same thing with an async version: ```python import json from threatmapper import Client from threatmapper.models import ModelApiAuthRequest from threatmapper.api.authentication import auth_token from threatmapper.errors import UnexpectedStatus client = Client(baseurl="YOURCONSOLEURL", verifyssl=False) client = Client(baseurl="YOURCONSOLEURL", verifyssl="/path/to/certificate_bundle.pem") async def getaccessrefreshtokenasync(): try: json_body = ModelApiAuthRequest( apitoken="YOURAPI_KEY" ) apiresponse = await authtoken.asyncio(client=client, jsonbody=jsonbody) print(apiresponse.accesstoken, apiresponse.refreshtoken) apiresponse = await authtoken.asynciodetailed(client=client, jsonbody=json_body) if apiresponse.statuscode == 200: jsonresponse = json.loads(apiresponse.content.decode("utf-8")) print(jsonresponse["accesstoken"], jsonresponse["refreshtoken"]) else: raise Exception("") except UnexpectedStatus as e: print("Exception when calling getaccessrefreshtokenasync-> %s\n" % e) ``` ```python import json from threatmapper import AuthenticatedClient from threatmapper.api.authentication import authtokenrefresh from threatmapper.errors import UnexpectedStatus client = AuthenticatedClient(baseurl="YOURCONSOLEURL", token="YOURREFRESHTOKEN", verifyssl=False) client = AuthenticatedClient(baseurl="YOURCONSOLEURL", token="YOURREFRESH_TOKEN", verifyssl="/path/to/certificatebundle.pem") def refreshtokensync(): try: res = authtokenrefresh.sync(client=client) print(res.accesstoken, res.refreshtoken) res = authtokenrefresh.sync_detailed(client=client) if res.status_code == 200: json_response = json.loads(res.content.decode("utf-8")) print(jsonresponse["access-token"], jsonresponse["refresh-token"]) else: raise Exception("") except UnexpectedStatus as e: print("Exception when calling refreshtokensync-> %s\n" % e) ``` Or do the same thing with an async version: ```python from threatmapper import AuthenticatedClient from threatmapper.api.authentication import authtokenrefresh from threatmapper.errors import UnexpectedStatus import json client = AuthenticatedClient(baseurl="YOURCONSOLEURL", token="YOURREFRESHTOKEN", verifyssl=False) client = AuthenticatedClient(baseurl="YOURCONSOLEURL", token="YOURREFRESHTOKEN", verifyssl="/path/to/certificate_bundle.pem") async def refreshtokenasync(): try: res = await authtokenrefresh.asyncio(client=client) print(res.accesstoken, res.refreshtoken) res = await authtokenrefresh.asyncio_detailed(client=client) if res.status_code == 200: json_response = json.loads(res.content.decode("utf-8")) print(jsonresponse["access-token"], jsonresponse["refresh-token"]) else: raise Exception("") except UnexpectedStatus as e: print("Exception when calling refreshtokenasync-> %s\n" % e) ``` ```python from threatmapper.types import File from threatmapper.api.registry import addregistrygcr from threatmapper.models import FormDataModelRegistryGCRAddReq from threatmapper import AuthenticatedClient from threatmapper.errors import UnexpectedStatus client = AuthenticatedClient(baseurl="YOURCONSOLEURL", token="YOURREFRESHTOKEN", verifyssl=False) client = AuthenticatedClient(baseurl="YOURCONSOLEURL", token="YOURREFRESHTOKEN", verifyssl="/path/to/certificate_bundle.pem") def addgcrregistry(): try: name = 'Google Registry' registryurl = 'YOURREGISTRY_URL' serviceaccountjson = File(payload=open('/path/to/json','rb'), mimetype="application/json", filename="service.json") multipartdata=FormDataModelRegistryGCRAddReq(name=name, registryurl=registryurl, serviceaccountjson=serviceaccount_json) response = addregistrygcr.sync(client=client, multipartdata=multipartdata) print(response.message) except UnexpectedStatus as e: print("Exception when calling refreshtokenasync-> %s\n" % e) ``` ```python from threatmapper.api.search import search_hosts from threatmapper.models import SearchSearchNodeReq from threatmapper import AuthenticatedClient from threatmapper.errors import UnexpectedStatus client = AuthenticatedClient(baseurl="YOURCONSOLEURL", token="YOURREFRESHTOKEN", verifyssl=False) client = AuthenticatedClient(baseurl="YOURCONSOLEURL", token="YOURREFRESHTOKEN", verifyssl="/path/to/certificate_bundle.pem") def list_hosts(): try: payload_dict = { "node_filter": { "filters": { "compare_filter": None, "contains_filter": { "filter_in": { "active": [ True ] } }, "match_filter": { "filter_in": None }, "notcontainsfilter": { "filter_in": {} }, "order_filter": { "order_fields": [] } }, "infieldfilter": None, "window": { "offset": 0, "size": 0 } }, "window": { "offset": 0, "size": 100 } } jsonbody = SearchSearchNodeReq.fromdict(payload_dict) hosts = searchhosts.sync(client=client,jsonbody=json_body) agenthostlist = [] discoveredhostlist = [] for host in hosts: if host.agent_running: agenthostlist.append(host.node_id) else: discoveredhostlist.append(host.node_id) print(agenthostlist, discoveredhostlist) except UnexpectedStatus as e: print("Exception when calling list_hosts-> %s\n" % e) ``` ```python from threatmapper.api.search import search_containers from threatmapper.models import SearchSearchNodeReq from threatmapper import AuthenticatedClient from

import UnexpectedStatus client = AuthenticatedClient(baseurl="YOURCONSOLEURL", token="YOURREFRESHTOKEN", verifyssl=False) client = AuthenticatedClient(baseurl="YOURCONSOLEURL", token="YOURREFRESHTOKEN", verifyssl="/path/to/certificate_bundle.pem") async def list_containers(): try: payload_dict = { "node_filter": { "filters": { "compare_filter": None, "contains_filter": { "filter_in": { "active": [ True ] } }, "match_filter": { "filter_in": None }, "notcontainsfilter": { "filter_in": {} }, "order_filter": { "order_fields": [] } }, "infieldfilter": None, "window": { "offset": 0, "size": 0 } }, "window": { "offset": 0, "size": 100 } } jsonbody = SearchSearchNodeReq.fromdict(payload_dict) containers = await searchcontainers.asyncio(client=client,jsonbody=json_body) for container in containers: print(container.nodeid, container.nodename) except UnexpectedStatus as e: print("Exception when calling list_containers-> %s\n" % e) ``` ```python from threatmapper.api.vulnerability import startvulnerabilityscan from threatmapper.api.search import search_hosts from threatmapper.models import ModelVulnerabilityScanTriggerReq, SearchSearchNodeReq, ModelScanTriggerResp from threatmapper import AuthenticatedClient from threatmapper.errors import UnexpectedStatus from typing import List client = AuthenticatedClient(baseurl="YOURCONSOLEURL", token="YOURREFRESHTOKEN", verifyssl=False) client = AuthenticatedClient(baseurl="YOURCONSOLEURL", token="YOURREFRESHTOKEN", verifyssl="/path/to/certificate_bundle.pem") def node_config() -> List: try: payload_dict = { "node_filter": { "filters": { "compare_filter": None, "contains_filter": { "filter_in": { "active": [ True ] } }, "match_filter": { "filter_in": None }, "notcontainsfilter": { "filter_in": {} }, "order_filter": { "order_fields": [] } }, "infieldfilter": None, "window": { "offset": 0, "size": 0 } }, "window": { "offset": 0, "size": 100 } } jsonbody = SearchSearchNodeReq.fromdict(payload_dict) hosts = searchhosts.sync(client=client,jsonbody=json_body) host_list = [] for host in hosts: hostlist.append({"nodeid":host.nodeid, "nodetype": "host"}) return host_list except UnexpectedStatus as e: print("Exception when calling node_config-> %s\n" % e) async def startvulnerabilityscanonhosts(): try: nodeids = nodeconfig() payload_dict = { "filters": { "cloudaccountscan_filter": { "filter_in": None }, "containerscanfilter": { "filter_in": None }, "hostscanfilter": { "filter_in": None }, "imagescanfilter": { "filter_in": None }, "kubernetesclusterscan_filter": { "filter_in": None } }, "nodeids": nodeids, "scan_config": [ { "language": "base" }, { "language": "java" }, { "language": "javascript" }, { "language": "rust" }, { "language": "golang" }, { "language": "ruby" }, { "language": "python" }, { "language": "php" }, { "language": "dotnet" } ] } jsonbody = ModelVulnerabilityScanTriggerReq.fromdict(payload_dict) response: ModelScanTriggerResp = await startvulnerabilityscan.asyncio(client=client,jsonbody=jsonbody) print(response.scanids, response.bulkscan_id) except UnexpectedStatus as e: print("Exception when calling startvulnerabilityscanonhosts-> %s\n" % e) ``` This project uses to manage dependencies and packaging. Here are the basics: Update the metadata in pyproject.toml (e.g. authors, version) If you're using a private repository, configure it with Poetry `poetry config repositories.<your-repository-name> <url-to-your-repository>` `poetry config http-basic.<your-repository-name> <username> <password>` Publish the client with `poetry publish --build -r <your-repository-name>` or, if for public PyPI, just `poetry publish --build` If you want to install this client into another project without publishing it (e.g. for development) then: If that project is using Poetry, you can simply do `poetry add <path-to-this-client>` from that project If that project is not using Poetry: Build a wheel with `poetry build -f wheel` Install that wheel from the other project `pip install <path-to-wheel>`