-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathtest_auth.py
199 lines (144 loc) · 5.9 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import os
import subprocess
import uuid
import pytest
from dask_gateway.auth import BasicAuth, JupyterHubAuth
from dask_gateway_server.utils import random_port
from traitlets.config import Config
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"])
async def test_basic_auth():
async with temp_gateway() as g:
async with g.gateway_client(auth="basic") as gateway:
await gateway.list_clusters()
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()
@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:
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 access_log, app_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()
def configure_dask_gateway(jhub_api_token, jhub_bind_url):
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"
return config
@pytest.mark.skipif(not hub_mocking, reason="JupyterHub not installed")
async def test_jupyterhub_auth_user(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 = configure_dask_gateway(jhub_api_token, jhub_bind_url)
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()
@pytest.mark.skipif(not hub_mocking, reason="JupyterHub not installed")
async def test_jupyterhub_auth_service(monkeypatch):
jhub_api_token = uuid.uuid4().hex
jhub_service_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},
{"name": "any-service", "api_token": jhub_service_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 = configure_dask_gateway(jhub_api_token, jhub_bind_url)
async with temp_gateway(config=config) as g:
async with temp_hub(hub):
# 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 service token
auth.api_token = jhub_api_token
await gateway.list_clusters()