Skip to content

Commit

Permalink
Ops 6558 improve 1password lookup (#98)
Browse files Browse the repository at this point in the history
Add option to not fail when the item doesn't exist and pass errors to Ansible
  • Loading branch information
simoncolincap authored Sep 12, 2024
1 parent d637d08 commit f3229dd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion onepwd/galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion onepwd/plugins/lookup/onepwd.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ 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=[]
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:
if ignore_not_found:
return []
raise AnsibleError(f"No item named {secret_name} in vault {vault}")
except onepwd.UnknownError as unknown_error:
raise AnsibleError(unknown_error)
return values
2 changes: 1 addition & 1 deletion onepwd/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 3 additions & 9 deletions onepwd/src/onepwd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, item_name, vault):
self.message = message


class UnauthorizedErrorError(Exception):
class UnauthorizedError(Exception):
pass


Expand Down Expand Up @@ -181,17 +181,11 @@ 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}"
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)
Expand Down

0 comments on commit f3229dd

Please sign in to comment.