Veracode API Signing Library
The `veracode-api-signing` library provides an easy way to sign HTTP requests destined for the Veracode API Gateway using Hash-based Message Authentication Code (HMAC). This is a crucial security measure for authenticating with Veracode's REST and XML APIs. The current version is 26.4.0, and new releases are typically published every few months.
Warnings
- breaking As of September 2019, Veracode API authentication transitioned from username/password (basic authentication) to API ID and Key (HMAC signing) for XML APIs, and REST APIs have always required HMAC. Any code still using basic authentication will fail.
- gotcha Veracode API credentials should be stored in either a `~/.veracode/credentials` file or as environment variables, but not both simultaneously for the same configuration profile, as this can lead to unpredictable behavior.
- deprecated The Veracode XML Admin API was deprecated in June 2022 in favor of the Identity REST APIs, with support ending on June 30, 2023. While `veracode-api-signing` can still sign requests for XML APIs, new integrations should exclusively target the more modern REST APIs.
Install
-
pip install veracode-api-signing
Imports
- RequestsAuthPluginVeracodeHMAC
from veracode_api_signing.plugin_requests import RequestsAuthPluginVeracodeHMAC
Quickstart
import requests
import os
from veracode_api_signing.plugin_requests import RequestsAuthPluginVeracodeHMAC
# Veracode API credentials can be loaded from ~/.veracode/credentials or environment variables.
# For quickstart, using environment variables for demonstration. In production, prefer file.
api_id = os.environ.get('VERACODE_API_KEY_ID', '')
api_key_secret = os.environ.get('VERACODE_API_KEY_SECRET', '')
if not api_id or not api_key_secret:
print("WARNING: VERACODE_API_KEY_ID and VERACODE_API_KEY_SECRET environment variables are not set.")
print("Please set them or configure ~/.veracode/credentials file for successful authentication.")
# Exit or provide mock values for a non-failing example
api_id = 'YOUR_MOCK_API_ID'
api_key_secret = 'YOUR_MOCK_API_SECRET'
# The base URL for Veracode REST APIs. For US Commercial Region.
# Adjust for other regions if necessary (e.g., https://api.veracode.eu/appsec/v1)
api_base = "https://api.veracode.com/appsec/v1"
try:
# Make a GET request to an API endpoint, e.g., /applications
# The RequestsAuthPluginVeracodeHMAC automatically handles signing the request.
response = requests.get(api_base + "/applications", auth=RequestsAuthPluginVeracodeHMAC(api_key_id=api_id, api_key_secret=api_key_secret))
response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx)
print("Successfully fetched applications:")
print(response.json())
except requests.exceptions.HTTPError as e:
print(f"HTTP Error: {e.response.status_code} - {e.response.text}")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")