1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Add Support to Bitwarden Lookup for Custom Fields (#5694)

* 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>
This commit is contained in:
reverendj1 2023-01-07 04:28:05 -05:00 committed by GitHub
parent fc2b1aac4a
commit e3f02cb161
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View file

@ -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).

View file

@ -47,6 +47,11 @@ EXAMPLES = """
ansible.builtin.debug: ansible.builtin.debug:
msg: >- msg: >-
{{ lookup('community.general.bitwarden', 'a_test') }} {{ 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 = """ RETURN = """
@ -109,10 +114,19 @@ class Bitwarden(object):
""" """
matches = self._get_matches(search_value, search_field) 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 [match['login'][field] for match in matches]
elif not field:
return matches 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): class LookupModule(LookupBase):