Skip to content

Commit fc39c1a

Browse files
committed
Merge branch 'main' into sdk-core/2025-04-10-6847242a
2 parents ffc5267 + 410d182 commit fc39c1a

File tree

3 files changed

+88
-22
lines changed

3 files changed

+88
-22
lines changed

.github/workflows/validate.yml

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ on:
1313

1414
jobs:
1515

16-
integration-test-trusted:
16+
test-trusted:
1717
# actions that are trusted by default must only be opened from within the repo, and skipped for forks because they'll fail there
1818
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
1919
strategy:
2020
matrix:
2121
os: [ubuntu-latest, windows-latest, macos-latest]
22-
python-version: ["3.9", "3.10", "3.11", "3.12"]
22+
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
2323
runs-on: ${{ matrix.os }}
2424
steps:
2525
- uses: actions/checkout@v3
@@ -35,7 +35,15 @@ jobs:
3535
pip install pytest-asyncio &&
3636
pip install pydantic &&
3737
python -m pytest src/onepassword/test_client.py
38-
38+
- name: Example Test
39+
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.9'
40+
env:
41+
OP_SERVICE_ACCOUNT_TOKEN: ${{ secrets.EXAMPLE_TESTS_OP_SERVICE_ACCOUNT_TOKEN }}
42+
OP_VAULT_ID: ${{ secrets.EXAMPLE_TESTS_OP_VAULT_ID }}
43+
run: |
44+
pip install cryptography &&
45+
pip install . &&
46+
python example/example.py
3947
lint:
4048
name: Lint
4149
runs-on: ubuntu-latest
@@ -84,7 +92,7 @@ jobs:
8492
uses: actions/setup-python@v4
8593
with:
8694
python-version: '3.x'
87-
95+
8896
- name: Integration Test
8997
env:
9098
OP_SERVICE_ACCOUNT_TOKEN: ${{ secrets.TEST_SERVICE_ACCOUNT_TOKEN }}
@@ -94,8 +102,14 @@ jobs:
94102
pip install pydantic &&
95103
python -m pytest src/onepassword/test_client.py
96104
97-
- run: |
98-
echo "Integration tests completed successfully!"
105+
- name: Example Test
106+
if: matrix.os == 'ubuntu-latest'
107+
env:
108+
OP_SERVICE_ACCOUNT_TOKEN: ${{ secrets.TEST_SERVICE_ACCOUNT_TOKEN }}
109+
OP_VAULT_ID: ${{ secrets.TEST_SERVICE_ACCOUNT_VAULT_ID }}
110+
run: |
111+
pip install . &&
112+
python example/example.py
99113
100114
# Update check run called "integration-fork" on the forked PR
101115
- uses: actions/github-script@v6
@@ -105,7 +119,7 @@ jobs:
105119
job: ${{ github.job }}
106120
ref: ${{ github.event.client_payload.pull_request.head.sha }}
107121
# Conveniently, job.status maps to https://developer.github.com/v3/checks/runs/#update-a-check-run
108-
conclusion: ${{ job.status }}
122+
conclusion: ${{ job.status }}
109123
with:
110124
github-token: ${{ secrets.GITHUB_TOKEN }}
111125
script: |

