如何自动化Jenkins SSH凭据创建/分配给节点?

时间:2015-07-26 13:04:20

标签: ssh jenkins automation credentials slave

我正在编写一个自动Jenkins机器创建脚本,我遇到了SSH凭据的问题,即:

在Jenkins中有一个名为credentials.xml的文件(在/var/lib/jenkins中),它存储了节点的凭据。我看起来像这样:

<?xml version='1.0' encoding='UTF-8'?>
<com.cloudbees.plugins.credentials.SystemCredentialsProvider plugin="credentials@1.18">
  <domainCredentialsMap class="hudson.util.CopyOnWriteMap$Hash">
    <entry>
      <com.cloudbees.plugins.credentials.domains.Domain>
        <specifications/>
      </com.cloudbees.plugins.credentials.domains.Domain>
      <java.util.concurrent.CopyOnWriteArrayList>
        <com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl>
          <scope>GLOBAL</scope>
          <id>8743cc14-bc2c-44a6-b6bb-c121bef4ae2d</id>
          <description>root_with_secret</description>
          <username>root</username>
          <password>2Xd4i7+8tjVXg2RHP6ggl/ZtWJp177ajXNajJxsj80o=</password>
        </com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl>
      </java.util.concurrent.CopyOnWriteArrayList>
    </entry>
  </domainCredentialsMap>

还有节点(从节点)配置文件(存储在/var/lib/jenkins/nodes/HOSTNAME/config.xml中为每个从节点),它们看起来像:

<?xml version='1.0' encoding='UTF-8'?>
<slave>
  <name>HOSTNAME_OF_MY_SECRET_MACHINE</name>
  <description>HOSTNAME_OF_MY_SECRET_MACHINE</description>
  <remoteFS>/root</remoteFS>
  <numExecutors>1</numExecutors>
  <mode>NORMAL</mode>
  <retentionStrategy class="hudson.slaves.RetentionStrategy$Always"/>
  <launcher class="hudson.plugins.sshslaves.SSHLauncher" plugin="ssh-slaves@1.9">
    <host>10.0.10.1</host>
    <port>22</port>
    <credentialsId>8743cc14-bc2c-44a6-b6bb-c121bef4ae2d</credentialsId>
    <maxNumRetries>0</maxNumRetries>
    <retryWaitTime>0</retryWaitTime>
  </launcher>
  <label></label>
  <nodeProperties/>
  <userId>anonymous</userId>
</slave>

问题在于,在创建jenkins机器后,为每个从属设备复制credentials.xmlconfig.xml,然后凭据将无法正常工作。我得到了

[07/26/15 16:00:39] [SSH] Opening SSH connection to 10.0.10.1:22.
ERROR: Failed to authenticate as root. Wrong password. (credentialId:8743cc14-bc2c-44a6-b6bb-c121bef4ae2d/method:password)
[07/26/15 16:00:41] [SSH] Authentication failed.
hudson.AbortException: Authentication failed.
    at hudson.plugins.sshslaves.SSHLauncher.openConnection(SSHLauncher.java:1178)
    at hudson.plugins.sshslaves.SSHLauncher$2.call(SSHLauncher.java:701)
    at hudson.plugins.sshslaves.SSHLauncher$2.call(SSHLauncher.java:696)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
[07/26/15 16:00:41] Launch failed - cleaning up connection
[07/26/15 16:00:41] [SSH] Connection closed.

要解决此问题,我可以访问 Jenkins - &gt; 凭证 - &gt;然后使用我将使用的相同密码更新凭证,它将起作用。

所以问题是Jenkins是否每次安装都使用salting / hashing,以便credentials.xml在复制到新机器时不起作用?

1 个答案:

答案 0 :(得分:6)

好的,我已经设法用(我相信)一个解决方法来解决这个问题,即:

要在credentials.xml中以纯文本格式存储密码,请在安装和启动服务后将其复制到Jenkins计算机上。然后詹金斯将用它的新秘密(或者它用于此目的的任何东西)加密它并且它将起作用:)

修改

第二个选项是安装Jenkins,启动它,然后将credentials.xml与加密密码一起复制到secrets目录和secret.xml之前的安装。这将复制加密主密钥和使用此主密钥创建的加密凭据。