(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 看不到文件

解决方法

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

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

  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-ring 替换为密钥所在的密钥环的名称。
  • 将位置替换为密钥环的 Cloud KMS 位置。
  • file-with-data-to-encryptfile-to-store-encrypted-data 替换为本地文件路径,用于读取明文数据并保存加密输出。