ARTIFACTORY: How to get the list of admin users in Artifactory?

Elina Floim
2022-08-21 09:48

 

Introduction:

Retrieving the list of admin users can be useful when managing user permissions in Artifactory and for permission monitoring purposes, especially for users with “Administer Platform” permissions. This article will demonstrate how to retrieve the list of admin users using a python script, based on the information retrieved from the Get Users and Get User Details REST API calls.

Prerequisites:

  • Python 3
  • An IDE with a python interpreter

Execution:

Use an IDE to create a python file (.py) that will contain the following script:


import json
import os

#gets the list of users
command_s = "curl -u admin:password https://$ART_HOST/artifactory/api/security/users"
command_e = " > /path/to/users.json"
request = command_s + command_e
command_exe = os.system(request)

#reads the json file containing the list of users
with open('/path/to/users.json', 'r') as fcc_file:
user_data = json.load(fcc_file)
values = []
#creates a list named 'values' which contains the user names
for i in user_data:
values.append(i["name"])

#creates an empty list that will hold the admin users
admins = []
#for each user, get the user details
for name in values:
command_s = "curl -u admin:password https://$ART_HOST/artifactory/api/security/users/"
command_e = " > /path/to/user_result.json"
request = command_s + name + command_e
command_exe = os.system(request)
with open('/path/to/user_result.json', 'r') as user_file:
admin_data = json.load(user_file)
#if the user is an admin, add him to the list
for key in admin_data:
if (key == 'admin') and admin_data[key]:
admins.append(admin_data['name'])
print(admins)

Notes:

  • The $ART_HOST variable in the URLs should be replaced with the actual Artifactory host. In addition, the credentials in both cURL requests should be changed to the actual credentials of your Artifactory instance.
  • The /path/to/user_result.json and /path/to/users.json paths should be replaced with the paths you wish to create these files under.

The output of the script will be a list of usernames, and will look similar to this:
[‘admin’, ‘elinaf’, ‘testuser@test.com’, ‘jsonk@test.com’, ‘jamesp’]