Skip to content

Commit 410d182

Browse files
moward98MOmarMiraj
andauthored
CI Pipeline Runs example/example.py (#151)
* Added example README. Edited example.py to resolve secret reference with parameters of created item. example.py reads vault id from env var. Call to run example.py added to pipeline * Fixed yaml syntax error in validate.yml * added installation of current directory before running example/example.py * Enforcing only one matrix combination runs at a time to avoid 409 Error. Edited GitHub Secret names to match. * Edited README to better reflect what is actually happening in the example code * removed successful strings being printed by pipeline. * fixed typo in readme * Updated to only run example file on ubuntu OS instead of all OS * Updated example/README.md to capture example changes after latest release * added requirements file to install packages that the example code requires to execute * Removed hardcoded instances of vault_id and replaced with vault_id var read from environment variables * removed max-parallel * Both instances of Example Test only run on ubuntu-latest * remove requrieement.txt and add python 3.13 and install right into the example test --------- Co-authored-by: Omar Miraj <[email protected]>
1 parent 44f99f2 commit 410d182

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
@@ -44,18 +44,16 @@ async def main():
4444
print(error)
4545
# [developer-docs.sdk.python.validate-secret-reference]-end
4646

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

5351
# [developer-docs.sdk.python.create-item]-start
5452
# Create an Item and add it to your vault.
5553
to_create = ItemCreateParams(
5654
title="MyName",
5755
category=ItemCategory.LOGIN,
58-
vault_id="7turaasywpymt3jecxoxk5roli",
56+
vault_id=vault_id,
5957
fields=[
6058
ItemField(
6159
id="username",
@@ -95,6 +93,12 @@ async def main():
9593

9694
print(dict(created_item))
9795

96+
# [developer-docs.sdk.python.resolve-secret]-start
97+
# Retrieves a secret from 1Password. Takes a secret reference as input and returns the secret to which it points.
98+
value = await client.secrets.resolve(f"op://{created_item.vault_id}/{created_item.id}/username")
99+
print(value)
100+
# [developer-docs.sdk.python.resolve-secret]-end
101+
98102
# [developer-docs.sdk.python.resolve-totp-code]-start
99103
# Retrieves a secret from 1Password. Takes a secret reference as input and returns the secret to which it points.
100104
code = await client.secrets.resolve(
@@ -173,11 +177,11 @@ async def main():
173177

174178
await share_item(client, created_item.vault_id, updated_item.id)
175179

176-
await create_ssh_key_item(client)
180+
await create_ssh_key_item(client, vault_id)
177181

178-
await create_and_replace_document_item(client)
182+
await create_and_replace_document_item(client, vault_id)
179183

180-
await create_attach_and_delete_file_field_item(client)
184+
await create_attach_and_delete_file_field_item(client, vault_id)
181185

182186
# [developer-docs.sdk.python.delete-item]-start
183187
# Delete a item from your vault.
@@ -230,7 +234,7 @@ async def share_item(client: Client, vault_id: str, item_id: str):
230234
# [developer-docs.sdk.python.item-share-create-share]-end
231235

232236

233-
async def create_ssh_key_item(client: Client):
237+
async def create_ssh_key_item(client: Client, vault_id: str):
234238
# [developer-docs.sdk.python.create-sshkey-item]-start
235239
# Generate a 2048-bit RSA private key
236240
private_key = rsa.generate_private_key(
@@ -249,7 +253,7 @@ async def create_ssh_key_item(client: Client):
249253
to_create = ItemCreateParams(
250254
title="SSH Key Item Created With Python SDK",
251255
category=ItemCategory.SSHKEY,
252-
vault_id="7turaasywpymt3jecxoxk5roli",
256+
vault_id=vault_id,
253257
fields=[
254258
ItemField(
255259
id="private_key",
@@ -273,13 +277,13 @@ async def create_ssh_key_item(client: Client):
273277
await client.items.delete(created_item.vault_id, created_item.id)
274278

275279

276-
async def create_and_replace_document_item(client: Client):
280+
async def create_and_replace_document_item(client: Client, vault_id: str):
277281
# [developer-docs.sdk.python.create-document-item]-start
278282
# Create a Document Item
279283
to_create = ItemCreateParams(
280284
title="Document Item Created with Python SDK",
281285
category=ItemCategory.DOCUMENT,
282-
vault_id="7turaasywpymt3jecxoxk5roli",
286+
vault_id=vault_id,
283287
sections=[
284288
ItemSection(id="", title=""),
285289
],
@@ -312,13 +316,13 @@ async def create_and_replace_document_item(client: Client):
312316
await client.items.delete(replaced_item.vault_id, replaced_item.id)
313317

314318

315-
async def create_attach_and_delete_file_field_item(client: Client):
319+
async def create_attach_and_delete_file_field_item(client: Client, vault_id: str):
316320
# [developer-docs.sdk.python.create-item-with-file-field]-start
317321
# Create a File Field Item
318322
to_create = ItemCreateParams(
319323
title="FileField Item created with Python SDK",
320324
category=ItemCategory.LOGIN,
321-
vault_id="7turaasywpymt3jecxoxk5roli",
325+
vault_id=vault_id,
322326
fields=[
323327
ItemField(
324328
id="username",

0 commit comments

Comments
 (0)