简体   繁体   English

带有对象过滤的 kubernetes 资源级别 RBAC

[英]kubernetes resource level RBAC with object filtering

i'm doing some resource level RBAC for k8s custom objects and finding it difficult to get filter resources using native k8s calls我正在为 k8s 自定义对象做一些资源级别的 RBAC,发现使用原生 k8s 调用很难获取过滤资源

cluster is my custom CRD and user john has access to only one crd instance not all instances of CRD using k8s native RBAC cluster是我的自定义 CRD,用户john只能访问一个 crd 实例,而不是所有使用 k8s 本地 RBAC 的 CRD 实例

➜  k get clusters
NAME               AGE
aws-gluohfhcwo     3d2h
azure-cikivygyxd   3d1h

➜  k get clusters --as=john
Error from server (Forbidden): clusters.operator.biqmind.com is forbidden: User "ranbir" cannot list resource "clusters" in API group "operator.biqmind.com" in the namespace "biqmind"

➜  k get clusters --as=john aws-gluohfhcwo
NAME             AGE
aws-gluohfhcwo   3d2h

i have explicitly specify object name to get the list of objects to which user is authenticated.我已明确指定对象名称以获取用户经过身份验证的对象列表。 any suggestions on how this can be solved?关于如何解决这个问题的任何建议?

full rbac is posted here完整的 rbac 发布在这里

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: biqmind
  name: cluster-admin-aws-gluohfhcwo
rules:
- apiGroups: ["operator.biqmind.com"]
  resources: ["clusters"]
  resourceNames: ["aws-gluohfhcwo"]
  verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: cluster-admin-aws-gluohfhcwo-binding
  namespace: biqmind
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: cluster-admin-aws-gluohfhcwo
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: User
  name: ranbir

User "ranbir" cannot list resource "clusters" in API group "operator.biqmind.com" in the namespace "biqmind"用户“ranbir”无法列出命名空间“biqmind”中 API 组“operator.biqmind.com”中的资源“clusters”

You must add RBAC permissions with the verb list for the specified user in the specified namespace, to let that user list "clusters".您必须为指定命名空间中的指定用户添加带有谓词list RBAC 权限,以让该用户list “集群”。

When doing做的时候

kubectl get clusters --as=john aws-gluohfhcwo

you use the RBAC verb get , but to list without specifying a specific name, the user also need permission to list .您使用 RBAC 动词get ,但要在不指定特定名称的情况下进行列表,用户还需要具有list权限。

Example of giving list permission, without resourceName: :授予list权限的示例,没有资源名称::

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: biqmind
  name: cluster-admin-aws-gluohfhcwo
rules:
- apiGroups: ["operator.biqmind.com"]
  resources: ["clusters"]
  resourceNames: ["aws-gluohfhcwo"]
  verbs: ["get", "watch", "create", "update", "patch", "delete"]

- apiGroups: ["operator.biqmind.com"]
  resources: ["clusters"]
  verbs: ["get", "list"]

Enforcing RBAC on the user side is easy in concept.在用户端实施RBAC在概念上很容易。 You can create RoleBindings for individual users, but this is not the recommended path as there's a high risk of operator insanity.您可以为单个用户创建角色绑定,但这不是推荐的路径,因为存在操作员精神错乱的高风险。

The better approach for sane RBAC is to create that your users map to;健全的 RBAC 的更好方法是创建您的用户映射到的; how this mapping is done is dependent on your cluster's authenticator (eg the aws-iam-authenticator for EKS uses mapRoles to map a role ARN to a set of groups).此映射的完成方式取决于您的集群的身份验证器(例如,用于 EKS 的 aws-iam-authenticator 使用 mapRoles 将角色 ARN 映射到一组组)。

Groups and the APIs they have access to are ultimately determined based on an organization's needs, but a generic reader (for new engineers just getting the hang of things), writer (for your engineers), and admin (for you) role is a good start.组和他们有权访问的 API 最终是根据组织的需求确定的,但通用读者(对于刚掌握事物的新工程师)、作者(对于您的工程师)和管理员(对于您)角色是一个很好的选择开始。 (Hey, it's better than admin for everyone.) (嘿,这对每个人来说都比管理员好。)

Here is example of configuration file:下面是配置文件的例子:

# An example reader ClusterRole – ClusterRole so you’re not worried about namespaces at this time. Remember, we’re talking generic reader/writer/admin roles.
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: reader
rules:
– apiGroups: [“*”]
resources:
– deployments
– configmaps
– pods
– secrets
– services
verbs:
– get
– list
– watch
---
# An example reader ClusterRoleBinding that gives read permissions to
# the engineering and operations groups
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: reader-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: reader
subjects:
- kind: Group
  name: umbrella:engineering
- kind: Group
  name: umbrella:operations
---
# An example writer ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: writer
rules:
- apiGroups: [“*”]
resources:
- deployments
- configmaps
- pods
- secrets
- services
verbs:
- create
- delete
- patch
- update
---
# An example writer ClusterRoleBinding that gives write permissions to
# the operations group
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: reader-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: reader
subjects:
- kind: Group
  name: umbrella:operations

Here is exact explanation: rbac-article .这是确切的解释: rbac-article

Take notice that default Roles and Role Bindings请注意默认角色和角色绑定

API servers create a set of default ClusterRole and ClusterRoleBinding objects. API 服务器创建一组默认的 ClusterRole 和 ClusterRoleBinding 对象。 Many of these are system: prefixed, which indicates that the resource is “owned” by the infrastructure.其中许多是 system: 前缀,表示资源由基础设施“拥有”。 Modifications to these resources can result in non-functional clusters.对这些资源的修改可能会导致无法正常工作的集群。 One example is the system:node ClusterRole.一个例子是 system:node ClusterRole。 This role defines permissions for kubelets.该角色定义了 kubelet 的权限。 If the role is modified, it can prevent kubelets from working.如果角色被修改,它会阻止 kubelets 工作。

All of the default cluster roles and rolebindings are labeled with kubernetes.io/bootstrapping=rbac-defaults .所有默认集群角色和角色绑定都标有kubernetes.io/bootstrapping=rbac-defaults

Remember about auto-reconciliation记住自动对帐

At each start-up, the API server updates default cluster roles with any missing permissions, and updates default cluster role bindings with any missing subjects.每次启动时,API 服务器都会更新默认集群角色的任何缺失权限,并更新默认集群角色绑定的任何缺失主题。 This allows the cluster to repair accidental modifications, and to keep roles and rolebindings up-to-date as permissions and subjects change in new releases.这允许集群修复意外修改,并在新版本中的权限和主题更改时使角色和角色绑定保持最新。

To opt out of this reconciliation, set the rbac.authorization.kubernetes.io/autoupdate annotation on a default cluster role or rolebinding to false.要选择退出此协调,请将默认集群角色或角色绑定上的 rbac.authorization.kubernetes.io/autoupdate 注释设置为 false。 Be aware that missing default permissions and subjects can result in non-functional clusters.请注意,缺少默认权限和主题可能会导致无法正常工作的集群。

Auto-reconciliation is enabled in Kubernetes version 1.6+ when the RBAC authorizer is active.自动对账在Kubernetes版本1.6+启用时的RBAC授权是有效的。

Useful article: understanding-rbac .有用的文章: 了解-rbac

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM