-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_usage.py
More file actions
147 lines (115 loc) Β· 5.55 KB
/
basic_usage.py
File metadata and controls
147 lines (115 loc) Β· 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
"""
Basic usage example for the Beagle Python SDK.
This example demonstrates the fundamental operations you can perform
with the Beagle SDK including project management, application testing,
and result retrieval.
"""
import os
import time
from beagle_sdk import BeagleClient, BeagleAPIError
def main():
# Initialize client with API key from environment variable
api_key = os.getenv('BEAGLE_API_KEY')
if not api_key:
print("Please set BEAGLE_API_KEY environment variable")
return
client = BeagleClient(api_key=api_key)
try:
# 1. List all projects
print("π Fetching all projects...")
projects = client.projects.list()
print(f"Found {len(projects.get('data', []))} projects")
if not projects.get('data'):
print("No projects found. Please create a project first.")
return
# Use the first project
project = projects['data'][0]
project_key = project['id']
print(f"Using project: {project['name']} ({project_key})")
# 2. List applications in the project
print(f"\nπ Fetching applications for project {project['name']}...")
applications = client.applications.list(project_key=project_key)
if not applications.get('data'):
print("No applications found in this project.")
return
# Use the first application
app = applications['data'][0]
app_token = app['application_token']
print(f"Using application: {app['name']} ({app_token})")
# 3. Get application details
print(f"\nπ Getting application details...")
app_details = client.applications.get(app_token)
print(f"Application URL: {app_details.get('url', 'N/A')}")
print(f"Application Status: {app_details.get('status', 'N/A')}")
# 4. Get application summary
print(f"\nπ Getting application summary...")
summary = client.applications.get_summary(app_token)
print(f"Total Vulnerabilities: {summary.get('total_vulnerabilities', 'N/A')}")
print(f"Critical Issues: {summary.get('critical_count', 'N/A')}")
print(f"High Issues: {summary.get('high_count', 'N/A')}")
# 5. Start a security test
print(f"\nπ Starting security test for {app['name']}...")
test_config = {
"scan_type": "quick",
"description": "SDK example test"
}
test_result = client.testing.start(app_token, test_config)
result_token = test_result.get('result_token')
if result_token:
print(f"Test started successfully. Result token: {result_token}")
# 6. Monitor test status
print("\nβ³ Monitoring test progress...")
max_wait_time = 300 # 5 minutes
wait_interval = 30 # 30 seconds
elapsed_time = 0
while elapsed_time < max_wait_time:
status = client.testing.get_status(app_token, result_token)
current_status = status.get('status', 'unknown')
progress = status.get('progress', 0)
print(f"Status: {current_status}, Progress: {progress}%")
if current_status in ['completed', 'failed', 'stopped']:
break
time.sleep(wait_interval)
elapsed_time += wait_interval
# 7. Get test results if completed
if current_status == 'completed':
print("\nπ Retrieving test results...")
results = client.results.get_json(app_token, result_token)
vulnerabilities = results.get('vulnerabilities', [])
print(f"Found {len(vulnerabilities)} vulnerabilities")
# Show top 5 vulnerabilities
for i, vuln in enumerate(vulnerabilities[:5], 1):
print(f" {i}. {vuln.get('title', 'Unknown')} "
f"(Severity: {vuln.get('severity', 'N/A')})")
# 8. Download PDF report
print("\nπ₯ Downloading PDF report...")
pdf_path = client.results.download_pdf(
app_token,
result_token,
f"beagle_report_{app['name'].replace(' ', '_')}.pdf"
)
print(f"Report saved to: {pdf_path}")
elif current_status == 'failed':
print("β Test failed")
elif elapsed_time >= max_wait_time:
print("β° Test is still running after 5 minutes")
# Stop the test
print("π Stopping the test...")
client.testing.stop(app_token)
else:
print("β Failed to start test")
# 9. Get running sessions
print(f"\nπ Getting running test sessions...")
running_sessions = client.testing.get_running_sessions()
active_count = len(running_sessions.get('data', []))
print(f"Active test sessions: {active_count}")
print("\nβ
Example completed successfully!")
except BeagleAPIError as e:
print(f"β Beagle API Error: {e}")
if hasattr(e, 'status_code'):
print(f"Status Code: {e.status_code}")
except Exception as e:
print(f"β Unexpected error: {e}")
if __name__ == "__main__":
main()