Skip to content

Commit

Permalink
Escape username (#73)
Browse files Browse the repository at this point in the history
Otherwise bad usernames can cause connection errors
  • Loading branch information
mrocklin committed May 5, 2018
1 parent f8c2761 commit b478487
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
11 changes: 10 additions & 1 deletion dask_kubernetes/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import os
import socket
import string
import time
from urllib.parse import urlparse
import uuid
Expand Down Expand Up @@ -159,7 +160,8 @@ def __init__(

if name is None:
worker_name = config.get('kubernetes-worker-name', 'dask-{user}-{uuid}')
name = worker_name.format(user=getpass.getuser(), uuid=str(uuid.uuid4())[:10], **os.environ)
name = worker_name.format(user=escape(getpass.getuser()),
uuid=str(uuid.uuid4())[:10], **os.environ)

self.pod_template = clean_pod_template(pod_template)
# Default labels that can't be overwritten
Expand Down Expand Up @@ -497,3 +499,10 @@ def select_workers_to_close(scheduler, n_to_close):
to_close.add(rest.pop())

return [ws.address for ws in to_close]


valid_characters = string.ascii_letters + string.digits + '_-.'


def escape(s):
return ''.join(c for c in s if c in valid_characters)
12 changes: 12 additions & 0 deletions dask_kubernetes/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,15 @@ def test_repr(cluster):
assert 'Box' not in text
assert cluster.scheduler.address in text
assert "workers=0" in text


def test_escape_username(pod_spec, loop, ns):
old_logname = os.environ.get('LOGNAME')
os.environ['LOGNAME'] = 'foo!'

try:
with KubeCluster(pod_spec, loop=loop, namespace=ns) as cluster:
assert 'foo' in cluster.name
assert '!' not in cluster.name
finally:
os.environ['LOGNAME'] = old_logname

0 comments on commit b478487

Please sign in to comment.