Skip to content

Commit aef8c4d

Browse files
committed
add additional examples and new README
1 parent 58011bb commit aef8c4d

7 files changed

Lines changed: 2642 additions & 0 deletions
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
#!/usr/bin/env python3
2+
"""
3+
JupiterOne Python SDK - Client Setup and Query Examples
4+
5+
This file demonstrates how to:
6+
1. Set up the JupiterOne client
7+
2. Execute basic queries
8+
3. Use pagination methods
9+
4. Handle deferred responses for large datasets
10+
"""
11+
12+
import os
13+
import time
14+
from jupiterone import JupiterOneClient
15+
16+
def setup_client():
17+
"""Set up JupiterOne client with credentials."""
18+
19+
# Method 1: Using environment variables (recommended for production)
20+
j1 = JupiterOneClient(
21+
account=os.getenv('JUPITERONE_ACCOUNT_ID'),
22+
token=os.getenv('JUPITERONE_API_TOKEN'),
23+
url=os.getenv('JUPITERONE_URL', 'https://graphql.us.jupiterone.io'),
24+
sync_url=os.getenv('JUPITERONE_SYNC_URL', 'https://api.us.jupiterone.io')
25+
)
26+
27+
# Method 2: Direct configuration (for testing/development)
28+
# j1 = JupiterOneClient(
29+
# account='your-account-id',
30+
# token='your-api-token',
31+
# url='https://graphql.us.jupiterone.io',
32+
# sync_url='https://api.us.jupiterone.io'
33+
# )
34+
35+
return j1
36+
37+
def basic_query_examples(j1):
38+
"""Demonstrate basic query operations."""
39+
40+
print("=== Basic Query Examples ===\n")
41+
42+
# 1. Simple entity query
43+
print("1. Finding all hosts:")
44+
hosts = j1.query_v1(query='FIND Host LIMIT 10')
45+
print(f"Found {len(hosts)} hosts\n")
46+
47+
# 2. Query with property filtering
48+
print("2. Finding Linux hosts:")
49+
linux_hosts = j1.query_v1(query='FIND Host WITH platform = "linux" LIMIT 5')
50+
print(f"Found {len(linux_hosts)} Linux hosts\n")
51+
52+
# 3. Query with relationships
53+
print("3. Finding hosts with applications:")
54+
hosts_with_apps = j1.query_v1(
55+
query='FIND Host AS h THAT HAS Application AS a RETURN h.displayName, a.displayName LIMIT 5'
56+
)
57+
print(f"Found {len(hosts_with_apps)} host-application relationships\n")
58+
59+
# 4. Tree query
60+
print("4. Tree query for host hierarchy:")
61+
tree_result = j1.query_v1(query='FIND Host RETURN TREE LIMIT 5')
62+
print(f"Tree query completed\n")
63+
64+
# 5. Query with deleted entities
65+
print("5. Query including deleted entities:")
66+
all_hosts = j1.query_v1(query='FIND Host LIMIT 5', include_deleted=True)
67+
print(f"Found {len(all_hosts)} hosts (including deleted)\n")
68+
69+
def pagination_examples(j1):
70+
"""Demonstrate different pagination methods."""
71+
72+
print("=== Pagination Examples ===\n")
73+
74+
# 1. Cursor-based pagination (recommended for large datasets)
75+
print("1. Cursor-based pagination:")
76+
cursor_result = j1._cursor_query(
77+
query="FIND (Device | Person)",
78+
max_workers=3 # Parallel processing
79+
)
80+
print(f"Cursor query found {len(cursor_result)} total results\n")
81+
82+
# 2. Limit and skip pagination
83+
print("2. Limit and skip pagination:")
84+
limit_skip_result = j1._limit_and_skip_query(
85+
query="FIND User",
86+
skip=0,
87+
limit=100
88+
)
89+
print(f"Limit/skip query completed\n")
90+
91+
# 3. Deferred response for very large datasets
92+
print("3. Deferred response for large datasets:")
93+
deferred_result = j1.query_with_deferred_response(
94+
query="FIND UnifiedDevice",
95+
polling_interval=30, # seconds
96+
max_retries=10
97+
)
98+
print(f"Deferred response query completed with {len(deferred_result)} results\n")
99+
100+
def complex_query_examples(j1):
101+
"""Demonstrate complex query patterns."""
102+
103+
print("=== Complex Query Examples ===\n")
104+
105+
# 1. Multi-step relationship traversal
106+
print("1. Multi-step relationship query:")
107+
complex_query = """
108+
FIND User AS u
109+
THAT HAS AccessPolicy AS ap
110+
THAT ALLOWS * AS resource
111+
WHERE resource.tag.Environment = 'production'
112+
RETURN u.displayName, ap.displayName, resource.displayName
113+
LIMIT 10
114+
"""
115+
complex_result = j1.query_v1(query=complex_query)
116+
print(f"Complex query found {len(complex_result)} results\n")
117+
118+
# 2. Aggregation query
119+
print("2. Aggregation query:")
120+
agg_query = """
121+
FIND Host AS h
122+
RETURN h.platform, count(h)
123+
ORDER BY count(h) DESC
124+
LIMIT 10
125+
"""
126+
agg_result = j1.query_v1(query=agg_query)
127+
print(f"Aggregation query completed\n")
128+
129+
# 3. Time-based query
130+
print("3. Time-based query:")
131+
time_query = """
132+
FIND Finding
133+
WITH createdOn > date.now - 7 days
134+
RETURN displayName, severity, createdOn
135+
ORDER BY createdOn DESC
136+
LIMIT 10
137+
"""
138+
time_result = j1.query_v1(query=time_query)
139+
print(f"Time-based query found {len(time_result)} recent findings\n")
140+
141+
def natural_language_to_j1ql(j1):
142+
"""Demonstrate natural language to J1QL conversion."""
143+
144+
print("=== Natural Language to J1QL Examples ===\n")
145+
146+
prompts = [
147+
"Find all AWS EC2 instances that are running and tagged as production",
148+
"Show me all databases that are not encrypted",
149+
"Find users who have admin access to production systems",
150+
"List all applications that haven't been updated in the last 30 days"
151+
]
152+
153+
for i, prompt in enumerate(prompts, 1):
154+
print(f"{i}. Prompt: {prompt}")
155+
try:
156+
result = j1.generate_j1ql(natural_language_prompt=prompt)
157+
print(f" Generated J1QL: {result['j1ql']}")
158+
except Exception as e:
159+
print(f" Error: {e}")
160+
print()
161+
162+
def main():
163+
"""Main function to run all examples."""
164+
165+
print("JupiterOne Python SDK - Client Setup and Query Examples")
166+
print("=" * 60)
167+
168+
try:
169+
# Set up client
170+
j1 = setup_client()
171+
print("✓ Client setup successful\n")
172+
173+
# Run examples
174+
basic_query_examples(j1)
175+
pagination_examples(j1)
176+
complex_query_examples(j1)
177+
natural_language_to_j1ql(j1)
178+
179+
print("✓ All examples completed successfully!")
180+
181+
except Exception as e:
182+
print(f"✗ Error: {e}")
183+
print("\nMake sure you have set the following environment variables:")
184+
print("- JUPITERONE_ACCOUNT_ID")
185+
print("- JUPITERONE_API_TOKEN")
186+
print("- JUPITERONE_URL (optional)")
187+
print("- JUPITERONE_SYNC_URL (optional)")
188+
189+
if __name__ == "__main__":
190+
main()

0 commit comments

Comments
 (0)