For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to page URLs.
Skip to content

Policy as Code for users and user groups

Use OPA policies to enforce identity governance rules on user invitations, scope assignments, and user group membership changes.

Harness uses Open Policy Agent (OPA) as its policy engine. You can write governance rules in Rego that are evaluated whenever a user is invited or assigned to a scope, or whenever a user group is created, updated, or its membership changes.

Use these policies to enforce a wide range of identity governance rules.


What you will learn in this topic

By the end of this topic, you will be able to:

  • Create OPA policies for user invitation and scope-assignment events.

  • Create OPA policies for user group create, update, and membership-change events.

  • Understand the input payloads available for each event type.

  • Describe policies for common identity governance use cases.


Before you begin

Before you configure OPA for user and user groups, ensure you have the following:


Policy evaluation behavior

When a user is added to multiple groups in a single operation, Harness evaluates the policy for each group independently. If any group's policy denies the operation, the entire operation fails, and no group memberships are updated. If a policy issues a warning, Harness allows the operation to proceed and displays the warning, provided no policy denies the operation.

Configure a policy and policy set

The steps below describe how to create a policy and a polciy set for users and user groups, along with Rego samples.

Step 1: Create a policy

Create the policy that contains the Rego rules to evaluate.

  1. In Harness, go to Account Settings → Policies → New Policy.

  2. Enter a Name and click Apply.

  3. Write your Rego policy. Described below are policies for user and user groups.

  • User policy example

    • Restrict invitations to allowed email domains:

  • User group policy example

    • Enforce a naming convention on identifiers:

    • Require at least one member before a group can be saved:

Go to Input payloads for the available fields for each entity type.

  1. Click Save.

Step 2: Create a policy set

Add the policy to a policy set, and specify when Harness evaluates it.

  1. Go to Policy Sets → New Policy Set.

  2. Enter a Name and optional Description.

  3. In Entity Type that this policy set applies to, select User or User Group.

  4. In On what event should the policy set be evaluated, select On Save.

  5. Click Continue.

  6. Click + Add Policy and select the policy you created.

  7. In What should happen if a policy fails?, select:

    • Warn & continue - the entity is saved and a warning is surfaced.

    • Error and exit - the save is blocked and an error is returned.

  8. Click Finish.


Input payloads

User

The following fields are available (inexhaustive) when a User On Save policy is evaluated:

Path
Contents

input.user.email

Email address being added or invited

input.user.orgIdentifier

Target org scope of the operation

input.user.existingMemberships

Existing org/project memberships, populated only for users already in the account; empty for first-time invitees

input.user.roleAssignments

Role bindings included in the request

input.user.userGroups

User groups included in the request

input.metadata.roleAssignmentMetadata

The invitor's current role assignments (use this for privilege checks)

Use case: Policy to prevent non-admins from granting the account admin role:

Use case: To block org-scoped operations that would add a user who already belongs to a different org (the existingMemberships field is empty for first-time invitees, so this rule applies only to existing account members):

User group

The following fields are available (inexhaustive) when a User Group On Save policy is evaluated:

Path
Contents

input.userGroup.identifier

The group's unique identifier

input.userGroup.orgIdentifier

The org the group belongs to (empty string at account scope)

input.userGroup.users

Array of user IDs in the group

input.userGroup.userMemberships

Map of userId → array of scope objects, each with scopeLevel ("ORGANIZATION", "PROJECT", "ACCOUNT") and scope.orgIdentifier / scope.projectIdentifier

Role-assignment operations are not yet evaluated by OPA policies The user group payload contains scope-membership data (which orgs and projects users belong to) but not role assignments. Operations such as inheriting role assignments from a parent scope cannot be blocked with a policy. To write policies based on explicit role assignments at invite time, use the User entity type and input.metadata.roleAssignmentMetadata.


Troubleshooting

  • A policy does not evaluate as expected for a user group operation. Confirm the policy set's Entity Type matches the operation (User or User Group) and that the policy set is configured to evaluate On Save.

  • A system-managed group's membership cannot be restricted. Harness-managed groups, such as All Account Users, All Org Users, and All Project Users, bypass OPA evaluation entirely. Use a custom user group instead.

  • A role-assignment change is not blocked by a policy. Role-assignment operations on existing user groups, such as inheriting assignments from a parent scope, are not evaluated by OPA policies. Enforce role-assignment rules at invite time with the User entity type and input.metadata.roleAssignmentMetadata.

  • A policy blocks an operation unexpectedly. Check whether What should happen if a policy fails? is set to Error and exit on the policy set. Change it to Warn & continue if the operation should be allowed with a warning instead.

The following resources provide more background on policy as code in Harness.

Last updated

Was this helpful?