From 8e139e2fe95eef75dd5bf5f4082a78aa6698d815 Mon Sep 17 00:00:00 2001
From: Thomas Woerner <twoerner@redhat.com>
Date: Fri, 28 Jun 2024 16:35:44 +0200
Subject: [PATCH] plugins/inventory/freeipa: Try imports for requests and
 urllib3

The bindings for requests and urllib3 might not be available, especially
in the ansible-test fake execution test (next version). These imports are
now in a try exception clause to make sure that the fake execution test
will be passing and also that there is a better error message if the
bindings are missing.

urllib3.exceptions.InsecureRequestWarning is now also only disabled if
no certificate has been given for the verification of the connection.
---
 plugins/inventory/freeipa.py | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/plugins/inventory/freeipa.py b/plugins/inventory/freeipa.py
index 887670d9..f17421be 100644
--- a/plugins/inventory/freeipa.py
+++ b/plugins/inventory/freeipa.py
@@ -92,12 +92,14 @@ verify: ca.crt
 """
 
 import os
-import requests
 try:
-    from requests.packages import urllib3
+    import requests
 except ImportError:
+    requests = None
+try:
     import urllib3
-urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
+except ImportError:
+    urllib3 = None
 
 from ansible import constants
 from ansible.errors import AnsibleParserError
@@ -125,6 +127,11 @@ class InventoryModule(BaseInventoryPlugin):
         self._read_config_data(path)  # This also loads the cache
 
         self.get_option("plugin")
+
+        if requests is None:
+            raise AnsibleParserError("The required Python library "
+                                     "'requests' could not be imported.")
+
         ipaadmin_principal = self.get_option("ipaadmin_principal")
         ipaadmin_password = self.get_option("ipaadmin_password")
         server = self.get_option("server")
@@ -137,6 +144,11 @@ class InventoryModule(BaseInventoryPlugin):
                 raise AnsibleParserError("ERROR: Could not load %s" % verify)
         else:
             verify = False
+            # Disable certificate verification warning without certificate
+            # as long as urllib3 could have been loaded.
+            if urllib3 is not None:
+                urllib3.disable_warnings(
+                    urllib3.exceptions.InsecureRequestWarning)
 
         self.inventory.add_group(inventory_group)
 
-- 
GitLab