teach bitwarden lookup about custom fields

This makes it easier to look up the values of custom fields in a
Bitwarden entry. If the `custom_field` named parameter is true, search
for the named `field` in the list of custom fields rather than the
top-level dictionary.
This commit is contained in:
Lars Kellogg-Stedman 2018-10-19 16:32:14 -04:00
parent a853a212ab
commit 87616af387
2 changed files with 51 additions and 13 deletions

View file

@ -79,6 +79,13 @@ TASK [debug] *********************************************************
ok: [localhost] => { ok: [localhost] => {
"msg": { "msg": {
"favorite": false, "favorite": false,
"fields": [
{
"name": "mycustomfield",
"type": 0,
"value": "the value of my custom field"
}
],
"folderId": null, "folderId": null,
"id": "12345678-0123-4321-0000-a97001342c31", "id": "12345678-0123-4321-0000-a97001342c31",
"login": { "login": {
@ -96,3 +103,20 @@ ok: [localhost] => {
} }
} }
``` ```
### Get the value of a custom field
```yaml
# Get the value of a custom field
- debug:
msg: {{ lookup('bitwarden', 'Google', field='mycustomfield', custom_field=true }}
```
The above might result in:
```
TASK [debug] *********************************************************
ok: [localhost] => {
"msg": "the value of my custom field"
}
```

View file

@ -9,6 +9,22 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import json
import os
import sys
from subprocess import Popen, PIPE, check_output
from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
try:
from __main__ import display
except ImportError:
from ansible.utils.display import Display
display = Display()
DOCUMENTATION = """ DOCUMENTATION = """
lookup: bitwarden lookup: bitwarden
author: author:
@ -26,6 +42,9 @@ DOCUMENTATION = """
field: field:
description: field to return from bitwarden description: field to return from bitwarden
default: 'password' default: 'password'
custom_field:
description: If True, look up named field in custom fields instead
of top-level dictionary.
""" """
EXAMPLES = """ EXAMPLES = """
@ -40,18 +59,6 @@ RETURN = """
- Items from Bitwarden vault - Items from Bitwarden vault
""" """
from subprocess import Popen, PIPE, check_output
import os, sys
from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
try:
from __main__ import display
except ImportError:
from ansible.utils.display import Display
display = Display()
class Bitwarden(object): class Bitwarden(object):
@ -91,6 +98,10 @@ class Bitwarden(object):
def get_entry(self, key, field): def get_entry(self, key, field):
return self._run(["get", field, key]) return self._run(["get", field, key])
def get_custom_field(self, key, field):
data = json.loads(self.get_entry(key, 'item'))
return next(x for x in data['fields'] if x['name'] == field)['value']
class LookupModule(LookupBase): class LookupModule(LookupBase):
@ -103,7 +114,10 @@ class LookupModule(LookupBase):
field = kwargs.get('field', 'password') field = kwargs.get('field', 'password')
values = [] values = []
for term in terms: for term in terms:
values.append(bw.get_entry(term, field)) if kwargs.get('custom_field'):
values.append(bw.get_custom_field(term, field))
else:
values.append(bw.get_entry(term, field))
return values return values