How can I add new user using python script in jenkins? I have gone through a lot of documentations but could not found any relevant idea

How can I create new user programatically using python script in jenkins? I have gone through a lot of documentations but could not found any relevant ideaI tried several endpoint like securityRealm/user/createUser etc. but could not find a correct one.
Kindly help.

Hello @diwanshi07 and welcome to this community :wave:

I have tried with Python and failed for security reasons:

Failed to create user. Error: b'<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>\n<title>Error 401 User sign up is prohibited</title>\n</head>\n<body><h2>HTTP ERROR 401 User sign up is prohibited</h2>\n<table>\n<tr><th>URI:</th><td>/securityRealm/createAccount</td></tr>\n<tr><th>STATUS:</th><td>401</td></tr>\n<tr><th>MESSAGE:</th><td>User sign up is prohibited</td></tr>\n<tr><th>SERVLET:</th><td>Stapler</td></tr>\n</table>\n<hr/><a href="https://eclipse.org/jetty">Powered by Jetty:// 10.0.15</a><hr/>\n\n</body>\n</html>\n'

I don’t know where I can configure that within Jenkins (I’m using JCasc).
Here is my script:

import requests
username = 'admin'
password = 'my_password'
jenkins_url = 'http://localhost:8080'
endpoint = f'{jenkins_url}/securityRealm/createAccount'

user_details = {
    'username': 'new_user',
    'password1': 'password123',
    'password2': 'password123',
    'fullname': 'New User',
    'email': 'newuser@example.com'
}

response = requests.post(endpoint, data=user_details, auth=(username, password))
if response.status_code == 200:
    print('User created successfully')
else:
    print(f'Failed to create user. Error: {response.content}')

Thanks for your response.
I tried to hit the api in postman by providing suitable authorization as well as jenkins-crumb but was unable to succeed. Here is my response body.

Error 404 Not Found

HTTP ERROR 404 Not Found

URI:/securityRealm/createAccount
STATUS:404
MESSAGE:Not Found
SERVLET:Stapler

Powered by Jetty:// 9.4.45.v20220203

Jenkins API is super limited.

The default user management system is super limited

I recommend using an external use management thing like oic, Google, GitHub, lap. They can all handle grips

Or use configuration as code and push your user information instead of single API calls

Thanks. I will try using the either way.