Skip to content

Commit bd01b8b

Browse files
add example
1 parent db1fa59 commit bd01b8b

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Example script demonstrating how to connect to a 1Password Connect server
4+
using CA certificate verification and list all secrets in a vault.
5+
6+
Shows both synchronous and asynchronous usage.
7+
Update the configuration variables below with your values.
8+
"""
9+
10+
import asyncio
11+
from onepasswordconnectsdk.client import new_client
12+
from onepasswordconnectsdk.config import ClientConfig
13+
14+
# Configuration
15+
CONNECT_URL = "https://connect.example.com" # Your 1Password Connect server URL
16+
TOKEN = "eyJhbGc..." # Your 1Password Connect token
17+
VAULT_ID = "vaults_abc123" # ID of the vault to list secrets from
18+
CA_FILE = "path/to/ca.pem" # Path to your CA certificate file
19+
20+
def list_vault_secrets():
21+
"""
22+
Connect to 1Password Connect server and list all secrets in the specified vault.
23+
Uses CA certificate verification for secure connection.
24+
"""
25+
try:
26+
# Configure client with CA certificate verification
27+
config = ClientConfig(
28+
cafile=CA_FILE,
29+
timeout=30.0 # 30 second timeout
30+
)
31+
32+
# Initialize client with configuration
33+
client = new_client(CONNECT_URL, TOKEN, config=config)
34+
35+
# Get all items in the vault
36+
items = client.get_items(VAULT_ID)
37+
38+
# Print items
39+
print(f"\nSecrets in vault {VAULT_ID}:")
40+
print("-" * 40)
41+
for item in items:
42+
print(f"- {item.title} ({item.category})")
43+
44+
except Exception as e:
45+
print(f"Error: {str(e)}")
46+
47+
48+
async def list_vault_secrets_async():
49+
"""
50+
Async version: Connect to 1Password Connect server and list all secrets in the specified vault.
51+
Uses CA certificate verification for secure connection.
52+
"""
53+
try:
54+
# Configure client with CA certificate verification
55+
config = ClientConfig(
56+
cafile=CA_FILE,
57+
timeout=30.0 # 30 second timeout
58+
)
59+
60+
# Initialize async client with configuration
61+
client = new_client(CONNECT_URL, TOKEN, is_async=True, config=config)
62+
63+
# Get all items in the vault
64+
items = await client.get_items(VAULT_ID)
65+
66+
# Print items
67+
print(f"\nSecrets in vault {VAULT_ID} (async):")
68+
print("-" * 40)
69+
for item in items:
70+
print(f"- {item.title} ({item.category})")
71+
72+
# Close the client gracefully
73+
await client.session.aclose()
74+
75+
except Exception as e:
76+
print(f"Error: {str(e)}")
77+
78+
if __name__ == "__main__":
79+
# Run sync version
80+
print("Running synchronous example...")
81+
list_vault_secrets()
82+
83+
# Run async version
84+
print("\nRunning asynchronous example...")
85+
asyncio.run(list_vault_secrets_async())

0 commit comments

Comments
 (0)