Rotate an Oracle database password
(self, record, new_password, revert=False)
| 53 | self.rotate(record, new_password, revert=True) |
| 54 | |
| 55 | def rotate(self, record, new_password, revert=False): |
| 56 | """Rotate an Oracle database password""" |
| 57 | if revert: |
| 58 | old_password = new_password |
| 59 | new_password = self.password |
| 60 | else: |
| 61 | old_password = self.password |
| 62 | |
| 63 | user = self.login |
| 64 | dsn = self.dsn if self.dsn else f'{self.host}/{self.db}' |
| 65 | kwargs = {'user': user, 'password': old_password, 'dsn': dsn} |
| 66 | if self.port: |
| 67 | kwargs['port'] = self.port |
| 68 | |
| 69 | connection = '' |
| 70 | result = False |
| 71 | try: |
| 72 | connection = oracledb.connect(**kwargs) |
| 73 | with connection.cursor() as cursor: |
| 74 | logging.debug(f'Connected to {dsn}') |
| 75 | sql = f'ALTER USER {user} IDENTIFIED BY "{new_password}" ACCOUNT UNLOCK' |
| 76 | cursor.execute(sql) |
| 77 | result = True |
| 78 | except Exception as e: |
| 79 | logging.error(f'Error during connection to Oracle server: {e}') |
| 80 | finally: |
| 81 | if connection: |
| 82 | connection.close() |
| 83 | return result |