git - GIT:仅适用于 http/https 克隆存储库:git push 失败以明文形式显示密码
问题描述
我们正在使用 https 通过 Jenkins 构建克隆一个 repo,如下所示:
git clone https://${repo_username}:${repo_password}@internalgit.com/scm/project/repo.git -b ${branch_name} $tmp
在上面的命令中: ${repo_username} 和 ${repo_password} 是作为秘密传递的 Jenkins 变量,因此它们不会被记录为明文。
但是,这会将用户凭据添加到 git 远程 URL,并且在任何推送失败的情况下,它会在以下错误中以明文形式显示凭据:
[ERROR] To https://user:password@internalgit.com/scm/project/repo.git
[ERROR] ! [remote rejected] master -> master (pre-receive hook declined)
[ERROR] error: failed to push some refs to 'https://user:password@internalgit.com/scm/project/repo.git'
推送失败可能有多种正当原因,但是,在屏幕上打印凭据是不可接受的。
有没有办法:
- 屏蔽上述错误信息。
- 更新远程 URL 以丢失密码,在推送期间不会再次提示密码。
以下变通方法有效,但在我们的用例中不可接受:
- 将密码存储在凭据缓存中(使用 credential.helper)
- 使用 ssh 克隆而不是 https。
解决方案
假设输出可见但输入不可见:
git clone https://${repo_username}:${repo_password}@internalgit.com/scm/project/repo.git -b ${branch_name} $tmp | sed "s/${repo_password}/<redacted>/g"
应该做你想做的。
我误读了这个问题;要使这个答案起作用,您必须在每次推送时运行它(即git push 2>&1 | sed "s/${repo_password}/<redacted>/g"
。我也错过了 git 将其打印到stderr
,因此除非您想使用process substition,否则很难重定向输出。
如果密码包含任何特殊的正则表达式字符,您应该转义密码(否则 sed 可能会比您的意思更多或更少匹配)。这个答案有一些现成的解决方案,用于转义字符串以与 sed 一起使用。
推荐阅读
- java - 按正则表达式拆分与多个一个字符拆分性能
- ruby - 如何从救援内部救援异常
- sql - 使用任意键解析 JSON 值
- java - flink 1.13.1 Kafka producer报错ByteArraySerializer is not an instance of org.apache.kafka.common.serialization.Serializer
- asp.net - OpenSeadragon 仅加载一个 Deep Zoom 磁贴
- javascript - Async Await API 调用中的错误处理
- docker - 如何将 sqlite 命令行工具添加到 Docker aspnet:5.0-alpine 映像?
- java - 我如何通过 http://localhost:8080/ 启动 Camunda bpm?
- sql - 递归 SQL 查询获取 Json 树模型数据
- javascript - 如何检查输入值是否为图像?