Пример #1
0
    def test_self_signed_certificate_subject_missing_fields_valid(self):
        """ Tests getter and setter for certificate subject field.
            Tests if certificate subject field is not set then expected defaults are chosen.
            This test applicable for the auto generated self signed certificate flow.
        """
        test_cases = []
        subj_dict = {
            EC.SUBJECT_COUNTRY_KEY: 'TC',
            EC.SUBJECT_STATE_KEY: 'Test State',
            EC.SUBJECT_LOCALITY_KEY: 'Test Locality',
            EC.SUBJECT_ORGANIZATION_KEY: 'Test Organization',
            EC.SUBJECT_ORGANIZATION_UNIT_KEY: 'Test Unit',
            EC.SUBJECT_COMMON_NAME_KEY: 'Test CommonName'
        }
        default_subj_dict = EdgeDefault.get_certificate_subject_dict()

        all_subj_keys = [
            EC.SUBJECT_COUNTRY_KEY, EC.SUBJECT_STATE_KEY,
            EC.SUBJECT_LOCALITY_KEY, EC.SUBJECT_ORGANIZATION_KEY,
            EC.SUBJECT_ORGANIZATION_UNIT_KEY, EC.SUBJECT_COMMON_NAME_KEY
        ]

        for subj_key in all_subj_keys:
            subj_ip_dict = subj_dict.copy()
            del subj_ip_dict[subj_key]
            subj_op_dict = subj_dict.copy()
            subj_op_dict[subj_key] = default_subj_dict[subj_key]
            self._add_subj_test_case(test_cases, False, EC.SELFSIGNED_KEY,
                                     subj_ip_dict, subj_op_dict)
        self._valid_test_cases_helper('cert subj missing field', test_cases)
Пример #2
0
 def test_self_signed_default_certificate_subject_chosen_valid(self):
     """ Tests getter and setter for certificate subject.
         Tests if certificate subject is None that expected defaults are chosen.
         This test applicable for the auto generated self signed certificate flow.
     """
     test_cases = []
     self._add_subj_test_case(test_cases, False, EC.SELFSIGNED_KEY, None,
                              EdgeDefault.get_certificate_subject_dict())
     self._valid_test_cases_helper('cert subj defaults', test_cases)
Пример #3
0
    def _validation_helper_certificate_subject(self,
                                               result_config_object,
                                               test_case,
                                               msg=None):
        op_subj_dict = test_case[TEST_OP_KEY][EC.CERTS_SUBJECT_KEY]
        if op_subj_dict is None:
            op_subj_dict = EdgeDefault.get_certificate_subject_dict()

        result_subj_dict = result_config_object.certificate_subject_dict
        self.assertEqual(op_subj_dict, result_subj_dict, msg)
Пример #4
0
 def test_preinstalled_certificate_subject_valid(self):
     """ Tests getter and setter for certificate subject.
         This test applicable for the preinstalled certificate flow.
         Regardless of setting a certificate subject, its getter should
         return None.
     """
     test_cases = []
     ip_kwargs = {
         KWARG_OWNER_CERT: OWNER_CERT_FILE_NAME,
         KWARG_DCA_CERT: DEVICE_CA_CERT_FILE_NAME,
         KWARG_DCA_CHAIN: DEVICE_CA_CHAIN_CERT_FILE_NAME,
         KWARG_DCA_PK: DEVICE_CA_PRIVATE_KEY_FILE_NAME
     }
     op_kwargs = {
         KWARG_OWNER_CERT: OWNER_CERT_FILE_NAME,
         KWARG_DCA_CERT: DEVICE_CA_CERT_FILE_NAME,
         KWARG_DCA_CHAIN: DEVICE_CA_CHAIN_CERT_FILE_NAME,
         KWARG_DCA_PK: DEVICE_CA_PRIVATE_KEY_FILE_NAME,
         KWARG_DCA_PASS: None,
         KWARG_AGT_PASS: None,
         KWARG_DCA_PASS_FILE: None,
         KWARG_AGT_PASS_FILE: None
     }
     subj_dict = {
         EC.SUBJECT_COUNTRY_KEY: 'TC',
         EC.SUBJECT_STATE_KEY: 'Test State',
         EC.SUBJECT_LOCALITY_KEY: 'Test Locality',
         EC.SUBJECT_ORGANIZATION_KEY: 'Test Organization',
         EC.SUBJECT_ORGANIZATION_UNIT_KEY: 'Test Unit',
         EC.SUBJECT_COMMON_NAME_KEY: 'Test CommonName'
     }
     self._add_subj_test_case(test_cases,
                              False,
                              EC.PREINSTALL_KEY,
                              subj_dict,
                              subj_dict,
                              ip_kwargs_dict=ip_kwargs,
                              op_kwargs_dict=op_kwargs)
     default_subj_dict = EdgeDefault.get_certificate_subject_dict()
     self._add_subj_test_case(test_cases,
                              False,
                              EC.PREINSTALL_KEY,
                              None,
                              default_subj_dict,
                              ip_kwargs_dict=ip_kwargs,
                              op_kwargs_dict=op_kwargs)
     with patch('edgectl.utils.EdgeUtils.check_if_file_exists',
                MagicMock(return_value=True)):
         self._valid_test_cases_helper('cert subj', test_cases)
