菜单
Grafana Cloud 企业版

管理 RBAC 角色

注意

可用版本

Grafana EnterpriseGrafana Cloud

本节包含关于如何查看与角色相关的权限、创建自定义角色以及更新和删除角色的说明。

以下示例包含 base64 编码的 username:password 基本授权。您不能在请求中使用授权令牌。

列出与角色相关的权限

使用 GET 命令查看与角色相关的操作和范围。有关查看每个角色的权限列表的更多信息,请参阅获取角色

要查看与基本角色相关的权限,请参阅以下基本角色 UID

基本角色UID
basic_none
查看者basic_viewer
编辑者basic_editor
管理员basic_admin
Grafana 管理员basic_grafana_admin

示例请求

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

示例响应

{
    "version": 2,
    "uid": "qQui_LCMk",
    "name": "fixed:users:writer",
    "displayName": "User writer",
    "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.",
    "global": true,
    "permissions": [
        {
            "action": "org.users:add",
            "scope": "users:*",
            "updated": "2021-05-17T20:49:18+02:00",
            "created": "2021-05-17T20:49:18+02:00"
        },
        {
            "action": "org.users:read",
            "scope": "users:*",
            "updated": "2021-05-17T20:49:18+02:00",
            "created": "2021-05-17T20:49:18+02:00"
        },
        {
            "action": "org.users:remove",
            "scope": "users:*",
            "updated": "2021-05-17T20:49:18+02:00",
            "created": "2021-05-17T20:49:18+02:00"
        },
        {
            "action": "org.users:write",
            "scope": "users:*",
            "updated": "2021-05-17T20:49:18+02:00",
            "created": "2021-05-17T20:49:18+02:00"
        }
    ],
    "updated": "2021-05-17T20:49:18+02:00",
    "created": "2021-05-13T16:24:26+02:00"
}

更多详细信息请参阅 RBAC HTTP API

创建自定义角色

本节展示了如何使用 Grafana 配置和 HTTP API 创建自定义 RBAC 角色。

当基本角色和固定角色不满足您的权限要求时,您可以创建自定义角色。

开始之前

  • 规划您的 RBAC 部署策略.
  • 确定要添加到自定义角色的权限。要查看操作和范围列表,请参阅RBAC 权限、操作和范围
  • 启用角色配置.
  • 确保您拥有创建自定义角色的权限。
    • 默认情况下,Grafana 管理员角色具有创建自定义角色的权限。
    • Grafana 管理员可以通过创建具有相关权限的自定义角色并添加 permissions:type:delegate 范围来将自定义角色权限委托给其他用户。

使用配置创建自定义角色

基于文件的配置是您可以用来创建自定义角色的一种方法。

  1. 打开 YAML 配置文件并找到 roles 部分。

  2. 请参阅下表添加属性和值。

属性描述
name角色的友好标识符,帮助管理员理解角色的用途。name 是必需的,不能超过 190 个字符。建议使用 ASCII 字符。角色名称在组织内必须是唯一的。
uid与角色关联的唯一标识符。UID 使您可以更改或删除角色。您可以自己生成 UID,也可以让 Grafana 为您生成。在同一个 Grafana 实例中不能使用相同的 UID。
orgId标识角色所属的组织。如果未指定 orgId,则使用默认组织 ID
global全局角色不与任何特定组织关联,这意味着您可以在所有组织中重复使用它们。此设置覆盖 orgId
displayNameUI 中显示的友好文本。角色显示名称不能超过 190 个基于 ASCII 的字符。对于固定角色,显示名称将按指定显示。如果您未设置显示名称,则显示名称会用 ' '(空格)替换 ':'(冒号)。
description描述角色提供的权限的友好文本。
group在角色选择器中组织角色。
version定义角色当前版本的正整数,防止覆盖较新的更改。
hidden隐藏角色不会出现在角色选择器中。
state角色的状态。默认为 present,如果设置为 absent,则将移除角色。
force可与状态 absent 一起使用,强制移除角色及其所有分配。
from一个可选的角色列表,您可以从中复制权限。
permissions为用户提供对 Grafana 资源的访问权限。有关权限列表,请参阅RBAC 权限操作和范围。如果您不知道要分配哪些权限,可以创建并分配没有任何权限的角色作为占位符。使用 from 属性,您可以通过向权限列表中添加 state 来指定要添加或移除的附加权限。
  1. 重新加载配置配置文件。

    有关在运行时重新加载配置配置文件的更多信息,请参阅重新加载配置配置

