About
This document is a high-level guide to make requests to the new Audit API endpoint. Provided are some examples to make basic requests with code snippets.
Audit v2 Swagger Doc URL
https://api.virtru.com/audit/api/v1/swagger/index.html#/
Authentication
-
In order to authenticate to the audit-api, you need to be an admin within your org and you need an HMAC token which you will need to request from your Virtru representative.
-
HMAC auth is leveraged to pull data from the Audit API endpoint. Below you will find example scripts to authenticate and pull audit data based on header parameters given.
Code Examples
Below you can find example API requests based on date/time constraints. Within the Python example, leverage the timedelta() value to pull recent audit events based on your needs. You can find additional parameters to modify your API call in the linked documentation at the top of this page.
Python
from datetime import datetime,timezone,timedelta
from hashlib import sha256
import requests
import hmac
import os
import json
method = "GET"
from_time = (datetime.now(timezone.utc) - timedelta(days=7)).strftime('%Y-%m-%dT%H:%M:%SZ')
to_time = (datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ'))
path = f"/audit/api/v1/events?from={from_time}&to={to_time}"
queryParams = f"from={from_time}&to={to_time}"
host = "api.virtru.com"
now = datetime.now(timezone.utc).strftime('%a, %d %b %Y %H:%M:%S GMT')
payload={}
# Has Empty Body
hash = sha256(str("").encode())
bodyHex = hash.hexdigest()
messageToSign = """{0}
{1}
{2}
content-type:application/json; charset=utf-8
date:{3}
host:{4}
content-type;date;host
{5}""".format(method,path,queryParams,now,host,bodyHex)
signature = hmac.new(os.getenv("API_TOKEN").encode(), messageToSign.encode(), sha256).hexdigest()
headers = {
'Authorization': 'HMAC {0}:{1}'.format(os.getenv("API_TOKEN_ID"),signature),
'accept': 'application/json',
'X-Request-Limit': '100',
'X-Request-Page': '1',
'Date': datetime.now(timezone.utc).strftime('%a, %d %b %Y %H:%M:%S GMT'),
'Content-Type': 'application/json; charset=utf-8',
'X-Auth-Signedheaders': 'content-type;date;host',
'Host':host
}
response = requests.request(method, "https://{0}{1}".format(host,path), headers=headers, data=payload)
# Pretty-print the JSON data
json_data = json.loads(response.text)
with open('auditdata.json', 'w') as writeFile:
json.dump(json_data, writeFile, indent=4)