Skip to content

Commit 9b6f86a

Browse files
authored
Merge pull request #181 from EvaSDK/handle-pg_ident-conf
Add support for configure pg_ident.conf
2 parents 8031f74 + f048ab8 commit 9b6f86a

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

pillar.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,14 @@ postgres:
5454
# If ``acls`` item value is empty ('', [], null), then the contents of
5555
# ``pg_hba.conf`` file will not be touched at all.
5656
acls:
57+
- ['local', 'db0', 'connuser', 'peer map=users_as_appuser']
5758
- ['local', 'db1', 'localUser']
5859
- ['host', 'db2', 'remoteUser', '192.168.33.0/24']
5960

61+
identity_map:
62+
- ['users_as_appuser', 'jdoe', 'connuser']
63+
- ['users_as_appuser', 'jsmith', 'connuser']
64+
6065
# Backup extension for configuration files, defaults to ``.bak``.
6166
# Set ``False`` to stop creation of backups when config files change.
6267
{%- if salt['status.time']|default(none) is callable %}

postgres/defaults.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ postgres:
4747
# IPv6 local connections:
4848
- ['host', 'all', 'all', '::1/128', 'md5']
4949

50+
pg_ident.conf: salt://postgres/templates/pg_ident.conf.j2
51+
identity_map: []
52+
5053
config_backup: '.bak'
5154

5255
service: postgresql

postgres/server/init.sls

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,33 @@ postgresql-pg_hba:
129129
- require:
130130
- file: postgresql-config-dir
131131
132+
{%- set pg_ident_path = salt['file.join'](postgres.conf_dir, 'pg_ident.conf') %}
133+
134+
postgresql-pg_ident:
135+
file.managed:
136+
- name: {{ pg_ident_path }}
137+
- user: {{ postgres.user }}
138+
- group: {{ postgres.group }}
139+
- mode: 600
140+
{%- if postgres.identity_map %}
141+
- source: {{ postgres['pg_ident.conf'] }}
142+
- template: jinja
143+
- defaults:
144+
mappings: {{ postgres.identity_map }}
145+
{%- if postgres.config_backup %}
146+
# Create the empty file before managing to overcome the limitation of check_cmd
147+
- onlyif: test -f {{ pg_ident_path }} || touch {{ pg_ident_path }}
148+
# Make a local backup before the file modification
149+
- check_cmd: >-
150+
salt-call --local file.copy
151+
{{ pg_ident_path }} {{ pg_ident_path ~ postgres.config_backup }} remove_existing=true
152+
{%- endif %}
153+
{%- else %}
154+
- replace: False
155+
{%- endif %}
156+
- require:
157+
- file: postgresql-config-dir
158+
132159
{%- for name, tblspace in postgres.tablespaces|dictsort() %}
133160
134161
postgresql-tablespace-dir-{{ name }}:
@@ -158,5 +185,6 @@ postgresql-running:
158185
{% endif %}
159186
- watch:
160187
- file: postgresql-pg_hba
188+
- file: postgresql-pg_ident
161189
162190
{%- endif %}

postgres/templates/pg_ident.conf.j2

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
######################################################################
2+
# ATTENTION! Managed by SaltStack. #
3+
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN! #
4+
######################################################################
5+
#
6+
# PostgreSQL User Name Maps
7+
# =========================
8+
#
9+
# Refer to the PostgreSQL documentation, chapter "Client
10+
# Authentication" for a complete description. A short synopsis
11+
# follows.
12+
#
13+
# This file controls PostgreSQL user name mapping. It maps external
14+
# user names to their corresponding PostgreSQL user names. Records
15+
# are of the form:
16+
#
17+
# MAPNAME SYSTEM-USERNAME PG-USERNAME
18+
#
19+
# (The uppercase quantities must be replaced by actual values.)
20+
#
21+
# MAPNAME is the (otherwise freely chosen) map name that was used in
22+
# pg_hba.conf. SYSTEM-USERNAME is the detected user name of the
23+
# client. PG-USERNAME is the requested PostgreSQL user name. The
24+
# existence of a record specifies that SYSTEM-USERNAME may connect as
25+
# PG-USERNAME.
26+
#
27+
# If SYSTEM-USERNAME starts with a slash (/), it will be treated as a
28+
# regular expression. Optionally this can contain a capture (a
29+
# parenthesized subexpression). The substring matching the capture
30+
# will be substituted for \1 (backslash-one) if present in
31+
# PG-USERNAME.
32+
#
33+
# Multiple maps may be specified in this file and used by pg_hba.conf.
34+
#
35+
# No map names are defined in the default configuration. If all
36+
# system user names and PostgreSQL user names are the same, you don't
37+
# need anything in this file.
38+
#
39+
# This file is read on server startup and when the postmaster receives
40+
# a SIGHUP signal. If you edit the file on a running system, you have
41+
# to SIGHUP the postmaster for the changes to take effect. You can
42+
# use "pg_ctl reload" to do that.
43+
44+
# Put your actual configuration here
45+
# ----------------------------------
46+
47+
# MAPNAME SYSTEM-USERNAME PG-USERNAME
48+
49+
{%- for mapping in mappings %}
50+
{{ '{0:<15} {1:<22} {2}'.format(mapping) -}}
51+
{% endfor %}

0 commit comments

Comments
 (0)