Skip to content

Commit 55e970e

Browse files
committed
Drop the databases and users fields
We follow the names used by postgresql (https://www.postgresql.org/docs/current/auth-pg-hba-conf.html). Only the single variant is kept. It is the responsibility of the users of the library to split the values if they want lists. The serialized argument for `as_dict` is removed. This is breaking change!
1 parent 5c45afe commit 55e970e

File tree

2 files changed

+20
-71
lines changed

2 files changed

+20
-71
lines changed

pgtoolkit/hba.py

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,13 @@ class HBARecord:
9090
"""Holds a HBA record composed of fields and a comment.
9191
9292
Common fields are accessible through attribute : ``conntype``,
93-
``databases``, ``users``, ``address``, ``netmask``, ``method``.
93+
``database``, ``user``, ``address``, ``netmask``, ``method``.
9494
Auth-options fields are also accessible through attribute like ``map``,
9595
``ldapserver``, etc.
9696
9797
``address`` and ``netmask`` fields are not always defined. If not,
9898
accessing undefined attributes trigger an :exc:`AttributeError`.
9999
100-
``databases`` and ``users`` have a single value variant respectively
101-
:attr:`database` and :attr:`user`, computed after the list representation
102-
of the field.
103-
104100
.. automethod:: parse
105101
.. automethod:: __init__
106102
.. automethod:: __str__
@@ -112,8 +108,8 @@ class HBARecord:
112108

113109
COMMON_FIELDS = [
114110
"conntype",
115-
"databases",
116-
"users",
111+
"database",
112+
"user",
117113
"address",
118114
"netmask",
119115
"method",
@@ -137,7 +133,7 @@ def parse(cls, line: str) -> HBARecord:
137133
138134
"""
139135
line = line.strip()
140-
record_fields = ["conntype", "databases", "users"]
136+
record_fields = ["conntype", "database", "user"]
141137

142138
# What the regexp below does is finding all elements separated by spaces
143139
# unless they are enclosed in double-quotes
@@ -146,9 +142,7 @@ def parse(cls, line: str) -> HBARecord:
146142
# double-quotes (alternative 1)
147143
# \S = any non-whitespace character (alternative 2)
148144
values = [p for p in re.findall(r"(?:\"+.*?\"+|\S)+", line) if p.strip()]
149-
# Split databases and users lists.
150-
values[1] = values[1].split(",")
151-
values[2] = values[2].split(",")
145+
assert len(values) > 2
152146
try:
153147
hash_pos = values.index("#")
154148
except ValueError:
@@ -173,8 +167,8 @@ def parse(cls, line: str) -> HBARecord:
173167
return cls(options, comment=comment)
174168

175169
conntype: str | None
176-
databases: list[str]
177-
users: list[str]
170+
database: str
171+
user: str
178172

179173
def __init__(
180174
self,
@@ -188,10 +182,6 @@ def __init__(
188182
:param comment: Comment at the end of the line.
189183
"""
190184
dict_values: dict[str, Any] = dict(values or {}, **kw_values)
191-
if "database" in dict_values:
192-
dict_values["databases"] = [dict_values.pop("database")]
193-
if "user" in dict_values:
194-
dict_values["users"] = [dict_values.pop("user")]
195185
self.__dict__.update(dict_values)
196186
self.fields = [k for k, _ in dict_values.items()]
197187
self.comment = comment
@@ -223,9 +213,7 @@ def __str__(self) -> str:
223213
fmt += "%%(%s)-%ds " % (field, width - 1)
224214
else:
225215
fmt += f"%({field})s "
226-
# Serialize database and user list using property.
227-
values = dict(self.__dict__, databases=self.database, users=self.user)
228-
line = fmt.rstrip() % values
216+
line = fmt.rstrip() % self.__dict__
229217

230218
auth_options = ['%s="%s"' % i for i in self.auth_options]
231219
if auth_options:
@@ -241,17 +229,13 @@ def __str__(self) -> str:
241229
def __eq__(self, other: object) -> bool:
242230
return str(self) == str(other)
243231

244-
def as_dict(self, serialized: bool = False) -> dict[str, Any]:
232+
def as_dict(self) -> dict[str, Any]:
245233
str_fields = self.COMMON_FIELDS[:]
246-
if serialized:
247-
str_fields[1:3] = ["database", "user"]
248234
return {f: getattr(self, f) for f in str_fields if hasattr(self, f)}
249235

250236
@property
251237
def common_values(self) -> list[str]:
252238
str_fields = self.COMMON_FIELDS[:]
253-
# Use serialized variant.
254-
str_fields[1:3] = ["database", "user"]
255239
return [getattr(self, f) for f in str_fields if f in self.fields]
256240

257241
@property
@@ -260,26 +244,6 @@ def auth_options(self) -> list[tuple[str, str]]:
260244
(f, getattr(self, f)) for f in self.fields if f not in self.COMMON_FIELDS
261245
]
262246

263-
@property
264-
def database(self) -> str:
265-
"""Hold database column as a single value.
266-
267-
Use `databases` attribute to get parsed database list. `database` is
268-
guaranteed to be a string.
269-
270-
"""
271-
return ",".join(self.databases)
272-
273-
@property
274-
def user(self) -> str:
275-
"""Hold user column as a single value.
276-
277-
Use ``users`` property to get parsed user list. ``user`` is guaranteed
278-
to be a string.
279-
280-
"""
281-
return ",".join(self.users)
282-
283247
def matches(self, **attrs: str) -> bool:
284248
"""Tells if the current record is matching provided attributes.
285249

tests/test_hba.py

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ def test_parse_host_line():
4040
assert "host" in repr(record)
4141
assert "host" == record.conntype
4242
assert "replication" == record.database
43-
assert ["replication"] == record.databases
4443
assert "all" == record.user
45-
assert ["all"] == record.users
44+
assert "all" == record.user
4645
assert "::1/128" == record.address
4746
assert "trust" == record.method
4847

@@ -57,7 +56,7 @@ def test_parse_local_line():
5756
record = HBARecord.parse("local all all trust")
5857
assert "local" == record.conntype
5958
assert "all" == record.database
60-
assert ["all"] == record.users
59+
assert "all" == record.user
6160
assert "trust" == record.method
6261

6362
with pytest.raises(AttributeError):
@@ -77,7 +76,7 @@ def test_parse_auth_option():
7776
)
7877
assert "local" == record.conntype
7978
assert "veryverylongdatabasenamethatdonotfit" == record.database
80-
assert ["all"] == record.users
79+
assert "all" == record.user
8180
assert "ident" == record.method
8281
assert "omicron" == record.map
8382

@@ -97,7 +96,7 @@ def test_parse_record_with_comment():
9796
record = HBARecord.parse("local all all trust # My comment")
9897
assert "local" == record.conntype
9998
assert "all" == record.database
100-
assert ["all"] == record.users
99+
assert "all" == record.user
101100
assert "trust" == record.method
102101
assert "My comment" == record.comment
103102

@@ -172,7 +171,7 @@ def test_hba_create():
172171
assert 2 == len(hba.lines)
173172

174173
r = hba.lines[1]
175-
assert ["all"] == r.databases
174+
assert "all" == r.database
176175

177176
# Should be a list
178177
with pytest.raises(ValueError):
@@ -335,8 +334,8 @@ def r(hba):
335334
other_hba = HBA()
336335
record = HBARecord(
337336
conntype="host",
338-
databases=["replication"],
339-
users=["all"],
337+
database="replication",
338+
user="all",
340339
address="1.2.3.4",
341340
method="trust",
342341
)
@@ -355,12 +354,6 @@ def test_as_dict():
355354
method="trust",
356355
)
357356
assert r.as_dict() == {
358-
"conntype": "local",
359-
"databases": ["all"],
360-
"users": ["all"],
361-
"method": "trust",
362-
}
363-
assert r.as_dict(serialized=True) == {
364357
"conntype": "local",
365358
"database": "all",
366359
"user": "all",
@@ -369,21 +362,13 @@ def test_as_dict():
369362

370363
r = HBARecord(
371364
conntype="local",
372-
databases=["mydb", "mydb2"],
373-
users=["bob", "alice"],
365+
database="mydb,mydb2",
366+
user="bob,alice",
374367
address="127.0.0.1",
375368
netmask="255.255.255.255",
376369
method="trust",
377370
)
378371
assert r.as_dict() == {
379-
"address": "127.0.0.1",
380-
"conntype": "local",
381-
"databases": ["mydb", "mydb2"],
382-
"users": ["bob", "alice"],
383-
"method": "trust",
384-
"netmask": "255.255.255.255",
385-
}
386-
assert r.as_dict(serialized=True) == {
387372
"address": "127.0.0.1",
388373
"conntype": "local",
389374
"database": "mydb,mydb2",
@@ -407,8 +392,8 @@ def test_hbarecord_equality():
407392

408393
r = HBARecord(
409394
conntype="host",
410-
databases=["all"],
411-
users=["u0", "u1"],
395+
database="all",
396+
user="u0,u1",
412397
address="127.0.0.1/32",
413398
method="trust",
414399
)

0 commit comments

Comments
 (0)