def _save_data(data, identifier): """Save data to cache file. Args: data: Dict to save identifier: Unique identifier for the source of the data. (AgentID) Returns: success: True: if successful """ # Initialize key variables success = False config = Config() cache_dir = config.agent_cache_directory(identifier) timestamp = int(time() * 1000) # Create a unique very long filename to reduce risk of filename = ('''{}{}{}_{}.json\ '''.format(cache_dir, os.sep, timestamp, identifier)) # Save data try: with open(filename, 'w') as f_handle: json.dump(data, f_handle) success = True except Exception as err: log_message = '{}'.format(err) log.log2warning(1030, log_message) except: (etype, evalue, etraceback) = sys.exc_info() log_message = ('''\ Cache-file save error: [{}, {}, {}]'''.format(etype, evalue, etraceback)) log.log2warning(1031, log_message) # Delete file if there is a failure. # Helps to protect against full file systems. if os.path.isfile(filename) is True and success is False: os.remove(filename) log_message = ('''\ Deleting corrupted cache file {} for identifier {}.\ '''.format(filename, identifier)) log.log2warning(1037, log_message) # Return return success
def purge(url, identifier): """Purge data from cache by posting to central server. Args: url: URL to receive posted data identifier: Unique identifier for the source of the data. (AgentID) Returns: None """ # Initialize key variables config = Config() cache_dir = config.agent_cache_directory(identifier) # Add files in cache directory to list only if they match the # cache suffix all_filenames = [ filename for filename in os.listdir(cache_dir) if os.path.isfile(os.path.join(cache_dir, filename)) ] filenames = [ filename for filename in all_filenames if filename.endswith('.json') ] # Read cache file for filename in filenames: # Only post files for our own UID value if identifier not in filename: continue # Get the full filepath for the cache file and post filepath = os.path.join(cache_dir, filename) with open(filepath, 'r') as f_handle: try: data = json.load(f_handle) except: # Log removal log_message = ('''\ Error reading previously cached agent data file {} for identifier {}. May be \ corrupted.'''.format(filepath, identifier)) log.log2warning(1064, log_message) # Delete file if os.path.isfile(filepath) is True: os.remove(filepath) log_message = ('''\ Deleting corrupted cache file {} for identifier {}.\ '''.format(filepath, identifier)) log.log2warning(1036, log_message) # Go to the next file. continue # Post file success = post(url, data, identifier, save=False) # Delete file if successful if success is True: if os.path.exists(filepath) is True: os.remove(filepath) # Log removal log_message = ('''\ Purging cache file {} after successfully contacting server {}\ '''.format(filepath, url)) log.log2info(1007, log_message)
def purge(url, identifier, suite=post): """Purge data from cache by posting to central server. Args: url: URL to receive posted data identifier: Unique identifier for the source of the data. (AgentID) suite: If a function, this will proceed to use the normal post function for unencrypted posting. If EncryptionSuite, the necessary variables from the named tuple will be used along with the encrypted_post function for encrypted posting Returns: None """ # Initialize key variables config = Config() cache_dir = config.agent_cache_directory(identifier) # Add files in cache directory to list only if they match the # cache suffix all_filenames = [ filename for filename in os.listdir(cache_dir) if os.path.isfile(os.path.join(cache_dir, filename)) ] filenames = [ filename for filename in all_filenames if filename.endswith('.json') ] # Read cache file for filename in filenames: # Only post files for our own UID value if identifier not in filename: continue # Get the full filepath for the cache file and post filepath = os.path.join(cache_dir, filename) with open(filepath, 'r') as f_handle: try: data = json.load(f_handle) except: # Log removal log_message = ('''\ Error reading previously cached agent data file {} for identifier {}. May be \ corrupted.'''.format(filepath, identifier)) log.log2warning(1064, log_message) # Delete file if os.path.isfile(filepath) is True: os.remove(filepath) log_message = ('''\ Deleting corrupted cache file {} for identifier {}.\ '''.format(filepath, identifier)) log.log2warning(1036, log_message) # Go to the next file. continue # Post file if callable(suite): # Is it a function? # Post unencrypted data success = suite(url, data, identifier, save=False) elif isinstance(suite, EncryptionSuite): # Is it EncryptionSuite? # Post encrypted data success = suite.encrypted_post( _EncrypedPost(encryption=suite.encryption, session=suite.session, symmetric_key=suite.symmetric_key, encryption_url=url, data=data, identifier=identifier)) # Delete file if successful if success is True: if os.path.exists(filepath) is True: os.remove(filepath) # Log removal log_message = ('''\ Purging cache file {} after successfully contacting server {}\ '''.format(filepath, url)) log.log2info(1007, log_message)