gcloud auth打印访问令牌出现拒绝权限错误



我有一个shell脚本,试图从dataproc集群内的秘密管理器下载一些秘密。

GetAuthJson() {
authjson=$(curl "https://secretmanager.googleapis.com/v1/projects/$PROJECT_ID/secrets/$AUTH_JSON/versions/1:access" 
--request "GET" 
--header "authorization: Bearer $(gcloud auth print-access-token)" 
--header "content-type: application/json")
if [ $? -ne 0 ]; then
Error "Unable to extract the $PIPENAME Auth json details from GCP Secret Manager"
fi
echo $authjson | grep -o '"data": "[^"]*' | grep -o '[^"]*$' >$BASE_DIR/encodedauth.json
if [ $? -ne 0 ]; then
Error "Unable to save the $PIPENAME auth.json server secret to auth.json"
fi
auth_json=$(base64 -d $BASE_DIR/encodedauth.json)
base64 -d $BASE_DIR/encodedauth.json >/etc/secrets/auth.json
if [ $? -ne 0 ]; then
Error "Unable to decode the $PIPENAME auth.json server secret"
fi
Log "auth.json secret extraction done"
}

当我运行这个curl时,它会生成一个错误

authjson='{
"error": {
"code": 403,
"message": "Permission '''secretmanager.versions.access''' denied for resource '''projects/**-**-dev/secrets/**_AUTH_JSON/versions/1''' (or it may not exist).",
"status": "PERMISSION_DENIED"
}
}'

具有相同服务帐户的相同curl在本地meachine工作。更重要的是,如果我从本地复制CURL并在dataproc集群中运行它,它也能工作。但是从dataproc生成的curl在本地失败。

更奇怪的是,如果我单独运行gcloud-auth-print-access令牌,并将其粘贴到curl命令中,它在两台机器中都能工作。

所以我的问题是,为什么在dataproc集群中作为curl的一部分生成的gcloudauth-print-access令牌不起作用?

如果您可以捕获curl命令的值,或者至少捕获脚本中失败的gcloud auth print-access-token的值,这将非常有用。

我怀疑(我不熟悉Dataproc)Dataproc实例没有安装gcloud,而gcloud auth print-access-token正在发生故障。

如果实例确实安装了gcloud,由于它正在运行,那么它必须有一个服务帐户,因此应该允许进行身份验证。获取访问令牌作为Dataproc实例可能存在(!?)更细微的问题,尚不清楚。

请考虑直接使用gcloud secrets versions access或谷歌的一个客户端库来访问机密。

通过curl设置端点,您使过程变得比需要的更复杂;无论如何,您都必须使用gcloud才能获得auth令牌。

问题是我以sudo用户的身份运行了脚本。当我正常跑步时,它起作用了。