Fine-grained access control usage scenarios

This guide contains several examples and usage scenarios of using fine-grained roles and permissions for controlling access to Grafana resources.

Before you get started, make sure to enable fine-grained access control.

Check all built-in role assignments

You can use the Fine-grained access control HTTP API to see all available built-in role assignments. The response contains a mapping between one of the organization roles (Viewer, Editor, Admin) or Grafana Admin to the custom or fixed roles.

Example request:

  1. curl --location --request GET '<grafana_url>/api/access-control/builtin-roles' --header 'Authorization: Basic YWRtaW46cGFzc3dvcmQ='

You must use the base64 username:password Basic Authorization here. Auth tokens are not applicable here.

Example response:

  1. {
  2. "Admin": [
  3. ...
  4. {
  5. "version": 2,
  6. "uid": "qQui_LCMk",
  7. "name": "fixed:users:org:writer",
  8. "displayName": "Users Organization writer",
  9. "description": "Within a single organization, add a user, invite a user, read information about a user and their role, remove a user from that organization, or change the role of a user.",
  10. "global": true,
  11. "updated": "2021-05-17T20:49:18+02:00",
  12. "created": "2021-05-13T16:24:26+02:00"
  13. },
  14. {
  15. "version": 1,
  16. "uid": "Kz9m_YjGz",
  17. "name": "fixed:reports:writer",
  18. "displayName": "Report writer",
  19. "description": "Create, read, update, or delete all reports and shared report settings.",
  20. "global": true,
  21. "updated": "2021-05-13T16:24:26+02:00",
  22. "created": "2021-05-13T16:24:26+02:00"
  23. }
  24. ...
  25. ],
  26. "Grafana Admin": [
  27. ...
  28. {
  29. "version": 2,
  30. "uid": "qQui_LCMk",
  31. "name": "fixed:users:writer",
  32. "displayName": "User writer",
  33. "description": "Read and update all attributes and settings for all users in Grafana: update user information, read user information, create or enable or disable a user, make a user a Grafana administrator, sign out a user, update a user’s authentication token, or update quotas for all users.",
  34. "global": true,
  35. "updated": "2021-05-17T20:49:18+02:00",
  36. "created": "2021-05-13T16:24:26+02:00"
  37. },
  38. {
  39. "version": 2,
  40. "uid": "ajum_YjGk",
  41. "name": "fixed:users:reader",
  42. "displayName": "User reader",
  43. "description": "Allows every read action for user organizations and in addition allows to administer user organizations.",
  44. "global": true,
  45. "updated": "2021-05-17T20:49:17+02:00",
  46. "created": "2021-05-13T16:24:26+02:00"
  47. },
  48. ...
  49. ]
  50. }

To see what permissions each of the assigned roles have, you can a Get a role by using an HTTP API.

Example request:

  1. curl --location --request GET '<grafana_url>/api/access-control/roles/qQui_LCMk' --header 'Authorization: Basic YWRtaW46cGFzc3dvcmQ='

Example response:

  1. {
  2. "version": 2,
  3. "uid": "qQui_LCMk",
  4. "name": "fixed:users:writer",
  5. "displayName": "User writer",
  6. "description": "Read and update all attributes and settings for all users in Grafana: update user information, read user information, create or enable or disable a user, make a user a Grafana administrator, sign out a user, update a user’s authentication token, or update quotas for all users.",
  7. "global": true,
  8. "permissions": [
  9. {
  10. "action": "org.users:add",
  11. "scope": "users:*",
  12. "updated": "2021-05-17T20:49:18+02:00",
  13. "created": "2021-05-17T20:49:18+02:00"
  14. },
  15. {
  16. "action": "org.users:read",
  17. "scope": "users:*",
  18. "updated": "2021-05-17T20:49:18+02:00",
  19. "created": "2021-05-17T20:49:18+02:00"
  20. },
  21. {
  22. "action": "org.users:remove",
  23. "scope": "users:*",
  24. "updated": "2021-05-17T20:49:18+02:00",
  25. "created": "2021-05-17T20:49:18+02:00"
  26. },
  27. {
  28. "action": "org.users.role:update",
  29. "scope": "users:*",
  30. "updated": "2021-05-17T20:49:18+02:00",
  31. "created": "2021-05-17T20:49:18+02:00"
  32. }
  33. ],
  34. "updated": "2021-05-17T20:49:18+02:00",
  35. "created": "2021-05-13T16:24:26+02:00"
  36. }

Manage roles granted directly to users

To learn about granting roles to users, refer to Manage user role assignments page.

Create your first custom role

You can create your custom role by either using an HTTP API or by using Grafana provisioning. You can take a look at actions and scopes to decide what permissions would you like to map to your role.

Example HTTP request:

  1. curl --location --request POST '<grafana_url>/api/access-control/roles/' \
  2. --header 'Authorization: Basic YWRtaW46cGFzc3dvcmQ=' \
  3. --header 'Content-Type: application/json' \
  4. --data-raw '{
  5. "version": 1,
  6. "uid": "jZrmlLCkGksdka",
  7. "name": "custom:users:admin",
  8. "displayName": "custom users admin",
  9. "description": "My custom role which gives users permissions to create users",
  10. "global": true,
  11. "permissions": [
  12. {
  13. "action": "users:create"
  14. }
  15. ]
  16. }'

Example response:

  1. {
  2. "version": 1,
  3. "uid": "jZrmlLCkGksdka",
  4. "name": "custom:users:admin",
  5. "displayName": "custom users admin",
  6. "description": "My custom role which gives users permissions to create users",
  7. "global": true,
  8. "permissions": [
  9. {
  10. "action": "users:create"
  11. "updated": "2021-05-17T22:07:31.569936+02:00",
  12. "created": "2021-05-17T22:07:31.569935+02:00"
  13. }
  14. ],
  15. "updated": "2021-05-17T22:07:31.564403+02:00",
  16. "created": "2021-05-17T22:07:31.564403+02:00"
  17. }

Once the custom role is created, you can create a built-in role assignment by using an HTTP API. If you created your role using Grafana provisioning, you can also create the assignment with it.

Example HTTP request:

  1. curl --location --request POST '<grafana_url>/api/access-control/builtin-roles' \
  2. --header 'Authorization: Basic YWRtaW46cGFzc3dvcmQ=' \
  3. --header 'Content-Type: application/json' \
  4. --data-raw '{
  5. "roleUid": "jZrmlLCkGksdka",
  6. "builtinRole": "Viewer",
  7. "global": true
  8. }'

Example response:

  1. {
  2. "message": "Built-in role grant added"
  3. }

Allow Viewers to create reports

In order to create reports, you need to have reports.admin:write permission. By default, a Grafana Admin or organization Admin can create reports as there is a built-in role assignment which comes with reports.admin:write permission.

If you want your users who have the Viewer organization role to create reports, you have two options:

  1. Create a built-in role assignment and map the fixed:reporting:admin:edit fixed role to the Viewer built-in role. Note that the fixed:reporting:admin:edit fixed role allows doing more than creating reports. Refer to fixed roles for full list of permission assignments.
  2. Create a custom role with reports.admin:write permission, and create a built-in role assignment for Viewer organization role.

Prevent Grafana Admin from creating and inviting users

In order to create users, you need to have users:create permission. By default, a user with the Grafana Admin role can create users as there is a built-in role assignment which comes with users:create permission.

If you want to prevent Grafana Admin from creating users, you can do the following:

  1. Check all built-in role assignments to see what built-in role assignments are available.
  2. From built-in role assignments, find the role which gives users:create permission. Refer to fixed roles for full list of permission assignments.
  3. Remove the built-in role assignment by using an Fine-grained access control HTTP API or by using Grafana provisioning.

Allow Editors to create new custom roles

By default, the Grafana Server Admin is the only user who can create and manage custom roles. If you want your users to do the same, you have two options:

  1. Create a built-in role assignment and map fixed:permissions:admin:edit and fixed:permissions:admin:read fixed roles to the Editor built-in role.
  2. Create a custom role with roles.builtin:add and roles:write permissions, then create a built-in role assignment for Editor organization role.

Note that any user with the ability to modify roles can only create, update or delete roles with permissions they themselves have been granted. For example, a user with the Editor role would be able to create and manage roles only with the permissions they have, or with a subset of them.

Create a custom role to access alerts in a folder

To see an alert rule in Grafana, the user must have read access to the folder that stores the alert rule, permission to read alerts in the folder, and permission to query all data sources that the rule uses.

The API command in this example is based on the following:

  • A Test-Folder with ID 92
  • Two data sources: DS1 with UID _oAfGYUnk, and DS2 with UID YYcBGYUnk
  • An alert rule that is stored in Test-Folder and queries the two data sources. The following request creates a custom role that includes permissions to access the alert rule:
  1. curl --location --request POST '<grafana_url>/api/access-control/roles/' \
  2. --header 'Authorization: Basic YWRtaW46cGFzc3dvcmQ=' \
  3. --header 'Content-Type: application/json' \
  4. --data-raw '{
  5. "version": 1,
  6. "name": "custom:alerts.reader.in.folder.123",
  7. "displayName": "Read-only access to alerts in folder Test-Folder",
  8. "description": "Let user query DS1 and DS2, and read alerts in folder Test-Folders",
  9. "group":"Custom",
  10. "global": true,
  11. "permissions": [
  12. {
  13. "action": "folders:read",
  14. "scope": "folders:id:92"
  15. },
  16. {
  17. "action": "alert.rules:read",
  18. "scope": "folders:id:92"
  19. },
  20. {
  21. "action": "datasources:query",
  22. "scope": "datasources:uid:_oAfGYUnk"
  23. },
  24. {
  25. "action": "datasources:query",
  26. "scope": "datasources:uid:YYcBGYUnk"
  27. }
  28. ]
  29. }'