如何在脚本控制台中列出我的所有Jenkins凭据?

时间:2016-01-14 16:45:00

标签: jenkins groovy mercurial

我试图让Jenkins从BitBucket克隆我的mercurial项目。它不会,因为它凭证存在问题 - 好吧,bitbucket拒绝Jenkins提供的任何内容。

我几乎100%肯定詹金斯没有提供应该提供的东西,因为当我跑步时

hg clone --ssh="ssh -i /path/to/my/key" ssh://hg@bitbucket.org/my-org/my-repo

克隆一个OK。 /path/to/my/key的内容是我在Jenkins'中放入密钥的内容。证书经理。我已经确认它存在于我的jenkins credentials.xml文件中。

然而,当我试着去做我的工作?克隆失败是因为

remote: Host key verification failed.

这让我相信问题在于通过mercurial插件传递的内容。但是我无法在日志中看到它,因为它是某种masked string,只显示为******

所以我想确保转到Hg插件的凭据实际上是我credentials.xml文件中的内容。

到目前为止,我已经到了这里:

import com.cloudbees.plugins.credentials.CredentialsProvider
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials


def creds = CredentialsProvider.all()
print creds

这给了我一份凭证提供商列表......但我不知道下一步该怎么做。我一直淹没在文档中,试图弄清楚如何获取我想要的凭证信息......但没有骰子。

(如何)我可以获取我所拥有的内容并在Groovy脚本控制台中显示我的Jenkins凭据列表?

5 个答案:

答案 0 :(得分:3)

这是一个组合脚本,将有关此主题的多个发现连接在一起。它列出了Jenkins所有范围的所有凭证,而不仅仅是根范围。希望它会有所帮助。

import com.cloudbees.plugins.credentials.Credentials


Set<Credentials> allCredentials = new HashSet<Credentials>();


def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
      com.cloudbees.plugins.credentials.Credentials.class
);


allCredentials.addAll(creds)


Jenkins.instance.getAllItems(com.cloudbees.hudson.plugins.folder.Folder.class).each{ f ->
 creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
      com.cloudbees.plugins.credentials.Credentials.class, f)
  allCredentials.addAll(creds)

}


for (c in allCredentials) {
  println(c.id)
  if (c.properties.username) {
    println("   description: " + c.description)
  }
  if (c.properties.username) {
    println("   username: " + c.username)
  }
  if (c.properties.password) {
    println("   password: " + c.password)
  }
  if (c.properties.passphrase) {
    println("   passphrase: " + c.passphrase)
  }
  if (c.properties.secret) {
    println("   secret: " + c.secret)
  }
  if (c.properties.privateKeySource) {
    println("   privateKey: " + c.getPrivateKey())
  }
  println("")
}

答案 1 :(得分:2)

我想要一份可用的凭据列表,并找到了:

def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials( com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class, Jenkins.instance, null, null );

for (c in creds) {
   println(c.id + ": " + c.description)
}

从这里得到它: https://wiki.jenkins-ci.org/display/JENKINS/Printing+a+list+of+credentials+and+their+IDs

答案 2 :(得分:1)

这对我来说很好...

def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
      com.cloudbees.plugins.credentials.Credentials.class
)

for (c in creds) {
  println(c.id)
  if (c.properties.username) {
    println("   description: " + c.description)
  }
  if (c.properties.username) {
    println("   username: " + c.username)
  }
  if (c.properties.password) {
    println("   password: " + c.password)
  }
  if (c.properties.passphrase) {
    println("   passphrase: " + c.passphrase)
  }
  if (c.properties.secret) {
    println("   secret: " + c.secret)
  }
  if (c.properties.privateKeySource) {
    println("   privateKey: " + c.getPrivateKey())
  }
  println("")
}

答案 3 :(得分:1)

这是我根据接受的答案修改的脚本。我们使用天蓝色的凭据,这将打印它们。如果未找到密码类型,还将打印c.properties

    com.cloudbees.plugins.credentials.Credentials.class
)

for (c in creds) {
  println(c.id)
  if (c.properties.username) {
    println("   description: " + c.description)
  }
  if (c.properties.username) {
    println("   username: |" + c.username + "|")
  }
  else if (c.properties.password) {
    println("   password: |" + c.password + "|")
  }
  else if (c.properties.passphrase) {
    println("   passphrase: |" + c.passphrase + "|")
  }
  else if (c.properties.secret) {
    println("   secret: |" + c.secret + "|")
  }
  else if (c.properties.privateKeySource) {
    println("   privateKey: " + c.getPrivateKey())
  }
  else if (c.properties.clientId) {
    println("   clientId    : " + c.getClientId())
    println("   tenant      : " + c.getTenant())
    println("   subscription: " + c.getSubscriptionId())
    println("   secret      : " + hudson.util.Secret.fromString(c.getClientSecret()))
  }
  else {
    println("unknown secret type")
    println(c.properties)
  }
  println("")
}

答案 4 :(得分:0)

这是快速的肮脏代码,它将在jenkins上打印一些凭证类型,如果您需要更多,则可以使用此处定义的不同类:

https://github.com/jenkinsci/credentials-plugin/tree/master/src/main/java/com/cloudbees/plugins/credentials/common

def StandardUsernameCredentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
    com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class,
    Jenkins.instance,
    null,
    null ); for (c in StandardUsernameCredentials) {
     println(c.id + ": " + c.description) } def StandardUsernamePasswordCredentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
    com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class,
    Jenkins.instance,
    null,
    null ); for (c in StandardUsernamePasswordCredentials) {
     println(c.id + ": " + c.description) }

def IdCredentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
    com.cloudbees.plugins.credentials.common.IdCredentials.class,
    Jenkins.instance,
    null,
    null ); for (c in IdCredentials) {
     println(c.id + ": " + c.description) }

def StandardCertificateCredentialsCredentials = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
    com.cloudbees.plugins.credentials.common.StandardCertificateCredentials.class,
    Jenkins.instance,
    null,
    null ); for (c in StandardCertificateCredentialsCredentials) {
     println(c.id + ": " + c.description) }