Eclipse EGit TFS Git 连接 - 不支持身份验证

时间:2021-05-11 18:55:13

标签: git eclipse tfs egit jgit

嗨,

当使用 eclipse 中的 egit 时,我们正面临着“不支持身份验证”这个可怕的问题。我读过很多文章和问题,但似乎没有一篇能解决手头的问题。

我们正在使用 Microsoft Team Foundation Server 2015,它可以提供一个 git 存储库作为版本控制系统。但是使用 eclipse 的 egit 插件,身份验证机制似乎不起作用。我们使用 eclipse 2020-06、2020-12、2021-03 中的不同 eclipse 版本进行了检查,使用不同的 jdk 设置 1.8、11 和 15,但没有成功。

克隆、推送/获取在 git 命令行 (git bash - windows) 上运行良好,即使是传统的 eclipse kepler 也运行良好,没有问题,但不是最新的。该问题似乎与 NTLM 身份验证有关。在较新版本的 Eclipse 中,即使服务器使用 WWW-Authenticate: NTLM 响应,egit 也不会协商或发送 NTLM 令牌。

以下是新旧 Eclipse 版本的详细信息,通过 TCP/IP 监视器进行隧道传输以进行流量捕获。即使门户在 https 上,TFS git 存储库克隆协议也是 http。

Eclipse 2021-03

请求:

GET /tfs/xxx/_git/xxx/info/refs?service=git-upload-pack HTTP/1.1
Accept-Encoding: gzip
Pragma: no-cache
User-Agent: JGit/5.11.0.202103091610-r
Accept: application/x-git-upload-pack-advertisement, */*
Git-Protocol: version=2
Host: tfs-xxx-xx:8090
Connection: Keep-Alive

回复:

HTTP/1.1 401 Unauthorized
Cache-Control: private
Content-Type: text/html
Server: Microsoft-IIS/8.5
X-TFS-ProcessId: 76ec3355-4bc2-498d-ba64-xxxxxxx
X-FRAME-OPTIONS: SAMEORIGIN
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3600
Access-Control-Allow-Methods: OPTIONS,GET,POST,PATCH,PUT,DELETE
Access-Control-Expose-Headers: ActivityId,X-TFS-Session
Access-Control-Allow-Headers: authorization
Set-Cookie: Tfs-SessionId=3b370bd6-2197-xxxxxxxxx; path=/
Set-Cookie: Tfs-SessionActive=2021-05-11T17:25:24; path=/
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
P3P: CP="CAO xxxxxxxxxxxxxxxxx"
X-Content-Type-Options: nosniff
Date: Tue, 11 May 2021 17:25:24 GMT
Content-Length: 1293

Eclipse 开普勒

第一次请求/响应与上述类似。

第二个请求:

GET /tfs/xxx/_git/xxx/info/refs?service=git-upload-pack HTTP/1.1
Accept-Encoding: gzip
Pragma: no-cache
User-Agent: JGit/3.2.0.201312181205-r
Accept: application/x-git-upload-pack-advertisement, */*
Cache-Control: no-cache
Host: tfs-pmo-app:8090
Connection: keep-alive
Authorization: NTLM TlRMTVxxxxxxx - Length 72 chars

第二反应:

HTTP/1.1 401 Unauthorized
Content-Type: text/html; charset=us-ascii
WWW-Authenticate: NTLM TlRMxxxxxx== - Length 354 chars
Server: Microsoft-HTTPAPI/2.0
Date: Tue, 11 May 2021 17:33:00 GMT
Content-Length: 341

第三次请求:

