Skip to content

Add declarative configuration for FAB custom roles - #72820

Merged
vincbeck merged 2 commits into
apache:mainfrom
ucaeon:fab-declarative-custom-roles
Sep 11, 2026
Merged

vincbeck merged 2 commits into
apache:mainfrom
ucaeon:fab-declarative-custom-roles

Conversation

@ucaeon

@ucaeon ucaeon commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The FAB provider currently has no configuration for provisioning custom roles during API server startup. Deployments must create them through the UI or CLI separately from their Airflow configuration.

This PR adds [fab] custom_roles to create missing roles with their declared permissions. Existing roles are skipped, preserving changes made through the UI or CLI. This follows the same-name skip behavior of airflow roles import.

Existing FAB creation helpers commit each operation individually. Reusing them could leave a role with incomplete permissions after a failure, and subsequent initialization would skip that role because it already exists.

Change

  • Add [fab] custom_roles, defaulting to {}, with support for AIRFLOW__FAB__CUSTOM_ROLES.
  • Validate the entire configuration before creating any configured role, including entries for existing and built-in roles.
  • Validate action and resource names independently against existing FAB database entries. Unknown names raise a configuration error instead of creating new actions or resources.
  • Allow new permission combinations when both the action and resource names already exist.
  • Create missing custom roles after default-role synchronization in sync_roles().
  • Skip existing roles and warn when built-in role names are configured.
  • Commit each new role and its permission assignments in one transaction.
  • Document configuration, validation requirements, and existing FAB initialization behavior.

Example:

[fab]
custom_roles = {"PythonTester": [{"action": "can_read", "resource": "DAGs"}], "Analyst": []}

Permission entries contain exactly action and resource, using actual FAB names such as can_read and DAGs. Duplicate permission pairs are applied once.

The configuration does not create actions or resources. It can create a missing permission combination using registered names and assign that permission to a new role.

The creation path reuses lookup helpers but avoids the existing helpers that commit individual writes. If creation fails, the current role's transaction is rolled back; roles successfully created earlier remain committed.

If another process creates the same role first, that role is skipped without adding permissions. A conflict caused by concurrent creation of a shared permission is checked after rollback and retried from the beginning of role creation, with at most three attempts. Other failures are propagated.

bulk_sync_roles() is not reused for configured roles because it can add permissions to existing roles.

Behavior change

API server startup applies the configuration when [fab] update_fab_perms is enabled. Explicitly running airflow sync-perm also applies it, regardless of that flag.

Changing or removing a configured role does not update or delete an existing database role. Deleting a configured role from the database allows it to be recreated during the next initialization.

An empty permission list declares no permissions, but normal FAB initialization still grants custom roles can_read on Website. Existing default-role and permission maintenance remains unchanged.

Action and resource names must already be registered when the configuration is applied. Default-role permissions are synchronized before validation. Names supplied by plugins or individual Dags must be registered beforehand; names registered later during startup are not available to this initialization step.

Registered names do not guarantee that every action-resource combination grants access to a feature. The documentation recommends DAG(access_control=...) for access to individual Dags.

This PR does not make roles read-only in the UI, create users, or assign roles to users.

Tests

  • Missing roles are created with their declared permissions, including empty lists and duplicate permission entries.
  • Existing roles retain manually modified permissions.
  • Invalid configuration or unknown action/resource names fail before any configured role is created, including entries for existing and built-in roles.
  • Registered action and resource names can form a new permission combination without creating additional actions or resources.
  • Failures during permission creation or before commit leave no incomplete role or newly created permission, while preserving previously committed data.
  • Two independent database sessions creating the same role with different permissions retain only the winning role's permissions, repeated five times on both PostgreSQL and MySQL.
  • Concurrent creation of a shared permission retries successfully on PostgreSQL and MySQL.
  • Startup respects update_fab_perms, while explicit role synchronization applies the configuration independently of the flag.

Tested locally with Breeze using Python 3.10 after the name-validation update:

  • Custom-role tests and startup/synchronization integration tests on SQLite: 51 passed, 1 skipped.
  • The same tests on PostgreSQL: 52 passed.
  • The same tests on MySQL: 52 passed.

The SQLite skip covers shared-permission creation concurrency, which is exercised on PostgreSQL and MySQL.

Regular and manual static checks passed, including formatting, linting, provider type checks, and provider documentation validation.

closes: #38347


Was generative AI tooling used to co-author this PR?
  • Yes — Codex (GPT-6)

Generated-by: Codex (GPT-6) following the guidelines

@vincbeck vincbeck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Allow deployments to provision custom roles from configuration while preserving permissions maintained through the UI and CLI. Role creation must not leave partial permissions or alter a role created concurrently.
Prevent misspelled names from creating unintended database entries while allowing new permission combinations for registered names.
@ucaeon
ucaeon force-pushed the fab-declarative-custom-roles branch from 9f66898 to 1e3179c Compare September 11, 2026 02:28
@vincbeck
vincbeck merged commit 72e2715 into apache:main Sep 11, 2026
80 checks passed
@boring-cyborg

boring-cyborg Bot commented Sep 11, 2026

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request! You are invited to check our Issue Tracker for additional contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add declarative configuration to define custom roles

2 participants