简体   繁体   English

GCP服务帐户密钥轮换

[英]GCP Service Account Key Rotation

I am trying to implement Key Rotation for GCP Service Accounts. 我正在尝试为GCP服务帐户实施密钥轮换。 I have managed to create a new key and then decode the privateKeyData which is base64 encoded, which has the actual SA JSON file. 我设法创建了一个新密钥,然后对base64编码的privateKeyData进行了解码,它具有实际的SA JSON文件。 Now when I am reading the file back to authenticate, it is giving me this error: 现在,当我读回文件进行身份验证时,它给了我这个错误:

'unicode object has no iterKeys()' 'unicode对象没有iterKeys()'

Issue is with json.dumps I think. 问题是我认为是json.dumps

data = base64.b64decode(key['privateKeyData']).decode('utf-8')
print data  # this prints expected output


with open('file.json', mode='w') as out:
    str = json.dumps(data)
    print out  # this adds \n,\\ to the output
    out.write(str)

Error: 错误:

AttributeError: 'unicode' object has no attribute 'iterkeys'

Dummy Snippet of how the file is being converted after json.dumps : 关于如何在json.dumps之后转换文件的虚拟片段:

"{\n  \"type\": \"service_account\",\n  \"project_id\": \"testproj\",\n  \"private_key_id\": \6866996939\"}"\n

The json.dumps() function is usually used to turn a dict into a string representing JSON: json.dumps()函数通常用于将dict转换为表示JSON的字符串:

>>> json.dumps({"foo": "bar"})
'{"foo": "bar"}'

But you're giving it a string instead, which is causing it to escape the quotes: 但是,您给它提供了一个字符串,这导致它转义了引号:

>>> json.dumps('{"foo": "bar"}')
'"{\\"foo\\": \\"bar\\"}"'

You should just write data to the file instead: 您应该改为将data写入文件:

with open('file.json', mode='w') as out:
    out.write(data)

It seems you might have a second problem that is causing the exception, you should include the full traceback in your answer, instead of just the last line. 看来您可能还有第二个问题导致了异常,您应该在答案中包括完整的追溯,而不是仅最后一行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 gcp - 如何在没有密钥文件的情况下将 Python 应用程序作为服务帐户运行 - gcp - how to run Python application as Service Account without a key file 有没有办法在 GCP 中禁用服务帐户密钥 8 小时或 7 天,然后使用 python 执行删除操作? - Is there a way to disable service account key in GCP for 8hrs or 7 days and then do a delete operation using python? 使用 Bitbucket 管道对 GCP 服务帐户进行身份验证 - Authenticating GCP service account with Bitbucket Pipelines 如何将GCP数据流作为服务帐户运行 - How to run GCP Dataflow as a Service Account GCP:允许服务帐户模拟具有 Google Analytics 范围的用户帐户 - GCP: Allow Service Account to Impersonate a User Account with Google Analytics Scopes GCP 使用 gcloud auth(不是服务帐户)以最终用户身份进行身份验证 - GCP Authenticate as End User using gcloud auth (not service account) GCP - storage.Client.from_service_account_json - GCP - storage.Client.from_service_account_json 使用 SQL alchemy 和 GCP 服务帐户连接到 GCP Cloud SQL Auth Proxy - Connect to a GCP Cloud SQL Auth Proxy using SQL alchemy and a GCP service account 无法使用python脚本和GCP服务帐户信用将upload_from_filename文件上传到GCP存储桶 - Not able to upload_from_filename files into GCP bucket using python script and GCP service account cred 在 Cloud Functions 中获取服务帐户凭据以在 GCP 之外调用 google 服务 - Obtain a service account credentials within a Cloud Functions to call a google service outside GCP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM