forked from sassoftware/pyviyatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetposixidentity.py
executable file
·95 lines (74 loc) · 3.14 KB
/
getposixidentity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# getposixidentity.py
# February 2021
#
# Returns the posix attributes of a user similar to the Linux "id" command
#
# Copyright © 2018, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the License);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import argparse
from sharedfunctions import printresult, callrestapi
# setup command-line arguements
parser = argparse.ArgumentParser(description="Display POSIX attributes for User or ALL users(default)")
parser.add_argument("-u","--user", help="Enter the user id or leave blank for all users.",default='all')
parser.add_argument("-q","--query", help="Enter a valid rest query of usernames.",default="not(blank(id))")
parser.add_argument("-d","--debug", action='store_true', help="Debug")
parser.add_argument("-o","--output", help="Output Style", choices=['csv','json','simple','simplejson'],default='json')
args = parser.parse_args()
user=args.user
debug=args.debug
query=args.query
output_style=args.output
if user=='all':
reqtype='get'
#reqval='/identities/users'
reqval='/identities/users/?filter=and(ne(providerId,"local"),'+query+')&limit=10000'
userslist_result_json=callrestapi(reqval,reqtype)
users = userslist_result_json['items']
for user in users:
userid=user['id']
reqval='/identities/users/'+userid+'/identifier'
posixinfo_result_json=callrestapi(reqval,reqtype,stoponerror=0,noprint=1)
# if a dictionary is returned posix attributes are available
if isinstance(posixinfo_result_json,dict):
# get uid
user["uid"]=posixinfo_result_json["uid"]
# get gid
user["gid"]=posixinfo_result_json["gid"]
if "secondaryGids" in posixinfo_result_json:
user["secgid"]=posixinfo_result_json["secondaryGids"]
else:
user["secgid"]=[""]
else:
user["uid"]=""
user["gid"]=""
user["secgid"]=[""]
cols=['id','uid','gid','secgid','name']
printresult(userslist_result_json,output_style,cols)
else:
# set the request type
reqtype='get'
# set the endpoint to call
reqval='/identities/users/'+user+"/identifier"
if debug: print(reqval)
#make the rest call using the callrestapi function. You can have one or many calls
user_info_result_json=callrestapi(reqval,reqtype)
# seems to be returning non standard results add id to make it standard
# print result expects there to an id so use uid
user_info_result_json['id']=user_info_result_json["uid"]
user_info_result_json['username']=user
printresult(user_info_result_json,output_style)