Skip to content

Commit 992c83a

Browse files
committed
Add pykeepass==4.0.1
- Replace socket data format from json to plain text - Fix fetching entities contains slashes in path - Move code into one file (socket opening by the plugin)
1 parent d8e176a commit 992c83a

10 files changed

+390
-371
lines changed

README.md

+27-45
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,51 @@
11
# Ansible KeePass Lookup Plugin
22

3-
Perhaps, from a security view point, this solution is the same as `ansible-vault`.
4-
Just if you are storing secrets data in KeePass, then why not use it,
5-
instead of duplicating to `ansible-vault`.
3+
The plugin allows to read data from KeePass file (modifying is not supported)
4+
5+
## How it works
6+
7+
The plugin opens a UNIX socket with decrypted KeePass file.
8+
For performance reasons, decryption occurs only once at socket startup,
9+
and the KeePass file remains decrypted as long as the socket is open.
10+
The UNIX socket file is stored in a temporary folder according to OS.
611

712

813
## Installation
914

10-
Dependency: `pykeepass==3.2.1`
15+
Requirements: `python 3`, `pykeepass==4.0.1`
1116

12-
pip install 'pykeepass==3.2.1' --user
17+
pip install 'pykeepass==4.0.1' --user
1318
mkdir -p ~/.ansible/plugins/lookup && cd "$_"
14-
curl https://raw.githubusercontent.com/viczem/ansible-keepass/master/keepass.py -o ./keepass.py
15-
16-
[More about ansible plugins installation](https://docs.ansible.com/ansible/latest/dev_guide/developing_locally.html)
19+
curl https://raw.githubusercontent.com/viczem/ansible-keepass/main/keepass.py -o ./keepass.py
1720

1821

1922
## Variables
2023

2124
- `keepass_dbx` - path to KeePass file
22-
- `keepass_psw` - password. [*optional*] if the socket is used
23-
- `keepass_key` - [*optional*] path to keyfile
24-
25-
26-
## Usage with UNIX socket
27-
28-
> _This usage is more preferred for performance reason,
29-
because of KeePass file stay decrypted and not need to reopen after done each playbook task
30-
[(see the issue for more info)](https://github.com/viczem/ansible-keepass/issues/1)_
31-
32-
In this case, there is no need to enter a password for KeePass each time Ansible is called.
33-
Run socket by the command and after that enter a password to make to open KeePass file.
25+
- `keepass_psw` - password
26+
- `keepass_key` - *Optional*. Path to keyfile
27+
- `keepass_ttl` - *Optional*. Socket TTL (will be closed automatically when not used).
28+
Default 60 seconds.
3429

35-
**Supported only Python 3**
3630

37-
python3 kpsock.py ~/.keepass/database.kdbx
31+
## Usage
3832

33+
`ansible-doc -t lookup keepass` to get description of the plugin
3934

40-
The command will creates UNIX socket in a system temp directory. Only one socket
41-
> **WARNING**: The KeePass file and password are stay decrypted in memory while the socket is open.
35+
> **WARNING**: For security reasons, do not store KeePass passwords in plain text.
36+
Use `ansible-vault encrypt_string` to encrypt it and use it like below
4237

43-
The socket timeout is 1 minute since past access (will be closed automatically when not used).
44-
To change timeout use `--ttl` argument.
45-
For logging requests in a file use `--log` (default `--log-level` is `INFO`).
38+
# file: group_vars/all
4639

47-
For help `python kpsock.py --help`
48-
49-
To send the running command in background press <kbd>CTRL</kbd>+<kbd>Z</kbd> and execute `bg`
50-
(`fg` to get the job into the foreground again).
51-
52-
53-
## Example
54-
55-
Define variables you need e.g. in any file in group_vars
40+
keepass_dbx: "~/.keepass/database.kdbx"
41+
keepass_psw: !vault |
42+
$ANSIBLE_VAULT;1.1;AES256
43+
...encrypted password...
5644

45+
### Example
5746

5847
ansible_user : "{{ lookup('keepass', 'path/to/entry', 'username') }}"
5948
ansible_become_pass : "{{ lookup('keepass', 'path/to/entry', 'password') }}"
60-
ansible_custom_field : "{{ lookup('keepass', 'path/to/entry', 'custom_field_property', true) }}"
61-
ansible_all_custom_fields: "{{ lookup('keepass', 'path/to/entry', '*', true) }}"
62-
63-
64-
You can get another [properties of an KeePass entry](https://github.com/pschmitt/pykeepass/blob/master/pykeepass/entry.py)
65-
(not only `username` or `password`)
49+
ansible_custom_field : "{{ lookup('keepass', 'path/to/entry', 'custom_properties', 'a_custom_property_name') }}"
6650

67-
Specify a boolean value of true to use custom field properties
68-
69-
`ansible-doc -t lookup keepass` - to get description of the plugin
51+
More examples see in [/examples](/examples).

example/README.md

-24
This file was deleted.

example/example-playbook.yml

-24
This file was deleted.

example/example.kdbx

-2.61 KB
Binary file not shown.

examples/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Example
2+
3+
`ansible-playbook example-playbook.yml --ask-vault-pass -vvv`
4+
5+
Password: `spamham`

examples/example-playbook.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
- name: Example
3+
hosts: 127.0.0.1
4+
connection: local
5+
vars:
6+
spam_login: "{{ lookup('keepass', 'spam', 'username') }}"
7+
spam_password: "{{ lookup('keepass', 'spam', 'password') }}"
8+
ham_login: "{{ lookup('keepass', 'example/ham', 'username') }}"
9+
ham_password: "{{ lookup('keepass', 'example/ham', 'password') }}"
10+
slash_login: "{{ lookup('keepass', 'slash\\/group/slash\\/title', 'username') }}"
11+
slash_url: "{{ lookup('keepass', 'slash\\/group/slash\\/title', 'url') }}"
12+
pork_custom_property: "{{ lookup('keepass', 'example/pork', 'custom_properties', 'pork_custom_property')}}"
13+
14+
15+
tasks:
16+
- debug:
17+
msg: "fetch entry: '/spam'; username: '{{ spam_login }}'; password: '{{ spam_password }}'"
18+
19+
- debug:
20+
msg: "fetch entry: '/examples/ham'; username: '{{ ham_login }}'; password: '{{ ham_password }}'"
21+
22+
- name: pause to emulate long time operation (greater than keepass_ttl)
23+
pause:
24+
seconds: 5
25+
26+
- debug:
27+
msg: "fetch entry: '/examples/pork'; custom_properties: 'pork_custom_property' - '{{ pork_custom_property }}'"
28+
29+
- debug:
30+
msg: "fetch entry: '/slash\\/group/slash\\/title'; username: '{{ slash_login }}'; url: '{{ slash_url }}'"
31+
32+
- debug: "{{ lookup('keepass', 'close') }}"

examples/example.kdbx

2.96 KB
Binary file not shown.

example/group_vars/all examples/group_vars/all

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
role_keepass_db: "./example.kdbx"
2-
3-
keepass_dbx: "{{ role_keepass_db }}"
1+
keepass_ttl: 3
2+
keepass_dbx: "./example.kdbx"
43
keepass_psw: !vault |
54
$ANSIBLE_VAULT;1.1;AES256
65
30656633313531336265353862356135373963636339376266373137376136636634393932623961

0 commit comments

Comments
 (0)