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))
Exemple #2
0
    def test_ssl_lookup_simplify(self):
        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)

        lookup = Lookup(name=self.lookup_name, historic_tag=None, con=con)
        lookup.send_headers(headers=["KEY", "HEX", "COLOR"],
                            key="KEY",
                            action="START")
        if len(con.socket.recv(1000)) == 0:
            raise Exception("Not msg sent!")
        lookup.send_data_line(key="11", fields=["11", "HEX12", "COLOR12"])
        if len(con.socket.recv(1000)) == 0:
            raise Exception("Not msg sent!")
        lookup.send_headers(headers=["KEY", "HEX", "COLOR"],
                            key="KEY",
                            action="END")
        if len(con.socket.recv(1000)) == 0:
            raise Exception("Not msg sent!")

        con.socket.shutdown(0)
def configure(args):
    """For load configuration file/object"""

    if args.get('config'):
        config = Configuration(args.get('config'))
        config.mix(dict(args))
    else:
        config = dict(args)

    if 'freq' in config.keys():
        parts = config['freq'].split('-')
        config['freq'] = (float(parts[0]), float(parts[1]))

    config['template'] = config['template'].read()

    # Initialize LtSender with the config credentials but only
    # if we aren't in batch mode or simulation mode
    engine = None
    if not (config['batch_mode'] or config['simulation']):
        try:
            if "sender" not in config.keys():
                config['sender'] = {
                    'key': config.get('key', None),
                    'chain': config.get('chain', None),
                    'cert': config.get('cert', None),
                    'address': config.get('address', None),
                    'port': config.get('port', 443)
                }

            engine = Sender(config=config.get('sender'))
        except Exception as error:
            print_error(error, show_help=False)
            print_error("Error when loading devo sender configuration",
                        show_help=True)
    return engine, config
Exemple #4
0
    def test_sender_as_handler_static(self):
        """
        Test that tries to check that Sender class can be used as a Handler
        through the static functions
        and related logs are send to remote server
        """
        try:
            engine_config = {"address": self.server, "port": self.port,
                             "key": self.key, "cert": self.cert,
                             "chain": self.chain, "check_hostname": False,
                             "verify_mode": CERT_NONE}

            con = Sender.for_logging(config=engine_config, tag=self.my_app,
                                     level=TEST_FACILITY)
            logger = get_log(name="DevoLogger2", handler=con,
                             level=TEST_FACILITY)

            print("Testing logger info")
            logger.info("Testing Sender static handler functionality... "
                        "INFO - log")
            data_received = con.socket.recv(5000)
            print(b"\n" + data_received)
            if len(data_received) == 0:
                raise Exception('Not msg sent!')

            print("Testing logger error")
            logger.error("Testing Sender static logging handler "
                         "functionality... ERROR - log")
            data_received = con.socket.recv(5000)
            print(b"\n" + data_received)
            if len(data_received) == 0:
                raise Exception('Not msg sent!')

            print("Testing logger warning")
            logger.warning("Testing Sender static logging handler "
                           "functionality... WARNING - log")
            data_received = con.socket.recv(5000)
            print(b"\n" + data_received)
            if len(data_received) == 0:
                raise Exception('Not msg sent!')

            print("Testing logger debug")
            logger.debug("Testing Sender static logging handler "
                         "functionality... DEBUG - log")
            data_received = con.socket.recv(5000)
            print(b"\n" + data_received)
            if len(data_received) == 0:
                raise Exception('Not msg sent!')

            print("Testing logger critical")
            logger.critical("Testing Sender static logging handler "
                            "functionality... CRITICAL - log")
            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))
Exemple #5
0
    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, self.port),
                                            key=self.key,
                                            cert=self.cert,
                                            chain=self.chain,
                                            check_hostname=False,
                                            verify_mode=CERT_NONE)
            con = Sender.for_logging(config=engine_config,
                                     tag=self.my_app,
                                     level=TEST_FACILITY)
            # 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")
            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))
