Skip to content

Commit 57fedc4

Browse files
authored
Upgrading cx_oracle to oracledb (#134)
1 parent fb8db4c commit 57fedc4

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

SecretsManagerRDSOracleRotationMultiUser/lambda_function.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import json
66
import logging
77
import os
8-
import cx_Oracle
8+
import oracledb
99

1010
logger = logging.getLogger()
1111
logger.setLevel(logging.INFO)
@@ -49,6 +49,9 @@ def lambda_handler(event, context):
4949
KeyError: If the secret json does not contain the expected keys
5050
5151
"""
52+
# Thick client to match full functionality of cx_oracle
53+
oracledb.init_oracle_client()
54+
5255
arn = event['SecretId']
5356
token = event['ClientRequestToken']
5457
step = event['Step']
@@ -234,7 +237,7 @@ def set_secret(service_client, arn, token):
234237
sql_commands = row[0].read().strip(' \n\t').replace("%s" % escaped_current, "%s" % escaped_username)
235238
for sql_command in sql_commands.split('\n'):
236239
cur.execute(sql_command)
237-
except cx_Oracle.DatabaseError:
240+
except oracledb.DatabaseError:
238241
# If we were unable to find any grants skip this type
239242
pass
240243
conn.commit()
@@ -321,7 +324,7 @@ def get_connection(secret_dict):
321324
secret_dict (dict): The Secret Dictionary
322325
323326
Returns:
324-
Connection: The cx_Oracle object if successful. None otherwise
327+
Connection: The oracledb object if successful. None otherwise
325328
326329
Raises:
327330
KeyError: If the secret json does not contain the expected keys
@@ -332,12 +335,12 @@ def get_connection(secret_dict):
332335

333336
# Try to obtain a connection to the db
334337
try:
335-
conn = cx_Oracle.connect(secret_dict['username'],
336-
secret_dict['password'],
337-
secret_dict['host'] + ':' + port + '/' + secret_dict['dbname'])
338+
conn = oracledb.connect(user=secret_dict['username'],
339+
password=secret_dict['password'],
340+
dsn=secret_dict['host'] + ':' + port + '/' + secret_dict['dbname'])
338341
logger.info("Successfully established connection as user '%s' with host: '%s'" % (secret_dict['username'], secret_dict['host']))
339342
return conn
340-
except (cx_Oracle.DatabaseError, cx_Oracle.OperationalError):
343+
except (oracledb.DatabaseError, oracledb.OperationalError):
341344
return None
342345

343346

@@ -551,4 +554,4 @@ def get_connection_params_from_rds_api(master_dict, master_instance_arn):
551554
if master_dict['engine'] in ['oracle-ee', 'oracle-ee-cdb', 'oracle-se2', 'oracle-se2-cdb', 'custom-oracle-ee']:
552555
master_dict['engine'] = 'oracle'
553556

554-
return master_dict
557+
return master_dict

SecretsManagerRDSOracleRotationSingleUser/lambda_function.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import json
66
import logging
77
import os
8-
import cx_Oracle
8+
import oracledb
99

1010
logger = logging.getLogger()
1111
logger.setLevel(logging.INFO)
@@ -44,6 +44,9 @@ def lambda_handler(event, context):
4444
KeyError: If the secret json does not contain the expected keys
4545
4646
"""
47+
# Thick client to match full functionality of cx_oracle
48+
oracledb.init_oracle_client()
49+
4750
arn = event['SecretId']
4851
token = event['ClientRequestToken']
4952
step = event['Step']
@@ -281,7 +284,7 @@ def get_connection(secret_dict):
281284
secret_dict (dict): The Secret Dictionary
282285
283286
Returns:
284-
Connection: The cx_Oracle.Connection object if successful. None otherwise
287+
Connection: The oracledb.Connection object if successful. None otherwise
285288
286289
Raises:
287290
KeyError: If the secret json does not contain the expected keys
@@ -292,12 +295,12 @@ def get_connection(secret_dict):
292295

293296
# Try to obtain a connection to the db
294297
try:
295-
conn = cx_Oracle.connect(secret_dict['username'],
296-
secret_dict['password'],
297-
secret_dict['host'] + ':' + port + '/' + secret_dict['dbname'])
298+
conn = oracledb.connect(user=secret_dict['username'],
299+
password=secret_dict['password'],
300+
dsn=secret_dict['host'] + ':' + port + '/' + secret_dict['dbname'])
298301
logger.info("Successfully established connection as user '%s' with host: '%s'" % (secret_dict['username'], secret_dict['host']))
299302
return conn
300-
except (cx_Oracle.DatabaseError, cx_Oracle.OperationalError):
303+
except (oracledb.DatabaseError, oracledb.OperationalError):
301304
return None
302305

303306

0 commit comments

Comments
 (0)