|
5 | 5 | from .base import RequestMicroService
|
6 | 6 | from ..exception import SATOSAConfigurationError
|
7 | 7 | from ..exception import SATOSAError
|
| 8 | +from ..exception import SATOSABackendNotFoundError |
8 | 9 |
|
9 | 10 |
|
10 | 11 | logger = logging.getLogger(__name__)
|
11 | 12 |
|
12 | 13 |
|
| 14 | +class DecideBackendByTarget(RequestMicroService): |
| 15 | + """ |
| 16 | + Select which backend should be used based on who is the SAML IDP |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, config, *args, **kwargs): |
| 20 | + """ |
| 21 | + Constructor. |
| 22 | + :param config: mapping from requester identifier to |
| 23 | + backend module name under the key 'requester_mapping' |
| 24 | + :type config: Dict[str, Dict[str, str]] |
| 25 | + """ |
| 26 | + super().__init__(*args, **kwargs) |
| 27 | + self.target_mapping = config['target_mapping'] |
| 28 | + |
| 29 | + |
| 30 | + def get_backend_by_endpoint_path(self, context, native_backend, |
| 31 | + backends): |
| 32 | + """ |
| 33 | + Returns a new path and target_backend according to its maps |
| 34 | +
|
| 35 | + :type context: satosa.context.Context |
| 36 | + :rtype: ((satosa.context.Context, Any) -> Any, Any) |
| 37 | +
|
| 38 | + :param context: The request context |
| 39 | + :param native_backed: the backed that the proxy normally have been used |
| 40 | + :param backends: list of all the backend configured in the proxy |
| 41 | +
|
| 42 | + :return: tuple or None |
| 43 | + """ |
| 44 | + entity_id = context.request.get('entityID') |
| 45 | + tr_backend = self.target_mapping.get(entity_id) |
| 46 | + if not entity_id: |
| 47 | + return |
| 48 | + if entity_id not in self.target_mapping.keys(): |
| 49 | + return |
| 50 | + if not tr_backend: |
| 51 | + return |
| 52 | + |
| 53 | + if not backends.get(tr_backend): |
| 54 | + raise SATOSABackendNotFoundError( |
| 55 | + f"'{tr_backend}' not found in proxy_conf.yaml" |
| 56 | + ) |
| 57 | + |
| 58 | + if not backends.get(tr_backend): |
| 59 | + raise SATOSABackendNotFoundError( |
| 60 | + f"'{tr_backend}' not found in proxy_conf.yaml" |
| 61 | + ) |
| 62 | + |
| 63 | + tr_path = context.path.replace(native_backend, tr_backend) |
| 64 | + for endpoint in backends[tr_backend]['endpoints']: |
| 65 | + # remove regex trailing chars |
| 66 | + if tr_path == endpoint[0].strip('^').strip('$'): |
| 67 | + msg = (f'Found DecideBackendByTarget ({self.name} microservice) ' |
| 68 | + f'redirecting {entity_id} from {native_backend} ' |
| 69 | + f'backend to {tr_backend}') |
| 70 | + logger.info(msg) |
| 71 | + return (tr_backend, tr_path) |
| 72 | + return |
| 73 | + |
| 74 | + |
13 | 75 | class DecideBackendByRequester(RequestMicroService):
|
14 | 76 | """
|
15 | 77 | Select which backend should be used based on who the requester is.
|
|
0 commit comments