Exemple #1
0
 def __init__(self, *args, **kwargs):
     MainArgumentParser.__init__(self, *args, **kwargs)
     self.parser.prog = "mbed device-management"
     self.parser.description = (
         "Create or transform a manifest. "
         "Use {} [command] -h for help on each command.").format(
             self.parser.prog)
Exemple #2
0
def mkCert(options):
        cmd = ['cert', 'create', '-o', defaults.certificate, '-K', defaults.certificateKey]

        country = ''
        state = ''
        locality = ''
        organization = ''
        commonName = ''
        if hasattr(options, 'vendor_domain') and options.vendor_domain:
            commonName = options.vendor_domain
        validity = defaults.certificateDuration

        if not options.quiet:
            print('A certificate has not been provided to init, and no certificate is provided in {cert}'.format(
                cert=defaults.certificate))
            print('Init will now guide you through the creation of a certificate.')
            print()
            print('This process will create a self-signed certificate, which is not suitable for production use.')
            print()
            print('In the terminology used by certificates, the "subject" means the holder of the private key that matches a certificate.')
            country = input('In which country is the subject located? ')
            state = input('In which state or province is the subject located? ')
            locality = input('In which city or region is the subject located? ')
            organization = input('What is the name of the subject organization? ')
            commonName = ''
            if hasattr(options, 'vendor_domain') and options.vendor_domain:
                commonName = input('What is the common name of the subject organization? [{}]'.format(options.vendor_domain)) or options.vendor_domain
            else:
                commonName = input('What is the common name of the subject organization? ')
            validity = input('How long (in days) should the certificate be valid? [{}]'.format(defaults.certificateDuration)) or defaults.certificateDuration

        try:
            os.makedirs(defaults.certificatePath)
        except os.error:
            # It is okay if the directory already exists. If something else went wrong, we'll find out when the
            # create occurs
            pass

        cmd = ['cert', 'create', '-o', defaults.certificate, '-K', defaults.certificateKey,
            '-V', str(validity)]
        if country:
            cmd += ['-C', country]
        if state:
            cmd += ['-S', state]
        if locality:
            cmd += ['-L', locality]
        if organization:
            cmd += ['-O', organization]
        if commonName:
            cmd += ['-U', commonName]
        cert_opts = MainArgumentParser().parse_args(cmd).options
        rc = cert.main(cert_opts)
        if rc:
            sys.exit(1)
        options.certificate = open(defaults.certificate, 'rb')
        LOG.info('Certificate written to {}'.format(defaults.certificate))
        options.private_key = open(defaults.certificateKey, 'rb')
        LOG.info('Private key written to {}'.format(defaults.certificateKey))
Exemple #3
0
 def __init__(self, *args, **kwargs):
     MainArgumentParser.__init__(self, *args, **kwargs)
     self.parser.prog = "mbed device-management"
     self.parser.description = (
         "Create or transform a manifest. "
         "Use {} [command] -h for help on each command.").format(
             self.parser.prog)
     for initIndex in range(len(self.parser._subparsers._actions)):
         try:
             if 'init' in (
                     self.parser._subparsers._actions[initIndex]).choices:
                 initParser = self.parser._subparsers._actions[
                     initIndex].choices['init']
         except TypeError:
             pass
     initParser.add_argument(
         '--no-developer-cert',
         help='Do not download the developer certificate')
    def __init__(self):
        self.options = MainArgumentParser().parse_args().options

        log_level = {
            'debug': logging.DEBUG,
            'info': logging.INFO,
            'warning': logging.WARNING,
            'exception': logging.CRITICAL
        }[self.options.log_level]
        logging.basicConfig(level=log_level,
                            format=LOG_FORMAT,
                            datefmt='%Y-%m-%d %H:%M:%S')
        logging.addLevelName(
            logging.INFO,
            "\033[1;32m%s\033[1;0m" % logging.getLevelName(logging.INFO))
        logging.addLevelName(
            logging.WARNING,
            "\033[1;93m%s\033[1;0m" % logging.getLevelName(logging.WARNING))
        logging.addLevelName(
            logging.CRITICAL,
            "\033[1;31m%s\033[1;0m" % logging.getLevelName(logging.CRITICAL))

        LOG.debug('CLIDriver created. Arguments parsed and logging setup.')
