Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kubernetes 正确使用Role.rules.resourceNames创建对资源访问受限的POD_Kubernetes_Kubernetes Pod_Kubernetes Rbac - Fatal编程技术网

Kubernetes 正确使用Role.rules.resourceNames创建对资源访问受限的POD

Kubernetes 正确使用Role.rules.resourceNames创建对资源访问受限的POD,kubernetes,kubernetes-pod,kubernetes-rbac,Kubernetes,Kubernetes Pod,Kubernetes Rbac,我正在尝试创建一个Pod,该Pod能够使用Role.rules.resourceNames创建和更新特定的configmap。我能够从pod中执行资源到API的get请求,但是我不能创建资源,而是获取资源 $ kubectl logs rbac-test Error from server (Forbidden): error when creating "/config/aaa.yaml": configmaps is forbidden: User "syste

我正在尝试创建一个Pod,该Pod能够使用
Role.rules.resourceNames
创建和更新特定的configmap。我能够从pod中执行资源到API的get请求,但是我不能创建资源,而是获取资源

$ kubectl logs rbac-test
Error from server (Forbidden): error when creating "/config/aaa.yaml": configmaps is forbidden: User "system:serviceaccount:default:rbac-test" cannot create resource "configmaps" in API group "" in the namespace "default"
如果我删除
resourceNames
属性,我就可以创建configmap,但我不一定希望这个pod能够随意创建和更新configmaps。如何限制serviceAccount的角色,使此pod只能操作命名的configmap
aaa

编辑 我在kubernetes slack得到了一些帮助(谢谢你,Alan)。RBAC发生在URL级别,因此resourceName受限授权不能用于创建URL,因为URL中没有资源名称

种类:吊舱
版本:v1
元数据:
名称:rbac测试
规格:
餐馆政策:永不
serviceAccountName:rbac测试
你的秘密:
-名称:docker hub creds
容器:
-名称:rbac测试
图片:bitnami/kubectl
命令:[“kubectl”]
参数:[“应用”、“-f”、“/config/aaa.yaml”]
体积数量:
-名称:aaa配置
装载路径:/config/
卷数:
-名称:aaa配置
配置映射:
名称:aaa配置
---
种类:配置地图
版本:v1
元数据:
名称:aaa配置
数据:
aaa.yaml:|
种类:配置地图
版本:v1
元数据:
姓名:aaa
数据:
价值:na
---
版本:v1
种类:服务帐户
元数据:
名称:rbac测试
---
apiVersion:rbac.authorization.k8s.io/v1
种类:角色
元数据:
名称:rbac测试
规则:
-apiGroups:[“”]
资源:[“配置映射”]
动词:[“获取”、“创建”、“更新”]
资源名称:[“aaa”]
---
apiVersion:rbac.authorization.k8s.io/v1
种类:RoleBinding
元数据:
名称:rbac测试
roleRef:
apiGroup:rbac.authorization.k8s.io
种类:角色
名称:rbac测试
学科:
-种类:服务帐户
名称:rbac测试

注意:不能按resourceName限制创建或删除收集请求。对于create,此限制是因为在授权时对象名称未知


参考资料:

正如Shai Katz在之前的回答中提到的,根据文件:

您不能通过
resourceName
限制
create
deletecollection
请求。对于create,此限制是因为在授权时对象名称未知

“编辑”部分中提供的解决方案-仍然不起作用

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: rbac-test
rules:
  - apiGroups: [ "" ]
    resources: [ "configmaps" ]
    verbs: [ "get", "create", "update" ]
    resourceNames: [ "aaa" ]
此配置仅允许您获取和更新名为
aaa
的现有configmap,而创建操作毫无意义,因为无法创建此资源

如何限制serviceAccount的角色,以便此pod只能操作命名的configmap 如果您要编辑您的

解决方案1

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: rbac-test
rules:
  - apiGroups: [ "" ]             #this rule will allow to update existing configmap aaa referenced by resourcename 
    resources: [ "configmaps" ]
    verbs: [ "get", "update" ]    
    resourceNames: [ "aaa" ]
  - apiGroups: [ "" ]             #this rule will allow you to create configmaps
    resources: [ "configmaps" ]
    verbs: [ "get", "create" ]   
可通过执行以下命令进行验证:

$ kubectl auth can-i update configmaps/aaa --as=system:serviceaccount:default:rbac-test
yes
$ kubectl auth can-i create configmaps/new-aaa --as=system:serviceaccount:default:rbac-test
yes
但是,在此场景中,
rbac测试pod/sa
可以通过执行
kubectl replace
命令f.e执行
update
操作:

$kubectl replace-f/config/aaa.yaml

kind: ConfigMap
apiVersion: v1
metadata:
  name: aaa
data:
  value: na
如果您对
$kubectl apply
感兴趣,您可能应该使用动词
patch
,如下例所示

  - apiGroups: [ "" ]       
    resources: [ "configmaps" ]
    verbs: [ "get", "patch" ]    
    resourceNames: [ "aaa" ]
解决方案2-更具限制性的方法

而不是授予您的
serviceCount:rbac test
在k8s api中创建配置的权限。请考虑创建两个配置图:

(a)

(b)

配置此RBAC角色:

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: rbac-test
rules:
  - apiGroups: [ "" ]             #this rule will allow to patch existing empty "aaa" configmap aaa referenced by resourcename 
    resources: [ "configmaps" ]
    verbs: [ "get", "patch" ]    
    resourceNames: [ "aaa" ]
在此场景中,您将预先配置
aaa
configmap
,并且您的
pod
rbac测试服务帐户相关联的
rbac测试服务帐户将能够使用存储在
/config/aaa.YAML
中提供的YAML清单执行
补丁
请求

$ kubectl auth can-i patch configmaps/aaa --as=system:serviceaccount:default:rbac-test
yes
$ kubectl auth can-i create configmaps/new-aaa --as=system:serviceaccount:default:rbac-test
no
要检查可在
RBAC
中为
ConfigMap
使用哪些动词,请执行:

$ kubectl api-resources -o wide | grep configmap
configmaps                        cm                                          true         ConfigMap                        [create delete deletecollection get list patch update watch]
$ kubectl auth can-i patch configmaps/aaa --as=system:serviceaccount:default:rbac-test
yes
$ kubectl auth can-i create configmaps/new-aaa --as=system:serviceaccount:default:rbac-test
no
$ kubectl api-resources -o wide | grep configmap
configmaps                        cm                                          true         ConfigMap                        [create delete deletecollection get list patch update watch]