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
In Harness, go to Account Settings → Policies → New Policy.
Enter a Name and click Apply.
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 to view the available fields for each entity type.
Click Save.
Step 2: Create a policy set
Go to Policy Sets → New Policy Set.
Enter a Name and optional Description.
In Entity Type that this policy set applies to, select User or User Group.
In On what event should the policy set be evaluated, select On Save.
Click Continue.
Click + Add Policy and select the policy you created.
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.
Click Finish.
Input payloads
User
The following fields are available (inexhaustive) when a User On Save policy is evaluated:
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:
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
Related articles
Last updated
Was this helpful?