2015-04-13 22 views
9

Devo creare una credenziale jenkins (https://wiki.jenkins-ci.org/display/JENKINS/Credentials+Plugin) tramite uno script. Come posso farlo usando l'API REST o il cli?Come creare credenziali jenkins tramite l'API REST?

nota che sono in grado di elencare le credenziali utilizzando/credenziale-store/dominio//api/JSON e/credenziale-store/domain//credenziale/8bd82461-e239-4db1-90bc-831ca3412e70/api/json etc.

+0

non sicuro con la possibilità di CLI o Riposo. Ma può essere realizzato in Python o Groovy usando jenkins api – DevD

risposta

3

Non esiste una chiamata API specifica per questo, ma è possibile farlo tramite i comandi cli al jar jenkins.

echo 'jenkins.model.Jenkins.instance.securityRealm.createAccount("username", "password")' | java -jar jenkins-cli.jar -s http://localhost/ groovy = 

Per concedere loro le autorizzazioni è possibile creare un'attività a Jenkins, che è in esecuzione ogni n minuti e l'esecuzione di uno script Groovy come descritto qui:

https://wiki.jenkins-ci.org/display/JENKINS/Grant+Cancel+Permission+for+user+and+group+that+have+Build+permission

+0

Ecco cosa ottengo (probabilmente perché è sensibile alla versione? Sto usando jenkins 1.596.1): 'echo 'jenkins.model.Jenkins.instance.securityRealm.createAccount ("username", "password") "| java -jar ~/jenkins-cli.jar -s http: // localhost: 11080/groovy = groovy.lang.MissingMethodException: Nessuna firma del metodo: hudson.security.SecurityRealm $ None.createAccount() è applicabile per i tipi di argomenti : (java.lang.String, java.lang.String) valori: [nome utente, password] ' –

+0

Sembra che questo sia per la creazione dell'account, non per le credenziali, giusto? –

+0

Come si crea un utente amministratore con una chiamata come questa? – Doug

16

Questo problema mi ha richiesto un po 'di tempo per capire, un sacco di scavi, quindi ho deciso di lasciare la soluzione qui, se qualcun altro ne ha bisogno.

curl -X POST 'http://user:[email protected]_server:8080/credentials/store/system/domain/_/createCredentials' \ 
--data-urlencode 'json={ 
    "": "0", 
    "credentials": { 
    "scope": "GLOBAL", 
    "id": "identification", 
    "username": "manu", 
    "password": "bar", 
    "description": "linda", 
    "$class": "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl" 
    } 
}' 
+1

che sembra molto interessante! Ti dispiacerebbe condividere i dettagli del ragionamento che ti portano a questa conclusione? –

9

con le ultime Jenkins è necessario una briciola per l'autenticazione per questa operazione (ref https://stackoverflow.com/a/38314286)

CRUMB=$(curl -s 'http://user:[email protected]_server:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)') 
curl -H $CRUMB -X POST 'http://user:[email protected]_server:8080/credentials/store/system/domain/_/createCredentials' \ 
--data-urlencode 'json={ 
    "": "0", 
    "credentials": { 
    "scope": "GLOBAL", 
    "id": "identification", 
    "username": "manu", 
    "password": "bar", 
    "description": "linda", 
    "$class": "com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl" 
    } 
}' 

altrimenti si ottiene

<body><h2>HTTP ERROR 403</h2> 
<p>Problem accessing /credentials/store/system/domain/_/createCredentials. Reason: 
<pre> No valid crumb was included in the request</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/> 
2

se è necessario creare le credenziali, ma con file PEM percorso è possibile utilizzare questo:

prerequisiti: ssh-credenziali plug

CRUMB=$(curl -s 'http://{{jenkins_admin_username}}:{{jenkins_admin_password}}@localhost:8080/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)') 
curl -H $CRUMB -X POST 'http://{{jenkins_admin_username}}:{{jenkins_admin_password}}@localhost:8080/credentials/store/system/domain/_/createCredentials' \ 
--data-urlencode 'json={ 
    "": "0", 
    "credentials": { 
    "scope": "GLOBAL", 
    "id": "'{{ii.ssh_user}}'", 
    "username": "'{{ii.ssh_user}}'", 
    "password": "", 
    "privateKeySource": { 
     "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$FileOnMasterPrivateKeySource", 
     "privateKeyFile": "'{{jenkins_home}}/{{ii.key_name}}.pem'", 
    }, 
    "description": "'{{ii.ssh_user}}'", 
    "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey" 
    } 
}' 

questo comando utilizzati in ansible ma è possibile sostituire le {{variabili}} con le proprie variabili

se avete bisogno di aggiungere tutto il contenuto del file PEM si è necessario modificare le righe in:

....  
"stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource", 
     "privateKey": "{{private_key_content}}", 
    }, 
    "description": "{{user}}", 
    "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey" 
... 
Problemi correlati