K8禁止尝试授予额外特权

时间:2019-01-29 06:23:29

标签: rest kubernetes rbac

无法使用K8s REST API创建ClusterRole。我收到“禁止:试图授予额外的特权”错误。即使可以使用'kubectl apply'创建相同的ClusterRole。使用同一用户。在GCP中运行。版本:“ 1.11.6-gke.3”。

这是我的步骤:

1。 IAM配置

IAM用户:berlioz-robot@xxx.iam.gserviceaccount.com 角色:Kubernetes Engine管理员

2。使用户成为管理员

使用kubectl进行应用:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: berlioz:robot-cluster-admin-binding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: User
  name: berlioz-robot@xxx.iam.gserviceaccount.com

3。生成登录令牌

标题:

{
  "alg": "RS256",
  "typ": "JWT",
  "kid": "xxxxxxxxxxxxxxxxxxxxxxxxxx"
}

有效载荷:

{
  "iss": "berlioz-robot@xxx.iam.gserviceaccount.com",
  "sub": "berlioz-robot@xxx.iam.gserviceaccount.com",
  "aud": "https://www.googleapis.com/oauth2/v4/token",
  "scope": "https://www.googleapis.com/auth/cloud-platform",
  "iat": 1548743213,
  "exp": 1548746813
}

4。登录

URL: https://www.googleapis.com/oauth2/v4/token
Method: POST
Body: {
    'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
    'assertion': here-goes-the-signed-token
}

结果:

{
    "access_token": "ya29.xxxxxxxxxxxxxxxx",
    "expires_in": 3600,
    "token_type": "Bearer"
}

5。使用REST API创建新的ClusterRole

URL: https://CLUSTER-IP-ADDRESS/apis/rbac.authorization.k8s.io/v1/clusterroles
Method: POST
Headers: {
    Authorization: "Bearer ya29.xxxxxxxxxxxxxxxx",
    Content-Type: "application/json"
}
Body: {
    "metadata": {
      "name": "berlioz:controller-cluster-role"
    },
    "rules": [
      {
        "verbs": [
          "get",
          "list",
          "watch"
        ],
        "apiGroups": [
          ""
        ],
        "resources": [
          "nodes"
        ]
      }
    ],
    "kind": "ClusterRole",
    "apiVersion": "rbac.authorization.k8s.io/v1"
}

结果:

{
    "kind": "Status",
    "apiVersion": "v1",
    "metadata": {},
    "status": "Failure",
    "message": "clusterroles.rbac.authorization.k8s.io \"berlioz:controller-cluster-role-test\" is forbidden: attempt to grant extra privileges: [{[get] [] [nodes] [] []} {[list] [] [nodes] [] []} {[watch] [] [nodes] [] []}] user=&{110887992956644566571  [system:authenticated] map[user-assertion.cloud.google.com:[xxxxx]]} ownerrules=[{[create] [authorization.k8s.io] [selfsubjectaccessreviews selfsubjectrulesreviews] [] []} {[get] [] [] [] [/api /api/* /apis /apis/* /healthz /openapi /openapi/* /swagger-2.0.0.pb-v1 /swagger.json /swaggerapi /swaggerapi/* /version /version/]}] ruleResolutionErrors=[]",
    "reason": "Forbidden",
    "details": {
        "name": "berlioz:controller-cluster-role-test",
        "group": "rbac.authorization.k8s.io",
        "kind": "clusterroles"
    },
    "code": 403
}

有趣的是,如果我将规则列表设置为空,那么一切都会进行。如上所述,使用kubectl成功创建了相同的群集角色。

1 个答案:

答案 0 :(得分:0)

根据Google cloud RBAC documentation

  

在GKE中,将Cloud IAM和Kubernetes RBAC集成在一起,以授权用户在根据任一工具拥有足够权限的情况下执行操作。这是引导GKE集群的重要部分,因为默认情况下,GCP用户没有任何Kubernetes RBAC角色绑定

     

对用户或GCP服务帐户进行身份验证后,还必须授权它们对GKE群集执行任何操作。

     

在使用GKE v1.11.x和更早版本的GKE群集中,存在一个限制,即Cloud IAM无法授予创建Kubernetes RBAC Role or ClusterRole的能力。但是,Kubernetes Engine Admin Cloud IAM role确实授予用户为任何用户(包括他们自己)创建Kubernetes RBAC RoleBinding or ClusterRoleBinding的能力,可以用来将GCP用户绑定到predefined RBAC Roles

     

特别是,cluster-admin预定义的RBAC角色向用户授予了群集中的全部权限。因此,要引导用户允许他们创建RBAC角色和ClusterRoles,请发出以下命令,将[USER_ACCOUNT]替换为目标用户的GCP登录电子邮件地址。

kubectl create clusterrolebinding cluster-admin-binding \
  --clusterrole cluster-admin \
  --user [USER_ACCOUNT]
  

注意:[USER_ACCOUNT]区分大小写。为避免错误,请以小写形式输入目标用户的电子邮件地址。

或者,您可以使用以下yaml:

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: cluster-admin-binding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: User
  name: username@google-account-domain.com

创建了此类ClusterRoleBinding之后,您将能够创建ClusterRole。