GET /tfs/xxx/_git/xxx/info/refs?service=git-upload-pack HTTP/1.1
Accept-Encoding: gzip
Pragma: no-cache
User-Agent: JGit/3.2.0.201312181205-r
Accept: application/x-git-upload-pack-advertisement, */*
Cache-Control: no-cache
Host: tfs-pmo-app:8090
Connection: keep-alive
Authorization: NTLM TlRMTVNxxxxx - Length 256 chars

第三次回应:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/x-git-upload-pack-advertisement
Server: Microsoft-IIS/8.5
X-TFS-ProcessId: 76ec3355-4bc2-xxxxxxxxxxxxxx
X-FRAME-OPTIONS: SAMEORIGIN
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3600
Access-Control-Allow-Methods: OPTIONS,GET,POST,PATCH,PUT,DELETE
Access-Control-Expose-Headers: ActivityId,X-TFS-Session
Access-Control-Allow-Headers: authorization
Set-Cookie: Tfs-SessionId=xxxxxxxxxxxx; path=/
Set-Cookie: Tfs-SessionActive=2021-05-11T17:33:01; path=/
X-VSS-UserData: 69b58752-xxxxxxxxxxxxx
X-AspNet-Version: 4.0.30319
Persistent-Auth: true
X-Powered-By: ASP.NET
P3P: CP="CAO xxxxxxxxxxxxxxxxxxxxxxxxx"
X-Content-Type-Options: nosniff
Date: Tue, 11 May 2021 17:33:00 GMT
Content-Length: 1098

到目前为止我们尝试过的,将连接超时增加到 300,在 Eclipse Preferences for Git 中更改 http 客户端,在克隆 repo 时添加用户名/密码 - 这没有任何可见的影响,使用 jdk 更改 eclipse 的不同变体,将 git 凭证助手更改为 wincred、管理器、存储甚至在没有任何凭证助手的情况下重置,尝试配置 cntlm,但在测试期间它说您不需要代理,这似乎都不起作用。

这可能意味着 egit/jgit 实现中存在错误。从错误中查看当前的实现源和跟踪堆栈:

!ENTRY org.eclipse.egit.core 4 0 2021-05-08 04:56:02.018
!MESSAGE Pulling 1 repository
!SUBENTRY 1 org.eclipse.egit.core 4 0 2021-05-08 04:56:02.021
!MESSAGE http://xxxxx:8090/tfs/xxxx/_git/xxx: authentication not supported
!STACK 0
org.eclipse.jgit.api.errors.TransportException: http://xxxxx:8090/tfs/xxxx/_git/xxx: authentication not supported
    at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:224)
    at org.eclipse.jgit.api.PullCommand.call(PullCommand.java:263)
    at org.eclipse.egit.core.op.PullOperation$PullJob.run(PullOperation.java:255)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: org.eclipse.jgit.errors.TransportException: http://xxxxx:8090/tfs/xxxx/_git/xxx: authentication not supported
    at org.eclipse.jgit.transport.TransportHttp.connect(TransportHttp.java:674)
    at org.eclipse.jgit.transport.TransportHttp.openFetch(TransportHttp.java:465)
    at org.eclipse.jgit.transport.FetchProcess.executeImp(FetchProcess.java:142)
    at org.eclipse.jgit.transport.FetchProcess.execute(FetchProcess.java:94)
    at org.eclipse.jgit.transport.Transport.fetch(Transport.java:1309)
    at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:213)
    ... 3 more

https://git.eclipse.org/r/plugins/gitiles/jgit/jgit/+/refs/tags/v5.11.0.202103091610-r/org.eclipse.jgit/src/org/eclipse/jgit/transport/TransportHttp.java

org.eclipse.jgit.transport.TransportHttp.connect(TransportHttp.java:674):

Line 671:   case HttpConnection.HTTP_UNAUTHORIZED:
Line 672:                       authMethod = HttpAuthMethod.scanResponse(conn, ignoreTypes);
Line 673:                       if (authMethod.getType() == HttpAuthMethod.Type.NONE)
Line 674:                           throw new TransportException(uri, MessageFormat.format(
Line 675:                                   JGitText.get().authenticationNotSupported, uri));

看起来 scanResponse 方法对于导致问题的 auth 方法类型返回 none,所以让我们回顾一下 HttpAuthMethod:

https://git.eclipse.org/r/plugins/gitiles/jgit/jgit/+/refs/tags/v5.11.0.202103091610-r/org.eclipse.jgit/src/org/eclipse/jgit/transport/HttpAuthMethod.java

这里的代码基本上检查了 3 种身份验证方法,BASIC、DIGEST 和 NEGOTIATE,在我们的例子中,头“WWW-Authenticate: NTLM”不支持一次,因此例外。

但令人困惑的部分是,kepler 是如何成功地对 NTLM 进行身份验证的,答案可能(通过网络搜索)是实际上 jdk 可能对此负责,因为它在 HttpURLConnection 类下固有地发送 NTLM 令牌,这可能不是较新的 jdks 的情况(不知道为什么)。

结论:我似乎没有找到问题出在哪里的正确答案?

  1. 是不是因为新的 jdks 本身并不支持 NTML(在 引擎盖)?
  2. egit/jgit 不应该退回到其他机制,比如 BASIC 身份验证?
  3. 为什么我不能使用 egit/jgit 绕过 NTLM?

P.S. 在写这篇文章时,看起来我们的服务器可能没有配置为支持 BASIC 身份验证,但我使用 git 命令尝试了不同的身份验证类型,它们确实有效。我将再次使用wireshark捕获进行检查并查看底层机制。

谢谢,如果您阅读了整篇文章:)

2 个答案:

答案 0 :(得分:0)

<块引用>

Eclipse EGit TFS Git 连接 - 不支持身份验证

我同意你的看法。 AFAIK,Eclipse 的 EGit 是基于 JGit 构建的,不幸的是,最近版本的 JGit 主动拒绝 NTLM 身份验证,导致在连接到需要 NTLM 的本地 TFS 安装时“不支持身份验证”。

您可以从 FAQ 获得更多解释和解决方案:

<块引用>
  • 使用 Cntlm,一个本地安装的代理,可即时添加 NTLM 身份验证

  • 使用旧版本的 Eclipse/EGit/JGit

  • 在您的 TFS 服务器上的 IIS 中使用 SSL 启用基本身份验证

  • 在 TFS 服务器上的 IIS 中启用 Kerberos 身份验证(TFS 2015 之后下一版本 TFS 的默认设置):

    1. 在 IIS 管理器中,单击连接下左侧的 TFS 站点 并打开 IIS 下的“身份验证”部分。设置“ASP.NET “模拟”设置为启用,“Windows 身份验证”设置为启用。

    2. 在“Windows 身份验证”下右键单击并选择“提供程序”。 添加/启用“协商”和“NTLM”提供程序。

    3. 在“Windows 身份验证”下右键单击并选择“高级设置”。取消选中“启用内核模式”,因为它不兼容 使用 Kerberos。

此外,使用 TFS 2017 RTW 及更高版本,您可以创建一个范围至少为代码(读取和写入)的 Personal Access Token,然后您可以在 Eclipse EGit 配置中使用它代替密码。

答案 1 :(得分:0)

我终于设法解决了这个问题。

由于 kepler 版本(jdk 1.7)能够与 tfs 通信,所以我查看了 1.7 之后 jdks 是否有变化,幸运的是:

NTLM authentication in HttpURLConnection not working in JRE but works in JDK environment

How to provide ntlm authentication while calling any url?

从 jdk1.8.0_181 开始,默认情况下禁用基于 http 的 ntlm 透明身份验证,因此只需将其设置为 allHosts 即可解决问题。

您需要做的就是在 Java 8 的 jre/lib/net.properties 文件或 Java 11/15 的 /conf/net.properties 文件中注释此行:

#jdk.http.ntlm.transparentAuth=disabled

并取消注释:

jdk.http.ntlm.transparentAuth=allHosts

您也可以使用“-Djdk.http.ntlm.transparentAuth=allHosts”作为 jvm 参数进行设置。

此外,由于这适用于 HttpUrlConnection jdk 本机客户端,因此我们还需要更改 git http 客户端配置以使用“Java 内置 HTTP”而不是“Apache HTTP”。