1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

[PR #7564/241cc02f backport][stable-7] onepassword lookup - Make section and field case insensitive (#7661)

onepassword lookup - Make section and field case insensitive (#7564)

* onepassword lookup: Make section and field case insensitive

This was a regression in behavior when adding support for op v2.

* Return a string by default to avoid an exception if a field is missing

* Use a helper function to lower a value if possible

* Update changelog

(cherry picked from commit 241cc02fa8)

Co-authored-by: Sam Doran <sdoran@redhat.com>
This commit is contained in:
patchback[bot] 2023-12-01 18:17:44 +01:00 committed by GitHub
parent 91c8d6badc
commit 84c883e854
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 6 deletions

View file

@ -0,0 +1,4 @@
bugfixes:
- >-
onepassword lookup plugin - field and section titles are now case insensitive when using
op CLI version two or later. This matches the behavior of version one (https://github.com/ansible-collections/community.general/pull/7564).

View file

@ -127,6 +127,14 @@ from ansible.module_utils.six import with_metaclass
from ansible_collections.community.general.plugins.module_utils.onepassword import OnePasswordConfig from ansible_collections.community.general.plugins.module_utils.onepassword import OnePasswordConfig
def _lower_if_possible(value):
"""Return the lower case version value, otherwise return the value"""
try:
return value.lower()
except AttributeError:
return value
class OnePassCLIBase(with_metaclass(abc.ABCMeta, object)): class OnePassCLIBase(with_metaclass(abc.ABCMeta, object)):
bin = "op" bin = "op"
@ -480,6 +488,7 @@ class OnePassCLIv2(OnePassCLIBase):
} }
""" """
data = json.loads(data_json) data = json.loads(data_json)
field_name = _lower_if_possible(field_name)
for field in data.get("fields", []): for field in data.get("fields", []):
if section_title is None: if section_title is None:
# If the field name exists in the section, return that value # If the field name exists in the section, return that value
@ -488,17 +497,19 @@ class OnePassCLIv2(OnePassCLIBase):
# If the field name doesn't exist in the section, match on the value of "label" # If the field name doesn't exist in the section, match on the value of "label"
# then "id" and return "value" # then "id" and return "value"
if field.get("label") == field_name: if field.get("label", "").lower() == field_name:
return field.get("value", "") return field.get("value", "")
if field.get("id") == field_name: if field.get("id", "").lower() == field_name:
return field.get("value", "") return field.get("value", "")
# Look at the section data and get an identifier. The value of 'id' is either a unique ID # Look at the section data and get an identifier. The value of 'id' is either a unique ID
# or a human-readable string. If a 'label' field exists, prefer that since # or a human-readable string. If a 'label' field exists, prefer that since
# it is the value visible in the 1Password UI when both 'id' and 'label' exist. # it is the value visible in the 1Password UI when both 'id' and 'label' exist.
section = field.get("section", {}) section = field.get("section", {})
current_section_title = section.get("label", section.get("id")) section_title = _lower_if_possible(section_title)
current_section_title = section.get("label", section.get("id", "")).lower()
if section_title == current_section_title: if section_title == current_section_title:
# In the correct section. Check "label" then "id" for the desired field_name # In the correct section. Check "label" then "id" for the desired field_name
if field.get("label") == field_name: if field.get("label") == field_name:

View file

@ -107,7 +107,7 @@ MOCK_ENTRIES = {
"queries": ["Omitted values"], "queries": ["Omitted values"],
"kwargs": { "kwargs": {
"field": "section-label-without-value", "field": "section-label-without-value",
"section": "section-without-values" "section": "Section-Without-Values"
}, },
"expected": [""], "expected": [""],
"output": load_file("v2_out_04.json") "output": load_file("v2_out_04.json")

View file

@ -13,10 +13,10 @@
"additional_information": "Jan 18, 2015, 08:13:38", "additional_information": "Jan 18, 2015, 08:13:38",
"fields": [ "fields": [
{ {
"id": "password", "id": "Password",
"type": "CONCEALED", "type": "CONCEALED",
"purpose": "PASSWORD", "purpose": "PASSWORD",
"label": "password", "label": "Password",
"value": "OctoberPoppyNuttyDraperySabbath", "value": "OctoberPoppyNuttyDraperySabbath",
"reference": "op://Test Vault/Authy Backup/password", "reference": "op://Test Vault/Authy Backup/password",
"password_details": { "password_details": {