Пример #5
0
 def test_cert_subject_dict_valid(self):
     """ Test if the certificate subject dict is valid and as expected"""
     test_dict = EdgeDefault.get_certificate_subject_dict()
     self.assertIn(EdgeConstants.SUBJECT_COUNTRY_KEY, test_dict)
     self.assertIsNotNone(test_dict[EdgeConstants.SUBJECT_COUNTRY_KEY])
     self.assertIn(EdgeConstants.SUBJECT_STATE_KEY, test_dict)
     self.assertIsNotNone(test_dict[EdgeConstants.SUBJECT_STATE_KEY])
     self.assertIn(EdgeConstants.SUBJECT_LOCALITY_KEY, test_dict)
     self.assertIsNotNone(test_dict[EdgeConstants.SUBJECT_LOCALITY_KEY])
     self.assertIn(EdgeConstants.SUBJECT_ORGANIZATION_KEY, test_dict)
     self.assertIsNotNone(test_dict[EdgeConstants.SUBJECT_ORGANIZATION_KEY])
     self.assertIn(EdgeConstants.SUBJECT_ORGANIZATION_UNIT_KEY, test_dict)
     self.assertIsNotNone(
         test_dict[EdgeConstants.SUBJECT_ORGANIZATION_UNIT_KEY])
     self.assertIn(EdgeConstants.SUBJECT_COMMON_NAME_KEY, test_dict)
     self.assertIsNotNone(test_dict[EdgeConstants.SUBJECT_COMMON_NAME_KEY])
