From 3e8c7b8f8b3030a97e30f87c3f7a39ba92361810 Mon Sep 17 00:00:00 2001 From: Chris Steinle Date: Fri, 6 Sep 2024 17:18:53 +0100 Subject: [PATCH] add --reveal to cli for v2.30.0 and later --- onepassword/client.py | 13 ++++--- test/test_cli_version.py | 77 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 test/test_cli_version.py diff --git a/onepassword/client.py b/onepassword/client.py index 2193ff1..9045381 100644 --- a/onepassword/client.py +++ b/onepassword/client.py @@ -277,6 +277,8 @@ class OnePassword: :param account: 1Password account name (Optional, default=None) :param password: password of 1Password account (Optional, default=None) """ + cli_version = tuple(map(int, read_bash_return("op --version").split("."))) + def __init__(self, signin_method: str = "app", account: str | None = None, password: str | None = None) -> None: # pragma: no cover if SERVICE_ACCOUNT_TOKEN in os.environ.keys(): @@ -421,8 +423,8 @@ def list_items(vault: str = "Private") -> dict: items = json.loads(read_bash_return("op items list --vault='{}' --format=json".format(vault), single=False)) return items - @staticmethod - def get_item(uuid: str | bytes, fields: str | bytes | list | None = None): + @classmethod + def get_item(cls, uuid: str | bytes, fields: str | bytes | list | None = None): """ Helper function to get a certain field, you can find the UUID you need using list_items @@ -431,9 +433,10 @@ def get_item(uuid: str | bytes, fields: str | bytes | list | None = None): (Optional, default=None which means all fields returned) :return: Dictionary of the item with requested fields """ + reveal = "--reveal" if cls.cli_version >= (2, 30, 0) else "" if isinstance(fields, list): item_list = json.loads(read_bash_return( - "op item get {} --format=json --fields label={}".format(uuid, ",label=".join(fields)), + "op item get {} {} --format=json --fields label={}".format(uuid, reveal, ",label=".join(fields)), single=False)) item = {} if isinstance(item_list, dict): @@ -444,10 +447,10 @@ def get_item(uuid: str | bytes, fields: str | bytes | list | None = None): elif isinstance(fields, str): item = { fields: read_bash_return( - "op item get {} --fields label={}".format(uuid, fields), single=False).rstrip('\n') + "op item get {} {} --fields label={}".format(uuid, reveal, fields), single=False).rstrip('\n') } else: - item = json.loads(read_bash_return("op item get {} --format=json".format(uuid), single=False)) + item = json.loads(read_bash_return("op item get {} {} --format=json".format(uuid, reveal), single=False)) return item @staticmethod diff --git a/test/test_cli_version.py b/test/test_cli_version.py new file mode 100644 index 0000000..afff7bd --- /dev/null +++ b/test/test_cli_version.py @@ -0,0 +1,77 @@ +import json +from unittest import TestCase +from unittest.mock import patch + +from onepassword import client + + +class TestCliVersion(TestCase): + @patch.object(client, "read_bash_return") + @patch.object(client.OnePassword, "cli_version", (2, 29, 0)) + def test_get_item_no_reveal(self, mock_read_bash_return): + op = client.OnePassword() + + mock_read_bash_return.return_value = json.dumps([]) + op.get_item("uuid") + mock_read_bash_return.assert_called_once_with( + "op item get uuid --format=json", + single=False, + ) + mock_read_bash_return.reset_mock() + + mock_read_bash_return.return_value = json.dumps( + [{"id": "field", "value": "value"}] + ) + op.get_item("uuid", "field") + mock_read_bash_return.assert_called_once_with( + "op item get uuid --fields label=field", single=False + ) + mock_read_bash_return.reset_mock() + + mock_read_bash_return.return_value = json.dumps( + [ + {"id": "list", "value": "value"}, + {"id": "of", "value": "value"}, + {"id": "fields", "value": "value"}, + ] + ) + op.get_item("uuid", ["list", "of", "fields"]) + mock_read_bash_return.assert_called_once_with( + "op item get uuid --format=json --fields label=list,label=of,label=fields", + single=False, + ) + + @patch.object(client, "read_bash_return") + @patch.object(client.OnePassword, "cli_version", (2, 30, 0)) + def test_get_item_reveal(self, mock_read_bash_return): + op = client.OnePassword() + + mock_read_bash_return.return_value = json.dumps([]) + op.get_item("uuid") + mock_read_bash_return.assert_called_once_with( + "op item get uuid --reveal --format=json", + single=False, + ) + mock_read_bash_return.reset_mock() + + mock_read_bash_return.return_value = json.dumps( + [{"id": "field", "value": "value"}] + ) + op.get_item("uuid", "field") + mock_read_bash_return.assert_called_once_with( + "op item get uuid --reveal --fields label=field", single=False + ) + mock_read_bash_return.reset_mock() + + mock_read_bash_return.return_value = json.dumps( + [ + {"id": "list", "value": "value"}, + {"id": "of", "value": "value"}, + {"id": "fields", "value": "value"}, + ] + ) + op.get_item("uuid", ["list", "of", "fields"]) + mock_read_bash_return.assert_called_once_with( + "op item get uuid --reveal --format=json --fields label=list,label=of,label=fields", + single=False, + )