-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add --reveal to cli for v2.30.0 and later #65
Open
csteinle
wants to merge
1
commit into
wandera:main
Choose a base branch
from
csteinle:support_cli_2_30
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -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 "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we go with my suggestions you won't need this and can just add |
||
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 | ||
|
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,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, | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually try not to do this and just fix the version in the
install.py
it gave us a lot more headaches trying to be fully backwards compatible and so we try to maintain and bump when we can. I know we suggest you can have any version over 2.0.0 in the README but if this new flag is a break change, let's update that suggestion :D