Exemplo n.º 1
0
def WriteObjectPropertyChangeLog(oType, sObjectID, sLabel, sFrom, sTo):
    if sFrom and sTo:
        if sFrom != sTo:
            sLog = "Changed: " + sLabel + " from [" + catocommon.tick_slash(
                sFrom) + "] to [" + catocommon.tick_slash(sTo) + "]."
            AddSecurityLog(uiGlobals.SecurityLogTypes.Object,
                           uiGlobals.SecurityLogActions.ObjectAdd, oType,
                           sObjectID, sLog)
Exemplo n.º 2
0
def WriteObjectChangeLog(oType, sObjectID, sObjectName, sLog = ""):
    if sObjectID and sObjectName:
        if not sObjectName:
            sObjectName = "[" + catocommon.tick_slash(sObjectName) + "]."
        else:
            sLog = "Changed: [" + catocommon.tick_slash(sObjectName) + "] - [" + sLog + "]"

        AddSecurityLog(uiGlobals.SecurityLogTypes.Object, uiGlobals.SecurityLogActions.ObjectAdd, oType, sObjectID, sLog)
Exemplo n.º 3
0
def WriteObjectChangeLog(oType, sObjectID, sObjectName, sLog=""):
    if sObjectID and sObjectName:
        if not sObjectName:
            sObjectName = "[" + catocommon.tick_slash(sObjectName) + "]."
        else:
            sLog = "Changed: [" + catocommon.tick_slash(
                sObjectName) + "] - [" + sLog + "]"

        AddSecurityLog(uiGlobals.SecurityLogTypes.Object,
                       uiGlobals.SecurityLogActions.ObjectAdd, oType,
                       sObjectID, sLog)
Exemplo n.º 4
0
    def DBUpdate(self):
        try:
            db = catocommon.new_conn()

            # if the password has not changed leave it as is.
            sPasswordUpdate = ""
            if self.Password and self.Password != "~!@@!~":
                sPasswordUpdate = ", password = '******'"

            #  same for privileged_password
            sPriviledgedPasswordUpdate = ""
            if self.PrivilegedPassword != "~!@@!~":
                #  updated password
                #  priviledged password can be blank, so if it is, set it to null
                if self.PrivilegedPassword:
                    sPriviledgedPasswordUpdate = ", privileged_password = null"
                else:
                    sPriviledgedPasswordUpdate = ", privileged_password = '******'"

            sSQL = "update asset_credential " \
                "set username = '******'," \
                "domain = '" + self.Domain + "'," \
                "shared_or_local = '" + self.SharedOrLocal + "'," \
                "shared_cred_desc = '" + catocommon.tick_slash(self.Description) + "'" \
                + sPasswordUpdate + sPriviledgedPasswordUpdate + \
                "where credential_id = '" + self.ID + "'"
            if not db.exec_db_noexcep(sSQL):
                return False, db.error

#                #  add security log
#                uiCommon.WriteObjectPropertyChangeLog(uiGlobals.CatoObjectTypes.Asset, sAssetID, sAssetName.strip().replace("'", "''") + "Changed credential", sOriginalUserName, sCredUsername)

            return True, None
        except Exception, ex:
            raise ex
Exemplo n.º 5
0
def SetNodeAttributeinXMLColumn(sTable, sXMLColumn, sWhereClause, sNodeToSet,
                                sAttribute, sValue):
    # THIS ONE WILL do adds if the attribute doesn't exist, or update it if it does.
    try:
        db = catocommon.new_conn()
        log(
            "Setting [%s] attribute [%s] to [%s] in [%s.%s where %s]" %
            (sNodeToSet, sAttribute, sValue, sTable, sXMLColumn, sWhereClause),
            4)

        sXML = ""

        sSQL = "select " + sXMLColumn + " from " + sTable + " where " + sWhereClause
        sXML = db.select_col_noexcep(sSQL)
        if db.error:
            log("Unable to get xml." + db.error)
            return ""

        if sXML:
            # parse the doc from the table
            xd = ET.fromstring(sXML)
            if xd is None:
                log("Unable to parse xml." + db.error)
                return ""

            # get the specified node from the doc
            # here's the rub - the request might be or the "root" node,
            # which "find" will not, er ... find.
            # so let's first check if the root node is the name we want.
            xNodeToSet = None

            if xd.tag == sNodeToSet:
                xNodeToSet = xd
            else:
                xNodeToSet = xd.find(sNodeToSet)

            if xNodeToSet is None:
                # do nothing if we didn't find the node
                return ""
            else:
                # set it
                xNodeToSet.attrib[sAttribute] = sValue

            # then send the whole doc back to the database
            sSQL = "update " + sTable + " set " + sXMLColumn + " = '" + catocommon.tick_slash(ET.tostring(xd)) + "'" \
                " where " + sWhereClause
            if not db.exec_db_noexcep(sSQL):
                log("Unable to update XML Column [" + sXMLColumn + "] on [" +
                    sTable + "]." + db.error)

        return ""
    except Exception:
        log_nouser(traceback.format_exc(), 0)
    finally:
        if db.conn.socket:
            db.close()
Exemplo n.º 6
0
def AddSecurityLog(LogType, Action, ObjectType, ObjectID, LogMessage):
    sTrimmedLog = catocommon.tick_slash(LogMessage).strip()
    if sTrimmedLog:
        if len(sTrimmedLog) > 7999:
            sTrimmedLog = sTrimmedLog[:7998]
    sSQL = """insert into user_security_log (log_type, action, user_id, log_dt, object_type, object_id, log_msg)
        values ('%s', '%s', '%s', now(), %d, '%s', '%s')""" % (LogType, Action, GetSessionUserID(), ObjectType, ObjectID, sTrimmedLog)
    db = catocommon.new_conn()
    if not db.exec_db_noexcep(sSQL):
        log_nouser(db.error, 0)
    db.close()
Exemplo n.º 7
0
    def DBUpdate(self):
        try:
            if not self.Name or not self.EcotemplateID or not self.AccountID:
                return False, "Name, EcotemplateID and Account ID are required Ecosystem properties."

            sSQL = "update ecosystem set" \
                " ecosystem_name = '" + self.Name + "'," \
                " ecotemplate_id = '" + self.EcotemplateID + "'," \
                " account_id = '" + self.AccountID + "'," \
                " ecosystem_desc = " + (" null" if not self.Description else " '" + catocommon.tick_slash(self.Description) + "'") + "," \
                " last_update_dt = now()," \
                " storm_file = " + (" null" if not self.StormFile else " '" + catocommon.tick_slash(self.StormFile) + "'") + \
                " where ecosystem_id = '" + self.ID + "'"
            
            db = catocommon.new_conn()
            if not db.exec_db_noexcep(sSQL):
                return False, db.error
            
            return True, ""
        except Exception, ex:
            raise Exception(ex)
Exemplo n.º 8
0
def AddSecurityLog(LogType, Action, ObjectType, ObjectID, LogMessage):
    sTrimmedLog = catocommon.tick_slash(LogMessage).strip()
    if sTrimmedLog:
        if len(sTrimmedLog) > 7999:
            sTrimmedLog = sTrimmedLog[:7998]
    sSQL = """insert into user_security_log (log_type, action, user_id, log_dt, object_type, object_id, log_msg)
        values ('%s', '%s', '%s', now(), %d, '%s', '%s')""" % (
        LogType, Action, GetSessionUserID(), ObjectType, ObjectID, sTrimmedLog)
    db = catocommon.new_conn()
    if not db.exec_db_noexcep(sSQL):
        log_nouser(db.error, 0)
    db.close()
Exemplo n.º 9
0
def SetNodeAttributeinXMLColumn(sTable, sXMLColumn, sWhereClause, sNodeToSet, sAttribute, sValue):
    # THIS ONE WILL do adds if the attribute doesn't exist, or update it if it does.
    try:
        db = catocommon.new_conn()
        log("Setting [%s] attribute [%s] to [%s] in [%s.%s where %s]" % (sNodeToSet, sAttribute, sValue, sTable, sXMLColumn, sWhereClause), 4)

        sXML = ""

        sSQL = "select " + sXMLColumn + " from " + sTable + " where " + sWhereClause
        sXML = db.select_col_noexcep(sSQL)
        if db.error:
            log("Unable to get xml." + db.error)
            return ""
 
        if sXML:
            # parse the doc from the table
            xd = ET.fromstring(sXML)
            if xd is None:
                log("Unable to parse xml." + db.error)
                return ""

            # get the specified node from the doc
            # here's the rub - the request might be or the "root" node,
            # which "find" will not, er ... find.
            # so let's first check if the root node is the name we want.
            xNodeToSet = None
            
            if xd.tag == sNodeToSet:
                xNodeToSet = xd
            else:
                xNodeToSet = xd.find(sNodeToSet)
            
            if xNodeToSet is None:
            # do nothing if we didn't find the node
                return ""
            else:
                # set it
                xNodeToSet.attrib[sAttribute] = sValue


            # then send the whole doc back to the database
            sSQL = "update " + sTable + " set " + sXMLColumn + " = '" + catocommon.tick_slash(ET.tostring(xd)) + "'" \
                " where " + sWhereClause
            if not db.exec_db_noexcep(sSQL):
                log("Unable to update XML Column [" + sXMLColumn + "] on [" + sTable + "]." + db.error)

        return ""
    except Exception:
        log_nouser(traceback.format_exc(), 0)
    finally:
        if db.conn.socket:
            db.close()
