Skip to content

Commit 014d97b

Browse files
committed
add new CFT methods and create usage example script
1 parent 2b229be commit 014d97b

File tree

4 files changed

+457
-172
lines changed

4 files changed

+457
-172
lines changed
Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Custom File Transfer (CFT) Integration Example
4+
5+
This script demonstrates how to use the JupiterOne Python client to:
6+
1. Get an upload URL for Custom File Transfer integration
7+
2. Upload a CSV file to the integration
8+
3. Invoke the integration to process the uploaded file
9+
10+
Prerequisites:
11+
- JupiterOne account with API access
12+
- Custom File Transfer integration instance configured
13+
- CSV file to upload
14+
15+
Usage:
16+
python 09_custom_file_transfer_example.py
17+
"""
18+
19+
import os
20+
import sys
21+
import time
22+
from pathlib import Path
23+
24+
# Add the parent directory to the path so we can import the jupiterone client
25+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
26+
27+
from jupiterone.client import JupiterOneClient
28+
29+
30+
def setup_client():
31+
"""Set up the JupiterOne client with credentials."""
32+
# You can set these as environment variables or replace with your actual values
33+
account = os.getenv('JUPITERONE_ACCOUNT')
34+
token = os.getenv('JUPITERONE_API_TOKEN')
35+
36+
if not account or not token:
37+
print("Error: Please set JUPITERONE_ACCOUNT and JUPITERONE_API_TOKEN environment variables")
38+
print("Example:")
39+
print("export JUPITERONE_ACCOUNT='your-account-id'")
40+
print("export JUPITERONE_API_TOKEN='your-api-token'")
41+
sys.exit(1)
42+
43+
try:
44+
client = JupiterOneClient(account=account, token=token)
45+
print(f"✅ Successfully connected to JupiterOne account: {account}")
46+
return client
47+
except Exception as e:
48+
print(f"❌ Failed to connect to JupiterOne: {e}")
49+
sys.exit(1)
50+
51+
52+
def get_cft_upload_url_example(client, integration_instance_id, filename, dataset_id):
53+
"""
54+
Example of getting an upload URL for Custom File Transfer integration.
55+
56+
Args:
57+
client: JupiterOne client instance
58+
integration_instance_id: ID of the CFT integration instance
59+
filename: Name of the file to upload
60+
dataset_id: Dataset ID for the upload
61+
"""
62+
print("\n" + "="*60)
63+
print("1. GETTING CFT UPLOAD URL")
64+
print("="*60)
65+
66+
try:
67+
print(f"Requesting upload URL for:")
68+
print(f" - Integration Instance ID: {integration_instance_id}")
69+
print(f" - Filename: {filename}")
70+
print(f" - Dataset ID: {dataset_id}")
71+
72+
upload_info = client.get_cft_upload_url(
73+
integration_instance_id=integration_instance_id,
74+
filename=filename,
75+
dataset_id=dataset_id
76+
)
77+
78+
print("✅ Upload URL obtained successfully!")
79+
print(f" - Upload URL: {upload_info['uploadUrl']}")
80+
print(f" - Expires In: {upload_info['expiresIn']} seconds")
81+
82+
return upload_info
83+
84+
except Exception as e:
85+
print(f"❌ Failed to get upload URL: {e}")
86+
return None
87+
88+
89+
def upload_cft_file_example(client, upload_url, file_path):
90+
"""
91+
Example of uploading a CSV file to Custom File Transfer integration.
92+
93+
Args:
94+
client: JupiterOne client instance
95+
upload_url: Upload URL obtained from get_cft_upload_url()
96+
file_path: Local path to the CSV file
97+
"""
98+
print("\n" + "="*60)
99+
print("2. UPLOADING CSV FILE")
100+
print("="*60)
101+
102+
try:
103+
# Check if file exists
104+
if not os.path.exists(file_path):
105+
print(f"❌ File not found: {file_path}")
106+
return None
107+
108+
# Check if file is CSV
109+
if not file_path.lower().endswith('.csv'):
110+
print(f"❌ File must be a CSV file. Got: {file_path}")
111+
return None
112+
113+
print(f"Uploading file:")
114+
print(f" - File path: {file_path}")
115+
print(f" - File size: {os.path.getsize(file_path)} bytes")
116+
print(f" - Upload URL: {upload_url}")
117+
118+
# Upload the file
119+
result = client.upload_cft_file(
120+
upload_url=upload_url,
121+
file_path=file_path
122+
)
123+
124+
print("✅ File upload completed!")
125+
print(f" - Status code: {result['status_code']}")
126+
print(f" - Success: {result['success']}")
127+
print(f" - Response headers: {result['headers']}")
128+
129+
if result['success']:
130+
print(" - File uploaded successfully to JupiterOne")
131+
else:
132+
print(f" - Upload failed. Response data: {result['response_data']}")
133+
134+
return result
135+
136+
except Exception as e:
137+
print(f"❌ Failed to upload file: {e}")
138+
return None
139+
140+
141+
def invoke_cft_integration_example(client, integration_instance_id):
142+
"""
143+
Example of invoking a Custom File Transfer integration instance.
144+
145+
Args:
146+
client: JupiterOne client instance
147+
integration_instance_id: ID of the CFT integration instance
148+
"""
149+
print("\n" + "="*60)
150+
print("3. INVOKING CFT INTEGRATION")
151+
print("="*60)
152+
153+
try:
154+
print(f"Invoicing integration instance:")
155+
print(f" - Integration Instance ID: {integration_instance_id}")
156+
157+
# Invoke the integration
158+
result = client.invoke_cft_integration(
159+
integration_instance_id=integration_instance_id
160+
)
161+
162+
if result == True:
163+
print("✅ Integration invoked successfully!")
164+
print(" - The integration is now processing the uploaded file")
165+
elif result == 'ALREADY_RUNNING':
166+
print("⚠️ Integration is already running")
167+
print(" - The integration instance is currently executing")
168+
else:
169+
print("❌ Integration invocation failed")
170+
print(" - Check the integration instance configuration")
171+
172+
return result
173+
174+
except Exception as e:
175+
print(f"❌ Failed to invoke integration: {e}")
176+
return None
177+
178+
179+
def complete_workflow_example(client, integration_instance_id, file_path, dataset_id):
180+
"""
181+
Complete workflow example combining all three methods.
182+
183+
Args:
184+
client: JupiterOne client instance
185+
integration_instance_id: ID of the CFT integration instance
186+
file_path: Local path to the CSV file
187+
dataset_id: Dataset ID for the upload
188+
"""
189+
print("\n" + "="*60)
190+
print("COMPLETE CFT WORKFLOW")
191+
print("="*60)
192+
193+
print("Starting complete Custom File Transfer workflow...")
194+
195+
# Step 1: Get upload URL
196+
upload_info = get_cft_upload_url_example(
197+
client, integration_instance_id, os.path.basename(file_path), dataset_id
198+
)
199+
200+
if not upload_info:
201+
print("❌ Workflow failed at step 1: Getting upload URL")
202+
return False
203+
204+
# Step 2: Upload file
205+
upload_result = upload_cft_file_example(client, upload_info['uploadUrl'], file_path)
206+
207+
if not upload_result or not upload_result['success']:
208+
print("❌ Workflow failed at step 2: Uploading file")
209+
return False
210+
211+
# Step 3: Invoke integration
212+
invoke_result = invoke_cft_integration_example(client, integration_instance_id)
213+
214+
if invoke_result == True:
215+
print("\n🎉 Complete workflow successful!")
216+
print(" - File uploaded successfully")
217+
print(" - Integration invoked successfully")
218+
print(" - Data processing has begun")
219+
return True
220+
elif invoke_result == 'ALREADY_RUNNING':
221+
print("\n⚠️ Workflow partially successful")
222+
print(" - File uploaded successfully")
223+
print(" - Integration is already running")
224+
return True
225+
else:
226+
print("\n❌ Workflow failed at step 3: Invoking integration")
227+
return False
228+
229+
230+
def main():
231+
"""Main function demonstrating the CFT methods."""
232+
print("🚀 JupiterOne Custom File Transfer Integration Examples")
233+
print("="*60)
234+
235+
# Configuration - Replace these with your actual values
236+
INTEGRATION_INSTANCE_ID = os.getenv('J1_CFT_INSTANCE_ID', 'your-integration-instance-id')
237+
DATASET_ID = os.getenv('J1_CFT_DATASET_ID', 'your-dataset-id')
238+
CSV_FILE_PATH = os.getenv('J1_CSV_FILE_PATH', 'examples/scanned_hosts.csv')
239+
240+
# Check if we have the required configuration
241+
if INTEGRATION_INSTANCE_ID == 'your-integration-instance-id':
242+
print("⚠️ Configuration required:")
243+
print("Set the following environment variables:")
244+
print(" - J1_CFT_INSTANCE_ID: Your CFT integration instance ID")
245+
print(" - J1_CFT_DATASET_ID: Your dataset ID")
246+
print(" - J1_CSV_FILE_PATH: Path to your CSV file (optional, defaults to examples/scanned_hosts.csv)")
247+
print("\nExample:")
248+
print("export J1_CFT_INSTANCE_ID='123e4567-e89b-12d3-a456-426614174000'")
249+
print("export J1_CFT_DATASET_ID='dataset-123'")
250+
print("export J1_CSV_FILE_PATH='/path/to/your/file.csv'")
251+
print("\nOr update the variables in this script directly.")
252+
return
253+
254+
# Set up the client
255+
client = setup_client()
256+
257+
# Individual method examples
258+
print("\n📚 INDIVIDUAL METHOD EXAMPLES")
259+
260+
# Example 1: Get upload URL
261+
upload_info = get_cft_upload_url_example(
262+
client, INTEGRATION_INSTANCE_ID, os.path.basename(CSV_FILE_PATH), DATASET_ID
263+
)
264+
265+
if upload_info:
266+
# Example 2: Upload file
267+
upload_result = upload_cft_file_example(
268+
client, upload_info['uploadUrl'], CSV_FILE_PATH
269+
)
270+
271+
if upload_result and upload_result['success']:
272+
# Example 3: Invoke integration
273+
invoke_cft_integration_example(client, INTEGRATION_INSTANCE_ID)
274+
275+
# Complete workflow example
276+
print("\n🔄 COMPLETE WORKFLOW EXAMPLE")
277+
complete_workflow_example(client, INTEGRATION_INSTANCE_ID, CSV_FILE_PATH, DATASET_ID)
278+
279+
print("\n" + "="*60)
280+
print("📖 USAGE PATTERNS")
281+
print("="*60)
282+
283+
print("""
284+
Common usage patterns:
285+
286+
1. Single file upload and processing:
287+
upload_info = client.get_cft_upload_url(instance_id, filename, dataset_id)
288+
upload_result = client.upload_cft_file(upload_info['uploadUrl'], file_path)
289+
if upload_result['success']:
290+
client.invoke_cft_integration(instance_id)
291+
292+
2. Batch processing multiple files:
293+
for file_path in csv_files:
294+
upload_info = client.get_cft_upload_url(instance_id, filename, dataset_id)
295+
client.upload_cft_file(upload_info['uploadUrl'], file_path)
296+
297+
# Invoke integration once after all files are uploaded
298+
client.invoke_cft_integration(instance_id)
299+
300+
3. Error handling and retries:
301+
try:
302+
result = client.upload_cft_file(upload_url, file_path)
303+
if result['success']:
304+
print("Upload successful")
305+
else:
306+
print(f"Upload failed: {result['response_data']}")
307+
except Exception as e:
308+
print(f"Upload error: {e}")
309+
""")
310+
311+
312+
if __name__ == "__main__":
313+
main()

0 commit comments

Comments
 (0)