Bug 50894 - check check username scheme syntax
check check username scheme syntax
Status: CLOSED FIXED
Product: UCS@school
Classification: Unclassified
Component: Import scripts
UCS@school 4.4
Other Linux
: P5 normal (vote)
: UCS@school 4.4 v5-errata
Assigned To: Toni Röhmeyer
Tobias Wenzel
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2020-03-04 13:04 CET by Daniel Tröder
Modified: 2020-06-10 14:36 CEST (History)
2 users (show)

See Also:
What kind of report is it?: Development Internal
What type of bug is this?: ---
Who will be affected by this bug?: ---
How will those affected feel about the bug?: ---
User Pain:
Enterprise Customer affected?:
School Customer affected?:
ISV affected?:
Waiting Support:
Flags outvoted (downgraded) after PO Review:
Ticket number:
Bug group (optional):
Max CVSS v3 score:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Tröder univentionstaff 2020-03-04 13:04:17 CET
Create a config check function that verifies that schemes in the import configuration below {"scheme" (email, record_uid and username→(default, student, ...)) is valid.
Check the following:
* number of opening '<' and closing '>' is equal and in correct order
* '[' and ']' is only allowed for '[\d*:\d*]', '[ALWAYSCOUNTER]' and '[COUNTER2]'
Comment 1 Toni Röhmeyer univentionstaff 2020-03-06 18:06:00 CET
Test function added to checks/default.py

It checks the format for each value in the "scheme" field.

It can be tested by trying to import a manipulated configuration.


The solution was pushed to branch troehmey/bug50894 with commit

commit 07017b8a44c1b40e5a1eaa4117027ab60f05b2c7
Bug #50894: added config check function


Waiting for QA
Comment 2 Tobias Wenzel univentionstaff 2020-04-22 08:59:40 CEST
QA 

[troehmey/bug50894] 07017b8a4 Bug #50894: added config check function

Tested with
UCS: 4.4-4 errata499
UCS@school 4.4 v5

--------------------------------


Code

Why is this a global value?

# If a '[' or a ']' appears in a "scheme" field, it should be in one of these contexts
SCHEME_ALLOWED_OCCURENCES = ("[\d*:\d*]", "[ALWAYSCOUNTER]", "[COUNTER2]")

Unallowed use __of__ symbol '[' or ']'.

Why do you use string?
opening = string.find(value, "<", start)

the same can be done with (if you use this, don't forget to remove the import)
opening = value.find("<", start)

I would remove the '' around the brackets, e.g. '<'.

Maybe use another name like scheme_field 
dictionary

--------------------------------

Functionality

Before change:

Change email-scheme:
"email": "<:umlauts><firstname>.<lastname><:lower>[OUNTER2]@<maildomain>",

leads to -> clara.meyer[ounter2]@wenzel-univention.intranet

After change:

InitialisationError: Unallowed use symbol '[' or ']'.
-> also [cOUNTER2]

<:umlauts><firstname>[:].<lastname><:lower>[COUNTER2]@<maildomain>
-> clara.meyer3@wenzel-univention.intranet

[hi [COUNTER2] -> was allowed since [COUNTER2] is in string.
-> clara.meyer[hi@wenzel-univention.intranet

Resulting from

if any(symbol in value for symbol in ["[", "]"]):
	if not any(element in value for element in SCHEME_ALLOWED_OCCURENCES):
		raise InitialisationError("Unallowed use symbol '[' or ']'.")


Suggestion: First remove the allowed cases from the string and then check if any brackets are remaining.

import re
# ...

scheme_allowed_occurencences 
scheme_allowed_occurencences = ("\[\d*:\d*\]", "\[ALWAYSCOUNTER\]", "\[COUNTER2\]")
rest = re.sub("|".join(scheme_allowed_occurencences), "", value)

if any(symbol in rest for symbol in ["[", "]"]):
	raise InitialisationError("Unallowed use symbol '[' or ']'.")
Comment 3 Tobias Wenzel univentionstaff 2020-04-22 09:03:10 CEST
As discussed also add [\d] to the allowed occurrences
-> https://docs.software-univention.de/ucsschool-import-handbuch-4.4.html#configuration:unique_usernames_and_email
Comment 4 Toni Röhmeyer univentionstaff 2020-04-26 16:30:15 CEST
Issues were resolved with 

commit 21656e985988188082dfde852e404766652787cd
Bug #50894: applied small fixes

on branch troehmey/bug50894.
Comment 5 Tobias Wenzel univentionstaff 2020-04-28 10:30:29 CEST
Thanks for the code changes!

Please Merge&Build

Code -> looks good.
Functionality -> same as before.
Comment 6 Tobias Wenzel univentionstaff 2020-04-28 10:32:01 CEST
Reopen for merge&build
Comment 7 Toni Röhmeyer univentionstaff 2020-04-29 17:07:41 CEST
Feature branch troehmey/bug50894 merged into 4.4 with 

commit d3f7f293309d1ecc27c917f5415d548355090ad4
Bug #50894: added yaml entry

commit ac412b3e752dc019d692e1f48c1e4bcef93f609b
Bug #50894: add changelog entry

commit 865735f94150e9a96bc291fd3d017386053397ec
Bug #50894: Merge branch 'troehmey/bug50894' into 4.4



successful build:

Package: ucs-school-import
Version: 17.0.33A~4.4.0.202004291659
Branch: ucs_4.4-0
Scope: ucs-school-4.4
Comment 8 Tobias Wenzel univentionstaff 2020-04-30 10:28:25 CEST
Please double-check your code after merging:

NameError: global name 'string' is not defined
-> add the missing import string

[4.4] 865735f94 Bug #50894: Merge branch 'troehmey/bug50894' into 4.4
Comment 9 Toni Röhmeyer univentionstaff 2020-04-30 11:42:43 CEST
Issue resolved with commits

commit 175f8c96e71306c714cf8c6731984c5357f1c08f
 Bug #50894: updated yaml

commit df02a6fd9b99d48e2ed330e56932987a8fe47fd6
Bug #51190: added changelog entry

commit 661b733fa3bd61019b40cb619966cd72576e6507
Bug #51190: added import string

on branch 4.4


Successful build:

Package: ucs-school-import
Version: 17.0.34A~4.4.0.202004301134
Branch: ucs_4.4-0
Scope: ucs-school-4.4



Please apologize my mistake.
Comment 10 Tobias Wenzel univentionstaff 2020-04-30 12:12:36 CEST
Of course - No problem. 

Code -> ok
changelog -> ok
yaml -> ok
Functionality -> works like before (tested in 4.4)
Comment 11 Daniel Tröder univentionstaff 2020-05-01 18:36:49 CEST
Sorry to reopen this.
The test function "test_username_handler()" in 113_import_factory_configurability fails in Jenkins:
https://jenkins.knut.univention.de:8181/job/UCSschool-4.4/job/Install%20Singleserver/467/Config=s4,TestGroup=base1,UCSRelease=public/testReport/junit/90_ucsschool/113_import_factory_configurability/master201/

I forgot to mention this before: The UCS@school import supports creating additional COUNTER variables.
The test test_username_handler() in 113_import_factory_configurability tests that functionality.

To fill "scheme_allowed_occurences" in ucs-school-import/.../checks/defaults.py -> DefaultConfigurationChecks.test_scheme_valid_format() don't statically add "[ALWAYSCOUNTER]" and "[COUNTER2]", but use "<UsernameHandler [subclass] instance>.counter_variable_to_function.keys()".

In test_scheme_valid_format() create a UsernameHandler instance using "factory.make_username_handler()". This will make sure possible class overrides (like in the test) will have been activated.
To get a factory instance run "factory = setup_factory(config['factory'])"

factory.make_username_handler(15).counter_variable_to_function.keys()
['[COUNTER2]', '[FOO]', '[ALWAYSCOUNTER]']
Comment 12 Daniel Tröder univentionstaff 2020-05-05 15:03:10 CEST
Sorry - was easier to code than to explain.

[4.4] d44c9829a Bug #50894: include custom username counter variables in schema test
[4.4] e53b76f2b Bug #50894: advisory update
Comment 13 Tobias Wenzel univentionstaff 2020-05-08 10:53:27 CEST
@Daniel thanks for the improvements!

code -> looks even better.
changelog -> ok
yaml -> ok
functionality -> works
test -> passed locally and on jenkins

https://jenkins.knut.univention.de:8181/job/UCSschool-4.4/job/Install%20Singleserver/474/Config=s4,TestGroup=base1,UCSRelease=public/testReport/90_ucsschool/113_import_factory_configurability/
Comment 14 Ole Schwiegert univentionstaff 2020-06-10 14:36:15 CEST
UCS@school 4.4 v5 has been released (errata update to the release).

https://docs.software-univention.de/changelog-ucsschool-4.4v5-de.html

If this error occurs again, please clone this bug.