示例#1
0
    def setUp(self):
        """Create and configure a new app instance for each test."""
        # create the app with common test config
        self.app = create_app('test')
        self.app.config['SECURIZER'] = True

        @self.app.route('/', methods=['GET', 'POST'])
        @securizer
        def home():
            return {'msg': 'default response'}

        self.app_context = self.app.app_context()
        self.app_context.push()
        self.client = self.app.test_client()
        db.init_app(self.app)
        db.create_all()
        self.server = Server('me', port=5000, me=True)
        db.session.add(self.server)

        self.dim = generate_dimension('dimension')
        self.dim.current = True
        db.session.add(self.dim)

        self.token = create_access_token('test')
        self.auth = HTTPBearerAuth(self.token)
        self.url = 'https://me:5000/'
示例#2
0
    def _invoke(self, timeout) -> bool:
        """
        invokes the command on the remote server

        Returns
        -------
        bool:
            True if command ended up gracefully. False otherwise

        Raises
        ------
        TimeoutError:
            raised when timeout reached while waiting the response back from the remote server
        """
        ctx = None
        if not has_app_context():
            ctx = self._app.app_context()
            ctx.push()
        try:
            self.__dict__['_server'] = Server.query.get(self._server)

            # set a timeout if none to avoid infinite wait in event
            if timeout is None:
                timeout = defaults.TIMEOUT_REMOTE_COMMAND
            if not self._command._cp:
                auth = HTTPBearerAuth(
                    create_access_token(self._command.var_context.env['executor_id'], datetime.timedelta(seconds=15)))
                start = time.time()
                data = dict(operation=base64.b64encode(pickle.dumps(self._command.implementation)).decode('ascii'),
                            var_context=base64.b64encode(pickle.dumps(self._command.var_context)).decode('ascii'),
                            params=base64.b64encode(pickle.dumps(self._command.params)).decode('ascii'),
                            timeout=timeout,
                            step_id=str(self.id[1]),
                            orch_execution=self._command.register.json_orch_execution,
                            event_id=str(uuid.uuid4()))
                resp = post(server=self.server, view_or_url='api_1_0.launch_operation', json=data, auth=auth,
                            timeout=timeout)
                if resp.code == 204:
                    current_app.events.register(data['event_id'], self.callback_completion_event)
                    event = self._completion_event.wait(timeout=timeout - (time.time() - start))
                    if event is not True:
                        self._command._cp = CompletedProcess(success=False, stdout='',
                                                             stderr=f'Timeout of {timeout} reached waiting '
                                                                    f'server operation completion')

                elif resp.code == 200:
                    self.callback_completion_event(Event(None, data=resp.msg))
                elif resp.code:
                    if isinstance(resp.msg, dict):
                        msg = json.dumps(resp.msg)
                    else:
                        msg = str(resp.msg)

                    self._command._cp = CompletedProcess(success=False, stdout='',
                                                         stderr=msg, rc=resp.code)

        finally:
            if ctx:
                ctx.pop()
        return self.success
示例#3
0
 def setUp(self):
     """Create and configure a new app instance for each test."""
     # create the app with common test config
     self.app = create_app('test')
     self.app_context = self.app.app_context()
     self.app_context.push()
     self.client = self.app.test_client()
     self.auth = HTTPBearerAuth(create_access_token('00000000-0000-0000-0000-000000000001'))
     db.create_all()
示例#4
0
 def setUp(self):
     """Create and configure a new app instance for each test."""
     # create the app with common test config
     self.app = create_app('test')
     self.app_context = self.app.app_context()
     self.app_context.push()
     self.client = self.app.test_client()
     db.create_all()
     set_initial()
     self.auth = HTTPBearerAuth(
         create_access_token(User.get_by_name('root').id))
示例#5
0
 def setUp(self):
     """Create and configure a new app instance for each test."""
     # create the app with common test config
     self.app = create_app('test')
     self.app_context = self.app.app_context()
     self.app_context.push()
     self.client = self.app.test_client()
     self.auth = HTTPBearerAuth(create_access_token('00000000-0000-0000-0000-000000000001'))
     db.create_all()
     self.src = Server('source', port=5000)
     self.dst = Server('destination', port=5000)