Exemple #6
0
    def test_create_lookup_key_index_preserves_structure(self):
        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)
        lookup = Lookup(name=self.lookup_name, con=con)
        headers = ["col1", "col2", "col3"]
        fields = ["a", "b", "c"]

        expected_headers = '[{"col1":{"type":"str","key":true}},{"col2":{"type":"str"}},{"col3":{"type":"str"}}]'
        with mock.patch.object(lookup,
                               "send_control",
                               wraps=lookup.send_control) as lookup_spy:
            lookup.send_headers(headers=headers,
                                key_index=0,
                                event="START",
                                action="FULL")
            lookup_spy.assert_called_with(action="FULL",
                                          event="START",
                                          headers=expected_headers)
            lookup.send_data_line(key_index=0, fields=fields)
            lookup.send_headers(headers=headers,
                                key_index=0,
                                event="END",
                                action="FULL")
            lookup_spy.assert_called_with(action="FULL",
                                          event="END",
                                          headers=expected_headers)
        con.socket.shutdown(0)
Exemple #7
0
    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, self.port),
                                            key=self.key, cert=self.cert,
                                            chain=self.chain,
                                            check_hostname=False,
                                            verify_mode=CERT_NONE)
            con = Sender.for_logging(config=engine_config, tag=self.my_app,
                                     level=TEST_FACILITY)
            logger = get_log(name="DevoLogger", handler=con,
                             level=TEST_FACILITY)
            print("Testing logger info")
            logger.info("Testing Sender inherit logging handler functio"
                        "nality... INFO - log")
            data_received = con.socket.recv(5000)
            print(b"\n" + data_received)
            if len(data_received) == 0:
                raise Exception('Not msg sent!')

            print("Testing logger error")
            logger.error("Testing Sender inherit logging handler function"
                         "ality... ERROR - log")
            data_received = con.socket.recv(5000)
            print(b"\n" + data_received)
            if len(data_received) == 0:
                raise Exception('Not msg sent!')

            print("Testing logger warning")
            logger.warning("Testing Sender inherit logging handler functio"
                           "nality... WARNING - log")
            data_received = con.socket.recv(5000)
            print(b"\n" + data_received)
            if len(data_received) == 0:
                raise Exception('Not msg sent!')

            print("Testing logger debug")
            logger.debug("Testing Sender inherit logging handler functiona"
                         "lity... DEBUG - log")
            data_received = con.socket.recv(5000)
            print(b"\n" + data_received)
            if len(data_received) == 0:
                raise Exception('Not msg sent!')

            print("Testing logger critical")
            logger.critical("Testing Sender inherit logging handler functio"
                            "nality... CRITICAL - log")
            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))
Exemple #8
0
def write_to_lookup_table_command():
    lookup_table_name = demisto.args()['lookupTableName']
    headers = check_type(demisto.args()['headers'], list)
    records = check_type(demisto.args()['records'], list)

    creds = get_writer_creds()

    engine_config = SenderConfigSSL(address=(WRITER_RELAY, 443),
                                    key=creds['key'].name,
                                    cert=creds['crt'].name,
                                    chain=creds['chain'].name)

    try:
        con = Sender(config=engine_config, timeout=60)

        lookup = Lookup(name=lookup_table_name, historic_tag=None, con=con)
        # Order sensitive list
        pHeaders = json.dumps(headers)

        lookup.send_control('START', pHeaders, 'INC')

        for r in records:
            lookup.send_data_line(key=r['key'], fields=r['values'])

        lookup.send_control('END', pHeaders, 'INC')
    finally:
        con.flush_buffer()
        con.socket.shutdown(0)

    entry = {
        'Type': entryTypes['note'],
        'Contents': {
            'recordsWritten': records
        },
        'ContentsFormat': formats['json'],
        'ReadableContentsFormat': formats['markdown'],
        'EntryContext': {
            'Devo.RecordsWritten': records
        }
    }

    md = tableToMarkdown('Entries to load into Devo', records)
    entry['HumanReadable'] = md

    return [entry]
