【问题标题】:How to create jenkins credentials via the REST API?如何通过 REST API 创建 jenkins 凭据?
【发布时间】:2015-04-13 23:20:54
【问题描述】:

我需要通过脚本创建一个 jenkins 凭证 (https://wiki.jenkins-ci.org/display/JENKINS/Credentials+Plugin)。如何使用 REST API 或 cli 做到这一点?

请注意,我可以使用 /credential-store/domain//api/json 和 /credential-store/domain//credential/8bd82461-e239-4db1-90bc 列出凭据-831ca3412​​e70/api/json 等。

【问题讨论】:

  • 不确定 CLI 或 Rest 的可能性。但它可以在 python 或 groovy 中使用 jenkins api 实现
  • 当我使用 API 创建带有空格的秘密时,得到Caused: javax.servlet.ServletException: Failed to parse JSON:{"": "7", "credentials"... 任何想法?

标签: jenkins


【解决方案1】:

这个问题我花了一些时间去想,大量的挖掘,所以我决定把解决方案放在这里,如果其他人需要它。

curl -X POST 'http://user:token@jenkins_server:8080/credentials/store/system/domain/_/createCredentials' \
--data-urlencode 'json={
  "": "0",
  "credentials": {
    "scope": "GLOBAL",
    "id": "identification",
    "username": "manu",
    "password": "bar",
    "description": "linda",
    "$class": "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"
  }
}'

【讨论】:

  • 看起来很有趣!你介意分享导致你得出这个结论的推理细节吗?
  • 这看起来很有用..还有什么可以删除凭据吗?
  • 只需添加 json 与秘密文本的外观,因为我们现在经常使用令牌:{ "": "0", "credentials": { "scope": "GLOBAL", " id”:“myid”,“secret”:“mysecret”,“description”:“mydescription”,“$class”:“org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl”} }
  • "":"0" 是什么意思?
  • 我们应该为“id”使用什么?
【解决方案2】:

如果您需要创建凭据但使用 pem 文件路径,您可以使用:

先决条件:ssh-credentials 插件

CRUMB=$(curl -s 'http://{{jenkins_admin_username}}:{{jenkins_admin_password}}@localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
curl -H $CRUMB -X POST 'http://{{jenkins_admin_username}}:{{jenkins_admin_password}}@localhost:8080/credentials/store/system/domain/_/createCredentials' \
--data-urlencode 'json={
  "": "0",
  "credentials": {
    "scope": "GLOBAL",
    "id": "'{{ii.ssh_user}}'",
    "username": "'{{ii.ssh_user}}'",
    "password": "",
    "privateKeySource": {
      "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$FileOnMasterPrivateKeySource",
      "privateKeyFile": "'{{jenkins_home}}/{{ii.key_name}}.pem'",
    },
    "description": "'{{ii.ssh_user}}'",
    "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey"
  }
}'

这个命令在 ansible 中使用,但您可以将 {{variables}} 替换为您自己的变量

如果你需要添加所有需要的pem文件内容你需要将这些行改为:

....      
"stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource",
      "privateKey": "{{private_key_content}}",
    },
    "description": "{{user}}",
    "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey"
...

【讨论】:

    【解决方案3】:

    Unable to point to ssh keys in ~/.ssh on Jenkins host

    意味着这不再有效,

    "privateKeySource": {
      "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$FileOnMasterPrivateKeySource",
      "privateKeyFile": "'{{jenkins_home}}/{{ii.key_name}}.pem'",
    },
    

    【讨论】:

      【解决方案4】:

      对于最新的 jenkins,您需要一个 CRUMB 来对此操作进行身份验证(参考 https://stackoverflow.com/a/38314286

      CRUMB=$(curl -s 'http://user:token@jenkins_server:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
      curl -H $CRUMB -X POST 'http://user:token@jenkins_server:8080/credentials/store/system/domain/_/createCredentials' \
      --data-urlencode 'json={
        "": "0",
        "credentials": {
          "scope": "GLOBAL",
          "id": "identification",
          "username": "manu",
          "password": "bar",
          "description": "linda",
          "$class": "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"
        }
      }'
      

      否则你会得到

      <body><h2>HTTP ERROR 403</h2>
      <p>Problem accessing /credentials/store/system/domain/_/createCredentials. Reason:
      <pre>    No valid crumb was included in the request</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/>
      

      【讨论】:

      • 我们应该为“id”使用什么?
      • 不,如果您使用访问令牌对 jenkins 进行身份验证,则不需要碎屑
      • @zhangjinzhou id 是 Jenkins 中凭证的名称
      【解决方案5】:

      只需在此处添加我的 2 美分:如果您想为特定文件夹创建凭据,请使用以下内容:

      curl -H $CRUMB -X POST 'http://user:token@jenkins_server:8080/job/MY_FOLDER_NAME/credentials/store/folder/domain/_/createCredentials' \
      ...
      

      因此,您需要在查询部分的开头使用 /job/My_Folder 并将 /store/system 替换为 /store/folder

      【讨论】:

        【解决方案6】:

        对此没有特定的 API 调用,但您可以通过 cli 命令对 jenkins jar 进行调用。

        echo 'jenkins.model.Jenkins.instance.securityRealm.createAccount("username", "password")' | java -jar jenkins-cli.jar -s http://localhost/ groovy =
        

        为了授予他们权限,您可以在 Jenkins 中创建一个任务,该任务每 N 分钟运行一次,并执行如下所述的 groovy 脚本:

        https://wiki.jenkins-ci.org/display/JENKINS/Grant+Cancel+Permission+for+user+and+group+that+have+Build+permission

        【讨论】:

        • 这是我得到的(可能是因为这是版本敏感的?我使用的是 jenkins 1.596.1):echo 'jenkins.model.Jenkins.instance.securityRealm.createAccount("username", "password")' | java -jar ~/jenkins-cli.jar -s http://localhost:11080/ groovy = groovy.lang.MissingMethodException: No signature of method: hudson.security.SecurityRealm$None.createAccount() is applicable for argument types: (java.lang.String, java.lang.String) values: [username, password]
        • 看起来这是用于创建帐户,而不是用于凭据,对吧?
        • 如何用这样的调用创建管理员用户?
        【解决方案7】:

        我有一个 groovy 脚本,它还使用基于 Matrix 的安全性设置用户权限。该脚本发布在Creating user in Jenkins via API

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-02-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多