how to use httpRequest in Jenkinsfile pipeline with basic auth

Viewed 33308

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,

2 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}"]]

Here is how you can do this with Groovy.

def accessToken = "ACCESS_TOKEN".bytes.encodeBase64().toString()
def req = new URL("https://raw.githubusercontent.com/xxxx/something/hosts").openConnection();
req.setRequestProperty("Authorization", "Basic " + accessToken)
def content = req.getInputStream().getText()
Related