Ejemplo n.º 1
0
    def cert_commands(self):
        """
        Certificate creation commands.
        """
        if self.parser_args.remove and os.path.exists(
                CONFIG['security']['cert_dir']):
            LOGGER.info("Removing certificates...")
            # We don't remove the cert directory because it can be mounted as a volume and it will
            # throw the 'Resource busy' error, we remove everything inside it instead
            for path in glob.glob(
                    os.path.join(CONFIG['security']['cert_dir'], '*')):
                if os.path.isfile(path):
                    os.remove(path)
                if os.path.isdir(path):
                    shutil.rmtree(path)

            LOGGER.info("... Removed certificates")

            LOGGER.info("Creating certificates directories...")
            revoke_dir = os.path.join(CONFIG['security']['cert_dir'],
                                      CONFIG['security']['revoke_cert_dir'])
            renew_dir = os.path.join(CONFIG['security']['cert_dir'],
                                     CONFIG['security']['renew_cert_dir'])
            os.makedirs(renew_dir, exist_ok=True)
            os.makedirs(revoke_dir, exist_ok=True)
            LOGGER.info("... Created certificates directories")

        if self.parser_args.devices is not None:
            if self.parser_args.processes > self.parser_args.devices:
                LOGGER.error(
                    "The number of certificates must be greather than the number of processes!"
                )
                sys.exit(1)
            # Generating the random IDs
            ids = [
                str(uuid.uuid4().hex) for _ in range(self.parser_args.devices)
            ]
            # Begins the certificate generation for random devices IDs
            self.generate_certs(ids)

        if self.parser_args.ids is not None:
            # Begins the certificate generation
            self.generate_certs(self.parser_args.ids)

        if self.parser_args.dojot:
            devices_ids = DojotAPI.get_devices(self.jwt)
            self.generate_certs(devices_ids)

        # Exports the certificates' files
        self.export_certs()
        # Retrieving the CA certificate
        self.retrieve_ca_cert()
        # Mapping the certificates
        self.map_device_ids()
Ejemplo n.º 2
0
    def test_successfully_no_devices(self, mock_requests):
        """
        Should successfully get an empty list of devices.
        """
        DojotAPI.call_api.return_value = {
            "devices": [],
            "pagination": {
                "total": 0
            }
        }

        devices = DojotAPI.get_devices("testJWT")

        DojotAPI.call_api.assert_called_once()
        DojotAPI.call_api.assert_called_with(mock_requests.get, ANY)
        self.assertEqual(devices, [])
Ejemplo n.º 3
0
    def test_successfully_get_devices(self, mock_requests):
        """
        Should successfully get devices.
        """
        DojotAPI.call_api.side_effect = [{
            "devices": [{"id": 0}, {"id": 1}],
            "pagination": {
                "total": 2
            }
        }, [0], [1]]

        devices = DojotAPI.get_devices("testJWT")

        self.assertEqual(DojotAPI.call_api.call_count, 3)
        DojotAPI.call_api.assert_called_with(mock_requests.get, ANY)
        self.assertEqual(devices, [0, 1])