From e843f3a734184e9c31af8cc22e1f30521d16af27 Mon Sep 17 00:00:00 2001
From: Salvatore Orlando <salvatore.orlando@broadcom.com>
Date: Wed, 18 Dec 2024 13:27:33 +0000
Subject: [PATCH] Encode username and password for basic auth

This is to ensure session creation also works if username and
password contain unicode characters.
Otherwise, python's requests will use latin-1 to encode these string.

Change-Id: Ia2a1c26cffe213d23cafcf5c33b6f82b8ef364e5
---
 vmware_nsxlib/tests/unit/v3/test_cluster.py | 4 +++-
 vmware_nsxlib/v3/cluster.py                 | 3 ++-
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/vmware_nsxlib/tests/unit/v3/test_cluster.py b/vmware_nsxlib/tests/unit/v3/test_cluster.py
index f5035d49..ed00105c 100644
--- a/vmware_nsxlib/tests/unit/v3/test_cluster.py
+++ b/vmware_nsxlib/tests/unit/v3/test_cluster.py
@@ -74,7 +74,9 @@ class RequestsHTTPProviderTestCase(unittest.TestCase):
                 mock_api, cluster.Provider('9.8.7.6', 'https://9.8.7.6',
                                            'nsxuser', password, None))
 
-            self.assertEqual(('nsxuser', password), session.auth)
+            self.assertEqual(('nsxuser'.encode('utf-8'),
+                              password.encode('utf-8')),
+                             session.auth)
             self.assertFalse(session.verify)
             self.assertIsNone(session.cert)
             self.assertEqual(100,
diff --git a/vmware_nsxlib/v3/cluster.py b/vmware_nsxlib/v3/cluster.py
index 0c809ee5..88996512 100644
--- a/vmware_nsxlib/v3/cluster.py
+++ b/vmware_nsxlib/v3/cluster.py
@@ -214,7 +214,8 @@ class NSXRequestsHTTPProvider(AbstractHTTPProvider):
         # Set the headers with Auth info when token provider is set,
         # otherwise set the username and password
         elif not config.token_provider:
-            session.auth = (provider.username, provider.password)
+            session.auth = (provider.username.encode('utf-8'),
+                            provider.password.encode('utf-8'))
 
         # NSX v3 doesn't use redirects
         session.max_redirects = 0