Skip to content

Commit

Permalink
[PR #7061/e75dc746 backport][stable-6] bitwarden lookup fix `get_fiel…
Browse files Browse the repository at this point in the history
…d` (#7089)

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 <felix@fontein.de>

* Update plugins/lookup/bitwarden.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/lookup/bitwarden.py

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/lookup/bitwarden.py

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Simon <simonleary@umass.edu>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit e75dc74)

Co-authored-by: simonLeary42 <71396965+simonLeary42@users.noreply.github.com>
  • Loading branch information
patchback[bot] and simonLeary42 authored Aug 11, 2023
1 parent 2638413 commit bcfcdd1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/7061-fix-bitwarden-get_field.yml
Original file line number Diff line number Diff line change
@@ -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).
33 changes: 21 additions & 12 deletions plugins/lookup/bitwarden.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,29 @@ def get_field(self, field, search_value, search_field="name", collection_id=None
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):
Expand Down

0 comments on commit bcfcdd1

Please sign in to comment.