MCPcopy Create free account
hub / github.com/Keeper-Security/Commander / rotate

Method rotate

keepercommander/plugins/postgresql/postgresql.py:42–66  ·  view source on GitHub ↗

Rotate a PostgreSQL password

(self, record, new_password, revert=False)

Source from the content-addressed store, hash-verified

40 self.rotate(record, new_password, revert=True)
41
42 def rotate(self, record, new_password, revert=False):
43 """Rotate a PostgreSQL password"""
44 if revert:
45 old_password = new_password
46 new_password = self.password
47 else:
48 old_password = self.password
49
50 try:
51 with psycopg2.connect(host=self.host, port=self.port, user=self.login, password=old_password,
52 database=self.db) as connection:
53 logging.debug(f'Connected to {self.host}')
54 with connection.cursor() as cursor:
55 sql = psycopg2.sql.SQL('ALTER USER {} WITH PASSWORD %s').format(
56 psycopg2.sql.Identifier(self.login)
57 )
58 cursor.execute(sql, (new_password,))
59 return True
60 except Exception as e:
61 if revert:
62 logging.error('Error reverting password rotation of Postgres server: %s', e)
63 else:
64 logging.error('Error rotating password of Postgres server: %s', e)
65
66 return False

Callers 1

revertMethod · 0.95

Calls 5

formatMethod · 0.80
connectMethod · 0.45
debugMethod · 0.45
executeMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected