コード例 #1
0
ファイル: views.py プロジェクト: AmmarNoman/ichnaea
    def __call__(self):
        """Execute the view and return a response."""
        api_key = None
        api_key_text = self.parse_apikey()
        skip_check = False

        if api_key_text is None:
            self.log_count("none")
            if self.error_on_invalidkey:
                raise self.prepare_exception(InvalidAPIKey())

        if api_key_text is not None:
            try:
                api_key = get_key(self.request.db_session, api_key_text)
            except (DatabaseError, DBAPIError):
                # if we cannot connect to backend DB, skip api key check
                skip_check = True
                self.raven_client.captureException()
                bind_threadlocal(
                    api_key=api_key_text,
                    api_path=self.metric_path,
                    api_type=self.view_type,
                    api_key_db_fail=True,
                )

        if api_key is not None:
            valid_key = api_key.valid_key
            if api_key.allowed(self.view_type):
                self.log_count(valid_key)

                # Potentially avoid overhead of Redis connection.
                if self.ip_log_and_rate_limit:
                    if self.log_ip_and_rate_limited(valid_key, api_key.maxreq):
                        raise self.prepare_exception(DailyLimitExceeded())
            else:
                self.log_count("invalid")
                # Switch "invalid" with real key, add "api_key_allowed"
                bind_threadlocal(api_key=valid_key, api_key_allowed=False)

                if self.error_on_invalidkey:
                    raise self.prepare_exception(InvalidAPIKey())

        elif skip_check:
            pass
        else:
            if api_key_text is not None:
                self.log_count("invalid")
                bind_threadlocal(invalid_api_key=api_key_text)
            if self.error_on_invalidkey:
                raise self.prepare_exception(InvalidAPIKey())

        # If we failed to look up an ApiKey, create an empty one
        # rather than passing None through
        if api_key is None:
            api_key = Key()
        return self.view(api_key)
コード例 #2
0
def show_api_key_details(ctx, key):
    """Print api key details to stdout."""
    db = configure_db("rw")
    with db_worker_session(db) as session:
        row = session.query(ApiKey).filter(
            ApiKey.valid_key == key).one_or_none()
        if row:
            api_key = Key.from_obj(row)
        else:
            api_key = None

    if api_key:
        table = [[name, value] for name, value in api_key.as_dict().items()]
        print_table(table, delimiter=" : ", stream_write=click_echo_no_nl)
    else:
        click.echo(f"API key '{key}' does not exist")
コード例 #3
0
ファイル: apikey.py プロジェクト: anisin97/ichnaea
def show_api_key_details(key):
    """Print api key details to stdout."""
    db = configure_db("rw")
    with db_worker_session(db) as session:
        columns = ApiKey.__table__.columns
        fields = [getattr(columns, f) for f in API_KEY_COLUMN_NAMES]
        row = (session.execute(
            select(fields).where(columns.valid_key == key))).fetchone()
        if row is not None:
            key = Key(**dict(row.items()))
        else:
            key = None
    table = []
    for field in API_KEY_COLUMN_NAMES:
        table.append([field, getattr(key, field, "")])

    print_table(table, " : ")
コード例 #4
0
    def __call__(self):
        """Execute the view and return a response."""
        api_key = None
        api_key_text = self.parse_apikey()
        skip_check = False

        if api_key_text is None:
            self.log_count('none')
            if self.error_on_invalidkey:
                raise self.prepare_exception(InvalidAPIKey())

        if api_key_text is not None:
            try:
                api_key = get_key(self.request.db_session, api_key_text)
            except Exception:
                # if we cannot connect to backend DB, skip api key check
                skip_check = True
                self.raven_client.captureException()

        if api_key is not None and api_key.allowed(self.view_type):
            valid_key = api_key.valid_key
            self.log_count(valid_key)

            # Potentially avoid overhead of Redis connection.
            if self.ip_log_and_rate_limit:
                if self.log_ip_and_rate_limited(valid_key, api_key.maxreq):
                    raise self.prepare_exception(DailyLimitExceeded())

        elif skip_check:
            pass
        else:
            if api_key_text is not None:
                self.log_count('invalid')
            if self.error_on_invalidkey:
                raise self.prepare_exception(InvalidAPIKey())

        # If we failed to look up an ApiKey, create an empty one
        # rather than passing None through
        if api_key is None:
            api_key = Key()
        return self.view(api_key)
 def test_empty(self, session_tracker):
     key = Key()
     assert isinstance(key, Key)
     assert key.valid_key is None
     session_tracker(0)