以下示例创建一个本地角色

yaml
# config file version
apiVersion: 2

roles:
  - name: custom:users:writer
    description: 'List, create, or update other users.'
    version: 1
    orgId: 1
    permissions:
      - action: 'users:read'
        scope: 'global.users:*'
      - action: 'users:write'
        scope: 'global.users:*'
      - action: 'users:create'

以下示例创建一个隐藏的全局角色。global: true 选项创建一个全局角色,而 hidden: true 选项在角色选择器中隐藏该角色。

yaml
# config file version
apiVersion: 2

roles:
  - name: custom:users:writer
    description: 'List, create, or update other users.'
    version: 1
    global: true
    hidden: true
    permissions:
      - action: 'users:read'
        scope: 'global.users:*'
      - action: 'users:write'
        scope: 'global.users:*'
      - action: 'users:create'

以下示例基于其他固定角色创建一个全局角色。from 选项包含我们想要复制权限的角色。可以使用权限 state: absent 选项指定要从复制中排除的权限。

yaml
# config file version
apiVersion: 2

roles:
  - name: custom:org.users:writer
    description: 'List and remove other users from the organization.'
    version: 1
    global: true
    from:
      - name: 'fixed:org.users:reader'
        global: true
      - name: 'fixed:org.users:writer'
        global: true
    permissions:
      - action: 'org.users:write'
        scope: 'users:*'
        state: 'absent'
      - action: 'org.users:add'
        scope: 'users:*'
        state: 'absent'

使用 HTTP API 创建自定义角色

以下示例展示了如何使用 Grafana HTTP API 创建自定义角色。有关 HTTP API 的更多信息,请参阅创建新自定义角色

注意

您不能创建包含您不具备的权限的自定义角色。例如,如果您仅拥有 users:create 权限,则不能创建包含其他权限的角色。

以下示例创建了一个 custom:users:admin 角色并为其分配了 users:create 操作。

示例请求

curl --location --request POST '<grafana_url>/api/access-control/roles/' \
--header 'Authorization: Basic YWRtaW46cGFzc3dvcmQ=' \
--header 'Content-Type: application/json' \
--data-raw '{
    "version": 1,
    "uid": "jZrmlLCkGksdka",
    "name": "custom:users:admin",
    "displayName": "custom users admin",
    "description": "My custom role which gives users permissions to create users",
    "global": true,
    "permissions": [
        {
            "action": "users:create"
        }
    ]
}'

示例响应

{
    "version": 1,
    "uid": "jZrmlLCkGksdka",
    "name": "custom:users:admin",
    "displayName": "custom users admin",
    "description": "My custom role which gives users permissions to create users",
    "global": true,
    "permissions": [
        {
            "action": "users:create"
            "updated": "2021-05-17T22:07:31.569936+02:00",
            "created": "2021-05-17T22:07:31.569935+02:00"
        }
    ],
    "updated": "2021-05-17T22:07:31.564403+02:00",
    "created": "2021-05-17T22:07:31.564403+02:00"
}

更多详细信息请参阅 RBAC HTTP API

更新基本角色权限

如果默认的基本角色定义不符合您的要求,您可以更改其权限。

开始之前

  • 确定要从基本角色中添加或移除的权限。有关与基本角色相关的权限的更多信息,请参阅RBAC 角色定义

注意

您不能修改 No Basic Role 的权限。

