324
324
@data_ready_action
325
325
def update_pg_hba_conf():
327
# grab the needed current state
328
config = hookenv.config()
329
rels = context.Relations()
330
path = postgresql.pg_hba_conf_path()
331
with open(path, 'r') as f:
334
# generate the new state
335
pg_hba_content = generate_pg_hba_conf(pg_hba, config, rels)
337
# write out the new state
338
helpers.rewrite(path, pg_hba_content)
341
def generate_pg_hba_conf(pg_hba, config, rels):
326
342
'''Update the pg_hba.conf file (host based authentication).'''
327
343
rules = [] # The ordered list, as tuples.
344
360
# user connect to their matching PostgreSQL user, if it exists.
345
361
add('local', 'all', 'all', 'peer')
347
rels = context.Relations()
349
363
# Peers need replication access as the charm replication user.
351
365
for peer, relinfo in rels.peer.items():
390
404
# as the relation gets its own user to avoid sharing credentials,
391
405
# and logical replication connections will want to specify the
393
for rel in rels['master']:
407
for rel in rels['master'].values():
394
408
for relinfo in rel.values():
395
409
addr = postgresql.addr_to_range(relinfo['private-address'])
396
410
add('host', 'replication',
397
411
postgresql.quote_identifier(rel.local['user']),
398
412
postgresql.quote_identifier(addr),
399
413
'md5', '# {}'.format(relinfo))
400
if 'database' is rel.local:
414
if 'database' in rel.local:
402
416
postgresql.quote_identifier(rel.local['database']),
403
417
postgresql.quote_identifier(rel.local['user']),
405
419
'md5', '# {}'.format(relinfo))
407
421
# External administrative addresses, if specified by the operator.
408
config = hookenv.config()
409
422
for addr in config['admin_addresses'].split(','):
411
add('host', 'all', 'all', postgresql.addr_to_range(addr),
424
add('host', 'all', 'all',
425
postgresql.quote_identifier(postgresql.addr_to_range(addr)),
412
426
'md5', '# admin_addresses config')
414
428
# And anything-goes rules, if specified by the operator.
415
for line in config['extra_pg_auth'].splitlines():
416
add((line, '# extra_pg_auth config'))
429
for line in config['extra_pg_auth'].split(','):
430
add(line + '# extra_pg_auth config')
418
432
# Deny everything else
419
433
add('local', 'all', 'all', 'reject', '# Refuse by default')
420
434
add('host', 'all', 'all', 'all', 'reject', '# Refuse by default')
422
# Load the existing file
423
path = postgresql.pg_hba_conf_path()
424
with open(path, 'r') as f:
427
436
# Strip out the existing juju managed section
428
437
start_mark = '### BEGIN JUJU SETTINGS ###'
429
438
end_mark = '### END JUJU SETTINGS ###'
438
447
rules.insert(0, (start_mark,))
439
448
rules.append((end_mark,))
440
449
pg_hba += '\n' + '\n'.join(' '.join(rule) for rule in rules)
441
helpers.rewrite(path, pg_hba)
444
453
def assemble_postgresql_conf():