From aacd22acc64fec4f2853b3dba4859ad18354f07d Mon Sep 17 00:00:00 2001
From: Ryan Conway <ryan@rjc.cc>
Date: Wed, 5 Dec 2018 16:49:26 +0000
Subject: [PATCH] Fix an issue retrieving some types of 1Password items.
 (#47213)

* Some types of 1Password items have a 'password' field alongside the 'fields' attribute, not inside it, so we need to search there as well.

* Add changelog fragement for onepassword_facts PR #47213.
---
 ...onepassword_facts_fix_password_lookup.yaml |  2 ++
 .../modules/identity/onepassword_facts.py     | 34 ++++++++++++-------
 2 files changed, 23 insertions(+), 13 deletions(-)
 create mode 100644 changelogs/fragments/47213-onepassword_facts_fix_password_lookup.yaml

diff --git a/changelogs/fragments/47213-onepassword_facts_fix_password_lookup.yaml b/changelogs/fragments/47213-onepassword_facts_fix_password_lookup.yaml
new file mode 100644
index 00000000000..f887ea13c53
--- /dev/null
+++ b/changelogs/fragments/47213-onepassword_facts_fix_password_lookup.yaml
@@ -0,0 +1,2 @@
+bugfixes:
+  - onepassword_facts - Fix an issue looking up some 1Password items which have a 'password' attribute alongside the 'fields' attribute, not inside it.
diff --git a/lib/ansible/modules/identity/onepassword_facts.py b/lib/ansible/modules/identity/onepassword_facts.py
index 83666baa22f..59364b1b6a6 100644
--- a/lib/ansible/modules/identity/onepassword_facts.py
+++ b/lib/ansible/modules/identity/onepassword_facts.py
@@ -192,20 +192,28 @@ class OnePasswordFacts(object):
 
         else:
             # This is not a document, let's try to find the requested field
-            if section_title is None:
-                for field_data in data['details'].get('fields', []):
-                    if field_data.get('name').lower() == field_name.lower():
-                        return {field_name: field_data.get('value', '')}
 
-            # Not found it yet, so now lets see if there are any sections defined
-            # and search through those for the field. If a section was given, we skip
-            # any non-matching sections, otherwise we search them all until we find the field.
-            for section_data in data['details'].get('sections', []):
-                if section_title is not None and section_title.lower() != section_data['title'].lower():
-                    continue
-                for field_data in section_data.get('fields', []):
-                    if field_data.get('t').lower() == field_name.lower():
-                        return {field_name: field_data.get('v', '')}
+            # Some types of 1Password items have a 'password' field directly alongside the 'fields' attribute,
+            # not inside it, so we need to check there first.
+            if (field_name in data['details']):
+                return {field_name: data['details'][field_name]}
+
+            # Otherwise we continue looking inside the 'fields' attribute for the specified field.
+            else:
+                if section_title is None:
+                    for field_data in data['details'].get('fields', []):
+                        if field_data.get('name').lower() == field_name.lower():
+                            return {field_name: field_data.get('value', '')}
+
+                # Not found it yet, so now lets see if there are any sections defined
+                # and search through those for the field. If a section was given, we skip
+                # any non-matching sections, otherwise we search them all until we find the field.
+                for section_data in data['details'].get('sections', []):
+                    if section_title is not None and section_title.lower() != section_data['title'].lower():
+                        continue
+                    for field_data in section_data.get('fields', []):
+                        if field_data.get('t').lower() == field_name.lower():
+                            return {field_name: field_data.get('v', '')}
 
         # We will get here if the field could not be found in any section and the item wasn't a document to be downloaded.
         optional_section_title = '' if section_title is None else " in the section '%s'" % section_title