ShdwKnght333
ShdwKnght333

Reputation: 330

How to mitigate this "Error 403 No valid crumb was included in the request" when trying to post a build trigger to perforce

According to https://support.cloudbees.com/hc/en-us/articles/219152268-Use-POST-Commit-Hook-with-Perforce-Triggers I have to use the following to post a trigger to build.

#!/bin/bash

# The first argument is the change number
CHANGE=$1

# POST the payload
curl --header 'Content-Type: application/json' \
     --request POST \ 
     --data "{\"change\":$CHANGE, \"p4port\":\"localhost:1666\"}" \
     http://localhost:8080/p4/change

I've modified that a bit to add my port and change number after which it started giving me the crumb not found issue.

After reading this How to request for the crumb issuer for Jenkins and Spinnaker: 403 No valid crumb was included in the request I first tried to disable CSRF, but this gave me a 404, so I tried with it enabled, got the crumb and embedded it like so which again gives me a 404.

curl -X POST http://username:password@jenkinsip:8080/p4/change -H "Jenkins-Crumb:the-crumb" --data "{\"change\":$CHANGE, \"p4port\":\"myport\"}"

But if I try this I get the same 403 error again.

curl -X POST http://jenkinsip:8080/p4/change -H "Jenkins-Crumb:the-crumb" --data "{\"change\":$CHANGE, \"p4port\":\"myport\"}"

So what am I doing wrong that it is giving me a 404

Upvotes: 1

Views: 7243

Answers (1)

pitseeker
pitseeker

Reputation: 2563

Before requesting Jenkins to trigger a build based on your new perforce change you need to request a crumb from Jenkins and include the response in your request.

The following trigger script works for us:

#!/bin/bash
CHANGE=$1

P4PORT=<perforceserver>:<portnumber>
JUSER=<jenkinsuser>
JPASS=<hispassword>
JSERVER=https://<jenkinsserver>

# Get CRUMB
CRUMB=$(curl --insecure --silent --user $JUSER:$JPASS $JSERVER/crumbIssuer/api/xml?xpath=concat\(//crumbRequestField,%22":"%22,//crumb\))

curl --header "$CRUMB" \
     --request POST \
     --insecure \
     --silent \
     --user $JUSER:$JPASS \
     --data payload="{change:$CHANGE,p4port:\"$P4PORT\"}" \
     $JSERVER/p4/change

Update:
I just discovered that I've once stolen the above script from the documentation for the Jenkins Perforce Plugin. See here.

Upvotes: 1

Related Questions