Skip to content

Commit

Permalink
fix: warning if keycloak auth error while discovering product types (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunato committed Nov 21, 2022
1 parent 2ddca49 commit b4c8b9f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 29 deletions.
16 changes: 13 additions & 3 deletions eodag/api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
)
from eodag.utils.exceptions import (
AuthenticationError,
MisconfiguredError,
NoMatchingProductType,
PluginImplementationError,
UnsupportedProvider,
Expand Down Expand Up @@ -648,11 +649,20 @@ def discover_product_types(self, provider=None):
search_plugin.provider
)
if callable(getattr(auth_plugin, "authenticate", None)):
search_plugin.auth = auth_plugin.authenticate()
try:
search_plugin.auth = auth_plugin.authenticate()
except (AuthenticationError, MisconfiguredError) as e:
logger.warning(
f"Could not authenticate on {provider}: {str(e)}"
)
ext_product_types_conf[provider] = None
continue
else:
raise AuthenticationError(
f"Could not authenticate using {auth_plugin} plugin"
logger.warning(
f"Could not authenticate on {provider} using {auth_plugin} plugin"
)
ext_product_types_conf[provider] = None
continue

ext_product_types_conf[
provider
Expand Down
28 changes: 14 additions & 14 deletions eodag/plugins/authentication/keycloak.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ def authenticate(self):
Makes authentication request
"""
self.validate_config_credentials()
response = self.session.post(
self.TOKEN_URL_TEMPLATE.format(
auth_base_uri=self.config.auth_base_uri.rstrip("/"),
realm=self.config.realm,
),
data={
"client_id": self.config.client_id,
"client_secret": self.config.client_secret,
"username": self.config.credentials["username"],
"password": self.config.credentials["password"],
"grant_type": self.GRANT_TYPE,
},
timeout=HTTP_REQ_TIMEOUT,
)
try:
response = self.session.post(
self.TOKEN_URL_TEMPLATE.format(
auth_base_uri=self.config.auth_base_uri.rstrip("/"),
realm=self.config.realm,
),
data={
"client_id": self.config.client_id,
"client_secret": self.config.client_secret,
"username": self.config.credentials["username"],
"password": self.config.credentials["password"],
"grant_type": self.GRANT_TYPE,
},
timeout=HTTP_REQ_TIMEOUT,
)
response.raise_for_status()
except requests.RequestException as e:
# check if error is identified as auth_error in provider conf
Expand Down
49 changes: 37 additions & 12 deletions tests/integration/test_core_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
AuthenticationError,
EODataAccessGateway,
HeaderAuth,
MisconfiguredError,
PluginConfig,
)

Expand Down Expand Up @@ -151,11 +150,15 @@ def test_core_discover_product_types_auth(self, mock_requests_get):
productType: '{productType}'
"""
)
self.assertRaises(
AuthenticationError,
self.dag.discover_product_types,
provider="foo_provider",
)
with self.assertLogs(level="WARNING") as cm:
ext_product_types_conf = self.dag.discover_product_types(
provider="foo_provider"
)
self.assertIsNone(ext_product_types_conf["foo_provider"])
self.assertIn(
"Could not authenticate on foo_provider using None plugin",
str(cm.output),
)

# with auth plugin but without credentials
self.dag.update_providers_config(
Expand All @@ -167,12 +170,17 @@ def test_core_discover_product_types_auth(self, mock_requests_get):
Authorization: "Apikey {apikey}"
"""
)
with self.assertRaisesRegex(
MisconfiguredError, r"Missing credentials configuration .*"
):
self.dag.discover_product_types(provider="foo_provider")

# with auth plugin and credentials
with self.assertLogs(level="WARNING") as cm:
ext_product_types_conf = self.dag.discover_product_types(
provider="foo_provider"
)
self.assertIsNone(ext_product_types_conf["foo_provider"])
self.assertIn(
"Could not authenticate on foo_provider: Missing credentials",
str(cm.output),
)

# succeeds with auth plugin and credentials
self.dag.update_providers_config(
"""
foo_provider:
Expand All @@ -192,3 +200,20 @@ def test_core_discover_product_types_auth(self, mock_requests_get):
)
call_args, call_kwargs = mock_requests_get.call_args
self.assertIsInstance(call_kwargs["auth"], HeaderAuth)

# warning if another AuthenticationError
with mock.patch(
"eodag.plugins.manager.PluginManager.get_auth_plugin",
) as mock_get_auth_plugin:
mock_get_auth_plugin.return_value.authenticate.side_effect = (
AuthenticationError("cannot auth for test")
)
with self.assertLogs(level="WARNING") as cm:
ext_product_types_conf = self.dag.discover_product_types(
provider="foo_provider"
)
self.assertIsNone(ext_product_types_conf["foo_provider"])
self.assertIn(
"Could not authenticate on foo_provider: cannot auth for test",
str(cm.output),
)

0 comments on commit b4c8b9f

Please sign in to comment.