2016-02-04 9 views
10

Znalazłem sposób na dostęp do credentials store in Jenkins:Jenkins Poświadczenia Store dostęp przez Groovy

def getPassword = { username -> 
    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") 

Ale teraz chciałbym dostać hasło do odpowiedniego użytkownika, który nie mogę zrobić ...

zawsze dotrzesz nieznaną metodę itd. Gdy próbuję dostęp passord itp

powodem takiego postępowania jest użycie tego użytkownika/hasło, aby zadzwonić git i wydobywania informacji z repozytorium ..

zawsze uzyskać coś takiego:

result: com.cl[email protected]1639eab2 

Aktualizacja

Po eksperymentach więcej (i podpowiedź Jeanne Boyarskiego) z nim i stwierdził, że myślałem compilcated. Poniższa już daje mi hasło dla użytkownika:

def getUserPassword = { username -> 
    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}" 
    } 
} 

Ponadto stosując następujący fragment można iteracyjne nad całym poświadczeń sklepu:

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 -> 
    println "credentials: -> ${it}" 
    if (it instanceof com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl) { 
     println "XXX: username: ${it.username} password: ${it.password} description: ${it.description}" 
    } 
} 

I dostaniesz wyjście takiego:

[(master)]: 
credentials_store: [[email protected]22be] 
Description: [The descriptions...] 
Target: [[email protected]22be] 
credentials_store.each: [email protected]22be 
credentials: -> com.cloud[email protected]38357ca1 
credentials: -> com.cloud[email protected]47cf7703 
credentials: -> com.clo[email protected]739abac5 
XXX: username: User1 password: Password description: The description of the user. 
credentials: -> com.clo[email protected]884a53e6 
XXX: username: User2 password: Password1 description: The description of the user1. 
Result: [com.cloud[email protected]38357ca1, com.cloud[email protected]47cf7703, com.clo[email protected]739abac5, com.clo[email protected]884a53e6] 

Dzięki zastosowaniu appropriate class in the instanceof clause możesz wybrać to, czego potrzebujesz.

Odpowiedz

7

To działa. Dostaje dane uwierzytelniające, a nie sklep.

Nie napisałem żadnej obsługi błędów, więc to rozwinie się, jeśli nie masz skonfigurowanego obiektu referencji (lub prawdopodobnie, jeśli masz dwa). Ta część jest łatwa do dodania. Najtrudniejszą częścią jest uzyskanie właściwych interfejsów API!

def getPassword = { username -> 
    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 systemCredentialsProvider = jenkins.model.Jenkins.instance.getExtensionList(
      'com.cloudbees.plugins.credentials.SystemCredentialsProvider' 
      ).first() 

     def password = systemCredentialsProvider.credentials.first().password 

     println password 


    } else { 
     println "could not find credential for ${username}" 
    } 
} 

getPassword("jeanne") 
+0

Twój pomysł pomógł mi we właściwym kierunku. Dzięki. – khmarbaise

+0

Nice! Więc moje też było zbyt skomplikowane! Podoba mi się twoje podejście, które zredagowałeś w pierwszym poście. Kiedy mój zwrócił odpowiedź, przestałem szukać innych sposobów. –

3

Oficjalna rozwiązanie n jenkins wiki

Drukowanie listy wszystkich mandatów w systemie i ich identyfikatory.

def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
     com.cloudbees.plugins.credentials.Credentials.class, 
     Jenkins.instance, 
     null, 
     null 
); 
for (c in creds) { 
    println(c.id + ": " + c.description) 
} 
Powiązane problemy