Skip to content

Commit

Permalink
Fix lint errors and update version test
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-bates committed Sep 13, 2019
1 parent 01c9d50 commit d1058f8
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 55 deletions.
14 changes: 7 additions & 7 deletions enterprise_gateway/base/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
"""Tornado handlers for the base of the API."""

import json
from tornado import web
import notebook.base.handlers as notebook_handlers
import notebook._version
from notebook.base.handlers import APIHandler
from tornado import web
from ..mixins import TokenAuthorizationMixin, CORSMixin, JSONErrorsMixin
from .._version import __version__


class APIVersionHandler(TokenAuthorizationMixin,
CORSMixin,
JSONErrorsMixin,
notebook_handlers.APIVersionHandler):
APIHandler):
"""Extends the notebook server base API handler with token auth, CORS, and
JSON errors.
JSON errors to produce version information for notebook and gateway.
"""
#pass

def get(self):
# not authenticated, so give as few info as possible
self.finish(json.dumps({"version":notebook.__version__, "gateway_version":__version__}))
# to be backwards compatibile, use only 'version' for the notebook version
# and be more specific for gateway_version
self.finish(json.dumps({"version": notebook.__version__, "gateway_version": __version__}))


class NotFoundHandler(JSONErrorsMixin, web.RequestHandler):
Expand Down
64 changes: 26 additions & 38 deletions enterprise_gateway/enterprisegatewayapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ class EnterpriseGatewayApp(JupyterApp):
description = """
Jupyter Enterprise Gateway
Provisions remote Jupyter kernels and proxies HTTP/Websocket traffic
to them.
Provisions remote Jupyter kernels and proxies HTTP/Websocket traffic to them.
"""

# Enable some command line shortcuts
Expand All @@ -77,8 +76,7 @@ class EnterpriseGatewayApp(JupyterApp):
port_env = 'KG_PORT'
port_default_value = 8888
port = Integer(port_default_value, config=True,
help='Port on which to listen (KG_PORT env var)'
)
help='Port on which to listen (KG_PORT env var)')

@default('port')
def port_default(self):
Expand All @@ -87,8 +85,8 @@ def port_default(self):
port_retries_env = 'KG_PORT_RETRIES'
port_retries_default_value = 50
port_retries = Integer(port_retries_default_value, config=True,
help='Number of ports to try if the specified port is not available (KG_PORT_RETRIES env var)'
)
help="""Number of ports to try if the specified port is not available
(KG_PORT_RETRIES env var)""")

@default('port_retries')
def port_retries_default(self):
Expand All @@ -97,8 +95,7 @@ def port_retries_default(self):
ip_env = 'KG_IP'
ip_default_value = '127.0.0.1'
ip = Unicode(ip_default_value, config=True,
help='IP address on which to listen (KG_IP env var)'
)
help='IP address on which to listen (KG_IP env var)')

@default('ip')
def ip_default(self):
Expand All @@ -108,7 +105,7 @@ def ip_default(self):
base_url_env = 'KG_BASE_URL'
base_url_default_value = '/'
base_url = Unicode(base_url_default_value, config=True,
help='The base path for mounting all API resources (KG_BASE_URL env var)')
help='The base path for mounting all API resources (KG_BASE_URL env var)')

@default('base_url')
def base_url_default(self):
Expand All @@ -117,8 +114,7 @@ def base_url_default(self):
# Token authorization
auth_token_env = 'KG_AUTH_TOKEN'
auth_token = Unicode(config=True,
help='Authorization token required for all requests (KG_AUTH_TOKEN env var)'
)
help='Authorization token required for all requests (KG_AUTH_TOKEN env var)')

@default('auth_token')
def _auth_token_default(self):
Expand All @@ -127,54 +123,49 @@ def _auth_token_default(self):
# CORS headers
allow_credentials_env = 'KG_ALLOW_CREDENTIALS'
allow_credentials = Unicode(config=True,
help='Sets the Access-Control-Allow-Credentials header. (KG_ALLOW_CREDENTIALS env var)'
)
help='Sets the Access-Control-Allow-Credentials header. (KG_ALLOW_CREDENTIALS env var)')

@default('allow_credentials')
def allow_credentials_default(self):
return os.getenv(self.allow_credentials_env, '')

allow_headers_env = 'KG_ALLOW_HEADERS'
allow_headers = Unicode(config=True,
help='Sets the Access-Control-Allow-Headers header. (KG_ALLOW_HEADERS env var)'
)
help='Sets the Access-Control-Allow-Headers header. (KG_ALLOW_HEADERS env var)')

@default('allow_headers')
def allow_headers_default(self):
return os.getenv(self.allow_headers_env, '')

allow_methods_env = 'KG_ALLOW_METHODS'
allow_methods = Unicode(config=True,
help='Sets the Access-Control-Allow-Methods header. (KG_ALLOW_METHODS env var)'
)
help='Sets the Access-Control-Allow-Methods header. (KG_ALLOW_METHODS env var)')

@default('allow_methods')
def allow_methods_default(self):
return os.getenv(self.allow_methods_env, '')

allow_origin_env = 'KG_ALLOW_ORIGIN'
allow_origin = Unicode(config=True,
help='Sets the Access-Control-Allow-Origin header. (KG_ALLOW_ORIGIN env var)'
)
help='Sets the Access-Control-Allow-Origin header. (KG_ALLOW_ORIGIN env var)')

@default('allow_origin')
def allow_origin_default(self):
return os.getenv(self.allow_origin_env, '')

expose_headers_env = 'KG_EXPOSE_HEADERS'
expose_headers = Unicode(config=True,
help='Sets the Access-Control-Expose-Headers header. (KG_EXPOSE_HEADERS env var)'
)
help='Sets the Access-Control-Expose-Headers header. (KG_EXPOSE_HEADERS env var)')

@default('expose_headers')
def expose_headers_default(self):
return os.getenv(self.expose_headers_env, '')

trust_xheaders_env = 'KG_TRUST_XHEADERS'
trust_xheaders = CBool(False, config=True,
help='Use x-* header values for overriding the remote-ip, useful when '
'application is behing a proxy. (KG_TRUST_XHEADERS env var)'
)
help="""Use x-* header values for overriding the remote-ip, useful when
application is behing a proxy. (KG_TRUST_XHEADERS env var)""")

@default('trust_xheaders')
def trust_xheaders_default(self):
return strtobool(os.getenv(self.trust_xheaders_env, 'False'))
Expand All @@ -197,7 +188,7 @@ def keyfile_default(self):

client_ca_env = 'KG_CLIENT_CA'
client_ca = Unicode(None, config=True, allow_none=True,
help="""The full path to a certificate authority certificate for SSL/TLS
help="""The full path to a certificate authority certificate for SSL/TLS
client authentication. (KG_CLIENT_CA env var)""")

@default('client_ca')
Expand All @@ -206,19 +197,17 @@ def client_ca_default(self):

max_age_env = 'KG_MAX_AGE'
max_age = Unicode(config=True,
help='Sets the Access-Control-Max-Age header. (KG_MAX_AGE env var)'
)
help='Sets the Access-Control-Max-Age header. (KG_MAX_AGE env var)')

@default('max_age')
def max_age_default(self):
return os.getenv(self.max_age_env, '')

max_kernels_env = 'KG_MAX_KERNELS'
max_kernels = Integer(None, config=True,
allow_none=True,
help='Limits the number of kernel instances allowed to run by this gateway. '
'Unbounded by default. (KG_MAX_KERNELS env var)'
)
allow_none=True,
help="""Limits the number of kernel instances allowed to run by this gateway.
Unbounded by default. (KG_MAX_KERNELS env var)""")

@default('max_kernels')
def max_kernels_default(self):
Expand All @@ -227,7 +216,7 @@ def max_kernels_default(self):

default_kernel_name_env = 'KG_DEFAULT_KERNEL_NAME'
default_kernel_name = Unicode(config=True,
help='Default kernel name when spawning a kernel (KG_DEFAULT_KERNEL_NAME env var)')
help='Default kernel name when spawning a kernel (KG_DEFAULT_KERNEL_NAME env var)')

@default('default_kernel_name')
def default_kernel_name_default(self):
Expand All @@ -236,10 +225,9 @@ def default_kernel_name_default(self):

list_kernels_env = 'KG_LIST_KERNELS'
list_kernels = Bool(config=True,
help="""Permits listing of the running kernels using API endpoints /api/kernels
and /api/sessions (KG_LIST_KERNELS env var). Note: Jupyter Notebook
allows this by default but kernel gateway does not."""
)
help="""Permits listing of the running kernels using API endpoints /api/kernels
and /api/sessions (KG_LIST_KERNELS env var). Note: Jupyter Notebook
allows this by default but kernel gateway does not.""")

@default('list_kernels')
def list_kernels_default(self):
Expand All @@ -255,7 +243,7 @@ def env_whitelist_default(self):

env_process_whitelist_env = 'KG_ENV_PROCESS_WHITELIST'
env_process_whitelist = List(config=True,
help="""Environment variables allowed to be inherited
help="""Environment variables allowed to be inherited
from the spawning process by the kernel""")

