Skip to content

Commit f55e4d8

Browse files
committed
Fix #33 Password shall not be mandatory for opening the db
1 parent c622672 commit f55e4d8

File tree

17 files changed

+82
-12
lines changed

17 files changed

+82
-12
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Requirements: `python 3`, `pykeepass==4.0.3`
2020
## Variables
2121

2222
- `keepass_dbx` - path to KeePass file
23-
- `keepass_psw` - password
24-
- `keepass_key` - *Optional*. Path to keyfile
23+
- `keepass_psw` - *Optional*. Password (required if `keepass_key` is not set)
24+
- `keepass_key` - *Optional*. Path to keyfile (required if `keepass_psw` is not set)
2525
- `keepass_ttl` - *Optional*. Socket TTL (will be closed automatically when not used).
2626
Default 60 seconds.
2727

galaxy.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace: viczem
88
name: keepass
99

1010
# The version of the collection. Must be compatible with semantic versioning
11-
version: 0.7.0
11+
version: 0.7.1
1212

1313
# The path to the Markdown (.md) readme file. This path is relative to the root of the collection
1414
readme: README.md

plugins/lookup/keepass.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
DOCUMENTATION = """
2222
lookup: keepass
2323
author: Victor Zemtsov <[email protected]>
24-
version_added: '0.6.0'
24+
version_added: '0.7.1'
2525
short_description: Fetching data from KeePass file
2626
description:
2727
- This lookup returns a value of a property of a KeePass entry
@@ -76,10 +76,11 @@ def run(self, terms, variables=None, **kwargs):
7676
if not os.path.isfile(var_key):
7777
raise AnsibleError("KeePass: '%s' is not found" % var_key)
7878

79-
# Check password (required)
79+
# Check password (optional)
8080
var_psw = self._var(variables_.get("keepass_psw", ""))
81-
if not var_psw:
82-
raise AnsibleError("KeePass: 'keepass_psw' is not set")
81+
82+
if not var_key and not var_psw:
83+
raise AnsibleError("KeePass: 'keepass_psw' and/or 'keepass_key' is not set")
8384

8485
# TTL of keepass socket (optional, default: 60 seconds)
8586
var_ttl = self._var(str(variables_.get("keepass_ttl", "60")))
@@ -121,7 +122,6 @@ def run(self, terms, variables=None, **kwargs):
121122
if resp[1] == "0":
122123
success = True
123124
else:
124-
sock.send(_rq("close"))
125125
raise AnsibleError("KeePass: wrong dbx password")
126126
sock.close()
127127
break
@@ -229,13 +229,14 @@ def _keepass_socket(kdbx, kdbx_key, sock_path, ttl=60, kdbx_password=None):
229229

230230
# CMD: password
231231
if kp is None:
232-
if arg_len == 0:
233-
conn.send(_resp("password", 1))
234-
break
235-
if cmd == "password" and arg[0]:
232+
if cmd == "password" and arg_len > 0:
236233
kp = PyKeePass(kdbx, arg[0], kdbx_key)
237234
conn.send(_resp("password", 0))
238235
break
236+
elif cmd == "password" and kdbx_key:
237+
kp = PyKeePass(kdbx, None, kdbx_key)
238+
conn.send(_resp("password", 0))
239+
break
239240
else:
240241
conn.send(_resp("password", 1))
241242
break
1.47 KB
Binary file not shown.
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<KeyFile>
3+
<Meta>
4+
<Version>2.0</Version>
5+
</Meta>
6+
<Key>
7+
<Data Hash="95ED5C71">
8+
8810353D 83453EDC 2266A931 A0A073F9
9+
54B90B68 1E341EF4 6B47729B F42DBE0A
10+
</Data>
11+
</Key>
12+
</KeyFile>

tests/keepass-keyfile-only/hosts.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[test]
2+
127.0.0.1 keepass_dbx=./ansible.kdbx keepass_key=./ansible.keyx keepass_ttl=3
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
- name: test-keepass-keyfile-only
3+
hosts: test
4+
connection: local
5+
vars:
6+
test_username: "{{ lookup('viczem.keepass.keepass', 'test', 'username') }}"
7+
test_password: "{{ lookup('viczem.keepass.keepass', 'test', 'password') }}"
8+
9+
tasks:
10+
- debug:
11+
msg: "fetch entry: '/test'; username: '{{ test_username }}'; password: '{{ test_password }}'"

tests/keepass-keyfile-only/run.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
ansible-playbook -i hosts.ini -vvvv playbook.yml
1.57 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<KeyFile>
3+
<Meta>
4+
<Version>2.0</Version>
5+
</Meta>
6+
<Key>
7+
<Data Hash="E13D7CBE">
8+
D7A7EA4F D6DCBFD7 B2DFE21C E89FFBB0
9+
B203AAA5 4A32C405 D6C1B3CA B69C40BF
10+
</Data>
11+
</Key>
12+
</KeyFile>
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[test]
2+
127.0.0.1 keepass_dbx=./ansible.kdbx keepass_psw=spamham keepass_key=./ansible.keyx keepass_ttl=3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
- name: test-keepass-keyfile-only
3+
hosts: test
4+
connection: local
5+
vars:
6+
test_username: "{{ lookup('viczem.keepass.keepass', 'test', 'username') }}"
7+
test_password: "{{ lookup('viczem.keepass.keepass', 'test', 'password') }}"
8+
9+
tasks:
10+
- debug:
11+
msg: "fetch entry: '/test'; username: '{{ test_username }}'; password: '{{ test_password }}'"

tests/keepass-password-keyfile/run.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
ansible-playbook -i hosts.ini -vvvv playbook.yml
1.57 KB
Binary file not shown.

tests/keepass-password-only/hosts.ini

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[test]
2+
127.0.0.1 keepass_dbx=./ansible.kdbx keepass_psw=spamham keepass_ttl=3
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
- name: test-keepass-keyfile-only
3+
hosts: test
4+
connection: local
5+
vars:
6+
test_username: "{{ lookup('viczem.keepass.keepass', 'test', 'username') }}"
7+
test_password: "{{ lookup('viczem.keepass.keepass', 'test', 'password') }}"
8+
9+
tasks:
10+
- debug:
11+
msg: "fetch entry: '/test'; username: '{{ test_username }}'; password: '{{ test_password }}'"

tests/keepass-password-only/run.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
ansible-playbook -i hosts.ini -vvvv playbook.yml

0 commit comments

Comments
 (0)