示例#6
0
 def setUp(self):
     """Create and configure a new app instance for each test."""
     # create the app with common test config
     self.app = create_app('test')
     self.app_context = self.app.app_context()
     self.app_context.push()
     self.client = self.app.test_client()
     self.auth = HTTPBearerAuth(create_access_token('00000000-0000-0000-0000-000000000001'))
     db.create_all()
     self.me = Server('me', port=5000, _me=True)
     self.remote = Server('remote', port=5000)
     db.session.add_all([self.remote, self.me])
     self.maxDiff = None
示例#7
0
def refresh_access_token(login_=True):
    url = f"{env.get('SCHEME')}://{env.get('SERVER')}:{env.get('PORT')}/refresh"

    if env._refresh_token is None:
        raise ValueError('empty refresh token. Login first')
    auth = HTTPBearerAuth(env._refresh_token)
    resp = requests.post(url, auth=auth, verify=False, timeout=10)
    if resp.status_code in (401, 422) and login_:
        login()
    else:
        resp.raise_for_status()
        env._access_token = resp.json().get('access_token', None)
        return resp.json().get('username', None)
示例#8
0
 def setUp(self):
     """Create and configure a new app instance for each test."""
     # create the app with common test config
     self.app = create_app('test')
     self.app_context = self.app.app_context()
     self.app_context.push()
     self.client = self.app.test_client()
     self.auth = HTTPBearerAuth(
         create_access_token('00000000-0000-0000-0000-000000000001'))
     db.create_all()
     self.at = ActionTemplate(name='action',
                              version=1,
                              action_type=ActionType.SHELL,
                              code='code to run',
                              expected_stdout='expected output',
                              expected_rc=0,
                              system_kwargs={})
     ActionTemplate.set_initial()
示例#9
0
 def setUp(self):
     self.maxDiff = None
     """Create and configure a new app instance for each test."""
     # create the app with common test config
     self.app = create_app('test')
     self.app_context = self.app.app_context()
     self.app_context.push()
     self.client = self.app.test_client()
     self.auth = HTTPBearerAuth(
         create_access_token('00000000-0000-0000-0000-000000000001'))
     db.create_all()
     self.src = Server('source',
                       port=5000,
                       id='00000000-0000-0000-0000-000000000000')
     self.dst1 = Server('destination1',
                        port=5000,
                        id='00000000-0000-0000-0000-000000000001')
     self.dst2 = Server('destination2',
                        port=5000,
                        id='00000000-0000-0000-0000-000000000002')
     self.dst3 = Server('destination3',
                        port=5000,
                        id='00000000-0000-0000-0000-000000000003')
     db.session.add_all([self.src, self.dst1, self.dst2, self.dst3])
示例#10
0
def lock_unlock(action: str,
                scope: Scope,
                servers: t.List[Server],
                applicant=None,
                identity=None):
    """

    Parameters
    ----------
    action
        'U' for unlocking and 'L' for locking
    scope
        scope of the lock
    servers
        servers to ask for a lock

    Raises
    ------
    Raises an error if something went wrong

    Returns
    -------
    None
        returns none if all went as expected.
    """

    assert action in 'UL'

    applicant = applicant or [str(s.id) for s in servers]

    if identity:
        token = create_access_token(identity)
    else:
        token = create_access_token(get_jwt_identity())
    auth = HTTPBearerAuth(token)
    if action == 'U':
        pool_responses = run(
            request_locker(servers=servers,
                           scope=scope,
                           action='unlock',
                           applicant=applicant,
                           auth=auth))

        if len(servers) == len(
            [r for r in pool_responses if r.code in (200, 210)]):
            return
    else:
        action = 'P'
        catalog_ver = Catalog.max_catalog(str)
        pool_responses = run(
            request_locker(servers=servers,
                           scope=scope,
                           action='prevent',
                           applicant=applicant,
                           datemark=catalog_ver,
                           auth=auth))

        if len(servers) == len(
            [r for r in pool_responses if r.code in (200, 210)]):
            action = 'L'
            pool_responses = run(
                request_locker(servers=servers,
                               scope=scope,
                               action='lock',
                               applicant=applicant,
                               auth=auth))
            if len(servers) == len(
                [r for r in pool_responses if r.code in (200, 210)]):
                return

    raise errors.LockError(
        scope, action, [r for r in pool_responses if r.code not in (200, 210)])
