Esempio n. 1
0
def sync_perm(args):
    """Updates permissions for existing roles and DAGs"""
    appbuilder = cached_app().appbuilder  # pylint: disable=no-member
    print('Updating permission, view-menu for all existing roles')
    # Add missing permissions for all the Base Views _before_ syncing/creating roles
    appbuilder.add_permissions(update_perms=True)
    appbuilder.sm.sync_roles()
    print('Updating permission on all DAG views')
    dagbag = DagBag(read_dags_from_db=True)
    dagbag.collect_dags_from_db()
    dags = dagbag.dags.values()
    for dag in dags:
        appbuilder.sm.sync_perm_for_dag(dag.dag_id, dag.access_control)
Esempio n. 2
0
    def test_collect_dags_from_db(self):
        """DAGs are collected from Database"""
        example_dags_folder = airflow.example_dags.__path__[0]
        dagbag = DagBag(example_dags_folder)

        example_dags = dagbag.dags
        for dag in example_dags.values():
            SerializedDagModel.write_dag(dag)

        new_dagbag = DagBag(read_dags_from_db=True)
        self.assertEqual(len(new_dagbag.dags), 0)
        new_dagbag.collect_dags_from_db()
        new_dags = new_dagbag.dags
        self.assertEqual(len(example_dags), len(new_dags))
        for dag_id, dag in example_dags.items():
            serialized_dag = new_dags[dag_id]

            self.assertEqual(serialized_dag.dag_id, dag.dag_id)
            self.assertEqual(set(serialized_dag.task_dict), set(dag.task_dict))
Esempio n. 3
0
    def create_dag_specific_permissions(self) -> None:
        """
        Creates 'can_read' and 'can_edit' permissions for all DAGs,
        along with any `access_control` permissions provided in them.

        This does iterate through ALL the DAGs, which can be slow. See `sync_perm_for_dag`
        if you only need to sync a single DAG.

        :return: None.
        """
        perms = self.get_all_permissions()
        dagbag = DagBag(read_dags_from_db=True)
        dagbag.collect_dags_from_db()
        dags = dagbag.dags.values()

        for dag in dags:
            dag_resource_name = permissions.resource_name_for_dag(dag.dag_id)
            for perm_name in self.DAG_PERMS:
                if (perm_name, dag_resource_name) not in perms:
                    self._merge_perm(perm_name, dag_resource_name)

            if dag.access_control:
                self._sync_dag_view_permissions(dag_resource_name, dag.access_control)