Beispiel #1
0
def _construct_x509_auth(device_id, primary_thumbprint, secondary_thumbprint, valid_days, output_dir):
    if any([primary_thumbprint, secondary_thumbprint]):
        return Authentication(x509_thumbprint=X509Thumbprint(primary_thumbprint, secondary_thumbprint))
    valid_days = valid_days if valid_days is not None else 365
    output_dir = output_dir if output_dir is not None else '.'
    cert_info = create_self_signed_certificate(device_id, valid_days, output_dir)
    return Authentication(x509_thumbprint=X509Thumbprint(cert_info['thumbprint']))
Beispiel #2
0
def iot_device_create(client, hub_name, device_id, resource_group_name=None, x509=False, primary_thumbprint=None,
                      secondary_thumbprint=None, valid_days=None, output_dir=None):
    if x509 is True:
        if (primary_thumbprint is not None or secondary_thumbprint is not None) and \
                (valid_days is not None or output_dir is not None):
            raise CLIError('Certificate thumbprint parameters are used for existing certificates.\n'
                           'Certificate valid days and output directory are used for creating self-signed certificate.\n'
                           'They must not be used together.')
        if output_dir is not None and not exists(output_dir):
            raise CLIError('Directory not exist: {0}'.format(output_dir))
    else:
        if primary_thumbprint is not None or secondary_thumbprint is not None or valid_days is not None or output_dir is not None:
            raise CLIError('X.509 certificate parameters must be used with --x509 flag.')

    device_client = _get_iot_device_client(client, resource_group_name, hub_name, device_id)
    device_description = DeviceDescription(device_id=device_id)

    if x509 is True:
        if primary_thumbprint is not None or secondary_thumbprint is not None:
            device_description.authentication = Authentication(x509_thumbprint=X509Thumbprint(primary_thumbprint,
                                                                                              secondary_thumbprint))
        else:
            valid_days = valid_days if valid_days is not None else 365
            output_dir = output_dir if output_dir is not None else '.'
            cert_info = create_self_signed_certificate(device_id, valid_days, output_dir)
            device_description.authentication = Authentication(x509_thumbprint=X509Thumbprint(cert_info['thumbprint']))

    results = device_client.create_or_update(device_id, device_description, if_match=None)

    # TODO: This is a work-around because thumbprint is not returned by IoT Device Registry REST API.
    #       Remove the following once this issue is fixed.
    if x509 is True:
        results.authentication.x509_thumbprint = device_description.authentication.x509_thumbprint
    return results