init_appbuilder.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. # mypy: disable-error-code=var-annotated
  18. from __future__ import annotations
  19. import logging
  20. from functools import reduce
  21. from typing import TYPE_CHECKING
  22. from flask import Blueprint, current_app, url_for
  23. from flask_appbuilder import __version__
  24. from flask_appbuilder.babel.manager import BabelManager
  25. from flask_appbuilder.const import (
  26. LOGMSG_ERR_FAB_ADD_PERMISSION_MENU,
  27. LOGMSG_ERR_FAB_ADD_PERMISSION_VIEW,
  28. LOGMSG_ERR_FAB_ADDON_IMPORT,
  29. LOGMSG_ERR_FAB_ADDON_PROCESS,
  30. LOGMSG_INF_FAB_ADD_VIEW,
  31. LOGMSG_INF_FAB_ADDON_ADDED,
  32. LOGMSG_WAR_FAB_VIEW_EXISTS,
  33. )
  34. from flask_appbuilder.filters import TemplateFilters
  35. from flask_appbuilder.menu import Menu
  36. from flask_appbuilder.views import IndexView, UtilView
  37. from airflow import settings
  38. from airflow.configuration import conf
  39. from airflow.www.extensions.init_auth_manager import get_auth_manager, init_auth_manager
  40. if TYPE_CHECKING:
  41. from flask import Flask
  42. from flask_appbuilder import BaseView
  43. from flask_appbuilder.security.manager import BaseSecurityManager
  44. from sqlalchemy.orm import Session
  45. # This product contains a modified portion of 'Flask App Builder' developed by Daniel Vaz Gaspar.
  46. # (https://github.com/dpgaspar/Flask-AppBuilder).
  47. # Copyright 2013, Daniel Vaz Gaspar
  48. # This module contains code imported from FlaskAppbuilder, so lets use _its_ logger name
  49. log = logging.getLogger("flask_appbuilder.base")
  50. def dynamic_class_import(class_path):
  51. """
  52. Will dynamically import a class from a string path.
  53. :param class_path: string with class path
  54. :return: class
  55. """
  56. # Split first occurrence of path
  57. try:
  58. tmp = class_path.split(".")
  59. module_path = ".".join(tmp[0:-1])
  60. package = __import__(module_path)
  61. return reduce(getattr, tmp[1:], package)
  62. except Exception as e:
  63. log.exception(e)
  64. log.error(LOGMSG_ERR_FAB_ADDON_IMPORT, class_path, e)
  65. class AirflowAppBuilder:
  66. """
  67. This is the base class for all the framework.
  68. This is where you will register all your views
  69. and create the menu structure.
  70. Will hold your flask app object, all your views, and security classes.
  71. Initialize your application like this for SQLAlchemy::
  72. from flask import Flask
  73. from flask_appbuilder import SQLA, AppBuilder
  74. app = Flask(__name__)
  75. app.config.from_object('config')
  76. db = SQLA(app)
  77. appbuilder = AppBuilder(app, db.session)
  78. When using MongoEngine::
  79. from flask import Flask
  80. from flask_appbuilder import AppBuilder
  81. from flask_appbuilder.security.mongoengine.manager import SecurityManager
  82. from flask_mongoengine import MongoEngine
  83. app = Flask(__name__)
  84. app.config.from_object('config')
  85. dbmongo = MongoEngine(app)
  86. appbuilder = AppBuilder(app)
  87. You can also create everything as an application factory.
  88. """
  89. baseviews: list[BaseView | Session] = []
  90. # Flask app
  91. app = None
  92. # Database Session
  93. session = None
  94. # Security Manager Class
  95. sm: BaseSecurityManager
  96. # Babel Manager Class
  97. bm = None
  98. # dict with addon name has key and instantiated class has value
  99. addon_managers: dict
  100. # temporary list that hold addon_managers config key
  101. _addon_managers: list
  102. menu = None
  103. indexview = None
  104. static_folder = None
  105. static_url_path = None
  106. template_filters = None
  107. def __init__(
  108. self,
  109. app=None,
  110. session: Session | None = None,
  111. menu=None,
  112. indexview=None,
  113. base_template="airflow/main.html",
  114. static_folder="static/appbuilder",
  115. static_url_path="/appbuilder",
  116. ):
  117. """
  118. App-builder constructor.
  119. :param app:
  120. The flask app object
  121. :param session:
  122. The SQLAlchemy session object
  123. :param menu:
  124. optional, a previous constructed menu
  125. :param indexview:
  126. optional, your customized indexview
  127. :param static_folder:
  128. optional, your override for the global static folder
  129. :param static_url_path:
  130. optional, your override for the global static url path
  131. """
  132. from airflow.providers_manager import ProvidersManager
  133. providers_manager = ProvidersManager()
  134. providers_manager.initialize_providers_configuration()
  135. self.baseviews = []
  136. self._addon_managers = []
  137. self.addon_managers = {}
  138. self.menu = menu
  139. self.base_template = base_template
  140. self.indexview = indexview
  141. self.static_folder = static_folder
  142. self.static_url_path = static_url_path
  143. self.app = app
  144. self.update_perms = conf.getboolean("fab", "UPDATE_FAB_PERMS")
  145. self.auth_rate_limited = conf.getboolean("fab", "AUTH_RATE_LIMITED")
  146. self.auth_rate_limit = conf.get("fab", "AUTH_RATE_LIMIT")
  147. if app is not None:
  148. self.init_app(app, session)
  149. def init_app(self, app, session):
  150. """
  151. Will initialize the Flask app, supporting the app factory pattern.
  152. :param app:
  153. :param session: The SQLAlchemy session
  154. """
  155. app.config.setdefault("APP_NAME", "F.A.B.")
  156. app.config.setdefault("APP_THEME", "")
  157. app.config.setdefault("APP_ICON", "")
  158. app.config.setdefault("LANGUAGES", {"en": {"flag": "gb", "name": "English"}})
  159. app.config.setdefault("ADDON_MANAGERS", [])
  160. app.config.setdefault("RATELIMIT_ENABLED", self.auth_rate_limited)
  161. app.config.setdefault("FAB_API_MAX_PAGE_SIZE", 100)
  162. app.config.setdefault("FAB_BASE_TEMPLATE", self.base_template)
  163. app.config.setdefault("FAB_STATIC_FOLDER", self.static_folder)
  164. app.config.setdefault("FAB_STATIC_URL_PATH", self.static_url_path)
  165. app.config.setdefault("AUTH_RATE_LIMITED", self.auth_rate_limited)
  166. app.config.setdefault("AUTH_RATE_LIMIT", self.auth_rate_limit)
  167. self.app = app
  168. self.base_template = app.config.get("FAB_BASE_TEMPLATE", self.base_template)
  169. self.static_folder = app.config.get("FAB_STATIC_FOLDER", self.static_folder)
  170. self.static_url_path = app.config.get("FAB_STATIC_URL_PATH", self.static_url_path)
  171. _index_view = app.config.get("FAB_INDEX_VIEW", None)
  172. if _index_view is not None:
  173. self.indexview = dynamic_class_import(_index_view)
  174. else:
  175. self.indexview = self.indexview or IndexView
  176. _menu = app.config.get("FAB_MENU", None)
  177. if _menu is not None:
  178. self.menu = dynamic_class_import(_menu)
  179. else:
  180. self.menu = self.menu or Menu()
  181. if self.update_perms: # default is True, if False takes precedence from config
  182. self.update_perms = app.config.get("FAB_UPDATE_PERMS", True)
  183. self._addon_managers = app.config["ADDON_MANAGERS"]
  184. self.session = session
  185. auth_manager = init_auth_manager(self)
  186. self.sm = auth_manager.security_manager
  187. self.bm = BabelManager(self)
  188. self._add_global_static()
  189. self._add_global_filters()
  190. app.before_request(self.sm.before_request)
  191. self._add_admin_views()
  192. self._add_addon_views()
  193. if self.app:
  194. self._add_menu_permissions()
  195. else:
  196. self.post_init()
  197. self._init_extension(app)
  198. self._swap_url_filter()
  199. def _init_extension(self, app):
  200. app.appbuilder = self
  201. if not hasattr(app, "extensions"):
  202. app.extensions = {}
  203. app.extensions["appbuilder"] = self
  204. def _swap_url_filter(self):
  205. """Use our url filtering util function so there is consistency between FAB and Airflow routes."""
  206. from flask_appbuilder.security import views as fab_sec_views
  207. from airflow.www.views import get_safe_url
  208. fab_sec_views.get_safe_redirect = get_safe_url
  209. def post_init(self):
  210. for baseview in self.baseviews:
  211. # instantiate the views and add session
  212. self._check_and_init(baseview)
  213. # Register the views has blueprints
  214. if baseview.__class__.__name__ not in self.get_app.blueprints.keys():
  215. self.register_blueprint(baseview)
  216. # Add missing permissions where needed
  217. self.add_permissions()
  218. @property
  219. def get_app(self):
  220. """
  221. Get current or configured flask app.
  222. :return: Flask App
  223. """
  224. if self.app:
  225. return self.app
  226. else:
  227. return current_app
  228. @property
  229. def get_session(self):
  230. """
  231. Get the current sqlalchemy session.
  232. :return: SQLAlchemy Session
  233. """
  234. return self.session
  235. @property
  236. def app_name(self):
  237. """
  238. Get the App name.
  239. :return: String with app name
  240. """
  241. return self.get_app.config["APP_NAME"]
  242. @property
  243. def require_confirmation_dag_change(self):
  244. """
  245. Get the value of the require_confirmation_dag_change configuration.
  246. The logic is:
  247. - return True, in page dag.html, when user trigger/pause the dag from UI.
  248. Once confirmation box will be shown before triggering the dag.
  249. - Default value is False.
  250. :return: Boolean
  251. """
  252. return self.get_app.config["REQUIRE_CONFIRMATION_DAG_CHANGE"]
  253. @property
  254. def app_theme(self):
  255. """
  256. Get the App theme name.
  257. :return: String app theme name
  258. """
  259. return self.get_app.config["APP_THEME"]
  260. @property
  261. def app_icon(self):
  262. """
  263. Get the App icon location.
  264. :return: String with relative app icon location
  265. """
  266. return self.get_app.config["APP_ICON"]
  267. @property
  268. def languages(self):
  269. return self.get_app.config["LANGUAGES"]
  270. @property
  271. def version(self):
  272. """
  273. Get the current F.A.B. version.
  274. :return: String with the current F.A.B. version
  275. """
  276. return __version__
  277. def _add_global_filters(self):
  278. self.template_filters = TemplateFilters(self.get_app, self.sm)
  279. def _add_global_static(self):
  280. bp = Blueprint(
  281. "appbuilder",
  282. "flask_appbuilder.base",
  283. url_prefix="/static",
  284. template_folder="templates",
  285. static_folder=self.static_folder,
  286. static_url_path=self.static_url_path,
  287. )
  288. self.get_app.register_blueprint(bp)
  289. def _add_admin_views(self):
  290. """Register indexview, utilview (back function), babel views and Security views."""
  291. self.indexview = self._check_and_init(self.indexview)
  292. self.add_view_no_menu(self.indexview)
  293. self.add_view_no_menu(UtilView())
  294. self.bm.register_views()
  295. self.sm.register_views()
  296. def _add_addon_views(self):
  297. """Register declared addons."""
  298. for addon in self._addon_managers:
  299. addon_class = dynamic_class_import(addon)
  300. if addon_class:
  301. # Instantiate manager with appbuilder (self)
  302. addon_class = addon_class(self)
  303. try:
  304. addon_class.pre_process()
  305. addon_class.register_views()
  306. addon_class.post_process()
  307. self.addon_managers[addon] = addon_class
  308. log.info(LOGMSG_INF_FAB_ADDON_ADDED, addon)
  309. except Exception as e:
  310. log.exception(e)
  311. log.error(LOGMSG_ERR_FAB_ADDON_PROCESS, addon, e)
  312. def _check_and_init(self, baseview):
  313. if hasattr(baseview, "datamodel"):
  314. baseview.datamodel.session = self.session
  315. if callable(baseview):
  316. baseview = baseview()
  317. return baseview
  318. def add_view(
  319. self,
  320. baseview,
  321. name,
  322. href="",
  323. icon="",
  324. label="",
  325. category="",
  326. category_icon="",
  327. category_label="",
  328. menu_cond=None,
  329. ):
  330. """
  331. Add your views associated with menus using this method.
  332. :param baseview:
  333. A BaseView type class instantiated or not.
  334. This method will instantiate the class for you if needed.
  335. :param name:
  336. The string name that identifies the menu.
  337. :param href:
  338. Override the generated href for the menu.
  339. You can use an url string or an endpoint name
  340. if non provided default_view from view will be set as href.
  341. :param icon:
  342. Font-Awesome icon name, optional.
  343. :param label:
  344. The label that will be displayed on the menu,
  345. if absent param name will be used
  346. :param category:
  347. The menu category where the menu will be included,
  348. if non provided the view will be accessible as a top menu.
  349. :param category_icon:
  350. Font-Awesome icon name for the category, optional.
  351. :param category_label:
  352. The label that will be displayed on the menu,
  353. if absent param name will be used
  354. :param menu_cond:
  355. If a callable, :code:`menu_cond` will be invoked when
  356. constructing the menu items. If it returns :code:`True`,
  357. then this link will be a part of the menu. Otherwise, it
  358. will not be included in the menu items. Defaults to
  359. :code:`None`, meaning the item will always be present.
  360. Examples::
  361. appbuilder = AppBuilder(app, db)
  362. # Register a view, rendering a top menu without icon.
  363. appbuilder.add_view(MyModelView(), "My View")
  364. # or not instantiated
  365. appbuilder.add_view(MyModelView, "My View")
  366. # Register a view, a submenu "Other View" from "Other" with a phone icon.
  367. appbuilder.add_view(MyOtherModelView, "Other View", icon="fa-phone", category="Others")
  368. # Register a view, with category icon and translation.
  369. appbuilder.add_view(
  370. YetOtherModelView,
  371. "Other View",
  372. icon="fa-phone",
  373. label=_("Other View"),
  374. category="Others",
  375. category_icon="fa-envelop",
  376. category_label=_("Other View"),
  377. )
  378. # Register a view whose menu item will be conditionally displayed
  379. appbuilder.add_view(
  380. YourFeatureView,
  381. "Your Feature",
  382. icon="fa-feature",
  383. label=_("Your Feature"),
  384. menu_cond=lambda: is_feature_enabled("your-feature"),
  385. )
  386. # Add a link
  387. appbuilder.add_link("google", href="www.google.com", icon="fa-google-plus")
  388. """
  389. baseview = self._check_and_init(baseview)
  390. log.info(LOGMSG_INF_FAB_ADD_VIEW, baseview.__class__.__name__, name)
  391. if not self._view_exists(baseview):
  392. baseview.appbuilder = self
  393. self.baseviews.append(baseview)
  394. self._process_inner_views()
  395. if self.app:
  396. self.register_blueprint(baseview)
  397. self._add_permission(baseview)
  398. self.add_limits(baseview)
  399. self.add_link(
  400. name=name,
  401. href=href,
  402. icon=icon,
  403. label=label,
  404. category=category,
  405. category_icon=category_icon,
  406. category_label=category_label,
  407. baseview=baseview,
  408. cond=menu_cond,
  409. )
  410. return baseview
  411. def add_link(
  412. self,
  413. name,
  414. href,
  415. icon="",
  416. label="",
  417. category="",
  418. category_icon="",
  419. category_label="",
  420. baseview=None,
  421. cond=None,
  422. ):
  423. """
  424. Add your own links to menu using this method.
  425. :param name:
  426. The string name that identifies the menu.
  427. :param href:
  428. Override the generated href for the menu.
  429. You can use an url string or an endpoint name
  430. :param icon:
  431. Font-Awesome icon name, optional.
  432. :param label:
  433. The label that will be displayed on the menu,
  434. if absent param name will be used
  435. :param category:
  436. The menu category where the menu will be included,
  437. if non provided the view will be accessible as a top menu.
  438. :param category_icon:
  439. Font-Awesome icon name for the category, optional.
  440. :param category_label:
  441. The label that will be displayed on the menu,
  442. if absent param name will be used
  443. :param baseview:
  444. A BaseView type class instantiated.
  445. :param cond:
  446. If a callable, :code:`cond` will be invoked when
  447. constructing the menu items. If it returns :code:`True`,
  448. then this link will be a part of the menu. Otherwise, it
  449. will not be included in the menu items. Defaults to
  450. :code:`None`, meaning the item will always be present.
  451. """
  452. self.menu.add_link(
  453. name=name,
  454. href=href,
  455. icon=icon,
  456. label=label,
  457. category=category,
  458. category_icon=category_icon,
  459. category_label=category_label,
  460. baseview=baseview,
  461. cond=cond,
  462. )
  463. if self.app:
  464. self._add_permissions_menu(name)
  465. if category:
  466. self._add_permissions_menu(category)
  467. def add_separator(self, category, cond=None):
  468. """
  469. Add a separator to the menu, you will sequentially create the menu.
  470. :param category:
  471. The menu category where the separator will be included.
  472. :param cond:
  473. If a callable, :code:`cond` will be invoked when
  474. constructing the menu items. If it returns :code:`True`,
  475. then this separator will be a part of the menu. Otherwise,
  476. it will not be included in the menu items. Defaults to
  477. :code:`None`, meaning the separator will always be present.
  478. """
  479. self.menu.add_separator(category, cond=cond)
  480. def add_view_no_menu(self, baseview, endpoint=None, static_folder=None):
  481. """
  482. Add your views without creating a menu.
  483. :param baseview: A BaseView type class instantiated.
  484. """
  485. baseview = self._check_and_init(baseview)
  486. log.info(LOGMSG_INF_FAB_ADD_VIEW, baseview.__class__.__name__, "")
  487. if not self._view_exists(baseview):
  488. baseview.appbuilder = self
  489. self.baseviews.append(baseview)
  490. self._process_inner_views()
  491. if self.app:
  492. self.register_blueprint(baseview, endpoint=endpoint, static_folder=static_folder)
  493. self._add_permission(baseview)
  494. else:
  495. log.warning(LOGMSG_WAR_FAB_VIEW_EXISTS, baseview.__class__.__name__)
  496. return baseview
  497. def security_cleanup(self):
  498. """
  499. Clean up security.
  500. This method is useful if you have changed the name of your menus or
  501. classes. Changing them leaves behind permissions that are not associated
  502. with anything. You can use it always or just sometimes to perform a
  503. security cleanup.
  504. .. warning::
  505. This deletes any permission that is no longer part of any registered
  506. view or menu. Only invoke AFTER YOU HAVE REGISTERED ALL VIEWS.
  507. """
  508. if not hasattr(self.sm, "security_cleanup"):
  509. raise NotImplementedError("The auth manager used does not support security_cleanup method.")
  510. self.sm.security_cleanup(self.baseviews, self.menu)
  511. def security_converge(self, dry=False) -> dict:
  512. """
  513. Migrates all permissions to the new names on all the Roles.
  514. This method is useful when you use:
  515. - ``class_permission_name``
  516. - ``previous_class_permission_name``
  517. - ``method_permission_name``
  518. - ``previous_method_permission_name``
  519. :param dry: If True will not change DB
  520. :return: Dict with all computed necessary operations
  521. """
  522. return self.sm.security_converge(self.baseviews, self.menu, dry)
  523. def get_url_for_login_with(self, next_url: str | None = None) -> str:
  524. return get_auth_manager().get_url_login(next_url=next_url)
  525. @property
  526. def get_url_for_login(self):
  527. return get_auth_manager().get_url_login()
  528. @property
  529. def get_url_for_index(self):
  530. return url_for(f"{self.indexview.endpoint}.{self.indexview.default_view}")
  531. def get_url_for_locale(self, lang):
  532. return url_for(
  533. f"{self.bm.locale_view.endpoint}.{self.bm.locale_view.default_view}",
  534. locale=lang,
  535. )
  536. def add_limits(self, baseview) -> None:
  537. if hasattr(baseview, "limits"):
  538. self.sm.add_limit_view(baseview)
  539. def add_permissions(self, update_perms=False):
  540. if self.update_perms or update_perms:
  541. for baseview in self.baseviews:
  542. self._add_permission(baseview, update_perms=update_perms)
  543. self._add_menu_permissions(update_perms=update_perms)
  544. def _add_permission(self, baseview, update_perms=False):
  545. if self.update_perms or update_perms:
  546. try:
  547. self.sm.add_permissions_view(baseview.base_permissions, baseview.class_permission_name)
  548. except Exception as e:
  549. log.exception(e)
  550. log.error(LOGMSG_ERR_FAB_ADD_PERMISSION_VIEW, e)
  551. def _add_permissions_menu(self, name, update_perms=False):
  552. if self.update_perms or update_perms:
  553. try:
  554. self.sm.add_permissions_menu(name)
  555. except Exception as e:
  556. log.exception(e)
  557. log.error(LOGMSG_ERR_FAB_ADD_PERMISSION_MENU, e)
  558. def _add_menu_permissions(self, update_perms=False):
  559. if self.update_perms or update_perms:
  560. for category in self.menu.get_list():
  561. self._add_permissions_menu(category.name, update_perms=update_perms)
  562. for item in category.childs:
  563. # don't add permission for menu separator
  564. if item.name != "-":
  565. self._add_permissions_menu(item.name, update_perms=update_perms)
  566. def register_blueprint(self, baseview, endpoint=None, static_folder=None):
  567. self.get_app.register_blueprint(
  568. baseview.create_blueprint(self, endpoint=endpoint, static_folder=static_folder)
  569. )
  570. def _view_exists(self, view):
  571. return any(baseview.__class__ == view.__class__ for baseview in self.baseviews)
  572. def _process_inner_views(self):
  573. for view in self.baseviews:
  574. for inner_class in view.get_uninit_inner_views():
  575. for v in self.baseviews:
  576. if isinstance(v, inner_class) and v not in view.get_init_inner_views():
  577. view.get_init_inner_views().append(v)
  578. def init_appbuilder(app: Flask) -> AirflowAppBuilder:
  579. """Init `Flask App Builder <https://flask-appbuilder.readthedocs.io/en/latest/>`__."""
  580. return AirflowAppBuilder(
  581. app=app,
  582. session=settings.Session,
  583. base_template="airflow/main.html",
  584. )