Exemple #9
0
    def test_sender_as_handler_static(self):
        """
        Test that tries to check that Sender class can be used as a Handler
        through the static functions
        and related logs are send to remote server
        """
        try:
            engine_config = {
                "address": self.server,
                "port": self.port,
                "key": self.key,
                "cert": self.cert,
                "chain": self.chain,
                "type": "SSL",
                "cert_regs": True
            }
            con = Sender.for_logging(engine_config, "SSL", self.my_app)
            logger = logging.getLogger('DEVO_logger_static')
            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 static handler functionality... "
                        "INFO - log")
            if len(con.socket.recv(5000)) == 0:
                raise Exception('Not msg sent!')

            logger.error("Testing Sender static logging handler "
                         "functionality... ERROR - log")
            if len(con.socket.recv(5000)) == 0:
                raise Exception('Not msg sent!')

            logger.warning("Testing Sender static logging handler "
                           "functionality... WARNING - log")
            if len(con.socket.recv(5000)) == 0:
                raise Exception('Not msg sent!')

            logger.debug("Testing Sender static logging handler "
                         "functionality... DEBUG - log")
            if len(con.socket.recv(5000)) == 0:
                raise Exception('Not msg sent!')

            logger.critical("Testing Sender static logging handler "
                            "functionality... 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)
Exemple #10
0
    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)
Exemple #11
0
    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)
Exemple #12
0
    def test_compose_mem(self):
        self.assertEqual(Sender.compose_mem("test.tag"),
                         '<14>Jan  1 00:00:00 %s test.tag: ' % self.localhost)

        self.assertEqual(Sender.compose_mem("test.tag", hostname="my-pc"),
                         '<14>Jan  1 00:00:00 my-pc test.tag: ')

        self.assertEqual(
            Sender.compose_mem("test.tag", date="1991-02-20 12:00:00"),
            '<14>1991-02-20 12:00:00 %s test.tag: ' % self.localhost)

        self.assertEqual(
            Sender.compose_mem(b"test.tag", bytes=True),
            b'<14>Jan  1 00:00:00 %s test.tag: ' %
            self.localhost.encode("utf-8"))

        self.assertEqual(
            Sender.compose_mem(b"test.tag", hostname=b"my-pc", bytes=True),
            b'<14>Jan  1 00:00:00 my-pc test.tag: ')

        self.assertEqual(
            Sender.compose_mem(b"test.tag",
                               date=b"1991-02-20 12:00:00",
                               bytes=True),
            b'<14>1991-02-20 12:00:00 %s test.tag: ' %
            self.localhost.encode("utf-8"))
Exemple #13
0
    def __init__(self,
                 profile='default',
                 key=None,
                 crt=None,
                 chain=None,
                 relay=None,
                 port=443,
                 credential_path=None,
                 **kwargs):

        self.profile = profile
        self.key = key
        self.crt = crt
        self.chain = chain
        self.relay = relay
        self.port = port

        if credential_path is None:
            self.credential_path = Path.home() / '.devo_credentials'
        else:
            self.credential_path = Path(credential_path).resolve().expanduser()

        if not all([key, crt, chain, relay]):
            self._read_profile()

        if not all([self.key, self.crt, self.chain, self.relay]):
            raise Exception(
                'Credentials and relay must be specified or in ~/.devo_credentials'
            )

        config_dict = kwargs
        config_dict.update(
            dict(address=self.relay,
                 port=self.port,
                 key=self.key,
                 cert=self.crt,
                 chain=self.chain))

        self.sender = Sender(config_dict)
Exemple #14
0
    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 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))
Exemple #16
0
def write_to_table_command():
    table_name = demisto.args()['tableName']
    records = check_type(demisto.args()['records'], list)
    linq_base = demisto.args().get('linqLinkBase', None)

    creds = get_writer_creds()
    linq = f"from {table_name}"

    sender = Sender(
        SenderConfigSSL(address=(WRITER_RELAY, PORT),
                        key=creds['key'].name,
                        cert=creds['crt'].name,
                        chain=creds['chain'].name))

    for r in records:
        try:
            sender.send(tag=table_name, msg=json.dumps(r))
        except TypeError:
            sender.send(tag=table_name, msg=f"{r}")

    querylink = {
        'DevoTableLink':
        build_link(linq,
                   int(1000 * time.time()) - 3600000,
                   int(1000 * time.time()),
                   linq_base=linq_base)
    }

    entry = {
        'Type': entryTypes['note'],
        'Contents': {
            'recordsWritten': records
        },
        'ContentsFormat': formats['json'],
        'ReadableContentsFormat': formats['markdown'],
        'EntryContext': {
            'Devo.RecordsWritten': records,
            'Devo.LinqQuery': linq
        }
    }
    entry_linq = {
        'Type': entryTypes['note'],
        'Contents': querylink,
        'ContentsFormat': formats['json'],
        'ReadableContentsFormat': formats['markdown'],
        'EntryContext': {
            'Devo.QueryLink': createContext(querylink)
        }
    }
    md = tableToMarkdown('Entries to load into Devo', records)
    entry['HumanReadable'] = md

    md_linq = tableToMarkdown(
        'Link to Devo Query',
        {'DevoTableLink': f'[Devo Direct Link]({querylink["DevoTableLink"]})'})
    entry_linq['HumanReadable'] = md_linq

    return [entry, entry_linq]
