Bug 52925 - TypeError while Kelvin tries to generate the username
Summary: TypeError while Kelvin tries to generate the username
Status: CLOSED FIXED
Alias: None
Product: UCS@school
Classification: Unclassified
Component: HTTP-API (Kelvin)
Version: UCS@school 4.4
Hardware: Other Linux
: P5 normal
Target Milestone: ---
Assignee: Toni Röhmeyer
QA Contact: Tobias Wenzel
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-03-16 16:48 CET by Jan-Luca Kiok
Modified: 2021-05-26 09:26 CEST (History)
8 users (show)

See Also:
What kind of report is it?: Bug Report
What type of bug is this?: 3: Simply Wrong: The implementation doesn't match the docu
Who will be affected by this bug?: 3: Will affect average number of installed domains
How will those affected feel about the bug?: 3: A User would likely not purchase the product
User Pain: 0.154
Enterprise Customer affected?:
School Customer affected?: Yes
ISV affected?:
Waiting Support:
Flags outvoted (downgraded) after PO Review:
Ticket number: 2021031521000389
Bug group (optional):
Customer ID: 44145
Max CVSS v3 score:


Attachments
Configuration of kelvin import (1.22 KB, application/json)
2021-03-16 16:48 CET, Jan-Luca Kiok
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jan-Luca Kiok univentionstaff 2021-03-16 16:48:37 CET
Created attachment 10648 [details]
Configuration of kelvin import

When POSTing a user via Kelvin without a name, but a valid template provided, Code 500 is returned and a TypeError occurs while generating the username:

Request:

{
    "school": "https://example.com/ucsschool/kelvin/v1/schools/dummy",
    "firstname": "Klaftan",
    "lastname": "Stringulinski",
    "record_uid": "stringulinski3000",
    "roles": [
      "https://example.com/ucsschool/kelvin/v1/roles/teacher" 
    ],
    "schools": [
      "https://example.com/ucsschool/kelvin/v1/schools/dummy" 
    ]
}


Error:

2021-03-15 14:20:28 ERROR Exception in ASGI application
Traceback (most recent call last):
  File "/kelvin/venv/lib/python3.8/site-packages/uvicorn/protocols/http/h11_impl.py", line 389, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/kelvin/venv/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "/kelvin/venv/lib/python3.8/site-packages/fastapi/applications.py", line 140, in __call__
    await super().__call__(scope, receive, send)
  File "/kelvin/venv/lib/python3.8/site-packages/starlette/applications.py", line 134, in __call__
    await self.error_middleware(scope, receive, send)
  File "/kelvin/venv/lib/python3.8/site-packages/starlette/middleware/errors.py", line 178, in __call__
    raise exc from None
  File "/kelvin/venv/lib/python3.8/site-packages/starlette/middleware/errors.py", line 156, in __call__
    await self.app(scope, receive, _send)
  File "/kelvin/venv/lib/python3.8/site-packages/starlette/exceptions.py", line 73, in __call__
    raise exc from None
  File "/kelvin/venv/lib/python3.8/site-packages/starlette/exceptions.py", line 62, in __call__
    await self.app(scope, receive, sender)
  File "/kelvin/venv/lib/python3.8/site-packages/starlette/routing.py", line 590, in __call__
    await route(scope, receive, send)
  File "/kelvin/venv/lib/python3.8/site-packages/starlette/routing.py", line 208, in __call__
    await self.app(scope, receive, send)
  File "/kelvin/venv/lib/python3.8/site-packages/starlette/routing.py", line 41, in app
    response = await func(request)
  File "/kelvin/venv/lib/python3.8/site-packages/fastapi/routing.py", line 127, in app
    raw_response = await dependant.call(**values)
  File "/kelvin/kelvin-api/ucsschool/kelvin/routers/user.py", line 653, in create
    user = await user_importer.determine_add_modify_action(user)
  File "/kelvin/ucs-school-import/modules/ucsschool/importer/mass_import/user_import.py", line 273, in determine_add_modify_action
    return self.prepare_imported_user(imported_user, None)
  File "/kelvin/ucs-school-import/modules/ucsschool/importer/mass_import/user_import.py", line 252, in prepare_imported_user
    imported_user.prepare_all(new_user=not old_user)
  File "/kelvin/ucs-school-import/modules/ucsschool/importer/models/import_user.py", line 530, in prepare_all
    self.prepare_attributes(new_user)
  File "/kelvin/ucs-school-import/modules/ucsschool/importer/models/import_user.py", line 544, in prepare_attributes
    self.make_username()
  File "/kelvin/ucs-school-import/modules/ucsschool/importer/models/import_user.py", line 864, in make_username
    self.name = self.format_from_scheme("username", self.username_scheme)
  File "/kelvin/ucs-school-import/modules/ucsschool/importer/models/import_user.py", line 1300, in format_from_scheme
    res: Union[bytes, str] = self.prop._replace(scheme, all_fields)
  File "/kelvin/venv/lib/python3.8/site-packages/univention/admin/__init__.py", line 279, in _replace
    return pattern_replace(copy.copy(res), object)
  File "/kelvin/venv/lib/python3.8/site-packages/univention/admin/__init__.py", line 114, in pattern_replace
    value = modify_text(value, global_commands)
  File "/kelvin/venv/lib/python3.8/site-packages/univention/admin/__init__.py", line 62, in modify_text
    text = text.replace(umlaut, code)
TypeError: a bytes-like object is required, not 'str'
Comment 2 Dirk Schnick univentionstaff 2021-03-17 08:06:33 CET
Same problem with kelvin 1.2 in my test environment. Using the example of the documentation regarding create fails with that traceback.
Comment 4 Toni Röhmeyer univentionstaff 2021-05-07 13:33:26 CEST
QA should check the following:

on single- AND multi-server environment:
- test manually without specified name (as shown in bug description)
- test manually with specified name (with and without umlauts)
- execute kelvin-api/tests/test_route_user.py::test_create_without_username
- also check the other kelvin-api/tests
- build docker container
- code review
Comment 5 Toni Röhmeyer univentionstaff 2021-05-18 12:20:08 CEST
Fix implemented on twenzel/kelvin/kelvin_type_error with commits

7da737306 Bug #52925: fixed create and modify (needed bytestring list)
27239aca4 Bug #52878: migrate changes from ucs-branch


Changes in the username_handler had to be made due to fixing the initial problem revealed another bug causing a TypeError in Tuple_to_LDAPMod().


When reproducing the error the given kelvin.json in the bug description must be adjusted (mapped_udm_properties, maildomain...)

Behavior can be tested with 
kelvin-api/tests/test_route_user.py::test_create_without_username
Comment 6 Tobias Wenzel univentionstaff 2021-05-18 14:21:20 CEST
QA → leaving resolved for code review of

27239aca4 Bug #52878: migrate changes from ucs-branch

After that:

→ merge
→ create new app version
→ build for test appcenter


on single- AND multi-server environment:
- test manually without specified name (as shown in bug description)
- test manually with specified name (with and without umlauts)
OK execute kelvin-api/tests/test_route_user.py::test_create_without_username
DOING also check the other kelvin-api/tests
OK build docker container
OK code review
Comment 7 Tobias Wenzel univentionstaff 2021-05-19 08:49:47 CEST
DOING also check the other kelvin-api/tests
→ this was already DONE, the tests pass on my vm
Comment 8 Daniel Tröder univentionstaff 2021-05-22 00:34:10 CEST
The Docker images was build and push to the registry.

docker-upload.software-univention.de/ucsschool-kelvin-rest-api:1.4.2 (f88b30fadc14)

The apps ini for version 1.4.2 was adapted to use that image.
Comment 9 Tobias Wenzel univentionstaff 2021-05-25 11:25:50 CEST
QA -> All OK -> Verify

OK Tests pass in Jenkins 
OK Container was built
OK Merge without conflicts
OK Changelog entries
Comment 10 Tobias Wenzel univentionstaff 2021-05-26 09:26:29 CEST
UCS@school Kelvin REST API 1.4.2 has been released.

http://appcenter.software-univention.de/univention-repository/4.4/maintained/component/ucsschool-kelvin-rest-api_20210518142444/README_UPDATE_DE

If this error occurs again, please clone this bug.