@default('env_process_whitelist')
Expand Down Expand Up @@ -593,7 +581,7 @@ def init_http_server(self):
xheaders=self.trust_xheaders,
ssl_options=ssl_options)

for port in random_ports(self.port, self.port_retries+1):
for port in random_ports(self.port, self.port_retries + 1):
try:
self.http_server.listen(port, self.ip)
except socket.error as e:
Expand Down
2 changes: 1 addition & 1 deletion enterprise_gateway/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def write_error(self, status_code, **kwargs):
"""Responds with an application/json error object.
Overrides the APIHandler.write_error in the notebook server until it
properly sets the 'reason' field.
properly sets the 'reason' field.
Parameters
----------
Expand Down
2 changes: 1 addition & 1 deletion enterprise_gateway/services/api/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from notebook.utils import maybe_future
from tornado import gen, web
from ...mixins import TokenAuthorizationMixin, CORSMixin, JSONErrorsMixin
from ...mixins import CORSMixin


class BaseSpecHandler(CORSMixin, web.StaticFileHandler):
Expand Down
4 changes: 2 additions & 2 deletions enterprise_gateway/services/kernels/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def post(self):
env = {'PATH': os.getenv('PATH', '')}
# Whitelist environment variables from current process environment
env.update({key: value for key, value in os.environ.items()
if key in self.env_process_whitelist})
if key in self.env_process_whitelist})
# Whitelist KERNEL_* args and those allowed by configuration from client
env.update({key: value for key, value in model['env'].items()
if key.startswith('KERNEL_') or key in self.env_whitelist})
if key.startswith('KERNEL_') or key in self.env_whitelist})
# No way to override the call to start_kernel on the kernel manager
# so do a temporary partial (ugh)
orig_start = self.kernel_manager.start_kernel
Expand Down
10 changes: 6 additions & 4 deletions enterprise_gateway/services/sessions/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import notebook.services.sessions.handlers as notebook_handlers
from ...mixins import TokenAuthorizationMixin, CORSMixin, JSONErrorsMixin


class SessionRootHandler(TokenAuthorizationMixin,
CORSMixin,
JSONErrorsMixin,
notebook_handlers.SessionRootHandler):
CORSMixin,
JSONErrorsMixin,
notebook_handlers.SessionRootHandler):
"""Extends the notebook root session handler with token auth, CORS, and
JSON errors.
"""
Expand All @@ -22,11 +23,12 @@ def get(self):
tornado.web.HTTPError
If kg_list_kernels is False, respond with 403 Forbidden
"""
if 'kg_list_kernels' not in self.settings or self.settings['kg_list_kernels'] != True:
if 'kg_list_kernels' not in self.settings or not self.settings['kg_list_kernels']:
raise tornado.web.HTTPError(403, 'Forbidden')
else:
super(SessionRootHandler, self).get()


default_handlers = []
for path, cls in notebook_handlers.default_handlers:
if cls.__name__ in globals():
Expand Down
3 changes: 2 additions & 1 deletion enterprise_gateway/services/sessions/sessionmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from traitlets.config.configurable import LoggingConfigurable
from ipython_genutils.py3compat import unicode_type


class SessionManager(LoggingConfigurable):
"""Simple implementation of the SessionManager interface that allows clients
to associate basic metadata with a kernel.
Expand Down Expand Up @@ -97,7 +98,7 @@ def save_session(self, session_id, path=None, kernel_id=None, *args, **kwargs):
Session model with `session_id`, `path`, and `kernel_id` keys
"""
self._sessions.append({'session_id': session_id,
'path':path,
'path': path,
'kernel_id': kernel_id})

return self.get_session(session_id=session_id)
Expand Down
1 change: 1 addition & 0 deletions enterprise_gateway/tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ def test_get_api(self):
self.assertEqual(response.code, 200)
info = json_decode(response.body)
self.assertIn('version', info)
self.assertIn('gateway_version', info)

@gen_test
def test_get_kernelspecs(self):
Expand Down
1 change: 0 additions & 1 deletion requirements.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dependencies:
- jinja2>=2.10
- jupyter_client>=5.2.0
- jupyter_core>=4.4.0
- jupyter_kernel_gateway>=2.4.0
- notebook>=5.7.6,<7.0
- paramiko>=2.1.2
- pexpect>=4.2.0
Expand Down

0 comments on commit d1058f8

Please sign in to comment.