def main(options):
    if hasattr(options, 'vendor_domain') and options.vendor_domain:
        if len(options.vendor_domain.split('.')) < 2:
            LOG.critical('"{0}" is not a valid domain name.'.format(
                options.vendor_domain))
            return 1
        options.vendor_id = str(
            uuid.uuid5(uuid.NAMESPACE_DNS, options.vendor_domain))
    vendorId = uuid.UUID(options.vendor_id)

    if hasattr(options, 'model_name') and options.model_name:
        options.class_id = str(uuid.uuid5(vendorId, options.model_name))
    classId = uuid.UUID(options.class_id)

    cert_required = True
    certFile = None
    if options.certificate:
        cert_required = False
        certFile = options.certificate.name
    elif hasattr(options, 'force') and options.force:
        cert_required = True
    else:
        try:
            options.certificate = open(defaults.certificate, 'rb')
            options.private_key = open(defaults.certificateKey, 'rb')
            cert_required = False
            LOG.warning('{} and {} already exist, not overwriting.'.format(
                defaults.certificate, defaults.certificateKey))
        except:
            cert_required = True

    if cert_required:
        cmd = [
            'cert', 'create', '-o', defaults.certificate, '-K',
            defaults.certificateKey
        ]

        country = ''
        state = ''
        locality = ''
        organization = ''
        commonName = ''
        if hasattr(options, 'vendor_domain') and options.vendor_domain:
            commonName = options.vendor_domain
        validity = defaults.certificateDuration

        if not options.quiet:
            print(
                'A certificate has not been provided to init, and no certificate is provided in {cert}'
                .format(cert=defaults.certificate))
            print(
                'Init will now guide you through the creation of a certificate.'
            )
            print()
            print(
                'This process will create a self-signed certificate, which is not suitable for production use.'
            )
            print()
            print(
                'In the terminology used by certificates, the "subject" means the holder of the private key that matches a certificate.'
            )
            country = input('In which country is the subject located? ')
            state = input(
                'In which state or province is the subject located? ')
            locality = input(
                'In which city or region is the subject located? ')
            organization = input(
                'What is the name of the subject organization? ')
            commonName = ''
            if hasattr(options, 'vendor_domain') and options.vendor_domain:
                commonName = input(
                    'What is the common name of the subject organization? [{}]'
                    .format(options.vendor_domain)) or options.vendor_domain
            else:
                commonName = input(
                    'What is the common name of the subject organization? ')
            validity = input(
                'How long (in days) should the certificate be valid? [{}]'.
                format(defaults.certificateDuration
                       )) or defaults.certificateDuration

        try:
            os.makedirs(defaults.certificatePath)
        except os.error:
            # It is okay if the directory already exists. If something else went wrong, we'll find out when the
            # create occurs
            pass

        cmd = [
            'cert', 'create', '-o', defaults.certificate, '-K',
            defaults.certificateKey, '-V',
            str(validity)
        ]
        if country:
            cmd += ['-C', country]
        if state:
            cmd += ['-S', state]
        if locality:
            cmd += ['-L', locality]
        if organization:
            cmd += ['-O', organization]
        if commonName:
            cmd += ['-U', commonName]
        cert_opts = MainArgumentParser().parse_args(cmd).options
        rc = cert.main(cert_opts)
        if rc:
            sys.exit(1)
        options.certificate = open(defaults.certificate, 'rb')
        LOG.info('Certificate written to {}'.format(defaults.certificate))
        options.private_key = open(defaults.certificateKey, 'rb')
        LOG.info('Private key written to {}'.format(defaults.certificateKey))
    # Write the settings

    settings = {
        'default-certificates': [{
            'file': options.certificate.name
        }],
        'signing-script': options.signing_script,
        'private-key': options.private_key.name,
        'classId': str(classId),
        'vendorId': str(vendorId),
        'vendorDomain': options.vendor_domain,
        'modelName': options.model_name
    }

    with open(defaults.config, 'w') as f:
        f.write(json.dumps(settings, sort_keys=True, indent=4))
        LOG.info('Default settings written to {}'.format(defaults.config))

    try:
        writeUpdateDefaults(options)
    except ValueError as e:
        LOG.critical('Error setting defaults: {}'.format(e.message))
        return 1

    cloud_settings = {}
    if hasattr(options, 'server_address') and options.server_address:
        cloud_settings['host'] = options.server_address
    if hasattr(options, 'api_key') and options.api_key:
        cloud_settings['api_key'] = options.api_key

    if cloud_settings:
        with open(defaults.cloud_config, 'w') as f:
            f.write(json.dumps(cloud_settings, sort_keys=True, indent=4))
            LOG.info('Cloud settings written to {}'.format(
                defaults.cloud_config))

    sys.exit(0)