From 1168973c7c34d8a3e9f9b12201314d31ce2b183c Mon Sep 17 00:00:00 2001
From: Tomas Hrcka <thrcka@redhat.com>
Date: Mar 15 2022 07:43:56 +0000
Subject: Use BZ api_key for auth


Signed-off-by: Tomas Hrcka <thrcka@redhat.com>

---

diff --git a/fedscm_admin/bugzilla.py b/fedscm_admin/bugzilla.py
index 9654239..97a6d50 100644
--- a/fedscm_admin/bugzilla.py
+++ b/fedscm_admin/bugzilla.py
@@ -18,16 +18,8 @@ Provides helper functions for Bugzilla
 from __future__ import absolute_import
 from six.moves.urllib.parse import urlencode
 from datetime import datetime
-
 import click
-from click import ClickException
 from bugzilla import Bugzilla
-# In version 2, BugzillaError was moved to bugzilla.transport
-try:
-    from bugzilla.transport import BugzillaError
-except ImportError:
-    from bugzilla import BugzillaError
-
 from . import FAS_CLIENT, is_epel
 from .exceptions import ValidationError
 
@@ -40,6 +32,7 @@ class BugzillaClient(object):
         self.url = 'https://bugzilla.redhat.com'
         self.api_url = '{0}/xmlrpc.cgi'.format(self.url)
         self._client = None
+        self.api_key = None
         self.pagure_namespace_to_component = {
             'rpms': 'Package Review',
             'container': 'Container Review',
@@ -59,26 +52,9 @@ class BugzillaClient(object):
         Returns the initialized client
         """
         if self._client is None:
-            try:
-                self._client = Bugzilla(self.api_url, use_creds=True)
-            except TypeError:
-                self._client = Bugzilla(self.api_url)
-        return self._client
+            self._client = Bugzilla(self.api_url)
 
-    def login(self, username, password):
-        """
-        A login function to Bugzilla. If the password is incorrect, a
-        ClickException is raised.
-        :param username: a string of the username
-        :param password: a string of the password
-        :return: None
-        """
-        try:
-            self.client.login(username, password)
-        except BugzillaError:
-            raise ClickException(
-                'The login to Bugzilla failed. Please make sure you typed in '
-                'the correct credentials.')
+        return self._client
 
     @staticmethod
     def get_fas_user_by_bz_email(email):
@@ -113,8 +89,6 @@ class BugzillaClient(object):
         """
         # When authenticated, you get a user's email address instead of their
         # full name
-        if require_auth and not self.client.logged_in:
-            raise ValidationError('The Bugzilla client is not authenticated')
 
         try:
             bug = self.client.getbug(bug_id)
diff --git a/fedscm_admin/utils.py b/fedscm_admin/utils.py
index 7e9f306..1cfd34d 100644
--- a/fedscm_admin/utils.py
+++ b/fedscm_admin/utils.py
@@ -24,7 +24,6 @@ from datetime import datetime
 
 import click
 from six import string_types
-from six.moves import xmlrpc_client
 
 from . import CONFIG, BUGZILLA_CLIENT, FAS_CLIENT, INVALID_EPEL_ERROR
 from . import MONITOR_CHOICES, STANDARD_BRANCH_SLAS, git, is_epel, pagure, pdc
@@ -36,25 +35,12 @@ from .pagure import get_project_default_branch
 
 def login_to_bugzilla_with_user_input():
     """
-    A helper function to prompt for the username and password to Bugzilla and
+    A helper function to prompt for the api_key to Bugzilla and
     login to Bugzilla
     :return: None
     """
-    logged_in = False
-    # An exception is thrown when the session cache is expired. Systems without
-    # the session cached at all, will return False.
-    try:
-        logged_in = BUGZILLA_CLIENT.client.logged_in
-    except xmlrpc_client.Error:
-        pass
-
-    if not logged_in:
-        username = click.prompt('Please enter your Bugzilla username')
-        password = click.prompt(
-            'Please enter your Bugzilla password', hide_input=True)
-        BUGZILLA_CLIENT.login(username, password)
-        # Force the password out of RAM
-        del password
+
+    BUGZILLA_CLIENT.client.interactive_save_api_key()
 
 
 def login_to_fas_with_user_input():
diff --git a/setup.py b/setup.py
index e4e9da9..ea90535 100644
--- a/setup.py
+++ b/setup.py
@@ -20,5 +20,5 @@ setup(
     packages=['fedscm_admin'],
     package_dir={'fedscm_admin': 'fedscm_admin'},
     url='https://pagure.io/fedscm_admin',
-    version='1.1.4',
+    version='1.1.5',
 )
diff --git a/tests/mock_values.py b/tests/mock_values.py
index 12395ec..480cb2a 100644
--- a/tests/mock_values.py
+++ b/tests/mock_values.py
@@ -29,6 +29,9 @@ class MockBugzilla(object):
     def login(self, *args, **kwargs):
         self.logged_in = True
 
+    def interactive_save_api_key(url):
+        return True
+
     def getbug(self, bug_id):
         if self.logged_in:
             reviewer = 'mjordan@redhat.com'
diff --git a/tests/test_admin.py b/tests/test_admin.py
index 7e75e58..cecbe3b 100644
--- a/tests/test_admin.py
+++ b/tests/test_admin.py
@@ -127,8 +127,7 @@ class FedScmAdmin(TestCase):
             runner = CliRunner()
             result = runner.invoke(
                 fedscm_admin_cli, ['process', '2'],
-                input='mprahl\n12345\nmprahl\n12345\napprove\nn\n')
-
+                input='mprahl\n12345\nmprahl12345\napprove\napprove\n')
         assert result.exit_code == 0
         assert result.output.count('- Adding comment to Pagure issue') == 1
         assert result.output.count('- Adding comment to rhbz#') == 1
@@ -171,7 +170,7 @@ class FedScmAdmin(TestCase):
         runner = CliRunner()
         result = runner.invoke(
             fedscm_admin_cli, ['process', '2'],
-            input='mprahl\n12345\nmprahl\n12345\napprove\nn\n')
+            input='mprahl\nmprahl12345\napprove\nn\n')
         assert result.exit_code == 0
         assert result.output.count('- Adding comment to Pagure issue') == 1
         assert result.output.count('- Closing Pagure issue 2') == 1
@@ -340,7 +339,7 @@ class FedScmAdmin(TestCase):
         runner = CliRunner()
         result = runner.invoke(
             fedscm_admin_cli, ['process', '2'],
-            input='mprahl\n12345\nmprahl\n12345\napprove\nn\n')
+            input='mprahl\n12345mprahl12345\napprove\nn\n')
         assert result.exit_code == 0
         assert 'New Repo for "rpms/nethack"' in result.output
         assert 'The Pagure repository was created' in result.output
@@ -784,7 +783,8 @@ class FedScmAdmin(TestCase):
             runner = CliRunner()
             result = runner.invoke(
                 fedscm_admin_cli, ['processall'],
-                input='mprahl\n12345\nmprahl\n12345\napprove\napprove\n')
+                input='mprahl\n12345\nmprahl12345\napprove\napprove\n')
+
         assert result.exit_code == 0
         assert 'New Branch "abc" for "rpms/nethack"' in result.output
         assert 'New Repo for "rpms/nethack"' in result.output
@@ -831,7 +831,7 @@ class FedScmAdmin(TestCase):
         runner = CliRunner()
         result = runner.invoke(
             fedscm_admin_cli, ['process', '2', '--force', '--auto-approve'],
-            input='mprahl\n12345\nmprahl\n12345\n')
+            input='mprahl\n12345mprahl12345\n')
         assert result.exit_code == 0
         expected = ('Auto-approving the new branch request for "{0}" on '
                     'rpms/nethack'.format(branch_name))
@@ -859,53 +859,6 @@ class FedScmAdmin(TestCase):
         url = fedscm_admin.pagure.get_scm_requests_git_url(username='joe')
         self.assertEqual(url, target)
 
-    @patch('fedscm_admin.request_utils.retry_session')
-    def test_fas_bad_password(self, mock_retry_session):
-        from fedscm_admin.fedscm_admin import cli as fedscm_admin_cli
-        from fedscm_admin import FAS_CLIENT
-        from fedscm_admin import config
-
-        mock_session = Mock()
-        mock_session.get.side_effect = [
-            mock_values.get_mock_issue_rv(
-                'abc', sla={'security_fixes': '2025-12-01'})]
-        mock_retry_session.return_value = mock_session
-
-        # fasjson_client.Client is already mocked in setup, no need to mock it again
-        FAS_CLIENT.client.verify_password.return_value = False
-
-        with patch.object(config, 'get_config_item') as mock_get_config_item:
-            mock_get_config_item.return_value = False
-            runner = CliRunner()
-            result = runner.invoke(
-                fedscm_admin_cli, ['process', '2'],
-                input='mprahl\n12345\n')
-            assert ('Error: The login to FAS failed. Please make sure you '
-                    'typed in the correct credentials.') in result.output
-            assert result.exit_code == 1
-
-    @patch('fedscm_admin.request_utils.retry_session')
-    def test_bugzilla_bad_password(self, mock_retry_session):
-        from fedscm_admin.fedscm_admin import cli as fedscm_admin_cli
-        from fedscm_admin.bugzilla import BugzillaError
-        from fedscm_admin import BUGZILLA_CLIENT
-        mock_session = Mock()
-        mock_session.get.side_effect = [
-            mock_values.get_mock_issue_rv(
-                'abc', sla={'security_fixes': '2025-12-01'})]
-        mock_retry_session.return_value = mock_session
-
-        with patch.object(BUGZILLA_CLIENT.client, 'login') as mock_verify:
-            mock_verify.side_effect = BugzillaError('Invalid credentials')
-            runner = CliRunner()
-            result = runner.invoke(
-                fedscm_admin_cli, ['process', '2'],
-                input='mprahl\n12345\nmprahl\n12345\n')
-            error = ('Error: The login to Bugzilla failed. Please make sure '
-                     'you typed in the correct credentials.')
-            assert error in result.output
-            assert result.exit_code == 1
-
     @patch('fedscm_admin.utils.verify_slas', return_value=None)
     @patch('fedscm_admin.request_utils.retry_session')
     def test_fedscm_admin_process_epel_wrong_bz_product(

