Example #1
0
def register_thing(name: str, n_certs: int) -> None:
    """
    Creates devices and exports them to a Redis database.

    Args:
        name: the thread name.
        n_certs: number of certificates to generate.
    """
    start_time = time.time()

    redis_conn = connect_to_redis()

    pipe = redis_conn.pipeline()
    start_batch_time = start_time
    for i in range(n_certs):

        if i % ARGS.batch == 0:
            end_batch_time = time.time()
            diff = end_batch_time - start_batch_time
            start_batch_time = end_batch_time
            logging.info("Execution time: %f secs by Thread %s with batch %s", diff, name, i)

        thing_id = str(uuid.uuid4().hex)
        thing = Thing(CONFIG['app']['tenant'], thing_id)
        pipe.hmset(thing_id, thing.get_args_in_dict())

    pipe.execute()

    end_time = time.time()
    redis_conn.close()
    logging.info("Thread %s finished in %fs", name, end_time - start_time)
Example #2
0
 def test_thing_renew_cert(self, _mock_utils, cert_mock):
     """
         Test renew certificate
     """
     thing = Thing('tenant', 'dev-id')
     thing.renew_cert()
     cert_mock().renew_cert.assert_called_once()
Example #3
0
    def register_thing(self, name: str, n_certs: int,
                       id_list: List[str]) -> None:
        """
        Creates devices and exports them to a Redis database.

        Args:
            name: the process name.
            n_certs: number of certificates to generate.
            id_list: list of IDs to be used by the certificates.
        """
        start_time = time.time()

        redis_conn = self.connect_to_redis()

        pipe = redis_conn.pipeline()
        start_batch_time = start_time
        for i in range(n_certs):

            if (i != 0) and (i % self.parser_args.batch == 0):
                end_batch_time = time.time()
                diff = end_batch_time - start_batch_time
                LOGGER.info(
                    "Execution time: %f secs by process %s with batch %s",
                    diff, name, i)

                pipe.execute()
                pipe = redis_conn.pipeline()

                LOGGER.debug("Waiting %.1fs to start another batch...",
                             self.parser_args.wait)
                time.sleep(self.parser_args.wait)
                LOGGER.debug("... Resuming certificate generation")

                start_batch_time = time.time()

            thing_id = id_list[i]
            thing = None

            has_failed = True

            while has_failed:
                try:
                    thing = Thing(CONFIG['app']['tenant'], thing_id)
                except requests.exceptions.ConnectionError as exception:
                    LOGGER.error(str(exception))
                    LOGGER.info("Regenerating the certificate")
                    time.sleep(5)
                else:
                    has_failed = False

            pipe.hmset(thing_id, thing.get_args_in_dict())

        pipe.execute()

        end_time = time.time()
        redis_conn.close()
        LOGGER.info("Process %s finished in %fs", name, end_time - start_time)
Example #4
0
 def test_get_args_in_dict(self, mock_utils, cert_mock):
     """
         Test ger args in dict
     """
     thing = Thing('tenant', 'dev-id')
     self.assertEqual(
         thing.get_args_in_dict(), {
             "thing_id": mock_utils.create_thing_id('tenant', 'dev-id'),
             "private_key": cert_mock().key['pem'],
             "thing_certificate": cert_mock().crt["pem"]
         })
Example #5
0
    def new_cert(tenant: str, device_id: str) -> Thing:
        """
        Creates/renovates the certificate for a device.
        """
        Utils.validate_tenant(tenant)
        Utils.validate_device_id(device_id)
        thing = Thing(tenant, device_id)

        return thing
Example #6
0
def generate_certs(ids: list) -> None:
    """
    Generates the certificates for the IDs.

    Args:
        ids: list of IDs.
    """
    start_time = time.time()

    redis_conn = connect_to_redis()

    for device_id in ids:
        thing = Thing(ARGS.tenant, device_id)
        redis_conn.hmset(device_id, thing.get_args_in_dict())

    end_time = time.time()
    logging.info("Generated %d IDs in %f", len(ids), end_time - start_time)
    redis_conn.close()
Example #7
0
    def new_cert(tenant: str, device_id: str) -> Thing:
        """
        Creates/renovates the certificate for a device.
        """
        thing = None
        try:
            thing = Thing(tenant + ":" + device_id)
        except Exception as exception:
            logging.error("Error: %s", str(exception))

        return thing
Example #8
0
    def test_thing_constructor(self, mock_utils, _cert_mock):
        """
            Thing contructor test
        """

        mock_utils.create_thing_id.return_value = 'thing-id'
        thing = Thing('tenant', 'dev-id')

        self.assertEqual(thing.tenant, 'tenant')
        self.assertEqual(thing.device_id, 'dev-id')
        self.assertIsNotNone(thing.cert)
        self.assertEqual(thing.thing_id, 'thing-id')
        self.assertIsNotNone(thing.private_key)
        self.assertIsNotNone(thing.thing_certificate)
        mock_utils.create_thing_id.assert_called_once_with('tenant', 'dev-id')