-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db1fa59
commit bd01b8b
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Example script demonstrating how to connect to a 1Password Connect server | ||
using CA certificate verification and list all secrets in a vault. | ||
Shows both synchronous and asynchronous usage. | ||
Update the configuration variables below with your values. | ||
""" | ||
|
||
import asyncio | ||
from onepasswordconnectsdk.client import new_client | ||
from onepasswordconnectsdk.config import ClientConfig | ||
|
||
# Configuration | ||
CONNECT_URL = "https://connect.example.com" # Your 1Password Connect server URL | ||
TOKEN = "eyJhbGc..." # Your 1Password Connect token | ||
VAULT_ID = "vaults_abc123" # ID of the vault to list secrets from | ||
CA_FILE = "path/to/ca.pem" # Path to your CA certificate file | ||
|
||
def list_vault_secrets(): | ||
""" | ||
Connect to 1Password Connect server and list all secrets in the specified vault. | ||
Uses CA certificate verification for secure connection. | ||
""" | ||
try: | ||
# Configure client with CA certificate verification | ||
config = ClientConfig( | ||
cafile=CA_FILE, | ||
timeout=30.0 # 30 second timeout | ||
) | ||
|
||
# Initialize client with configuration | ||
client = new_client(CONNECT_URL, TOKEN, config=config) | ||
|
||
# Get all items in the vault | ||
items = client.get_items(VAULT_ID) | ||
|
||
# Print items | ||
print(f"\nSecrets in vault {VAULT_ID}:") | ||
print("-" * 40) | ||
for item in items: | ||
print(f"- {item.title} ({item.category})") | ||
|
||
except Exception as e: | ||
print(f"Error: {str(e)}") | ||
|
||
|
||
async def list_vault_secrets_async(): | ||
""" | ||
Async version: Connect to 1Password Connect server and list all secrets in the specified vault. | ||
Uses CA certificate verification for secure connection. | ||
""" | ||
try: | ||
# Configure client with CA certificate verification | ||
config = ClientConfig( | ||
cafile=CA_FILE, | ||
timeout=30.0 # 30 second timeout | ||
) | ||
|
||
# Initialize async client with configuration | ||
client = new_client(CONNECT_URL, TOKEN, is_async=True, config=config) | ||
|
||
# Get all items in the vault | ||
items = await client.get_items(VAULT_ID) | ||
|
||
# Print items | ||
print(f"\nSecrets in vault {VAULT_ID} (async):") | ||
print("-" * 40) | ||
for item in items: | ||
print(f"- {item.title} ({item.category})") | ||
|
||
# Close the client gracefully | ||
await client.session.aclose() | ||
|
||
except Exception as e: | ||
print(f"Error: {str(e)}") | ||
|
||
if __name__ == "__main__": | ||
# Run sync version | ||
print("Running synchronous example...") | ||
list_vault_secrets() | ||
|
||
# Run async version | ||
print("\nRunning asynchronous example...") | ||
asyncio.run(list_vault_secrets_async()) |