From e75dc74613641ad278a4a1272289dad9179b9b49 Mon Sep 17 00:00:00 2001 From: simonLeary42 <71396965+simonLeary42@users.noreply.github.com> Date: Fri, 11 Aug 2023 07:22:26 -0400 Subject: [PATCH] bitwarden lookup fix `get_field` (#7061) * bitwarden lookup rewrite `get_field` * add changelog fragment * PEP8 add newline * Update changelogs/fragments/7061-fix-bitwarden-get_field.yml Co-authored-by: Felix Fontein * Update plugins/lookup/bitwarden.py Co-authored-by: Felix Fontein * Update plugins/lookup/bitwarden.py Co-authored-by: Felix Fontein * Update plugins/lookup/bitwarden.py Co-authored-by: Felix Fontein --------- Co-authored-by: Simon Co-authored-by: Felix Fontein --- .../7061-fix-bitwarden-get_field.yml | 2 ++ plugins/lookup/bitwarden.py | 33 ++++++++++++------- 2 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 changelogs/fragments/7061-fix-bitwarden-get_field.yml diff --git a/changelogs/fragments/7061-fix-bitwarden-get_field.yml b/changelogs/fragments/7061-fix-bitwarden-get_field.yml new file mode 100644 index 0000000000..a3f740a2f7 --- /dev/null +++ b/changelogs/fragments/7061-fix-bitwarden-get_field.yml @@ -0,0 +1,2 @@ +bugfixes: + - bitwarden lookup plugin - the plugin made assumptions about the structure of a Bitwarden JSON object which may have been broken by an update in the Bitwarden API. Remove assumptions, and allow queries for general fields such as ``notes`` (https://github.com/ansible-collections/community.general/pull/7061). diff --git a/plugins/lookup/bitwarden.py b/plugins/lookup/bitwarden.py index 727aceef3c..d49274c5c9 100644 --- a/plugins/lookup/bitwarden.py +++ b/plugins/lookup/bitwarden.py @@ -132,20 +132,29 @@ class Bitwarden(object): If field is None, return the whole record for each match. """ matches = self._get_matches(search_value, search_field, collection_id) - - if field in ['autofillOnPageLoad', 'password', 'passwordRevisionDate', 'totp', 'uris', 'username']: - return [match['login'][field] for match in matches] - elif not field: + if not field: return matches - else: - custom_field_matches = [] - for match in matches: + field_matches = [] + for match in matches: + # if there are no custom fields, then `match` has no key 'fields' + if 'fields' in match: + custom_field_found = False for custom_field in match['fields']: - if custom_field['name'] == field: - custom_field_matches.append(custom_field['value']) - if matches and not custom_field_matches: - raise AnsibleError("Custom field {field} does not exist in {search_value}".format(field=field, search_value=search_value)) - return custom_field_matches + if field == custom_field['name']: + field_matches.append(custom_field['value']) + custom_field_found = True + break + if custom_field_found: + continue + if 'login' in match and field in match['login']: + field_matches.append(match['login'][field]) + continue + if field in match: + field_matches.append(match[field]) + continue + if matches and not field_matches: + raise AnsibleError("field {field} does not exist in {search_value}".format(field=field, search_value=search_value)) + return field_matches class LookupModule(LookupBase):