Esempio n. 1
0
def get_build_logs(request_id):
    """
    Retrieve the logs for the build request.

    :param int request_id: the request ID that was passed in through the URL.
    :rtype: flask.Response
    :raise NotFound: if the request is not found or there are no logs for the request
    :raise Gone: if the logs for the build request have been removed due to expiration
    """
    request_log_dir = flask.current_app.config['IIB_REQUEST_LOGS_DIR']
    if not request_log_dir:
        raise NotFound()

    request = Request.query.get_or_404(request_id)
    log_file_path = os.path.join(request_log_dir, f'{request_id}.log')
    if not os.path.exists(log_file_path):
        expired = request.logs_expiration < datetime.utcnow()
        if expired:
            raise Gone(
                f'The logs for the build request {request_id} no longer exist')
        finalized = request.state.state_name in RequestStateMapping.get_final_states(
        )
        if finalized:
            raise NotFound()
        # The request may not have been initiated yet. Return empty logs until it's processed.
        return flask.Response('', mimetype='text/plain')

    with open(log_file_path) as f:
        return flask.Response(f.read(), mimetype='text/plain')
Esempio n. 2
0
def get_request_logs(request_id):
    """
    Retrieve the logs for the Cachito request.

    :param int request_id: the value of the request ID
    :return: a Flask JSON response
    :rtype: flask.Response
    :raise NotFound: if the request is not found
    :raise Gone: if the logs no longer exist
    """
    request_log_dir = flask.current_app.config["CACHITO_REQUEST_FILE_LOGS_DIR"]
    if not request_log_dir:
        raise NotFound()
    request = Request.query.get_or_404(request_id)
    log_file_path = os.path.join(request_log_dir, f"{request_id}.log")
    if not os.path.exists(log_file_path):
        if request.state.state_name == "stale":
            raise Gone(
                f"The logs for the Cachito request {request_id} no longer exist"
            )
        finalized = request.state.state_name in RequestStateMapping.get_final_states(
        )
        if finalized:
            raise NotFound()
        # The request may not have been initiated yet. Return empty logs until it's processed.
        return flask.Response("", mimetype="text/plain")

    return flask.Response(stream_with_context(
        generate_stream_response(log_file_path)),
                          mimetype="text/plain")
Esempio n. 3
0
def update_registration_status():
    """
    Updates the email registration status
    ---
    deprecated: true
    description: >
        Deprecated, all calls to this endpoint will result in a 410 (Gone)!
    tags:
        - email
    parameters:
        - name: token
          in: query
          schema:
            type: string
          required: true
    responses:
        410:
            description: Gone
        200:
            description: OK
        404:
            description: No Hacker exists with that email!
        5XX:
            description: Unexpected error.
    """

    raise Gone("Hacker emails are no longer being verified. "
               "If you have arrived here after clicking the link "
               "in your email, you don't need to do anything and "
               "you may safely close this window. "
               "If you have any questions or are experiencing "
               "any issues, please contact us at [email protected]")
Esempio n. 4
0
def send_registration_email(email: str):
    """
    Sends a registration email to the hacker.
    ---
    deprecated: true
    description: >
        Deprecated, all calls to this endpoint will result in a 410 (Gone)!
    tags:
        - email
    parameters:
        - name: email
          in: path
          schema:
            type: string
            format: email
          required: true
    responses:
        410:
            description: Gone
        201:
            description: OK
        404:
            description: No Hacker exists with that hackername!
        5XX:
            description: Unexpected error.
    """

    raise Gone("Hacker emails are no longer being verified.")
Esempio n. 5
0
File: api_v1.py Progetto: zanssa/iib
def _get_artifact_file_from_s3_bucket(s3_key_prefix, s3_file_name, request_id,
                                      request_temp_data_expiration_date,
                                      s3_bucket_name):
    """
    It's a helper function to get artifact file from S3 bucket.

    :param str s3_key_prefix: the logical location of the file in the S3 bucket
    :param str s3_file_name: the name of the file in S3 bucket
    :param int request_id: the request ID of the request in question
    :param str request_temp_data_expiration_date: expiration date of the temporary data
        for the request in question
    :param str s3_bucket_name: the name of the S3 bucket in AWS
    :raise NotFound: if the request is not found or there are no logs for the request
    :raise Gone: if the logs for the build request have been removed due to expiration
    :rtype: botocore.response.StreamingBody
    :return: streaming body of the file fetched from AWS S3 bucket
    """
    artifact_file = get_object_from_s3_bucket(s3_key_prefix, s3_file_name,
                                              s3_bucket_name)
    if artifact_file:
        return artifact_file

    expired = request_temp_data_expiration_date < datetime.utcnow()
    if expired:
        raise Gone(
            f'The data for the build request {request_id} no longer exist')
    raise NotFound()
