Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use httpRequest in Jenkinsfile pipeline with basic auth

Does anyone know how to use httpRequest in Jenkinsfile pipeline with basic auth? I checked in the https://github.com/jenkinsci/http-request-plugin, they support Supports Basic Authentication (see global configuration). But when i implemented in my groovy script, i found the following errors. Can anyone show how to pass the user name and password to the httprequest.

def masterDataReplicatorTriggerDeployment() {
milestone 40
VaultUtil vaultUtil = PipelineUtil.getInstance(this).getProdVaultUtil()
def ROUTER_USER
def ROUTER_PASS
def baseurl="http://master-data-replicator.cfapps.us10.hana.ondemand.com"
def sourcetenant="revcdevkp"
def targettenant="revcdevpo"
def copyBCdata="/replicator/v1/businessConfig/"
def response


ROUTER_USER=vaultUtil.readCredential
("secret/landscapes/infrastructure/router").get("router.security.username")
ROUTER_PASS=vaultUtil.readCredential
("secret/landscapes/infrastructure/router").get("router.security.password")
response = httpRequest url: 
baseurl+copyBCdata+sourcetenant+"/"+targettenant,
httpMode: 'POST',
authentication: [Username: ROUTER_USER, Password: ROUTER_PASS]

}

org.kohsuke.stapler.NoStaplerConstructorException: There's no @DataBoundConstructor on any constructor of class java.lang.String at org.kohsuke.stapler.ClassDescriptor.loadConstructorParamNames(ClassDescriptor.java:247) at org.jenkinsci.plugins.structs.describable.DescribableModel.(DescribableModel.java:122) at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:380) at

     def creds = "ROUTER_USER:ROUTER_PASS"
     String auth = creds.bytes.encodeBase64().toString()
     httpRequest  consoleLogResponseBody: true,  
         url: baseurl+copyBCdata+sourcetenant+"/"+targettenant,                       
         customHeaders:[[name:'Authorization', value:"Basic ${auth}"]]

The new problem: 401 authrization problem. But i did use the right credential and can visit them.

I know my format of authentication is not correct, do you know which supposed to be the correct way? Thanks.

Best Regards,

like image 582
neilxie Avatar asked Nov 25 '17 11:11

neilxie


People also ask

How does Jenkinsfile use Jenkins credentials?

From the Jenkins home page (i.e. the Dashboard of the Jenkins classic UI), click Manage Jenkins > Manage Credentials. Under Stores scoped to Jenkins on the right, click on Jenkins. Under System, click the Global credentials (unrestricted) link to access this default domain. Click Add Credentials on the left.

What happens when you send POST HTTP request to Jenkins URL?

This plugin sends a HTTP/HTTPS request to a user specified URL. The request is made as a job execution in Jenkins and depending of the HTTP response the job could be marked as failed (configurable). For example, responses such as 404 and 500 could make the job fail.


1 Answers

Use the credential plugin Credential plugin for storing your credentials. Then use the ID in the httpRequest.

Example :

New credential

Scope : Global
Username : my_technical_user
Password : *******
ID : my_user_id

httpRequest

httpRequest httpMode: 'POST',
url: "${baseurl}${copyBCdata}${sourcetenant}/${targettenant}",
authentication: 'my_user_id'

Or you can inject your username and password on the header directly.

def creds = "your_username:your_password"
String auth = creds.bytes.encodeBase64().toString()
httpRequest  consoleLogResponseBody: true,  
             url: "your_url",                       
             customHeaders:[[name:'Authorization', value:"Basic ${auth}"]]
like image 156
Lesly Bernaola Avatar answered Sep 19 '22 13:09

Lesly Bernaola