Skip to content

ENG-2784: Auto-populate rules and targets on DSR policy creation#7494

Merged
JadeCara merged 13 commits intomainfrom
ENG-2784-auto-populate-policy-rules
Feb 27, 2026
Merged

ENG-2784: Auto-populate rules and targets on DSR policy creation#7494
JadeCara merged 13 commits intomainfrom
ENG-2784-auto-populate-policy-rules

Conversation

@JadeCara
Copy link
Copy Markdown
Contributor

@JadeCara JadeCara commented Feb 25, 2026

Ticket ENG-2784

Description Of Changes

Add support for auto-generating rules and default data-category targets when creating DSR policies via PATCH /api/v1/dsr/policy.

The policy creation endpoint now accepts two new optional fields (mutually exclusive):

  • action_type — auto-generates a rule (named {policy_name} Rule) and seeds default data-category targets:
    • Access/Erasure: all user.* 2nd-level categories excluding user.financial, user.payment, user.authorization (same defaults that ship OOTB)
    • Erasure: masking strategy set to HMAC
    • Consent: rule only, no targets (matching seed data pattern)
  • rules — creates explicitly provided rules with optional inline targets, using the existing RuleCreate schema extended with a targets field

Backward compatible: if neither field is provided, behavior is unchanged. Existing policies being updated ignore both fields.

Code Changes

  • src/fides/api/schemas/policy.py - Added RuleCreateWithTargets schema, added action_type and rules fields to Policy schema with mutual exclusivity validator
  • src/fides/api/api/v1/endpoints/policy_endpoints.py - Added _create_rule_and_targets() and _auto_create_rule_and_targets() helpers; modified create_or_update_policies to auto-create rules/targets for new policies
  • tests/ops/api/v1/endpoints/test_policy_endpoints.py - Added TestCreatePolicyWithAutoPopulatedRules class with 7 parametrized/standalone tests

Steps to Confirm

  1. Create an access policy with auto-population:

    curl -X PATCH http://localhost:8080/api/v1/dsr/policy \
      -H "Authorization: Bearer <token>" \
      -H "Content-Type: application/json" \
      -d '[{"name": "My Access Policy", "action_type": "access"}]'

    Verify: response includes a rule named "My Access Policy Rule" with default user.* targets

  2. Create an erasure policy:

    curl -X PATCH http://localhost:8080/api/v1/dsr/policy \
      -H "Authorization: Bearer <token>" \
      -H "Content-Type: application/json" \
      -d '[{"name": "My Erasure Policy", "action_type": "erasure"}]'

    Verify: rule has masking_strategy.strategy == "hmac" and default targets

  3. Create a consent policy:
    Same pattern with "action_type": "consent" — verify rule created with no targets

  4. Create a policy with explicit rules and targets:

    curl -X PATCH http://localhost:8080/api/v1/dsr/policy \
      -H "Authorization: Bearer <token>" \
      -H "Content-Type: application/json" \
      -d '[{"name": "Custom Policy", "rules": [{"name": "Custom Rule", "action_type": "access", "targets": [{"data_category": "user.name"}]}]}]'

    Verify: rule and target created as specified

  5. Verify mutual exclusivity: passing both action_type and rules returns 422

Pre-Merge Checklist

  • Issue requirements met
  • All CI pipelines succeeded
  • CHANGELOG.md updated
    • Updates unreleased work already in Changelog, no new entry necessary
  • UX feedback:
    • No UX review needed
  • Followup issues:
    • No followup issues
  • Database migrations:
    • No migrations
  • Documentation:
    • No documentation updates required

Add support for auto-generating rules and default data-category targets
when creating DSR policies via PATCH /api/v1/dsr/policy.

Accepts either `action_type` (auto-generates rule + default targets) or
explicit `rules` with inline `targets` — mutually exclusive. Existing
policies being updated ignore both fields (backward compatible).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@vercel
Copy link
Copy Markdown
Contributor

vercel bot commented Feb 25, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

2 Skipped Deployments
Project Deployment Actions Updated (UTC)
fides-plus-nightly Ignored Ignored Preview Feb 27, 2026 9:43pm
fides-privacy-center Ignored Ignored Feb 27, 2026 9:43pm

Request Review

Jade Wibbels and others added 4 commits February 25, 2026 16:49
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…orted types

- Override action_type with exclude=True in PolicyResponse so it doesn't
  leak into the API response schema
- Add early validation against SUPPORTED_ACTION_TYPES before attempting
  rule creation, giving a clear error for unsupported types like "update"
- Add test for unsupported action_type and assertion that action_type
  is excluded from response

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add type: ignore comments for PolicyResponse field overrides and
  Policy.create_or_update return type (FidesBase -> Policy)
- Apply ruff formatting to test file

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@JadeCara JadeCara marked this pull request as ready for review February 26, 2026 00:12
@JadeCara JadeCara requested a review from a team as a code owner February 26, 2026 00:12
@JadeCara JadeCara requested review from vcruces and removed request for a team February 26, 2026 00:12
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Feb 26, 2026

Greptile Summary

This PR successfully implements auto-population of rules and targets when creating DSR policies, with two mutually exclusive input options: action_type for automatic generation with sensible defaults, or rules for explicit rule/target specifications.

Key changes:

  • Added RuleCreateWithTargets schema extending RuleCreate with inline targets
  • Enhanced Policy schema with transient action_type and rules fields (excluded from responses)
  • Implemented helper functions _create_rule_and_targets() and _auto_create_rule_and_targets() with proper error handling
  • Auto-creation only applies to new policies; existing policies ignore these fields
  • Default erasure policies use HMAC masking strategy and exclude sensitive categories (financial, payment, authorization)
  • Proper transactional cleanup: policies are deleted if rule/target creation fails
  • Comprehensive test coverage validates all scenarios including edge cases

Implementation quality:

  • Clean separation of concerns with helper functions
  • Proper validation at schema level (mutual exclusivity) and endpoint level (supported action types)
  • Maintains backward compatibility - no breaking changes
  • Good error handling with clear messages for bulk operation failures

Confidence Score: 5/5

  • This PR is safe to merge with only minor style improvements needed
  • The implementation is well-designed with proper error handling, validation, and backward compatibility. Code follows established patterns and includes comprehensive tests. The only issue is a minor testing best practice violation (manual database cleanup) that doesn't affect functionality.
  • All files are production-ready. The test file has manual cleanup calls that can be removed as a minor style improvement, but this doesn't block merging.

Important Files Changed

Filename Overview
src/fides/api/schemas/policy.py Added RuleCreateWithTargets schema and transient action_type/rules fields to Policy with proper validation and response exclusion
src/fides/api/api/v1/endpoints/policy_endpoints.py Added helper functions to auto-create rules/targets, enhanced endpoint with proper validation, error handling, and transactional cleanup on failure
tests/ops/api/v1/endpoints/test_policy_endpoints.py Comprehensive test coverage for new functionality, but includes manual database cleanup that should be removed per testing best practices

Last reviewed commit: 95bf52e

Copy link
Copy Markdown
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

4 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

JadeCara and others added 3 commits February 25, 2026 17:16
Copy link
Copy Markdown
Contributor

@vcruces vcruces left a comment

Choose a reason for hiding this comment

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

Looks great to me! The code is easy to follow and the key cases are well covered by tests

pol = Policy.filter(
db=db, conditions=(Policy.key == policy_resp["key"])
).first()
pol.delete(db=db)
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.

Are these explicit deletes necessary? Don’t these instances get cleaned up automatically after each test?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call! Thanks :) - removing.

@JadeCara JadeCara enabled auto-merge February 27, 2026 22:17
@JadeCara JadeCara added this pull request to the merge queue Feb 27, 2026
Merged via the queue into main with commit 808e957 Feb 27, 2026
77 of 79 checks passed
@JadeCara JadeCara deleted the ENG-2784-auto-populate-policy-rules branch February 27, 2026 22:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants