首页 > 解决方案 > (gcloud.kms.encrypt) 无法读取明文文件

问题描述

我想用gcloud命令行工具加密一个密钥文件。

我正在运行的命令是:

gcloud kms encrypt --project=pname --location=global --keyring=keyring \
--key=key-credential \
--plaintext-file=/Users/macuser/Desktop/test/keys/testkey.json.decrypted \
--ciphertext-file=testkey.json.encrypted

但我不断收到错误

ERROR: (gcloud.kms.encrypt) Failed to read plaintext file 
[/Users/macuser/Desktop/test/keys/testkey.json.decrypted]: 
Unable to read file [/Users/macuser/Desktop/test/keys/testkey.json.decrypted]: 
[Errno 2] No such file or directory: 
'/Users/macuser/Desktop/test/keys/testkey.json.decrypted'

文件

/Users/macuser/Desktop/test/keys/testkey.json.decrypted

存在。我尝试使用绝对路径和相对路径,以及带和不带引号,但我一直收到同样的错误。

为什么 gcloud 看不到文件?

标签: google-cloud-platformsshgoogle-cloud-buildgoogle-cloud-kms

解决方案


  1. 确保调用加密和解密方法的用户对用于加密或解密的密钥具有cloudkms.cryptoKeyVersions.useToEncrypt和权限。cloudkms.cryptoKeyVersions.useToDecrypt

允许用户加密或解密的一种方法是将用户添加到该密钥的roles/cloudkms.cryptoKeyEncrypterroles/cloudkms.cryptoKeyDecrypterroles/cloudkms.cryptoKeyEncrypterDecrypterIAM 角色。

  1. 要在命令行上使用 Cloud KMS,请先安装或升级到最新版本的 Cloud SDK。
gcloud kms encrypt \
    --key key \
    --keyring key-ring \
    --location location  \
    --plaintext-file file-with-data-to-encrypt \
    --ciphertext-file file-to-store-encrypted-data
  • 将 key 替换为用于加密的密钥的名称。
  • 将 key-ring 替换为密钥所在的密钥环的名称。
  • 将 location 替换为 Cloud KMS 位置作为密钥环。
  • file-with-data-to-encrypt和替换file-to-store-encrypted-data为用于读取明文数据并保存加密输出的本地文件路径。

推荐阅读