]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: Reverse proxy changes
authorKanika Murarka <kmurarka@redhat.com>
Thu, 4 Oct 2018 16:05:16 +0000 (21:35 +0530)
committerKanika Murarka <kmurarka@redhat.com>
Thu, 4 Oct 2018 16:05:16 +0000 (21:35 +0530)
Fixes: https://tracker.ceph.com/issues/24999
Signed-off-by: Kanika Murarka <kmurarka@redhat.com>
src/pybind/mgr/dashboard/controllers/grafana.py
src/pybind/mgr/dashboard/settings.py
src/pybind/mgr/dashboard/tests/test_grafana.py

index 03fdfbed4d36f7cda6f7e5bae1686abc55319087..a18dd9c18534b48f9bcf961cab1f5a4053c08db0 100644 (file)
@@ -14,32 +14,28 @@ from ..settings import Settings
 class GrafanaRestClient(object):
     _instance = None
 
-    def _raise_for_validation(self):
+    @staticmethod
+    def _raise_for_validation(url, user, password):
         msg = 'No {} found or misconfigured, please consult the ' \
               'documentation about how to configure Grafana for the dashboard.'
 
-        o = urlparse(self._url)
+        o = urlparse(url)
         if not (o.netloc and o.scheme):
             raise LookupError(msg.format('URL'))
 
-        auth_method = 'password' if self._token is None else 'token'
-        if auth_method == 'password' and not all((self._user, self._password)):
-            raise LookupError(msg.format('username and/or password'))
-        elif auth_method == 'token' and not self._token:
-            raise LookupError(msg.format('token'))
+        if not all((user, password)):
+            raise LookupError(msg.format('credentials'))
 
-    def __init__(self, url, username=None, password=None, token=None):
+    def __init__(self, url, username, password):
         """
         :type url: str
         :type username: str
         :type password: str
         """
+        self._raise_for_validation(url, username, password)
         self._url = url.rstrip('/')
         self._user = username
         self._password = password
-        self._token = token
-
-        self._raise_for_validation()
 
     @classmethod
     def instance(cls):
@@ -50,19 +46,11 @@ class GrafanaRestClient(object):
         :rtype: GrafanaRestClient
         """
         if not cls._instance:
-            kwargs = {}
-            if Settings.GRAFANA_API_AUTH_METHOD.lower() == 'password':
-                kwargs['username'] = Settings.GRAFANA_API_USERNAME
-                kwargs['password'] = Settings.GRAFANA_API_PASSWORD
-            elif Settings.GRAFANA_API_AUTH_METHOD.lower() == 'token':
-                kwargs['token'] = Settings.GRAFANA_API_TOKEN
-            else:
-                raise LookupError('No or unknown authentication method '
-                                  'provided. Please consult the documentation '
-                                  'about how to configure the '
-                                  'Grafana integration correctly.')
-            cls._instance = GrafanaRestClient(Settings.GRAFANA_API_URL,
-                                              **kwargs)
+            url = Settings.GRAFANA_API_URL
+            user = Settings.GRAFANA_API_USERNAME
+            password = Settings.GRAFANA_API_PASSWORD
+
+            cls._instance = GrafanaRestClient(url, user, password)
 
         return cls._instance
 
@@ -73,19 +61,13 @@ class GrafanaRestClient(object):
         headers = {k: v for k, v in cherrypy.request.headers.items()
                    if k.lower() in ('content-type', 'accept')}
 
-        auth = None
-        if self._token:
-            headers['Authorization'] = 'Bearer {}'.format(self._token)
-        else:
-            auth = (self._user, self._password)
-
         response = requests.request(
             method,
             url,
             params=params,
             data=data,
             headers=headers,
-            auth=auth)
+            auth=(self._user, self._password))
         logger.debug("proxying method=%s path=%s params=%s data=%s", method,
                      path, params, data)
 
index c28e31d11ab86d8ad56baef2e59244917132bce3..953e918234b1f5ab569a8e19601e11a1472dead5 100644 (file)
@@ -35,8 +35,6 @@ class Options(object):
     GRAFANA_API_URL = ('', str)
     GRAFANA_API_USERNAME = ('admin', str)
     GRAFANA_API_PASSWORD = ('admin', str)
-    GRAFANA_API_TOKEN = ('', str)
-    GRAFANA_API_AUTH_METHOD = ('', str)  # Either 'password' or 'token'
 
     @staticmethod
     def has_default_value(name):
index f88cb72635808e5a8abc04b3434a49260966a9c6..1a0665269b4f300d115def571d30521a2a825ad4 100644 (file)
@@ -11,21 +11,15 @@ from .helper import ControllerTestCase
 
 class Grafana(TestCase):
     def test_missing_credentials(self):
-        with six.assertRaisesRegex(self, LookupError,
-                                   r'username and/or password'):
+        with six.assertRaisesRegex(self, LookupError, r'^No credentials.*'):
             GrafanaRestClient(
                 url='http://localhost:3000', username='', password='admin')
-        with six.assertRaisesRegex(self, LookupError, r'token'):
-            GrafanaRestClient(
-                url='http://localhost:3000',
-                token='',
-            )
         with six.assertRaisesRegex(self, LookupError, r'^No URL.*'):
             GrafanaRestClient(
                 url='//localhost:3000', username='admin', password='admin')
 
 
-@Controller('/grafana/mocked', secure=False)
+@Controller('grafana/mocked', secure=False)
 class GrafanaMockInstance(BaseController):
     @Proxy()
     def __call__(self, path, **params):
@@ -39,8 +33,7 @@ class GrafanaControllerTestCase(ControllerTestCase):
         settings = {
             'GRAFANA_API_URL': 'http://localhost:{}/grafana/mocked/'.format(54583),
             'GRAFANA_API_USERNAME': 'admin',
-            'GRAFANA_API_PASSWORD': 'admin',
-            'GRAFANA_API_AUTH_METHOD': 'password',
+            'GRAFANA_API_PASSWORD': 'admin'
         }
         mgr.get_config.side_effect = settings.get
         GrafanaProxy._cp_config['tools.authenticate.on'] = False  # pylint: disable=protected-access