将RSA密钥放入Azure密钥保管库

如何将我的密钥对(通常是id_rsa和id_rsa.pub)存储在Azure Key Vault中? 我想将公钥放在我的GIT服务中,并允许虚拟机从Azure Key Vault下载私钥,以便安全地访问GIT。 我尝试创建一对PEM文件,并将它们组合成一个pfx文件,然后将其上传为一个秘密,但是返回的文件似乎与任何一个pem文件完全不同。 我还尝试手动输入我的秘密密钥到Azure中,但它将换行符转换为空格。
3个回答

你可以使用 Azure CLIid_rsa 上传到 Azure Key Vault。
azure keyvault secret set --name shui --vault-name shui --file ~/.ssh/id_rsa
你可以使用-h来获取帮助。
--file <file-name>                 the file that contains the secret value to be uploaded; cannot be used along with the --value or --json-value flag
你还可以从密钥保管库下载秘密。
az keyvault secret download --name shui --vault-name shui --file ~/.ssh/id_rsa
我比较了实验室里的钥匙,它们是一样的。

抱歉,我不是原帖的作者,只是读了这个并测试了一下,将其作为有用的知识存档,并觉得应该给你点赞和留言:)。对于造成的混淆表示歉意。 - Reaces
抱歉,我不是原帖作者,只是阅读并测试了这个内容,并将其作为有用的知识存放起来,觉得我欠你一个点赞和评论 :) 听起来很有趣。如此友好的社群。 - Net Runner
我是OP,非常感谢Walter!我无法让原生CLI工作,但通过Python解决了这个问题。我能够登录、存储我的密钥并检索它。 -h的提示非常有帮助,因为它显示了比仅仅出错时更多的信息。 - MercilessMaverick
-u and -s flags refer to the options available in azure-cli (2.0.14). - s g
@sg 你好,你使用的是cli 1.0吗?-u vault name -s secret name。 - Shui shengbao
2以下是获取秘密的正确方法,FYI,“get”不再起作用。az keyvault secret download --name <KeyNameHere> --vault-name <vaultNamehere> --file <filename here> - Gregory Suvalian
Azure命令行工具(使用Python编写)是az而不是azure。因此,上述命令需要将azure替换为az。第二个命令的语法是正确的。 - Frederick Ollinger

之前Shengbao Shui的回答展示了使用Azure CLI 1.0(Node)存储秘密的命令。对于Azure CLI 2.0 (Python),请使用以下语法:

设置/存储密钥:

az keyvault secret set --vault-name 'myvault' -n 'secret-name' -f '~/.ssh/id_rsa'

参数:

Arguments
    --name -n    [Required]: Name of the secret.
    --vault-name [Required]: Name of the key vault.
    --description          : Description of the secret contents (e.g. password, connection string,
                             etc).
    --disabled             : Create secret in disabled state.  Allowed values: false, true.
    --expires              : Expiration UTC datetime  (Y-m-d'T'H:M:S'Z').
    --not-before           : Key not usable before the provided UTC datetime  (Y-m-d'T'H:M:S'Z').
    --tags                 : Space-separated tags in 'key[=value]' format. Use '' to clear existing
                             tags.

Content Source Arguments
    --encoding -e          : Source file encoding. The value is saved as a tag (`file-
                             encoding=<val>`) and used during download to automatically encode the
                             resulting file.  Allowed values: ascii, base64, hex, utf-16be,
                             utf-16le, utf-8.  Default: utf-8.
    --file -f              : Source file for secret. Use in conjunction with '--encoding'.
    --value                : Plain text secret value. Cannot be used with '--file' or '--encoding'.

Global Arguments
    --debug                : Increase logging verbosity to show all debug logs.
    --help -h              : Show this help message and exit.
    --output -o            : Output format.  Allowed values: json, jsonc, table, tsv.  Default:
                             json.
    --query                : JMESPath query string. See http://jmespath.org/ for more information
                             and examples.
    --verbose              : Increase logging verbosity. Use --debug for full debug logs.

获取密钥:

使用jq工具将密钥保存到文件~/.ssh/mykey中。

az keyvault secret show --vault-name myvault --name 'secret-name' | jq -r .value > ~/.ssh/mykey
文件可能会打印出一个尾随的换行符,你可以使用 Perl 的一行命令来删除它:
perl -pi -e 'chomp if eof' ~/.ssh/mykey

# Set permissions to user-read only
chmod 600 ~/.ssh/mykey
从私钥文件生成公钥...
ssh-keygen -y -f ~/.ssh/myfile > ~/.ssh/myfile.pub

如果我们想以ASCII编码格式将ssh密钥存储在KeyVault中,可以使用以下命令。 $az keyvault secret set –-vault-name <KEY_VAULT_NAME> -–name <NAME_OF_THE_KEY> –-file <PATH_OF_THE_SSH_KEY_FILE> -–encoding ascii