Exemplo n.º 10
0
def RemoveNodeFromXMLColumn(sTable, sXMLColumn, sWhereClause, sNodeToRemove):
    try:
        db = catocommon.new_conn()
        log(
            "Removing node [%s] from [%s.%s where %s]." %
            (sNodeToRemove, sTable, sXMLColumn, sWhereClause), 4)
        sSQL = "select " + sXMLColumn + " from " + sTable + " where " + sWhereClause
        sXML = db.select_col_noexcep(sSQL)
        if not sXML:
            log("Unable to get xml." + db.error)
        else:
            # parse the doc from the table
            xd = ET.fromstring(sXML)
            if xd is None:
                log("Error: Unable to parse XML.")

            # get the specified node from the doc
            xNodeToWhack = xd.find(sNodeToRemove)
            if xNodeToWhack is None:
                log(
                    "INFO: attempt to remove [%s] - the element was not found."
                    % sNodeToRemove, 4)
                # no worries... what you want to delete doesn't exist?  perfect!
                return

            # OK, here's the deal...
            # we have found the node we want to delete, but we found it using an xpath,
            # ElementTree doesn't support deleting by xpath.
            # so, we'll use a parent map to find the immediate parent of the node we found,
            # and on the parent we can call ".remove"
            parent_map = dict((c, p) for p in xd.getiterator() for c in p)
            xParentOfNodeToWhack = parent_map[xNodeToWhack]

            # whack it
            if xParentOfNodeToWhack is not None:
                xParentOfNodeToWhack.remove(xNodeToWhack)

            sSQL = "update " + sTable + " set " + sXMLColumn + " = '" + catocommon.tick_slash(ET.tostring(xd)) + "'" \
                " where " + sWhereClause
            if not db.exec_db_noexcep(sSQL):
                log("Unable to update XML Column [" + sXMLColumn + "] on [" +
                    sTable + "]." + db.error)

        return
    except Exception:
        log_nouser(traceback.format_exc(), 0)
    finally:
        if db.conn.socket:
            db.close()
Exemplo n.º 11
0
    def DBCreateNew(sName, sEcotemplateID, sAccountID, sDescription="", sStormStatus="", sParameterXML="", sCloudID=""):
        try:
            if not sName or not sEcotemplateID or not sAccountID:
                return None, "Name, Ecotemplate and Cloud Account are required Ecosystem properties."
              
            db = catocommon.new_conn()
            
            sID = catocommon.new_guid()

            sSQL = "insert into ecosystem (ecosystem_id, ecosystem_name, ecosystem_desc, account_id, ecotemplate_id," \
                " storm_file, storm_status, storm_parameter_xml, storm_cloud_id, created_dt, last_update_dt)" \
                " select '" + sID + "'," \
                " '" + sName + "'," \
                + (" null" if not sDescription else " '" + catocommon.tick_slash(sDescription) + "'") + "," \
                " '" + sAccountID + "'," \
                " ecotemplate_id," \
                " storm_file," \
                + (" null" if not sStormStatus else " '" + catocommon.tick_slash(sStormStatus) + "'") + "," \
                + (" null" if not sParameterXML else " '" + catocommon.tick_slash(sParameterXML) + "'") + "," \
                + (" null" if not sCloudID else " '" + sCloudID + "'") + "," \
                " now(), now()" \
                " from ecotemplate where ecotemplate_id = '" + sEcotemplateID + "'"
            
            if not db.exec_db_noexcep(sSQL):
                if db.error == "key_violation":
                    return None, "An Ecosystem with that name already exists.  Please select another name."
                else:
                    return None, db.error

            #now it's inserted and in the session... lets get it back from the db as a complete object for confirmation.
            e = Ecosystem()
            e.FromID(sID)
            #yay!
            return e, None
        except Exception, ex:
            raise Exception(ex)
Exemplo n.º 12
0
    def wmSaveSchedule(self):
        sScheduleID = uiCommon.getAjaxArg("sScheduleID")
        aMonths = uiCommon.getAjaxArg("sMonths")
        aDays = uiCommon.getAjaxArg("sDays")
        aHours = uiCommon.getAjaxArg("sHours")
        aMinutes = uiCommon.getAjaxArg("sMinutes")
        sDaysOrWeeks = uiCommon.getAjaxArg("sDaysOrWeeks")
        sParameterXML = uiCommon.getAjaxArg("sParameterXML")
        iDebugLevel = uiCommon.getAjaxArg("iDebugLevel")
        """
         * JUST AS A REMINDER:
         * There is no parameter 'merging' happening here.  This is a Scheduled Plan ...
         *   it has ALL the parameters it needs to pass to the CE.
         * 
         * """

        if not sScheduleID or not aMonths or not aDays or not aHours or not aMinutes or not sDaysOrWeeks:
            uiCommon.log("Missing Schedule ID or invalid timetable.")

        # we encoded this in javascript before the ajax call.
        # the safest way to unencode it is to use the same javascript lib.
        # (sometimes the javascript and .net libs don't translate exactly, google it.)
        sParameterXML = uiCommon.unpackJSON(sParameterXML)

        # we gotta peek into the XML and encrypt any newly keyed values
        sParameterXML = task.Task.PrepareAndEncryptParameterXML(sParameterXML)

        # whack all plans for this schedule, it's been changed
        sSQL = "delete from action_plan where schedule_id = '" + sScheduleID + "'"
        self.db.exec_db(sSQL)


        # figure out a label
        sLabel, sDesc = catocommon.GenerateScheduleLabel(aMonths, aDays, aHours, aMinutes, sDaysOrWeeks)

        sSQL = "update action_schedule set" \
            " months = '" + ",".join([str(x) for x in aMonths]) + "'," \
            " days = '" + ",".join([str(x) for x in aDays]) + "'," \
            " hours = '" + ",".join([str(x) for x in aHours]) + "'," \
            " minutes = '" + ",".join([str(x) for x in aMinutes]) + "'," \
            " days_or_weeks = '" + sDaysOrWeeks + "'," \
            " label = " + ("'" + sLabel + "'" if sLabel else "null") + "," \
            " descr = " + ("'" + sDesc + "'" if sDesc else "null") + "," \
            " parameter_xml = " + ("'" + catocommon.tick_slash(sParameterXML) + "'" if sParameterXML else "null") + "," \
            " debug_level = " + (iDebugLevel if iDebugLevel > -1 else "null") + \
            " where schedule_id = '" + sScheduleID + "'"

        self.db.exec_db(sSQL)
Exemplo n.º 13
0
def FixIt(xd, node):
    if node.get("input_type") is None:
        print "-- Action appears to be correct."
        return
    
    old_step = node.text
    print "fixing action ... "
    print " - referenced step is %s" % old_step
    
    sql = "select replace(function_xml, 'command_type=', 'name=') as function_xml from task_step where step_id = '%s'" % old_step
    embedded_xml = db.select_col_noexcep(sql)
    
    print " - referenced steps xml is \n%s" % embedded_xml
    
    if not embedded_xml:
        print " - Found no xml for the embedded command - it doesn't exist.  Emptying it..."
        node.clear()
    else:
        xNew = ET.fromstring(embedded_xml)
        if xNew is not None:
            # we remove the input_type attrib, that's how we knew it was old school
            # and empty it
            node.clear()
            node.append(xNew)
        else:
            print " - ERROR: embedded xml didn't parse."
        
        print " - success! New xml is \n%s" % ET.tostring(xd)

    print " - updating ..."
    sql = "update task_step set function_xml = '%s' where step_id = '%s'" % (catocommon.tick_slash(ET.tostring(xd)), this_step)
    if not db.exec_db_noexcep(sql):
        print db.error

    print " - removing old school step %s..." % old_step
    sql = "delete from task_step where step_id = '%s'" % old_step
    if not db.exec_db_noexcep(sql):
        print db.error
    print " - removing codeblock %s..." % this_step
    sql = "delete from task_codeblock where codeblock_name = '%s'" % this_step[:32]
    print sql
    print db.error
    print "^^^^^"
    if not db.exec_db_noexcep(sql):
        print db.error
Exemplo n.º 14
0
def RemoveNodeFromXMLColumn(sTable, sXMLColumn, sWhereClause, sNodeToRemove):
    try:
        db = catocommon.new_conn()
        log("Removing node [%s] from [%s.%s where %s]." % (sNodeToRemove, sTable, sXMLColumn, sWhereClause), 4)
        sSQL = "select " + sXMLColumn + " from " + sTable + " where " + sWhereClause
        sXML = db.select_col_noexcep(sSQL)
        if not sXML:
            log("Unable to get xml." + db.error)
        else:
            # parse the doc from the table
            xd = ET.fromstring(sXML)
            if xd is None:
                log("Error: Unable to parse XML.")

            # get the specified node from the doc
            xNodeToWhack = xd.find(sNodeToRemove)
            if xNodeToWhack is None:
                log("INFO: attempt to remove [%s] - the element was not found." % sNodeToRemove, 4)
                # no worries... what you want to delete doesn't exist?  perfect!
                return

            # OK, here's the deal...
            # we have found the node we want to delete, but we found it using an xpath,
            # ElementTree doesn't support deleting by xpath.
            # so, we'll use a parent map to find the immediate parent of the node we found,
            # and on the parent we can call ".remove"
            parent_map = dict((c, p) for p in xd.getiterator() for c in p)
            xParentOfNodeToWhack = parent_map[xNodeToWhack]
            
            # whack it
            if xParentOfNodeToWhack is not None:
                xParentOfNodeToWhack.remove(xNodeToWhack)

            sSQL = "update " + sTable + " set " + sXMLColumn + " = '" + catocommon.tick_slash(ET.tostring(xd)) + "'" \
                " where " + sWhereClause
            if not db.exec_db_noexcep(sSQL):
                log("Unable to update XML Column [" + sXMLColumn + "] on [" + sTable + "]." + db.error)

        return
    except Exception:
        log_nouser(traceback.format_exc(), 0)
    finally:
        if db.conn.socket:
            db.close()
Exemplo n.º 15
0
def SetNodeValueinXMLColumn(sTable, sXMLColumn, sWhereClause, sNodeToSet,
                            sValue):
    try:
        db = catocommon.new_conn()
        log(
            "Setting node [%s] to [%s] in [%s.%s where %s]." %
            (sNodeToSet, sValue, sTable, sXMLColumn, sWhereClause), 4)
        sSQL = "select " + sXMLColumn + " from " + sTable + " where " + sWhereClause
        sXML = db.select_col_noexcep(sSQL)
        if not sXML:
            log("Unable to get xml." + db.error)
        else:
            # parse the doc from the table
            xd = ET.fromstring(sXML)
            if xd is None:
                log("Error: Unable to parse XML.")

            # get the specified node from the doc, IF IT'S NOT THE ROOT
            if xd.tag == sNodeToSet:
                xNodeToSet = xd
            else:
                xNodeToSet = xd.find(sNodeToSet)

            if xNodeToSet is not None:
                xNodeToSet.text = sValue

                # then send the whole doc back to the database
                sSQL = "update " + sTable + " set " + sXMLColumn + " = '" + catocommon.tick_slash(
                    ET.tostring(xd)) + "' where " + sWhereClause
                if not db.exec_db_noexcep(sSQL):
                    log("Unable to update XML Column [" + sXMLColumn +
                        "] on [" + sTable + "]." + db.error)
            else:
                log("Unable to update XML Column ... [" + sNodeToSet +
                    "] not found.")

        return
    except Exception:
        log_nouser(traceback.format_exc(), 0)
    finally:
        if db.conn.socket:
            db.close()
Exemplo n.º 16
0
def AddTaskInstance(sUserID, sTaskID, sEcosystemID, sAccountID, sAssetID,
                    sParameterXML, sDebugLevel):
    try:
        if not sUserID: return ""
        if not sTaskID: return ""

        sParameterXML = unpackJSON(sParameterXML)

        # we gotta peek into the XML and encrypt any newly keyed values
        sParameterXML = PrepareAndEncryptParameterXML(sParameterXML)

        if IsGUID(sTaskID) and IsGUID(sUserID):
            sSQL = "call addTaskInstance ('" + sTaskID + "','" + \
                sUserID + "',NULL," + \
                sDebugLevel + ",NULL,'" + \
                catocommon.tick_slash(sParameterXML) + "','" + \
                sEcosystemID + "','" + \
                sAccountID + "')"

            db = catocommon.new_conn()
            row = db.exec_proc(sSQL)
            if db.error:
                log("Unable to run task [" + sTaskID + "]." + db.error)
            db.close()

            # this needs fixing, this whole weird result set.
            log(
                "Starting Task [%s] ... Instance is [%s]" %
                (sTaskID, row[0]["_task_instance"]), 3)

            return row[0]["_task_instance"]
        else:
            log("Unable to run task. Missing or invalid task [" + sTaskID +
                "] or user [" + sUserID + "] id.")

        #uh oh, return nothing
        return ""
    except Exception:
        log_nouser(traceback.format_exc(), 0)
        return ""
Exemplo n.º 17
0
    def DBUpdate(self):
        try:
            if not self.Name:
                return False, "Name is required."

            sSQL = "update ecotemplate set" \
                " ecotemplate_name = '" + self.Name + "'," \
                " ecotemplate_desc = " + (" null" if not self.Description else " '" + catocommon.tick_slash(self.Description) + "'") + "," \
                " storm_file_type = " + (" null" if not self.StormFileType else " '" + catocommon.tick_slash(self.StormFileType) + "'") + "," \
                " storm_file = " + (" null" if not self.StormFile else " '" + catocommon.tick_slash(self.StormFile) + "'") + \
                " where ecotemplate_id = '" + self.ID + "'"

            db = catocommon.new_conn()
            if not db.exec_db_noexcep(sSQL):
                print db.error
                if db.error == "key_violation":
                    return None, "An Ecotemplate with that name already exists.  Please select another name."
                else:
                    return False, db.error
            
            return True, ""
        except Exception, ex:
            raise Exception(ex)
Exemplo n.º 18
0
def AddTaskInstance(sUserID, sTaskID, sEcosystemID, sAccountID, sAssetID, sParameterXML, sDebugLevel):
    try:
        if not sUserID: return ""
        if not sTaskID: return ""
        
        sParameterXML = unpackJSON(sParameterXML)
                        
        # we gotta peek into the XML and encrypt any newly keyed values
        sParameterXML = PrepareAndEncryptParameterXML(sParameterXML);                
    
        if IsGUID(sTaskID) and IsGUID(sUserID):
            sSQL = "call addTaskInstance ('" + sTaskID + "','" + \
                sUserID + "',NULL," + \
                sDebugLevel + ",NULL,'" + \
                catocommon.tick_slash(sParameterXML) + "','" + \
                sEcosystemID + "','" + \
                sAccountID + "')"
            
            db = catocommon.new_conn()
            row = db.exec_proc(sSQL)
            if db.error:
                log("Unable to run task [" + sTaskID + "]." + db.error)
            db.close()
            
            # this needs fixing, this whole weird result set.
            log("Starting Task [%s] ... Instance is [%s]" % (sTaskID, row[0]["_task_instance"]), 3)
            
            return row[0]["_task_instance"]
        else:
            log("Unable to run task. Missing or invalid task [" + sTaskID + "] or user [" + sUserID + "] id.")

        #uh oh, return nothing
        return ""
    except Exception:
        log_nouser(traceback.format_exc(), 0)
        return ""
Exemplo n.º 19
0
def SetNodeValueinXMLColumn(sTable, sXMLColumn, sWhereClause, sNodeToSet, sValue):
    try:
        db = catocommon.new_conn()
        log("Setting node [%s] to [%s] in [%s.%s where %s]." % (sNodeToSet, sValue, sTable, sXMLColumn, sWhereClause), 4)
        sSQL = "select " + sXMLColumn + " from " + sTable + " where " + sWhereClause
        sXML = db.select_col_noexcep(sSQL)
        if not sXML:
            log("Unable to get xml." + db.error)
        else:
            # parse the doc from the table
            xd = ET.fromstring(sXML)
            if xd is None:
                log("Error: Unable to parse XML.")

            # get the specified node from the doc, IF IT'S NOT THE ROOT
            if xd.tag == sNodeToSet:
                xNodeToSet = xd
            else:
                xNodeToSet = xd.find(sNodeToSet)

            if xNodeToSet is not None:
                xNodeToSet.text = sValue

                # then send the whole doc back to the database
                sSQL = "update " + sTable + " set " + sXMLColumn + " = '" + catocommon.tick_slash(ET.tostring(xd)) + "' where " + sWhereClause
                if not db.exec_db_noexcep(sSQL):
                    log("Unable to update XML Column [" + sXMLColumn + "] on [" + sTable + "]." + db.error)
            else:
                log("Unable to update XML Column ... [" + sNodeToSet + "] not found.")

        return
    except Exception:
        log_nouser(traceback.format_exc(), 0)
    finally:
        if db.conn.socket:
            db.close()
Exemplo n.º 20
0
    def DBSave(self):
        try:
            if not self.Name or not self.ID:
                return False, "ID and Name are required Ecotemplate properties."

            db = catocommon.new_conn()

            if self.DBExists:
                # uh oh... this ecotemplate exists.  unless told to do so, we stop here.
                if self.OnConflict == "cancel":
                    return False, "Another Ecotemplate with that ID or Name exists.  [" + self.ID + "/" + self.Name + "]  Conflict directive set to 'cancel'. (Default is 'cancel' if omitted.)"
                else:
                    # ok, what are we supposed to do then?
                    if self.OnConflict == "replace":
                        # whack it all so we can re-insert
                        # but by name or ID?  which was the conflict?
                        
                        # no worries! the _DBExists function called when we created the object
                        # will have resolved any name/id issues.
                        
                        # if the ID existed it doesn't matter, we'll be plowing it anyway.
                        # by "plow" I mean drop and recreate the actions... the ecotemplate row will be UPDATED
                        sSQL = "update ecotemplate" \
                            " set ecotemplate_name = '" + self.Name + "'," \
                            " ecotemplate_desc = " + (" null" if not self.Description else " '" + catocommon.tick_slash(self.Description + "'")) + "," \
                            " storm_file_type = " + (" null" if not self.StormFileType else " '" + self.StormFileType + "'") + "," \
                            " storm_file = " + (" null" if not self.StormFile else " '" + catocommon.tick_slash(self.StormFile + "'")) + \
                            " where ecotemplate_id = '" + self.ID + "'"
                        if not db.tran_exec_noexcep(sSQL):
                            return False, db.error

                        sSQL = "delete from ecotemplate_action" \
                            " where ecotemplate_id = '" + self.ID + "'"
                        if not db.tran_exec_noexcep(sSQL):
                            return False, db.error
                    else:
                        # there is no default action... if the on_conflict didn't match we have a problem... bail.
                        return False, "There is an ID or Name conflict, and the on_conflict directive isn't a valid option. (replace/cancel)"
            else:
                # doesn't exist, we'll add it                
                sSQL = "insert into ecotemplate (ecotemplate_id, ecotemplate_name, ecotemplate_desc, storm_file_type, storm_file)" \
                    " values ('" + self.ID + "'," \
                        " '" + self.Name + "',"  + \
                        (" null" if not self.Description else " '" + catocommon.tick_slash(self.Description) + "'") + "," + \
                        (" null" if not self.StormFileType else " '" + self.StormFileType + "'") + "," + \
                        (" null" if not self.StormFile else " '" + catocommon.tick_slash(self.StormFile) + "'") + \
                        ")"
                if not db.tran_exec_noexcep(sSQL):
                    return False, db.error
                
            
            # create the actions
            # actions aren't referenced by id anywhere, so we'll just give them a new guid
            # to prevent any risk of PK issues.
            for ea in self.Actions.itervalues():
                print ea.Name
                sSQL = "insert into ecotemplate_action" \
                    " (action_id, ecotemplate_id, action_name, action_desc, category, action_icon, original_task_id, task_version, parameter_defaults)" \
                    " values (" \
                    " uuid()," \
                    " '" + self.ID + "'," \
                    " '" + catocommon.tick_slash(ea.Name) + "'," + \
                    ("null" if not ea.Description else " '" + catocommon.tick_slash(ea.Description) + "'") + "," + \
                    ("null" if not ea.Category else " '" + catocommon.tick_slash(ea.Category) + "'") + "," + \
                    ("null" if not ea.Icon else " '" + ea.Icon + "'") + "," + \
                    ("null" if not ea.OriginalTaskID else " '" + ea.OriginalTaskID + "'") + "," + \
                    ("null" if not ea.TaskVersion else " '" + ea.TaskVersion + "'") + "," + \
                    ("null" if not ea.ParameterDefaultsXML else " '" + catocommon.tick_slash(ea.ParameterDefaultsXML) + "'") + \
                    ")"
                
                if not db.tran_exec_noexcep(sSQL):
                    return False, db.error
                
                # now, does this action contain a <task> section?  If so, we'll branch off and do 
                # the create task logic.
                if ea.Task is not None:
                    result, msg = ea.Task.DBSave()
                    if not result:
                        # the task 'should' have rolled back on any errors, but in case it didn't.
                        db.tran_rollback()
                        return False, msg

                    # finally, don't forget to update the action with the new values if any
                    ea.OriginalTaskID = ea.Task.OriginalTaskID
                        
                    # we don't update the version if the action referenced the default (it was empty)
                    if ea.TaskVersion:
                        ea.TaskVersion = ea.Task.Version
            
            # yay!
            print "done"
            db.tran_commit()
            return True, None

        except Exception, ex:
            raise ex
Exemplo n.º 21
0
def AddNodeToXMLColumn(sTable, sXMLColumn, sWhereClause, sXPath, sXMLToAdd):
    # BE WARNED! this function is shared by many things, and should not be enhanced
    # with sorting or other niceties.  If you need that stuff, build your own function.
    # AddRegistry:Node is a perfect example... we wanted sorting on the registries, and also we don't allow array.
    # but parameters for example are by definition arrays of parameter nodes.
    try:
        db = catocommon.new_conn()
        log("Adding node [%s] to [%s] in [%s.%s where %s]." % (sXMLToAdd, sXPath, sTable, sXMLColumn, sWhereClause), 4)
        sSQL = "select " + sXMLColumn + " from " + sTable + " where " + sWhereClause
        sXML = db.select_col_noexcep(sSQL)
        if not sXML:
            log("Unable to get xml." + db.error)
        else:
            # parse the doc from the table
            log(sXML, 4)
            xd = ET.fromstring(sXML)
            if xd is None:
                log("Error: Unable to parse XML.")

            # get the specified node from the doc, IF IT'S NOT THE ROOT
            # either a blank xpath, or a single word that matches the root, both match the root.
            # any other path DOES NOT require the root prefix.
            if sXPath == "":
                xNodeToEdit = xd
            elif xd.tag == sXPath:
                xNodeToEdit = xd
            else:
                xNodeToEdit = xd.find(sXPath)
            
            if xNodeToEdit is None:
                log("Error: XML does not contain path [" + sXPath + "].")
                return

            # now parse the new section from the text passed in
            xNew = ET.fromstring(sXMLToAdd)
            if xNew is None:
                log("Error: XML to be added cannot be parsed.")

            # if the node we are adding to has a text value, sadly it has to go.
            # we can't detect that, as the Value property shows the value of all children.
            # but this works, even if it seems backwards.
            # if the node does not have any children, then clear it.  that will safely clear any
            # text but not stomp the text of the children.
            if len(xNodeToEdit) == 0:
                xNodeToEdit.text = ""
            # add it to the doc
            xNodeToEdit.append(xNew)


            # then send the whole doc back to the database
            sSQL = "update " + sTable + " set " + sXMLColumn + " = '" + catocommon.tick_slash(ET.tostring(xd)) + "'" \
                " where " + sWhereClause
            if not db.exec_db_noexcep(sSQL):
                log("Unable to update XML Column [" + sXMLColumn + "] on [" + sTable + "]." + db.error)

        return
    except Exception:
        log_nouser(traceback.format_exc(), 0)
    finally:
        if db.conn.socket:
            db.close()
Exemplo n.º 22
0
def WriteObjectPropertyChangeLog(oType, sObjectID, sLabel, sFrom, sTo):
    if sFrom and sTo:
        if sFrom != sTo:
            sLog = "Changed: " + sLabel + " from [" + catocommon.tick_slash(sFrom) + "] to [" + catocommon.tick_slash(sTo) + "]."
            AddSecurityLog(uiGlobals.SecurityLogTypes.Object, uiGlobals.SecurityLogActions.ObjectAdd, oType, sObjectID, sLog)
Exemplo n.º 23
0
    def DBSave(self):
        try:
            db = catocommon.new_conn()

            sSQL = "update object_registry set registry_xml = '%s' where object_id = '%s'" % (catocommon.tick_slash(self.xml_text), self.object_id)
            if not db.exec_db_noexcep(sSQL):
                return False, "Error: Could not create Registry." + db.error
            
            return True, ""
        except Exception, ex:
            raise Exception(ex)
Exemplo n.º 24
0
def AddNodeToXMLColumn(sTable, sXMLColumn, sWhereClause, sXPath, sXMLToAdd):
    # BE WARNED! this function is shared by many things, and should not be enhanced
    # with sorting or other niceties.  If you need that stuff, build your own function.
    # AddRegistry:Node is a perfect example... we wanted sorting on the registries, and also we don't allow array.
    # but parameters for example are by definition arrays of parameter nodes.
    try:
        db = catocommon.new_conn()
        log(
            "Adding node [%s] to [%s] in [%s.%s where %s]." %
            (sXMLToAdd, sXPath, sTable, sXMLColumn, sWhereClause), 4)
        sSQL = "select " + sXMLColumn + " from " + sTable + " where " + sWhereClause
        sXML = db.select_col_noexcep(sSQL)
        if not sXML:
            log("Unable to get xml." + db.error)
        else:
            # parse the doc from the table
            log(sXML, 4)
            xd = ET.fromstring(sXML)
            if xd is None:
                log("Error: Unable to parse XML.")

            # get the specified node from the doc, IF IT'S NOT THE ROOT
            # either a blank xpath, or a single word that matches the root, both match the root.
            # any other path DOES NOT require the root prefix.
            if sXPath == "":
                xNodeToEdit = xd
            elif xd.tag == sXPath:
                xNodeToEdit = xd
            else:
                xNodeToEdit = xd.find(sXPath)

            if xNodeToEdit is None:
                log("Error: XML does not contain path [" + sXPath + "].")
                return

            # now parse the new section from the text passed in
            xNew = ET.fromstring(sXMLToAdd)
            if xNew is None:
                log("Error: XML to be added cannot be parsed.")

            # if the node we are adding to has a text value, sadly it has to go.
            # we can't detect that, as the Value property shows the value of all children.
            # but this works, even if it seems backwards.
            # if the node does not have any children, then clear it.  that will safely clear any
            # text but not stomp the text of the children.
            if len(xNodeToEdit) == 0:
                xNodeToEdit.text = ""
            # add it to the doc
            xNodeToEdit.append(xNew)

            # then send the whole doc back to the database
            sSQL = "update " + sTable + " set " + sXMLColumn + " = '" + catocommon.tick_slash(ET.tostring(xd)) + "'" \
                " where " + sWhereClause
            if not db.exec_db_noexcep(sSQL):
                log("Unable to update XML Column [" + sXMLColumn + "] on [" +
                    sTable + "]." + db.error)

        return
    except Exception:
        log_nouser(traceback.format_exc(), 0)
    finally:
        if db.conn.socket:
            db.close()