如何使用 oidc 和 azure 应用程序配置 kubernetes 以仅允许使用指定的安全组进行身份验证

How do I configure kubernetes with oidc and azure apps to allow authentication only with a specified Security Group

我正在尝试在我们的(常规而非 AKS)kubernetes 集群和 Azure AD 之间设置 SSO。 由于我不知道如何将令牌转发到 kube-dashboard,我目前正在尝试在我的计算机上安装 kubectl 二进制文件。 它在不涉及任何组时有效,但我们希望按安全组过滤(AAD 上的帐户从我们的本地 Active Directory 同步),不涉及 kube RBAC。

设置灵感来自 https://medium.com/@olemarkus/using-azure-ad-to-authenticate-to-kubernetes-eb143d3cce10 and https://docs.microsoft.com/fr-fr/azure/aks/azure-ad-integration :

- --oidc-client-id=spn:abc123

- --oidc-issuer-url=https://sts.windows.net/OurAADTenantID

kubectl config set-cluster test-legacy-2 --server=https://192.168.x.y:4443 --certificate-authority=/somelocation/ca.pem
kubectl config set-credentials USER@mydomain.com --auth-provider=azure --auth-provider-arg=environment=AzurePublicCloud --auth-provider-arg=client-id=xyz456 --auth-provider-arg=tenant-id=OurAADTenantID --auth-provider-arg=apiserver-id=abc123

同样在 Azure 客户端应用程序清单中,必须指定:

"allowPublicClient":true,

"oauth2AllowIdTokenImplicitFlow":true

否则会出现错误“无法获取令牌:获取新的新令牌:等待设备代码身份验证完成:autorest/adal/devicetoken:检索 OAuth 令牌时出错:未知错误”。 =76=]。 发现于 https://github.com/MicrosoftDocs/azure-docs/issues/10326

根据 https://kubernetes.io/docs/reference/access-authn-authz/authentication/#openid-connect-tokens

尝试过滤我在 JWT 中找到的某些安全组时出现问题

即使 Azure 发送给我的 JWT 确实包含格式正确的组(json 字符串数组)

,我仍收到格式错误

配置:

  • 在 azure web 应用程序清单中将这些组包含在我的 JWT 中:

"groupMembershipClaims": "SecurityGroup",

  • kube api 服务器 yaml 清单:

- --oidc-groups-claim=groups

- --oidc-required-claim=groups=bbc2eedf-79cd-4505-9fb4-39856ed3790e

这里的字符串是我的目标安全组的 GUID。

我在 kubectl 的输出上收到 error: You must be logged in to the server (Unauthorized),kube api 服务器日志为我提供了这个 authentication.go:62] Unable to authenticate the request due to an error: [invalid bearer token, [invalid bearer token, oidc: parse claim groups: json: cannot unmarshal array into Go value of type string]]

但我不明白为什么当我解码我拥有的 JWT 时它不开心

"groups": [
    "00530f35-0013-4237-8947-6e3f6a7895ca",
    "bbc2eedf-79cd-4505-9fb4-39856ed3790e",
    "17dff614-fd68-4a38-906c-69561daec8b7"
  ],

据我所知,这是一个格式良好的 json 字符串数组...

为什么 api 服务器抱怨 JWT?

Ok so, Required claims must be a string, not an array of strings

但我找到了解决方法。

  • 不要使用 oidc-groups-claim 和 oidc-required-claim
  • 在 Azure 中,转到 API 服务器应用程序的属性。
  • Select 是 "User assignment required"
  • 在 "Users and groups" 中添加您要过滤的特定安全组
  • 测试:将您自己从安全组中移除
  • 等待令牌过期(在我的例子中是 1 小时)
  • 您不能再登录了