示例#1
0
def receive(uid):
    """Function for handling /receive/<uid> route.

    Args:
        uid: Unique Identifier of an Infoset Agent

    Returns:
        Text response of Received

    """
    # Read configuration
    config = Config()
    cache_dir = config.ingest_cache_directory()

    # Get Json from incoming agent POST
    data = request.json
    timestamp = data['timestamp']
    uid = data['uid']
    hostname = data['hostname']

    # Create a hash of the hostname
    host_hash = jm_general.hashstring(hostname, sha=1)
    json_path = ('%s/%s_%s_%s.json') % (cache_dir, timestamp, uid, host_hash)

    with open(json_path, "w+") as temp_file:
        json.dump(data, temp_file)
        temp_file.close()

    return "Received"
示例#2
0
    def post(self, save=True, data=None):
        """Post data to central server.

        Args:
            save: When True, save data to cache directory if postinf fails
            data: Data to post. If None, then uses self.data

        Returns:
            success: "True: if successful

        """
        # Initialize key variables
        success = False
        response = False
        timestamp = self.data['timestamp']
        uid = self.data['uid']

        # Create data to post
        if data is None:
            data = self.data

        # Post data save to cache if this fails
        try:
            result = requests.post(self.url, json=data)
            response = True
        except:
            if save is True:
                # Create a unique very long filename to reduce risk of
                hosthash = jm_general.hashstring(self.data['hostname'], sha=1)
                filename = ('%s/%s_%s_%s.json') % (
                    self.cache_dir, timestamp, uid, hosthash)

                # Save data
                with open(filename, 'w') as f_handle:
                    json.dump(data, f_handle)

        # Define success
        if response is True:
            if result.status_code == 200:
                success = True

        # Log message
        if success is True:
            log_message = (
                'Agent "%s" successfully contacted server %s'
                '') % (self.name(), self.url)
            log.log2quiet(1027, log_message)
        else:
            log_message = (
                'Agent "%s" failed to contact server %s'
                '') % (self.name(), self.url)
            log.log2warn(1028, log_message)

        # Return
        return success
示例#3
0
def _generate_uid():
    """Generate a UID.

    Args:
        None

    Returns:
        uid: the UID

    """
    # Create a UID and save
    prehash = ('%s%s%s%s%s') % (
        random(), random(), random(), random(), time.time())
    uid = jm_general.hashstring(prehash)

    # Return
    return uid
示例#4
0
def _did(uid, label, index, agent_name, hostname):
    """Create a unique DID from ingested data.

    Args:
        uid: UID of device that created the cache data file
        label: Label of the data
        index: Index of the data
        agent_name: Name of agent
        hostname: Hostname

    Returns:
        did: Datapoint ID

    """
    # Initialize key variables
    prehash = ('%s%s%s%s%s') % (uid, label, index, agent_name, hostname)
    result = jm_general.hashstring(prehash)
    did = result

    # Return
    return did
示例#5
0
文件: agent.py 项目: Pr0x1m4/infoset
def get_uid(agent_name):
    """Create a permanent UID for the agent.

    Args:
        agent_name: Agent to create UID for
        hostname: hostname

    Returns:
        uid: UID for agent

    """
    # Initialize key variables
    filez = hidden.File()
    dirz = hidden.Directory()
    uid_dir = dirz.uid()
    filename = filez.uid(agent_name)

    # Create UID directory if not yet created
    if os.path.exists(uid_dir) is False:
        os.makedirs(uid_dir)

    # Read environment file with UID if it exists
    if os.path.isfile(filename):
        with open(filename) as f_handle:
            uid = f_handle.readline()
    else:
        # Create a UID and save
        prehash = ("%s%s%s%s%s") % (random(), random(), random(), random(), time.time())
        uid = jm_general.hashstring(prehash)

        # Save UID
        with open(filename, "w+") as env:
            env.write(str(uid))

    # Return
    return uid