From f834220fcecccf6a12f6dfcad2401a48aa7e9924 Mon Sep 17 00:00:00 2001 From: Daniel Huppmann Date: Tue, 29 Aug 2023 10:19:29 +0200 Subject: [PATCH 1/9] Set `credentials` as own attribute --- ixmp4/conf/settings.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/ixmp4/conf/settings.py b/ixmp4/conf/settings.py index aa8b950b..d7893023 100644 --- a/ixmp4/conf/settings.py +++ b/ixmp4/conf/settings.py @@ -46,15 +46,24 @@ def __init__(self, *args, **kwargs) -> None: self.configure_logging(self.mode) - self.load_credentials() + self._credentials = None + + self.get_auth() self.load_manager_config() self.load_toml_config() + @property + def credentials(self): + if self._credentials is None: + self.load_credentials() + return self._credentials + def load_credentials(self): credentials_config = self.storage_directory / "credentials.toml" credentials_config.touch() - self.credentials = Credentials(credentials_config) + self._credentials = Credentials(credentials_config) + def get_auth(self): self.default_credentials = None self.default_auth = None try: From b9d9d14cd3463d07e09fbd89ccdaa1f9253691d6 Mon Sep 17 00:00:00 2001 From: Daniel Huppmann Date: Tue, 29 Aug 2023 12:35:21 +0200 Subject: [PATCH 2/9] Set `toml` as own attribute --- ixmp4/conf/settings.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ixmp4/conf/settings.py b/ixmp4/conf/settings.py index d7893023..fe575fec 100644 --- a/ixmp4/conf/settings.py +++ b/ixmp4/conf/settings.py @@ -47,10 +47,10 @@ def __init__(self, *args, **kwargs) -> None: self.configure_logging(self.mode) self._credentials = None + self._toml = None self.get_auth() self.load_manager_config() - self.load_toml_config() @property def credentials(self): @@ -58,6 +58,12 @@ def credentials(self): self.load_credentials() return self._credentials + @property + def toml(self): + if self._toml is None: + self.load_toml_config() + return self._toml + def load_credentials(self): credentials_config = self.storage_directory / "credentials.toml" credentials_config.touch() @@ -99,7 +105,7 @@ def load_toml_config(self): toml_config = self.storage_directory / "platforms.toml" toml_config.touch() - self.toml = TomlConfig(toml_config, toml_user) + self._toml = TomlConfig(toml_config, toml_user) @validator("storage_directory") def expand_user(cls, v): From 3c4678c5fbd5e734e49d28a180db4b153bdec434 Mon Sep 17 00:00:00 2001 From: Daniel Huppmann Date: Tue, 29 Aug 2023 12:44:54 +0200 Subject: [PATCH 3/9] Set `manager` and `default_auth` as own properties --- ixmp4/conf/settings.py | 46 +++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/ixmp4/conf/settings.py b/ixmp4/conf/settings.py index fe575fec..144064bc 100644 --- a/ixmp4/conf/settings.py +++ b/ixmp4/conf/settings.py @@ -48,9 +48,8 @@ def __init__(self, *args, **kwargs) -> None: self._credentials = None self._toml = None - - self.get_auth() - self.load_manager_config() + self._default_auth = None + self._manager = None @property def credentials(self): @@ -64,39 +63,50 @@ def toml(self): self.load_toml_config() return self._toml + @property + def default_auth(self): + if self._default_auth is None: + self.get_auth() + return self._default_auth + + @property + def manager(self): + if self._manager is None: + self.load_manager_config() + return self._manager + + def load_credentials(self): credentials_config = self.storage_directory / "credentials.toml" credentials_config.touch() self._credentials = Credentials(credentials_config) def get_auth(self): - self.default_credentials = None - self.default_auth = None + try: - self.default_credentials = self.credentials.get("default") + default_credentials = self.credentials.get("default") except KeyError: - logger.warn("No default credentials provided.") + logger.warning("No default credentials provided.") + default_credentials = None - if self.default_credentials is not None: - username, password = self.default_credentials + if default_credentials is not None: + username, password = default_credentials try: - self.default_auth = ManagerAuth(username, password, self.manager_url) + self._default_auth = ManagerAuth(username, password, self.manager_url) return except InvalidCredentials: - logger.warn( + logger.warning( "Failure while requesting management service authentication: Invalid credentials." ) except ConnectError: - logger.warn(f"Unable to connect to {self.manager_url}.") + logger.warning(f"Unable to connect to {self.manager_url}.") - self.default_auth = AnonymousAuth() + self._default_auth = AnonymousAuth() def load_manager_config(self): - self.manager = None - if self.default_auth is not None: - self.manager = ManagerConfig( - self.manager_url, self.default_auth, remote=True - ) + self._manager = ManagerConfig( + self.manager_url, self.default_auth, remote=True + ) def load_toml_config(self): toml_user = self.default_auth.get_user() From fc3f1bf0165d7dcf6baa87c66826c59cbf61b7dc Mon Sep 17 00:00:00 2001 From: Daniel Huppmann Date: Tue, 29 Aug 2023 12:54:37 +0200 Subject: [PATCH 4/9] Add own property `default_credentials` --- ixmp4/conf/settings.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/ixmp4/conf/settings.py b/ixmp4/conf/settings.py index 144064bc..83501d59 100644 --- a/ixmp4/conf/settings.py +++ b/ixmp4/conf/settings.py @@ -57,6 +57,13 @@ def credentials(self): self.load_credentials() return self._credentials + @property + def default_credentials(self): + try: + return self.credentials.get("default") + except KeyError: + logger.warning("No default credentials provided.") + @property def toml(self): if self._toml is None: @@ -75,24 +82,16 @@ def manager(self): self.load_manager_config() return self._manager - def load_credentials(self): credentials_config = self.storage_directory / "credentials.toml" credentials_config.touch() self._credentials = Credentials(credentials_config) def get_auth(self): - - try: - default_credentials = self.credentials.get("default") - except KeyError: - logger.warning("No default credentials provided.") - default_credentials = None - + default_credentials = self.default_credentials if default_credentials is not None: - username, password = default_credentials try: - self._default_auth = ManagerAuth(username, password, self.manager_url) + self._default_auth = ManagerAuth(*default_credentials, self.manager_url) return except InvalidCredentials: logger.warning( From c4b673503b68957cc175fdcb6367771922ebe379 Mon Sep 17 00:00:00 2001 From: Daniel Huppmann Date: Tue, 29 Aug 2023 15:50:10 +0200 Subject: [PATCH 5/9] Streamline log message --- ixmp4/conf/settings.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ixmp4/conf/settings.py b/ixmp4/conf/settings.py index 83501d59..216efead 100644 --- a/ixmp4/conf/settings.py +++ b/ixmp4/conf/settings.py @@ -94,9 +94,7 @@ def get_auth(self): self._default_auth = ManagerAuth(*default_credentials, self.manager_url) return except InvalidCredentials: - logger.warning( - "Failure while requesting management service authentication: Invalid credentials." - ) + logger.warning(f"Invalid credentials for {self.manager_url}.") except ConnectError: logger.warning(f"Unable to connect to {self.manager_url}.") From 5dad3eee05c792058c3e64d62f520969cd2111d7 Mon Sep 17 00:00:00 2001 From: Daniel Huppmann Date: Wed, 30 Aug 2023 08:56:47 +0200 Subject: [PATCH 6/9] Do not show "No default credentials" warning --- ixmp4/conf/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ixmp4/conf/settings.py b/ixmp4/conf/settings.py index 216efead..272b0e77 100644 --- a/ixmp4/conf/settings.py +++ b/ixmp4/conf/settings.py @@ -62,7 +62,7 @@ def default_credentials(self): try: return self.credentials.get("default") except KeyError: - logger.warning("No default credentials provided.") + pass @property def toml(self): From a69a5e294f1ff4b4628c0e46908607ee56f6ac1d Mon Sep 17 00:00:00 2001 From: Daniel Huppmann Date: Wed, 30 Aug 2023 10:08:50 +0200 Subject: [PATCH 7/9] Do not default to anonymous-login without internet connection --- ixmp4/conf/settings.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ixmp4/conf/settings.py b/ixmp4/conf/settings.py index 272b0e77..27f0331c 100644 --- a/ixmp4/conf/settings.py +++ b/ixmp4/conf/settings.py @@ -97,6 +97,7 @@ def get_auth(self): logger.warning(f"Invalid credentials for {self.manager_url}.") except ConnectError: logger.warning(f"Unable to connect to {self.manager_url}.") + return self._default_auth = AnonymousAuth() @@ -106,8 +107,11 @@ def load_manager_config(self): ) def load_toml_config(self): - toml_user = self.default_auth.get_user() - if not toml_user.is_authenticated: + if self.default_auth is not None: + toml_user = self.default_auth.get_user() + if not toml_user.is_authenticated: + toml_user = local_user + else: # if no connection to manager toml_user = local_user toml_config = self.storage_directory / "platforms.toml" From 7000048f8cd6dc1382f4d9286e565916e22d1c84 Mon Sep 17 00:00:00 2001 From: Daniel Huppmann Date: Wed, 30 Aug 2023 10:26:07 +0200 Subject: [PATCH 8/9] Add log-message with username when connecting to manager --- ixmp4/conf/settings.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ixmp4/conf/settings.py b/ixmp4/conf/settings.py index 27f0331c..ab060d0a 100644 --- a/ixmp4/conf/settings.py +++ b/ixmp4/conf/settings.py @@ -92,14 +92,16 @@ def get_auth(self): if default_credentials is not None: try: self._default_auth = ManagerAuth(*default_credentials, self.manager_url) - return + logger.info( + f"Connecting as user '{self._default_auth.get_user().username}'." + ) except InvalidCredentials: logger.warning(f"Invalid credentials for {self.manager_url}.") except ConnectError: logger.warning(f"Unable to connect to {self.manager_url}.") - return - self._default_auth = AnonymousAuth() + else: + self._default_auth = AnonymousAuth() def load_manager_config(self): self._manager = ManagerConfig( From a1801eb8dcab25e2407d9912c2b1617b1236a0af Mon Sep 17 00:00:00 2001 From: Daniel Huppmann Date: Wed, 30 Aug 2023 10:30:40 +0200 Subject: [PATCH 9/9] Avoid short-term assignment --- ixmp4/conf/settings.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ixmp4/conf/settings.py b/ixmp4/conf/settings.py index ab060d0a..376c9c95 100644 --- a/ixmp4/conf/settings.py +++ b/ixmp4/conf/settings.py @@ -88,10 +88,11 @@ def load_credentials(self): self._credentials = Credentials(credentials_config) def get_auth(self): - default_credentials = self.default_credentials - if default_credentials is not None: + if self.default_credentials is not None: try: - self._default_auth = ManagerAuth(*default_credentials, self.manager_url) + self._default_auth = ManagerAuth( + *self.default_credentials, self.manager_url + ) logger.info( f"Connecting as user '{self._default_auth.get_user().username}'." )