From 5b9daea2f073f14d59c6cfed5e11f65d570a3b3a Mon Sep 17 00:00:00 2001 From: simoncolincap Date: Tue, 3 Sep 2024 11:55:57 +0000 Subject: [PATCH 1/5] OPS-6558 Improve error handling --- onepwd/galaxy.yml | 2 +- onepwd/plugins/lookup/onepwd.py | 11 ++++++++++- onepwd/setup.py | 2 +- onepwd/src/onepwd/__init__.py | 7 ++----- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/onepwd/galaxy.yml b/onepwd/galaxy.yml index 2209b76..1824e1d 100644 --- a/onepwd/galaxy.yml +++ b/onepwd/galaxy.yml @@ -9,7 +9,7 @@ namespace: dbildungscloud name: onepwd # The version of the collection. Must be compatible with semantic versioning -version: 2.4.0 +version: 2.5.0 # The path to the Markdown (.md) readme file. This path is relative to the root of the collection readme: README.md diff --git a/onepwd/plugins/lookup/onepwd.py b/onepwd/plugins/lookup/onepwd.py index 111a500..fc6e161 100644 --- a/onepwd/plugins/lookup/onepwd.py +++ b/onepwd/plugins/lookup/onepwd.py @@ -50,5 +50,14 @@ def run(self, terms, variables=None, **kwargs): vault=kwargs.get('vault', None) field=kwargs.get('field', None) values=[] - values.append(onepwd.get_single_secret(op, secret_name, field=field, vault=vault)) + try: + values.append(onepwd.get_single_secret(op, secret_name, field=field, vault=vault)) + # except onepwd.UnauthorizedError: + # raise AnsibleError("Unauthorized") + except onepwd.DuplicateItemsError: + raise AnsibleError(f"More than one item named {secret_name} in vault {vault}") + except onepwd.UnknownResourceItem: + raise AnsibleError(f"No item named {secret_name} in vault {vault}") + except onepwd.UnknownError as unknown_error: + raise AnsibleError(unknown_error) return values diff --git a/onepwd/setup.py b/onepwd/setup.py index ceab77b..bef15ff 100644 --- a/onepwd/setup.py +++ b/onepwd/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="onepwd", - version="2.2.1", + version="2.5.0", author="HPI Schulcloud", author_email="devops@dbildungscloud.de", description="Utilities to work with 1password", diff --git a/onepwd/src/onepwd/__init__.py b/onepwd/src/onepwd/__init__.py index 7002f7d..1536d02 100644 --- a/onepwd/src/onepwd/__init__.py +++ b/onepwd/src/onepwd/__init__.py @@ -22,7 +22,7 @@ def __init__(self, item_name, vault): self.message = message -class UnauthorizedErrorError(Exception): +class UnauthorizedError(Exception): pass @@ -181,10 +181,7 @@ def delete_item(self, item_name, vault=None): def get(self, resource, item_name, vault=None): vault_flag = get_optional_flag(vault=vault) op_command = f"{self.op} {resource} get '{item_name}' {vault_flag} --session={self.session_token}" - try: - return json.loads(run_op_command_in_shell(op_command)) - except subprocess.CalledProcessError: - raise UnknownResourceItem(f"{resource}: {item_name}") + return json.loads(run_op_command_in_shell(op_command)) def get_document(self, item_name): op_command = f"{self.op} document get '{item_name}' --session={self.session_token}" From e5cd6a962b66fd320cfb49718af6821ef7e2486a Mon Sep 17 00:00:00 2001 From: simoncolincap Date: Tue, 3 Sep 2024 12:07:14 +0000 Subject: [PATCH 2/5] OPS-6558 Comment in UnauthorizedError --- onepwd/plugins/lookup/onepwd.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/onepwd/plugins/lookup/onepwd.py b/onepwd/plugins/lookup/onepwd.py index fc6e161..437e5ee 100644 --- a/onepwd/plugins/lookup/onepwd.py +++ b/onepwd/plugins/lookup/onepwd.py @@ -52,8 +52,8 @@ def run(self, terms, variables=None, **kwargs): values=[] try: values.append(onepwd.get_single_secret(op, secret_name, field=field, vault=vault)) - # except onepwd.UnauthorizedError: - # raise AnsibleError("Unauthorized") + except onepwd.UnauthorizedError: + raise AnsibleError("Unauthorized") except onepwd.DuplicateItemsError: raise AnsibleError(f"More than one item named {secret_name} in vault {vault}") except onepwd.UnknownResourceItem: From 05836d6bf03c683b581fc7df5a80088ef4306124 Mon Sep 17 00:00:00 2001 From: simoncolincap Date: Tue, 3 Sep 2024 12:14:38 +0000 Subject: [PATCH 3/5] OPS-6558 Add option to not fail when no item is found --- onepwd/plugins/lookup/onepwd.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/onepwd/plugins/lookup/onepwd.py b/onepwd/plugins/lookup/onepwd.py index 437e5ee..7dabb1c 100644 --- a/onepwd/plugins/lookup/onepwd.py +++ b/onepwd/plugins/lookup/onepwd.py @@ -49,6 +49,7 @@ def run(self, terms, variables=None, **kwargs): secret_name=kwargs.get('secret_name', '') vault=kwargs.get('vault', None) field=kwargs.get('field', None) + ignore_not_found=kwargs.get('ignore_not_found', False) values=[] try: values.append(onepwd.get_single_secret(op, secret_name, field=field, vault=vault)) @@ -57,6 +58,8 @@ def run(self, terms, variables=None, **kwargs): except onepwd.DuplicateItemsError: raise AnsibleError(f"More than one item named {secret_name} in vault {vault}") except onepwd.UnknownResourceItem: + if ignore_not_found: + return None raise AnsibleError(f"No item named {secret_name} in vault {vault}") except onepwd.UnknownError as unknown_error: raise AnsibleError(unknown_error) From 8a69821168435881f01b9d507f932547e2fd6ace Mon Sep 17 00:00:00 2001 From: simoncolincap Date: Tue, 3 Sep 2024 14:15:50 +0000 Subject: [PATCH 4/5] OPS-6558 Return empty list instead of None --- onepwd/plugins/lookup/onepwd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onepwd/plugins/lookup/onepwd.py b/onepwd/plugins/lookup/onepwd.py index 7dabb1c..9f23c74 100644 --- a/onepwd/plugins/lookup/onepwd.py +++ b/onepwd/plugins/lookup/onepwd.py @@ -59,7 +59,7 @@ def run(self, terms, variables=None, **kwargs): raise AnsibleError(f"More than one item named {secret_name} in vault {vault}") except onepwd.UnknownResourceItem: if ignore_not_found: - return None + return [] raise AnsibleError(f"No item named {secret_name} in vault {vault}") except onepwd.UnknownError as unknown_error: raise AnsibleError(unknown_error) From 0e50c154e183588316726cf1db5511e7ae71a6b8 Mon Sep 17 00:00:00 2001 From: simoncolincap Date: Thu, 5 Sep 2024 10:11:01 +0000 Subject: [PATCH 5/5] OPS-6558 Improve error handling --- onepwd/src/onepwd/__init__.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/onepwd/src/onepwd/__init__.py b/onepwd/src/onepwd/__init__.py index 1536d02..ac9392c 100644 --- a/onepwd/src/onepwd/__init__.py +++ b/onepwd/src/onepwd/__init__.py @@ -185,10 +185,7 @@ def get(self, resource, item_name, vault=None): def get_document(self, item_name): op_command = f"{self.op} document get '{item_name}' --session={self.session_token}" - try: - return run_op_command_in_shell(op_command) - except subprocess.CalledProcessError: - raise UnknownResourceItem(f"document: {item_name}") + return run_op_command_in_shell(op_command) def create_document_from_file(self, path, title, vault=None): vault_flag = get_optional_flag(vault=vault)