-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathoracle_role.py
297 lines (248 loc) · 8.64 KB
/
oracle_role.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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
---
module: oracle_role
short_description: Manage Oracle roles
description:
- Modify Oracle system parameters
options:
name:
description:
- Role name
required: true
roles:
description:
- List of roles granted to the role.
- If an empty list ([]) is specified all granted roles will be revoked.
- All items will be converted using uppercase.
required: false
default: None
sys_privs:
description:
- List of system privileges granted to the role.
- If an empty list ([]) is specified all granted system privileges will be revoked.
- All items will be converted using uppercase.
state:
description:
- Parameter state
required: False
default: present
choices: ["present", "absent"]
oracle_host:
description:
- Hostname or IP address of Oracle DB
required: False
default: 127.0.0.1
oracle_port:
description:
- Listener Port
required: False
default: 1521
oracle_user:
description:
- Account to connect as
required: False
default: SYSTEM
oracle_pass:
description:
- Password to be used to authenticate.
- Can be omitted if environment variable ORACLE_PASS is set.
required: False
default: manager
oracle_sid:
description:
- SID to connect to
required: False
default: None
oracle_service:
description:
- Service name to connect to
required: False
default: None
notes:
- Requires cx_Oracle
author: "Thomas Krahn (@nosmoht)"
'''
EXAMPLES = '''
- name: Ensure Oracle role
oracle_role:
name: APP_ROLE
roles:
- PLUSTRACE
sys_privs:
- CONNECT
oracle_host: db.example.com
oracle_port: 1523
oracle_user: system
oracle_pass: manager
oracle_sid: ORCL
'''
try:
import cx_Oracle
oracleclient_found = True
except ImportError:
oracleclient_found = False
def map_mode(mode):
if mode == 'SYSDBA':
return cx_Oracle.SYSDBA
elif mode == 'SYSOPER':
return cx_Oracle.SYSOPER
else:
return None
def create_connection(module, user, password, host, port, sid=None, service=None, mode=None):
if sid:
dsn = cx_Oracle.makedsn(host=host, port=port, sid=sid)
else:
dsn = cx_Oracle.makedsn(host=host, port=port, service_name=service)
try:
if mode:
conn = cx_Oracle.connect(
user=user, password=password, dsn=dsn, mode=map_mode(mode))
else:
conn = cx_Oracle.connect(user=user, password=password, dsn=dsn)
return conn
except cx_Oracle.DatabaseError as e:
module.fail_json(msg='{dsn}: {err}'.format(dsn=dsn, err=str(e)))
def execute_sql(module, conn, sql):
cur = conn.cursor()
try:
cur.execute(sql)
except cx_Oracle.DatabaseError as e:
module.fail_json(msg='{sql}: {err}'.format(sql=sql, err=e))
cur.close()
def get_role(module, conn, name):
cur = conn.cursor()
try:
sql = 'SELECT role, password_required FROM DBA_ROLES WHERE role = :name'
cur.prepare(sql)
cur.execute(None, dict(name=name))
row = cur.fetchone()
if not row:
return None
except cx_Oracle.DatabaseError as e:
module.fail_json(msg='{sql}: {err}'.format(sql=sql, err=str(e)))
data = dict()
data['name'] = row[0]
data['password_required'] = row[1]
try:
sql = 'SELECT privilege FROM DBA_SYS_PRIVS WHERE grantee = :name'
cur.prepare(sql)
cur.execute(None, dict(name=name))
row = cur.fetchall()
except cx_Oracle.DatabaseError as e:
module.fail_json(msg='{sql}: {err}'.format(sql=sql, err=str(e)))
data['sys_privs'] = [item[0] for item in row]
try:
sql = 'SELECT granted_role FROM DBA_ROLE_PRIVS WHERE grantee = :name'
cur.prepare(sql)
cur.execute(None, dict(name=name))
row = cur.fetchall()
except cx_Oracle.DatabaseError as e:
module.fail_json(msg='{sql}: {err}'.format(sql=sql, err=str(e)))
data['roles'] = [item[0] for item in row]
cur.close()
return data
def get_create_role_sql(name, password_required=None):
sql = 'CREATE ROLE {name}'.format(name=name)
# if password_required:
# sql='{sql}'
return sql
def get_drop_role_sql(name):
sql = 'DROP ROLE {name}'.format(name=name)
return sql
def get_privilege_sql(action, name, priv):
from_to = 'FROM' if action == 'REVOKE' else 'TO'
sql = '{action} {priv} {from_to} {name}'.format(
action=action, priv=priv, from_to=from_to, name=name)
return sql
def get_grant_privilege_sql(name, priv):
return get_privilege_sql(action='GRANT', priv=priv, name=name)
def get_revoke_privilege_sql(name, priv):
return get_privilege_sql(action='REVOKE', priv=priv, name=name)
def ensure(module, conn):
name = module.params['name'].upper()
if module.params['roles'] is not None:
roles = [item.upper() for item in module.params['roles']]
else:
roles = None
state = module.params['state']
if module.params['sys_privs'] is not None:
sys_privs = [item.upper() for item in module.params['sys_privs']]
else:
sys_privs = None
role = get_role(module, conn, name)
sql = list()
if not role and state != 'absent':
sql.append(get_create_role_sql(name=name))
elif role and state == 'absent':
sql.append(get_drop_role_sql(name=name))
if state != 'absent':
# Roles
if roles is not None:
roles_to_grant = list(
set(roles) - set(role.get('roles') if role else list()))
for item in roles_to_grant:
sql.append(get_grant_privilege_sql(priv=item, name=name))
roles_to_revoke = list(
set(role.get('roles') if role else list()) - set(roles))
for item in roles_to_revoke:
sql.append(get_revoke_privilege_sql(priv=item, name=name))
# System privileges
if sys_privs is not None:
privs_to_grant = list(
set(sys_privs) - set(role.get('sys_privs') if role else list()))
for item in privs_to_grant:
sql.append(get_grant_privilege_sql(priv=item, name=name))
privs_to_revoke = list(
set(role.get('sys_privs') if role else list()) - set(sys_privs))
for item in privs_to_revoke:
sql.append(get_revoke_privilege_sql(priv=item, name=name))
if len(sql) > 0:
if module.check_mode:
module.exit_json(changed=True, sql=sql, role=role)
for stmt in sql:
execute_sql(module, conn, stmt)
return True, get_role(module, conn, name=name), sql
return False, role, sql
def main():
module = AnsibleModule(
argument_spec=dict(
name=dict(type='str', required=True),
roles=dict(type='list', default=None),
state=dict(type='str', default='present',
choices=['present', 'absent']),
sys_privs=dict(type='list', default=None),
oracle_host=dict(type='str', default='127.0.0.1'),
oracle_port=dict(type='str', default='1521'),
oracle_user=dict(type='str', default='SYSTEM'),
oracle_mode=dict(type='str', required=None, default=None, choices=[
'SYSDBA', 'SYSOPER']),
oracle_pass=dict(type='str', default=None, no_log=True),
oracle_sid=dict(type='str', default=None),
oracle_service=dict(type='str', default=None),
),
required_one_of=[['oracle_sid', 'oracle_service']],
mutually_exclusive=[['oracle_sid', 'oracle_service']],
supports_check_mode=True,
)
if not oracleclient_found:
module.fail_json(
msg='cx_Oracle not found. Needs to be installed. See http://cx-oracle.sourceforge.net/')
oracle_host = module.params['oracle_host']
oracle_port = module.params['oracle_port']
oracle_user = module.params['oracle_user']
oracle_pass = module.params['oracle_pass'] or os.environ['ORACLE_PASS']
oracle_mode = module.params['oracle_mode']
oracle_sid = module.params['oracle_sid']
oracle_service = module.params['oracle_service']
conn = create_connection(module=module,
user=oracle_user, password=oracle_pass, mode=oracle_mode,
host=oracle_host, port=oracle_port,
sid=oracle_sid, service=oracle_service)
changed, role, sql = ensure(module, conn)
module.exit_json(changed=changed, role=role, sql=sql)
# import module snippets
from ansible.module_utils.basic import *
if __name__ == '__main__':
main()