From 0371910a159324a8390320e62d3dce89cb05957d Mon Sep 17 00:00:00 2001 From: Richard Klose Date: Mon, 5 Jun 2023 14:06:48 +0200 Subject: [PATCH 1/6] 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. --- changelogs/fragments/bitwarden-lookup-performance.yaml | 3 +++ plugins/lookup/bitwarden.py | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/bitwarden-lookup-performance.yaml diff --git a/changelogs/fragments/bitwarden-lookup-performance.yaml b/changelogs/fragments/bitwarden-lookup-performance.yaml new file mode 100644 index 00000000000..7db3ce61e09 --- /dev/null +++ b/changelogs/fragments/bitwarden-lookup-performance.yaml @@ -0,0 +1,3 @@ +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. diff --git a/plugins/lookup/bitwarden.py b/plugins/lookup/bitwarden.py index d49274c5c9f..4ef61131a52 100644 --- a/plugins/lookup/bitwarden.py +++ b/plugins/lookup/bitwarden.py @@ -112,7 +112,10 @@ def _get_matches(self, search_value, search_field, collection_id): """ # 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: params.extend(['--collectionid', collection_id]) @@ -121,7 +124,8 @@ def _get_matches(self, search_value, search_field, collection_id): # This includes things that matched in different fields. initial_matches = AnsibleJSONDecoder().raw_decode(out)[0] - + if search_field == 'id': + initial_matches = [initial_matches] # Filter to only include results from the right field. return [item for item in initial_matches if item[search_field] == search_value] From ddcfd659ab65511f122ea2a06fa2dd5229b03a99 Mon Sep 17 00:00:00 2001 From: Richard Klose Date: Mon, 5 Jun 2023 22:10:17 +0200 Subject: [PATCH 2/6] Update changelogs/fragments/bitwarden-lookup-performance.yaml Co-authored-by: Felix Fontein --- changelogs/fragments/bitwarden-lookup-performance.yaml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/changelogs/fragments/bitwarden-lookup-performance.yaml b/changelogs/fragments/bitwarden-lookup-performance.yaml index 7db3ce61e09..ab35c76d075 100644 --- a/changelogs/fragments/bitwarden-lookup-performance.yaml +++ b/changelogs/fragments/bitwarden-lookup-performance.yaml @@ -1,3 +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. + - "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/6619)." From 5ffcac33db7a397f636893b029ff090aa4057620 Mon Sep 17 00:00:00 2001 From: Richard Klose Date: Tue, 6 Jun 2023 08:29:41 +0200 Subject: [PATCH 3/6] fix indentation --- plugins/lookup/bitwarden.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/lookup/bitwarden.py b/plugins/lookup/bitwarden.py index 4ef61131a52..7b902b44429 100644 --- a/plugins/lookup/bitwarden.py +++ b/plugins/lookup/bitwarden.py @@ -113,9 +113,9 @@ def _get_matches(self, search_value, search_field, collection_id): # Prepare set of params for Bitwarden CLI if search_field == 'id': - params = ['get', 'item', search_value] + params = ['get', 'item', search_value] else: - params = ['list', 'items', '--search', search_value] + params = ['list', 'items', '--search', search_value] if collection_id: params.extend(['--collectionid', collection_id]) @@ -125,7 +125,7 @@ def _get_matches(self, search_value, search_field, collection_id): # This includes things that matched in different fields. initial_matches = AnsibleJSONDecoder().raw_decode(out)[0] if search_field == 'id': - initial_matches = [initial_matches] + initial_matches = [initial_matches] # Filter to only include results from the right field. return [item for item in initial_matches if item[search_field] == search_value] From 43c8fd47b59b334d00e6ca0cd57f5c472784b490 Mon Sep 17 00:00:00 2001 From: Pascal Hofmann Date: Fri, 3 Nov 2023 15:13:44 +0100 Subject: [PATCH 4/6] Ensure backwards compatible behavior in case of errors when doing bitwarden lookup by id --- plugins/lookup/bitwarden.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/plugins/lookup/bitwarden.py b/plugins/lookup/bitwarden.py index 7b902b44429..adafc371107 100644 --- a/plugins/lookup/bitwarden.py +++ b/plugins/lookup/bitwarden.py @@ -104,6 +104,8 @@ def _run(self, args, stdin=None, expected_rc=0): out, err = p.communicate(to_bytes(stdin)) rc = p.wait() 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) return to_text(out, errors='surrogate_or_strict'), to_text(err, errors='surrogate_or_strict') @@ -125,7 +127,10 @@ def _get_matches(self, search_value, search_field, collection_id): # This includes things that matched in different fields. initial_matches = AnsibleJSONDecoder().raw_decode(out)[0] if search_field == 'id': - initial_matches = [initial_matches] + if initial_matches == None: + initial_matches = [] + else: + initial_matches = [initial_matches] # Filter to only include results from the right field. return [item for item in initial_matches if item[search_field] == search_value] From 89a858f90872dc8ff438c91d2feb7685c8f1b28b Mon Sep 17 00:00:00 2001 From: Pascal Hofmann Date: Fri, 3 Nov 2023 15:24:54 +0100 Subject: [PATCH 5/6] chore: Link to correct PR in changelog fragment --- changelogs/fragments/bitwarden-lookup-performance.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/bitwarden-lookup-performance.yaml b/changelogs/fragments/bitwarden-lookup-performance.yaml index ab35c76d075..cb0405b1cbf 100644 --- a/changelogs/fragments/bitwarden-lookup-performance.yaml +++ b/changelogs/fragments/bitwarden-lookup-performance.yaml @@ -1,2 +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/6619)." + - "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)." From e1dc4714993b7aca432409acf08bfe5b4c1b9ac0 Mon Sep 17 00:00:00 2001 From: Pascal Hofmann Date: Sat, 4 Nov 2023 05:22:55 +0100 Subject: [PATCH 6/6] Use identity check when comparing with None --- plugins/lookup/bitwarden.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/lookup/bitwarden.py b/plugins/lookup/bitwarden.py index adafc371107..3756c1fcfe6 100644 --- a/plugins/lookup/bitwarden.py +++ b/plugins/lookup/bitwarden.py @@ -127,7 +127,7 @@ def _get_matches(self, search_value, search_field, collection_id): # This includes things that matched in different fields. initial_matches = AnsibleJSONDecoder().raw_decode(out)[0] if search_field == 'id': - if initial_matches == None: + if initial_matches is None: initial_matches = [] else: initial_matches = [initial_matches]