gpt4 book ai didi

kubernetes - 如何引用角色定义中的所有子资源?

转载 作者:行者123 更新时间:2023-12-02 11:36:57 25 4
gpt4 key购买 nike

这是一个简单的 Kubernetes 角色:

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: Role
metadata:
name: temp-role
namespace: stackoverflow
rules:
- apiGroups: [""]
resources:
- pods
verbs:
- get

这个角色让我说 kubectl get pod foobar我可以拿到 pods 。

但是,我现在无法获取 pod 日志:
Error from server (Forbidden): pods "foobar" is forbidden: User "system:serviceaccount:kube-system:myuser" cannot get resource "pods/log" in API group "" in the namespace "stackoverflow"

所以错误告诉我有一个单独的子资源 pods/log我需要在我的资源中明确提及。

有趣的是 kubectl auth can-i对我撒谎:
$ kubectl -n stackoverflow auth can-i get pods/log                                                                           
yes

好的,让我们解决这个问题并直接提及子资源:
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: Role
metadata:
name: temp-role
namespace: stackoverflow
rules:
- apiGroups: [""]
resources:
- pods
- pods/log
verbs:
- get

现在我可以正确检索日志了!

所以有什么问题

问题是,我正在尝试创建一个 ClusterRole具有对某些特定资源的读/写访问权限(特别是 edit ClusterRole 的子集),我曾希望我可以通过使用 kubectl api-resources 来完成它并允许一切从那里开始,除了我不想允许的少数事情。

但是像 pods/log这样的子资源不要出现在列表中,所以这种方法不起作用 - 我会阻止访问我打算公开的一些东西,但我什至不知道到底是什么。我只知道 pods/log在我尝试之后发现它不起作用。

