Ejemplo n.º 1
0
def _handle_post(req, req_args):
    """Private method for handling an HTTP POST to this endpoint."""
    req_tuple = ProvTuple(req_args['uuid'], req_args['service_name'],
                        req_args['category_name'], req_args['event_name'],
                        req_args['username'], req.remote_addr)
    req_tuple.proxy_username = req.params.get('proxy_username')
    req_tuple.event_data = req.params.get('event_data')
    req_tuple.track_history = req.params.get('track_history')
    req_tuple.track_history_code = req.params.get('track_history_code')
    req_tuple.version = req.params.get('version')

    # if the above was an object, a json.dumps() call would create this
    # same string that is being contenated and it would include key names
    all_data = str(req_tuple)
    info_msg = "Received provenance request: " + all_data
    log_info(info_msg)

    validated, details = validate(req_tuple)

    info_msg = "Validation:" + str(validated) + " Details: " + str(details)
    log_info(info_msg)

    if validated:
        json_data, webstatus = process_valid_request(req_tuple)
    else:
        json_data = json.dumps({'result': {'Status': 'Failed',
                               'Details': 'Validation Failed',
                               'Report': details}}, indent=4)
        webstatus = '400 Bad Request'

    return (json_data, webstatus)
def _handle_post(request, routing_args):
    """
    Private method for processing an HTTP POST to the WSGI endpoint
    encapsulated in this file.
    """
    # we're not using the service parameters from the URL in routing_args
    log_info(routing_args)
    # the is the possibility that values for [service_name, category_name,
    # event_name] could conflict with the values contained in the body.

    # let's grab the POST body, if it's there...
    prov, json_data, webstatus = _get_post_body(request)

    if prov.uuid is None:
        # we look for object_* and insert, fetch uuid
        prov.uuid = register_object(prov.service_key, prov.object_id,
                                    prov.object_name, prov.object_desc,
                                    prov.parent_uuid)
    # then log provenance
    succeeded = commit_provenance(prov)

    if succeeded and prov.uuid is not None:
        webstatus = '200 OK'
        json_data = json.dumps({'result': {'Status': 'Success',
                                'Details': 'Provenance recorded' }},
                                indent=4)
    else:
        webstatus = '418 I am a teapot'
        if json_data is None:
            json_data = json.dumps({'result': {'Status': 'Failed',
                                'Details': 'Something went horribly wrong' }},
                                indent=4)

    return json_data, webstatus
def get_uuid(obj_data):
    """
    Provide a universal unique identifier.
    """
    log_info(obj_data)
    # return get_ghetto_uuid() # <= if you don't have snowflake deployed
    return get_snowflake_uuid()
def application(environ, start_response):
    """Endpoint for creating provenance records given a service, a
    category, and an event.

    These elements must be added prior to registering an object and
    creating any provenance information.

    Also included for additional tracking and bookkeeping are:
    ``username`` - this may be a system or daemon account
    ``proxy_username`` - the user as system account is acting on behalf of
    ``event_data`` - any additional info associated with the event
    ``request_ipaddress`` - the IP address of the service posting data
    ``track_history`` - boolean-like code indicating to track history
    ``track_history_code`` - the association code for history tracking
    ``created_date`` - time in seconds since Epoch
    ``version`` - string identifier indicating version
    """
    req = Request(environ)
    args, kwargs = environ["wsgiorg.routing_args"]
    if len(args) > 0:
        log_info("Positional arguments include: " + args)

    if req.method == "POST":
        json_data, webstatus = _handle_post(req, kwargs)
    else:
        webstatus = "405 Method Not Allowed"
        json_data = json.dumps({"result": {"Status": "Failed", "Details": "Incorrect HTTP METHOD"}}, indent=4)

    info_msg = "Request Processed: " + str(webstatus) + " Details: " + str(json_data)
    log_info(info_msg)

    response_headers = [("Content-Type", "application/json"), ("Content-Length", len(json_data))]
    start_response(webstatus, response_headers)
    return json_data
Ejemplo n.º 5
0
def application(environ, start_response):
    """
    Endpoint for a 'fire-and-forget' style provenance logging
    interaction.

    This endpoint will expect the union of the information associated
    with a call to `/register` and `/provenance`.  This is, all required
    parameters of both endpoints will need to be included such that if a
    UUID is not included, a "multi-action" operation can be performed.

    This means that the conversational nature of the original design is
    overted and a calling semantic that does not require statefulness
    can be employed by clients calling this API.
    """
    req = Request(environ)
    args, kwargs = environ['wsgiorg.routing_args']
    if (len(args) > 0):
        log_info('Positional arguments include: ' + args)

    if (req.method == 'POST'):
        json_data, webstatus = _handle_post(req, kwargs)
    else:
        webstatus = '405 Method Not Allowed'
        json_data = json.dumps({'result': {'Status': 'Failed',
                                'Details': 'Incorrect HTTP METHOD'}},
                               indent=4)
    info_msg = "Request Processed: " + str(webstatus) + " Details: " + \
        str(json_data)
    log_info(info_msg)

    response_headers = [('Content-type', 'application/json')]
    start_response(webstatus, response_headers)
    return (json_data)
def _register_obj(srv_key, obj_id, obj_name, obj_desc, obj_data, parent_uuid):
    """Handles registration of the given object information."""
    try:
        conn = MySQLdb.connect(host=PROV_DB_HOST,
                               user=PROV_DB_USERNAME,
                               passwd=PROV_DB_PASSWORD,
                               db=PROV_DB_NAME,
                               port=PROV_DB_PORT)
        cursor = conn.cursor()

        log_info(SERVICE_ID_FROM_KEY_QUERY % (srv_key))
        cursor.execute(SERVICE_ID_FROM_KEY_QUERY % (srv_key))
        key_to_id = cursor.fetchone()
        srv_id, = key_to_id

        log_info(OBJECT_QUERY_UUID_LOOKUP % (obj_id, srv_id))
        cursor.execute(OBJECT_QUERY_UUID_LOOKUP % (obj_id, srv_id))
        results = cursor.fetchone()
        log_info(results)

        if results is None:
            uuid_ = get_uuid(obj_data)
            log_info(uuid_)

            if uuid_ == None:
                log_errors("Obj UUID is null: " + obj_data)
                failed_inserts_audit(obj_data)
                data_string = json.dumps(
                    {
                        'Status': 'Failed',
                        'Details': 'Error retrieving UUID'
                    },
                    indent=4)
                webstatus = '503 Service Unavailable'
            else:
                data_string, webstatus = _insert_object(
                    cursor, uuid_, srv_id, obj_id, obj_name, obj_desc,
                    parent_uuid)
        else:
            for value in results:
                info_msg = "Lookup: Object Exists" + " " + str(value)
                log_info(info_msg)
                data_string = json.dumps({'UUID': str(value)})
                webstatus = '200 OK'
        cursor.close()
    except Exception, e:
        err_msg = "MySQL DB Exception: " + " " + \
            str(e) + " " + obj_data
        log_exception(err_msg)
        failed_inserts_audit(obj_data)

        data_string = json.dumps(
            {
                'Status': 'Failed',
                'Details': 'MySQL Exception. Failed' + 'to retrieve data'
            },
            indent=4)
        webstatus = '500 Internal Server Error'
def _register_obj(srv_key, obj_id, obj_name, obj_desc, obj_data, parent_uuid):
    """Handles registration of the given object information."""
    try:
        conn = MySQLdb.connect(host=PROV_DB_HOST, user=PROV_DB_USERNAME,
                               passwd=PROV_DB_PASSWORD, db=PROV_DB_NAME,
                               port=PROV_DB_PORT)
        cursor = conn.cursor()

        log_info(SERVICE_ID_FROM_KEY_QUERY % (srv_key))
        cursor.execute(SERVICE_ID_FROM_KEY_QUERY % (srv_key))
        key_to_id = cursor.fetchone()
        srv_id, = key_to_id

        log_info(OBJECT_QUERY_UUID_LOOKUP % (obj_id, srv_id))
        cursor.execute(OBJECT_QUERY_UUID_LOOKUP % (obj_id, srv_id))
        results = cursor.fetchone()
        log_info(results)

        if results is None:
            uuid_ = get_uuid(obj_data)
            log_info(uuid_)

            if uuid_ == None:
                log_errors("Obj UUID is null: " + obj_data)
                failed_inserts_audit(obj_data)
                data_string = json.dumps({'Status': 'Failed',
                                         'Details':
                                         'Error retrieving UUID'},
                                         indent=4)
                webstatus = '503 Service Unavailable'
            else:
                data_string, webstatus = _insert_object(cursor, uuid_, srv_id,
                                                        obj_id, obj_name,
                                                        obj_desc, parent_uuid)
        else:
            for value in results:
                info_msg = "Lookup: Object Exists" + " " + str(value)
                log_info(info_msg)
                data_string = json.dumps({'UUID': str(value)})
                webstatus = '200 OK'
        cursor.close()
    except Exception, e:
        err_msg = "MySQL DB Exception: " + " " + \
            str(e) + " " + obj_data
        log_exception(err_msg)
        failed_inserts_audit(obj_data)

        data_string = json.dumps({'Status': 'Failed',
                                  'Details': 'MySQL Exception. Failed' +
                                  'to retrieve data'}, indent=4)
        webstatus = '500 Internal Server Error'
