Kubernetes RBAC规则动词列表

问题描述 投票:0回答:5

我想给我的应用程序提供有限的访问权限,以获取不同状态集(可能还有部署)的副本,并在必要时扩大或缩小它们。

我为此创建了 ServiceAccount、Rolebinding 和 Role,但我找不到完整的规则动词列表(“get”、“watch”、“list”、“update”)以及它们的局限性,例如我可以使用

update
缩放或者我需要另一个动词?我在哪里可以找到描述这些动词的列表或表格?

我的yaml文件:

kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: scaler-role
  namespace: {{ .Release.Namespace  | quote }}
rules:
- apiGroups: ["apps"]
  resources: ["statefulset"]
  verbs: ["get", "watch", "list", "update"]
kubernetes service-accounts rbac
5个回答
53
投票

你可以通过这个获得相当多的信息:

kubectl api-resources --sort-by name -o wide

上面的

api-resources
命令是明确的并且易于grep。可以这样获得可能动词的完整列表:

$ kubectl api-resources --no-headers --sort-by name -o wide | sed 's/.*\[//g' | tr -d "]" | tr " " "\n" | sort | uniq
create
delete
deletecollection
get
list
patch
update
watch

API 参考文档的Resource Operations 部分(例如https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/)谈了一些关于它们但没有提到

 deletecollection
(顺便说一句:请参阅有关deletecollection
有趣信息;建议无论何时您提供
delete
,如果资源支持,您也应该授予
deletecollection
权限)。

Authorization OverviewDetermine the Request Verb部分确实简要提到了

deletecollection
,以及另外六个动词(例如@RoryMcCune正确指出的
escalate
),不幸的是,没有出现在
kubectl api-resources -o wide
命令的输出中。

BTW

api-resources
命令还列出了命令的简称,例如
svc
代表
services
.

2023 年 5 月更新:

另一种不太用户友好但更完整的获取动词的方法是直接查询 API 服务器:

  1. 在一个终端中,启动API服务器的代理;例如
    kubectl proxy --port=8080
  2. 在另一个终端,在
    curl
    /api/v1
    上使用
    /apis

对于核心资源(配置映射等):

使用

