async def test_jupyterhub_auth(tmpdir, monkeypatch): from jupyterhub.tests.utils import add_user gateway_address = "http://127.0.0.1:%d" % random_port() 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", "url": gateway_address, "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.public_url = gateway_address + "/services/dask-gateway/" config.DaskGateway.temp_dir = str(tmpdir) 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 gateway_proc: 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 Gateway( address=gateway_proc.public_urls.connect_url, proxy_address=gateway_proc.gateway_urls.connect_url, asynchronous=True, 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()
def test_jupyterhub_auth(monkeypatch): with pytest.raises(ValueError) as exc: get_auth("jupyterhub") assert "JUPYTERHUB_API_TOKEN" in str(exc.value) monkeypatch.setenv("JUPYTERHUB_API_TOKEN", "abcde") auth = get_auth("jupyterhub") assert auth.api_token == "abcde" assert isinstance(auth, JupyterHubAuth) # Parameters override environment variable assert JupyterHubAuth(api_token="other").api_token == "other"
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()
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()
return_train_score=False, cv=5, n_jobs=-1) with joblib.parallel_backend('dask'): grid_search.fit(X, y) fs = gcsfs.GCSFileSystem() path = "gcs://pangeo-scratch/tomaugspurger/model-1.pkl" with fs.open(path, "wb") as f: joblib.dump(grid_search, f) return path if __name__ == "__main__": auth = JupyterHubAuth(os.environ["PANGEO_TOKEN"]) # Proxy address will be made easier to find. print("Connecting to Gateway") gateway = Gateway( address= "https://staging.us-central1-b.gcp.pangeo.io/services/dask-gateway/", proxy_address="tls://104.197.142.28:8786", auth=auth) cluster = gateway.new_cluster(shutdown_on_close=False) client = cluster.get_client() print("Dashboard:", client.dashboard_link) cluster.scale(4) client.wait_for_workers(4) print("Cluster ready")