gpt4 book ai didi

python - 为 GKE 身份验证生成的 token 缺少权限

转载 作者:太空宇宙 更新时间:2023-11-03 15:35:46 25 4
gpt4 key购买 nike

我尝试在 Google Kubernetes (GKE) 中以自动化方式创建角色。

为此,我使用 python 客户端库,但我不想对 kubectl 和 kubeconfig 或 gcloud 有任何依赖,

我使用一个服务帐户(带有来自 GCP 的 json key 文件),它有权在命名空间中创建角色(它是一个集群管理员)。当我使用此命令提供的访问 token 时:

gcloud auth activate-service-account --key-file=credentials.json 
gcloud auth print-access-token

有效。

但是当我尝试自己生成 token 时,我可以创建命名空间和其他标准资源,但是当涉及到角色时我有这个错误:

E           kubernetes.client.rest.ApiException: (403)
E Reason: Forbidden
E HTTP response headers: HTTPHeaderDict({'Audit-Id': 'b89b0fc2-9350-456e-9eca-730e7ad2cea1', 'Content-Type': 'application/json', 'Date': 'Tue, 26 Feb 2019 20:35:20 GMT', 'Content-Length': '1346'})
E HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"roles.rbac.authorization.k8s.io \"developers\" is forbidden: attempt to grant extra privileges: [{[*] [apps] [statefulsets] [] []} {[*] [apps] [deployments] [] []} {[*] [autoscaling] [horizontalpodautoscalers] [] []} {[*] [] [pods] [] []} {[*] [] [pods/log] [] []} {[*] [] [pods/portforward] [] []} {[*] [] [serviceaccounts] [] []} {[*] [] [containers] [] []} {[*] [] [services] [] []} {[*] [] [secrets] [] []} {[*] [] [configmaps] [] []} {[*] [extensions] [ingressroutes] [] []} {[*] [networking.istio.io] [virtualservices] [] []}] user=\u0026{100701357824788592239 [system:authenticated] map[user-assertion.cloud.google.com:[AKUJVp+KNvF6jw9II+AjCdqjbC0vz[...]hzgs0JWXOyk7oxWHkaXQ==]]} 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":"developers","group":"rbac.authorization.k8s.io","kind":"roles"},"code":403}

我使用的是相同的服务帐户,所以我猜 gcloud 做的不仅仅是我的脚本。

这里是我用来生成 token 的 python 代码:

def _get_token(self) -> str:
# See documentation here
# https://developers.google.com/identity/protocols/OAuth2ServiceAccount
epoch_time = int(time.time())
# Generate a claim from the service account file.
claim = {
"iss": self._service_account_key["client_email"],
"scope": "https://www.googleapis.com/auth/cloud-platform",
"aud": "https://www.googleapis.com/oauth2/v4/token",
"exp": epoch_time + 3600,
"iat": epoch_time
}
# Sign claim with JWT.
assertion = jwt.encode(
claim,
self._service_account_key["private_key"],
algorithm='RS256'
).decode()
# Create payload for API.
data = urlencode({
"grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer",
"assertion": assertion
})
# Request the access token.
result = requests.post(
url="https://www.googleapis.com/oauth2/v4/token",
headers={
"Content-Type": "application/x-www-form-urlencoded"
},
data=data
)
result.raise_for_status()
return json.loads(result.text)["access_token"]

def _get_api_client(self) -> client.ApiClient:
configuration = client.Configuration()
configuration.host = self._api_url
configuration.verify_ssl = self._tls_verify
configuration.api_key = {
"authorization": f"Bearer {self._get_token()}"
}
return client.ApiClient(configuration)

以及创建角色的函数(生成 403 错误):

def _create_role(self, namespace: str, body: str):
api_client = self._get_api_client()
rbac = client.RbacAuthorizationV1Api(api_client)
rbac.create_namespaced_role(
namespace,
body
)

如果我使用从 gcloud 中提取的 token 将 _get_token 方法短路,它就可以工作。

我想这与我创建 token 的方式有关(缺少范围?),但我没有找到任何相关文档。

答案:

添加范围就可以了!非常感谢:

# Generate a claim from the service account file.
claim = {
"iss": self._service_account_key["client_email"],
"scope": " ".join([
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/userinfo.email"
]),
"aud": "https://www.googleapis.com/oauth2/v4/token",
"exp": epoch_time + 3600,
"iat": epoch_time
}

最佳答案

因此,如果您查看代码 here对于 print-access-token,您可以看到访问 token 通常是在没有范围的情况下打印的。你看:

try:
creds = client.GoogleCredentials.get_application_default()
except client.ApplicationDefaultCredentialsError as e:
log.debug(e, exc_info=True)
raise c_exc.ToolException(str(e))

if creds.create_scoped_required():
...

然后在这个file你看:

def create_scoped_required(self):
"""Whether this Credentials object is scopeless.
create_scoped(scopes) method needs to be called in order to create
a Credentials object for API calls.
"""
return False

显然,在您的代码中,您正在获取具有 https://www.googleapis.com/auth/cloud-platform 范围的 token 。您可以尝试删除它或尝试使用 USER_EMAIL_SCOPE因为您指定:"iss": self._service_account_key["client_email"]

您始终可以检查 gcloud auth activate-service-account --key-file=credentials.json 存储在 ~/.config 下的内容。所以你知道 gcloud auth print-access-token 使用什么。请注意,根据 thisthis看起来商店在 sqlite格式。

关于python - 为 GKE 身份验证生成的 token 缺少权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54893852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com