def handler(event, context):
    logger.debug(event)

    table_arn = jmespath.search(
        "Records[?eventSource=='aws:dynamodb'].eventSourceARN | [0]",
        event
    ).split('/stream/')[0]
    if not table_arn:
        raise(EventParsingError('eventSourceARN'))
    table = dynamodb_resource.Table(convertor.arn_to_name(table_arn))

    altered_policies = jmespath.search(
        "Records[?eventSource=='aws:dynamodb'].dynamodb.Keys.policy.S",
        event
    )
    # TODO handle scan api call limit
    for policy in altered_policies:
        items = table.scan(FilterExpression=Attr('policy').eq(policy))['Items']
        policy_resource = Policy(policy)
        if not items:
            policy_resource.delete()
            Role(policy.replace('policy', 'role')).delete()
        else:
            policy_document = table_items_to_document(items)
            policy_resource.update_document(policy_document)
Пример #2
0
    def wait_until_modem_started(self):
        result = 0
        counter = 0
        # Check modem USB interface
        for _ in range(120):
            output = shell_command("lsusb")
            if output[0].find(self.vid) != -1:
                logger.debug("Modem USB interface detected.")
                counter = 0
                result += 1
                break
            else:
                time.sleep(1)
                counter += 1

        # Check modem AT FW
        for _ in range(10):
            output = send_at_com("AT", "OK")
            if output[2] == 0:
                logger.debug("Modem AT FW is working.")
                counter = 0
                result += 1
                break
            else:
                time.sleep(1)
                counter += 1

        if result != 2:
            raise ModemNotFound("Modem couldn't be started!")
Пример #3
0
    def diag_modem_reachable(self):
        logger.debug("[-] : Is modem reachable?")

        output = send_at_com("AT", "OK")
        if output[2] == 0:
            self.diagnostic["modem_reachable"] = True
        else:
            self.diagnostic["modem_reachable"] = False
Пример #4
0
    def diag_sim_ready(self):
        logger.debug("[-] : Is the SIM ready?")

        output = send_at_com("AT+CPIN?", "READY")
        if output[2] == 0:
            self.diagnostic["sim_ready"] = True
        else:
            self.diagnostic["sim_ready"] = False
Пример #5
0
 def create(self, path, name, base_trust_policy):
     response = self.iam_client.create_role(
         Path='/{}/'.format(path),
         RoleName=name,
         AssumeRolePolicyDocument=base_trust_policy,
         Description='Assumed by resources provisioned by products \
          associated with Service Catalog Portfolio {}'.format(name))
     logger.debug('Role created: {}'.format(name))
     return response['Role']['Arn']
Пример #6
0
    def diag_modem_mode(self):
        logger.debug("[-] : Is the modem mode OK?")

        output = send_at_com(self.modem.mode_status_command,
                             self.modem.ecm_mode_response)
        if output[2] == 0:
            self.diagnostic["modem_mode"] = True
        else:
            self.diagnostic["modem_mode"] = False
Пример #7
0
    def diag_network_register(self):
        logger.debug("[-] : Is the network is registered?")

        try:
            self.modem.check_network()
        except:
            self.diagnostic["network_reqister"] = False
        else:
            self.diagnostic["network_reqister"] = True
Пример #8
0
    def diag_apn_set(self):
        logger.debug("[-] : Is the APN is OK?")

        apn_with_quotes = '"%s"' % conf.apn
        output = send_at_com("AT+CGDCONT?", apn_with_quotes)
        if output[2] == 0:
            self.diagnostic["modem_apn"] = True
        else:
            self.diagnostic["modem_apn"] = False
Пример #9
0
    def diag_ecm_pdp_context(self):
        logger.debug("[-] : Is ECM PDP Context is active?")

        output = send_at_com(self.modem.pdp_status_command,
                             self.modem.desired_pdp_status)
        if output[2] == 0:
            self.diagnostic["pdp_context"] = True
        else:
            self.diagnostic["pdp_context"] = False
