community.general icon indicating copy to clipboard operation
community.general copied to clipboard

community.general.bitwarden errors with search argument

Open zoredache opened this issue 1 year ago • 4 comments

Summary

I am trying to use the community.general.bitwarden lookup to search for items without providing an id or an exact match for the name.

The docs seem to say suggest that I can do this by passing search=None, or search=''`.

https://docs.ansible.com/ansible/latest/collections/community/general/bitwarden_lookup.html#parameter-search

search

If set to None or '', or if _terms is empty, records are not filtered by fields.

Trying to set search like this results in a KeyError.

This error seems to be coming from line 178. Specifically the item[search_field] results in a KeyError when '' is passed. Setting search=None returns no results.

Issue Type

Bug Report

Component Name

bitwarden

Ansible Version

$ ansible --version
ansible [core 2.17.1]
  config file = None
  configured module search path = ['/home/zoredache/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/ansible/stable/lib/python3.11/site-packages/ansible
  ansible collection location = /home/zoredache/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] (/usr/local/ansible/stable/bin/python)
  jinja version = 3.1.4
  libyaml = True

Community.general Version

$ ansible-galaxy collection list community.general

# /usr/local/ansible/stable/lib/python3.11/site-packages/ansible_collections
Collection        Version
----------------- -------
community.general 9.1.0 

Configuration

$ ansible-config dump --only-changed
COLLECTIONS_PATHS(./sandbox_ansible/ansible.cfg) = ['./sandbox_ansible/.ansible/collections', '/home/zoredache/.ansible/collections', '/usr/share/ansible/collections']
CONFIG_FILE() = ./sandbox_ansible/ansible.cfg
DEFAULT_FORKS(./sandbox_ansible/ansible.cfg) = 10
DEFAULT_GATHERING(./sandbox_ansible/ansible.cfg) = explicit
DEFAULT_HOST_LIST(./sandbox_ansible/ansible.cfg) = ['./sandbox_ansible/inventory']
DEFAULT_LOOKUP_PLUGIN_PATH(./sandbox_ansible/ansible.cfg) = ['./sandbox_ansible/plugins/lookup', '/home/zoredache/.ansible/plugins/lookup', '/usr/share/ansible/plugins/lookup']
DEFAULT_MODULE_PATH(./sandbox_ansible/ansible.cfg) = ['./sandbox_ansible/.ansible/plugins/modules', '/home/zoredache/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
DEFAULT_ROLES_PATH(./sandbox_ansible/ansible.cfg) = ['./sandbox_ansible/roles', './sandbox_ansible/.ansible/roles', '/home/zoredache/.ansible/roles', '/usr/share/ansible/roles']
DEFAULT_STDOUT_CALLBACK(./sandbox_ansible/ansible.cfg) = yaml
YAML_FILENAME_EXTENSIONS(./sandbox_ansible/ansible.cfg) = ['.yml', '.yaml', '.json', '.vault']

OS / Environment

$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 12 (bookworm)
Release:        12
Codename:       bookworm

Steps to Reproduce


- name: Lookup value in bitwarden
  hosts: localhost
  tasks:

    - name: Lookup value in bitwarden - search=''
      ignore_errors: true
      ansible.builtin.debug:
        msg: |
          {{ lookup('community.general.bitwarden', 'example_1', search='') }}

    # TASK [Lookup value in bitwarden - search=''] ******************************************************************************************
    # fatal: [localhost]: FAILED! =>
    #   msg: 'An unhandled exception occurred while running the lookup plugin ''community.general.bitwarden''. Error was a <class ''KeyError''>, original message: ''''. '''''
    # ...ignoring

    - name: Lookup value in bitwarden - search='None'
      ignore_errors: true
      ansible.builtin.debug:
        msg: |
          {{ lookup('community.general.bitwarden', 'example_1', search='None') }}

    # TASK [Lookup value in bitwarden - search='None'] **************************************************************************************
    # fatal: [localhost]: FAILED! =>
    #   msg: 'An unhandled exception occurred while running the lookup plugin ''community.general.bitwarden''. Error was a <class ''KeyError''>, original message: ''None''. ''None'''
    # ...ignoring

    - name: Lookup value in bitwarden - search=None
      ignore_errors: true
      ansible.builtin.debug:
        msg: |
          {{ lookup('community.general.bitwarden', 'example_1', search=None) }}

    # TASK [Lookup value in bitwarden - search=None] ****************************************************************************************
    # ok: [localhost] =>
    #   msg: []

    - name: Lookup value in bitwarden - partial search='name'
      ignore_errors: true
      ansible.builtin.debug:
        msg: |
          {{ lookup('community.general.bitwarden', 'example_1', search='name') }}

    # TASK [Lookup value in bitwarden - partial search='name'] ******************************************************************************
    # ok: [localhost] =>
    #   msg: []

    - name: Lookup value in bitwarden - full search='name'
      ignore_errors: true
      ansible.builtin.debug:
        msg: |
          {{ lookup('community.general.bitwarden', 'example_1 asdf', search='name') }}

    # TASK [Lookup value in bitwarden - full search='name'] *********************************************************************************
    # ok: [localhost] =>
    #   msg:
    #   - collectionIds: []
    #     creationDate: '2024-06-21T02:38:52.030Z'
    #     deletedDate: null
    #     favorite: false
    #     folderId: null
    #     id: 2af0753a-de96-4ce0-865d-cd033c5ed274
    #     login:
    #       fido2Credentials: []
    #       password: asdf
    #       passwordRevisionDate: null
    #       totp: null
    #       uris: []
    #       username: asdf
    #     name: example_1 asdf
    #     notes: asdf
    #     object: item
    #     organizationId: null
    #     passwordHistory: null
    #     reprompt: 0
    #     revisionDate: '2024-06-21T02:38:52.035Z'
    #     type: 1

Expected Results

It would seem to me that either the docs are unclear/wrong, or the behavior is wrong. The docs seem to say I can search=None, and I was expecting that to return all results from bitwarden that matched the search terms I used.

Basically I was hoping I could get exactly the same as what bw list items --search example_1 would return. But the extra filtering from the lookup seems to be getting in the way.

Actual Results

Output was included inline with the reproduction example.

Code of Conduct

  • [X] I agree to follow the Ansible Code of Conduct

zoredache avatar Jun 21 '24 03:06 zoredache

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

ansibullbot avatar Jun 21 '24 03:06 ansibullbot

cc @lungj click here for bot help

ansibullbot avatar Jun 21 '24 03:06 ansibullbot

CC @sistason, this could be related to #8188.

felixfontein avatar Jun 22 '24 09:06 felixfontein

thanks for the find @zoredache and the ping @felixfontein I opened a PR to fix my oversight.

sistason avatar Jun 25 '24 10:06 sistason