Skip to content

Instantly share code, notes, and snippets.

@jovemfelix
Last active September 12, 2022 08:40
Show Gist options
  • Select an option

  • Save jovemfelix/d23f2327aeb7301b1f60274035a57310 to your computer and use it in GitHub Desktop.

Select an option

Save jovemfelix/d23f2327aeb7301b1f60274035a57310 to your computer and use it in GitHub Desktop.
How to Manage and Assign Roles of Jenkins using CURL

Variables

# credentials info
USERNAME='my-username'
PASSWORD='my-clear-password'
USER_PASS="${USERNAME}:${PASSWORD}"

# 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}
"

CURL to 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"

CURL to assignRole

curl -k -s --user "${USER_PASS}" \
-F "type=${TYPE}" \
-F "roleName=${ROLENAME}" \
-F "sid=${SID}" \
"${JENKINS_URL_ROLE_STRATEGY}/assignRole"

CURL to getRole

curl -k -s --user "${USER_PASS}" \
-F "type=${TYPE}" \
-F "roleName=${ROLENAME}" \
"${JENKINS_URL_ROLE_STRATEGY}/getRole"

Sample Output

	 USER_PASS 			= my-username:my-clear-password
	 TYPE 				= projectRoles
	 ROLENAME 			= my-rolename
	 PATTERN 			= ^pipeline-hml|.*my-rolename-.*
	 SID 				= my-rolename
	 PERMISSIONIDS 			= hudson.model.Item.Read,hudson.model.Item.Build
	 JENKINS_URL_ROLE_STRATEGY 	= https://JENKINS_URL/role-strategy/strategy

{"permissionIds":{"hudson.model.Item.Read":true,"hudson.model.Item.Build":true},"pattern":"^pipeline-hml|.*my-rolename-.*","sids":["my-rolename"]}%  

Verify user logged details

JENKINS_URL/whoAmI/

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment