Skip to content

Commit

Permalink
Restructure fatal error messages
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Kanakarakis <[email protected]>
  • Loading branch information
c00kiemon5ter committed Jun 12, 2023
1 parent 62f8775 commit 014e121
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 56 deletions.
97 changes: 65 additions & 32 deletions src/satosa/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@
from saml2.s_utils import UnknownSystemEntity

from satosa import util
from satosa.response import Redirect
from satosa.response import BadRequest
from satosa.response import NotFound
from satosa.response import Redirect
from .context import Context
from .exception import SATOSAError
from .exception import SATOSAAuthenticationError
from .exception import SATOSAUnknownError
from .exception import SATOSAMissingStateError
from .exception import SATOSAAuthenticationFlowError
from .exception import SATOSABadRequestError
from .plugin_loader import load_backends, load_frontends
from .plugin_loader import load_request_microservices, load_response_microservices
from .routing import ModuleRouter, SATOSANoBoundEndpointError
from .state import cookie_to_state, SATOSAStateError, State, state_to_cookie
from .exception import SATOSAError
from .exception import SATOSAMissingStateError
from .exception import SATOSANoBoundEndpointError
from .exception import SATOSAUnknownError
from .exception import SATOSAStateError
from .plugin_loader import load_backends
from .plugin_loader import load_frontends
from .plugin_loader import load_request_microservices
from .plugin_loader import load_response_microservices
from .routing import ModuleRouter
from .state import State
from .state import cookie_to_state
from .state import state_to_cookie

import satosa.logging_util as lu

Expand Down Expand Up @@ -262,77 +268,104 @@ def run(self, context):
resp = self._run_bound_endpoint(context, spec)
self._save_state(resp, context)
except SATOSABadRequestError as e:
error_id = uuid.uuid4().urn
msg = {
"message": "Bad Request",
"error": e.error,
"error_id": uuid.uuid4().urn
"error": str(e),
"error_id": error_id,
}
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.error(logline)
generic_error_url = self.config.get("ERROR_URL")
if generic_error_url:
redirect_url = f"{generic_error_url}?errorid={error_id}"
return Redirect(generic_error_url)
else:
return BadRequest(e.error)
return BadRequest(error)
except SATOSAMissingStateError as e:
error_id = uuid.uuid4().urn
msg = {
"message": "Missing SATOSA State",
"error": e.error,
"error_id": uuid.uuid4().urn
"error": str(e),
"error_id": error_id,
}
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.error(logline)
generic_error_url = self.config.get("ERROR_URL")
if generic_error_url:
redirect_url = f"{generic_error_url}?errorid={error_id}"
return Redirect(generic_error_url)
else:
raise
raise
except SATOSAAuthenticationFlowError as e:
error_id = uuid.uuid4().urn
msg = {
"message": "SATOSA Authentication Flow Error",
"error": e.error,
"error_id": uuid.uuid4().urn
"error": str(e),
"error_id": error_id,
}
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.error(logline)
generic_error_url = self.config.get("ERROR_URL")
if generic_error_url:
redirect_url = f"{generic_error_url}?errorid={error_id}"
return Redirect(generic_error_url)
else:
raise
raise
except SATOSANoBoundEndpointError as e:
msg = str(e)
error_id = uuid.uuid4().urn
msg = {
"message": "URL-path is not bound to any endpoint function",
"error": str(e),
"error_id": error_id,
}
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.error(logline)
generic_error_url = self.config.get("ERROR_URL")
if generic_error_url:
redirect_url = f"{generic_error_url}?errorid={error_id}"
return Redirect(generic_error_url)
return NotFound("The Service or Identity Provider you requested could not be found.")
except SATOSAError:
msg = "Uncaught SATOSA error"
except SATOSAError as e:
error_id = uuid.uuid4().urn
msg = {
"message": "Uncaught SATOSA error",
"error": str(e),
"error_id": error_id,
}
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.error(logline)
generic_error_url = self.config.get("ERROR_URL")
if generic_error_url:
redirect_url = f"{generic_error_url}?errorid={error_id}"
return Redirect(generic_error_url)
else:
raise
raise
except UnknownSystemEntity as e:
msg = f"Configuration error: unknown system entity: {e}"
error_id = uuid.uuid4().urn
msg = {
"message": "Configuration error: unknown system entity",
"error": str(e),
"error_id": error_id,
}
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.error(logline)
generic_error_url = self.config.get("ERROR_URL")
if generic_error_url:
redirect_url = f"{generic_error_url}?errorid={error_id}"
return Redirect(generic_error_url)
else:
raise
raise
except Exception as e:
msg = "Uncaught exception"
error_id = uuid.uuid4().urn
msg = {
"message": "Uncaught exception",
"error": str(e),
"error_id": error_id,
}
logline = lu.LOG_FMT.format(id=lu.get_session_id(context.state), message=msg)
logger.error(logline)
generic_error_url = self.config.get("ERROR_URL")
if generic_error_url:
return Redirect(generic_error_url)
else:
raise SATOSAUnknownError("Unknown error") from e
return resp
raise SATOSAUnknownError("Unknown error") from e
else:
return resp


class SAMLBaseModule(object):
Expand Down
9 changes: 1 addition & 8 deletions src/satosa/context.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
from warnings import warn as _warn

from satosa.exception import SATOSAError


class SATOSABadContextError(SATOSAError):
"""
Raise this exception if validating the Context and failing.
"""
pass
from satosa.exception import SATOSABadContextError


class Context(object):
Expand Down
18 changes: 18 additions & 0 deletions src/satosa/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ def message(self):
"""
return self._message.format(error_id=self.error_id)


class SATOSABasicError(SATOSAError):
"""
eduTEAMS error
"""
def __init__(self, error):
self.error = error


class SATOSAMissingStateError(SATOSABasicError):
"""
SATOSA Missing State error.
Expand All @@ -85,6 +87,7 @@ class SATOSAMissingStateError(SATOSABasicError):
"""
pass


class SATOSAAuthenticationFlowError(SATOSABasicError):
"""
SATOSA Flow error.
Expand All @@ -95,10 +98,25 @@ class SATOSAAuthenticationFlowError(SATOSABasicError):
"""
pass


class SATOSABadRequestError(SATOSABasicError):
"""
SATOSA Bad Request error.
This exception should be raised when we want to return an HTTP 400 Bad Request
"""
pass


class SATOSABadContextError(SATOSAError):
"""
Raise this exception if validating the Context and failing.
"""
pass


class SATOSANoBoundEndpointError(SATOSAError):
"""
Raised when a given url path is not bound to any endpoint function
"""
pass
18 changes: 2 additions & 16 deletions src/satosa/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import logging
import re

from satosa.context import SATOSABadContextError
from satosa.exception import SATOSAError
from satosa.exception import SATOSABadContextError
from satosa.exception import SATOSANoBoundEndpointError

import satosa.logging_util as lu

Expand All @@ -15,20 +15,6 @@
STATE_KEY = "ROUTER"


class SATOSANoBoundEndpointError(SATOSAError):
"""
Raised when a given url path is not bound to any endpoint function
"""
pass


class SATOSAUnknownTargetBackend(SATOSAError):
"""
Raised when targeting an unknown backend
"""
pass


class ModuleRouter(object):
class UnknownEndpoint(ValueError):
pass
Expand Down

0 comments on commit 014e121

Please sign in to comment.