[英]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.