Jenkins git 插件自签名证书

Tob*_*ias 4 git ssl github jenkins

我目前正在尝试让 Jenkins 构建托管在 GitHub 企业存储库中的项目。我们为 GitHub 服务器使用自签名证书,这会导致一些问题。

我能够解决的大部分问题:

git 命令行:
获取 PEM 格式的证书并教 git 使用 .config 文件查找信息:

[http "https://github.*******************.com"]
    sslCAInfo = "path\\to\\selvSignedCerts\\fileName.pem"
    sslCAPath = "path\\to\\selvSignedCerts\\"
    sslVerify = true
Run Code Online (Sandbox Code Playgroud)

Jenkins GitHub 插件:
将证书信息添加到 java cacerts keystore:

keytool -import -trustcacerts -alias myAlias 
   -keystore "%JenkinsInstallDir\jre\lib\security\cacerts"
   -file "myCertsInfo.pem"
Run Code Online (Sandbox Code Playgroud)

将信息导入密钥库后重新启动 Jenkins 最终成功了,因此我能够在
管理 Jenkins -> 配置系统 -> GitHub Enterprise 服务器 -> API 端点中配置 GitHub Enterprise 服务器

此配置以前是不可能的,原因是:

SSL certificate problem: self signed certificate in certificate chain
Run Code Online (Sandbox Code Playgroud)

现在,我的 git 命令行很高兴,Jenkins 中的 GitHub 插件也很高兴,这意味着 Jenkins 本身很高兴,但 git-client 插件显然不高兴。

我在 jenkins 中创建了一个GitHub Organizations作业,指定了企业服务器上组织的链接。该配置使用全局 jenkins 设置中配置的 API 端点,使用配置的凭据并查找默认名称为Jenkinsfile 的管道文件

该作业能够执行结帐,它识别两个分支,并在每个分支中找到相应的 Jenkinsfile。它还使用 Jenkinsfile 配置自动为每个分支创建一个新作业。

jenkins 文件目前如下所示:

#!/usr/bin/env groovy

pipeline {
  agent any
  stages {
    stage('Scan for new jobs') {
      steps {
        echo 'Scanning...'
      }
    }
    stage('Build') {
      steps {
        echo 'Build'
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

现在管道中的第一步是隐式 scm 结账。这失败了,输出如下所示:

14:01:27 Connecting to https://github.*********.com/api/v3 using <userId>/******
Obtained Jenkinsfile from <commitHash>
[Pipeline] node
Running on Jenkins in D:\****\Jenkins\****\workspace\****
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
 > C:\Program Files\Git\bin\git.exe rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > C:\Program Files\Git\bin\git.exe config remote.origin.url https://github.****.com/***/***.git # timeout=10
Fetching without tags
Fetching upstream changes from https://github.****.com/***/***.git
 > C:\Program Files\Git\bin\git.exe --version # timeout=10
using GIT_ASKPASS to set credentials 
 > C:\Program Files\Git\bin\git.exe fetch --no-tags --progress https://github.****.com/***/***.git +refs/heads/develop:refs/remotes/origin/develop
ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from https://github.****.com/***/***.git
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:825)
    at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1092)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1123)
    at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:113)
    at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:85)
    at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:75)
    at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1$1.call(AbstractSynchronousNonBlockingStepExecution.java:47)
    at hudson.security.ACL.impersonate(ACL.java:260)
    at org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution$1.run(AbstractSynchronousNonBlockingStepExecution.java:44)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: hudson.plugins.git.GitException: Command "C:\Program Files\Git\bin\git.exe fetch --no-tags --progress https://github.****.com/***/***.git +refs/heads/develop:refs/remotes/origin/develop" returned status code 128:
stdout: 
stderr: fatal: unable to access 'https://github.****.com/***/***.git/': SSL certificate problem: self signed certificate in certificate chain

    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1970)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1689)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$300(CliGitAPIImpl.java:71)
    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:380)
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:823)
    ... 13 more
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Run Code Online (Sandbox Code Playgroud)

于是错误又来了:

stderr: fatal: unable to access 'https://github.****.com/***/***.git/': SSL certificate problem: self signed certificate in certificate chain
Run Code Online (Sandbox Code Playgroud)

现在看来,git-client 插件(或 git 插件?)似乎在lib\security\cacerts的密钥库之外寻找其他地方,并且似乎也不关心 .gitconfig文件

现在我的问题是,这个插件将在哪里寻找有效的证书?或者还有什么我没有得到的吗?

所有针对此问题或类似问题的研究都会导致将证书添加到 java 密钥库,我已经这样做了。我没有找到任何其他指向正确方向的信息。我最想知道的是,为什么GitHub Organizations作业可以很好地处理存储库、检测所有分支、签出并处理Jenkinsfile和其他作业(我还手动创建了一个自由式作业来检查它是否可以签出存储库 -具有相同的负面结果)由于自签名证书问题仍然无法访问存储库......

环境:(Jenkins 运行于)
Windows 7 x64
git 版本 1.9.4.msysgit.0
Jenkins 版本:2.73.2
Java JRE:1.8.0_144-b01
插件:
git 3.6.3
git-client 2.6.0
git-server 1.7
github 1.28.1
github-api 1.90

Tob*_*ias 5

好的,我找到了答案。进程监视器来救援!

使用进程监视器,我可以看到 git.exe构建我的小型自由式项目时正在读取 gitconfig 文件。
现在,令我惊讶的是,它没有读取用户目录中的.gitconfig(也称为全局配置),也没有读取系统下的 .gitconfig 文件(也称为系统配置),而是读取位于以下位置的 .gitconfig 文件:

C:\Program Files\Git\mingw64\etc\.gitconfig
Run Code Online (Sandbox Code Playgroud)

这个文件的原始内容是这样的:

[http]
    sslCAInfo = C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
    sslBackend = openssl
[diff "astextplain"]
    textconv = astextplain
[credential]
    helper = manager
Run Code Online (Sandbox Code Playgroud)

添加有关我的自签名证书信息的信息(如上所示)后,内容现在为:

[http]
    sslCAInfo = C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
    sslBackend = openssl
##################################
# Self Signed Server Certificate
[http "https://github.****.com"]
    sslCAInfo = "path\\to\\selvSignedCerts\\fileName.pem"
    sslCAPath = "path\\to\\selvSignedCerts\\"
    sslVerify = true
[diff "astextplain"]
    textconv = astextplain
[credential]
    helper = manager
Run Code Online (Sandbox Code Playgroud)

瞧 - 自由式作业和 Jenkinsfile 定义的自动生成的作业成功签出存储库并成功构建。

希望这可以帮助其他经历自签名证书痛苦的人。
但主要提示:停止使用自签名证书!买官方的会更便宜!(必须与我们的 IT 部门交谈...)