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 cache functionality to improve lookup executing times #18

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

rkokkelk
Copy link

This PR adds caching functionality and improves executing times by remove unnecessary logged_in verification. As mentioned in #11, I've also encountered really long executing times in Ansible due to this plugin looking up passwords used in become.

Due to how lookups are being handled internally by Ansible, the only way to implement a caching method is to use global variables. Implementing a cache in the actual LookModule or Bitwarden module is not beneficiary because these objects are recreated for every lookup. However importing the actual Lookup plugin happens rarely and thus the cache exists for a long time within a playbook.

Because the plugin check for every lookup whether BW is still logged in, this is also now improved to only check it initially and then trust that result. This also prevents a lot of unnecessary HTTP and CLI lookups.

Caching specific values greatly increases executing times because now certain passwords does not have to be looked up every time. E.g. in my situation the sudo password is looked up for every task, even for every item in a loop, this greatly increases execution time. With this new modification now a cache lookup is used instead of BW lookups. Manual verification shows that this has a extreme benefit on the execution time.

This PR will fix #11.

Adds cache decorator using Python dictionary. Now each value gathered
from bw via the CLI is store in the cache. If the same exact field and
key is requested, then initiall the data from the cache is returned
instead of executing BW again, which is a lot slower.
For the caching functionality to work properly, the Bitwarden object
should live as long as the Lookup module exists, instead of creating a
new object, and thus a new cache, for each lookup.

Therefore the Bitwarden object is persisted in the initial lookup and
reused after that.
The `LookupModule` is initialised during every lookup. This results in a
new Bitwarden object for every lookup removing the benefit of the cache.

However, importing the bitwarden Lookup module is only done at specific
stages during a playbook and not run for every lookup itself. So
changing the cache to a global variable makes it persistent for as long
as possible and thus making the cache actually benefical for improving
the executing time.

Fixes c0sco#11
During every lookup the Bitwarden object verifies whether it is logged
in. This executes CLI and HTTP calls which take time, which is
unnecessary for most of the time. Therefore I've implemented a global
variable which is undefined initially, thus ensuring that `logged_in`
function is executed and otherwise use the previous versions.

Manual verification shows that this decreases overall execution time of
lookup plugin
By setting the BW object as a global instance, everything can be
implemented in the Bitwarden class itself, thus reducing the number of
global variables and making it more object oriented.
@c0sco c0sco self-assigned this Sep 22, 2021
@rkokkelk
Copy link
Author

rkokkelk commented Jun 4, 2023

@c0sco any indication when this could be merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Cache
2 participants