简体   繁体   English

以编程方式将SSH凭证添加到Jenkins时发生异常

[英]Exception while adding ssh credentials to Jenkins programmatically

I have used a method mentioned here to add credential to Jenkins programmatically. 我使用这里提到的方法以编程方式向Jenkins添加了凭证。 It worked successfully for adding secret texts and secrets files. 它成功地添加了秘密文本和秘密文件。 But it gives an exception while adding ssh private keys. 但是它在添加ssh私钥时给出了例外。 Below is the curl command I used. 下面是我使用的curl命令。

curl -X POST 'http://localhost:8080/jenkins/credentials/store/system/domain/_/createCredentials' \
--data-urlencode 'json={
"": "0",
"credentials": {
"scope": "GLOBAL",
"id": "temp",
"username": "temp",
"privateKeySource": {
  "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$FileOnMasterPrivateKeySource",
  "privateKeyFile": "/home/udhan/private-key.pem",
},
"stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey"
}
}'

Here is the exception I get. 这是我得到的例外。

A problem occurred while processing the request.
Please check <a href="https://jenkins.io/redirect/issue-tracker">our bug tracker</a> to see if a similar problem has already been reported.
If it is already reported, please vote and put a comment on it to let us gauge the impact of the problem.
If you think this is a new issue, please file a new issue.
When you file an issue, make sure to add the entire stack trace, along with the version of Jenkins and relevant plugins.
<a href="https://jenkins.io/redirect/users-mailing-list">The users list</a> might be also useful in understanding what has happened.</p><h2>Stack trace</h2><pre style="margin:2em; clear:both">org.kohsuke.stapler.NoStaplerConstructorException: There's no @DataBoundConstructor on any constructor of class com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$FileOnMasterPrivateKeySource
at org.kohsuke.stapler.ClassDescriptor.loadConstructorParamNames(ClassDescriptor.java:265)
at org.kohsuke.stapler.RequestImpl.instantiate(RequestImpl.java:765)
at org.kohsuke.stapler.RequestImpl.access$200(RequestImpl.java:83)
at org.kohsuke.stapler.RequestImpl$TypePair.convertJSON(RequestImpl.java:678)

I've just bumped into this same problem right now. 我现在碰到了同样的问题。 Rather than using a pem file, I ended up putting the SSH pem's value into a variable and passed it that way instead. 最后,我没有使用pem文件,而是将SSH pem的值放入变量中,并以这种方式传递了它。

CRUMB=$(curl -s 'https://{{jenkins_admin_username}}:{{jenkins_admin_password}}@localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)')
SSH_KEY="$(cat /your/ssh/pem)"
curl -H $CRUMB -X POST 'https://{{jenkins_admin_username}}:{{jenkins_admin_password}}@localhost:8080/credentials/store/system/domain/_/createCredentials' --data-urlencode 'json={
  "": "0",
  "credentials": {
    "scope": "GLOBAL",
    "id": "'test-jenkins-id'",
    "username": "'test-username'",
    "password": "",
    "privateKeySource": {
      "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource",
      "privateKey": "$SSH_KEY",
    },
    "description": "test-jenkins-ssh description",
    "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey"
  }
}'

Not that I went with https instead of http here as we're passing things that should be secured. 并不是说我在这里使用https而不是http,因为我们传递的是应该保护的东西。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM