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)
Beispiel #2
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()