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
Open
Changes from 1 commit
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
Prev Previous commit
refactor[bw]: reduce global variable to single bw instance
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.
  • Loading branch information
rkokkelk committed Aug 22, 2021
commit 2709e008fd4a3c5d5e429205bca12b344d5cc0b7
50 changes: 22 additions & 28 deletions lookup_plugins/bitwarden.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@
"""

# Global variables
CACHE = dict()
LOGGED_IN = None
bw = None


class Bitwarden(object):
def __init__(self, path):
self._cli_path = path
self._bw_session = ""
self._cache = dict()
self._logged_in = None

try:
check_output([self._cli_path, "--version"])
Expand All @@ -92,23 +93,22 @@ def cli_path(self):

@property
def logged_in(self):
global LOGGED_IN

if LOGGED_IN is None:
if self._logged_in is None:
# Parse Bitwarden status to check if logged in
LOGGED_IN = (self.status() == 'unlocked')
self._logged_in = (self.status() == 'unlocked')

return LOGGED_IN
return self._logged_in

def cache(func):
def inner(*args, **kwargs):
self = args[0]
key = '_'.join(args[1:])

if key not in CACHE:
if key not in self._cache:
value = func(*args, **kwargs)
CACHE[key] = value
self._cache[key] = value

return CACHE[key]
return self._cache[key]

return inner

Expand Down Expand Up @@ -142,9 +142,7 @@ def _run(self, args):
return out.strip()

def sync(self):
global CACHE

CACHE = dict() # Clear cache to prevent using old values in cache
self._cache = dict() # Clear cache to prevent using old values in cache
self._run(['sync'])

def status(self):
Expand Down Expand Up @@ -176,17 +174,13 @@ def get_attachments(self, key, itemid, output):

class LookupModule(LookupBase):

def __init__(self, *args, **kwargs):
self.bw = None

# Init super
super().__init__(*args, **kwargs)

def run(self, terms, variables=None, **kwargs):
if not self.bw:
self.bw = Bitwarden(path=kwargs.get('path', 'bw'))
global bw

if not bw:
bw = Bitwarden(path=kwargs.get('path', 'bw'))

if not self.bw.logged_in:
if not bw.logged_in:
raise AnsibleError("Not logged into Bitwarden: please run "
"'bw login', or 'bw unlock' and set the "
"BW_SESSION environment variable first")
Expand All @@ -195,26 +189,26 @@ def run(self, terms, variables=None, **kwargs):
values = []

if kwargs.get('sync'):
self.bw.sync()
bw.sync()
if kwargs.get('session'):
self.bw.session = kwargs.get('session')
bw.session = kwargs.get('session')

for term in terms:
if kwargs.get('custom_field'):
values.append(self.bw.get_custom_field(term, field))
values.append(bw.get_custom_field(term, field))
elif field == 'notes':
values.append(self.bw.get_notes(term))
values.append(bw.get_notes(term))
elif kwargs.get('attachments'):
if kwargs.get('itemid'):
itemid = kwargs.get('itemid')
output = kwargs.get('output', term)
values.append(self.bw.get_attachments(term, itemid, output))
values.append(bw.get_attachments(term, itemid, output))
else:
raise AnsibleError("Missing value for - itemid - "
"Please set parameters as example: - "
"itemid='f12345-d343-4bd0-abbf-4532222' ")
else:
values.append(self.bw.get_entry(term, field))
values.append(bw.get_entry(term, field))
return values


Expand Down