Esempio n. 6
0
def check_verification_status(email: str):
    """
    Checks the email verification status
    ---
    deprecated: true
    description: >
        Deprecated, all calls to this endpoint will result in a 410 (Gone)!
    tags:
        - email
    parameters:
        - name: email
          in: path
          schema:
            type: string
          required: true
    responses:
        410:
            description: Gone
        200:
            description: OK
        401:
            description: Unauthorized
        404:
            description: No Hacker exists with that email!
    """

    raise Gone("Hacker emails are no longer being verified.")
Esempio n. 7
0
def authenticate_session(s_id, s_key):
    """Authenticates a user request using a cookie"""

    session = UserSession.query.filter_by(id=s_id).first()

    if not session:
        raise Unauthorized("Session not found")

    if not bcrypt.checkpw(s_key.encode('utf8'), session.key):
        DB.session.delete(session)
        DB.session.commit()
        raise Unauthorized("Invalid session key")

    if datetime.datetime.today().timestamp() > session.expiry:
        DB.session.delete(session)
        DB.session.commit()
        raise Unauthorized("Session expired")

    user = User.query.filter_by(id=session.user_id).first()
    if not user:
        DB.session.delete(session)
        DB.session.commit()
        raise Gone("User is missing (possibly deleted)")

    return user.username
Esempio n. 8
0
File: api_v1.py Progetto: zanssa/iib
def get_build_logs(request_id):
    """
    Retrieve the logs for the build request.

    :param int request_id: the request ID that was passed in through the URL.
    :rtype: flask.Response
    :raise NotFound: if the request is not found or there are no logs for the request
    :raise Gone: if the logs for the build request have been removed due to expiration
    :raise ValidationError: if the request has not completed yet
    """
    request_log_dir = flask.current_app.config['IIB_REQUEST_LOGS_DIR']
    s3_bucket_name = flask.current_app.config['IIB_AWS_S3_BUCKET_NAME']
    if not s3_bucket_name and not request_log_dir:
        raise NotFound()

    request = Request.query.get_or_404(request_id)

    finalized = request.state.state_name in RequestStateMapping.get_final_states(
    )
    if not finalized:
        raise ValidationError(
            f'The request {request_id} is not complete yet.'
            ' logs will be available once the request is complete.')

    # If S3 bucket is configured, fetch the log file from the S3 bucket.
    # Else, check if logs are stored on the system itself and return them.
    # Otherwise, raise an IIBError.
    if s3_bucket_name:
        log_file = _get_artifact_file_from_s3_bucket(
            'request_logs',
            f'{request_id}.log',
            request_id,
            request.temporary_data_expiration,
            s3_bucket_name,
        )
        return flask.Response(log_file.read(), mimetype='text/plain')

    local_log_file_path = os.path.join(request_log_dir, f'{request_id}.log')
    if not os.path.exists(local_log_file_path):
        expired = request.temporary_data_expiration < datetime.utcnow()
        if expired:
            raise Gone(
                f'The logs for the build request {request_id} no longer exist')
        flask.current_app.logger.warning(
            ' Please make sure either an S3 bucket is configured or the logs are'
            ' stored locally in a directory by specifying IIB_REQUEST_LOGS_DIR'
        )
        raise IIBError(
            'IIB is done processing the request and could not find logs.')

    with open(local_log_file_path) as f:
        return flask.Response(f.read(), mimetype='text/plain')
Esempio n. 9
0
 def error_generator():
     if "type" in request.args:
         if request.args.get("type") == "server_error":
             raise InternalServerError("InternalServerError Test Message")
         if request.args.get("type") == "bad_request":
             raise BadRequest("BadRequest Test Message")
         if request.args.get("type") == "gone":
             raise Gone("Gone Test Message")
         if request.args.get("type") == "unauthorized":
             raise Unauthorized("Unauthorized Test Message")
         if request.args.get("type") == "forbidden":
             raise Forbidden("Forbidden Test Message")
     return jsonify({"message": "Nothing bad happened."}), 200
Esempio n. 10
0
def get_related_bundles(request_id):
    """
    Retrieve the related bundle images from the bundle CSV for a regenerate-bundle request.

    :param int request_id: the request ID that was passed in through the URL.
    :rtype: flask.Response
    :raise NotFound: if the request is not found or there are no related bundles for the request
    :raise Gone: if the related bundles for the build request have been removed due to expiration
    """
    request_related_bundles_dir = flask.current_app.config['IIB_REQUEST_RELATED_BUNDLES_DIR']
    if not request_related_bundles_dir:
        raise NotFound()

    request = Request.query.get_or_404(request_id)
    if request.type != RequestTypeMapping.regenerate_bundle.value:
        raise ValidationError(
            f'The request {request_id} is of type {request.type_name}. '
            'This endpoint is only valid for requests of type regenerate-bundle.'
        )

    finalized = request.state.state_name in RequestStateMapping.get_final_states()
    if not finalized:
        raise ValidationError(
            f'The request {request_id} is not complete yet.'
            ' related_bundles will be available once the request is complete.'
        )

    related_bundles_file_path = os.path.join(
        request_related_bundles_dir, f'{request_id}_related_bundles.json'
    )
    if not os.path.exists(related_bundles_file_path):
        expired = request.temporary_data_expiration < datetime.utcnow()
        if expired:
            raise Gone(f'The related_bundles for the build request {request_id} no longer exist')
        raise IIBError(
            'IIB is done processing the request and cannot find related_bundles. Please make '
            f'sure the iib_organizaiton_customizations for organization {request.organization}'
            ' has related_bundles customization type set'
        )

    with open(related_bundles_file_path) as f:
        return flask.Response(f.read(), mimetype='application/json')
