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

[PR #7468/65f58afb backport][stable-8] Improve performance of the bitwarden lookup plugin (#7512)

Improve performance of the bitwarden lookup plugin (#7468)

* Improve performance of the bitwarden lookup plugin

When looking for items using an item id, we can access the item directly with
bw get item instead of searching through all items. This doubles the lookup
speed.

* Update changelogs/fragments/bitwarden-lookup-performance.yaml

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

* fix indentation

* Ensure backwards compatible behavior in case of errors when doing bitwarden lookup by id

* chore: Link to correct PR in changelog fragment

* Use identity check when comparing with None

---------

Co-authored-by: Richard Klose <richard.klose@gec.io>
Co-authored-by: Richard Klose <richard@klose.dev>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 65f58afbd9)

Co-authored-by: Pascal Hofmann <mail@pascalhofmann.de>
This commit is contained in:
patchback[bot] 2023-11-13 20:22:25 +01:00 committed by GitHub
parent 20d2488f0f
commit f2e2157bcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 2 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- "bitwarden lookup plugin - when looking for items using an item ID, the item is now accessed directly with ``bw get item`` instead of searching through all items. This doubles the lookup speed (https://github.com/ansible-collections/community.general/pull/7468)."

View file

@ -104,6 +104,8 @@ class Bitwarden(object):
out, err = p.communicate(to_bytes(stdin)) out, err = p.communicate(to_bytes(stdin))
rc = p.wait() rc = p.wait()
if rc != expected_rc: if rc != expected_rc:
if len(args) > 2 and args[0] == 'get' and args[1] == 'item' and b'Not found.' in err:
return 'null', ''
raise BitwardenException(err) raise BitwardenException(err)
return to_text(out, errors='surrogate_or_strict'), to_text(err, errors='surrogate_or_strict') return to_text(out, errors='surrogate_or_strict'), to_text(err, errors='surrogate_or_strict')
@ -112,7 +114,10 @@ class Bitwarden(object):
""" """
# Prepare set of params for Bitwarden CLI # Prepare set of params for Bitwarden CLI
params = ['list', 'items', '--search', search_value] if search_field == 'id':
params = ['get', 'item', search_value]
else:
params = ['list', 'items', '--search', search_value]
if collection_id: if collection_id:
params.extend(['--collectionid', collection_id]) params.extend(['--collectionid', collection_id])
@ -121,7 +126,11 @@ class Bitwarden(object):
# This includes things that matched in different fields. # This includes things that matched in different fields.
initial_matches = AnsibleJSONDecoder().raw_decode(out)[0] initial_matches = AnsibleJSONDecoder().raw_decode(out)[0]
if search_field == 'id':
if initial_matches is None:
initial_matches = []
else:
initial_matches = [initial_matches]
# Filter to only include results from the right field. # Filter to only include results from the right field.
return [item for item in initial_matches if item[search_field] == search_value] return [item for item in initial_matches if item[search_field] == search_value]