Skip to content

Commit ecba3d9

Browse files
committed
Allow merging of acls from multiple pillar files
It would be useful to be able to define acls in multiple different pillar files. This is not possible using a list because lists can not be merged. If we use a dict then salt can merge all the acls together. The key name for the lists is only used for sorting the groupings of acls. For backwards compatibility we check to see if postgres:acls is a list and handle it properly.
1 parent 9ece3e9 commit ecba3d9

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

pillar.example

+11-7
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,23 @@ postgres:
3434
# databases they can access. Records take one of these forms:
3535
#
3636
#acls:
37-
# - ['local', 'DATABASE', 'USER', 'METHOD']
38-
# - ['host', 'DATABASE', 'USER', 'ADDRESS', 'METHOD']
39-
# - ['hostssl', 'DATABASE', 'USER', 'ADDRESS', 'METHOD']
40-
# - ['hostnossl', 'DATABASE', 'USER', 'ADDRESS', 'METHOD']
37+
# group:
38+
# - ['local', 'DATABASE', 'USER', 'METHOD']
39+
# - ['host', 'DATABASE', 'USER', 'ADDRESS', 'METHOD']
40+
# - ['hostssl', 'DATABASE', 'USER', 'ADDRESS', 'METHOD']
41+
# - ['hostnossl', 'DATABASE', 'USER', 'ADDRESS', 'METHOD']
4142
#
4243
# The uppercase items must be replaced by actual values.
4344
# METHOD could be omitted, 'md5' will be appended by default.
4445
#
45-
# If ``acls`` item value is empty ('', [], null), then the contents of
46+
# If ``acls`` item value is empty ('', {}, [], null), then the contents of
4647
# ``pg_hba.conf`` file will not be touched at all.
4748
acls:
48-
- ['local', 'db1', 'localUser']
49-
- ['host', 'db2', 'remoteUser', '192.168.33.0/24']
49+
db1:
50+
- ['local', 'db1', 'localUser']
51+
- ['host', 'db1', 'localUser', '127.0.0.1/32']
52+
db2:
53+
- ['host', 'db2', 'remoteUser', '192.168.33.0/24']
5054

5155
# PostgreSQL service name
5256
service: postgresql

postgres/templates/pg_hba.conf.j2

+17-12
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,26 @@ local all postgres peer
2020

2121
# TYPE DATABASE USER ADDRESS METHOD
2222

23-
{%- for acl in acls %}
24-
{%- if acl|first() == 'local' %}
23+
{%- if acls is list %}
24+
{%- set acls = {'_all': acls} %}
25+
{%- endif %}
26+
{%- for _, entry in acls|dictsort %}
27+
{%- for acl in entry %}
28+
{%- if acl|first() == 'local' %}
2529

26-
{%- if acl|length() == 3 %}
27-
{%- do acl.extend(['', 'md5']) %}
28-
{%- elif acl|length() == 4 %}
29-
{%- do acl.insert(3, '') %}
30-
{%- endif %}
30+
{%- if acl|length() == 3 %}
31+
{%- do acl.extend(['', 'md5']) %}
32+
{%- elif acl|length() == 4 %}
33+
{%- do acl.insert(3, '') %}
34+
{%- endif %}
3135

32-
{%- else %}
36+
{%- else %}
3337

34-
{%- if acl|length() == 4 %}
35-
{%- do acl.append('md5') %}
36-
{%- endif %}
38+
{%- if acl|length() == 4 %}
39+
{%- do acl.append('md5') %}
40+
{%- endif %}
3741

38-
{%- endif %}
42+
{%- endif %}
3943
{{ '{0:<7} {1:<15} {2:<15} {3:<23} {4}'.format(*acl) -}}
44+
{%- endfor -%}
4045
{% endfor %}

0 commit comments

Comments
 (0)