Esempio n. 11
0
def download_result(task_id):
    if task_id not in session.get('tasks', []):
        app.logger.debug("Valid tasks %r" % session.get('tasks', []))
        raise NotFound()

    try:
        result = handle_pdfs_task.AsyncResult(task_id)
        if result.ready():
            if hasattr(result, 'available') and not result.available():
                raise Gone("Result expired. "
                        "You probably waited too long to download the file.")

            if result.successful():
                output = result.result
                return _respond_with_pdf(output.decode('zlib'))
            else:
                app.logger.debug("Result not successful: %r" % result.result)
                raise InternalServerError(unicode(result.result))
    except NotRegistered:
        app.logger.debug("Result not registered %r" % task_id)
        raise NotFound()

    return redirect(url_for('result_page', task_id=task_id))
Esempio n. 12
0
def delete(card_id, id):
    authz.require(authz.logged_in())
    reference = obj_or_404(Reference.by_id(id, card_id=card_id))
    db.session.delete(reference)
    db.session.commit()
    raise Gone()
Esempio n. 13
0
File: api_v1.py Progetto: zanssa/iib
def get_related_bundles(request_id):
    """
    Retrieve the related bundle images from the bundle CSV for a regenerate-bundle request.

    :param int request_id: the request ID that was passed in through the URL.
    :rtype: flask.Response
    :raise NotFound: if the request is not found or there are no related bundles for the request
    :raise Gone: if the related bundles for the build request have been removed due to expiration
    :raise ValidationError: if the request is of invalid type or is not completed yet
    """
    request_related_bundles_dir = flask.current_app.config[
        'IIB_REQUEST_RELATED_BUNDLES_DIR']
    s3_bucket_name = flask.current_app.config['IIB_AWS_S3_BUCKET_NAME']
    if not s3_bucket_name and not request_related_bundles_dir:
        raise NotFound()

    request = Request.query.get_or_404(request_id)
    if request.type != RequestTypeMapping.regenerate_bundle.value:
        raise ValidationError(
            f'The request {request_id} is of type {request.type_name}. '
            'This endpoint is only valid for requests of type regenerate-bundle.'
        )

    finalized = request.state.state_name in RequestStateMapping.get_final_states(
    )
    if not finalized:
        raise ValidationError(
            f'The request {request_id} is not complete yet.'
            ' related_bundles will be available once the request is complete.')

    # If S3 bucket is configured, fetch the related bundles file from the S3 bucket.
    # Else, check if related bundles are stored on the system itself and return them.
    # Otherwise, raise an IIBError.
    if s3_bucket_name:
        log_file = _get_artifact_file_from_s3_bucket(
            'related_bundles',
            f'{request_id}_related_bundles.json',
            request_id,
            request.temporary_data_expiration,
            s3_bucket_name,
        )
        return flask.Response(log_file.read(), mimetype='application/json')

    related_bundles_file_path = os.path.join(
        request_related_bundles_dir, f'{request_id}_related_bundles.json')
    if not os.path.exists(related_bundles_file_path):
        expired = request.temporary_data_expiration < datetime.utcnow()
        if expired:
            raise Gone(
                f'The related_bundles for the build request {request_id} no longer exist'
            )
        if request.organization:
            raise IIBError(
                'IIB is done processing the request and cannot find related_bundles. Please make '
                f'sure the iib_organization_customizations for organization {request.organization}'
                ' has related_bundles customization type set')
        flask.current_app.logger.warning(
            ' Please make sure either an S3 bucket is configured or the logs are'
            ' stored locally in a directory by specifying IIB_REQUEST_LOGS_DIR'
        )
        raise IIBError(
            'IIB is done processing the request and could not find related_bundles.'
        )

    with open(related_bundles_file_path) as f:
        return flask.Response(f.read(), mimetype='application/json')
Esempio n. 14
0
def delete(parent_id, id):
    authz.require(authz.logged_in())
    link = obj_or_404(Link.by_id(id, parent_id=parent_id))
    db.session.delete(link)
    db.session.commit()
    raise Gone()
Esempio n. 15
0
def delete(id):
    authz.require(authz.logged_in())
    card = obj_or_404(Card.by_id(id))
    db.session.delete(card)
    db.session.commit()
    raise Gone()