Skip to content

fix: handle org: principal remapping failure in restriction_policies [DRALLSTSBX-49]#540

Merged
michael-richey merged 3 commits intomainfrom
restriction-policies-org-fix
Apr 22, 2026
Merged

fix: handle org: principal remapping failure in restriction_policies [DRALLSTSBX-49]#540
michael-richey merged 3 commits intomainfrom
restriction-policies-org-fix

Conversation

@michael-richey
Copy link
Copy Markdown
Collaborator

@michael-richey michael-richey commented Apr 21, 2026

Summary

  • Fixes UnboundLocalError crash in pre_apply_hook when GET /api/v1/org returns 403
  • Changes org_principal default from "org:{}" to None and moves f-string assignment inside the try block
  • Adds if self.org_principal guard in pre_resource_action_hook so stale org:source-uuid principals are no longer overwritten with the invalid "org:{}" template
  • Fixes split(":")split(":", 1) in connect_id for crash safety on unexpected principal formats

Test plan

  • python -m pytest tests/unit/test_restriction_policies.py -v — 4 new tests pass
  • python -m pytest tests/unit/ -v — 326 tests pass
  • tox -e ruff,black (tox-testing pyenv) — clean

Related

  • Jira: DRALLSTSBX-49

🤖 Generated with Claude Code

…[DRALLSTSBX-49]

When GET /api/v1/org returns 403, the pre_apply_hook was crashing with
UnboundLocalError then falling through to an invalid .format() call,
leaving org_principal as the unformatted "org:{}" template which caused
"not a valid principal" errors on 7 dashboard restriction policies.

- Change org_principal default to None (was "org:{}")
- Move f-string assignment inside try block, replacing .format() outside try
- Add `if self.org_principal` guard in pre_resource_action_hook
- Fix split(":") to split(":", 1) in connect_id for crash safety
- Add unit tests covering all four org principal scenarios

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@michael-richey michael-richey marked this pull request as ready for review April 21, 2026 20:24
@michael-richey michael-richey requested a review from a team as a code owner April 21, 2026 20:24
Switch from GET /api/v1/org (requires org_management permission, returns
public_id) to GET /api/v2/current_user (accessible to all users, returns
org UUID matching the format used in restriction policy org: principals).

Per Datadog API docs, org: principals use the UUID from current_user, not
the public_id from the v1 org endpoint.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@michael-richey michael-richey merged commit c9a1a92 into main Apr 22, 2026
11 checks passed
@michael-richey michael-richey deleted the restriction-policies-org-fix branch April 22, 2026 14:49
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.

2 participants