-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dangoncalves-handle_file_attached' into develop
- Loading branch information
Showing
4 changed files
with
47 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
DOCUMENTATION = """ | ||
lookup: keepass | ||
author: Victor Zemtsov <[email protected]> | ||
version_added: '0.5.1' | ||
version_added: '0.6.0' | ||
short_description: Fetching data from KeePass file | ||
description: | ||
- This lookup returns a value of a property of a KeePass entry | ||
|
@@ -39,6 +39,7 @@ | |
- "{{ lookup('keepass', 'path/to/entry', 'username') }}" | ||
- "{{ 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') }}" | ||
""" | ||
|
||
display = Display() | ||
|
@@ -186,6 +187,7 @@ def _keepass_socket(kdbx, kdbx_key, sock_path, ttl=60, kdbx_password=None): | |
Socket messages have multiline format. | ||
First line is a command for both messages are request and response | ||
""" | ||
tmp_files = [] | ||
try: | ||
os.umask(0o177) | ||
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: | ||
|
@@ -302,6 +304,40 @@ def _keepass_socket(kdbx, kdbx_key, sock_path, ttl=60, kdbx_password=None): | |
) | ||
) | ||
break | ||
if prop == "attachments": | ||
if arg_len == 2: | ||
conn.send( | ||
_resp( | ||
"fetch", | ||
1, | ||
"attachment key is not set for '%s'" % arg[0], | ||
) | ||
) | ||
break | ||
|
||
prop_key = arg[2] | ||
attachment = None | ||
for _ in entry.attachments: | ||
if _.filename == prop_key: | ||
attachment = _ | ||
break | ||
if attachment is None: | ||
conn.send( | ||
_resp( | ||
"fetch", | ||
1, | ||
"attachment '%s' is not found " | ||
"for '%s'" % (prop_key, path), | ||
) | ||
) | ||
break | ||
|
||
tmp_file = tempfile.mkstemp(f".{attachment.filename}")[1] | ||
with open(tmp_file, "wb") as f: | ||
f.write(attachment.data) | ||
tmp_files.append(tmp_file) | ||
conn.send(_resp("fetch", 0, tmp_file)) | ||
break | ||
|
||
if not hasattr(entry, prop): | ||
conn.send( | ||
|
@@ -327,6 +363,9 @@ def _keepass_socket(kdbx, kdbx_key, sock_path, ttl=60, kdbx_password=None): | |
except KeyboardInterrupt: | ||
pass | ||
finally: | ||
for tmp_file in tmp_files: | ||
if os.path.exists(tmp_file): | ||
os.remove(tmp_file) | ||
if os.path.exists(sock_path): | ||
os.remove(sock_path) | ||
|
||
|