title: Jenkins Credentials Store Access via Groovy
body: <p>I have found a way to access the <a href="https://github.com/jenkinsci/jenkins-scripts/blob/master/scriptler/changeCredentialPassword.groovy" rel="noreferrer">credentials store in Jenkins</a>:</p>
  
  <pre><code>def getPassword = { username -&gt;
      def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
          com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class,
          jenkins.model.Jenkins.instance
      )
  
      def c = creds.findResult { it.username == username ? it : null }
  
      if ( c ) {
          println "found credential ${c.id} for username ${c.username}"
  
          def credentials_store = jenkins.model.Jenkins.instance.getExtensionList(
              'com.cloudbees.plugins.credentials.SystemCredentialsProvider'
              )[0].getStore()
  
           println "result: " + credentials_store
      } else {
        println "could not find credential for ${username}"
      }
  }
  
  getPassword("XYZ")
  </code></pre>
  
  <p>But now i would like to get the password for the appropriate user which i can't do...</p>
  
  <p>I always get unknown method etc. if i try to access passord etc.</p>
  
  <p>The reason for doing this is to use this user/password to call git and extract information from repository..</p>
  
  <p>I always get something like this:</p>
  
  <pre><code>result: com.cloudbees.plugins.credentials.SystemCredentialsProvider$StoreImpl@1639eab2
  </code></pre>
  
  <h1>Update</h1>
  
  <p>After experimenting more (and the hint of Jeanne Boyarsky) with it i found that i was thinking to compilcated. The following already gives me the password for the user:</p>
  
  <pre><code>def getUserPassword = { username -&gt;
      def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
              com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials.class,
              jenkins.model.Jenkins.instance
              )
  
      def c = creds.findResult { it.username == username ? it : null }
  
      if ( c ) {
          return c.password
      } else {
          println "could not find credential for ${username}"
      }
  }
  </code></pre>
  
  <p>Furthermore by using the following snippet you can iterate over the whole credentials store:</p>
  
  <pre><code>def credentials_store = jenkins.model.Jenkins.instance.getExtensionList(
          'com.cloudbees.plugins.credentials.SystemCredentialsProvider'
          )
  
  println "credentials_store: ${credentials_store}"
  println " Description: ${credentials_store.description}"
  println " Target: ${credentials_store.target}"
  credentials_store.each {  println "credentials_store.each: ${it}" }
  
  credentials_store[0].credentials.each { it -&gt;
      println "credentials: -&gt; ${it}"
      if (it instanceof com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) {
          println "XXX: username: ${it.username} password: ${it.password} description: ${it.description}"
      }
  }
  </code></pre>
  
  <p>And you will get an output like this:</p>
  
  <pre><code>[(master)]:
  credentials_store: [com.cloudbees.plugins.credentials.SystemCredentialsProvider@5a2822be]
   Description: [The descriptions...]
   Target: [com.cloudbees.plugins.credentials.SystemCredentialsProvider@5a2822be]
  credentials_store.each: com.cloudbees.plugins.credentials.SystemCredentialsProvider@5a2822be
  credentials: -&gt; com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey@38357ca1
  credentials: -&gt; com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey@47cf7703
  credentials: -&gt; com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl@739abac5
  XXX: username: User1 password: Password description: The description of the user.
  credentials: -&gt; com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl@884a53e6
  XXX: username: User2 password: Password1 description: The description of the user1.
  Result:   [com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey@38357ca1, com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey@47cf7703, com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl@739abac5, com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl@884a53e6]
  </code></pre>
  
  <p>So by using the <a href="https://github.com/jenkinsci/credentials-plugin/blob/master/src/main/java/com/cloudbees/plugins/credentials/" rel="noreferrer">appropriate class in the <code>instanceof</code> clause</a> you can select what you need.</p>