微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何通过 ssh 获取托管在 Azure DevOps Repo 中的 Terraform 模块

如何解决如何通过 ssh 获取托管在 Azure DevOps Repo 中的 Terraform 模块

正在寻找通过 SSH 获取托管在私有 Azure DevOps git 存储库中的 terraform 模块的正确/工作方式(请参阅TF Docs on Git repo sourcing via ssh)。

拥有 PAT 令牌是不可取的,因为它最终会过期并且还不能自动续订 (although coming soon apparently)。尽管如此,已经验证了 https 方法,但如果可以作为模块源引用 can't include terraform vars at init time,我希望避免需要编辑源。

我已按照以下 steps 生成 ssh 私钥/公钥对,并且这些文件添加到我的本地 .ssh 文件夹 @ C:\Windows\Users\<username>\.ssh

以下示例 terraform 代码:​​

module "test" {
    source = "<ref>"
}

其中 <ref> 已经过测试:

  • git::https://<pat token>:dev.azure.com/<org name>/<project name>/_git/<repo name>//<sub path>?ref=<version ref> - 作为最后手段的有效/经过验证的 https 方法
  • git::ssh://git@ssh.dev.azure.com:v3/<org name>/<project name>/_git/<repo name>//<sub path>?ref=<version ref>
  • git::ssh://git@ssh.dev.azure.com/v3/<org name>/<project name>/_git/<repo name>//<sub path>?ref=<version ref> - see terraform/issues/18869

在此处阅读有关 multiple keys内容后:

一般情况下,如果您为一个 SSH 客户端配置多个密钥并连接到 SSH 服务器,则客户端可以一次尝试一个密钥,直到服务器接受一个。但是,由于与 SSH 协议以及我们的 Git SSH URL 的结构相关的技术原因,这不适用于 Azure DevOps。 Azure DevOps 将盲目接受客户端在身份验证期间提供的第一个密钥。如果该密钥对于请求的存储库无效,则请求将失败并显示以下错误:远程:公钥身份验证失败。 致命:无法从远程存储库读取。

我玩弄了我的 .ssh/config 文件包括

  • 删除automation ssh 密钥对之外的所有其他密钥,我想专门用于这种情况(而不是我自己的个人 SSH 密钥来识别我)
  • .ssh/config 更新为:
Host automation
    HostName ssh.dev.azure.com
    IdentityFile ~/.ssh/automation_account
    IdentitiesOnly yes
Host azuredevops
    HostName ssh.dev.azure.com
    IdentityFile ~/.ssh/azuredevops
    IdentitiesOnly yes

然后在 <ref> 内尝试:

  • git::ssh://git@automation/v3/<org name>/<project name>/_git/<repo name>//<sub path>?ref=<version ref>
  • git::ssh://automation/v3/<org name>/<project name>/_git/<repo name>//<sub path>?ref=<version ref>

所有这些尝试的结果:

Permission denied,please try again.
git@ssh.dev.azure.com: Permission denied (password,publickey).
fatal: Could not read from remote repository.

does not appear to be a git repository
fatal: Could not read from remote repository.

我正在使用的 ssh 密钥已通过获取指纹 ssh-keygen -E md5 -lf <path to private key> 并返回 2048 MD5:ab:e2:... automation (RSA) 来验证它已注册到 Azure DevOps,我可以确认它存在于我的 SSH 密钥下的 Azure DevOps 中。

我已验证该密钥允许我通过使用该特定密钥添加新的 git 远程来访问我的目标 TF 存储库之一。 git remote -v 给了我:

aorigin       git@automation:v3/<org name>/<project name>/_git/<repo name> (fetch)
aorigin       git@automation:v3/<org name>/<project name>/_git/<repo name> (push)
origin  git@ssh.dev.azure.com:v3/<org name>/<project name>/_git/<repo name> (fetch)
origin  git@ssh.dev.azure.com:v3/<org name>/<project name>/_git/<repo name> (push)

并且 git pull aorigin 按预期工作。 Origin 是来自 Azure DevOps gui 的原始 SSH 克隆 URL。

几乎可以肯定我遗漏了一些明显的东西,但经过大量的谷歌搜索和许多不同的配置,我终生无法让它工作。感谢帮助/指示/建议

工具版本:

  • terraform v0.13.4
  • git 版本 2.17.1.windows.2
  • OpenSSH_for_Windows_7.7p1、LibreSSL 2.6.5

解决方法

this issueTerraform documentation 之后,我会尝试 URL

git::automation:v3/<org name>/<project name>/_git/<repo name>//<sub path>?ref=<version ref>

(将 User git 添加到我的 .ssh/config 文件中,紧跟在 Hostname 之后,以便不必添加用户)

OP Jamie 执行了以下步骤:

  • 将 Git 更新到提到的版本
  • 使用 MS Docs 中的步骤重新生成一些新密钥
  • 使用 ssh-add 添加所有密钥
  • 为 Azure DevOps 添加了公钥
  • .ssh/config 文件进行了以下更改:
Host ssh.dev.azure.com 
  IdentityFile ~/.ssh/me
  IdentitiesOnly yes

Host automation
  HostName ssh.dev.azure.com
  User git
  IdentityFile ~/.ssh/auto 
  IdentitiesOnly yes 

Host automationsec 
  HostName ssh.dev.azure.com 
  User git 
  IdentityFile ~/.ssh/auto_sec 
  IdentitiesOnly yes 

测试了以下两种情况:

module "test" {
  source     = "git::automation:v3/<org name>/<project name>/_git/<repo name>//<sub path>?ref=<version ref>"
}

module "test" {
  source     = "git::automationsec:v3/<org name>/<project name>/_git/<repo name>//<sub path>?ref=<version ref>"
}

第一个无密码条目有效,第二个情况无效。不确定为什么不接受带有密码的 ssh 密钥的根本原因

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?