繁体   English   中英

对于使用 WSL2 签名的 git 提交,Windows 10 上的 Visual Studio Code 中没有 GPG 密码提示

[英]No GPG passphrase prompt in Visual Studio Code on Windows 10 for signed git commits using WSL2

当从 Windows 10 提交 WSL2 中的代码时,我无法在 Visual Studio Code 中使用签名提交。

问题

Windows 10 内没有显示密码提示导致 git failed with error: gpg failed to sign the data


设置

Windows 设置

带有 WSL2 和 Windows 终端的最新版本 Windows 10。

Visual Studio Code 安装在 Windows 10 内,是最新版本 (1.48.0),包含最新的 Remote WSL Extension (v0.44.4)。

我的代码在运行 Ubuntu 20.04 的 WSL2 中,所有包都已更新。

使用code. 来自 WSL2 的命令。

Git 安装

Git (2.25.1) 安装在 WSL2 中,具有以下全局配置:

[user]
    name = My Name
    email = my.email@example.com
    signingkey = A1B2C3D4E5F67890
[commit]
    gpgsign = true

git 存储库是使用 WSL2 中的命令行启动的。

GPG设置

gnupg (2.2.19) 安装在 WSL2 中。

我有~/.gnupg/gpg.conf设置

use-agent
default-key A1B2C3D4E5F67890

~/.gnupg/gpg-agent.conf

default-cache-ttl 34560000
max-cache-ttl 34560000
pinentry-program /usr/bin/pinentry-curses

还有我的~/.zprofile

export GPG_TTY=$(tty)
export GPG_AGENT_INFO=${HOME}/.gnupg/S.gpg-agent:0:1

什么有效

如果我在 Windows 终端的 WSL2 中运行git commit ,它会提示我输入密码。

┌────────────────────────────────────────────────────────────────┐
│ Please enter the passphrase to unlock the OpenPGP secret key:  │
│ "My Name <my.email@example.com>"                               │
│ 4096-bit RSA key, ID A1B2C3D4E5F67890,                         │
│ created 2020-08-10.                                            │
│                                                                │
│                                                                │
│ Passphrase: __________________________________________________ │
│                                                                │
│         <OK>                                    <Cancel>       │
└────────────────────────────────────────────────────────────────┘

然后,此密码短语将缓存到我的 Windows session 的 rest,直到我重新启动笔记本电脑。 此后的任何提交都不再提示输入密码。

我现在可以使用 Visual Studio Code git UI 和 VSCode 的内置终端来提交更改。


问题

如果我重新启动我的笔记本电脑,那么从 Visual Studio Code 提交将不起作用,直到我首先在 WSL2 中缓存 GPG 密码。

也许值得注意,但如果我没有缓存密码并在 Visual Studio Code 中打开代码并尝试从内置 VSCode 终端进行提交,它也会失败。 仅当密码首先通过 Windows 终端在 WSL2 中输入时才有效。

我很欣赏 Windows 10 和 WSL2 实际上是两个不同的系统,但是有什么方法可以让 Windows 显示在 WSL2 中运行的 git 的 GPG 密码提示?

还是我需要完全不同地进行设置? 我已经使用 MacOS 进行开发很长时间了,所以不确定 Windows 10 上的最佳路线是什么。

编辑:2020-08-17我找到了 https://github.com/diablodale/pinentry-wsl-ps1并将脚本用作 pinentry。 当未缓存密码时,这可以工作并在 Windows 10 中显示 GUI。 但我想找到一个不依赖于可能停止运行的脚本的解决方案。

我找到了一个解决方案......但在撰写本文时它有一些警告。

由于 Visual Studio Code 在 Windows 10 和 git 和 gpg 在 WSL2 中运行,我想在 Windows 10 侧尝试一个可视化的 pinentry。

直到我订阅了 Windows Insider 的跟踪并更新到最新的 Dev Build(2004 Build 20190.1000),这才奏效。

所需步骤:

加入 Windows Insider 计划并将 Windows 10 更新为 2004 Build 20190.1000(可能适用于早期版本,但这是我首先收到的版本)。 这将为 WSL2 带来一些有趣的附加功能,提高互操作性 - 您现在还会注意到 Windows Explorer 中的 Linux 选项。

https://www.gpg4win.org 安装 GPG4Win 除了默认的 gnupg 之外什么都不需要,但我也安装了 Kleopatra,以防它在其他地方派上用场。

编辑~/.gnupg/gpg-agent.conf并更改 pinentry

pinentry-program "/mnt/c/Program Files (x86)/GnuPG/bin/pinentry-basic.exe"

现在,当您被要求输入密码时,GPG GUI 就会出现。

我已经测试了重新启动机器以确保密码不会被缓存并且它适用于:

  • 从 WSL2 命令行提交
  • 使用 Visual Studio Code git UI 提交
  • 使用 Visual Studio Code 内置终端提交

这符合我的要求,但很高兴听到是否有任何我可能没有考虑过的部分。

只是添加一点,现在看起来很简单

git config --local gpg.program /usr/bin/gpg

足以(在您的 git 存储库中)VS Code 能够正确地使用本地 gpg 安装签署提交

非常感谢@39digits 的有用回答!

截至 2021 年 8 月的一些更新:

使用最新版本的 gpg4win,您需要在 WSL2 ubuntu 中~/.gnupg/gpg-agent.conf文件中设置以下配置:

pinentry-program "/mnt/c/Program Files (x86)/Gpg4win/bin/pinentry.exe"

此外,您不再需要订阅 windows 内部人员跟踪。

您已经指定了pinentry-curses程序,这意味着当您想要被提示输入密码时,您需要有一个合适的 TTY 来提示您。

如果您不想要这种行为,您可以使用其他 pinentry 程序; 例如,Debian 提供pinentry-gtk3 package,它可以提供图形提示。 但是,这要求您的环境中有一个可用的功能 X11 服务器。 这是必要的,因为 Linux 环境通常只支持通过 X11 的图形(或者可能是 Wayland,这在此处不再适用)。

还有像您提到的其他可能性,可能能够提供本机 Windows 提示,但所有这些都将依赖额外的附加软件,因为 Linux 发行版和 ZAEA23489CE3AA9B6406EBB28E0CDA 软件都没有提供此功能。 Linux 发行版通常不提供 Windows 图形界面的软件,因为它们不提供 Windows,因此大多数用户将无法使用它。

微软计划在未来为 WSL 提供更好的图形支持,但目前还没有这样做。

就我而言:

  • 我使用配置了 X 服务器 ( X410 ) 的 wsl2 ( ubuntu20.04 LTS ),并通过安装 pinentry-gtk2 找到解决方案。
  • 然后将pinentry-program配置为/usr/bin/pinentry-gtk-2 ,一切正常。

脚步:

  1. 配置你的 X 服务器。
  2. 安装 pinentry-gtk2。 sudo apt install pinentry-gtk-2
  3. 配置 gpg 代理。 添加pinentry-program /usr/bin/pinentry-gtk-2~/.gnupg/gpg-agent.conf
  4. 重新加载 gpg-agent。 执行命令gpg-connect-agent reloadagent /bye

备注:可用的 pinentries

效果: 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM