def getParser( self, desc, isHbsTask = False ): parser = argparse.ArgumentParser( prog = desc ) if isHbsTask: parser.add_argument( '-!', type = AgentId, required = False, default = AgentId( self.aid ), help = 'agent id to change context to ONLY for the duration of this command.', dest = 'toAgent' ) parser.add_argument( '-x', type = int, required = False, default = ( 60 * 60 * 1 ), help = 'set this command\'s specific expiry time in seconds.', dest = 'expiry' ) parser.add_argument( '-@', type = str, required = False, default = self.investigationId, help = 'the investigation id to attach to the command, results and side-effects.', dest = 'investigationId' ) return parser
def hbs_taskAgent(self, toAgent, task, key, id, expiry=None, investigationId=None): # Make sure it's a valid agentid a = AgentId(toAgent) if not type(task) is rSequence: return None s = Signing(key) r = rpcm(isHumanReadable=True, isDebug=True) tags = Symbols() if investigationId is not None and '' != investigationId: task.addStringA(tags.hbs.INVESTIGATION_ID, investigationId) toSign = (rSequence().addSequence( tags.base.HCP_IDENT, rSequence().addBuffer( tags.base.HCP_SENSOR_ID, (a.sensor_id if a.sensor_id is not None else self.empty_uuid).bytes).addBuffer( tags.base.HCP_ORG_ID, (a.org_id if a.org_id is not None else self.empty_uuid).bytes).addBuffer( tags.base.HCP_INSTALLER_ID, (a.ins_id if a.ins_id is not None else self.empty_uuid).bytes).addInt32( tags.base.HCP_ARCHITECTURE, a.architecture if a.architecture is not None else 0).addInt32( tags.base.HCP_PLATFORM, a.platform if a.platform is not None else 0)).addSequence( tags.hbs.NOTIFICATION, task).addInt32(tags.hbs.NOTIFICATION_ID, id)) if None != expiry: toSign.addTimestamp(tags.base.EXPIRY, int(expiry)) toSign = r.serialise(toSign) sig = s.sign(toSign) final = r.serialise(rSequence().addBuffer( tags.base.BINARY, toSign).addBuffer(tags.base.SIGNATURE, sig)) return self._query('hbs.task_agent', { 'task': final, 'aid': str(a), 'expiry': expiry })
def _executeHbsTasking( self, notifId, payload, arguments ): setattr( arguments, 'id', notifId ) setattr( arguments, 'task', payload ) # Multiplex to all agents matching getAgentsArg = argparse.Namespace() setattr( getAgentsArg, 'aid', arguments.toAgent ) agents = self.execAndPrintResponse( self.be.hcp_getAgentStates, getAgentsArg ) if agents is not None: if 'agents' in agents: for aid in agents[ 'agents' ].keys(): print( "Tasking agent %s" % aid ) arguments.toAgent = AgentId( aid ) self.execAndPrintResponse( self.be.hbs_taskAgent, arguments, True ) else: print( "No matching agents found." ) else: print( "Failed to get agent list from endpoint." )
def hbs_taskAgent(self, toAgent, task, key, id, expiry=None, investigationId=None): # Make sure it's a valid agentid a = AgentId(toAgent) if not a.isValid: return None if not type(task) is rSequence: return None s = Signing(key) r = rpcm(isHumanReadable=True, isDebug=True) tags = Symbols() if investigationId is not None and '' != investigationId: task.addStringA(tags.hbs.INVESTIGATION_ID, investigationId) toSign = (rSequence().addSequence( tags.base.HCP_ID, rSequence().addInt8(tags.base.HCP_ID_ORG, a.org).addInt8( tags.base.HCP_ID_SUBNET, a.subnet).addInt32(tags.base.HCP_ID_UNIQUE, a.unique).addInt8( tags.base.HCP_ID_PLATFORM, a.platform).addInt8( tags.base.HCP_ID_CONFIG, a.config)).addSequence( tags.hbs.NOTIFICATION, task).addInt32(tags.hbs.NOTIFICATION_ID, id)) if None != expiry: toSign.addTimestamp(tags.base.EXPIRY, int(expiry)) toSign = r.serialise(toSign) sig = s.sign(toSign) final = r.serialise(rSequence().addBuffer( tags.base.BINARY, toSign).addBuffer(tags.base.SIGNATURE, sig)) return self._query('hbs.task_agent', { 'task': final, 'agentid': str(a), 'expiry': expiry })