__init__.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import logging
  17. import sys
  18. from importlib.util import module_from_spec, spec_from_file_location
  19. from pathlib import Path
  20. from flask import Blueprint, Flask, request
  21. from werkzeug.wrappers.request import Request
  22. from fate_arch.common.base_utils import CustomJSONEncoder
  23. from fate_flow.entity import RetCode
  24. from fate_flow.hook import HookManager
  25. from fate_flow.hook.common.parameters import AuthenticationParameters, ClientAuthenticationParameters
  26. from fate_flow.settings import API_VERSION, CLIENT_AUTHENTICATION, SITE_AUTHENTICATION, access_logger
  27. from fate_flow.utils.api_utils import get_json_result, server_error_response
  28. __all__ = ['app']
  29. logger = logging.getLogger('flask.app')
  30. for h in access_logger.handlers:
  31. logger.addHandler(h)
  32. Request.json = property(lambda self: self.get_json(force=True, silent=True))
  33. app = Flask(__name__)
  34. app.url_map.strict_slashes = False
  35. app.json_encoder = CustomJSONEncoder
  36. app.errorhandler(Exception)(server_error_response)
  37. def search_pages_path(pages_dir):
  38. return [path for path in pages_dir.glob('*_app.py') if not path.name.startswith('.')]
  39. def register_page(page_path):
  40. page_name = page_path.stem.rstrip('_app')
  41. module_name = '.'.join(page_path.parts[page_path.parts.index('fate_flow'):-1] + (page_name, ))
  42. spec = spec_from_file_location(module_name, page_path)
  43. page = module_from_spec(spec)
  44. page.app = app
  45. page.manager = Blueprint(page_name, module_name)
  46. sys.modules[module_name] = page
  47. spec.loader.exec_module(page)
  48. page_name = getattr(page, 'page_name', page_name)
  49. url_prefix = f'/{API_VERSION}/{page_name}'
  50. app.register_blueprint(page.manager, url_prefix=url_prefix)
  51. return url_prefix
  52. client_urls_prefix = [
  53. register_page(path)
  54. for path in search_pages_path(Path(__file__).parent)
  55. ]
  56. scheduling_urls_prefix = [
  57. register_page(path)
  58. for path in search_pages_path(Path(__file__).parent.parent / 'scheduling_apps')
  59. ]
  60. def client_authentication_before_request():
  61. for url_prefix in scheduling_urls_prefix:
  62. if request.path.startswith(url_prefix):
  63. return
  64. result = HookManager.client_authentication(ClientAuthenticationParameters(
  65. request.full_path, request.headers,
  66. request.form, request.data, request.json,
  67. ))
  68. if result.code != RetCode.SUCCESS:
  69. return get_json_result(result.code, result.message)
  70. def site_authentication_before_request():
  71. for url_prefix in client_urls_prefix:
  72. if request.path.startswith(url_prefix):
  73. return
  74. result = HookManager.site_authentication(AuthenticationParameters(
  75. request.headers.get('src_party_id'),
  76. request.headers.get('site_signature'),
  77. request.json,
  78. ))
  79. if result.code != RetCode.SUCCESS:
  80. return get_json_result(result.code, result.message)
  81. @app.before_request
  82. def authentication_before_request():
  83. if CLIENT_AUTHENTICATION:
  84. return client_authentication_before_request()
  85. if SITE_AUTHENTICATION:
  86. return site_authentication_before_request()