示例#11
0
 def generate_auth(self):
     u = User.query.get(ROOT)
     if u:
         self.auth = HTTPBearerAuth(create_access_token(u.id))
示例#12
0
 def auth(self):
     return HTTPBearerAuth(create_access_token(self.user.id))
示例#13
0
def request(method,
            url,
            session=None,
            token_refreshed=False,
            login=True,
            **kwargs) -> Response:
    exception = None
    content = None
    status = None
    json_data = None
    headers = {}

    if not session:
        _session = requests.session()
    else:
        _session = session

    raise_on_error = kwargs.pop('raise_on_error', False)

    func = getattr(_session, method.lower())

    if 'auth' not in kwargs:
        if env._access_token is None:
            try:
                refresh_access_token(login_=login)
            except requests.exceptions.ConnectionError as e:
                return Response(exception=ConnectionError(
                    f"Unable to contact with {env.get('SCHEME')}://"
                    f"{env.get('SERVER')}:{env.get('PORT')}/refresh"),
                                url=url)
            except Exception as e:
                return Response(exception=e, url=url)
            else:
                if env._access_token is None:
                    return Response(
                        exception=ValueError("No authentication set"), url=url)
        kwargs['auth'] = HTTPBearerAuth(env._access_token)

    if 'headers' not in kwargs:
        kwargs['headers'] = {}
    kwargs['headers'].update({'D-Securizer': 'plain'})

    kwargs['verify'] = env.get('SSL_VERIFY')

    logger.debug(f"{method.upper()} {url}\n{kwargs}")

    resp = None
    try:
        resp: requests.Response = func(url, **kwargs)
    except (requests.Timeout, ) as e:
        timeout = kwargs.get('timeout', None)
        if isinstance(timeout, tuple):
            timeout = timeout[0] + timeout[1]
        exception = TimeoutError(
            f"Socket timeout reached while trying to connect to {url} "
            f"for {timeout} seconds")
    except requests.ConnectionError as e:
        exception = ConnectionError(f"Unable to contact to {url}")
    except Exception as e:
        exception = e
    finally:
        if session is None and not (getattr(resp, 'status_code', None) == 401
                                    and resp.json().get('msg')
                                    == 'Token has expired'):
            _session.close()
    if exception is None:
        status = resp.status_code
        headers = resp.headers
        if status == 401 and not token_refreshed:
            json_data = resp.json()
            if json_data.get('msg') == 'Token has expired':
                try:
                    refresh_access_token()
                except requests.exceptions.ConnectionError as e:
                    return Response(exception=ConnectionError(
                        f"Unable to contact with {env.get('SCHEME')}://"
                        f"{env.get('SERVER')}:{env.get('PORT')}/refresh"),
                                    url=url)
                kwargs['auth'] = HTTPBearerAuth(env._access_token)
                resp = request(method,
                               url,
                               session=_session,
                               token_refreshed=True,
                               **kwargs)
                if not session:
                    _session.close()
                return resp
        try:
            json_data = resp.json()
        except (ValueError, ):
            content = resp.text

        if json_data is not None:
            # try:
            #     content = unpack_msg(json_data)
            # except NotValidMessage:
            #     content = json_data
            content = json_data
    else:
        if raise_on_error:
            raise exception

    return Response(msg=content,
                    code=status,
                    exception=exception,
                    url=url,
                    headers=headers)