Пример #6
0
    def _process_cli_args(self):
        parser = argparse.ArgumentParser(prog=EdgeCLI._prog(),
                                         formatter_class=argparse.RawTextHelpFormatter,
                                         description='Azure IoT Edge Runtime Control Interface (DEPRECATED).\n' +
                                                     'For more information visit: https://docs.microsoft.com/en-us/azure/iot-edge/',
                                         epilog='''''')
        parser.add_argument('--version',
                            action='version',
                            version='{0} {1}'.format(EdgeCLI._prog(), self._version()))

        verbose_help_str = 'Set verbosity. Levels: ' \
                           + ', '.join(EdgeCLI._supported_log_levels()) \
                           + '. Default: ' + EdgeCLI._default_log_level()

        parser.add_argument('--verbose', dest='verbose_level',
                            choices=EdgeCLI._log_level_choices(),
                            default=EdgeCLI._default_log_level(),
                            help=verbose_help_str, metavar='')

        subparsers = parser.add_subparsers(title='commands',
                                           description='Azure IoT Edge Commands',
                                           help='sub-command help',
                                           dest='subparser_name')

        cmd_setup = subparsers.add_parser('setup',
                                          description='Setup the runtime. This must be run before starting.',
                                          help='Setup the runtime. This must be run before starting.',
                                          formatter_class=argparse.RawTextHelpFormatter)

        cmd_setup.add_argument('--config-file',
                               help='Setup the runtime using the specified configuration file. Optional.\n'
                               + 'If specified, all other command line inputs will be ignored.',
                               metavar='')

        cmd_setup.add_argument('--connection-string',
                               help='Set the Azure IoT Hub device connection string. Required.\n'
                               + 'Note: Use double quotes when supplying this input.',
                               metavar='')

        cmd_setup.add_argument('--edge-config-dir',
                               help='Set runtime configuration directory. Optional.\n'
                               + 'Note: If the configuration directory value is provided, it is saved in\n'
                               + 'the ' + EdgeCLI._prog() + ' configuration file located in the following directories:\n'
                               + '   Linux Hosts - ' + EdgeDefault.get_edge_ctl_diagnostic_path(EC.DOCKER_HOST_LINUX) + '\n'
                               + '   Windows Hosts - ' + EdgeDefault.get_edge_ctl_diagnostic_path(EC.DOCKER_HOST_WINDOWS) + '\n'
                               + '   MacOS Hosts - ' + EdgeDefault.get_edge_ctl_diagnostic_path(EC.DOCKER_HOST_DARWIN) + '\n'
                               + 'Instead of using this option, an environment variable "' + EC.ENV_EDGECONFIGDIR +'"\n'
                               + 'can be set with an absolute path to a home directory.\n'
                               + 'If environment variable "' + EC.ENV_EDGECONFIGDIR +'" is set and this option is specified,\n'
                               + 'the environment variable will take precedence and the supplied directory value will be ignored.\n'
                               + 'If none of these are provided, the following directories will be used as the default\n'
                               + 'IoT Edge configuration directory:\n'
                               + '   Linux Hosts - ' + EdgeDefault.get_config_dir(EC.DOCKER_HOST_LINUX) + '\n'
                               + '   Windows Hosts - ' + EdgeDefault.get_config_dir(EC.DOCKER_HOST_WINDOWS) + '\n'
                               + '   MacOS Hosts - ' + EdgeDefault.get_config_dir(EC.DOCKER_HOST_DARWIN),
                               metavar='')

        cmd_setup.add_argument('--edge-home-dir',
                               help='Set runtime home directory. Optional.\n'
                               + 'Default:\n'
                               + '   Linux Hosts - ' + EdgeDefault.get_home_dir(EC.DOCKER_HOST_LINUX) + '\n'
                               + '   Windows Hosts - ' + EdgeDefault.get_home_dir(EC.DOCKER_HOST_WINDOWS) + '\n'
                               + '   MacOS Hosts - ' + EdgeDefault.get_home_dir(EC.DOCKER_HOST_DARWIN),
                               metavar='')

        cmd_setup.add_argument('--edge-hostname',
                               help='Set the runtime hostname (FQDN). Optional.\n'
                               + 'Used when operating the runtime as a \'Gateway\' for leaf devices.',
                               metavar='')

        log_levels = EdgeDefault.get_runtime_log_levels()
        log_levels = ", ".join(log_levels)
        cmd_setup.add_argument('--runtime-log-level',
                               help='Set runtime log level. Optional.\n'
                               + 'Levels:  ' + log_levels + '\n'
                               + 'Default: ' + EdgeDefault.get_default_runtime_log_level(),
                               metavar='')

        cmd_setup.add_argument('--image',
                               help='Set the Edge Agent image. Optional.\n',
                               metavar='')

        cmd_setup.add_argument('--docker-registries',
                               help='Set a list of registries and their credentials. Optional.\n'
                               + 'Specified as triples of registry address, username, password.\n'
                               + 'Example: --docker-registries reg1 user1 pass1'
                               , nargs='+', metavar='')

        cmd_setup.add_argument('--docker-uri', '--edge-runtime-docker-uri',
                               help='Set docker endpoint URI for the IoT Edge runtime. Optional.\n'
                               + 'Default:\n'
                               + '   Linux Hosts - ' + EdgeDefault.get_docker_uri(EC.DOCKER_HOST_LINUX, EC.DOCKER_ENGINE_LINUX) + '\n'
                               + '   Windows Hosts (Linux VM) - ' + EdgeDefault.get_docker_uri(EC.DOCKER_HOST_WINDOWS, EC.DOCKER_ENGINE_LINUX) + '\n'
                               + '   Windows Hosts (Native) - ' + EdgeDefault.get_docker_uri(EC.DOCKER_HOST_WINDOWS, EC.DOCKER_ENGINE_WINDOWS) + '\n'
                               + '   MacOS Hosts - ' + EdgeDefault.get_docker_uri(EC.DOCKER_HOST_DARWIN, EC.DOCKER_ENGINE_LINUX) + '\n'
                               + 'Note: This is strictly the URI that the Edge runtime will use to interact with docker daemon.\n'
                               + 'The URI is determined by the underlying container technology being used by the docker daemon.\n'
                               + '  - Specifically, these could be Linux based containers or Windows based containers.\n'
                               + 'Sub note: Windows hosts are able to run both Linux and Windows containers but not vice versa.\n'
                               + 'In most cases, the Edge runtime docker URI is the same as the host docker daemon URI\n'
                               + 'but that may not always be the case. For example, if the docker host is Windows or\n'
                               + 'Windows Subsystem for Linux (WSL), the docker URI could be tcp://x.y.z.w:2375.\n'
                               + 'However, the underlying container technology could be Linux based and thus\n'
                               + 'the Edge runtime docker URI would be: ' + EdgeDefault.get_docker_uri(EC.DOCKER_HOST_LINUX, EC.DOCKER_ENGINE_LINUX),
                               metavar='')

        cmd_setup.add_argument('--upstream-protocol',
                               help='Set the protocol that the edge runtime should use to communicate with the IoTHub. Optional.\n'
                               + 'Permitted values are Amqp (Amqp over TCP), AmqpWs (Amqp over WebSockets), \n'
                               + 'Mqtt (Mqtt over TCP) and MqttWs (Mqtt over WebSockets)\n',
                               metavar='')

        cmd_setup.add_argument('--auto-cert-gen-force-no-passwords', '--nopass',
                               help='Do not prompt for passwords when generating private keys. Optional.',
                               action='store_true')

        cmd_setup.add_argument('--owner-ca-cert-file',
                               help='Owner CA certificate in X.509 PEM format.\n' \
                               'Used when operating the runtime as a \'Gateway\' for leaf devices. Optional.',
                               metavar='')

        cmd_setup.add_argument('--device-ca-cert-file',
                               help='Device CA certificate in X.509 PEM format.\n' \
                               'Used when operating the runtime as a \'Gateway\' for leaf devices. Optional.',
                               metavar='')

        cmd_setup.add_argument('--device-ca-chain-cert-file',
                               help='Device CA chain certificate in X.509 PEM format.\n' \
                               'Used when operating the runtime as a \'Gateway\' for leaf devices. Optional.',
                               metavar='')

        cmd_setup.add_argument('--device-ca-private-key-file',
                               help='Device CA certificate private key file in PEM format.\n' \
                               'Used when operating the runtime as a \'Gateway\' for leaf devices. Optional.',
                               metavar='')

        cmd_setup.add_argument('--device-ca-passphrase-file',
                               help='Device CA certificate private key passphrase file in ascii text.\n' \
                               'Either provide the passphrase file or use the --device-ca-passphrase option but not both.',
                               metavar='')

        cmd_setup.add_argument('--device-ca-passphrase',
                               help='Device CA certificate private key passphrase in ascii text.\n' \
                               'Use this option to provide the passphrase or use the\n' \
                               '--device-ca-passphrase-file option but not both.',
                               metavar='')

        cmd_setup.add_argument('--agent-ca-passphrase-file',
                               help='Agent CA certificate private key passphrase file in ascii text.\n' \
                               'Either provide the passphrase or use the --agent-ca-passphrase option but not both.',
                               metavar='')

        cmd_setup.add_argument('--agent-ca-passphrase',
                               help='Agent CA certificate private key passphrase in ascii text.\n' \
                               'Use this option to provide the passphrase or use the\n' \
                               '--agent-ca-passphrase-file option but not both.',
                               metavar='')

        subj = EdgeDefault.get_certificate_subject_dict()
        cmd_setup.add_argument('-C', '--country',
                               help='Two letter country code. This parameter is used when autogenerating certificates. Optional.\n'
                               'Default: \'{0}\''.format(subj[EC.SUBJECT_COUNTRY_KEY]), metavar='')

        cmd_setup.add_argument('-ST', '--state',
                               help='State. This parameter is used when autogenerating certificates. Optional.\n'
                               'Default: \'{0}\''.format(subj[EC.SUBJECT_STATE_KEY]), metavar='')

        cmd_setup.add_argument('-L', '--locality',
                               help='Locality or city. This parameter is used when autogenerating certificates. Optional.\n'
                               'Default: \'{0}\''.format(subj[EC.SUBJECT_LOCALITY_KEY]), metavar='')

        cmd_setup.add_argument('-OR', '--organization',
                               help='Organization name. This parameter is used when autogenerating certificates. Optional.\n'
                               'Default: \'{0}\''.format(subj[EC.SUBJECT_ORGANIZATION_KEY]), metavar='')

        cmd_setup.add_argument('-OU', '--organization-unit',
                               help='Organization unit name. This parameter is used when autogenerating certificates. Optional.\n'
                               'Default: \'{0}\''.format(subj[EC.SUBJECT_ORGANIZATION_UNIT_KEY]), metavar='')

        cmd_setup.add_argument('-CN', '--common-name',
                               help='Common name used for the Device CA certificate. This parameter is used when autogenerating\n' \
                               'certificates. Optional.\n' \
                               'Default: \'{0}\'. ' \
                               .format(subj[EC.SUBJECT_COMMON_NAME_KEY]),
                               metavar='')

        cmd_setup.set_defaults(func=self._parse_edge_command)

        cmd_start = subparsers.add_parser('start', description="Start the runtime.", help='Start the runtime.')
        cmd_start.set_defaults(func=self._parse_edge_command)

        cmd_restart = subparsers.add_parser('restart', description='Restart the runtime.', help='Restart the runtime.')
        cmd_restart.set_defaults(func=self._parse_edge_command)

        cmd_stop = subparsers.add_parser('stop', description='Stop the runtime.', help='Stop the runtime.')
        cmd_stop.set_defaults(func=self._parse_edge_command)

        cmd_status = subparsers.add_parser('status', description='Report the status of the runtime.', help='Report the status of the runtime.')
        cmd_status.set_defaults(func=self._parse_edge_command)

        cmd_update = subparsers.add_parser('update', description='Update the Edge Agent image.', help='Update the Edge Agent image.')
        cmd_update.add_argument('--image', help='Specify the Edge Agent image', metavar='')
        cmd_update.set_defaults(func=self._parse_edge_command)

        cmd_uninstall = subparsers.add_parser('uninstall', description='Remove all modules and generated files.',
                                              help='Remove all modules and generated files.')
        cmd_uninstall.set_defaults(func=self._parse_edge_command)

        cmd_login = subparsers.add_parser('login', description="Log in to a container registry.",
                                          help='Log in to a container registry.',
                                          formatter_class=argparse.RawTextHelpFormatter)
        cmd_login.add_argument('--address', help='Specify the container registry. (e.g. example.azurecr.io)\nDefault: Docker Hub',
                               required=False, default=EdgeCLI._DEFAULT_REGISTRY_ADDRESS)
        cmd_login.add_argument('--username', help='Specify the username of the container registry', required=True)
        cmd_login.add_argument('--password', help='Specify the password of the container registry', required=True)
        cmd_login.set_defaults(func=self._parse_edge_command)

        args = parser.parse_args()
        if 'func' in vars(args):
            return args.func(args)
        parser.print_usage()
        return (False, False)