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

feat: add logic to search entry by title/url with regex #53

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ The UNIX socket file is stored in a temporary folder according to OS.

## Installation

Requirements: `python 3`, `pykeepass==4.0.3`
Requirements: `python 3`, `pykeepass>=4.0.4`

pip install 'pykeepass==4.0.3' --user
pip install 'pykeepass>=4.0.4' --user
ansible-galaxy collection install viczem.keepass


Expand Down
28 changes: 20 additions & 8 deletions plugins/lookup/keepass.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import getpass
import hashlib
import fcntl
import json
import os
import re
import socket
Expand Down Expand Up @@ -41,6 +42,9 @@
- "{{ lookup('keepass', 'path/to/entry', 'password') }}"
- "{{ lookup('keepass', 'path/to/entry', 'custom_properties', 'my_prop_name') }}"
- "{{ lookup('keepass', 'path/to/entry', 'attachments', 'my_file_name') }}"
- "{{ lookup('keepass', 'entry', 'username') }}"
- "{{ lookup('keepass', 'entry', 'password', regex=true) }}"
- "{{ lookup('keepass', 'entry', 'password', url='github.com', regex=true) }}"
"""

display = Display()
Expand Down Expand Up @@ -150,9 +154,10 @@ def run(self, terms, variables=None, **kwargs):
return []
else:
# Fetching data from the keepass socket
return self._send(socket_path, "fetch", terms)
return self._send(socket_path, "fetch", terms, **kwargs)

def _send(self, kp_soc, cmd, terms, **kwargs):

def _send(self, kp_soc, cmd, terms):
display.vvv("KeePass: connect to '%s'" % kp_soc)
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

Expand All @@ -163,7 +168,7 @@ def _send(self, kp_soc, cmd, terms):

try:
display.vvv("KeePass: %s %s" % (cmd, terms))
sock.send(_rq(cmd, *terms))
sock.send(_rq(cmd, *terms, **kwargs))

data = b''
while True:
Expand Down Expand Up @@ -230,12 +235,12 @@ def _keepass_socket(kdbx, kdbx_key, sock_path, ttl=60, kdbx_password=None):
if not data:
break

rq = data.splitlines()
rq = json.loads(data)
if len(rq) == 0:
conn.send(_resp("", 1, "empty request"))
break

cmd, *arg = rq
cmd, arg, kwargs = rq['cmd'], rq['arg'], rq['kwargs']
arg_len = len(arg)

# CMD: quit | exit | close
Expand Down Expand Up @@ -287,7 +292,14 @@ def _keepass_socket(kdbx, kdbx_key, sock_path, ttl=60, kdbx_password=None):
for _ in re.split(r"(?<!\\)/", arg[0])
if _ != ""
]
entry = kp.find_entries_by_path(path, first=True)
if 'first' not in kwargs:
kwargs['first'] = True
if len(path) > 1:
entry = kp.find_entries(path=path, **kwargs)
elif len(path) == 1:
entry = kp.find_entries(title=path[0], **kwargs)
else:
entry = kp.find_entries(**kwargs)

if entry is None:
conn.send(
Expand Down Expand Up @@ -396,13 +408,13 @@ def _keepass_socket(kdbx, kdbx_key, sock_path, ttl=60, kdbx_password=None):
os.remove(lock_file_)


def _rq(cmd, *arg):
def _rq(cmd, *arg, **kwargs):
"""Request to keepass socket

:param str cmd: Command name
:param arg: Arguments
"""
return "\n".join((cmd, *arg)).encode()
return json.dumps({'cmd': cmd, 'arg': arg, 'kwargs': kwargs}).encode()


def _resp(cmd, status_code, payload=""):
Expand Down