要更改基本角色的权限

  1. 打开 YAML 配置文件并找到 roles 部分。

  2. 请参阅下表添加属性和值。

    属性描述
    name要更新的基本角色的名称。您可以指定 uid 而不是角色名称。角色 nameuid 是必需的。
    orgId标识角色所属的组织。可以使用 global 代替来指定它是全局角色。
    version标识角色的版本,防止覆盖较新的更改。
    overrideRole如果设置为 true,无论其在数据库中的版本如何,都将更新角色。如果 overrideRole 设置为 true,则无需指定 version
    from要从中复制权限的角色列表。
    permissions > state权限的状态。您可以将其设置为 absent 以确保将其排除在复制列表之外。
  3. 重新加载配置配置文件。

    有关在运行时重新加载配置配置文件的更多信息,请参阅重新加载配置配置

以下示例修改 Grafana Admin 基本角色权限。

  • 移除列出、授予和撤销团队角色的权限。
  • 添加读取和写入 Grafana 文件夹的权限。
yaml
# config file version
apiVersion: 2

roles:
  - name: 'basic:grafana_admin'
    global: true
    version: 3
    from:
      - name: 'basic:grafana_admin'
        global: true
    permissions:
      # Permissions to remove
      - action: 'teams.roles:read'
        scope: 'teams:*'
        state: 'absent'
      - action: 'teams.roles:remove'
        scope: 'permissions:type:delegate'
        state: 'absent'
      - action: 'teams.roles:add'
        scope: 'permissions:type:delegate'
        state: 'absent'
      # Permissions to add
      - action: 'folders:read'
        scope: 'folder:*'
      - action: 'folders:write'
        scope: 'folder:*'

注意

您可以在 from 部分添加多个 fixedbasiccustom 角色。它们的权限将被复制并添加到基本角色中。请务必递增角色版本,以便反映更改。

您还可以使用 API 更改基本角色的权限。更多详细信息请参阅 RBAC HTTP API

将基本角色重置为默认值

本节介绍如何将基本角色重置为其默认值。

您有两种选项可以将基本角色权限重置为其默认值。

使用配置选项

注意:自 Grafana Enterprise 9.4 版本起可用。

警告:如果此选项保持为 true,权限将在每次启动时重置。

使用 reset_basic_roles 选项在 Grafana 实例启动时将基本角色权限重置为其默认值。

  1. 打开您的配置文件并按如下更新 rbac 部分
bash
[rbac]
reset_basic_roles = true

使用 http 端点

除了配置选项外,还可以使用 HTTP 端点。

  1. 打开 YAML 配置文件并找到 roles 部分。

  2. 授予 action: "roles:write", scope: "permissions:type:escalate" 权限给 Grafana Admin。请注意,此权限默认未授予任何基本角色,因为用户可以通过基本角色权限重置获得比之前更多的权限。

    yaml
    apiVersion: 2
    roles:
      - name: 'basic:grafana_admin'
        global: true
        version: 3
        from:
          - name: 'basic:grafana_admin'
            global: true
        permissions:
          # Permission allowing to reset basic roles
          - action: 'roles:write'
            scope: 'permissions:type:escalate'
  3. 作为 Grafana Admin,调用 API 端点将基本角色重置为默认值。更多详细信息请参阅 RBAC HTTP API

使用 Grafana 配置删除自定义角色

不再需要自定义角色时将其删除。当您删除自定义角色时,该自定义角色将从分配给它的用户和团队中移除。

开始之前

  • 识别要删除的角色。
  • 确保您可以访问 YAML 配置文件。

要删除自定义角色

  1. 打开 YAML 配置文件并找到 roles 部分。

  2. 请参阅下表添加属性和值。

    属性描述
    name要删除的自定义角色的名称。您可以指定 uid 而不是角色名称。角色 nameuid 是必需的。
    orgId标识角色所属的组织。
    state角色的状态设置为 absent 以触发其移除。
    force当设置为 true 时,即使存在现有分配,也会移除角色。
  3. 重新加载配置配置文件。

    有关在运行时重新加载配置配置文件的更多信息,请参阅重新加载配置配置

以下示例删除一个自定义角色

yaml
# config file version
apiVersion: 2

roles:
  - name: 'custom:reports:editor'
    orgId: 1
    state: 'absent'
    force: true

您还可以使用 API 删除自定义角色。更多详细信息请参阅 RBAC HTTP API