Add a record to pg_hba.conf.
(self, *, type='host', database, user, address=None,
auth_method, auth_options=None)
| 381 | 'cannot modify HBA records: {}'.format(e)) from e |
| 382 | |
| 383 | def add_hba_entry(self, *, type='host', database, user, address=None, |
| 384 | auth_method, auth_options=None): |
| 385 | """Add a record to pg_hba.conf.""" |
| 386 | status = self.get_status() |
| 387 | if status == 'not-initialized': |
| 388 | raise ClusterError( |
| 389 | 'cannot modify HBA records: cluster is not initialized') |
| 390 | |
| 391 | if type not in {'local', 'host', 'hostssl', 'hostnossl'}: |
| 392 | raise ValueError('invalid HBA record type: {!r}'.format(type)) |
| 393 | |
| 394 | pg_hba = os.path.join(self._data_dir, 'pg_hba.conf') |
| 395 | |
| 396 | record = '{} {} {}'.format(type, database, user) |
| 397 | |
| 398 | if type != 'local': |
| 399 | if address is None: |
| 400 | raise ValueError( |
| 401 | '{!r} entry requires a valid address'.format(type)) |
| 402 | else: |
| 403 | record += ' {}'.format(address) |
| 404 | |
| 405 | record += ' {}'.format(auth_method) |
| 406 | |
| 407 | if auth_options is not None: |
| 408 | record += ' ' + ' '.join( |
| 409 | '{}={}'.format(k, v) for k, v in auth_options) |
| 410 | |
| 411 | try: |
| 412 | with open(pg_hba, 'a') as f: |
| 413 | print(record, file=f) |
| 414 | except IOError as e: |
| 415 | raise ClusterError( |
| 416 | 'cannot modify HBA records: {}'.format(e)) from e |
| 417 | |
| 418 | def trust_local_connections(self): |
| 419 | self.reset_hba() |