Skip to content

Commit

Permalink
Add Support to Bitwarden Lookup for Custom Fields (#5694)
Browse files Browse the repository at this point in the history
* Add Support to Bitwarden Lookup for Custom Fields

This adds support to the Bitwarden lookup for retrieving values from
custom fields, such as api keys.

* Need to Return Whole Record if Field is Not Defined

* whitespace

* Add Changelog Fragment

* Need to Make Sure All Login Fields are Represented

We need to make sure that all login fields are accounted for, since
there will be no other way to retrieve them with this change, and we
don't want to break backwards compatibility. Looking at this code from
the official client,
https://github.com/bitwarden/clients/blob/master/libs/common/spec/models/domain/login.spec.ts,
autofillOnPageLoad might be another login field.

* Update changelogs/fragments/5694-add-custom-fields-to-bitwarden.yml

Clarify changelog fragment

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

* Update plugins/lookup/bitwarden.py

Fix logic. Should only error if matches were found, but are missing the custom field.

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

Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit e3f02cb)
  • Loading branch information
reverendj1 authored and patchback[bot] committed Jan 7, 2023
1 parent c8b8668 commit 694910c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/5694-add-custom-fields-to-bitwarden.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- bitwarden lookup plugin - can now retrieve secrets from custom fields (https://github.com/ansible-collections/community.general/pull/5694).
20 changes: 17 additions & 3 deletions plugins/lookup/bitwarden.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
ansible.builtin.debug:
msg: >-
{{ lookup('community.general.bitwarden', 'a_test') }}
- name: "Get custom field 'api_key' from Bitwarden record named 'a_test'"
ansible.builtin.debug:
msg: >-
{{ lookup('community.general.bitwarden', 'a_test', field='api_key') }}
"""

RETURN = """
Expand Down Expand Up @@ -109,10 +114,19 @@ def get_field(self, field, search_value, search_field="name"):
"""
matches = self._get_matches(search_value, search_field)

if field:
if field in ['autofillOnPageLoad', 'password', 'passwordRevisionDate', 'totp', 'uris', 'username']:
return [match['login'][field] for match in matches]

return matches
elif not field:
return matches
else:
custom_field_matches = []
for match in matches:
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


class LookupModule(LookupBase):
Expand Down

0 comments on commit 694910c

Please sign in to comment.