-
Notifications
You must be signed in to change notification settings - Fork 31
/
applyauthmodel.py
executable file
·215 lines (174 loc) · 6.67 KB
/
applyauthmodel.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# applyauthmodel.py
# May 2023
#
#
# Change History
#
# 10MAY2023 Initial development
#
# To be used in conjunction with the ViyaAuthModel project (https://gitlab.sas.com/snztst/viyaauthmodel)
# Once an Auth Model Supplement document has been converted to csv files, that that collection of files
# may be applied to a Viya environment using this script.
#
# Usage examples:
# applyauthmodel.py -e <ENV> -d <DIR>
# applyauthmodel.py -e <ENV> -d <DIR> --dryrun
# applyauthmodel.py -e <ENV> -d <DIR> --dryrun --replacerules
#
# ENV refers to the same ENV value that was used for the extraction from the Supplement doc.
# DIR refers to the directory that contains the csv files extracted.
#
import argparse, os, shutil
# setup command-line arguements
parser = argparse.ArgumentParser(description="Applies the CSV files output by the ViyaAuthModel project (https://gitlab.sas.com/snztst/viyaauthmodel) and applies them to a Viya environment.")
parser.add_argument("-e","--env", help="Name of environment that is to be extracted.",required='True')
parser.add_argument("-d","--directory", help="Directory that contains CSV auth files to be implemented.",required='True')
parser.add_argument("--dryrun", help="Compiles commands for review without running them.",action='store_true')
parser.add_argument("--replacerules", help="Changes the function of toggleviyarules call from \'disable\' to \'replace\' \
- creating replacement rules for the Viya service account \'sasapp\'.",action='store_true')
#parser.add_argument("-q","--quiet", help="Suppress the are you sure prompt when creating CASLIBs.", action='store_true')
args = parser.parse_args()
viyaenv=args.env
indir=args.directory
dryrun=args.dryrun
reprules=args.replacerules
#quietmode=args.quiet
viyaglobal="ALL"
cwd=os.getcwd()
caslibdir=os.path.join(indir, 'caslibs')
## Check for presence of dryrun arg
if dryrun:
print("\n********** DRY-RUN MODE ********** \n")
dryrunnote="Command preview: "
#### Stage 1 - Prework ####
# Checks for 'caslibs' dir existence and deletes dir if found
if os.path.exists(caslibdir):
shutil.rmtree(caslibdir)
pass
## Convert "<env> - CASLIB Auth.csv" to JSON files
## Uses createcaslibjson.py
print("Preparing CASLIB files...")
command=os.path.join(cwd, 'createcaslibjson.py -f "')
csvfile=os.path.join(indir, viyaenv+' - CASLIB Auth.csv"')
if not dryrun:
os.system(command+csvfile)
else:
print(dryrunnote+command+csvfile)
## Convert "<env> - CASLIB Auth (Perms).csv" to _authorization_ files
## Uses createcaslibsjsonauth.py
command=os.path.join(cwd, 'createcaslibjsonauth.py -f "')
csvfile=os.path.join(indir, viyaenv+' - CASLIB Auth (Perms).csv"')
if not dryrun:
os.system(command+csvfile)
else:
print(dryrunnote+command+csvfile)
# Moves caslibs dir to the directory location input when running applyauthmodel.py
if not dryrun:
tempcaslibdir=os.path.join(cwd, 'caslibs')
if os.path.exists(tempcaslibdir):
shutil.move(tempcaslibdir,indir)
else:
pass
#### End Stage 1 ###
#### Stage 2 - Setup Custom Groups ####
## Implement Capability Roles
## Uses creategroups.py to read "ALL - Cap. Roles.csv"
print("Implementing Capability Roles...")
command=os.path.join(cwd, 'creategroups.py --skipfirstrow -f "')
csvfile=os.path.join(indir, viyaglobal+' - Cap. Roles.csv"')
if not dryrun:
os.system(command+csvfile)
else:
print(dryrunnote+command+csvfile)
## Implement OAGs
## Uses creategroups.py to read "ALL - Org Access Groups (OAGs).csv"
print("Implementing OAGs...")
command=os.path.join(cwd, 'creategroups.py --skipfirstrow -f "')
csvfile=os.path.join(indir, viyaglobal+' - Org Access Groups (OAGs).csv"')
if not dryrun:
os.system(command+csvfile)
else:
print(dryrunnote+command+csvfile)
## Implement Personas
## Uses creategroups.py to read "<env> - Personas.csv"
print("Implementing Personas...")
command=os.path.join(cwd, 'creategroups.py --skipfirstrow -f "')
csvfile=os.path.join(indir, viyaenv+' - Personas.csv"')
if not dryrun:
os.system(command+csvfile)
else:
print(dryrunnote+command+csvfile)
## Implement Inheritance (Custom Group nesting)
## Uses creategroups.py to read "<env> - Inheritance Model.csv"
print("Implementing inheritance...")
command=os.path.join(cwd, 'creategroups.py --skipfirstrow -f "')
csvfile=os.path.join(indir, viyaenv+' - Inheritance Model.csv"')
if not dryrun:
os.system(command+csvfile)
else:
print(dryrunnote+command+csvfile)
#### End Stage 2 ####
#### Stage 3 - Setup Folders ####
## Implement folders
## Uses "createfolders.py" to read "<env> - Folder Security.csv"
print("Creating Viya folders...")
command=os.path.join(cwd, 'createfolders.py --skipfirstrow -f "')
csvfile=os.path.join(indir, viyaenv+' - Folder Security.csv"')
if not dryrun:
os.system(command+csvfile)
else:
print(dryrunnote+command+csvfile)
## Apply security to folders
## Uses "applyfolderauthorization.py" to read "<env> - Folder Security (Perms).csv"
print("Implementing security to Viya folders...")
command=os.path.join(cwd, 'applyfolderauthorization.py -f "')
csvfile=os.path.join(indir, viyaenv+' - Folder Security (Perms).csv"')
if not dryrun:
os.system(command+csvfile)
else:
print(dryrunnote+command+csvfile)
#### End Stage 3 ####
#### Stage 4 - Setup CASLIBs ####
## Implement CASLIBs and their authorisation
## Uses "importcaslibs.py" to read the contents of a "caslibs" directory
print("Implementing CASLIBs...")
command=os.path.join(cwd, 'importcaslibs.py -su -q -d '+indir+'/caslibs')
#if quietmode:
# command=os.path.join(cwd, 'importcaslibs.py -su -q -d "')
#else:
# command=os.path.join(cwd, 'importcaslibs.py -su -d "')
if not dryrun:
os.system(command)
else:
print(dryrunnote+command)
#### End Stage 4 ####
#### Stage 5 - Setup Rules ####
## Implement rules
## Uses "applyviyarules.py" to read "ALL - Rules2Json.csv"
print("Applying new rules...")
command=os.path.join(cwd, 'applyviyarules.py -f "')
csvfile=os.path.join(indir, viyaglobal+' - Rules2Json.csv"')
if not dryrun:
os.system(command+csvfile)
else:
print(dryrunnote+command+csvfile)
## Disable OOTB rules
## Uses "toggleviyarules.py" to read "ALL - Rules2Disable.csv"
print("Disabling old rules...")
if not reprules:
command=os.path.join(cwd, 'toggleviyarules.py --skipfirstrow -o disable -f "')
else:
command=os.path.join(cwd, 'toggleviyarules.py --skipfirstrow -o replace -p sasapp -ptype group -f "')
csvfile=os.path.join(indir, viyaglobal+' - Rules2Disable.csv"')
if not dryrun:
os.system(command+csvfile)
else:
print(dryrunnote+command+csvfile)
### End Stage 5 ####
if not dryrun:
print("Auth Model implementation successfully completed.")
else:
print("\n********** DRY-RUN COMPLETE ********** \n")