Exemple #17
0
    def test_ssl_lookup_override(self):
        engine_config = SenderConfigSSL(address=(self.server, self.port),
                                        key=self.key,
                                        cert=self.cert,
                                        chain=self.chain)
        con = Sender(engine_config)

        lookup = Lookup(name=self.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", "HEX12", "COLOR12"])
        lookup.send_control('END', p_headers, 'FULL')

        con.socket.shutdown(0)
Exemple #18
0
    def test_ssl_lookup_csv_send(self):

        engine_config = SenderConfigSSL(address=(self.server, self.port),
                                        key=self.key, cert=self.cert,
                                        chain=self.chain)
        con = Sender(engine_config)
        lookup = Lookup(name=self.lookup_name, historic_tag=None, con=con)

        with open(self.lookup_file) as f:
            line = f.readline()

        lookup.send_csv(self.lookup_file,
                        headers=line.rstrip().split(","),
                        key=self.lookup_key)

        con.socket.shutdown(0)
Exemple #19
0
    def test_ssl_lookup_simplify(self):
        engine_config = SenderConfigSSL(address=(self.server, self.port),
                                        key=self.key,
                                        cert=self.cert,
                                        chain=self.chain)
        con = Sender(engine_config)

        lookup = Lookup(name=self.lookup_name, historic_tag=None, con=con)
        lookup.send_headers(headers=['KEY', 'HEX', 'COLOR'],
                            key='KEY',
                            action='START')
        lookup.send_data_line(key="11", fields=["11", "HEX12", "COLOR12"])
        lookup.send_headers(headers=['KEY', 'HEX', 'COLOR'],
                            key='KEY',
                            action='END')

        con.socket.shutdown(0)
Exemple #20
0
    def test_sender_as_handler_static(self):
        """
        Test that tries to check that Sender class can be used as a Handler
        through the static functions
        and related logs are send to remote server
        """
        try:
            engine_config = {
                "address": self.server,
                "port": self.port,
                "key": self.key,
                "cert": self.cert,
                "chain": self.chain
            }

            con = Sender.for_logging(config=engine_config, tag=self.my_app)
            logger = get_log(name="DevoLogger2", handler=con)

            logger.info("Testing Sender static handler functionality... "
                        "INFO - log")
            if len(con.socket.recv(5000)) == 0:
                raise Exception('Not msg sent!')

            logger.error("Testing Sender static logging handler "
                         "functionality... ERROR - log")
            if len(con.socket.recv(5000)) == 0:
                raise Exception('Not msg sent!')

            logger.warning("Testing Sender static logging handler "
                           "functionality... WARNING - log")
            if len(con.socket.recv(5000)) == 0:
                raise Exception('Not msg sent!')

            logger.debug("Testing Sender static logging handler "
                         "functionality... DEBUG - log")
            if len(con.socket.recv(5000)) == 0:
                raise Exception('Not msg sent!')

            logger.critical("Testing Sender static logging handler "
                            "functionality... 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" % str(error))
Exemple #21
0
    def test_escape_quotes_in_send_data_line(self):
        engine_config = SenderConfigSSL(
            address=(self.server, self.port),
            key=self.key,
            cert=self.cert,
        )
        con = Sender(engine_config)

        lookup = Lookup(name=self.lookup_name,
                        historic_tag=None,
                        con=con,
                        escape_quotes=True)

        with mock.patch.object(Lookup, 'clean_field',
                               wraps=Lookup.clean_field) as clean_field:
            lookup.send_data_line(fields=["11", 'Double quotes"'])
            clean_field.assert_called_with('Double quotes"', True)
Exemple #22
0
 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)
         for i in range(0, self.default_numbers_sendings):
             con.send(tag=self.my_bapp,
                      msg=b'Test SSL msg_ python_sdk_ fork',
                      zip=True)
         con.flush_buffer()
         con.close()
     except Exception as error:
         self.fail("Problems with test: %s" % error)
