Skip to content

Commit ca97a36

Browse files
committed
Nuked superfluous UserId methods
Again, the constructor alone is enough. Change-Id: I8a871b2cc93b252c68fe606431edae5747ebbe72
1 parent 33899db commit ca97a36

File tree

2 files changed

+44
-50
lines changed

2 files changed

+44
-50
lines changed

cmk/utils/user.py

+43-49
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class UserId(str):
1717
"""
1818
A Checkmk user ID
1919
20-
UserIds must comply to a restricted set of allowed characters (see UserId.validate).
20+
UserIds must comply to a restricted set of allowed characters (see UserId.validate_userid).
2121
2222
UserIds must either be compatible with all protocols and components we interface with, or we
2323
must ensure proper encoding whenever UserIds leave Checkmk.
@@ -62,93 +62,96 @@ def __get_pydantic_core_schema__(
6262
cls, source_type: Any, _handler: GetCoreSchemaHandler
6363
) -> core_schema.CoreSchema:
6464
return core_schema.no_info_after_validator_function(
65-
cls.validate_userid,
65+
cls,
6666
core_schema.union_schema(
6767
[core_schema.str_schema(), core_schema.is_instance_schema(cls)]
6868
),
6969
serialization=core_schema.to_string_ser_schema(),
7070
)
7171

72-
@classmethod
73-
def validate(cls, text: str) -> None:
74-
"""Check if it is a valid UserId
72+
def __new__(cls, text: str) -> UserId:
73+
"""Construct a new UserId object
7574
7675
UserIds are used in a variety of contexts, including HTML, file paths and other external
7776
systems. Currently we have no means of ensuring proper output encoding wherever UserIds
7877
are used. Thus, we strictly limit the characters that can be used in a UserId.
7978
8079
See class docstring for an incomplete list of places where UserIds are processed.
8180
82-
Examples:
81+
Raises:
82+
- ValueError: whenever the given text contains special characters.
8383
84+
Examples:
8485
The empty UserId is allowed for historical reasons (see `UserId.builtin`).
8586
86-
>>> UserId.validate("")
87+
>>> UserId("")
88+
''
8789
8890
ASCII letters, digits, and selected special characters are allowed.
8991
90-
>>> UserId.validate("cmkadmin")
91-
>>> UserId.validate("$cmk_@dmün.1")
92+
>>> UserId("cmkadmin")
93+
'cmkadmin'
94+
95+
>>> UserId("$cmk_@dmün.1")
96+
'$cmk_@dmün.1'
9297
9398
Anything considered a letter in Unicode and the dollar sign is allowed.
9499
95-
>>> UserId.validate("cmkadmin")
96-
>>> UserId.validate("$cmkädmin")
97-
>>> UserId.validate("ↄ𝒽ѥ𝕔𖹬-艋く")
100+
>>> UserId("$cmkädmin")
101+
'$cmkädmin'
102+
103+
>>> UserId("ↄ𝒽ѥ𝕔𖹬-艋く")
104+
'ↄ𝒽ѥ𝕔𖹬-艋く'
98105
99106
Emails are allowed
100107
101-
>>> UserId.validate("[email protected]")
102-
>>> UserId.validate("[email protected]")
108+
>>> UserId("[email protected]")
109+
110+
111+
>>> UserId("[email protected]")
112+
103113
104114
Special characters other than '$_-@.' are not allowed (see `USER_ID_REGEX`).
105115
106-
>>> UserId.validate("foo/../")
116+
>>> UserId("foo/../")
107117
Traceback (most recent call last):
108118
...
109-
ValueError: Invalid username: 'foo/../'
119+
ValueError: invalid username: 'foo/../'
110120
111-
>>> UserId.validate("%2F")
121+
>>> UserId("%2F")
112122
Traceback (most recent call last):
113123
...
114-
ValueError: Invalid username: '%2F'
124+
ValueError: invalid username: '%2F'
115125
116126
Some special characters are not allowed at the start.
117127
118-
>>> UserId.validate(".")
128+
>>> UserId(".")
119129
Traceback (most recent call last):
120130
...
121-
ValueError: Invalid username: '.'
131+
ValueError: invalid username: '.'
122132
123-
>>> UserId.validate("@example.com")
133+
>>> UserId("@example.com")
124134
Traceback (most recent call last):
125135
...
126-
ValueError: Invalid username: '@example.com'
136+
ValueError: invalid username: '@example.com'
127137
128138
UserIds must not be longer than 255 bytes.
129139
130-
>>> UserId.validate("𐌈")
131-
>>> UserId.validate(64*"𐌈")
140+
>>> UserId("𐌈")
141+
'𐌈'
142+
143+
>>> UserId(64*"𐌈")
132144
Traceback (most recent call last):
133145
...
134-
ValueError: Username too long: '𐌈𐌈𐌈𐌈𐌈𐌈𐌈𐌈𐌈𐌈𐌈𐌈𐌈𐌈𐌈𐌈…'
146+
ValueError: username too long: '𐌈𐌈𐌈𐌈𐌈𐌈𐌈𐌈𐌈𐌈𐌈𐌈𐌈𐌈𐌈𐌈…'
135147
"""
136-
cls.validate_userid(text)
137-
138-
@classmethod
139-
def validate_userid(cls, text: str) -> str:
140-
if not text:
141-
# see UserId.builtin
142-
return ""
143-
148+
# Ext4 and other file systems allow filenames of up to 255 bytes.
144149
if len(bytes(text, encoding="utf-8")) > 255:
145-
# ext4 and others allow filenames of up to 255 bytes
146-
raise ValueError(f"Username too long: {text[:16] + '…'!r}")
147-
148-
if not cls.USER_ID_REGEX.match(text):
149-
raise ValueError(f"Invalid username: {text!r}")
150-
151-
return text
150+
raise ValueError(f"username too long: {text[:16] + '…'!r}")
151+
# For the empty case, see UserId.builtin().
152+
if text and not cls.USER_ID_REGEX.match(text):
153+
raise ValueError(f"invalid username: {text!r}")
154+
return super().__new__(cls, text)
152155

153156
@classmethod
154157
def builtin(cls) -> UserId:
@@ -161,12 +164,3 @@ def builtin(cls) -> UserId:
161164
with a different meaning.
162165
"""
163166
return UserId("")
164-
165-
def __new__(cls, text: str) -> UserId:
166-
"""Construct a new UserId object
167-
168-
Raises:
169-
- ValueError: whenever the given text contains special characters. See `validate`.
170-
"""
171-
cls.validate(text)
172-
return super().__new__(cls, text)

tests/unit/cmk/test_cmkpasswd.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def raise_err() -> Password:
6565

6666

6767
def test_invalid_user() -> None:
68-
with pytest.raises(InvalidUsernameError, match="Invalid username"):
68+
with pytest.raises(InvalidUsernameError, match="invalid username"):
6969
_run_cmkpasswd("test🔥user", _get_pw(), None)
7070

7171

0 commit comments

Comments
 (0)