Esempio n. 1
0
def new_spawner(spawner_class):
    """ Create a new spawner from a given Spawner class
    """
    # Server for the user and the hub
    generic_server = orm.Server(proto="http",
                                ip="127.0.0.2",
                                port=31337,
                                base_url="/")

    # Mock db
    db = mock.Mock()
    db.query = mock.Mock()
    db.query().first = mock.Mock(return_value=orm.Proxy(
        auth_token="whatever",
        api_server=orm.Server(
            proto="http", ip="127.0.0.1", port=12345, base_url="/foo/bar/")))

    # Mock user
    user = mock.Mock()
    user.name = username()
    user.admin = False
    user.state = None
    user.server = generic_server

    # Mock hub
    hub = orm.Hub(server=generic_server)

    return spawner_class(db=db, user=user, hub=hub)
Esempio n. 2
0
def get_db():
    """Get a db session"""
    db = orm.new_session_factory('sqlite:///:memory:', echo=True)()
    user = orm.User(
        name=getuser(),
        server=orm.Server(),
    )
    hub = orm.Hub(
        server=orm.Server(),
    )
    db.add(user)
    db.add(hub)
    db.commit()
    return db
def _server_from_url(url):
    """Creates a orm.Server from a given url"""
    parsed = parse.urlparse(url)
    return jupyterhub_orm.Server(proto=parsed.scheme,
                                 ip=parsed.hostname,
                                 port=parsed.port,
                                 base_url=parsed.path)
Esempio n. 4
0
def populate_db(url):
    """Populate a jupyterhub database"""
    connect_args = {}
    if 'mysql' in url:
        connect_args['auth_plugin'] = 'mysql_native_password'
    db = orm.new_session_factory(url, connect_args=connect_args)()
    # create some users
    admin = orm.User(name='admin', admin=True)
    db.add(admin)
    user = orm.User(name='has-server')
    db.add(user)
    db.commit()

    # create a group
    g = orm.Group(name='group')
    db.add(g)
    db.commit()
    g.users.append(user)
    db.commit()

    service = orm.Service(name='service')
    db.add(service)
    db.commit()

    # create some API tokens
    user.new_api_token()
    admin.new_api_token()

    # services got API tokens in 0.7
    if jupyterhub.version_info >= (0, 7):
        # create a group
        service.new_api_token()

    # Create a Spawner for user
    if jupyterhub.version_info >= (0, 8):
        # create spawner for user
        spawner = orm.Spawner(name='', user=user)
        db.add(spawner)
        db.commit()
        spawner.server = orm.Server()
        db.commit()

        # admin's spawner is not running
        spawner = orm.Spawner(name='', user=admin)
        db.add(spawner)
        db.commit()
    else:
        user.server = orm.Server()
        db.commit()

    # create some oauth objects
    client = orm.OAuthClient(identifier='oauth-client')
    db.add(client)
    db.commit()
    code = orm.OAuthCode(client_id=client.identifier)
    db.add(code)
    db.commit()
    if jupyterhub.version_info < (2, 0):
        Token = partial(
            orm.OAuthAccessToken,
            grant_type=orm.GrantType.authorization_code,
        )
    else:
        Token = orm.APIToken
    access_token = Token(
        client_id=client.identifier,
        user_id=user.id,
    )
    db.add(access_token)
    db.commit()

    # set some timestamps added in 0.9
    assert user.created
    assert admin.created
    # set last_activity
    user.last_activity = datetime.utcnow()
    spawner = user.orm_spawners['']
    spawner.started = datetime.utcnow()
    spawner.last_activity = datetime.utcnow()
    db.commit()
    def init_services(self):
        """Override JupyterHub's default init_services class to support initializing Kubernetes services"""

        self._service_map.clear()
        if self.domain:
            domain = 'services.' + self.domain
            parsed = urlparse(self.subdomain_host)
            host = '%s://services.%s' % (parsed.scheme, parsed.netloc)
        else:
            domain = host = ''

        for spec in self.services:
            self.log.info("Reach app.py kubespawner services %s"%spec)
            if 'name' not in spec:
                raise ValueError('service spec must have a name: %r' % spec)
            name = spec['name']
            # get/create orm
            orm_service = orm.Service.find(self.db, name=name)
            if orm_service is None:
                # not found, create a new one
                orm_service = orm.Service(name=name)
                self.db.add(orm_service)
            orm_service.admin = spec.get('admin', False)
            self.db.commit()
            service = Service(
                parent=self,
                app=self,
                base_url=self.base_url,
                db=self.db,
                orm=orm_service,
                domain=domain,
                host=host,
                hub=self.hub,
            )

            traits = service.traits(input=True)
            for key, value in spec.items():
                if key not in traits:
                    raise AttributeError("No such service field: %s" % key)
                setattr(service, key, value)

            if service.managed:
                if not service.api_token:
                    # generate new token
                    # TODO: revoke old tokens?
                    service.api_token = service.orm.new_api_token(
                        note="generated at startup"
                    )
                else:
                    # ensure provided token is registered
                    self.service_tokens[service.api_token] = service.name
            else:
                self.service_tokens[service.api_token] = service.name

            if service.image and service.port:
                service.url = "http://0.0.0.0:%d/"%service.port

            if service.url:
                parsed = urlparse(service.url)
                if parsed.port is not None:
                    port = parsed.port
                elif parsed.scheme == 'http':
                    port = 80
                elif parsed.scheme == 'https':
                    port = 443
                server = service.orm.server = orm.Server(
                    proto=parsed.scheme,
                    ip=parsed.hostname,
                    port=port,
                    cookie_name='jupyterhub-services',
                    base_url=service.prefix,
                )
                self.db.add(server)

            else:
                service.orm.server = None

            if service.oauth_available:
                self.oauth_provider.add_client(
                    client_id=service.oauth_client_id,
                    client_secret=service.api_token,
                    redirect_uri=service.oauth_redirect_uri,
                    description="JupyterHub service %s" % service.name,
                )

            self._service_map[name] = service

        # delete services from db not in service config:
        for service in self.db.query(orm.Service):
            if service.name not in self._service_map:
                self.db.delete(service)
        self.db.commit()