curl -s lo calhost:8080 /api/v1
获取带有每个核心资源类型名称动词的json。例如(如果你有
jq

$ curl -s http://localhost:8080/api/v1 | jq '.resources[] | [.name, (.verbs | join(" "))] | join(" = ")' -r
bindings = create
componentstatuses = get list
configmaps = create delete deletecollection get list patch update watch
endpoints = create delete deletecollection get list patch update watch
...

对于非核心资源(部署、CRD 等):

假设你想要部署的动词,你知道部署的 API 组是

apps
。首先使用
curl -s http://localhost:8080/apis
获取该 API 的版本组名称。例如(如果你有
jq

```
$ curl -s http://localhost:8080/apis | jq '.groups[].preferredVersion.groupVersion' -r | grep ^apps
apps/v1
```

通过使用

curl -s http://localhost:8080/apis/VERSIONED_API
,即在上面的示例中
curl -s http://localhost:8080/apis/apps/v1
,使用它来查询该组的API 以获取动词。 eg(如果你有
jq
,jq是一样的),

```
$ curl -s http://localhost:8080/apis/apps/v1 | jq '.resources[] | [.name, (.verbs | join(" "))] | join(" = ")' -r
controllerrevisions = create delete deletecollection get list patch update watch
daemonsets = create delete deletecollection get list patch update watch
daemonsets/status = get patch update
deployments = create delete deletecollection get list patch update watch
deployments/scale = get patch update
deployments/status = get patch update
...
```

顺便说一句,页面https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/ 记录了如何使用 Python、Java 等代替 curl。

我创建了一个 kubectl 插件,用于想要获取特定资源类型的动词的用例:https://github.com/schollii/my-devops-lab/blob/main/kubernetes/kubectl-verbs .例如

$ kubectl verbs configmaps
configmaps = create delete deletecollection get list patch update watch

$ kubectl verbs deployments apps
deployments = create delete deletecollection get list patch update watch
deployments/scale = get patch update
deployments/status = get patch update

该文件包含将其安装为插件的说明。这是一个简单的 bash 脚本。


40
投票

这里是RBAC动词列表:

对于缩放,我认为您需要写入权限(

create
update
patch
)以及读取权限(
get
list
watch
)。


5
投票

可以在这里找到动词列表https://kubernetes.io/docs/reference/access-authn-authz/authorization/#review-your-request-attributes

可以在此处找到简要说明https://kubernetes.io/docs/reference/access-authn-authz/authorization/#determine-the-request-verb

我有一个角色,我用它来更新部署的 docker 图像标签,看起来像这样(我不使用我的来创建部署,只是为了修补图像标签)

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: deployer
rules:
- apiGroups: ["apps"]
  resources: ["deployments"]
  verbs: ["get", "patch"]


1
投票

在 Linux/Mac/WSL/等上

  1. 运行:kubectl proxy &
  2. 运行:curl http://127.0.0.1:8001 -k | grep -v '路径' | grep '"' | sed -e 's/"//g' -e 's/,//g' |排序 |读行时;做 kubectl get --raw ${line} ;完成 | jq |少
  3. 在less中搜索您需要的api以查看动词。

1
投票

与此相关,要了解 apiGroups 的完整动词列表和正在运行的集群上的资源,包括通过运算符或 CRD 引入的任何其他动词,您可以执行以下操作。

在终端中打开:

kubectl proxy

然后在不同的终端,你可以运行:

# List all API urls
curl http://localhost:8001/ | yq '.paths[]'

# List all objects and verbs for an API path like /api/v1
curl http://localhost:8001/api/v1 | yq '.resources[] | [{"resources":.name,"verbs":.verbs}]'

如果你想要一个脚本以一种很好的方式在一个地方列出所有的东西,运行这个:

#!/bin/bash

# Remember to run on another terminal before this script:
# kubectl proxy

set -euo pipefail

# Show Kubernetes server version
echo -n '# '
kubectl version --short 2>/dev/null | grep 'Server'

for url in $(curl -s http://localhost:8001/ | yq '.paths[]' | xargs) ; do 
    [ "$url" == "/metrics" ] && continue
    doc=$(curl -s http://localhost:8001$url)

    # If URL doesn't publish info, skip
    [ "$doc" == "ok" ] && continue

    # Remove from apiGroup prefix (/, /api/v1, /apis/) and suffix ( /v1, /v1beta1)
    apiGroup=$(echo $url | sed 's/\/api\/v1//' | sed 's/\/apis\///' | sed 's/^\///' | sed 's/\/v[0-9]*\(beta[0-9]\+\)\?$//'  )

    # Get permissions and format them nicely in YAML
    yaml=$(echo "$doc" | yq -M '[.resources[] | {"apiGroups":["'$apiGroup'"], "resources":[.name],"verbs":.verbs}] | .. style="double" | .[].* style="flow"' 2>/dev/null ||:);
    
    # TODO: group resources from the same apiGroup with the same verbs together

    # If document is empty, skip it
    [ "$yaml" == "[]" ] && continue

    echo ""
    echo "# $url"
    echo "$yaml"
done

您还可以在此要点上找到该脚本: https://gist.github.com/vicenteherrera/0bfe2762ecd5794eba65ed19d0d51188

当你执行它时,你可以将输出保存在一个文件中:

./list_verbs.sh >verbs.yaml

这里是一个简短的输出示例:

# /apis/templates.gatekeeper.sh/v1beta1
- apiGroups: ["templates.gatekeeper.sh"]
  resources: ["constrainttemplates"]
  verbs: ["delete", "deletecollection", "get", "list", "patch", "create", "update", "watch"]
- apiGroups: ["templates.gatekeeper.sh"]
  resources: ["constrainttemplates/status"]
  verbs: ["get", "patch", "update"]

然后您只需将该文件中的 YAML 块复制并粘贴到您的 Kubernetes 角色,它已经是该角色预期的格式。

© www.soinside.com 2019 - 2024. All rights reserved.