-
Notifications
You must be signed in to change notification settings - Fork 270
/
test_authorizer.py
276 lines (245 loc) · 7.62 KB
/
test_authorizer.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"""Tests for authorization"""
import json
import pytest
from jupyter_client.kernelspec import NATIVE_KERNEL_NAME
from nbformat import writes
from nbformat.v4 import new_notebook
from tornado.httpclient import HTTPClientError
from tornado.websocket import WebSocketHandler
from jupyter_server.auth.authorizer import Authorizer
from jupyter_server.auth.utils import HTTP_METHOD_TO_AUTH_ACTION, match_url_to_resource
from jupyter_server.services.security import csp_report_uri
class AuthorizerforTesting(Authorizer):
# Set these class attributes from within a test
# to verify that they match the arguments passed
# by the REST API.
permissions = {}
def normalize_url(self, path):
"""Drop the base URL and make sure path leads with a /"""
base_url = self.parent.base_url
# Remove base_url
if path.startswith(base_url):
path = path[len(base_url) :]
# Make sure path starts with /
if not path.startswith("/"):
path = "/" + path
return path
def is_authorized(self, handler, user, action, resource):
# Parse Request
if isinstance(handler, WebSocketHandler):
method = "WEBSOCKET"
else:
method = handler.request.method
url = self.normalize_url(handler.request.path)
# Map request parts to expected action and resource.
expected_action = HTTP_METHOD_TO_AUTH_ACTION[method]
expected_resource = match_url_to_resource(url)
# Assert that authorization layer returns the
# correct action + resource.
assert action == expected_action
assert resource == expected_resource
# Now, actually apply the authorization layer.
return all(
[
action in self.permissions.get("actions", []),
resource in self.permissions.get("resources", []),
]
)
@pytest.fixture
def jp_server_config():
return {"ServerApp": {"authorizer_class": AuthorizerforTesting}}
@pytest.fixture
def send_request(jp_fetch, jp_ws_fetch):
"""Send to Jupyter Server and return response code."""
async def _(url, **fetch_kwargs):
if url.endswith("channels") or "/websocket/" in url:
fetch = jp_ws_fetch
else:
fetch = jp_fetch
try:
r = await fetch(url, **fetch_kwargs, allow_nonstandard_methods=True)
code = r.code
except HTTPClientError as err:
code = err.code
else:
if fetch is jp_ws_fetch:
r.close()
print(code, url, fetch_kwargs)
return code
return _
HTTP_REQUESTS = [
{
"method": "GET",
"url": "/view/{nbpath}",
},
{
"method": "GET",
"url": "/api/contents",
},
{
"method": "POST",
"url": "/api/contents",
"body": json.dumps({"type": "directory"}),
},
{
"method": "PUT",
"url": "/api/contents/foo",
"body": json.dumps({"type": "directory"}),
},
{
"method": "PATCH",
"url": "/api/contents/{nbpath}",
"body": json.dumps({"path": "/newpath"}),
},
{
"method": "DELETE",
"url": "/api/contents/{nbpath}",
},
{
"method": "GET",
"url": "/api/kernels",
},
{
"method": "GET",
"url": "/api/kernels/{kernel_id}",
},
{
"method": "GET",
"url": "/api/kernels/{kernel_id}/channels",
},
{
"method": "POST",
"url": "/api/kernels/{kernel_id}/interrupt",
},
{
"method": "POST",
"url": "/api/kernels/{kernel_id}/restart",
},
{
"method": "DELETE",
"url": "/api/kernels/{kernel_id}",
},
{
"method": "POST",
"url": "/api/kernels",
},
{"method": "GET", "url": "/api/kernelspecs"},
{"method": "GET", "url": "/api/kernelspecs/{kernelspec}"},
{"method": "GET", "url": "/api/nbconvert"},
{"method": "GET", "url": "/api/spec.yaml"},
{"method": "GET", "url": "/api/status"},
{"method": "GET", "url": "/api/config/foo"},
{"method": "PUT", "url": "/api/config/foo", "body": "{}"},
{"method": "PATCH", "url": "/api/config/foo", "body": "{}"},
{
"method": "POST",
"url": "/".join(tuple(csp_report_uri.split("/")[1:])),
},
{
"method": "GET",
"url": "/api/sessions",
},
{
"method": "GET",
"url": "/api/sessions/{session_id}",
},
{"method": "PATCH", "url": "/api/sessions/{session_id}", "body": "{}"},
{
"method": "DELETE",
"url": "/api/sessions/{session_id}",
},
{
"method": "POST",
"url": "/api/sessions",
"body": json.dumps({"path": "foo", "type": "bar"}),
},
{
"method": "POST",
"url": "/api/terminals",
"body": "",
},
{
"method": "GET",
"url": "/api/terminals",
},
{
"method": "GET",
"url": "/terminals/websocket/{term_name}",
},
{
"method": "DELETE",
"url": "/api/terminals/{term_name}",
},
]
HTTP_REQUESTS_PARAMETRIZED = [(req["method"], req["url"], req.get("body")) for req in HTTP_REQUESTS]
# -------- Test scenarios -----------
@pytest.mark.parametrize("method, url, body", HTTP_REQUESTS_PARAMETRIZED)
@pytest.mark.parametrize("allowed", (True, False))
async def test_authorized_requests(
request,
io_loop,
send_request,
tmp_path,
jp_serverapp,
method,
url,
body,
allowed,
):
# Setup stuff for the Contents API
# Add a notebook on disk
contents_dir = tmp_path / jp_serverapp.root_dir
p = contents_dir / "dir_for_testing"
p.mkdir(parents=True, exist_ok=True)
# Create a notebook
nb = writes(new_notebook(), version=4)
nbname = p.joinpath("nb_for_testing.ipynb")
nbname.write_text(nb, encoding="utf-8")
# Setup
nbpath = "dir_for_testing/nb_for_testing.ipynb"
kernelspec = NATIVE_KERNEL_NAME
km = jp_serverapp.kernel_manager
if "session" in url:
request.addfinalizer(lambda: io_loop.run_sync(km.shutdown_all))
session_model = await jp_serverapp.session_manager.create_session(path="foo")
session_id = session_model["id"]
if "kernel" in url:
request.addfinalizer(lambda: io_loop.run_sync(km.shutdown_all))
kernel_id = await km.start_kernel()
kernel = km.get_kernel(kernel_id)
# kernels take a moment to be ready
# wait for it to respond
kc = kernel.client()
kc.start_channels()
await kc.wait_for_ready()
kc.stop_channels()
if "terminal" in url:
term_manager = jp_serverapp.web_app.settings["terminal_manager"]
request.addfinalizer(lambda: io_loop.run_sync(term_manager.terminate_all))
term_model = term_manager.create()
term_name = term_model["name"]
url = url.format(**locals())
if allowed:
# Create a server with full permissions
permissions = {
"actions": ["read", "write", "execute"],
"resources": [
"contents",
"kernels",
"kernelspecs",
"nbconvert",
"sessions",
"api",
"config",
"csp",
"server",
"terminals",
],
}
expected_codes = {200, 201, 204, None} # Websockets don't return a code
else:
permissions = {"actions": [], "resources": []}
expected_codes = {403}
jp_serverapp.authorizer.permissions = permissions
code = await send_request(url, body=body, method=method)
assert code in expected_codes