Exemple #23
0
    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)
Exemple #24
0
 def test_ssl_zip_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, timeout=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" % str(error))
Exemple #25
0
def check_configuration():
    # Check all settings related if set
    # Basic functionality of integration
    list(
        ds.Reader(oauth_token=READER_OAUTH_TOKEN,
                  end_point=READER_ENDPOINT,
                  verify=not ALLOW_INSECURE).query(HEALTHCHECK_QUERY,
                                                   start=int(time.time() - 1),
                                                   stop=int(time.time()),
                                                   output='dict'))

    if WRITER_RELAY and WRITER_CREDENTIALS:
        creds = get_writer_creds()
        Sender(SenderConfigSSL(address=(WRITER_RELAY, PORT),
                               key=creds['key'].name, cert=creds['crt'].name, chain=creds['chain'].name))\
            .send(tag=HEALTHCHECK_WRITER_TABLE, msg=f'{HEALTHCHECK_WRITER_RECORD}')

    if FETCH_INCIDENTS_FILTER:
        alert_filters = check_type(FETCH_INCIDENTS_FILTER, dict)

        assert alert_filters['type'] in [
            'AND', 'OR'
        ], 'Missing key:"type" or unsupported value in fetch_incidents_filters'

        filters = check_type(alert_filters['filters'], list)

        for filt in filters:
            assert filt[
                'key'], 'Missing key: "key" in fetch_incidents_filters.filters configuration'
            assert filt['operator'] in ['=', '/=', '>', '<', '>=', '<=', 'and', 'or', '->'], 'Missing key: "operator"'\
                ' or unsupported operator in fetch_incidents_filters.filters configuration'
            assert filt[
                'value'], 'Missing key:"value" in fetch_incidents_filters.filters configuration'

    if FETCH_INCIDENTS_DEDUPE:
        dedupe_conf = check_type(FETCH_INCIDENTS_DEDUPE, dict)

        assert isinstance(
            dedupe_conf['cooldown'],
            (int,
             float)), 'Invalid fetch_incidents_deduplication configuration'

    return True
Exemple #26
0
    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)
            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()

            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)
Exemple #27
0
    def test_ssl_lookup_new_line(self):
        engine_config = SenderConfigSSL(address=(self.server, self.port),
                                        key=self.key, cert=self.cert,
                                        chain=self.chain)
        con = Sender(engine_config)

        lookup = Lookup(name=self.lookup_name, historic_tag=None, con=con)
        p_headers = Lookup.list_to_headers(['KEY', 'HEX', 'COLOR'], 'KEY')
        lookup.send_control('START', p_headers, 'INC')
        if len(con.socket.recv(1000)) == 0:
            raise Exception('Not msg sent!')
        lookup.send_data_line(key="11", fields=["11", "HEX12", "COLOR12"])
        if len(con.socket.recv(1000)) == 0:
            raise Exception('Not msg sent!')
        lookup.send_control('END', p_headers, 'INC')
        if len(con.socket.recv(1000)) == 0:
            raise Exception('Not msg sent!')

        con.socket.shutdown(0)
Exemple #28
0
    def test_escape_quotes_in_send_csv(self):
        engine_config = SenderConfigSSL(
            address=(self.server, self.port),
            key=self.key,
            cert=self.cert,
        )
        con = Sender(engine_config)

        lookup = Lookup(name=self.lookup_name,
                        historic_tag=None,
                        con=con,
                        escape_quotes=True)

        with mock.patch.object(Lookup, 'clean_field',
                               wraps=Lookup.clean_field) as clean_field:
            lookup.send_csv(path=self.lookup_file,
                            has_header=True,
                            key=self.lookup_key)
            clean_field.assert_called_with('ffffff', True)
Exemple #29
0
 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)
Exemple #30
0
    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, self.port),
                                            key=self.key,
                                            cert=self.cert,
                                            chain=self.chain)
            con = Sender.for_logging(config=engine_config, tag=self.my_app)
            logger = get_log(name="DevoLogger", handler=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" % str(error))