|
| 1 | +from blackduck.HubRestApi import HubInstance |
| 2 | +import json |
| 3 | +import csv |
| 4 | +import argparse |
| 5 | + |
| 6 | +username = "sysadmin" |
| 7 | +password = "blackduck" |
| 8 | +urlbase = "https://blackduck.local" |
| 9 | +inactiveLog = r"c:\temp\inactive.csv" #add a location to your CSV log file |
| 10 | + |
| 11 | +parser = argparse.ArgumentParser("Enter a number of days since last logged?") |
| 12 | +parser.add_argument("-d", "--days", dest="days", type=int, default=30, |
| 13 | + help="Specify the number of days that a user has not logged in the console.") |
| 14 | + |
| 15 | +args = parser.parse_args() |
| 16 | + |
| 17 | +hub = HubInstance(urlbase, username, password, insecure=True) |
| 18 | + |
| 19 | +last_login = hub.get_last_login(sinceDays=args.days) |
| 20 | + |
| 21 | +print("********************************************") |
| 22 | +print("Print every user who should be inactive.") |
| 23 | +print("********************************************") |
| 24 | +print("") |
| 25 | +print(last_login) |
| 26 | +print("") |
| 27 | +print("") |
| 28 | + |
| 29 | +################################################################################################################################## |
| 30 | +# 1) - If System Users (anonymous, sysadmin, blackduck_system, default-authenticated user) are in this list, remove them from it # |
| 31 | +# 2) - Remove users who have never logged in before |
| 32 | +################################################################################################################################## |
| 33 | + |
| 34 | +last_login['items'] = [x for x in last_login['items'] if x['username'] not in ('sysadmin', 'anonymous', 'blackduck_system', 'default-authenticated-user')] #1 |
| 35 | +last_login['items'] = [x for x in last_login['items'] if 'lastLogin' in x] #2 |
| 36 | + |
| 37 | +print("********************************************") |
| 38 | +print("Filter List: Users never logged in and system users: anonymous, sysadmin, blackduck_system, default-authenticated user") |
| 39 | +print("********************************************") |
| 40 | +print("") |
| 41 | +print(last_login['items']) |
| 42 | +print("") |
| 43 | +print("") |
| 44 | + |
| 45 | +############################################################# |
| 46 | +# Create a new last-login list based on the remaining users # |
| 47 | +############################################################# |
| 48 | + |
| 49 | +new_last_login = last_login['items'] |
| 50 | + |
| 51 | +############################################### |
| 52 | +# Move (href:) key/value pair outside of meta # |
| 53 | +############################################### |
| 54 | + |
| 55 | +for index in range(len(new_last_login)): |
| 56 | + d = new_last_login[index] # Fetch the Dictionary element |
| 57 | + _meta = d['_meta'] # Fetch _Meta Key |
| 58 | + href = _meta['href'] # Fetch href |
| 59 | + d['href'] = href # Add to Main List |
| 60 | + del _meta['href'] # Delete from _Meta |
| 61 | + |
| 62 | + |
| 63 | +print("********************************************") |
| 64 | +print("Print updated list ") |
| 65 | +print("********************************************") |
| 66 | +print("") |
| 67 | +print(new_last_login) |
| 68 | +print("") |
| 69 | +print("") |
| 70 | + |
| 71 | +####################################################### |
| 72 | +# 1) deletes (lastLogin) and *(_meta) keypairs # |
| 73 | +# 2) remove the '/last-login' from the HREF URL value # |
| 74 | +# 3) grab the user data from current user in loop # |
| 75 | +# 4) create json data set with updated info for user # |
| 76 | +# 5) deactivate user # |
| 77 | +# 6) print user update status # |
| 78 | +####################################################### |
| 79 | + |
| 80 | +header = ['username','first name', 'last name', 'last login'] |
| 81 | + |
| 82 | +with open(inactiveLog, "w+", newline='') as f: |
| 83 | + writer = csv.writer(f, delimiter=',') |
| 84 | + writer.writerow(header) # write the header |
| 85 | + |
| 86 | +for i in range(len(new_last_login)): |
| 87 | + |
| 88 | + del new_last_login[i]['_meta'] #1 |
| 89 | + #del new_last_login[i]['lastLogin'] #1 |
| 90 | + new_last_login[i]['lastLogin'] #1 |
| 91 | + new_last_login[i]['href'] = new_last_login[i]['href'].replace("/last-login", "") #2 |
| 92 | + |
| 93 | + user_url = new_last_login[i]['href'] #3 |
| 94 | + user_info = hub.get_user_by_url(user_url) #3 |
| 95 | + |
| 96 | + #print ("") |
| 97 | + #print(user_info) |
| 98 | + #print ("") |
| 99 | + |
| 100 | + print ("***getting user data***") |
| 101 | + print ("") |
| 102 | + |
| 103 | + |
| 104 | + data_set = {"active" : False, "userName" :new_last_login[i]['username'],"firstName" :user_info['firstName'], "lastName" :user_info['lastName']} #4 |
| 105 | + json_dump = json.dumps(data_set) #4 |
| 106 | + update_json=json_dump #4 |
| 107 | + |
| 108 | + #print(user_url) |
| 109 | + #print(json_dump) |
| 110 | + |
| 111 | + print ("***deactivating user***") |
| 112 | + print ("") |
| 113 | + |
| 114 | + updateuser = hub.update_user_by_url(user_url,update_json) #5 |
| 115 | + |
| 116 | + print("Result for user:" + new_last_login[i]['username']) |
| 117 | + print(updateuser) #6 |
| 118 | + |
| 119 | + log = [new_last_login[i]['username'], user_info['firstName'], user_info['lastName'], new_last_login[i]['lastLogin']] |
| 120 | + |
| 121 | + with open(inactiveLog, mode='a', newline='') as f: |
| 122 | + writer = csv.writer(f, delimiter=',') |
| 123 | + writer.writerow(log) #write the inactive user data |
| 124 | + |
| 125 | + i=i+1 |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + |
0 commit comments