Пример #10
0
    def write_log(url, **kwargs):
        as_curl = utils.request_as_curl(url,
                                        method=kwargs.get('method'),
                                        data=kwargs.get('data'),
                                        params=kwargs.get('params'),
                                        headers=kwargs.get('headers'),
                                        cookies=kwargs.get('cookies'))

        utils.allure_attach('curl', as_curl)
        logger.debug(as_curl)
Пример #11
0
    def diag_connection_interface(self):
        logger.debug("[-] : Is connection interface exist?")

        output = shell_command("route -n")
        if output[2] == 0:
            if output[0].find(self.modem.interface_name) != -1:
                self.diagnostic["con_interface"] = True
            else:
                self.diagnostic["con_interface"] = False
        else:
            raise RuntimeError("Error occured processing shell command!")
    def wrapper_func(event, context):

        logger.debug("Received %s request with event: %s" %
                     (event['RequestType'], json.dumps(event)))

        response = {
            "StackId": event["StackId"],
            "RequestId": event["RequestId"],
            "LogicalResourceId": event["LogicalResourceId"],
            "PhysicalResourceId": "custom_resource_physical_id",
            "Status": SUCCESS,
        }
        if event.get("PhysicalResourceId", False):
            response["PhysicalResourceId"] = event["PhysicalResourceId"]

        if base_response is not None:
            response.update(base_response)

        try:
            response.update(func(event, context))
        except NoResponse:
            # Do nothing, maybe we're being rescheduled?
            return
        except Exception as e:
            logger.exception("Failed to execute resource function")
            reason = "Exception was raised while handling custom resource."
            reason += " Message {}".format(e.args or e.message)
            response.update({
                "Status": FAILED,
                "Reason": reason,
                "Data": {
                    'FailureReason': reason
                }
            })

        serialized = json.dumps(response)
        logger.info("Responding to '%s' request with: %s" %
                    (event['RequestType'], serialized))

        req = requests.put(event['ResponseURL'],
                           data=serialized,
                           headers={
                               'Content-Length': str(len(serialized)),
                               'Content-Type': ''
                           })

        try:
            req
            logger.debug("Request to CFN API succeeded, nothing to do here")
        except requests.HTTPError as e:
            logger.error("Callback to CFN API failed with status %d" % e.code)
            logger.error("Response: %s" % e.reason)
        except requests.ConnectionError as e:
            logger.error("Failed to reach the server - %s" % e.reason)
 def wrapper(*args, **kwargs):
     try:
         return func(*args, **kwargs)
     except ClientError as err:
         if err.response['Error']['Code'] == code:
             logger.debug(
                 'ClientError {} was ignored while execute function {}'.
                 format(code, func.__name__))
             return None
         else:
             raise err
 def wrapper(*args, **kwargs):
     try:
         return func(*args, **kwargs)
     except error as err:
         if handler:
             return handler()
         else:
             logger.debug(
                 'Error {} was ignored while execute function {}'.
                 format(err, func.__name__))
             return None
Пример #15
0
 def wait_until_modem_turned_off(self):
     counter = 0
     for _ in range(20):
         output = shell_command("lsusb")
         if output[0].find(self.vid) != -1:
             time.sleep(1)
             counter += 1
         else:
             logger.debug("Modem turned off.")
             counter = 0
             return 0
     raise RuntimeError("Modem didn't turn off as expected!")
Пример #16
0
    def diag_usb_interface(self):
        logger.debug("[-] : Is USB interface exist?")

        output = shell_command("lsusb")

        if output[2] == 0:
            if output[0].find(self.modem.vid) != -1:
                self.diagnostic["usb_interface"] = True
            else:
                self.diagnostic["usb_interface"] = False
        else:
            raise RuntimeError("Error occured processing shell command!")
 def exclude(self, resource_generator):
     items = []
     for resource in resource_generator:
         logger.debug('include resource {}'.format(resource.arn))
         if resource.arn:
             items.append({'policy': self.policy.arn, 'arn': resource.arn})
         if resource.assumed_role != self.role.name:
             resource.detach_policy(self.policy.arn)
         else:
             resource.stop_assume_role(self.role.name)
     if len(items) > 0:
         logger.info('Remove resources {}'.format(items))
         self.table.remove(items)
Пример #18
0
    def diag_usb_driver(self):
        logger.debug("[-] : Is USB driver exist?")

        output = shell_command("usb-devices")
        if output[2] == 0:
            if output[0].find("cdc_ether") != -1:
                if output[0].count("cdc_ether") >= 2:
                    self.diagnostic["usb_driver"] = True
                else:
                    self.diagnostic["usb_driver"] = False
            else:
                self.diagnostic["usb_driver"] = False
        else:
            raise RuntimeError("Error occured processing shell command!")
Пример #19
0
 def create(self, path, name, base_trust_policy):
     try:
         response = self.iam_client.create_role(
             Path='/{}/'.format(path),
             RoleName=name,
             AssumeRolePolicyDocument=base_trust_policy,
             Description='Assumed by resources provisioned by products \
              associated with Service Catalog Portfolio {}'.format(name))
         logger.debug('Role created: {}'.format(name))
         return response['Role']['Arn']
     except ClientError as err:
         if err.response['Error']['Code'] == 'EntityAlreadyExists':
             return self.find(name)
         else:
             raise err
Пример #20
0
    def wait_until_modem_interface_up(self):
        counter = 0
        logger.debug("Interface Name: %s", self.interface_name)
        # Check modem connection interface
        for _ in range(20):
            output = shell_command("route -n")
            if output[0].find(self.interface_name) != -1:
                logger.info("Modem interface is detected.")
                counter = 0
                break
            else:
                time.sleep(1)
                counter += 1

        if counter != 0:
            raise ModemNotFound("Modem interface couln't be detected.")
 def update_trust_document(self, service_name):
     policy = self.aro.AssumeRolePolicy()
     policy_doc = self.aro.assume_role_policy_document
     principal_service = jmespath.search("Statement[].Principal.Service",
                                         policy_doc)
     principal_service = jmespath.search("[][][]", principal_service)
     if service_name not in principal_service:
         principal_service.append(service_name)
     policy_doc['Statement'] = [{
         'Action': 'sts:AssumeRole',
         'Effect': 'Allow',
         'Principal': {
             'Service': principal_service
         }
     }]
     policy.update(PolicyDocument=json.dumps(policy_doc))
     logger.debug("Trust policy updated: {}".format(policy_doc))
Пример #22
0
def _identify_os():
    try:
        logger.debug("[+] OS artchitecture")
        system_id["arc"] = str(platform.architecture()[0])

        logger.debug("[+] OS machine")
        system_id["machine"] = str(platform.machine())

        logger.debug("[+] Kernel version")
        system_id["kernel"] = str(platform.release())

        logger.debug("[+] Host name")
        system_id["host_name"] = str(platform.node())

        logger.debug("[+] OS platform")
        system_id["platform"] = str(platform.platform())
    except Exception as error:
        raise RuntimeError("Error occured while getting OS identification!") from error
Пример #23
0
def thread_monitor_and_config(event_object):
    global modem
    while True:
        with lock:
            logger.debug("Configurator is working...")
            configure()
            logger.debug("Network manager is working...")
            network = manage_network(modem)
            logger.debug("Monitor is working...")
            monitor(modem, network)
            logger.debug("Geolocation is working...")
            update_geolocation(modem)
        event_object.wait(conf.get_send_monitoring_data_interval_config())
 def include(self, resource_generator):
     items = []
     for resource in resource_generator:
         logger.debug('include resource {}'.format(resource.arn))
         if resource.access_to:
             items.append({
                 'arn': resource.arn,
                 'policy': self.policy.arn,
                 'sid': resource.statement_id,
                 'actions': resource.actions
             })
             if resource.assumed_role:
                 resource.attach_policy(self.policy.arn)
         if resource.access_from and not resource.assumed_role:
             try:
                 resource.assume_role(self.role.arn)
             except MissingTrustRelationshipError:
                 self.role.update_trust_document(
                     resource.trust_relationship_service)
                 if resource.service_name != 'ec2':
                     resource.assume_role(self.role.arn)
     if len(items) > 0:
         logger.info('Add resources {}'.format(items))
         self.table.add(items)
Пример #25
0
def thread_monitor_and_config(event_object):
    while True:
        with lock:
            logger.debug("Configurator is working...")
            configure()
            logger.debug("Network manager is working...")
            manage_network()
            logger.debug("Monitor is working...")
            monitor()
        event_object.wait(conf.get_send_monitoring_data_interval_config())
Пример #26
0
def identify_setup():

    # Get old system setup if it is exist
    old_system_id = {}
    if os.path.isfile(SYSTEM_PATH):
        try:
            old_system_id = read_yaml_all(SYSTEM_PATH)
        except Exception as error:
            logger.warning("Old system_id in system.yaml file couln't be read!")

    logger.info("[?] System identifying...")

    # Turn off AT command echo (Required)
    logger.debug("[+] Turning off AT command echo")
    try:
        _turn_off_echo()
    except Exception as error:
        raise error

    # Product Name (Optional)
    logger.debug("[+] Product Name")
    try:
        _identify_product_name()
    except Exception as error:
        logger.warning("Modem name identification failed!")
        system_id["modem_name"] = "Unknown"

    # IMEI (Optional)
    logger.debug("[+] IMEI")
    try:
        _identify_imei()
    except Exception as error:
        logger.warning("IMEI identification failed!")
        system_id["imei"] = "Unknown"

    # SW version (Optional)
    logger.debug("[+] Modem firmware revision")
    try:
        _identify_fw_version()
    except Exception as error:
        logger.warning("Modem firmware ver. identification failed!")
        system_id["sw_version"] = "Unknown"

    # ICCID (Optional)
    logger.debug("[+] SIM ICCID")
    try:
        _identify_iccid()
    except Exception as error:
        logger.warning("SIM ICCID identification failed!")
        system_id["iccid"] = "Unknown"

    # OS (Optional)
    logger.debug("[>] OS Identification")
    try:
        _identify_os()
    except Exception as error:
        logger.warning("OS identification failed!")

    # Board (Optional)
    logger.debug("[+] Board Identification")
    try:
        _identify_board()
    except Exception as error:
        logger.warning("Board identification failed!")

    try:
        system_id["last_update"] = int(time.time())
    except Exception as error:
        logger.error("identify() timestamp -> %s", error)

    # IDENTIFICATION REPORT
    if conf.debug_mode and conf.verbose_mode:
        print("")
        print("********************************************************************")
        print("[?] IDENTIFICATION REPORT")
        print("-------------------------")
        for item in system_id.items():
            print(f"[+] {item[0]} --> {item[1]}")
        print("********************************************************************")
        print("")

    # Save ID's to file
    try:
        write_yaml_all(SYSTEM_PATH, system_id)
    except Exception as error:
        raise error

    if system_id != old_system_id:
        logger.warning("System setup has changed!")

    return system_id or {}
