Policy Builder Examples
This section uses Policy Builder examples to show you how Policy Builder is used. To follow these examples, enable the Policy Builder feature. The example uses a new Entitlements System, using the default sample data and the Entitlements Playground sample application.
The examples in the following sections build upon previous examples and assume that all examples are completed. The examples are not intended as standalone tasks. Use the same Entitlements System for all example tasks.
Using the Entitlements System Playground
The following task uses the Entitlements System Playground.
- Create a new Entitlements System.
- Select System > Settings > Install > Entitlements Playground.
- Follow the instructions to Launch the Entitlements Playground.
- In the Entitlements Playground, verify the requests are
Denied
. - Navigate to System > Decisions and verify that all decisions are
Denied
. It takes a minute or so for decisions to appear.
You are ready to test the following examples.
Implementing Role-Based Access Control
In this example, we will implement a Role-Based Access Control (RBAC) policy using Styra’s built-in snippets.
- In your Entitlements System, go to Policy > rules.rego.
- In the authoring toolbar, click Add rule.
- Select RBAC: Role explicitly allows subject access to resource.
Previewing the Rule
You have created a policy implementing RBAC. However, the added rule is set in Monitor mode by default, meaning it is not enforced. Any data the rule returns will be included in the final decision data, but access will be denied. Verify this by previewing the rule using an input document.
- In your Entitlements System, go to Policy > rules.rego.
- In the toolbar, click the Preview icon.
- Toggle Coverage to on and Exclude Stacks to off.
- Click Preview, and in the Input pane, add the following:
{
"subject": "alice",
"resource": "/cars",
"action": "GET"
} - Click Preview.
The outcome in the Policy Dashboard to the right of our rule should say Denied
. Click the Arrow next to the outcome line to expand a description of the outcome.
In the Output panel, you can see more detailed decision information, including the data the rule returned under the key monitored
. It should be similar to the following:
"monitored": [
{
"allowed": true,
"entz": [],
"message": "Access is allowed by roles car-readers"
}
],
To start enforcing the rule:
- Go to the Rule card and click Monitor.
- Click Enforce.
- Click Preview.
You should now see a tag on the Rule card that says Allowed
, and the outcome on the policy dashboard should also say Allowed
. If you expand the details, you should see an explanation of why the request was allowed.
To finalize the policy and use it with the Entitlements Playground:
- From the toolbar, click Publish.
- Click Publish changes.
- In the Entitlements Playground, verify that the relevant requests are allowed. It takes a minute or so to update.
You have now successfully implemented a policy that enforces access by using RBAC.
Combining Conditions for Fine-Grained Authorization
Implementing RBAC with Styra DAS Entitlements is easy and will cover many typical access control scenarios. You can also make more fine-grained access checks, for example, limiting access on certain days.
In this example, we will combine the RBAC condition with Attribute Based Access Control (ABAC). Attributes can be associated with a user, or other contextual information such as the day of the week, used in the following example.
- In your Entitlements System, go to Policy > rules.rego.
- In the Rule card where a rule was previously created, click the Plus icon.
- Click Add condition.
- Select Calendar: Match a Day of the week.
- In the open panel, type the current day of the week into the days parameter input field.
- Click Save.
The Rule card will now be updated to include the new condition. Preview the policy to make sure that it is still Allowed
. You have now modified the Rule to combine the RBAC condition with an ABAC condition that checks that the current weekday matches an expected value and only then allows access.
To verify that this does deny access on other days, modify the condition to change the expected days:
- Hover over the current weekday condition.
- Click the Pen icon or double-click on the condition summary.
- Change the days parameter input so it does not include the current weekday, either by leaving it empty or setting a different day than the current weekday. For example, if today is Friday, change the days parameter to
"tuesday"
. - Click Save.
Preview the policy using the same input as before.
The outcome should now be Denied
.
Before publishing this policy, change the condition only to allow access on weekdays, not weekends.
- Open the current weekday condition panel.
- Change the days parameter input to only include weekdays, for example
"monday"
,"tuesday"
,"wednesday"
,"thursday"
, and"friday"
. - Click Save.
- Click Publish in the toolbar to apply the changes to your policy.
- Navigate to the Entitlements Playground and verify that access is
allowed
if the current day of the week is a weekday, anddenied
if it is the weekend.
You have successfully modified the policy to combine RBAC with ABAC.
Combining Multiple Rules
In the previous example, RBAC was combined with ABAC by checking for an environmental attribute – the current day of the week.
In this example, you will modify the policy to add an additional rule, combining RBAC and ABAC. However, instead of checking for an environmental attribute, you will check for a user attribute.
- In your Entitlements System, go to Policy > rules.rego.
- Click Add rule to create a new Rule using RBAC: Role explicitly allows subject access to resource condition.
- The new Rule is added to the top of the list, and is in Monitor mode by default.
- Click the Plus icon to add a condition to the new Rule.
- Click Add condition and select ABAC: User has attributes.
- Set the Key input to
"team"
. - Set the Value input to
"SLT"
. - Click Save.
- Change the status of the rule to
Enforce
.
The new Rule checks if the user has a role that allows access and also inspects the user’s attributes to see if there’s a key called team
that has the value SLT
.
In the sample dataset, the user alice
does not have this attribute, so when you Preview this rule using the same input document as before the outcome should be Allowed
, but the new Rule that was added should not show a tag. The icons next to the conditions shouldn't be all checkmarks either.
Use the following steps to inspect what happened.
- Hover over the Checkmark icon beside the first condition in the new Rule card, it should specify
Allowed
. - Hover over the Stop icon beside the second condition, it should specify
Denied
. Hover over the Indeterminate icon beside the rule decision, it should specifyNot evaluated
.
This indicates the first condition succeeded but the second failed, and because the second failed the rule decision was never evaluated. That means this rule did not explicitly deny access, but it also did not allow access, so a decision was not made.
If this were the only rule in the policy, the outcome would have been Denied
because that is how the Entitlements System works. If no rule explicitly allows access, then the default decision is to deny access.
Use the following task to test the outcome.
- In the bottom Rule that was created, change the status to
Ignore
to cause the rule to not contribute to the decision at all. - Preview the Policy using the previous input document.
- Verify that the outcome is now
Denied
, and expand the details to see an explanation of why. - Change the
subject
key in the input document to have the value"bob"
instead of"alice"
. - Verify that the outcome is
Allowed
. This is becausebob
has theteam
attribute set toSLT
. - Publish the policy, leaving the old Rule ignored and the new rule enforced.
- Open the Entitlements Playground.
- Verify that the requests with
alice
as the subject are denied. - Verify that the requests with
bob
as the subject are allowed.
You have modified the Policy to add a rule combining RBAC and ABAC, using a user attribute instead of an environmental one in the check.
To give alice
her access back (on weekdays), change the status of the old Rule back to Enforce and Publish the Policy. In the Entitlements Playground, if it is a weekday, alice
and bob
should now be allowed access, but if it is the weekend, only bob
will be allowed access.
Conclusion
These examples show how to combine conditions to create fine-grained authorization policies and combine rules to create complex authorization scenarios where some users are allowed access while others are not, depending on data attributes or environmental factors.
As shown in these examples, using the Policy Builder makes it simple to implement complex scenarios combining RBAC and ABAC without having to author policies using Rego.
Use Policy Builder to experiment further with other policies (for example, adding or removing conditions, rules, or modifying parameters) to understand how the Policy Builder works in the Entitlements System.