Esempio n. 6
0
def populate_db(url):
    """Populate a jupyterhub database"""
    connect_args = {}
    if 'mysql' in url:
        connect_args['auth_plugin'] = 'mysql_native_password'
    db = orm.new_session_factory(url, connect_args=connect_args)()

    if jupyterhub.version_info >= (2, ):
        if (not db.query(orm.OAuthClient).filter_by(
                identifier="jupyterhub").one_or_none()):
            # create the oauth client for jupyterhub itself
            # this allows us to distinguish between orphaned tokens
            # (failed cascade deletion) and tokens issued by the hub
            # it has no client_secret, which means it cannot be used
            # to make requests
            client = orm.OAuthClient(
                identifier="jupyterhub",
                secret="",
                redirect_uri="",
                description="JupyterHub",
            )
            db.add(client)
            db.commit()

        from jupyterhub import roles

        for role in roles.get_default_roles():
            roles.create_role(db, role)

    # create some users
    admin = orm.User(name='admin', admin=True)
    db.add(admin)
    user = orm.User(name='has-server')
    db.add(user)
    db.commit()

    # create a group
    g = orm.Group(name='group')
    db.add(g)
    db.commit()
    g.users.append(user)
    db.commit()

    service = orm.Service(name='service')
    db.add(service)
    db.commit()

    # create some API tokens
    user.new_api_token()
    admin.new_api_token()

    # services got API tokens in 0.7
    if jupyterhub.version_info >= (0, 7):
        # create a group
        service.new_api_token()

    # Create a Spawner for user
    if jupyterhub.version_info >= (0, 8):
        # create spawner for user
        spawner = orm.Spawner(name='', user=user)
        db.add(spawner)
        db.commit()
        spawner.server = orm.Server()
        db.commit()

        # admin's spawner is not running
        spawner = orm.Spawner(name='', user=admin)
        db.add(spawner)
        db.commit()
    else:
        user.server = orm.Server()
        db.commit()

    # create some oauth objects
    client = orm.OAuthClient(identifier='oauth-client')
    db.add(client)
    db.commit()
    code = orm.OAuthCode(client_id=client.identifier)
    db.add(code)
    db.commit()
    if jupyterhub.version_info < (2, 0):
        Token = partial(
            orm.OAuthAccessToken,
            grant_type=orm.GrantType.authorization_code,
        )
    else:
        Token = orm.APIToken
    access_token = Token(
        client_id=client.identifier,
        user_id=user.id,
    )
    if jupyterhub.version_info >= (2, ):
        if jupyterhub.version_info < (2, 2):
            access_token.roles = [
                db.query(orm.Role).filter_by(name="server").one()
            ]
        else:
            access_token.scopes = [f"read:users!user={user.name}"]
    db.add(access_token)
    db.commit()

    # set some timestamps added in 0.9
    assert user.created
    assert admin.created
    # set last_activity
    user.last_activity = datetime.utcnow()
    spawner = user.orm_spawners['']
    spawner.started = datetime.utcnow()
    spawner.last_activity = datetime.utcnow()
    db.commit()