diff --git a/releasenotes/notes/auth-config-4f05bbfaea15bd2b.yaml b/releasenotes/notes/auth-config-4f05bbfaea15bd2b.yaml new file mode 100644 index 00000000..2b5d0778 --- /dev/null +++ b/releasenotes/notes/auth-config-4f05bbfaea15bd2b.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Fixes authentication when the configuration is provided via the + ``--config`` option (as opposed to the environment). diff --git a/sushy_tools/emulator/main.py b/sushy_tools/emulator/main.py index f199b373..b83ebc41 100755 --- a/sushy_tools/emulator/main.py +++ b/sushy_tools/emulator/main.py @@ -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 diff --git a/sushy_tools/tests/unit/emulator/test_main.py b/sushy_tools/tests/unit/emulator/test_main.py index 3a506650..87715ca1 100644 --- a/sushy_tools/tests/unit/emulator/test_main.py +++ b/sushy_tools/tests/unit/emulator/test_main.py @@ -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):