-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcdsec_credbarestrole.sql
314 lines (290 loc) · 12.4 KB
/
cdsec_credbarestrole.sql
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
--------------------------------------------------------------------------------
-- Trivadis - Part of Accenture, Platform Factory - Data Platforms
-- Saegereistrasse 29, 8152 Glattbrugg, Switzerland
--------------------------------------------------------------------------------
-- Name......: cdsec_credbarestrole.sql
-- Author....: Stefan Oehrli (oes) [email protected]
-- Editor....: Stefan Oehrli
-- Date......: 2023.03.17
-- Usage.....: cdsec_credbarestrole.sql <ROLE NAME>
-- Purpose...: Script to create a restricted DBA role inlcuding re-grant to
-- existing users.
-- Notes.....:
-- Reference.:
-- License...: Apache License Version 2.0, January 2004 as shown
-- at http://www.apache.org/licenses/
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- define default values
DEFINE _role_name = 'DBA_RESTRICTED'
--------------------------------------------------------------------------------
-- assign default value for parameter if argument 1 is empty
SET FEEDBACK OFF
SET VERIFY OFF
COLUMN 1 NEW_VALUE 1 NOPRINT
SELECT '' "1" FROM dual WHERE ROWNUM = 0;
DEFINE role_name = &1 &_role_name
COLUMN role_name NEW_VALUE role_name NOPRINT
SELECT upper('&role_name') role_name FROM dual;
--------------------------------------------------------------------------------
-- Define SQLPlus configuration
SET SERVEROUTPUT ON
SET LINESIZE 160 PAGESIZE 200
SPOOL create_dba_restricted_role.log
--------------------------------------------------------------------------------
-- create a temporary type
CREATE OR REPLACE TYPE table_varchar AS
TABLE OF VARCHAR2(128)
/
--------------------------------------------------------------------------------
-- Anonymous PL/SQL Block to configure restricted dba role
DECLARE
-- black list of system privileges
restricted_sys_privs table_varchar := table_varchar('DROP USER', 'ALTER USER', 'CREATE USER', 'BECOME USER');
-- black list of object privileges
restricted_obj_privs table_varchar := table_varchar('DBMS_SYS_SQL');
-- black list of role privileges
restricted_role_privs table_varchar := table_varchar('n/a');
-- white list of system privileges
allowed_sys_privs table_varchar := table_varchar('CREATE ANY INDEX','DROP ANY INDEX','ADMINISTER ANY SQL TUNING SET', 'ANALYZE ANY','AUDIT ANY');
-- white list of object privileges
allowed_obj_privs table_varchar := table_varchar('TOAD_TABLE_PLAN');
-- white list of role privileges
allowed_role_privs table_varchar := table_varchar('AUDIT_ADMIN','AUDIT_VIEWER');
TYPE t_dba_role_privs IS
TABLE OF dba_role_privs%rowtype;
r_dba_role_privs t_dba_role_privs;
v_ref_role VARCHAR2(128) := 'DBA';
v_role VARCHAR2(128) := '&role_name';
v_sql VARCHAR2(4000);
v_admin_option VARCHAR2(128) := '';
v_common VARCHAR2(128) := '';
v_exist INT;
v_container INT;
BEGIN
-- check if we are in a multitenant DATABASE
SELECT sys_context('userenv','con_id') INTO v_container FROM dual;
----------------------------------------------------------------------------
-- check if we do have grants for the restricted DBA role
SELECT
COUNT(*)
INTO v_exist
FROM
dba_roles
WHERE
role = v_role;
IF v_exist = 1 THEN
dbms_output.put_line('INFO : temporarly store grants for role '
|| sys.dbms_assert.enquote_name(v_role));
-- store the information from dba_role_privs
SELECT
*
BULK COLLECT
INTO r_dba_role_privs
FROM
dba_role_privs
WHERE
granted_role = v_role;
FOR i IN 1..r_dba_role_privs.last LOOP
-- set the admin option depending on the current setting
IF r_dba_role_privs(i).admin_option = 'YES' THEN
v_admin_option := ' WITH ADMIN OPTION';
ELSE
v_admin_option := '';
END IF;
-- set the container option depending on the current setting
IF v_container = 1 AND r_dba_role_privs(i).common = 'YES' THEN
v_common := ' CONTAINER=ALL';
ELSE
v_common := '';
END IF;
dbms_output.put_line('INFO : role '
|| sys.dbms_assert.enquote_name(v_role)
|| ' granted to '
|| sys.dbms_assert.enquote_name(r_dba_role_privs(i).grantee)
|| v_admin_option
||v_common );
END LOOP;
v_sql :='DROP ROLE ' || sys.dbms_assert.enquote_name(v_role);
dbms_output.put_line('INFO : execute ' || v_sql);
-- execute GRANT statement
EXECUTE IMMEDIATE v_sql;
END IF;
----------------------------------------------------------------------------
-- create role restriced DBA role
v_sql :='CREATE ROLE ' || sys.dbms_assert.enquote_name(v_role);
dbms_output.put_line('INFO : execute ' || v_sql);
-- execute GRANT statement
EXECUTE IMMEDIATE v_sql;
----------------------------------------------------------------------------
-- Query the current system privileges of the DBA role limited / extended the ANY and black and white list system privileges
dbms_output.put_line('INFO : grant all sys privileges based on DBA limited / extended ANY as well black and white listed system privileges'
);
FOR r_dba_sys_privs IN (
SELECT
*
FROM
dba_sys_privs
WHERE
grantee = v_ref_role
AND ( ( privilege NOT LIKE '%ANY%'
AND privilege NOT LIKE '%USER%'
AND privilege NOT IN ( SELECT * FROM TABLE ( restricted_sys_privs ) ) )
OR privilege IN ( SELECT * FROM TABLE ( allowed_sys_privs ))
)
ORDER BY
privilege
) LOOP
-- set the admin option depending on the current setting
IF r_dba_sys_privs.admin_option = 'YES' AND r_dba_sys_privs.grantee = v_ref_role THEN
v_admin_option := ' WITH ADMIN OPTION';
ELSE
v_admin_option := '';
END IF;
-- set the container option depending on the current setting
IF v_container = 1 AND r_dba_sys_privs.common = 'YES' THEN
v_common := ' CONTAINER=ALL';
ELSE
v_common := '';
END IF;
-- create the GRANT statement
v_sql := 'GRANT '
|| r_dba_sys_privs.privilege
|| ' TO '
|| sys.dbms_assert.enquote_name(v_role)
|| v_admin_option
|| v_common;
-- display revoke statement
dbms_output.put_line('INFO : execute ' || v_sql);
-- execute GRANT statement
EXECUTE IMMEDIATE v_sql;
END LOOP;
----------------------------------------------------------------------------
-- Query the current object privileges of the DBA role limited / extended the black and white list
dbms_output.put_line('INFO : grant all object privileges based on DBA limited / extended by black and white listed objects');
FOR r_dba_tab_privs IN (
SELECT
*
FROM
dba_tab_privs
WHERE
grantee = v_ref_role
AND type IN ( 'TABLE', 'VIEW', 'PACKAGE', 'PROCEDURE', 'FUNCTION', 'TYPE' )
AND privilege IN ( 'SELECT', 'READ', 'EXECUTE' )
AND grantor = 'SYS'
AND ( table_name NOT IN ( SELECT * FROM TABLE ( restricted_obj_privs ) )
OR table_name IN ( SELECT * FROM TABLE ( allowed_obj_privs ) ) )
ORDER BY
privilege
) LOOP
-- set the admin option depending on the current setting
IF r_dba_tab_privs.grantable = 'YES' AND r_dba_tab_privs.grantee = v_ref_role THEN
v_admin_option := ' WITH GRANT OPTION';
ELSE
v_admin_option := '';
END IF;
-- set the container option depending on the current setting
IF v_container = 1 AND r_dba_tab_privs.common = 'YES' THEN
v_common := ' CONTAINER=ALL';
ELSE
v_common := '';
END IF;
-- create the GRANT statement
v_sql := 'GRANT '
|| r_dba_tab_privs.privilege
|| ' ON '
|| sys.dbms_assert.schema_name(r_dba_tab_privs.owner)
|| '.'
|| sys.dbms_assert.sql_object_name(r_dba_tab_privs.table_name)
|| ' TO '
|| sys.dbms_assert.enquote_name(v_role)
|| v_admin_option
|| v_common;
-- display revoke statement
dbms_output.put_line('INFO : execute ' || v_sql);
-- execute GRANT statement
EXECUTE IMMEDIATE v_sql;
END LOOP;
----------------------------------------------------------------------------
-- Query the current role privileges of the DBA role limited / extended the black and white listed roles
dbms_output.put_line('INFO : grant all role privileges based on DBA limited / extended the black and white listed roles');
FOR r_dba_role_privs IN (
SELECT
*
FROM
dba_role_privs
WHERE
( grantee = v_ref_role
AND granted_role NOT IN ( SELECT * FROM TABLE ( restricted_role_privs ) )
AND granted_role NOT IN (
SELECT DISTINCT grantee FROM dba_sys_privs WHERE
( privilege LIKE '%ANY%'
OR privilege LIKE '%USER%'
OR privilege IN ( SELECT * FROM TABLE ( restricted_sys_privs ) ) )
AND privilege NOT IN ( SELECT * FROM TABLE ( allowed_sys_privs ) )
)
)
OR granted_role IN ( SELECT * FROM TABLE ( allowed_role_privs ) )
ORDER BY
granted_role
) LOOP
-- set the admin option depending on the current setting
IF r_dba_role_privs.admin_option = 'YES' AND r_dba_role_privs.grantee = v_ref_role THEN
v_admin_option := ' WITH ADMIN OPTION';
ELSE
v_admin_option := '';
END IF;
-- set the container option depending on the current setting
IF v_container = 1 AND r_dba_role_privs.common = 'YES' THEN
v_common := ' CONTAINER=ALL';
ELSE
v_common := '';
END IF;
-- create the GRANT statement
v_sql := 'GRANT '
|| r_dba_role_privs.granted_role
|| ' TO '
|| sys.dbms_assert.enquote_name(v_role)
|| v_admin_option
|| v_common;
-- display revoke statement
dbms_output.put_line('INFO : execute ' || v_sql);
-- execute GRANT statement
EXECUTE IMMEDIATE v_sql;
END LOOP;
----------------------------------------------------------------------------
-- restore GRANT for the restricted DBA role
IF v_exist = 1 THEN
dbms_output.put_line('INFO : recreate existing grants for role '
|| sys.dbms_assert.enquote_name(v_role));
FOR i IN 1..r_dba_role_privs.last LOOP
-- set the admin option if required
IF r_dba_role_privs(i).admin_option = 'YES' THEN
v_admin_option := ' WITH ADMIN OPTION';
ELSE
v_admin_option := '';
END IF;
v_sql := 'GRANT '
|| r_dba_role_privs(i).granted_role
|| ' TO '
|| sys.dbms_assert.enquote_name(sys.dbms_assert.schema_name(r_dba_role_privs(i).grantee))
|| v_admin_option;
-- display revoke statement
dbms_output.put_line('INFO : execute ' || v_sql);
-- execute revoke statement
EXECUTE IMMEDIATE v_sql;
END LOOP;
END IF;
dbms_output.put_line('INFO : Done updating role '
|| sys.dbms_assert.enquote_name(v_role));
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('ERROR: executing : ' || v_sql);
END;
/
--------------------------------------------------------------------------------
-- drop temporary created type
DROP TYPE table_varchar
/
spool off
-- EOF -------------------------------------------------------------------------