Google Cloud KMS - 加密 SSH 密钥 (id_rsa)

Google Cloud KMS - Encrypting SSH key (id_rsa)

我目前正在关注 https://cloud.google.com/cloud-build/docs/access-private-github-repos 但我卡住了,我不确定是否理解。

事实上我做了以下事情:

MBP-de-Emixam23:security-service emixam23$ gcloud kms keyrings create id_rsa --location=global
MBP-de-Emixam23:security-service emixam23$ gcloud kms keys create gitlab-key --location=global --keyring=id_rsa --purpose=encryption
MBP-de-Emixam23:security-service emixam23$ gcloud kms encrypt --plaintext-file=~/.ssh/id_rsa --ciphertext-file=~/.ssh/id_rsa.enc --location=global --keyring=id_rsa --key=gitlab-key
ERROR: (gcloud.kms.encrypt) Failed to read plaintext file [~/.ssh/id_rsa]: Unable to read file [~/.ssh/id_rsa]: [Errno 2] No such file or directory: '~/.ssh/id_rsa'
MBP-de-Emixam23:security-service emixam23$ cat ~/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
.....
-----END RSA PRIVATE KEY-------

谁能告诉我 gcloud kms 在找什么?我实际上需要它用于 google 云构建触发目的

谢谢!

编辑 1 - 为 John 的评论添加了冗长的内容

DEBUG: Running [gcloud.kms.encrypt] with arguments: [--ciphertext-file: "~/.ssh/id_rsa.enc", --key: "gitlab-key", --keyring: "id_rsa", --location: "global", --plaintext-file: "~/.ssh/id_rsa", --verbosity: "debug"]
DEBUG: (gcloud.kms.encrypt) Failed to read plaintext file [~/.ssh/id_rsa]: Unable to read file [~/.ssh/id_rsa]: [Errno 2] No such file or directory: '~/.ssh/id_rsa'
Traceback (most recent call last):
  File "/Users/emixam23/google-cloud-sdk/lib/googlecloudsdk/calliope/cli.py", line 984, in Execute
    resources = calliope_command.Run(cli=self, args=args)
  File "/Users/emixam23/google-cloud-sdk/lib/googlecloudsdk/calliope/backend.py", line 798, in Run
    resources = command_instance.Run(args)
  File "/Users/emixam23/google-cloud-sdk/lib/surface/kms/encrypt.py", line 90, in Run
    args.plaintext_file, e))
BadFileException: Failed to read plaintext file [~/.ssh/id_rsa]: Unable to read file [~/.ssh/id_rsa]: [Errno 2] No such file or directory: '~/.ssh/id_rsa'
ERROR: (gcloud.kms.encrypt) Failed to read plaintext file [~/.ssh/id_rsa]: Unable to read file [~/.ssh/id_rsa]: [Errno 2] No such file or directory: '~/.ssh/id_rsa'

如错误输出所示,文件 ~/.ssh/id_rsa 不存在。这可能有几个原因。

在大多数 shell 中(sh、bash、zsh),~ 扩展到当前用户的主目录。 shell 执行此扩展,通常基于您是 运行 命令的用户的 $HOME。例如,如果您使用 sudo 调用命令,~ 将解析为 root 的家,而不是您的家。从命令输出来看,~ 似乎是按字面意思发送到 gcloud,但 gcloud 期望 shell 首先解析它。

如果您使用的是不同的 shell,则 ~ 可能不受支持。鉴于您的 cat 命令有效,这似乎很可能。

另一种可能性是 = 导致您的 shell 无法解析 ~。我试图重现你的问题,但我不能。尽管如此,尝试以下命令会有所帮助(我已经删除了参数的可选 =):

gcloud kms encrypt \
  --plaintext-file ~/.ssh/id_rsa \
  --ciphertext-file ~/.ssh/id_rsa.enc \
  --location global \
  --keyring id_rsa \
  --key gitlab-key

不要用引号将文件路径括起来,这一点非常重要,因为这样它们将按字面解释,而不是由 shell 解析。

作为最后的手段,您可以强制 shell 像这样扩展目录:

gcloud kms encrypt \
  --plaintext-file "$(cd ~/.ssh && pwd)/id_rsa" \
  --ciphertext-file "$(cd ~/.ssh && pwd)/id_rsa.enc" \
  --location global \
  --keyring id_rsa \
  --key gitlab-key

正如评论中所讨论的,这里的反派是波浪线扩展。这通常由 shell 完成,但(与其他所有事情一样)也有复杂性。

波浪号扩展仅在 sh 和 bash 中的单词开头进行。因此,如果您传递 --plaintext-file ~/.ssh/id_rsa,它会起作用,因为 shell 将第二个参数解析为它自己的单词并扩展前导 ~。如果您传递 --plaintext-file=~/.ssh/id_rsa,缺少空格意味着 shell 将所有内容视为单个标记并且不会扩展 ~。如果 gcloud 在解析出参数后扩展了前导 ~ 本身,你会没事的(但显然没有)。

每当您看到文件名中打印有 ~ 的错误消息时,请保持怀疑:如果 shell 扩展了 ~,您会看到 shell 会传递给二进制文件,而不是原始 ~.

部分shell支持=后参数内扩展;例如,zsh 有一个配置参数 MAGIC_EQUAL_SUBST 可以打开此行为。如果参数看起来像变量赋值,bash 也会在不处于 POSIX 模式时执行此操作,但前导 -- 意味着这看起来不像 [=39= 的变量赋值].

虽然 gcloud 支持 --flag value--flag=value 语法,但如果您需要 --flag=value 结构,您可以使用 ${HOME} 而不是 ~ 来获取类似的扩展。 (我不会详细说明这些可能会如何变化,是的边缘情况。)

感谢使用 Cloud KMS!