Skip to content

Commit

Permalink
Merge pull request #8 from PADAS/refactors-to-auth-handler
Browse files Browse the repository at this point in the history
GUNDI-3355: Refactor to auth handler
  • Loading branch information
vgarcia13 authored Nov 20, 2024
2 parents d801d7e + 20e77ef commit 87cbcb9
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 42 deletions.
1 change: 1 addition & 0 deletions app/actions/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from movebank_client import MovebankClient
from movebank_client.errors import MBClientError, MBForbiddenError

from app.actions.configurations import AuthenticateConfig
from app.services.errors import ConfigurationNotFound
Expand Down
21 changes: 14 additions & 7 deletions app/actions/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,18 @@ async def action_auth(integration, action_config: AuthenticateConfig):
password=action_config.password.get_secret_value(),
)

token = await mb_client.get_token()

if token:
logger.info(f"Auth successful for integration '{integration.name}'. Token: '{token['api-token']}'")
return {"valid_credentials": True}
try:
token = await mb_client.get_token()
except client.MBForbiddenError:
logger.exception(f"Auth unsuccessful for integration {str(integration.id)}. MB returned 403 (wrong credentials)")
return {"valid_credentials": False, "message": "Invalid credentials"}
except client.MBClientError as e:
logger.exception(f"Auth action failed for integration {str(integration.id)}. Exception: {e}")
return {"error": "An internal error occurred while trying to test credentials. Please try again later."}
else:
logger.error(f"Auth unsuccessful for integration {integration}.")
return {"valid_credentials": False}
if token:
logger.info(f"Auth successful for integration '{integration.name}'. Token: '{token['api-token']}'")
return {"valid_credentials": True}
else:
logger.error(f"Auth unsuccessful for integration {integration}.")
return {"valid_credentials": False}
1 change: 0 additions & 1 deletion app/api_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ class ActionRequest(BaseModel):
action_id: str
run_in_background: bool = False
config_overrides: dict = None
config: dict = None
2 changes: 0 additions & 2 deletions app/routers/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ async def execute(
integration_id=request.integration_id,
action_id=request.action_id,
config_overrides=request.config_overrides,
config_data=request.config
)
return {"message": "Action execution started in background"}
else:
return await execute_action(
integration_id=request.integration_id,
action_id=request.action_id,
config_overrides=request.config_overrides,
config_data=request.config
)
55 changes: 25 additions & 30 deletions app/services/action_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
logger = logging.getLogger(__name__)


async def execute_action(integration_id: str, action_id: str, config_overrides: dict = None, config_data: dict = None):
async def execute_action(integration_id: str, action_id: str, config_overrides: dict = None):
"""
Interface for executing actions.
:param integration_id: The UUID of the integration
:param action_id: "test_auth", "pull_observations", "pull_events"
:param config_overrides: Optional dictionary with configuration overrides
:param config_data: Optional dictionary with an already-set configuration (to be passed to action handler as it is)
:return: action result if any, or raise an exception
"""
logger.info(f"Executing action '{action_id}' for integration '{integration_id}'...")
Expand All @@ -47,35 +46,31 @@ async def execute_action(integration_id: str, action_id: str, config_overrides:
content=jsonable_encoder({"detail": message}),
)

# If "config" variable present in the request, we bypass the integration/config retrieval from the portal
if config_data:
config = config_data
else:
# Look for the configuration of the action being executed
action_config = find_config_for_action(
configurations=integration.configurations,
action_id=action_id
# Look for the configuration of the action being executed
action_config = find_config_for_action(
configurations=integration.configurations,
action_id=action_id
)
if not action_config and not config_overrides:
message = f"Configuration for action '{action_id}' for integration {str(integration.id)} " \
f"is missing. Please fix the integration setup in the portal or include a config to override."
logger.error(message)
await publish_event(
event=IntegrationActionFailed(
payload=ActionExecutionFailed(
integration_id=integration_id,
action_id=action_id,
error=f"Configuration missing for action '{action_id}'",
config_data={"configurations": [i.dict() for i in integration.configurations]},
)
),
topic_name=settings.INTEGRATION_EVENTS_TOPIC,
)
return JSONResponse(
status_code=status.HTTP_404_NOT_FOUND,
content=jsonable_encoder({"detail": message}),
)
if not action_config:
message = f"Configuration for action '{action_id}' for integration {str(integration.id)} " \
f"is missing. Please fix the integration setup in the portal."
logger.error(message)
await publish_event(
event=IntegrationActionFailed(
payload=ActionExecutionFailed(
integration_id=integration_id,
action_id=action_id,
error=f"Configuration missing for action '{action_id}'",
config_data={"configurations": [i.dict() for i in integration.configurations]},
)
),
topic_name=settings.INTEGRATION_EVENTS_TOPIC,
)
return JSONResponse(
status_code=status.HTTP_404_NOT_FOUND,
content=jsonable_encoder({"detail": message}),
)
config = action_config.data
config = action_config.data if action_config else {}
try: # Execute the action
handler, config_model = action_handlers[action_id]
if config_overrides:
Expand Down
2 changes: 1 addition & 1 deletion requirements.in
Original file line number Diff line number Diff line change
@@ -1 +1 @@
movebank-client~=1.1.0
movebank-client~=1.1.1
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ idna==3.7
# yarl
marshmallow==3.21.3
# via environs
movebank-client==1.1.0
movebank-client==1.1.1
# via -r requirements.in
multidict==6.0.5
# via
Expand Down

0 comments on commit 87cbcb9

Please sign in to comment.