【问题标题】:Restrict user to access only one service in a namespace限制用户只能访问命名空间中的一项服务
【发布时间】:2019-05-12 15:23:14
【问题描述】:

我一直在尝试一种场景,即用户应该能够对命名空间中的服务执行所有操作,但在一个服务上他应该只能执行读取操作。

以下是我用来授予集群级别所有用户访问服务的集群角色。

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: test-clusterRole
rules:
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - pods/exec
  verbs:
  - create
- apiGroups:
  - ""
  resources:
  - replicationcontrollers
  - services
  verbs:
  - get
  - list
  - watch
  - create
  - delete
  - update
- apiGroups:
  - ""
  resources:
  - persistentvolumeclaims
  - serviceaccounts
  - namespaces/status
  - pods/log
  - pods/status
  - replicationcontrollers/status
  - resourcequotas
  - resourcequotas/status
  - namespaces
  - persistentvolumes
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - configmaps
  - secrets
  verbs:
  - get
  - list
  - watch
  - create
  - update
  - delete
- apiGroups:
  - apps
  resources:
  - deployments
  - replicasets
  - statefulsets
  verbs:
  - get
  - list
  - watch
  - create
  - update
  - delete
- apiGroups:
  - extensions
  resources:
  - ingresses
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - extensions
  resources:
  - replicasets
  - deployments
  verbs:
  - get
  - list
  - watch
  - create
  - update
  - delete

并且我已经为上述 ClusterRole 创建了关联的 RoleBinding。

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: test-roleBinding
  namespace: test-namespace
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: User
  name: pradeep
- kind: ServiceAccount
  name: default
  namespace: test-namespace
roleRef:
  kind: ClusterRole
  name: test-clusterRole
  apiGroup: rbac.authorization.k8s.io

现在,我正在尝试为命名空间“test-namespace”创建角色和 RoleBinding,以限制用户“pradeep”对特定服务“test-service”的只读访问,如下所示

角色:

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
    name: test-role
    namespace: test-namespace
rules:
  - apiGroups: [""]
    resources: ["services"]
    resourceNames : ["test-service"]
    verbs: ["get","list","watch"]

角色绑定:

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: test-roleBinding1
  namespace: test-namespace
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: User
  name: pradeep
- kind: ServiceAccount
  name: default
  namespace: test-namespace
roleRef:
  kind: Role
  name: test-role
  apiGroup: rbac.authorization.k8s.io

但是,用户“pradeep”仍然能够出于某种原因删除指定的服务“test-service”。 test-clusterRole 权限是否覆盖了 test-role 权限?如果是这样,如何解决这个问题。

如果没有,请提出一种实现此场景的方法。

【问题讨论】:

    标签: service kubernetes roles rbac


    【解决方案1】:

    ClusterRole 和 Role 权限是相加的。 ClusterRole 权限将作为任何命名空间的基本权限,并将特定命名空间的角色权限添加到其中。

    如果用户只能访问单个命名空间,则不能将他分配给 ClusterRole。

    【讨论】:

    • 感谢您的回复@Lukas Eichler。但是,我使用集群角色为集群周围的所有用户提供默认权限。当谈到命名空间时,我不希望用户编辑或删除某些服务。因此,对于命名空间级别的第二个角色和角色绑定,我试图将权限限制为仅 ["get"、"list"、"watch"] 并且他们仍然能够编辑服务。我想避免这种情况。有什么办法吗?
    • 您应该避免将 ClusterRoles 提供给普通用户。在命名空间中为用户提供角色。
    猜你喜欢
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2012-07-23
    • 2017-08-26
    • 1970-01-01
    • 2021-01-21
    • 2014-02-22
    • 1970-01-01
    相关资源
    最近更新 更多