forked from dask/dask-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_auth.py
162 lines (117 loc) · 4.57 KB
/
test_auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import os
import subprocess
import uuid
import pytest
from traitlets.config import Config
from dask_gateway.auth import BasicAuth, JupyterHubAuth
from dask_gateway_server.utils import random_port
from .utils_test import temp_gateway
try:
import kerberos
del kerberos
skip = not os.environ.get("TEST_DASK_GATEWAY_YARN")
requires_kerberos = pytest.mark.skipif(skip, reason="No kerberos server running")
except ImportError:
requires_kerberos = pytest.mark.skipif(True, reason="Cannot import kerberos")
try:
import jupyterhub.tests.mocking as hub_mocking
except ImportError:
hub_mocking = None
KEYTAB_PATH = "/home/dask/dask.keytab"
def kinit():
subprocess.check_call(["kinit", "-kt", KEYTAB_PATH, "dask"])
def kdestroy():
subprocess.check_call(["kdestroy"])
@pytest.mark.asyncio
async def test_basic_auth():
async with temp_gateway() as g:
async with g.gateway_client(auth="basic") as gateway:
await gateway.list_clusters()
@pytest.mark.asyncio
async def test_basic_auth_password():
config = Config()
config.DaskGateway.authenticator_class = (
"dask_gateway_server.auth.SimpleAuthenticator"
)
config.SimpleAuthenticator.password = "mypass"
async with temp_gateway(config=config) as g:
auth = BasicAuth()
async with g.gateway_client(auth=auth) as gateway:
with pytest.raises(Exception):
await gateway.list_clusters()
auth.password = "mypass"
await gateway.list_clusters()
@pytest.mark.asyncio
@requires_kerberos
async def test_kerberos_auth():
config = Config()
config.Proxy.address = "master.example.com:0"
config.DaskGateway.authenticator_class = (
"dask_gateway_server.auth.KerberosAuthenticator"
)
config.KerberosAuthenticator.keytab = KEYTAB_PATH
async with temp_gateway(config=config) as g:
async with g.gateway_client(auth="kerberos") as gateway:
kdestroy()
with pytest.raises(Exception):
await gateway.list_clusters()
kinit()
await gateway.list_clusters()
kdestroy()
class temp_hub(object):
def __init__(self, hub):
self.hub = hub
async def __aenter__(self):
await self.hub.initialize([])
await self.hub.start()
# alembic turns off all logs, reenable them for the tests
import logging
from tornado.log import app_log, access_log, gen_log
logs = [app_log, access_log, gen_log, logging.getLogger("DaskGateway")]
for log in logs:
log.disabled = False
# Disable curl http client for easier testing
from tornado.httpclient import AsyncHTTPClient
AsyncHTTPClient.configure("tornado.simple_httpclient.SimpleAsyncHTTPClient")
async def __aexit__(self, *args):
if self.hub.http_server:
self.hub.http_server.stop()
await self.hub.cleanup()
type(self.hub).clear_instance()
@pytest.mark.skipif(not hub_mocking, reason="JupyterHub not installed")
@pytest.mark.asyncio
async def test_jupyterhub_auth(monkeypatch):
from jupyterhub.tests.utils import add_user
jhub_api_token = uuid.uuid4().hex
jhub_bind_url = "http://127.0.0.1:%i/@/space%%20word/" % random_port()
hub_config = Config()
hub_config.JupyterHub.services = [
{"name": "dask-gateway", "api_token": jhub_api_token}
]
hub_config.JupyterHub.bind_url = jhub_bind_url
class MockHub(hub_mocking.MockHub):
def init_logging(self):
pass
hub = MockHub(config=hub_config)
# Configure gateway
config = Config()
config.DaskGateway.authenticator_class = (
"dask_gateway_server.auth.JupyterHubAuthenticator"
)
config.JupyterHubAuthenticator.jupyterhub_api_token = jhub_api_token
config.JupyterHubAuthenticator.jupyterhub_api_url = jhub_bind_url + "api/"
async with temp_gateway(config=config) as g:
async with temp_hub(hub):
# Create a new jupyterhub user alice, and get the api token
u = add_user(hub.db, name="alice")
api_token = u.new_api_token()
hub.db.commit()
# Configure auth with incorrect api token
auth = JupyterHubAuth(api_token=uuid.uuid4().hex)
async with g.gateway_client(auth=auth) as gateway:
# Auth fails with bad token
with pytest.raises(Exception):
await gateway.list_clusters()
# Auth works with correct token
auth.api_token = api_token
await gateway.list_clusters()