Last active
March 5, 2025 16:27
-
-
Save jovemfelix/33f1cf34fbbbe1b5d3198569e33f1d8f to your computer and use it in GitHub Desktop.
How to Manage and Assign Roles of Jenkins using CURL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# variables - credentials info | |
USERNAME='my-username' | |
PASSWORD='my-clear-password' | |
USER_PASS="${USERNAME}:${PASSWORD}" | |
# variables - jenkins project and roles | |
TYPE='projectRoles' | |
## example of jenkins permission required to execute the Jenkins Job | |
PERMISSIONIDS='hudson.model.Item.Read,hudson.model.Item.Build' | |
ROLENAME='my-rolename' | |
## example of regex that requires a folder with name pipeline-hml and contains name of Jenkins Job with Role Name | |
PATTERN="^pipeline-hml|.*${ROLENAME}-.*" | |
SID="${ROLENAME}" | |
## JENKINS_URL with role-strategy-api | |
JENKINS_URL_ROLE_STRATEGY='https://JENKINS_URL/role-strategy/strategy' | |
## show variables values | |
echo " | |
\t USER_PASS \t\t\t= ${USER_PASS} | |
\t TYPE \t\t\t\t= ${TYPE} | |
\t ROLENAME \t\t\t= ${ROLENAME} | |
\t PATTERN \t\t\t= ${PATTERN} | |
\t SID \t\t\t\t= ${SID} | |
\t PERMISSIONIDS \t\t\t= ${PERMISSIONIDS} | |
\t JENKINS_URL_ROLE_STRATEGY \t= ${JENKINS_URL_ROLE_STRATEGY} | |
" | |
## addRole | |
curl -k -s --user "${USER_PASS}" \ | |
-F "type=${TYPE}" \ | |
-F "roleName=${ROLENAME}" \ | |
-F "permissionIds=${PERMISSIONIDS}" \ | |
-F "pattern=${PATTERN}" \ | |
-F 'overwrite=true' \ | |
"${JENKINS_URL_ROLE_STRATEGY}/addRole" | |
## assignRole | |
curl -k -s --user "${USER_PASS}" \ | |
-F "type=${TYPE}" \ | |
-F "roleName=${ROLENAME}" \ | |
-F "sid=${SID}" \ | |
"${JENKINS_URL_ROLE_STRATEGY}/assignRole" | |
## getRole | |
curl -k -s --user "${USER_PASS}" \ | |
-F "type=${TYPE}" \ | |
-F "roleName=${ROLENAME}" \ | |
"${JENKINS_URL_ROLE_STRATEGY}/getRole" | |
# Verify user logged details | |
# JENKINS_URL/whoAmI/ | |
# Reference | |
#- [Jenkins Role-based Authorization Strategy Plugin Requirement](https://plugins.jenkins.io/role-strategy/) | |
#- [Jenkins API Reference of Role Based Authorization Strategy](https://github.com/jenkinsci/role-strategy-plugin/blob/master/src/main/java/com/michelin/cio/hudson/plugins/rolestrategy/RoleBasedAuthorizationStrategy.java) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment