简体   繁体   English

代理K8S应用程序委托来自其他pod的请求的身份验证

[英]Proxy K8S app delegating authentication of requests from other pods


Background 背景

I have a K8S cluster with a number of different pods that have their own specific service accounts, cluster roles, and cluster role bindings, so that they can execute various read/write requests directly with the K8S REST API. 我有一个K8S群集,其中包含许多不同的pod,这些pod具有自己的特定服务帐户,群集角色和群集角色绑定,因此他们可以直接使用K8S REST API执行各种读/写请求。 There are some complicated requests that can be issued, and I'd like to make a function to wrap the complex logic. 有一些复杂的请求可以发出,我想创建一个函数来包装复杂的逻辑。 However, the various services in the cluster are written in multiple (ie 6+) programming languages, and there does not (yet) seem to be a trivial way to allow all these services to directly re-use this code. 但是,集群中的各种服务都是用多种(即6+)编程语言编写的,并且(似乎)还没有一种简单的方法可以让所有这些服务直接重用这些代码。

I'm considering creating a "proxy" micro-service, that exposes its own REST API, and issues the necessary requests and handles the "complex logic" on behalf of the client. 我正在考虑创建一个“代理”微服务,它公开自己的REST API,并发出必要的请求并代表客户端处理“复杂的逻辑”。


Problem 问题

The only problem is that, with the current deployment model, a client could request that the proxy micro-service execute an HTTP request that the client itself isn't authorized to make. 唯一的问题是,使用当前的部署模型,客户端可以请求代理微服务执行客户端本身无权进行的HTTP请求。


Question

Is there a trivial/straightforward way for one pod, for example, to identify the client pod, and execute some kind of query/result-of-policy operation (ie by delegating the authentication to the K8S cluster authentication mechanism itself) to determine if it should honor the request from the client pod? 例如,对于一个pod是否存在一种简单/直接的方式来识别客户端pod,并执行某种查询/策略结果操作(即通过将身份验证委派给K8S集群身份验证机制本身)来确定是否它应该尊重来自客户端pod的请求?


Kubernetes Authentication model represents a way how the particular user or service account can be entitled in k8s cluster, however Authorization methods determine whether initial request from the cluster visitor, aimed to do some action on cluster resources/objects, has sufficient permissions to make that possible. Kubernetes 身份验证模型表示特定用户或服务帐户在k8s群集中的授权方式,但授权方法确定来自群集访问者的初始请求(旨在对群集资源/对象执行某些操作)是否具有足够的权限以使其成为可能。

Due to the fact that you've used specific service accounts per each Pod entire the cluster and granting them specific RBAC rules, it might be possible to use SelfSubjectAccessReview API in order to inspect requests to k8s REST API and determine whether the client's Pod service account has appropriate permission to perform any action on target's Pod namespace. 由于您已经在整个群集中使用每个Pod的特定服务帐户并授予它们特定的RBAC规则,因此可以使用SelfSubjectAccessReview API来检查对k8s REST API的请求并确定客户端的Pod服务帐户具有对目标的Pod命名空间执行任何操作的适当权限。

That can be achievable using kubectl auth can-i subcommand by submitting essential information for user impersonation. 通过提交用户模仿的基本信息,可以使用kubectl auth can-i 子命令实现这kubectl auth can-i

I assume that you might also be able to query k8s authorization API group within HTTP request schema and then parse structured data from JSON/YAML format, like in the example below: 我假设您也可以在HTTP请求模式中查询k8s授权API组,然后从JSON / YAML格式解析结构化数据,如下例所示:

Regular kubectl auth can-i command to check whether default SA can retrieve data about Pods in default namespace: 定期kubectl auth can-i命令检查default SA是否可以在default命名空间中检索有关kubectl auth can-i数据:

kubectl auth can-i get pod --as system:serviceaccount:default:default

Equivalent method via HTTP call to k8s REST API using JSON type of content within Bearer Token authentication: 使用Bearer Token身份验证中使用JSON类型内容通过HTTP调用k8s REST API的等效方法:

curl -k \
    -X POST \
    -d @- \
    -H "Authorization: Bearer $MY_TOKEN" \
    -H 'Accept: application/json' \
    -H "Impersonate-User: system:serviceaccount:default:default" \
    -H 'Content-Type: application/json' \
    https://<API-Server>/apis/authorization.k8s.io/v1/selfsubjectaccessreviews <<'EOF'
{
  "kind": "SelfSubjectAccessReview",
  "apiVersion": "authorization.k8s.io/v1",
  "spec":{"resourceAttributes":{"namespace":"default","verb":"get","resource":"pods"}}
}
EOF

Output: 输出:

.... "status": { "allowed": true, "reason": "RBAC: allowed by RoleBinding .... ....“status”:{“allowed”:true,“reason”:“RBAC:RoleBinding允许....

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

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