From 87616af38774daa565af0cdaf8d507970ae70e1f Mon Sep 17 00:00:00 2001 From: Lars Kellogg-Stedman Date: Fri, 19 Oct 2018 16:32:14 -0400 Subject: [PATCH] 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. --- README.md | 24 ++++++++++++++++++++++ lookup_plugins/bitwarden.py | 40 +++++++++++++++++++++++++------------ 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index d9bac92..dbd7f68 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,13 @@ TASK [debug] ********************************************************* ok: [localhost] => { "msg": { "favorite": false, + "fields": [ + { + "name": "mycustomfield", + "type": 0, + "value": "the value of my custom field" + } + ], "folderId": null, "id": "12345678-0123-4321-0000-a97001342c31", "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" + } +``` diff --git a/lookup_plugins/bitwarden.py b/lookup_plugins/bitwarden.py index 1fb605a..e2feab4 100755 --- a/lookup_plugins/bitwarden.py +++ b/lookup_plugins/bitwarden.py @@ -9,6 +9,22 @@ from __future__ import (absolute_import, division, print_function) __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 = """ lookup: bitwarden author: @@ -26,6 +42,9 @@ DOCUMENTATION = """ field: description: field to return from bitwarden default: 'password' + custom_field: + description: If True, look up named field in custom fields instead + of top-level dictionary. """ EXAMPLES = """ @@ -40,18 +59,6 @@ RETURN = """ - 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): @@ -91,6 +98,10 @@ class Bitwarden(object): def get_entry(self, key, field): 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): @@ -103,7 +114,10 @@ class LookupModule(LookupBase): field = kwargs.get('field', 'password') values = [] 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