Skip to main content

Overview

The Tyk Dashboard permission system can be extended by writing custom rules using an Open Policy Agent (OPA). The rules engine works on top of your Dashboard API, which means you can control not only access rules, but also behavior of all Dashboard APIs (except your public developer portal). To give you some inspiration here are some ideas of the rules you can implement now:
  • Enforce HTTP proxy option for all APIs for which the target URL does not point at the internal domain
  • Control access for individual fields. For example, do not allow changing the API “active” status (e.g. deploy), unless you have a specific permission set (and make new permissions to be available to the Dashboard/API). Custom permissions can be creating using the Additional Permissions API
  • Have a user(or group) which has read access to one APIs and write to another
We have a video that demonstrates how our Open Policy Agent enables you to add custom permissions.

Configuration

By default the Dashboard OPA engine is turned off, and you need to explicitly enable it via your Dashboard tyk_analytics.conf file. You can then control OPA functionality on a global level via your tyk_analytics.conf file, or at an organization level using either the OPA API or the Dashboard.

Example

With the OPA turned on, the majority of the security rules will be dynamically evaluated based on these rules. Additionally, users can modify OPA rules, and define their own, through the OPA API. For Self-Managed installations you can access and modify the OPA rules from your Tyk installation directory from schemas/dashboard.rego. Moreover, using these rules you can also modify request content. Our recommendation is to use those modifications in a development environment and remember to create a backup of the rego rules.

Language intro

The Open Policy Agent (OPA, pronounced “oh-pa”) is an open source, general-purpose policy engine that unifies policy enforcement across the stack. OPA provides a high-level declarative language (Rego) that lets you specify policy as code and simple APIs to offload policy decision-making from your software. (source: https://www.openpolicyagent.org/docs/latest/)

Tyk policy primitives

The main building block which is required for controlling access is a “deny” rule, which should return a detailed error in case of a rejection. You can specify multiple deny rules, and they will all be evaluated. If none of the rules was matched, user will be allowed to access the resource. A simple deny rule with a static error message can look like:
You can also specify a dynamic error message:
In addition, to deny rules, you can also modify the requests using patch_request. You should respond with a JSON merge patch format https://tools.ietf.org/html/rfc7396 For example:

Getting Tyk Objects

In some cases, you may want to write a rule which is based on existing Tyk Object. For example, you can write a rule for a policy API, which depends on the metadata of the API inside it. The policy engine has access to the TykAPIGet function, which essentially just does a GET call to the Tyk Dashboard API. Example:
Getting changeset of current request For requests which modify the content, you can get a changeset (e.g. difference) using the TykDiff function, combined with a TykAPIGet call to get the original object. Example:

Developer guide

Since Opa rules are declarative, to test them in the majority of the cases, you can test your rules without using the Tyk Dashboard, and using this Rego playground. When it comes to the TykAPIGet and TykDiff functions, you can mock them in your tests. In order to understand how the Dashboard evaluates the rules, you can enable debugging mode by setting the security.open_policy.debug option, and in the Dashboard logs, you will see the detailed output with input and output of the rule engine. It can be useful to copy-paste the Dashboard log output to the Rego playground, fix the issue, and validate it on the Dashboard. When you modify the dashboard.opa file, you will need to restart your tyk Dashboard.

Using the Open Policy Agent in the Dashboard

As well as configuring OPA rules through the API, admin users can view and edit OPA rules from within the Tyk Dashboard. The advantage of configuring your OPA rules in the Dashboard is that you can use a code editor for it, emulating a proper developer experience. There are two ways you can do this:
  1. From the OPA Rules menu. From the Dashboard Management menu, select OPA Rules. You can view and make any changes and select whether your OPA rules should be enabled or disabled.
OPA Rules Menu
  1. From Developer Tools. Using the keyboard shortcut CMD+SHIFT+D (or CTRL+SHIFT+D for PC), you can open the Developer Tools panel on any page in the Dashboard and configure the permissions. Updates are applied in real-time.
    OPA rules can only be accessed by admin role users in the Dashboard.
OPA Floating UI OPA screen

OPA Rule Precedence: Organization-Level Rules Override the File

Tyk Dashboard evaluates OPA rules from one of two sources, with a strict precedence:
  1. Organization-level rules (database). If a ruleset is stored at the organization level, Tyk Dashboard uses it and ignores the file.
  2. File-based default (schema/dashboard.rego). Used only when no organization-level ruleset exists.
OPA rules are stored at the organization level when:
  • the organization is first bootstrapped, where the default ruleset is seeded, or
  • you save rules through the OPA API (PUT /api/org/opa) or the Tyk Dashboard UI.
To check which rules are currently stored at the organization level, use the List OPA rules and settings endpoint.
Because organization-level rules take precedence, editing schema/dashboard.rego (or its mounted ConfigMap on Kubernetes) has no effect while organization-level rules exist, even after a Tyk Dashboard restart. Setting security.open_policy.enable_api: false does not delete rules already stored at the organization level; they continue to load and be enforced.

Revert to the File-Based Ruleset

To make Tyk Dashboard fall back to schema/dashboard.rego, clear the organization-level rules:
  1. Temporarily enable the OPA API in tyk_analytics.conf and restart:
    On Kubernetes, update the opa block in values.yaml:
  2. Back up the current rules:
  3. Clear them with an empty ruleset:
  4. Confirm GET /api/org/opa now returns the schema/dashboard.rego ruleset. Tyk Dashboard now enforces schema/dashboard.rego, so your OPA policies come from the file mount. Set enable_api: false again and restart.
To restore the previous rules, enable the API config again and PUT the backup:

Dashboard OPA rules

Configuring Open Policy Agent Rules

This is an end-to-end worked example showing how to configure Open Policy Agent rules with some additional permissions.

Use Case

Tyk’s RBAC includes out of the box permissions to Write, Read, and Deny access to API Definitions, but what if we want to distinguish between those users who can create APIs and those users who can edit or update APIs? Essentially, we want to extend Tyk’s out of the box RBAC to include more fine grained permissions that prevent an API Editor role from creating new APIs, but allow them to edit or update existing APIs.

High Level Steps

The high level steps to realize this use case are as follows:
  1. Create additional permissions using API
  2. Create user
  3. Add Open Policy Agent Rule
  4. Test new rule

Create additional permissions

To include the API Editor role with additional permissions, send a PUT Request to the Dashboard Additional Permissions API endpoint /api/org/permissions Sample Request In order to add the new role/permissions use the following payload.
Sample Response

Remember to set the authorization header to your Tyk Dashboard API Access Credentials secret, obtained from your user profile on the Dashboard UI.This assumes no other additional permissions already exist. If you’re adding to existing permissions you’ll want to send a GET to /api/org/permissions first, and then add the new permission to the existing list.

Create user

In the Dashboard UI, navigate to System Management -> Users, and hit the Add User button. Create a user that has API Write access and the newly created API Editor permission, e.g. User with Additional Permission

Add Open Policy Agent (OPA) Rule

In the Dashboard UI, navigate to Dashboard Management -> OPA Rules Edit the rules to add the following:
Updated Default OPA Rules incorporating the above rules as follows:

Test

Login to the Dashboard UI as the new API Editor user and try to create a new API. You should see an Access Denied error message. Now try to update an existing API. This should be successful!!