Skip to content

Commit d5be5e8

Browse files
holdenkFokko Driesprong
authored and
Fokko Driesprong
committedNov 5, 2018
[AIRFLOW-2192] Allow non-latin1 usernames with MySQL backend by adding a SQL_ENGINE_ENCODING param and default to UTF-8 (#4087)
Compromised of: Since we have unicode_literals importred and the engine arguments must be strings in Python2 explicitly make 'utf-8' a string. replace bare exception with conf.AirflowConfigException for missing value. It's just got for strings apparently. Add utf-8 to default_airflow.cfg - question do I still need the try try/except block or can we depend on defaults (I note some have both). Get rid of try/except block and depend on default_airflow.cfg Use __str__ since calling str just gives us back a newstr as well. Test that a panda user can be saved.
1 parent f3d5a70 commit d5be5e8

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed
 

‎airflow/config_templates/default_airflow.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ executor = SequentialExecutor
8787
# their website
8888
sql_alchemy_conn = sqlite:///{AIRFLOW_HOME}/airflow.db
8989

90+
# The encoding for the databases
91+
sql_engine_encoding = utf-8
92+
9093
# If SqlAlchemy should pool database connections.
9194
sql_alchemy_pool_enabled = True
9295

‎airflow/settings.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def configure_orm(disable_connection_pool=False):
153153
engine_args['poolclass'] = NullPool
154154
log.debug("settings.configure_orm(): Using NullPool")
155155
elif 'sqlite' not in SQL_ALCHEMY_CONN:
156-
# Engine args not supported by sqlite.
156+
# Pool size engine args not supported by sqlite.
157157
# If no config value is defined for the pool size, select a reasonable value.
158158
# 0 means no limit, which could lead to exceeding the Database connection limit.
159159
try:
@@ -175,6 +175,16 @@ def configure_orm(disable_connection_pool=False):
175175
engine_args['pool_size'] = pool_size
176176
engine_args['pool_recycle'] = pool_recycle
177177

178+
try:
179+
# Allow the user to specify an encoding for their DB otherwise default
180+
# to utf-8 so jobs & users with non-latin1 characters can still use
181+
# us.
182+
engine_args['encoding'] = conf.get('core', 'SQL_ENGINE_ENCODING')
183+
except conf.AirflowConfigException:
184+
engine_args['encoding'] = 'utf-8'
185+
# For Python2 we get back a newstr and need a str
186+
engine_args['encoding'] = engine_args['encoding'].__str__()
187+
178188
engine = create_engine(SQL_ALCHEMY_CONN, **engine_args)
179189
reconnect_timeout = conf.getint('core', 'SQL_ALCHEMY_RECONNECT_TIMEOUT')
180190
setup_event_handlers(engine, reconnect_timeout)

‎tests/core.py

+5
Original file line numberDiff line numberDiff line change
@@ -1930,6 +1930,11 @@ def test_password_user_authenticate(self):
19301930
self.password_user.password = "secure_password"
19311931
self.assertTrue(self.password_user.authenticate("secure_password"))
19321932

1933+
def test_password_unicode_user_authenticate(self):
1934+
self.password_user.username = u"🐼" # This is a panda
1935+
self.password_user.password = "secure_password"
1936+
self.assertTrue(self.password_user.authenticate("secure_password"))
1937+
19331938
def test_password_authenticate_session(self):
19341939
from airflow.contrib.auth.backends.password_auth import PasswordUser
19351940
self.password_user.password = 'test_password'

0 commit comments

Comments
 (0)