From 77388a016a07748f0167916968bd8c30659452d2 Mon Sep 17 00:00:00 2001 From: "hockguan.tok" <11731575+hgtok@users.noreply.github.com> Date: Sun, 4 Aug 2024 21:53:11 +0800 Subject: [PATCH 1/3] feat: replace socket payload with json with kwargs support --- plugins/lookup/keepass.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/plugins/lookup/keepass.py b/plugins/lookup/keepass.py index 7bd4b5a..5e06bc4 100644 --- a/plugins/lookup/keepass.py +++ b/plugins/lookup/keepass.py @@ -4,6 +4,7 @@ import getpass import hashlib import fcntl +import json import os import re import socket @@ -150,9 +151,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) @@ -163,7 +165,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: @@ -230,12 +232,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 @@ -396,13 +398,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=""): From 7f5cdc81681ed8a43b3f4d841c781217e2253ea4 Mon Sep 17 00:00:00 2001 From: "hockguan.tok" <11731575+hgtok@users.noreply.github.com> Date: Sun, 4 Aug 2024 21:53:39 +0800 Subject: [PATCH 2/3] feat: add logic to search entry by title/url with regex; requires pykeepass >= 4.0.4 --- plugins/lookup/keepass.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/plugins/lookup/keepass.py b/plugins/lookup/keepass.py index 5e06bc4..cb16ca7 100644 --- a/plugins/lookup/keepass.py +++ b/plugins/lookup/keepass.py @@ -42,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() @@ -289,7 +292,14 @@ def _keepass_socket(kdbx, kdbx_key, sock_path, ttl=60, kdbx_password=None): for _ in re.split(r"(? 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( From 4a1a97f409eb9de3870a8d5213852d819c615d76 Mon Sep 17 00:00:00 2001 From: "hockguan.tok" <11731575+hgtok@users.noreply.github.com> Date: Sun, 4 Aug 2024 21:54:09 +0800 Subject: [PATCH 3/3] feat: requires at least pykeepass 4.0.4 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 31bccb5..2f84af7 100644 --- a/README.md +++ b/README.md @@ -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