【问题标题】:How do I list all of my Jenkins credentials in the script console?如何在脚本控制台中列出我的所有 Jenkins 凭据?
【发布时间】:2016-01-14 16:44:59
【问题描述】:

我正试图让 Jenkins 从 BitBucket 克隆我的 mercurial 项目。它不会,因为它凭据有问题 - 好吧,bitbucket 拒绝 Jenkins 提供的任何东西。

我几乎 100% 确定 Jenkins 没有提供它应该提供的东西,因为当我运行时

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

它克隆 a-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 凭据列表吗?

【问题讨论】:

    标签: jenkins groovy mercurial


    【解决方案1】:

    这对我来说效果很好......

    import java.nio.charset.StandardCharsets;
    def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
          com.cloudbees.plugins.credentials.Credentials.class
    )
    
    for (c in creds) {
      println(c.id)
      if (c.properties.description) {
        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.secretBytes) {
        println("    secretBytes: ")
        println("\n" + new String(c.secretBytes.getPlainData(), StandardCharsets.UTF_8))
        println("")
      }
      if (c.properties.privateKeySource) {
        println("   privateKey: " + c.getPrivateKey())
      }
      if (c.properties.apiToken) {
        println("   apiToken: " + c.apiToken)
      }
      if (c.properties.token) {
        println("   token: " + c.token)
      }
      println("")
    }
    

    【讨论】:

    • 编辑代码以添加 secretBytes、apiToken 和令牌支持。
    【解决方案2】:

    这是一个组合脚本,将有关该主题的多个发现连接在一起。它列出了来自 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("")
    }
    

    【讨论】:

    • 这是一个有用的答案,而 sn-p 可以解决问题。谢谢@vkozyrev
    • 直到我删除了大部分 println:s,我才让它工作。不断收到 50 倍的错误。我只留下了println(c.id),但这没关系,因为我只是在寻找一个特定的 ID。
    • 谢谢我用了这个,除了我用c.properties.each { println "$it.key -&gt; $it.value" }替换了for循环中的所有东西来转储所有东西
    【解决方案3】:

    我想要一份可用凭据的列表,结果发现:

    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

    【讨论】:

    • 您应该将相关信息添加到您的答案中,以防止链接失效时丢失。
    • 这不会在文件夹中获取任何凭据。
    【解决方案4】:

    这是快速脏代码,它会在 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) }
    

    【讨论】:

      【解决方案5】:

      这是我根据接受的答案修改的脚本。我们使用 azure 凭据,这将打印它们。如果未找到机密类型,它还将打印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("")
      }
      

      【讨论】:

      • 缺少sn-p的开头
      猜你喜欢
      • 2018-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-12
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      相关资源
      最近更新 更多