Files @ 6f9970a36190
Branch filter:

Location: kallithea/scripts/validate-minimum-dependency-versions

Thomas De Schampheleire
auth_ldap: fix interpretation of LDAP attributes in Python 3

The python-ldap module returns the LDAP attribute names as strings, and the
attribute values as arrays of bytes, e.g. for email:

'mail': [b'john.doe@example.com'],

See https://www.python-ldap.org/en/latest/bytes_mode.html, particularly:
https://www.python-ldap.org/en/latest/bytes_mode.html#what-s-text-and-what-s-bytes

Due to a missing conversion from bytes to unicode for the attribute values
obtained from LDAP, storing the values in a unicode field in the database would
fail. It would apparently either store a repr of the bytes or store them in
some other way.

Upon user login, SQLAlchemy warned about this:

.../sqlalchemy/sql/sqltypes.py:269: SAWarning: Unicode type received non-unicode bind param value b'John'. (this warning may be suppressed after 10 occurrences)
.../sqlalchemy/sql/sqltypes.py:269: SAWarning: Unicode type received non-unicode bind param value b'Doe'. (this warning may be suppressed after 10 occurrences)

In PostgreSQL, this would result in 'weird' values for first name, last
name, and email fields, both in the database and the web UI, e.g.
firstname: \x4a6f686e
lastname: \x446f65
email: \x6a6f686e406578616d706c652e636f6d
These values represent the actual values in hexadecimal, e.g.
\x4a6f686e = 0x4a 0x6f 0x68 0x6e = J o h n

In SQLite, the problem initially shows differently, as an exception in
gravatar_url():

File "_base_root_html", line 207, in render_body

File "_index_html", line 78, in render_header_menu

File "_base_base_html", line 479, in render_menu

File ".../kallithea/lib/helpers.py", line 908, in gravatar_div
gravatar(email_address, cls=cls, size=size)))
File ".../kallithea/lib/helpers.py", line 923, in gravatar
src = gravatar_url(email_address, size * 2)
File ".../kallithea/lib/helpers.py", line 956, in gravatar_url
.replace('{email}', email_address) \
TypeError: replace() argument 2 must be str, not bytes

but nevertheless the root cause of the problem is the same.

Fix the problem by converting the LDAP attributes from bytes to strings.
#!/bin/bash
# Test that installation of all dependencies works fine if versions are set to
# the minimum ones.

set -e

if [ -n "$VIRTUAL_ENV" ]; then
    echo "This script will create its own virtualenv - please don't run it inside an existing one." >&2
    exit 1
fi

cd "$(hg root)"

venv=build/minimum-dependency-versions-venv
log=build/minimum-dependency-versions.log
min_requirements=build/minimum-dependency-versions-requirements.txt
echo "virtualenv: $venv"
echo "log: $log"
echo "minimum requirements file: $min_requirements"

# clean up previous runs
rm -rf "$venv" "$log"
mkdir -p "$venv"

# Make a light weight parsing of setup.py and dev_requirements.txt,
# finding all >= requirements and dumping into a custom requirements.txt
# while fixating the requirement at the lower bound.
sed -n 's/.*"\(.*\)>=\(.*\)".*/\1==\2/p' setup.py > "$min_requirements"
sed 's/>=/==/p' dev_requirements.txt >> "$min_requirements"

python3 -m venv "$venv"
source "$venv/bin/activate"
pip install --upgrade pip setuptools
pip install -e . -r "$min_requirements" python-ldap python-pam 2> >(tee "$log" >&2)

# Treat any message on stderr as a problem, for the caller to interpret.
if [ -s "$log" ]; then
    echo
    echo "Error: pip detected following problems:"
    cat "$log"
    echo
    exit 1
fi

freeze_txt=build/minimum-dependency-versions.txt
pip freeze > $freeze_txt
echo "Installation of minimum packages was successful, providing a set of packages as in $freeze_txt . Now running test suite..."

pytest

echo "Test suite execution was successful."
echo "You can now do additional validation using virtual env '$venv'."