Пример #27
0
    def diagnose(self, diag_type=0):

        self.diagnostic = {
            "con_interface": "",
            "modem_reachable": "",
            "usb_driver": "",
            "pdp_context": "",
            "network_reqister": "",
            "sim_ready": "",
            "modem_mode": "",
            "modem_apn": "",
            "timestamp": "",
        }

        logger.info("Diagnostic is working...")

        # 1 - Is connection interface exist?
        logger.debug("[1] : Is connection interface exist?")

        output = shell_command("route -n")
        if output[2] == 0:
            if output[0].find(self.interface_name) != -1:
                self.diagnostic["con_interface"] = True
            else:
                self.diagnostic["con_interface"] = False
        else:
            raise RuntimeError("Error occured processing shell command!")

        # 2 - Is USB interface exist?
        logger.debug("[2] : Is USB interface exist?")

        output = shell_command("lsusb")
        if output[2] == 0:
            if output[0].find(self.vid) != -1:
                self.diagnostic["usb_interface"] = True
            else:
                self.diagnostic["usb_interface"] = False
        else:
            raise RuntimeError("Error occured processing shell command!")

        # 3 - Is USB driver exist?
        logger.debug("[3] : Is USB driver exist?")

        output = shell_command("usb-devices")
        if output[2] == 0:
            if output[0].find("cdc_ether") != -1:
                if output[0].count("cdc_ether") >= 2:
                    self.diagnostic["usb_driver"] = True
                else:
                    self.diagnostic["usb_driver"] = False
            else:
                self.diagnostic["usb_driver"] = False
        else:
            raise RuntimeError("Error occured processing shell command!")

        # 4 - Is modem reachable?
        logger.debug("[4] : Is modem reachable?")

        output = send_at_com("AT", "OK")
        if output[2] == 0:
            self.diagnostic["modem_reachable"] = True
        else:
            self.diagnostic["modem_reachable"] = False

        # 5 - Is ECM PDP Context active?
        logger.debug("[5] : Is ECM PDP Context is active?")

        output = send_at_com(self.pdp_status_command, "1,1")
        if output[2] == 0:
            self.diagnostic["pdp_context"] = True
        else:
            self.diagnostic["pdp_context"] = False

        # 6 - Is the network registered?
        logger.debug("[6] : Is the network is registered?")

        try:
            self.check_network()
        except:
            self.diagnostic["network_reqister"] = False
        else:
            self.diagnostic["network_reqister"] = True

        # 7 - Is the APN OK?
        logger.debug("[7] : Is the APN is OK?")

        apn_with_quotes = '"%s"' % conf.apn
        output = send_at_com("AT+CGDCONT?", apn_with_quotes)
        if output[2] == 0:
            self.diagnostic["modem_apn"] = True
        else:
            self.diagnostic["modem_apn"] = False

        # 8 - Is the modem mode OK?
        logger.debug("[8] : Is the modem mode OK?")

        output = send_at_com(self.mode_status_command, self.ecm_mode_response)
        if output[2] == 0:
            self.diagnostic["modem_mode"] = True
        else:
            self.diagnostic["modem_mode"] = False

        # 9 - Is the SIM ready?
        logger.debug("[9] : Is the SIM ready?")

        output = send_at_com("AT+CPIN?", "READY")
        if output[2] == 0:
            self.diagnostic["sim_ready"] = True
        else:
            self.diagnostic["sim_ready"] = False

        timestamp = time.strftime("%Y-%m-%d_%H:%M:%S")
        self.diagnostic["timestamp"] = timestamp

        if diag_type == 0:
            diag_file_name = "cm-diag_" + str(timestamp) + ".yaml"
            diag_file_path = DIAG_FOLDER_PATH + diag_file_name
            logger.info("Creating diagnostic report on --> %s", diag_file_path)
            write_yaml_all(diag_file_path, self.diagnostic)
        else:
            diag_file_name = "cm-diag-repeated.yaml"
            diag_file_path = DIAG_FOLDER_PATH + diag_file_name
            logger.info("Creating diagnostic report on --> %s", diag_file_path)
            write_yaml_all(diag_file_path, self.diagnostic)

        if conf.debug_mode and conf.verbose_mode:
            print("")
            print("********************************************************************")
            print("[?] DIAGNOSTIC REPORT")
            print("---------------------")
            for item in self.diagnostic.items():
                print(f"[+] {item[0]} --> {item[1]}")
            print("********************************************************************")
            print("")
 def __call__(self, event, context):
     request = event['RequestType']
     logger.debug("Received {} type event. Full parameters: {}".format(
         request, json.dumps(event)))
     return self._dispatch.get(request, self._succeed())(event, context)