example/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Examples
2+
This folder contains a code snippet demonstrating how to use the 1Password Python SDK for performing various operations on 1Password vaults and items. Specifically, the example showcases how to:
3+
4+
- Authenticate with the 1Password API using a service account token.
5+
- List available vaults and items within those vaults.
6+
- Retrieve a specific secret and resolve a one-time password (TOTP).
7+
- Create a new item in a vault with multiple fields and tags.
8+
- Update an existing item by modifying its fields and adding a new website.
9+
- Generate different types of passwords (PIN, memorable, and random).
10+
- Share an item with valid recipients and create a shareable link.
11+
- Archive or delete items from the vault.
12+
- Create and manage SSH key items.
13+
- Create and manage document items, including replacing and reading documents.
14+
- Create and manage file field items by attaching and deleting files.
15+
16+
## Prerequisites
17+
18+
1. Clone the repository and follow the steps to [get started](https://github.com/1Password/onepassword-sdk-python/blob/main/README.md).
19+
2. Ensure that you have a valid service account token by exporting it as an environment variable:
20+
```bash
21+
export OP_SERVICE_ACCOUNT_TOKEN="<your token>"
22+
```
23+
3. Export the vault UUID you wish to interact with as an environment variable:
24+
```bash
25+
export OP_VAULT_ID="<your vault uuid>"
26+
```
27+
28+
## How to Run
29+
30+
To run the example file, navigate to project root directory and run:
31+
```bash
32+
python example/example.py
33+
```
34+
35+
## Terminal Output
36+
37+
When running the example, the terminal will display:
38+
39+
- A list of vaults and items.
40+
- Retrieved secrets and TOTP codes.
41+
- Details of newly created and updated items.
42+
- Generated passwords (PIN, memorable, random).
43+
- A shareable link for shared items.
44+
- SSH key attributes like public key and fingerprint.
45+
- Document content after replacing the file.
46+
- A list of file field items and file deletions.
47+
48+
These outputs show the results of vault and item operations, password generation, item sharing, and management of SSH and document items.

example/example.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,16 @@ async def main():
5353
print(error)
5454
# [developer-docs.sdk.python.validate-secret-reference]-end
5555

56-
# [developer-docs.sdk.python.resolve-secret]-start
57-
# Retrieves a secret from 1Password. Takes a secret reference as input and returns the secret to which it points.
58-
value = await client.secrets.resolve("op://vault/item/field")
59-
print(value)
60-
# [developer-docs.sdk.python.resolve-secret]-end
56+
vault_id= os.getenv("OP_VAULT_ID")
57+
if vault_id is None:
58+
raise Exception("OP_VAULT_ID environment variable is not set")
6159

6260
# [developer-docs.sdk.python.create-item]-start
6361
# Create an Item and add it to your vault.
6462
to_create = ItemCreateParams(
6563
title="MyName",
6664
category=ItemCategory.LOGIN,
67-
vault_id="bhld6zk6hkuntyqlsjy3bdawey",
65+
vault_id=vault_id,
6866
fields=[
6967
ItemField(
7068
id="username",
@@ -104,6 +102,12 @@ async def main():
104102

105103
print(dict(created_item))
106104

105+
# [developer-docs.sdk.python.resolve-secret]-start
106+
# Retrieves a secret from 1Password. Takes a secret reference as input and returns the secret to which it points.
107+
value = await client.secrets.resolve(f"op://{created_item.vault_id}/{created_item.id}/username")
108+
print(value)
109+
# [developer-docs.sdk.python.resolve-secret]-end
110+
107111
# [developer-docs.sdk.python.resolve-totp-code]-start
108112
# Retrieves a secret from 1Password. Takes a secret reference as input and returns the secret to which it points.
109113
code = await client.secrets.resolve(
@@ -182,11 +186,11 @@ async def main():
182186

183187
await share_item(client, updated_item.vault_id, updated_item.id)
184188

185-
await create_ssh_key_item(client)
189+
await create_ssh_key_item(client, vault_id)
186190

187-
await create_and_replace_document_item(client)
191+
await create_and_replace_document_item(client, vault_id)
188192

189-
await create_attach_and_delete_file_field_item(client)
193+
await create_attach_and_delete_file_field_item(client, vault_id)
190194

191195
await archive_item(client, updated_item.vault_id, updated_item.id)
192196

@@ -237,7 +241,7 @@ async def share_item(client: Client, vault_id: str, item_id: str):
237241
# [developer-docs.sdk.python.item-share-create-share]-end
238242

239243

240-
async def create_ssh_key_item(client: Client):
244+
async def create_ssh_key_item(client: Client, vault_id: str):
241245
# [developer-docs.sdk.python.create-sshkey-item]-start
242246
# Generate a 2048-bit RSA private key
243247
private_key = rsa.generate_private_key(
@@ -256,7 +260,7 @@ async def create_ssh_key_item(client: Client):
256260
to_create = ItemCreateParams(
257261
title="SSH Key Item Created With Python SDK",
258262
category=ItemCategory.SSHKEY,
259-
vault_id="bhld6zk6hkuntyqlsjy3bdawey",
263+
vault_id=vault_id,
260264
fields=[
261265
ItemField(
262266
id="private_key",
@@ -280,13 +284,13 @@ async def create_ssh_key_item(client: Client):
280284
await client.items.delete(created_item.vault_id, created_item.id)
281285

282286

283-
async def create_and_replace_document_item(client: Client):
287+
async def create_and_replace_document_item(client: Client, vault_id: str):
284288
# [developer-docs.sdk.python.create-document-item]-start
285289
# Create a Document Item
286290
to_create = ItemCreateParams(
287291
title="Document Item Created with Python SDK",
288292
category=ItemCategory.DOCUMENT,
289-
vault_id="bhld6zk6hkuntyqlsjy3bdawey",
293+
vault_id=vault_id,
290294
sections=[
291295
ItemSection(id="", title=""),
292296
],
@@ -319,13 +323,13 @@ async def create_and_replace_document_item(client: Client):
319323
await client.items.delete(replaced_item.vault_id, replaced_item.id)
320324

321325

322-
async def create_attach_and_delete_file_field_item(client: Client):
326+
async def create_attach_and_delete_file_field_item(client: Client, vault_id: str):
323327
# [developer-docs.sdk.python.create-item-with-file-field]-start
324328
# Create a File Field Item
325329
to_create = ItemCreateParams(
326330
title="FileField Item created with Python SDK",
327331
category=ItemCategory.LOGIN,
328-
vault_id="bhld6zk6hkuntyqlsjy3bdawey",
332+
vault_id=vault_id,
329333
fields=[
330334
ItemField(
331335
id="username",

0 commit comments

Comments
 (0)