gpt4 book ai didi

bash - 带有用户和密码的 GIT_ASKPASS

转载 作者:行者123 更新时间:2023-12-05 02:40:57 24 4
gpt4 key购买 nike

我在 Docker 容器中工作 git --version 2.20.1,基本镜像是 Debian 10。我想使用 GIT_ASKPASS 通过 HTTPS 克隆一个 git 存储库。甚至更好的配置属性 core.askpass .这是我尝试过的:

GIT_ASKPASS=$(mktemp) && chmod a+rx $GIT_ASKPASS && export GIT_ASKPASS
cat > $GIT_ASKPASS <<< '#!/bin/bash
exec printf "$JENKINS_CREDENTIALS_USR\n$JENKINS_CREDENTIALS_PSW\n"
'

最初我是在 Jenkins 中运行它,但我发现我能够在一个简单的 docker 容器中重现同样的问题。 $JENKINS_CREDENTIALS_USR$JENKINS_CREDENTIALS_PSW定义如下:

JENKINS_CREDENTIALS_USR=myuser
JENKINS_CREDENTIALS_PSW=mypass
export JENKINS_CREDENTIALS_USR
export JENKINS_CREDENTIALS_PSW

我没有设置任何全局配置属性。 ~/.git-credentials不存在。错误是:

fatal: Authentication failed for 'https://myserver.com/git/project.git/' 

它通过在 HTTPS url 中指定用户名来工作,就像这样

git clone https://jenkins@myserver.com/git/project.git .

并且只在 GIT_ASKPASS 指向的脚本中打印密码,所以我想知道为什么如果没有 url 中的用户名它就不能工作。 documentation明确地说

If the GIT_ASKPASS environment variable is set, the program specified by the variable is invoked. A suitable prompt is provided to the program on the command line, and the user's input is read from its standard output.

因为当我尝试克隆时,我被要求同时输入用户名和密码,原来的GIT_ASKPASS有什么问题?脚本?

最佳答案

我找到了解决方案 here .所有学分归于@AlexeyPelykh,我刚刚改编了他的剧本。


当尝试 git clone 时,我们的 shell 脚本会为每个提示行调用一次,基本上:

$GIT_ASKPASS "Username for ..."
$GIT_ASKPASS "Password for ..."

我的原始脚本中的问题是它返回的用户名和密码与我要求的内容无关。这也是为什么在第二种情况下(使用 URL 中指定的用户名)它起作用的原因,事实上我只需要密码并且它只返回密码。

这就是我如何更改我的原始脚本以使其与 GIT_ASKPASS 一起工作:

GIT_ASKPASS=$(mktemp) && chmod a+rx $GIT_ASKPASS && export GIT_ASKPASS
cat > $GIT_ASKPASS <<< '#!/bin/bash
case "$1" in
Username*) exec echo "$JENKINS_CREDENTIALS_USR" ;;
Password*) exec echo "$JENKINS_CREDENTIALS_PSW" ;;
esac
'

如果你想使用配置变量core.askPass,用这些替换前两行:

git_askPass_script=$(mktemp) && chmod a+rx $git_askPass_script
cat > $git_askPass_script <<< '#!/bin/bash

并设置配置变量:

git config --global core.askPass "$git_askPass_script"

甚至更好

git -c core.askPass="$git_askPass_script" clone ...

关于bash - 带有用户和密码的 GIT_ASKPASS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68344358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com