def test_sender_with_default_logger(self): """ Test that tries to check that Sender class can still use an internal logger and shows both local and remote traces """ try: engine_config = SenderConfigSSL(address=self.server, port=self.port, key=self.key, cert=self.cert, chain=self.chain) con = Sender(engine_config, tag=self.my_app) # NOTE: this logger logging traces will be visible in console con.logger.info("Testing Sender default handler functionality in " "local console... INFO - log") # NOTE: this logger logging traces will be visible in the remote # table con.info("Testing Sender default handler functionality in remote " "table... INFO - log") if len(con.socket.recv(5000)) == 0: raise Exception('Not msg sent!') con.close() except Exception as error: self.fail("Problems with test: %s" % error)
def test_Sender_with_default_logger(self): """ Test that tries to check that Sender class can still use an internal logger and shows both local and remote traces """ try: engine_config = SenderConfigSSL(address=self.server, port=self.port, key=self.key, cert=self.cert, chain=self.chain) con = Sender(engine_config, tag=self.my_app) for i in range(0, self.default_numbers_sendings): # NOTE: this logger logging traces will be visible in console con.logger.info( "Testing Sender default handler functionality in local console... INFO - log" ) # NOTE: this logger logging traces will be visible in the remote table con.info( "Testing Sender default handler functionality in remote table... INFO - log" ) con.close() except Exception as error: self.fail("Problems with test: %s" % error)
def lambda_handler(event, context): # Get the object from the event and show its content type bucket = event['Records'][0]['s3']['bucket']['name'] key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key']) try: print("File to process %s" % key) response = s3.get_object(Bucket=bucket, Key=key) body = response['Body'] data = body.read() # If the name has a .gz extension, then decompress the data if key[-3:] == '.gz': data = zlib.decompress(data, 16 + zlib.MAX_WBITS) config = Configuration("config.json") con = Sender(config=config.get("sender")) # Send json events to Devo print("Starting to send lines to Devo") counter = 0 for line in data.splitlines(): events_json = json.loads(line) for single_event in events_json["Records"]: dumped_event = json.dumps(single_event) aws_account_id = "a" if single_event.get("getuserIdentity", None) is not None: if single_event.get("getuserIdentity")\ .get("accountId", None) is not None: aws_account_id = single_event["getuserIdentity"][ "accountId"] elif single_event.get("account", None) is not None: aws_account_id = single_event["account"] elif single_event.get("recipientAccountId", None) is not None: aws_account_id = single_event["recipientAccountId"] aws_region = "b" if single_event.get("awsRegion", None) is not None: aws_region = single_event["awsRegion"] elif single_event.get("region", None) is not None: aws_region = single_event["region"] tag = "{!s}.{!s}.{!s}".format(config.get("tag"), aws_account_id, aws_region) counter += con.send(tag=encode(tag), msg=encode(dumped_event), zip=False) con.close() print("Finished sending lines to Devo (%d)" % counter) except Exception as e: print(e) print("Error getting file '%s' from bucket '%s'. Make sure they \ exist and your bucket is in the same region as this function." % (key, bucket))
def test_sender_as_handler(self): """ Test that tries to check that Sender class can be used as a Handler and related logs are send to remote server """ try: engine_config = SenderConfigSSL(address=self.server, port=self.port, key=self.key, cert=self.cert, chain=self.chain) con = Sender(engine_config, tag=self.my_app) logger = logging.getLogger('DEVO_logger') logger.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s|%(levelname)s' '|%(message)s') con.setFormatter(formatter) con.setLevel(logging.DEBUG) logger.addHandler(con) logger.info("Testing Sender inherit logging handler functio" "nality... INFO - log") if len(con.socket.recv(5000)) == 0: raise Exception('Not msg sent!') logger.error("Testing Sender inherit logging handler function" "ality... ERROR - log") if len(con.socket.recv(5000)) == 0: raise Exception('Not msg sent!') logger.warning("Testing Sender inherit logging handler functio" "nality... WARNING - log") if len(con.socket.recv(5000)) == 0: raise Exception('Not msg sent!') logger.debug("Testing Sender inherit logging handler functiona" "lity... DEBUG - log") if len(con.socket.recv(5000)) == 0: raise Exception('Not msg sent!') logger.critical("Testing Sender inherit logging handler functio" "nality... CRITICAL - log") if len(con.socket.recv(5000)) == 0: raise Exception('Not msg sent!') con.close() except Exception as error: self.fail("Problems with test: %s" % error)
def test_tcp_rt_send(self): """ Tests that a TCP connection and data send it is possible """ try: engine_config = SenderConfigTCP(address=(self.tcp_server, self.tcp_port)) con = Sender(engine_config) for i in range(self.default_numbers_sendings): con.send(tag=self.my_app, msg=self.test_msg) if len(con.socket.recv(5000)) == 0: raise Exception('Not msg sent!') con.close() except Exception as error: self.fail("Problems with test: %s" % error)
def test_rt_send_no_certs(self): """ Test that tries to send a message without using certificates """ if self.test_tcp == "True": try: engine_config = SenderConfigSSL(address=(self.server, self.port)) con = Sender(engine_config) for i in range(self.default_numbers_sendings): con.send(tag=self.my_app, msg=self.test_msg) con.close() except Exception as error: return False else: return True
def test_ssl_rt_send(self): """ Test that tries to send a message through a ssl connection """ try: engine_config = SenderConfigSSL(address=self.server, port=self.port, key=self.key, cert=self.cert, chain=self.chain) con = Sender(engine_config) for i in range(0, self.default_numbers_sendings): con.send(tag=self.my_app, msg='Test SSL msg_ python_sdk_ fork') con.close() except Exception as error: self.fail("Problems with test: %s" % error)
def test_tcp_rt_send(self): """ Tests that a TCP connection and data send it is possible """ if self.test_tcp == "True": try: engine_config = SenderConfigTCP(address=self.tcp_server, port=self.tcp_port) con = Sender(engine_config) for i in range(0, 1): con.send(tag=self.my_app, msg='Test TCP msg') con.close() except Exception as error: self.fail("Problems with test: %s" % error) else: return True
def test_rt_send_no_certs(self): """ Test that tries to send a message without using certificates """ if self.test_tcp == "True": try: engine_config = SenderConfigSSL(address=self.tcp_server, port=self.tcp_port, cert_reqs=False) con = Sender(engine_config) for i in range(0, self.default_numbers_sendings): con.send(tag=self.my_app, msg='Test RT msg') con.close() except Exception as error: self.fail("Problems with test: %s" % error) else: return True
def test_multiline_send(self): """ Test that tries to send a multiple line message through a ssl connection """ try: engine_config = SenderConfigSSL(address=self.server, port=self.port, key=self.key, cert=self.cert, chain=self.chain) con = Sender(engine_config) with open(self.test_file, 'r') as file: content = file.read() con.send(tag=self.my_app, msg=content, multiline=True) con.close() except Exception as error: self.fail("Problems with test: %s" % error)
def test_ssl_rt_send(self): """ Test that tries to send a message through a ssl connection """ try: engine_config = SenderConfigSSL(address=(self.server, self.port), key=self.key, cert=self.cert, chain=self.chain) con = Sender(engine_config) for i in range(self.default_numbers_sendings): con.send(tag=self.my_app, msg=self.test_msg) if len(con.socket.recv(5000)) == 0: raise Exception('Not msg sent!') con.close() except Exception as error: self.fail("Problems with test: %s" % str(error))
def test_Sender_as_handler(self): """ Test that tries to check that Sender class can be used as a Handler and related logs are send to remote server """ try: engine_config = SenderConfigSSL(address=self.server, port=self.port, key=self.key, cert=self.cert, chain=self.chain) con = Sender(engine_config, tag=self.my_app) logger = logging.getLogger('DEVO_logger') logger.setLevel(logging.DEBUG) formatter = logging.Formatter( '%(asctime)s|%(levelname)s|%(message)s') con.setFormatter(formatter) con.setLevel(logging.DEBUG) logger.addHandler(con) for i in range(0, self.default_numbers_sendings): # con.debug("DEVO LOGGING HANDLER TEST at: test_Sender_as_handler" ) # logger.addHandler(con.logger.handlers[0]) logger.info( "Testing Sender inherit logging handler functionality... INFO - log" ) logger.error( "Testing Sender inherit logging handler functionality... ERROR - log" ) logger.warning( "Testing Sender inherit logging handler functionality... WARNING - log" ) logger.debug( "Testing Sender inherit logging handler functionality... DEBUG - log" ) logger.critical( "Testing Sender inherit logging handler functionality... CRITICAL - log" ) con.close() except Exception as error: self.fail("Problems with test: %s" % error)
def test_ssl_rt_send(self): """ Test that tries to send a message through a ssl connection """ try: engine_config = SenderConfigSSL(address=(self.server, self.port), key=self.key, cert=self.cert, chain=self.chain, check_hostname=False, verify_mode=CERT_NONE) con = Sender(engine_config) for i in range(self.default_numbers_sendings): con.send(tag=self.my_app, msg=self.test_msg) data_received = con.socket.recv(5000) print(b"\n" + data_received) if len(data_received) == 0: raise Exception('Not msg sent!') con.close() except Exception as error: self.fail("Problems with test: %s" % str(error))
def test_ssl_zip_send(self): """ Test that tries to send a message through a ssl connection """ try: engine_config = SenderConfigSSL(address=self.server, port=self.port, key=self.key, cert=self.cert, chain=self.chain) con = Sender(engine_config, sockettimeout=15) for i in range(self.default_numbers_sendings): con.send(tag=self.my_bapp, msg=self.test_msg.encode("utf-8"), zip=True) con.flush_buffer() if len(con.socket.recv(1000)) == 0: raise Exception('Not msg sent!') con.close() except Exception as error: self.fail("Problems with test: %s" % error)
def lambda_handler(event, context): # Get the object from the event and show its content type bucket = event['Records'][0]['s3']['bucket']['name'] key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key']) try: print("File to process %s" % key) response = s3.get_object(Bucket=bucket, Key=key) body = response['Body'] data = body.read() ###### START: From this point until END, you need to ###### carefully review the code to make sure all ###### variables match your environment. # If the name has a .gz extension, then decompress the data if key[-3:] == '.gz': data = zlib.decompress(data, 16 + zlib.MAX_WBITS) config = Configuration("config.json") con = Sender(config=config.get("sender")) # Send json events to Devo print("Starting to send lines to Devo") counter = 0 for line in data.splitlines(): events_json = json.loads(line) for single_event in events_json["Records"]: counter += con.send(tag=encode(config.get("tag")), msg=encode(json.dumps(single_event)), zip=False) con.close() print("Finished sending lines to Devo (%d)" % counter) ###### END of code containing key variables. except Exception as e: print(e) print("Error getting file '%s' from bucket '%s'. Make sure they \ exist and your bucket is in the same region as this function." % (key, bucket))
def test_multiline_send(self): """ Test that tries to send a multiple line message through a ssl connection """ try: engine_config = SenderConfigSSL(address=(self.server, self.port), key=self.key, cert=self.cert, chain=self.chain, check_hostname=False, verify_mode=CERT_NONE) con = Sender(engine_config) with open(self.test_file, 'r') as file: content = file.read() con.send(tag=self.my_app, msg=content, multiline=True) con.flush_buffer() data_received = con.socket.recv(5000) print(b"\n" + data_received) if len(data_received) == 0: raise Exception('Not msg sent!') con.close() except Exception as error: self.fail("Problems with test: %s" % error)
import os from devo.sender import Sender, SenderConfigSSL, Lookup server = "us.elb.relay.logtrust.net" port = 443 key = os.getenv('DEVO_SENDER_KEY') cert = os.getenv('DEVO_SENDER_CERT') chain = os.getenv('DEVO_SENDER_CHAIN') lookup_name = 'Test_Lookup_Line_By_Line' engine_config = SenderConfigSSL(address=(server, port), key=key, cert=cert, chain=chain) con = Sender(engine_config) lookup = Lookup(name=lookup_name, historic_tag=None, con=con) p_headers = Lookup.list_to_headers(['KEY', 'HEX', 'COLOR'], 'KEY') lookup.send_control('START', p_headers, 'FULL') lookup.send_data_line(key="11", fields=["11", "HEX11", "COLOR11"]) lookup.send_data_line(key="22", fields=["22", "HEX22", "COLOR22"]) lookup.send_data_line(key="33", fields=["33", "HEX33", "COLOR33"]) lookup.send_control('END', p_headers, 'FULL') con.close()