vincbeck commented on code in PR #74371:
URL: https://github.com/apache/airflow/pull/74371#discussion_r4630415987
##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/cli/commands.py:
##########
@@ -570,10 +754,49 @@ def _get_permissions_to_create(
"resources": [item.value for item in MenuItem],
}
)
+ return perm_configs
+
+
+def _validate_permission_prerequisites(
+ client: KeycloakAdmin,
+ client_uuid: str,
+ *,
+ teams: list[str],
+) -> None:
+ """Check live scopes and resources before policy writes."""
+ permission_configs = _get_permission_configs(teams)
+ scopes = client.get_client_authz_scopes(client_uuid)
+ resources = client.get_client_authz_resources(client_uuid)
+ _validate_required_authz_objects(
+ permission_configs,
+ scopes=scopes,
+ resources=resources,
+ teams=teams,
+ )
+
+def _get_permissions_to_create(
+ client: KeycloakAdmin,
+ client_uuid: str,
+ teams: list[str],
+ *,
+ include_global_admin: bool = True,
+) -> list[dict]:
+ """Validate permission prerequisites and resolve names to Keycloak IDs."""
+ perm_configs = _get_permission_configs(
+ teams,
+ include_global_admin=include_global_admin,
+ )
all_scopes = client.get_client_authz_scopes(client_uuid)
all_resources = client.get_client_authz_resources(client_uuid)
+ _validate_required_authz_objects(
Review Comment:
`create-all` and `create-permissions` already call
`_validate_permission_prerequisites` before this, so the same check runs twice.
Can we keep it in only one place?
##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/cli/commands.py:
##########
@@ -257,57 +279,169 @@ def _ensure_multi_team_enabled(*, teams: list[str],
command_name: str) -> None:
raise SystemExit(f"{command_name} requires core.multi_team=True when
--teams is used.")
+def _ensure_resource_server_decision_strategy(
+ client: KeycloakAdmin,
+ client_uuid: str,
+ *,
+ _dry_run: bool = False,
+) -> None:
+ realm = client.connection.realm_name
+ url = f"admin/realms/{realm}/clients/{client_uuid}/authz/resource-server"
+
+ response = client.connection.raw_get(url)
+ settings = raise_error_from_response(
+ response,
+ KeycloakGetError,
+ expected_codes=[200],
+ )
+
+ if not isinstance(settings, dict):
Review Comment:
Is that needed? Keycloak always returns a JSON object here. Same for the
type checks in `_get_exported_permission_names`. Removing them would also
remove a few tests, which helps with the PR size.
##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/cli/commands.py:
##########
@@ -977,10 +1275,22 @@ def _attach_team_menu_permissions(
def _attach_superadmin_permissions(
- client: KeycloakAdmin, client_uuid: str, team: str, *, _dry_run: bool =
False
+ client: KeycloakAdmin,
+ client_uuid: str,
+ team: str,
+ *,
+ _dry_run: bool = False,
+ authz_settings: dict | None = None,
) -> None:
+ if _dry_run and authz_settings is None:
+ authz_settings =
client.get_client_authz_settings(client_id=client_uuid)
+ attach_scope = (
+ partial(_attach_policy_to_scope_permission,
authz_settings=authz_settings)
+ if _dry_run
+ else _attach_policy_to_scope_permission
+ )
team_scoped_resources = [f"{resource}:{team}" for resource in
sorted(TEAM_SCOPED_RESOURCE_NAMES)]
- _attach_policy_to_scope_permission(
+ attach_scope(
client,
client_uuid,
permission_name="Admin",
Review Comment:
`create-team --dry-run` always reports a change on the global `Admin`
permission, even when nothing would change. The preview here compares against
`AFFIRMATIVE` and this team's resources only, but the real run then calls
`_update_admin_permission_resources`, which puts it back to `UNANIMOUS` with
every team's resources. Example: re-run `create-team team-a --dry-run` on a
client already provisioned with team-a and team-b, it prints "Would configure
permission 'Admin': decisionStrategy=AFFIRMATIVE, resources=[...]" with team-a
resources only, which reads as if team-b would lose SuperAdmin access. Fix: for
`Admin`, only preview the policy attachment here and leave the
scopes/resources/strategy preview to `_update_admin_permission_resources`.
##########
providers/keycloak/docs/auth-manager/manage/permissions.rst:
##########
@@ -88,12 +103,48 @@ Finally, with the command below, we create the permissions
using the previously
airflow keycloak-auth-manager create-permissions --teams team-a,team-b
-This will create
-
-* read-only permissions (per-team when ``--teams`` is provided)
-* admin permissions (global)
-* user permissions (per-team when ``--teams`` is provided)
-* operations permissions (per-team when ``--teams`` is provided)
+Without ``--teams``, the CLI creates resource-specific permissions for
non-team installations.
+It generates a ``<Resource>Read`` permission with ``GET`` and ``LIST`` scopes
and a
+``<Resource>Write`` permission with ``POST``, ``PUT``, and ``DELETE`` scopes
for each resource
+except the generic ``Menu`` resource. The current model has 24 read/write
permissions for
Review Comment:
nit: I would not put the numbers here, they will get outdated as soon as we
add a resource
##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/cli/commands.py:
##########
@@ -829,8 +1091,25 @@ def _ensure_team_policies(
def _attach_team_permissions(
- client: KeycloakAdmin, client_uuid: str, team: str, *, _dry_run: bool =
False
+ client: KeycloakAdmin,
+ client_uuid: str,
+ team: str,
+ *,
+ _dry_run: bool = False,
+ authz_settings: dict | None = None,
) -> None:
+ if _dry_run and authz_settings is None:
+ authz_settings =
client.get_client_authz_settings(client_id=client_uuid)
+ attach_scope = (
+ partial(_attach_policy_to_scope_permission,
authz_settings=authz_settings)
+ if _dry_run
+ else _attach_policy_to_scope_permission
+ )
+ attach_resource = (
+ partial(_attach_policy_to_resource_permission,
authz_settings=authz_settings)
+ if _dry_run
+ else _attach_policy_to_resource_permission
+ )
Review Comment:
Why the `partial`? `_attach_policy_to_scope_permission` and
`_attach_policy_to_resource_permission` only read `authz_settings` in dry-run,
so you can pass `authz_settings=authz_settings` directly. The `authz_settings
is None` fallback is also never used since `create_team_command` always passes
it. Same in the 3 other functions.
##########
providers/keycloak/src/airflow/providers/keycloak/auth_manager/cli/commands.py:
##########
@@ -365,6 +499,48 @@ def _create_scopes(client: KeycloakAdmin, client_uuid:
str, *, _dry_run: bool =
print("Scopes created successfully.")
+def _get_resource_names_to_create(
+ teams: list[str],
+) -> tuple[list[str], list[str]]:
+ """Build standard and menu resource names without querying Keycloak."""
+ standard_names = [resource.value for resource in KeycloakResource]
+ existing = set(standard_names)
+
+ for team in teams:
+ for resource_name in TEAM_SCOPED_RESOURCE_NAMES:
+ name = f"{resource_name}:{team}"
+ if name not in existing:
+ standard_names.append(name)
+ existing.add(name)
+
+ menu_names = [item.value for item in MenuItem]
+ return standard_names, menu_names
+
+
+def _validate_permission_creation_plan(
+ teams: list[str],
+ *,
+ include_global_admin: bool = True,
+ scopes: list[dict] | None = None,
+) -> None:
+ """Validate permission prerequisites against a provisioning plan."""
+ standard_names, menu_names = _get_resource_names_to_create(teams)
+ resources = [{"name": name} for name in standard_names + menu_names]
+
+ if scopes is None:
Review Comment:
nit: the only caller always passes `scopes`, so this branch is never used
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]