问题描述
我想使用 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 看不到文件?
解决方法
- 确保调用加密和解密方法的用户对用于加密或解密的密钥具有
cloudkms.cryptoKeyVersions.useToEncrypt
和cloudkms.cryptoKeyVersions.useToDecrypt
权限。
允许用户加密或解密的一种方法是将用户添加到该密钥的 roles/cloudkms.cryptoKeyEncrypter
、roles/cloudkms.cryptoKeyDecrypter
或 roles/cloudkms.cryptoKeyEncrypterDecrypter
IAM 角色。
- 要在命令行上使用 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-encrypt
和file-to-store-encrypted-data
替换为本地文件路径,用于读取明文数据并保存加密输出。