def application(environ, start_response):
    """Endpoint for easy registration of provenance objects.

    It will perform the lookup, if the object does not exists, it will
    register the object return the UUID value.

    Call returns UUID if the object is found else, returns 404 NOT FOUND
    is the status code for the response.

    Keyword Arguments Defined:
    ``service_key`` - shortened service alpha-numberic identifier
    ``object_id`` - object identifier (used as a composite primary key)
    ``object_name`` - name of the object
    ``object_desc`` - description of the object
    ``parent_uuid`` - the UUID of the parent object (optional)

    ``service_key`` will be used to fetch the ``service_id`` for the
    insert, as the ``service_key`` has a 'unique' constraint on it
    within the ``Service`` table in the ``provenance`` database.

    For more information on the arguments, see the URL definition in
    ``police_box.py``.

    Note: if the object exists, the parameters ``object_name`` and
    ``object_desc`` are ignored and the existing UUID is returned.
    """
    req = Request(environ)

    # request got routed to us, let's grab the args associated
    args, kwargs = environ['wsgiorg.routing_args']
    if (len(args) > 0):
        log_info('We should not have positional arguments: ' + args)

    if (req.method == 'POST'):
        data_string, webstatus = _handle_post(kwargs)

    else:
        data_string = json.dumps(
            {
                'Status': 'Failed',
                'Details': 'Incorrect HTTP METHOD'
            }, indent=4)
        webstatus = '405 Method Not Allowed'

    response_headers = [('Content-Type', 'application/json'),
                        ('Content-Length', len(data_string))]
    start_response(webstatus, response_headers)
    return (data_string)
def application(environ, start_response):
    """Endpoint for easy registration of provenance objects.

    It will perform the lookup, if the object does not exists, it will
    register the object return the UUID value.

    Call returns UUID if the object is found else, returns 404 NOT FOUND
    is the status code for the response.

    Keyword Arguments Defined:
    ``service_key`` - shortened service alpha-numberic identifier
    ``object_id`` - object identifier (used as a composite primary key)
    ``object_name`` - name of the object
    ``object_desc`` - description of the object
    ``parent_uuid`` - the UUID of the parent object (optional)

    ``service_key`` will be used to fetch the ``service_id`` for the
    insert, as the ``service_key`` has a 'unique' constraint on it
    within the ``Service`` table in the ``provenance`` database.

    For more information on the arguments, see the URL definition in
    ``police_box.py``.

    Note: if the object exists, the parameters ``object_name`` and
    ``object_desc`` are ignored and the existing UUID is returned.
    """
    req = Request(environ)

    # request got routed to us, let's grab the args associated
    args, kwargs = environ['wsgiorg.routing_args']
    if (len(args) > 0):
        log_info('We should not have positional arguments: ' + args)

    if (req.method == 'POST'):
        data_string, webstatus = _handle_post(kwargs)

    else:
        data_string = json.dumps({'Status': 'Failed', 'Details':
                                 'Incorrect HTTP METHOD'}, indent=4)
        webstatus = '405 Method Not Allowed'

    response_headers = [('Content-Type', 'application/json'),
                        ('Content-Length', len(data_string))]
    start_response(webstatus, response_headers)
    return (data_string)
Ejemplo n.º 10
0
def _handle_post(request, routing_args):
    """
    Private method for processing an HTTP POST to the WSGI endpoint
    encapsulated in this file.
    """
    # we're not using the service parameters from the URL in routing_args
    log_info(routing_args)
    # the is the possibility that values for [service_name, category_name,
    # event_name] could conflict with the values contained in the body.

    # let's grab the POST body, if it's there...
    prov, json_data, webstatus = _get_post_body(request)

    if prov.uuid is None:
        # we look for object_* and insert, fetch uuid
        prov.uuid = register_object(prov.service_key, prov.object_id,
                                    prov.object_name, prov.object_desc,
                                    prov.parent_uuid)
    # then log provenance
    succeeded = commit_provenance(prov)

    if succeeded and prov.uuid is not None:
        webstatus = '200 OK'
        json_data = json.dumps(
            {
                'result': {
                    'Status': 'Success',
                    'Details': 'Provenance recorded'
                }
            },
            indent=4)
    else:
        webstatus = '418 I am a teapot'
        if json_data is None:
            json_data = json.dumps(
                {
                    'result': {
                        'Status': 'Failed',
                        'Details': 'Something went horribly wrong'
                    }
                },
                indent=4)

    return json_data, webstatus
def commit_provenance(req_tuple):
    """
    Inserts provenance.

    We could say that this method "logs provenance" but that would
    confuse the matter what _provenance_ is not the same as "logging".

    This method is committing some historical information regarding the
    actions or operations processed on an object that has been
    registered in the ``Objects`` table.

    Note: this was extracted as a method so that it could be called
    from another WSGI endpoint application that is performing a
    similar, but not 100 percent identifical operation.
    """
    json_data, webstatus = _add_valid_tuple(req_tuple)
    log_info(webstatus + " " + json_data)
    # if we've got an ``OK`` then we can assume that it was inserted
    return webstatus == "200 OK"  # again, I hate to do this comparison
Ejemplo n.º 12
0
def application(environ, start_response):
    """
    Endpoint for a 'fire-and-forget' style of committing provenance.

    This is the desired interaction from the service engineers that will
    be using the TARDIS APIs.

    This endpoint will expect the union of the information associated
    with a call to `/register` and `/provenance`.  This is, all required
    parameters of both endpoints will need to be included such that if a
    UUID is not included, a "multi-action" operation can be performed.

    This means that the conversational nature of the original design is
    overted and a calling semantic that does not require statefulness
    can be employed by clients calling this API.
    """
    req = Request(environ)
    args, kwargs = environ['wsgiorg.routing_args']

    if (len(args) > 0):
        log_info('Positional arguments include: ' + args)

    if (req.method == 'POST'):
        json_data, webstatus = _handle_post(req, kwargs)
    else:
        webstatus = '405 Method Not Allowed'
        json_data = json.dumps(
            {
                'result': {
                    'Status': 'Failed',
                    'Details': 'Incorrect HTTP METHOD'
                }
            },
            indent=4)
    info_msg = "Request Processed: " + str(webstatus) + " Details: " + \
        str(json_data)
    log_info(info_msg)

    response_headers = [('Content-Type', 'application/json'),
                        ('Content-Length', len(json_data))]
    start_response(webstatus, response_headers)
    return (json_data)
def _add_valid_tuple(req_tuple):
    """
    If we've got a valid request tuple, we add it - like a boss.

    This was factored out of _handle_post() so that a public method,
    ``commit_provenance()`` could reuse it.
    """
    validated, details = validate(req_tuple)

    info_msg = "Validation:" + str(validated) + " Details: " + str(details)
    log_info(info_msg)

    if validated:
        json_data, webstatus = _process_valid_request(req_tuple)
    else:
        json_data = json.dumps(
            {"result": {"Status": "Failed", "Details": "Validation Failed", "Report": details}}, indent=4
        )
        webstatus = "400 Bad Request"
    return (json_data, webstatus)
Ejemplo n.º 14
0
def _insert_object(cursor, obj_uuid, obj_id, obj_name, obj_desc, parent_uuid):
    """
    Handle the insertion of a new object.
    """
    insert = ''

    if parent_uuid is None:
        insert = OBJECT_QUERY_UUID_INSERT % (obj_uuid, obj_id, obj_name,
                                             obj_desc)
    else:
        insert = OBJECT_QUERY_UUID_INSERT_PARENT % (obj_uuid, obj_id, obj_name,
                                                    obj_desc, parent_uuid)
    log_info(insert)
    cursor.execute(insert)

    info_msg = "Object created: " + " " + str(obj_uuid)
    log_info(info_msg)
    data_string = json.dumps({'UUID': str(obj_uuid)}, indent=4)
    webstatus = '200 OK'

    return data_string, webstatus
def _insert_object(cursor, obj_uuid, srv_id, obj_id, obj_name, obj_desc,
                   parent_uuid):
    """
    Handle the insertion of a new object.
    """
    insert = ''

    if parent_uuid is None:
        insert = OBJECT_QUERY_UUID_INSERT % (obj_uuid, srv_id, obj_id,
                                             obj_name, obj_desc)
    else:  # parent's gotta parent!
        insert = OBJECT_QUERY_UUID_INSERT_PARENT % (
            obj_uuid, srv_id, obj_id, obj_name, obj_desc, parent_uuid)
    log_info(insert)
    cursor.execute(insert)

    info_msg = "Object created: " + " " + str(obj_uuid)
    log_info(info_msg)
    data_string = json.dumps({'UUID': str(obj_uuid)}, indent=4)
    webstatus = '200 OK'

    return data_string, webstatus
def _handle_post(req, req_args):
    """Private method for handling an HTTP POST to this endpoint."""
    req_tuple = ProvTuple(
        req_args["uuid"],
        req_args["service_name"],
        req_args["category_name"],
        req_args["event_name"],
        req_args["username"],
        req.remote_addr,
    )
    req_tuple.proxy_username = req.params.get("proxy_username")
    req_tuple.event_data = req.params.get("event_data")
    req_tuple.track_history = req.params.get("track_history")
    req_tuple.track_history_code = req.params.get("track_history_code")
    req_tuple.version = req.params.get("version")

    # if the above was an object, a json.dumps() call would create this
    # same string that is being contenated and it would include key names
    all_data = str(req_tuple)
    info_msg = "Received provenance request: " + all_data
    log_info(info_msg)

    return _add_valid_tuple(req_tuple)
