From e522339769b7b6c71d2f623a5e7c4b95a81cd188 Mon Sep 17 00:00:00 2001 From: Antonio Mindov Date: Sun, 3 Nov 2024 03:44:28 +0200 Subject: [PATCH 1/2] feat: add support for read CLI command (#66) Signed-off-by: Antonio Mindov --- onepassword/client.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/onepassword/client.py b/onepassword/client.py index 2193ff1..3e02058 100644 --- a/onepassword/client.py +++ b/onepassword/client.py @@ -450,6 +450,21 @@ def get_item(uuid: str | bytes, fields: str | bytes | list | None = None): item = json.loads(read_bash_return("op item get {} --format=json".format(uuid), single=False)) return item + @staticmethod + def read(secret_ref: str): + """ + Helper function to read a secret based on its reference(ex: op:////) + You can get this reference from the UI or by using this command: + op item get --format json --fields + + :param secret_ref: Reference to the secret you wish to read + :return: The secret in plain text + """ + if not secret_ref or not isinstance(secret_ref, str): + raise ValueError("secret_ref must be a non-empty string") + + return read_bash_return("op read '{}'".format(secret_ref)) + @staticmethod def get_item_otp(uuid: str | bytes): """ From f47a76e645de08ef8d7b318e02b6399cd5c38499 Mon Sep 17 00:00:00 2001 From: Antonio Mindov Date: Mon, 4 Nov 2024 15:10:09 +0200 Subject: [PATCH 2/2] docs: add info about secret references Signed-off-by: Antonio Mindov --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index 127f80e..e9b167b 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,30 @@ op = OnePassword() op.list_vaults() ``` +### Secret References + +Secret references are direct keys to a field in an item. They are useful when trying to get only one field(ex. API Key or a password) from an item. + +You can fetch secret based on secret references with the following code: + +```python +from onepassword import OnePassword + +op = OnePassword() +op.read("op:////") +``` + +You can get the secret reference in one of the supported ways explained here: +https://developer.1password.com/docs/cli/secret-references/ + +Or you can use this: +```python +from onepassword import OnePassword + +op = OnePassword() +op.get_item("Item")["fields"][0]['reference'] +``` + ### Input formats To be sure what you are using is of the right format @@ -165,6 +189,7 @@ This is the set of commands the current python SDK covers: - list: List objects and events - items - vaults +- read: Get a secret based on a secret reference - signin: Sign in to a 1Password account - signout: Sign out of a 1Password account