如何增加 gcloud 重新身份验证超时,目前每 1 小时过期

My *_*URU 2 authentication google-cloud-platform gcloud google-kubernetes-engine kubectl

我运行以下命令

  • 使用我的公司电子邮件 ID (ldap) 向 google cloud 进行身份验证
  • 更新我的本地计算机上的 kubeconfig 文件
  • 使用 kube-api-proxy 从本地计算机访问 k8s 控制平面。(我使用此代理来访问控制平面,因为 GKE 控制平面 vpc 和我的公司网络之间没有 VPC 对等)
gcloud auth login --no-launch-browser  ## I use corporate email id to authenticate
gcloud container clusters get-credentials <>gke_cluster_name> --region <region> --project <gcp_project>
export https_proxy=<kube_api_proxy>:8118  ## Proxy to connect to k8s controlplane
kubectl get no
Run Code Online (Sandbox Code Playgroud)

每隔 1 小时,我必须重复上述步骤来重新进行身份验证,因为我会失败并出现以下错误,否则当我尝试连接到 k8S 时

Unable to connect to the server: error executing access token command "/usr/lib64/google-cloud-sdk/bin/gcloud 
config config-helper --format=json": err=exit status 1 output= stderr=ERROR: gcloud crashed (TransportError):
HTTPSConnectionPool(host='oauth2.googleapis.com', port=443): Max retries exceeded with url: /token 
(Caused by ProxyError('Cannot connect to proxy.', 
OSError('Tunnel connection failed: 403 Request blocked by Privoxy')))
Run Code Online (Sandbox Code Playgroud)

有没有办法可以增加这个超时时间,比如说 4 小时左右,因为我有一个运行时间超过 1 小时的作业,并且由于超时而在中间失败。

Joh*_*ley 5

CLI gcloud创建有效期为 3,600 秒的 OAuth 访问令牌。这是非组织项目支持的最大生命周期。这也是您正在使用的用户身份的最长生命周期。

要延长组织的令牌生命周期,您必须从服务帐户创建凭据并设置组织策略约束constraints/iam.allowServiceAccountCredentialLifetimeExtension,该扩展支持生命周期为 12 小时的令牌。关联

但是,我不知道在 CLI 中使用该约束而不修改 CLI 源代码的方法,它是用 Python 编写的。我从未进行过此更改,因为编写自己的代码要容易得多。

相反,编写您自己的令牌生成器。互联网上有很多源代码示例。我写了一篇文章,其中包含源代码链接。将我的代码中的这一行更改为所需的时间:

# Set how long this token will be valid in seconds
expires_in = 3600   # Expires in 1 hour
Run Code Online (Sandbox Code Playgroud)

总之:

  1. 您必须是 Google Cloud 组织的一部分。
  2. 您必须从服务帐户创建凭据。
  3. 您必须设置组织策略约束。
  4. 该约束必须包含允许的服务帐户的电子邮件地址。