Ejemplo n.º 17
0
def process_valid_request(req_tuple):

    if req_tuple.version == None:
        req_tuple.version = "Default"

    event_id = get_id(req_tuple.event_name, "EVENT", req_tuple.version)
    category_id = get_id(req_tuple.category_name, "CATEGORY",
                        req_tuple.version)
    service_id = get_id(req_tuple.service_name, "SERVICE", req_tuple.version)

    if event_id != "EMPTY" and category_id != "EMPTY" and service_id != "EMPTY":

        all_data = str(req_tuple)

        try:
            conn = MySQLdb.connect(
                host=PROV_DB_HOST, user=PROV_DB_USERNAME,
                passwd=PROV_DB_PASSWORD, db=PROV_DB_NAME)
            cursor = conn.cursor()

            cursor.execute(QUERY_CHECK_UUID % (req_tuple.uuid))
            check_results = cursor.fetchall()
            if len(check_results) == 1:

                if (req_tuple.proxy_username is None and
                    req_tuple.event_data is None):
                    insert_status = cursor.execute(
                        QUERY_NO_PROXY_DATA % (req_tuple.uuid,
                                                req_tuple.event_id,
                                                req_tuple.category_id,
                                                req_tuple.service_id,
                                                req_tuple.username,
                                                req_tuple.request_ipaddress,
                                                req_tuple.created_date))
                    if str(insert_status) == "1":
                        info_msg = "Success: " + all_data
                        log_info(info_msg)
                    else:
                        err_msg = "QUERY_NO_PROXY_DATA query failed" + all_data
                        log_errors(err_msg)
                        audit_insert = cursor.execute(AUDIT_NO_PROXY_DATA
                                                % (req_tuple.uuid,
                                                    req_tuple.event_id,
                                                    req_tuple.category_id,
                                                    req_tuple.service_id,
                                                    req_tuple.username,
                                                    req_tuple.request_ipaddress,
                                                    req_tuple.created_date))
                        if audit_insert != 1:
                            failed_inserts_audit(all_data)

                elif req_tuple.proxy_username != None:
                    insert_status = cursor.execute(QUERY_PROXY
                                                % (req_tuple.uuid,
                                                    req_tuple.event_id,
                                                    req_tuple.category_id,
                                                    req_tuple.service_id,
                                                    req_tuple.username,
                                                    req_tuple.proxy_username,
                                                    req_tuple.request_ipaddress,
                                                    req_tuple.created_date))
                    if str(insert_status) == "1":
                        info_msg = "Success: " + all_data
                        log_info(info_msg)
                    else:
                        err_msg = "QUERY_PROXY query failed" + all_data
                        log_errors(err_msg)
                        audit_insert = cursor.execute(AUDIT_PROXY
                                                % (req_tuple.uuid,
                                                    req_tuple.event_id,
                                                    req_tuple.category_id,
                                                    req_tuple.service_id,
                                                    req_tuple.username,
                                                    req_tuple.proxy_username,
                                                    req_tuple.request_ipaddress,
                                                    req_tuple.created_date))
                        if audit_insert != 1:
                            failed_inserts_audit(all_data)

                elif req_tuple.event_data != None:

                    insert_status = cursor.execute(QUERY_DATA
                                                   % (req_tuple.uuid,
                                                    req_tuple.event_id,
                                                    req_tuple.category_id,
                                                    req_tuple.service_id,
                                                    req_tuple.username,
                                                    req_tuple.event_data,
                                                    req_tuple.request_ipaddress,
                                                    req_tuple.created_date))
                    if str(insert_status) == "1":
                        info_msg = "Success: " + all_data
                        log_info(info_msg)
                    else:
                        err_msg = "QUERY_DATA query failed" + all_data
                        log_errors(err_msg)
                        audit_insert = cursor.execute(AUDIT_DATA
                                                % (req_tuple.uuid,
                                                    req_tuple.event_id,
                                                    req_tuple.category_id,
                                                    req_tuple.service_id,
                                                    req_tuple.username,
                                                    req_tuple.event_data,
                                                    req_tuple.request_ipaddress,
                                                    req_tuple.created_date))
                        if audit_insert != 1:
                            failed_inserts_audit(all_data)

                else:
                    insert_status = cursor.execute(QUERY_ALL
                                                % (req_tuple.uuid,
                                                req_tuple.event_id,
                                                req_tuple.category_id,
                                                req_tuple.service_id,
                                                req_tuple.username,
                                                req_tuple.proxy_username,
                                                req_tuple.event_data,
                                                req_tuple.request_ipaddress,
                                                req_tuple.created_date))
                    if str(insert_status) == "1":
                        info_msg = "Success: " + all_data
                        log_info(info_msg)
                    else:
                        err_msg = "QUERY_ALL query failed" + all_data
                        log_errors(err_msg)
                        audit_insert = cursor.execute(AUDIT_ALL
                                                    % (req_tuple.uuid,
                                                    req_tuple.event_id,
                                                    req_tuple.category_id,
                                                    req_tuple.service_id,
                                                    req_tuple.username,
                                                    req_tuple.proxy_username,
                                                    req_tuple.event_data,
                                                    req_tuple.request_ipaddress,
                                                    req_tuple.created_date))
                        if audit_insert != 1:
                            failed_inserts_audit(all_data)

                if req_tuple.track_history == "1":

                    if req_tuple.track_history_code != None:
                        history_data = str(
                            req_tuple.track_history_code) + " " + all_data

                        cursor.execute(HIST_SELECT_QUERY %
                                      (req_tuple.track_history_code))
                        results = cursor.fetchall()
                        if len(results) != 0:
                            hist_status = cursor.execute(HIST_INSERT_QUERY
                                            % (req_tuple.track_history_code,
                                                req_tuple.uuid,
                                                req_tuple.event_id,
                                                req_tuple.category_id,
                                                req_tuple.service_id,
                                                req_tuple.username,
                                                req_tuple.created_date))
                            if str(hist_status) == "1":
                                info_msg = ("History request recorded:" + " " +
                                            str(req_tuple.track_history_code) +
                                            " " + all_data)
                                log_info(info_msg)
                            else:
                                err_msg = "HIST_INSERT_QUERY failed" + \
                                    history_data
                                log_errors(err_msg)
                                track_history_errors(history_data)
                        else:
                            parent_query = "Y"
                            hist_status = cursor.execute(
                                HIST_INSERT_QUERY_PARENT % (
                                    req_tuple.track_history_code,
                                    req_tuple.uuid, req_tuple.event_id,
                                    req_tuple.category_id, req_tuple.service_id,
                                    req_tuple.username, req_tuple.created_date,
                                    parent_query))
                            if str(hist_status) == "1":
                                info_msg = ("History request recorded:" + " " +
                                            str(req_tuple.track_history_code) +
                                            " " + all_data)
                                log_info(info_msg)
                            else:
                                err_msg = "HIST_INSERT_QUERY_PARENT failed" + \
                                    history_data
                                log_errors(err_msg)
                                track_history_errors(history_data)

                    else:
                        history_data = req_tuple.get_history_data()
                        history_code = get_history_code(history_data)
                        info_msg = "History code generated: " + \
                            str(history_code) + " " + all_data
                        log_info(info_msg)
                else:
                    if req_tuple.track_history_code != None:
                        err_msg = ("Track History flag not set but history " +
                                   "code was sent. Please check history " +
                                   "tracking error logs. " +
                                   str(req_tuple.track_history_code))
                        log_errors(err_msg)
                        history_data = str(req_tuple.track_history_code) + ","\
                                     + str(all_data)
                        track_history_errors(history_data)

                cursor.close()

                webstatus = '200 OK'
                if (req_tuple.track_history == "1" and
                    req_tuple.track_history_code == None):
                    data = json.dumps({'result': {'Status': 'Success',
                                      'Details': 'Provenance recorded',
                                      'History code': history_code}},
                                      indent=4)
                elif (req_tuple.track_history == None and
                      req_tuple.track_history_code != None):
                    data = json.dumps({'result': {'Status': 'Success',
                                      'Details': 'Provenance recorded',
                                      'Warning': 'Track history flag is not' +
                                      'set but history code was sent'}},
                                      indent=4)
                else:
                    data = json.dumps({'result': {'Status': 'Success',
                                      'Details': 'Provenance recorded'}},
                                      indent=4)

                return (data, webstatus)

            else:
                cursor.close()
                webstatus = '500 Internal Server Error'
                data = json.dumps({'result': {'Status': 'Failed', 'Details':
                                  'Provenance not recorded', 'Report':
                                  'More than one record found for given ' +
                                  ' UUID. Support has been notified'}},
                                  indent=4)
                err_msg = "Duplicate UUID enery found: " + all_data
                # notify_support
                log_errors(err_msg)
                failed_inserts_audit(all_data)
                return (data, webstatus)

        except Exception as exc:
            err_msg = "EXCEPTION: " + str(exc) + ": " + all_data
            log_exception(err_msg)
            audit_insert = cursor.execute(
                AUDIT_ALL % (req_tuple.uuid, req_tuple.event_id,
                            req_tuple.category_id, req_tuple.service_id,
                            req_tuple.username, req_tuple.proxy_username,
                            req_tuple.event_data, req_tuple.request_ipaddress,
                            req_tuple.created_date))
            if audit_insert != 1:
                failed_inserts_audit(all_data)

            cursor.close()

            webstatus = '500 Internal Server Error'
            data = json.dumps({'result': {'Status': 'Failed', 'Details':
                              'Provenance was not recorded. Audit data ' +
                              'recorded.'}}, indent=4)
            return (data, webstatus)

    else:
        webstatus = '400 Bad Request'
        data = json.dumps({'result': {'Status': 'Failed', 'Details':
                          'Incorrect Service/Category/Event data.'}}, indent=4)
        return (data, webstatus)