所以我正在寻找一种方法:
  • rules.resources 中提及包含所有子资源的资源(我尝试了 pods/* 但它似乎没有做任何事情)
  • 如果以上不可行:获取所有资源和子资源的列表,以便我可以在 rules.resources 中将它们全部列入白名单个别。

  • 想法?

    最佳答案

    答案的灵感来自 [Bash] [Kubernetes] Script to List All Available Resource/Sub-resource Name for RBAC Configuration文章。

    2个脚本,都对我有用:

    _list=($(kubectl get --raw / |grep "^    \"/api"|sed 's/[",]//g')); 
    for _api in ${_list[@]}; do
    _aruyo=$(kubectl get --raw ${_api} | jq .resources);
    if [ "x${_aruyo}" != "xnull" ]; then
    echo;
    echo "===${_api}===";
    kubectl get --raw ${_api} | jq -r ".resources[].name";
    fi;
    done

    或者
    _list=($(kubectl get --raw / |grep "^    \"/api"|sed 's/[",]//g')); for _api in ${_list[@]}; do _aruyo=$(kubectl get --raw ${_api} | jq .resources); if [ "x${_aruyo}" != "xnull" ]; then echo; echo "===${_api}==="; kubectl get --raw ${_api} | jq -r ".resources[].name"; fi; done

    结果:
    ===/api/v1===
    bindings
    componentstatuses
    configmaps
    endpoints
    events
    limitranges
    namespaces
    namespaces/finalize
    namespaces/status
    nodes
    nodes/proxy
    nodes/status
    persistentvolumeclaims
    persistentvolumeclaims/status
    persistentvolumes
    persistentvolumes/status
    pods
    pods/attach
    pods/binding
    pods/eviction
    pods/exec
    pods/log
    pods/portforward
    pods/proxy
    pods/status
    podtemplates
    replicationcontrollers
    replicationcontrollers/scale
    replicationcontrollers/status
    resourcequotas
    resourcequotas/status
    secrets
    serviceaccounts
    serviceaccounts/token
    services
    services/proxy
    services/status

    ===/apis/admissionregistration.k8s.io/v1beta1===
    mutatingwebhookconfigurations
    validatingwebhookconfigurations

    ===/apis/apiextensions.k8s.io/v1beta1===
    customresourcedefinitions
    customresourcedefinitions/status

    ===/apis/apiregistration.k8s.io/v1===
    apiservices
    apiservices/status

    ===/apis/apiregistration.k8s.io/v1beta1===
    apiservices
    apiservices/status

    ===/apis/apps/v1===
    controllerrevisions
    daemonsets
    daemonsets/status
    deployments
    deployments/scale
    deployments/status
    replicasets
    replicasets/scale
    replicasets/status
    statefulsets
    statefulsets/scale
    statefulsets/status

    ===/apis/apps/v1beta1===
    controllerrevisions
    deployments
    deployments/rollback
    deployments/scale
    deployments/status
    statefulsets
    statefulsets/scale
    statefulsets/status

    ===/apis/apps/v1beta2===
    controllerrevisions
    daemonsets
    daemonsets/status
    deployments
    deployments/scale
    deployments/status
    replicasets
    replicasets/scale
    replicasets/status
    statefulsets
    statefulsets/scale
    statefulsets/status

    ===/apis/authentication.k8s.io/v1===
    tokenreviews

    ===/apis/authentication.k8s.io/v1beta1===
    tokenreviews

    ===/apis/authorization.k8s.io/v1===
    localsubjectaccessreviews
    selfsubjectaccessreviews
    selfsubjectrulesreviews
    subjectaccessreviews

    ===/apis/authorization.k8s.io/v1beta1===
    localsubjectaccessreviews
    selfsubjectaccessreviews
    selfsubjectrulesreviews
    subjectaccessreviews

    ===/apis/autoscaling/v1===
    horizontalpodautoscalers
    horizontalpodautoscalers/status

    ===/apis/autoscaling/v2beta1===
    horizontalpodautoscalers
    horizontalpodautoscalers/status

    ===/apis/batch/v1===
    jobs
    jobs/status

    ===/apis/batch/v1beta1===
    cronjobs
    cronjobs/status

    ===/apis/certificates.k8s.io/v1beta1===
    certificatesigningrequests
    certificatesigningrequests/approval
    certificatesigningrequests/status

    ===/apis/cloud.google.com/v1beta1===
    backendconfigs

    ===/apis/coordination.k8s.io/v1beta1===
    leases

    ===/apis/extensions/v1beta1===
    daemonsets
    daemonsets/status
    deployments
    deployments/rollback
    deployments/scale
    deployments/status
    ingresses
    ingresses/status
    networkpolicies
    podsecuritypolicies
    replicasets
    replicasets/scale
    replicasets/status
    replicationcontrollers
    replicationcontrollers/scale

    ===/apis/metrics.k8s.io/v1beta1===
    nodes
    pods

    ===/apis/networking.gke.io/v1beta1===
    managedcertificates

    ===/apis/networking.k8s.io/v1===
    networkpolicies

    ===/apis/policy/v1beta1===
    poddisruptionbudgets
    poddisruptionbudgets/status
    podsecuritypolicies

    ===/apis/rbac.authorization.k8s.io/v1===
    clusterrolebindings
    clusterroles
    rolebindings
    roles

    ===/apis/rbac.authorization.k8s.io/v1beta1===
    clusterrolebindings
    clusterroles
    rolebindings
    roles

    ===/apis/scalingpolicy.kope.io/v1alpha1===
    scalingpolicies

    ===/apis/scheduling.k8s.io/v1beta1===
    priorityclasses

    ===/apis/storage.k8s.io/v1===
    storageclasses
    volumeattachments
    volumeattachments/status

    ===/apis/storage.k8s.io/v1beta1===
    storageclasses
    volumeattachments

    我还想做的 - 是请注意 kubernetes 不允许您获得此列表 ny 默认值,这是预期的和设计的。

    引用 Permission to "pods/*" should work

    评论:

    services/* does not grant permissions to service status updates.

    If you want to give unrestricted access to all resources, you can grant that with *

    Unrestricted access to all current and future subresources is misleading to reason about. Different subresources are used for different purposes. Authorizing all subresources of a resource assumes that no new subresource will ever be added that grants access to far more powerful capabilities. Granting access to pods/* would allow what is currently a restricted user access to future subresources, even if those subresources far exceeded the capabilities of the current subresources.

    The format */scale can be used to grant access to the subresource named scale on all resources, and is useful for things like autoscaling which needs access to a specific subresource.

    关于kubernetes - 如何引用角色定义中的所有子资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57872201/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com