| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
A modern Python client library for Rapid7 InsightVM API. Built with industry-standard patterns, comprehensive type hints, and a clean, intuitive interface.
# Clone the repository
git clone https://github.com/talltechy/insightvm-python.git
cd insightvm-python
# Create virtual environment
python3 -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
pip install -r requirements.txtCreate a .env file in the project root (see .env.example for template).
Do NOT commit real credentials. Use .env.example as a template for local development only, and use your platform's secret manager (GitHub Secrets, AWS Secrets Manager, Azure Key Vault, etc.) for CI and production settings.
Example placeholders (do not commit real values):
# Rapid7 InsightVM API (placeholders - DO NOT COMMIT)
INSIGHTVM_API_USERNAME=<INSIGHTVM_API_USERNAME>
INSIGHTVM_API_PASSWORD=<INSIGHTVM_API_PASSWORD>
INSIGHTVM_BASE_URL=https://your-console:3780
# SSL Configuration (optional)
INSIGHTVM_VERIFY_SSL=false # Set to false only for trusted development/testing environmentsSee .github/copilot-instructions.md for Copilot-specific guidance on generating examples and handling secrets safely.
from rapid7 import InsightVMClient
# Create client (loads credentials from environment)
with InsightVMClient() as client:
# Asset Management
assets = client.assets.list(page=0, size=100)
print(f"Found {len(assets['resources'])} assets")
# Scan Operations
scan_id = client.scans.start_site_scan(
site_id=123,
scan_name="Security Audit"
)
print(f"Started scan: {scan_id}")
# Report Generation
content = client.reports.generate_and_download(
report_id=42,
timeout=3600
)
with open("security_report.pdf.gz", "wb") as f:
f.write(content)
# Asset Group Management
group = client.asset_groups.create_high_risk(
name="Critical Assets",
threshold=25000
)
print(f"Created group: {group['name']}")insightvm-python/ ├── src/rapid7/ # Main package │ ├── auth.py # Authentication classes │ ├── client.py # InsightVMClient │ ├── config.py # Configuration management │ ├── constants.py # API constants │ ├── ui.py # User interface utilities │ └── api/ # API modules │ ├── base.py # BaseAPI foundation │ ├── assets.py # Asset operations │ ├── asset_groups.py # Asset group operations │ ├── scans.py # Scan management │ ├── reports.py # Report generation │ ├── sites.py # Site management │ └── sonar_queries.py # Sonar integration ├── docs/ # Documentation │ ├── API_REFERENCE.md │ ├── SCANS_API.md │ ├── REPORTS_API.md │ └── ... ├── requirements.txt # Dependencies ├── .env.example # Configuration template └── SECURITY.md # Security policy
BaseAPI Inheritance - All API modules inherit from a common base class:
from rapid7.api.base import BaseAPI
class ScansAPI(BaseAPI):
MAX_PAGE_SIZE = 500 # Optimization constant
def list(self, page=0, size=500):
size = min(size, self.MAX_PAGE_SIZE)
return self._request('GET', 'scans', params={'page': page, 'size': size})Unified Client - Single entry point with sub-clients:
client = InsightVMClient()
client.assets.list() # Asset operations
client.asset_groups.list() # Asset group operations
client.scans.list() # Scan operations
client.reports.list() # Report operations
client.sites.list() # Site operations
client.sonar_queries.list() # Sonar operationsfrom rapid7 import InsightVMClient
# Explicit credentials
client = InsightVMClient(
username="admin",
password="password",
base_url="https://console:3780",
verify_ssl=False,
timeout=(10, 90) # (connect, read) timeouts
)# Start a scan for a site
scan_id = client.scans.start_site_scan(
site_id=123,
scan_name="Monthly Security Scan",
scan_template_id="full-audit-without-web-spider"
)
# Monitor scan progress
scan = client.scans.get_scan(scan_id)
print(f"Status: {scan['status']}")
print(f"Progress: {scan.get('tasks', {}).get('pending', 0)} tasks pending")
# Wait for completion
final_scan = client.scans.wait_for_completion(
scan_id,
poll_interval=60,
timeout=7200
)
# Stop a running scan if needed
client.scans.stop_scan(scan_id)# List available report templates
templates = client.reports.get_templates()
for template in templates['resources']:
print(f"{template['id']}: {template['name']}")
# Generate and download a report
content = client.reports.generate_and_download(
report_id=42,
poll_interval=30,
timeout=3600
)
# Save the report (usually GZip compressed)
with open("vulnerability_report.pdf.gz", "wb") as f:
f.write(content)
# Or manage report generation manually
instance_id = client.reports.generate(report_id=42)
client.reports.wait_for_completion(42, instance_id)
report_content = client.reports.download(42, instance_id)# Get all assets (handles pagination automatically)
all_assets = client.assets.get_all(batch_size=500)
print(f"Total assets: {len(all_assets)}")
# Get all scans across all pages
all_scans = client.scans.get_all_scans()
print(f"Total scans: {len(all_scans)}")
# Get all reports
all_reports = client.reports.get_all_reports()
print(f"Total reports: {len(all_reports)}")# Search for high-risk Windows servers
results = client.assets.search({
"filters": [
{"field": "risk-score", "operator": "is-greater-than", "value": 20000},
{"field": "operating-system", "operator": "contains", "value": "Windows Server"}
],
"match": "all"
})
# Filter scans by status
active_scans = client.scans.list(active=True)import requests
try:
client = InsightVMClient()
# Start a scan
scan_id = client.scans.start_site_scan(site_id=123)
# Wait for completion with timeout
result = client.scans.wait_for_completion(
scan_id,
timeout=3600
)
except ValueError as e:
print(f"Configuration error: {e}")
except TimeoutError as e:
print(f"Operation timed out: {e}")
except requests.exceptions.RequestException as e:
print(f"API error: {e}")This project enforces a strict secrets policy to avoid accidental credential leaks:
⚠️ Self-Signed Certificates: When using verify_ssl=False, you bypass SSL certificate validation. Only use this in trusted environments with self-signed certificates.
See SECURITY.md for complete security policy and vulnerability reporting.
# Install test dependencies (included in requirements.txt)
pip install -r requirements.txt
# Run all tests
pytest
# Run with coverage report (local)
pytest --cov=src --cov-report=html
# Run specific test files
pytest tests/test_auth.py
pytest tests/test_client.py
pytest tests/test_rapid7/
# Run tests in verbose mode
pytest -v
# Run tests with coverage and open HTML report
pytest --cov=src --cov-report=html && open htmlcov/index.htmlFor testing in an environment matching CI/CD:
# Build and run tests in Docker
./.docker-test.sh
# Or manually:
docker build -f Dockerfile.test -t insightvm-test:local .
docker run --rm insightvm-test:localThis provides a consistent testing environment that matches the GitHub Actions workflow.
This project includes automated test coverage reporting with Codacy integration:
Current Coverage Targets: 30-40% baseline with room for expansion
tests/
├── __init__.py
├── conftest.py # Shared fixtures and utilities
├── test_auth.py # Authentication module tests
├── test_client.py # Client initialization tests
└── test_rapid7/ # Rapid7 API module tests
├── __init__.py
├── test_base.py # Base API functionality
└── test_assets.py # Assets API examples
The v2.0 release has been tested against live InsightVM instances:
Core Refactoring:
Sprint 3: Core Operations (COMPLETE - 100%):
Previously Supported:
Breaking Changes:
Contributions are welcome! Please read CONTRIBUTING.md for guidelines.
Sprint 3: Core Operations ✅ COMPLETE (100%)
Sprint 4: Vulnerabilities & Remediation (NEXT - High Priority)
This project is licensed under the MIT License - see the LICENSE file for details.
For issues, questions, or contributions, please:
Note: This is v2.0 with breaking changes from v1.0. See MIGRATION.md for upgrade instructions.
| Back | FazBrowse Home | New Git URL |