Ejemplo n.º 18
0
def insert_audit():
    """<add a docstring when method is fully understood>"""
    scriptname = "Audit"

    try:
        conn = MySQLdb.connect(host=PROV_DB_HOST, user=PROV_DB_USERNAME,
                               passwd=PROV_DB_PASSWORD, db=PROV_DB_NAME,
                               port=PROV_DB_PORT)
        cursor = conn.cursor()
    except:
        err_msg = "Audit: Connection failed to Provenance database."
        track_history_exceptions(err_msg)
        notify_support(err_msg, scriptname)


    try:
        cursor.execute(AUDIT_SELECT % ('N'))
        results = cursor.fetchall()

        # inconsistent indent characters makes this even _harder_ to follow
        # THOU SHALL NOT HAVE 180+ line methods with nesting this DEEP!
        if len(results) != 0:

            for row in results:
                # Python has a built-in `id`, so use _id to avoid redefining.
                _id = int(row[0])
                uuid = int(row[1])
                service_id = int(row[2])
                category_id = int(row[3])
                event_id = int(row[4])
                username = str(row[5])
                proxy_username = str(row[6])
                event_data = str(row[7])
                request_ipaddress = str(row[8])
                created_date = int(row[9])

                all_data = "{Audit row : " + str(_id) + "}"
                # no proxy_username AND no event_data
                if proxy_username is None and event_data is None:
                    insert_status = cursor.execute(QUERY_NO_PROXY_DATA %
                                                (uuid, event_id, category_id,
                                                service_id, username,
                                                request_ipaddress,
                                                created_date))
                    if insert_status == 1:
                        update_status = cursor.execute(AUDIT_UPDATE_STATUS %
                                                        ('Y', _id))
                        if update_status == 1:
                            info_msg = "Audit Success: " + all_data
                            log_info(info_msg)
                        else:
                            err_msg = ("Audit Update: AUDIT_UPDATE_STATUS " +
                                        "query failed" + all_data)
                            log_errors(err_msg)
                            failed_inserts_audit(all_data)
                            notify_support(err_msg, scriptname)
                    else:
                        err_msg = ("Audit: QUERY_NO_PROXY_DATA query failed" +
                                    all_data)
                        log_errors(err_msg)
                        failed_inserts_audit(all_data)
                        notify_support(err_msg, scriptname)
                # no proxy_username case (inside the 'if len > 0')
                elif proxy_username != None:
                    insert_status = cursor.execute(QUERY_PROXY %
                                            (uuid, event_id, category_id,
                                            service_id, username,
                                            proxy_username, request_ipaddress,
                                            created_date))
                    if insert_status == 1:
                        update_status = cursor.execute(AUDIT_UPDATE_STATUS %
                                                        ('Y', _id))
                        if update_status == 1:
                            info_msg = "Audit Success: " + all_data
                            log_info(info_msg)
                        else:
                            err_msg = ("Audit Update: AUDIT_UPDATE_STATUS " +
                                        " query failed" + all_data)
                            log_errors(err_msg)
                            failed_inserts_audit(all_data)
                            notify_support(err_msg, scriptname)
                    else:
                        err_msg = "Audit: QUERY_PROXY query failed" + all_data
                        log_errors(err_msg)
                        failed_inserts_audit(all_data)
                        notify_support(err_msg, scriptname)
                # no event_data case (inside the 'if len > 0')
                elif event_data != None:
                    insert_status = cursor.execute(QUERY_DATA %
                                                (uuid, event_id, category_id,
                                                service_id, username,
                                                event_data, request_ipaddress,
                                                created_date))

                    if insert_status == 1:
                        update_status = cursor.execute(AUDIT_UPDATE_STATUS %
                                                        ('Y', _id))
                        if update_status == 1:
                            info_msg = "Audit Success: " + all_data
                            log_info(info_msg)
                        else:
                            err_msg = ("Audit Update: AUDIT_UPDATE_STATUS " +
                                        "query failed" + all_data)
                            log_errors(err_msg)
                            failed_inserts_audit(all_data)
                            notify_support(err_msg, scriptname)

                    else:
                        err_msg = "Audit: QUERY_DATA query failed" + all_data
                        log_errors(err_msg)
                        failed_inserts_audit(all_data)
                        notify_support(err_msg, scriptname)
                else:
                # final else block
                    insert_status = cursor.execute(QUERY_ALL %
                                                (uuid, event_id, category_id,
                                                service_id, username,
                                                proxy_username, event_data,
                                                request_ipaddress,
                                                created_date))
                    if insert_status == 1:
                        update_status = cursor.execute(AUDIT_UPDATE_STATUS %
                                                        ('Y', _id))
                        if update_status == 1:
                            info_msg = "Audit Success: " + all_data
                            log_info(info_msg)
                        else:
                            err_msg = ("Audit Update: AUDIT_UPDATE_STATUS " +
                                        "query failed" + all_data)
                            log_errors(err_msg)
                            failed_inserts_audit(all_data)
                            notify_support(err_msg, scriptname)
                    else:
                        err_msg = "Audit: QUERY_ALL query failed" + all_data
                        log_errors(err_msg)
                        failed_inserts_audit(all_data)
                        notify_support(err_msg, scriptname)

        # outside the if block ...
        cursor.close()

    except Exception, e:

        err_msg = "AUDIT EXCEPTION: " + str(e) + ": " + all_data
        log_exception(err_msg)
        failed_inserts_audit(all_data)
        notify_support(err_msg, scriptname)
        cursor.close()