Make ConfigInvalid generic

The ConfigInvalid class is now used not only by basicauth middleware
but also by healthcheck middleware. Move it out from basicauth
middleware to a common place.

This change also address a few follow-up items of [1].

[1] 01cd608104

Change-Id: I1865cbfe6ff22ffb7db57cf685e340de4a3568d4
This commit is contained in:
Takashi Kajinami 2024-07-04 11:08:14 +09:00
parent 01cd608104
commit 67a0a3906c
5 changed files with 29 additions and 16 deletions

View File

@ -22,6 +22,7 @@ import webob
from oslo_config import cfg
from oslo_middleware import base
from oslo_middleware import exceptions
LOG = logging.getLogger(__name__)
@ -34,12 +35,6 @@ OPTS = [
cfg.CONF.register_opts(OPTS, group='oslo_middleware')
class ConfigInvalid(Exception):
def __init__(self, error_msg):
super().__init__(
'Invalid configuration file. %(error_msg)s')
class BasicAuthMiddleware(base.ConfigurableMiddleware):
"""Middleware which performs HTTP basic authentication on requests"""
@ -135,7 +130,8 @@ def validate_auth_file(auth_file):
if entry and ':' in entry:
parse_entry(entry)
except OSError:
raise ConfigInvalid(error_msg='Problem reading auth user file')
raise exceptions.ConfigInvalid(
error_msg='Problem reading auth user file')
def parse_entry(entry):

View File

@ -0,0 +1,17 @@
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
class ConfigInvalid(Exception):
def __init__(self, error_msg):
super().__init__(
'Invalid configuration. %(error_msg)s')

View File

@ -39,7 +39,7 @@ except ImportError:
greenlet = None
from oslo_middleware import base
from oslo_middleware.basic_auth import ConfigInvalid
from oslo_middleware.exceptions import ConfigInvalid
from oslo_middleware.healthcheck import opts
@ -422,13 +422,12 @@ Reason
def _verify_configured_plugins(self):
backends = self._conf_get('backends')
desired_plugins = ['disable_by_file', 'enable_by_files']
if set(desired_plugins).issubset(set(backends)):
raise ConfigInvalid('`enable_by_files` and `disable_by_file`'
' healthcheck middleware should not be '
'configured at once.')
exclusive_plugins = ['disable_by_file', 'enable_by_files']
if set(exclusive_plugins).issubset(set(backends)):
raise ConfigInvalid('`enable_by_files` plugin and '
'`disable_by_file` plugin should not be '
'enabled at the same time.')
def _conf_get(self, key, group='healthcheck'):
return super(Healthcheck, self)._conf_get(key, group=group)

View File

@ -21,6 +21,7 @@ from oslo_config import cfg
import webob
from oslo_middleware import basic_auth as auth
from oslo_middleware import exceptions as exc
from oslotest import base as test_base
@ -116,7 +117,7 @@ class TestAuthBasic(test_base.BaseTestCase):
auth.validate_auth_file(auth_file)
# failed, missing auth file
auth_file = auth_file + '.missing'
self.assertRaises(auth.ConfigInvalid,
self.assertRaises(exc.ConfigInvalid,
auth.validate_auth_file, auth_file)
# failed, invalid entry
auth_file = self.write_auth_file(

View File

@ -24,7 +24,7 @@ import requests
import webob.dec
import webob.exc
from oslo_middleware.basic_auth import ConfigInvalid
from oslo_middleware.exceptions import ConfigInvalid
from oslo_middleware import healthcheck
from oslo_middleware.healthcheck import __main__