When interacting with the Jenkins API using curl and an API token, you might encounter issues due to Jenkins' Cross-Site Request Forgery (CSRF) protection mechanisms. Specifically, Jenkins may require a special header (a "crumb") to be included in API requests to prevent CSRF attacks. Visiting the API in a browser can implicitly handle this crumb, which is why your curl requests work for a period after visiting the API through a browser.
To make your curl requests work independently and consistently without needing to visit the API in a browser first, you'll need to obtain the CSRF crumb programmatically and include it in your requests.
Here's how you can do that:
Step 1: Obtain a CSRF Crumb
Jenkins provides an API endpoint to fetch the CSRF crumb. You can obtain it using the following curl command:
curl -s -u "username:api_token" "http://your_jenkins_url/crumbIssuer/api/json"
- Replace
username with your Jenkins username.
- Replace
api_token with your Jenkins API token.
- Replace
http://your_jenkins_url with the URL to your Jenkins server.
This command will return a JSON object similar to:
{
"_class": "hudson.security.csrf.DefaultCrumbIssuer",
"crumb": "bf217820b5c45f9b345c3e7cedb7f5f6",
"crumbRequestField": "Jenkins-Crumb"
}
Explanation:
crumb: The CSRF protection token you need to include in your subsequent requests.
crumbRequestField: The header name you need to use when including the crumb in your request headers.
Step 2: Use the Crumb in Your Curl Requests
Include the crumb in the header of your curl requests when interacting with Jenkins API endpoints.
Example:
curl -u "username:api_token" -H "Jenkins-Crumb: bf217820b5c45f9b345c3e7cedb7f5f6" "http://your_jenkins_url/api/endpoint"
- Replace
bf217820b5c45f9b345c3e7cedb7f5f6 with the actual crumb you obtained.
- Replace
http://your_jenkins_url/api/endpoint with the API endpoint you want to access.
Automating the Process:
You can automate obtaining the crumb and using it in your requests in a shell script.
Example Script:
#!/bin/bash
# Jenkins credentials
JENKINS_USER="username"
JENKINS_API_TOKEN="api_token"
JENKINS_URL="http://your_jenkins_url"
# Get the crumb
CRUMB_JSON=$(curl -s -u "$JENKINS_USER:$JENKINS_API_TOKEN" "$JENKINS_URL/crumbIssuer/api/json")
CRUMB=$(echo $CRUMB_JSON | python3 -c "import sys, json; print(json.load(sys.stdin)['crumb'])")
CRUMB_REQUEST_FIELD=$(echo $CRUMB_JSON | python3 -c "import sys, json; print(json.load(sys.stdin)['crumbRequestField'])")
# Make the API request with the crumb
curl -u "$JENKINS_USER:$JENKINS_API_TOKEN" -H "$CRUMB_REQUEST_FIELD: $CRUMB" "$JENKINS_URL/api/endpoint"
Explanation:
- The script first fetches the crumb JSON data.
- It then extracts the
crumb and crumbRequestField using Python (you can also use jq or awk).
- It includes the crumb in the header of the subsequent API request.
Notes and Best Practices
By properly obtaining and including the CSRF crumb in your curl requests, you should be able to interact with the Jenkins API without the need to visit the API in a browser first.
Example Using jq for JSON Parsing
If you have jq installed, you can simplify the extraction:
# Get the crumb
CRUMB_JSON=$(curl -s -u "$JENKINS_USER:$JENKINS_API_TOKEN" "$JENKINS_URL/crumbIssuer/api/json")
CRUMB=$(echo $CRUMB_JSON | jq -r '.crumb')
CRUMB_REQUEST_FIELD=$(echo $CRUMB_JSON | jq -r '.crumbRequestField')
# Make the API request with the crumb
curl -u "$JENKINS_USER:$JENKINS_API_TOKEN" -H "$CRUMB_REQUEST_FIELD: $CRUMB" "$JENKINS_URL/api/endpoint"
Alternative: Disabling CSRF Protection (Not Recommended)
If you're operating in a secure, isolated environment and fully understand the risks, you might consider disabling CSRF protection in Jenkins:
- Navigate to
Manage Jenkins > Configure Global Security.
- Uncheck the option
Prevent Cross Site Request Forgery exploits.
- Save the configuration.
Warning: Disabling CSRF protection exposes Jenkins to CSRF attacks and is not recommended for production environments.
References
By following these steps, you can ensure that your curl requests to the Jenkins API work reliably without manual intervention.