【问题标题】:K8s Forbidden Attempt to Grant Extra PrivilegesK8s 禁止尝试授予额外权限
【发布时间】:2019-06-22 05:21:02
【问题描述】:

无法使用 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。将用户设为 admin

使用 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 成功创建了相同的集群角色。

【问题讨论】:

    标签: rest kubernetes rbac


    【解决方案1】:

    根据Google cloud RBAC documentation

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

    用户或 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 角色和集群角色,请发出以下命令,将 [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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-19
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      • 2015-01-23
      • 1970-01-01
      相关资源
      最近更新 更多