Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support to Bitwarden Lookup for Custom Fields #5694

Merged
merged 7 commits into from
Jan 7, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 ['password', 'passwordRevisionDate', 'totp', 'uris', 'username']:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will login never contain anything else than these five fields? (If it does, this PR breaks backwards compatibility.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're very welcome. Changelog fragment has been added.

I can't answer that definitively, as I can't find the object definition in the source. However, there are no fields in the client that are not represented in my example record above, so I would imagine there are no other "login" fields at this time.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, the problem is that if there are more fields, this would be a breaking change since if anyone was querying these fields successfully in the past, it won't work for them anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dug through the source a bit. I still couldn't find where it's defined on the server side, but I did find this on the official client, which seems to suggest there is one more field, autofillOnPageLoad. I have updated the PR with this additional field.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for that. I'm not a developer, just a sysadmin, so it's difficult for me to understand and navigate larger code bases. Looks like we definitively got all the login fields now.

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 not len(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