amazon-web-services - Kubernetes 预计服务帐户 token 到期时间问题

标签 amazon-web-services kubernetes openid-connect amazon-eks

我正在使用启用了服务账户发现的 AWS EKS 1.21。
创建了一个 OIDC 提供者,.well-known/openid-configuration 端点返回正确的配置:

{
  "issuer": "https://oidc.eks.eu-west-1.amazonaws.com/id/***",
  "jwks_uri": "https://ip-***.eu-west-1.compute.internal:443/openid/v1/jwks",
  "response_types_supported": [
    "id_token"
  ],
  "subject_types_supported": [
    "public"
  ],
  "id_token_signing_alg_values_supported": [
    "RS256"
  ]
}

为我的一个部署创建了一个 ServiceAccount,Pod 将其作为预计量获取:

  volumes:
  - name: kube-api-access-b4xt9
    projected:
      defaultMode: 420
      sources:
      - serviceAccountToken:
          expirationSeconds: 3607
          path: token
      - configMap:
          items:
          - key: ca.crt
            path: ca.crt
          name: kube-root-ca.crt
      - downwardAPI:
          items:
          - fieldRef:
              apiVersion: v1
              fieldPath: metadata.namespace
            path: namespace

为 ServiceAccount 创建的 secret 包含此 token :

{
  "iss": "kubernetes/serviceaccount",
  "kubernetes.io/serviceaccount/namespace": "sbx",
  "kubernetes.io/serviceaccount/secret.name": "dliver-site-config-service-token-kz874",
  "kubernetes.io/serviceaccount/service-account.name": "dliver-site-config-service",
  "kubernetes.io/serviceaccount/service-account.uid": "c26ad760-9067-4d90-a327-b3d6e32bce42",
  "sub": "system:serviceaccount:sbx:dliver-site-config-service"
}

安装到 pod 中的投影 token 包含以下内容:

{
  "aud": [
    "https://kubernetes.default.svc"
  ],
  "exp": 1664448004,
  "iat": 1632912004,
  "iss": "https://oidc.eks.eu-west-1.amazonaws.com/id/***",
  "kubernetes.io": {
    "namespace": "sbx",
    "pod": {
      "name": "dliver-site-config-service-77494b8fdd-45pxw",
      "uid": "0dd440a6-1213-4faa-a69e-398b83d2dd6b"
    },
    "serviceaccount": {
      "name": "dliver-site-config-service",
      "uid": "c26ad760-9067-4d90-a327-b3d6e32bce42"
    },
    "warnafter": 1632915611
  },
  "nbf": 1632912004,
  "sub": "system:serviceaccount:sbx:dliver-site-config-service"
}

Kubernetes 每小时更新一次投影 token ,所以一切看起来都很好。
除了投影 token “exp”字段:
"iat": 16329120042021 年 9 月 29 日星期三上午 10:40:04
"exp": 16644480042022 年 9 月 29 日星期四上午 10:40:04

所以问题是,预计的 token 到期时间是 1 年,而不是大约 1 小时,这使得 Kubernetes 更新 token 的努力基本上没有用。
我搜索了几个小时,但根本无法弄清楚这是从哪里来的。
过期标志传递给 kube-api 服务器:--service-account-max-token-expiration="24h0m0s",所以 我的假设是,这应该以某种方式在 OIDC 提供程序上进行配置,但无法找到任何相关文档。

知道如何使预计 token 到期日期与 pod 预计数量中的 expirationSeconds 大致相同吗?

更新

它只会发生,当预计 token expirationSeconds 设置为默认 3607 值时,任何其他值都会给出正确的 exp mounted token,这真的很奇怪。

最佳答案

终于有了答案elsewhere .

cluster operators can specify flag --service-account-extend-token-expiration=true to kube apiserver to allow tokens have longer expiration temporarily during the migration. Any usage of legacy token will be recorded in both metrics and audit logs.

“3607”魔数(Magic Number)是绑定(bind)服务帐户 token 安全推出计划的一部分,described in this kep . 实际人数hardcoded in the source code .
--service-account-extend-token-expiration 标志是 set to true默认从 1.20 开始。

可以找到提到的指标/日志信息 in the kep too并实现了here .
要在 EKS 中查看这些日志,需要 enable audit logging on the cluster ,然后检查 Cloudwatch 以获取相关日志条目。

我在 Cloudwatch Log Insight 中使用此查询来查找哪些 pod 不会定期重新加载 token :

filter @logStream like 'kube-apiserver-audit'
 | filter ispresent(`annotations.authentication.k8s.io/stale-token`)
 | parse `annotations.authentication.k8s.io/stale-token` "subject: *," as subject
 | stats count(*) as staleCount by subject, `user.username`
 | sort staleCount desc

关于amazon-web-services - Kubernetes 预计服务帐户 token 到期时间问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69375195/

相关文章:

kubernetes - 如果POD具有唯一的IP地址,那么POD中每个容器的IP地址是什么?

asp.net-core - asp.net core 中关联失败

mysql - 如何将Windows 2008 R2上的Mysql数据库备份到Amazon S3?

ruby - 如何同时使用 scala 和 rails 中的 memcached(Amazon elasticache)?

amazon-web-services - 由于不可用,无法使用 Amazon EC2 实例安装 Sndfile

kubernetes - 大使可以处理 CORS 请求吗?

apache - Apache Hive在亚马逊上如何工作?

nginx - Kubernetes/Minikube Ingress 错误(301 永久移动)

go - k8s oidc 问题与帮助程序包 (k8s-oidc-helper : command not found)

java - Keycloak公共(public)客户端和授权