Skip to content

Commit 5ff09c5

Browse files
authored
Fix 777 (#779)
fixes #777 (#779)
1 parent bc96c50 commit 5ff09c5

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- "postgresql_pg_hba - fixes #777 the module will ignore the 'address' and 'netmask' options again when the contype is 'local' (https://github.com/ansible-collections/community.postgresql/pull/779)"

plugins/modules/postgresql_pg_hba.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,12 +1084,7 @@ def _from_rule_dict(self, rule_dict):
10841084
if self._address_type.startswith("IP") and self._prefix_len == -1:
10851085
raise PgHbaRuleError("If the address is a bare ip-address without a CIDR suffix, "
10861086
"the rule needs to contain a netmask")
1087-
1088-
# if the contype is "local", the rule can't contain an address or netmask
1089-
else:
1090-
if (("address" in rule_dict and rule_dict["address"])
1091-
or ("netmask" in rule_dict and rule_dict["netmask"])):
1092-
raise PgHbaRuleError("Rule can't contain an address and netmask if the connection-type is 'local'")
1087+
# we ignore address / netmask when contype is 'local'
10931088

10941089
# verify the method
10951090
self._auth_method = _strip_quotes(rule_dict["method"])

tests/integration/targets/postgresql_pg_hba/tasks/postgresql_pg_hba_initial.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,29 @@
253253
that:
254254
- '"#comment1\nhost\tall\tall\t2001:db8::1/128\tmd5\nhost\tall\tall\t2001:db8::2/128\tmd5\t#comment2\nhost\tall\tall\t2001:db8::3/128\tmd5\t#comment3" == content'
255255

256+
- community.postgresql.postgresql_pg_hba:
257+
dest: /tmp/pg_hba3.conf
258+
contype: local
259+
method: trust
260+
state: present
261+
create: true
262+
register: local_with_address
263+
264+
- assert:
265+
that: 'local_with_address.pg_hba == [{"db": "all", "method": "trust", "type": "local", "usr": "all"}]'
266+
267+
- community.postgresql.postgresql_pg_hba:
268+
dest: /tmp/pg_hba3.conf
269+
contype: local
270+
method: trust
271+
address: 127.0.0.0
272+
netmask: 255.0.0.0
273+
state: present
274+
register: local_with_address
275+
276+
- assert:
277+
that: 'local_with_address.pg_hba == [{"db": "all", "method": "trust", "type": "local", "usr": "all"}]'
278+
256279
- community.postgresql.postgresql_pg_hba:
257280
dest: pg_hba.conf
258281
users: '{ "oh": "no" }'

tests/unit/plugins/modules/test_postgresql_pg_hba.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,20 +195,18 @@ def test_rule_validation_from_dict():
195195

196196
d = copy.copy(base_dict)
197197
d['address'] = '127.0.0.1/32'
198-
with pytest.raises(PgHbaRuleError,
199-
match="Rule can't contain an address and netmask if the connection-type is 'local'"):
200-
PgHbaRule(rule_dict=d)
198+
assert not PgHbaRule(rule_dict=d).address
199+
201200
d = copy.copy(base_dict)
202-
d['address'] = '255.255.255.255'
203-
with pytest.raises(PgHbaRuleError,
204-
match="Rule can't contain an address and netmask if the connection-type is 'local'"):
205-
PgHbaRule(rule_dict=d)
201+
d['netmask'] = '255.255.255.255'
202+
assert not PgHbaRule(rule_dict=d).netmask
203+
206204
d = copy.copy(base_dict)
207205
d['address'] = '127.0.0.1/32'
208206
d['address'] = '255.255.255.255'
209-
with pytest.raises(PgHbaRuleError,
210-
match="Rule can't contain an address and netmask if the connection-type is 'local'"):
211-
PgHbaRule(rule_dict=d)
207+
rule = PgHbaRule(rule_dict=d)
208+
assert (not rule.address) and (not rule.netmask)
209+
212210
base_dict['contype'] = 'host'
213211
with pytest.raises(PgHbaRuleError, match="If the contype isn't 'local', the rule needs to contain an address"):
214212
PgHbaRule(rule_dict=base_dict)

0 commit comments

Comments
 (0)