Fix enabling authentication via --config

Because of an ordering problem, the configuration file provided via
--config (as opposed to environment) is read after WSGI is set up.
This change fixes it by moving Application configuration into
a new method.

Change-Id: I3b69e148f2f82704d636ebdea88e5c86a483f86e
This commit is contained in:
Dmitry Tantsur 2021-09-21 18:52:57 +02:00
parent 7688392865
commit cb10724139
3 changed files with 14 additions and 7 deletions

View File

@ -0,0 +1,5 @@
---
fixes:
- |
Fixes authentication when the configuration is provided via the
``--config`` option (as opposed to the environment).

View File

@ -74,18 +74,20 @@ class RedfishAuthMiddleware(auth_basic.BasicAuthMiddleware):
class Application(flask.Flask):
def __init__(self, extra_config=None):
def __init__(self):
super().__init__(__name__)
# Turn off strict_slashes on all routes
self.url_map.strict_slashes = False
config_file = os.environ.get('SUSHY_EMULATOR_CONFIG')
def configure(self, config_file=None, extra_config=None):
config_file = config_file or os.environ.get('SUSHY_EMULATOR_CONFIG')
if config_file:
self.config.from_pyfile(config_file)
if extra_config:
self.config.update(extra_config)
auth_file = self.config.get("SUSHY_EMULATOR_AUTH_FILE")
if auth_file:
if auth_file and not isinstance(self.wsgi_app, RedfishAuthMiddleware):
self.wsgi_app = RedfishAuthMiddleware(self.wsgi_app, auth_file)
@property
@ -768,8 +770,7 @@ def main():
app.debug = args.debug
if args.config:
app.config.from_pyfile(args.config)
app.configure(config_file=args.config)
if args.os_cloud:
app.config['SUSHY_EMULATOR_OS_CLOUD'] = args.os_cloud

View File

@ -65,8 +65,9 @@ class AuthenticatedTestCase(base.BaseTestCase):
self.auth_file.write(TEST_PASSWD)
self.auth_file.flush()
self.addCleanup(self.auth_file.close)
app = main.Application({
'SUSHY_EMULATOR_AUTH_FILE': self.auth_file.name})
app = main.Application()
app.configure(
extra_config={'SUSHY_EMULATOR_AUTH_FILE': self.auth_file.name})
self.app = app.test_client()
def test_root_resource(self):