def test_get_iam_policy(self):
        # Setup Expected Response
        version = 351608024
        etag = b'21'
        expected_response = {'version': version, 'etag': etag}
        expected_response = policy_pb2.Policy(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch('google.api_core.grpc_helpers.create_channel')
        with patch as create_channel:
            create_channel.return_value = channel
            client = iot_v1.DeviceManagerClient()

        # Setup Request
        resource = client.registry_path('[PROJECT]', '[LOCATION]',
                                        '[REGISTRY]')

        response = client.get_iam_policy(resource)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = iam_policy_pb2.GetIamPolicyRequest(
            resource=resource)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 2
0
def create_rs256_device(service_account_json, project_id, cloud_region,
                        registry_id, device_id, certificate_file):
    """Create a new device with the given id, using RS256 for
    authentication."""
    # [START iot_create_rsa_device]
    client = iot_v1.DeviceManagerClient()

    parent = client.registry_path(project_id, cloud_region, registry_id)

    with io.open(certificate_file) as f:
        certificate = f.read()

    # Note: You can have multiple credentials associated with a device.
    device_template = {
        'id':
        device_id,
        'credentials': [{
            'public_key': {
                'format': 'RSA_X509_PEM',
                'key': certificate
            }
        }]
    }

    return client.create_device(parent, device_template)
Exemplo n.º 3
0
def create_device(service_account_json, project_id, cloud_region, registry_id,
                  device_id):
    """Create a device to bind to a gateway if it does not exist."""
    # [START iot_create_device]
    # Check that the device doesn't already exist
    client = iot_v1.DeviceManagerClient()

    exists = False

    parent = client.registry_path(project_id, cloud_region, registry_id)

    devices = list(client.list_devices(parent=parent))

    for device in devices:
        if device.id == device_id:
            exists = True

    # Create the device
    device_template = {
        'id': device_id,
        'gateway_config': {
            'gateway_type': 'NON_GATEWAY',
            'gateway_auth_method': 'ASSOCIATION_ONLY'
        }
    }

    if not exists:
        res = client.create_device(parent, device_template)
        print('Created Device {}'.format(res))
    else:
        print('Device exists, skipping')
Exemplo n.º 4
0
def submit(device_id):
    cloud_region = 'us-central1'
    registry_id = 'cybergym-registry'

    # Init Publisher client for server
    client = iot_v1.DeviceManagerClient()  # publishes to device
    device_path = client.device_path(project, cloud_region, registry_id,
                                     device_id)
    if request.method == 'POST':
        resp = json.loads(request.data)
        command = resp['command']
        data = command.encode("utf-8")
        print("[+] Publishing to device topic")
        try:
            client.send_command_to_device(request={
                "name": device_path,
                "binary_data": data
            })
        except NotFound as e:
            print(e)
            flash(
                f'Device {device_id} responded with 404. Ensure the device is properly connected.',
                'alert-warning')
            return jsonify(error=e)
        except Exception as e:
            return jsonify(error=e)
        finally:
            return jsonify(success=True)
    else:
        return jsonify({'resp': 404})
Exemplo n.º 5
0
def get_registry(service_account_json, project_id, cloud_region, registry_id):
    """ Retrieves a device registry."""
    # [START iot_get_registry]
    client = iot_v1.DeviceManagerClient()
    registry_path = client.registry_path(project_id, cloud_region, registry_id)

    return client.get_device_registry(registry_path)
Exemplo n.º 6
0
def send_message(project_id, cloud_region, registry_id, device_id, message):
    client = iot_v1.DeviceManagerClient()
    device_path = client.device_path(project_id, cloud_region, registry_id,
                                     device_id)
    data = message.encode('utf-8')
    version = 0  #default version
    return client.send_command_to_device(device_path, data, version)
Exemplo n.º 7
0
def patch_es256_auth(service_account_json, project_id, cloud_region,
                     registry_id, device_id, public_key_file):
    """Patch the device to add an ES256 public key to the device."""
    # [START iot_patch_es]
    # project_id = 'YOUR_PROJECT_ID'
    # cloud_region = 'us-central1'
    # registry_id = 'your-registry-id'
    # device_id = 'your-device-id'
    # public_key_file = 'path/to/certificate.pem'
    print('Patch device with ES256 certificate')

    client = iot_v1.DeviceManagerClient()
    device_path = client.device_path(project_id, cloud_region, registry_id,
                                     device_id)

    public_key_bytes = ''
    with io.open(public_key_file) as f:
        public_key_bytes = f.read()

    key = iot_v1.types.PublicKeyCredential(format='ES256_PEM',
                                           key=public_key_bytes)

    cred = iot_v1.types.DeviceCredential(public_key=key)
    device = client.get_device(device_path)

    device.id = b''
    device.num_id = 0
    device.credentials.append(cred)

    mask = iot_v1.types.FieldMask()
    mask.paths.append('credentials')

    return client.update_device(device=device, update_mask=mask)
Exemplo n.º 8
0
def create_rs256_device(
    service_account_json,
    project_id,
    cloud_region,
    registry_id,
    device_id,
    certificate_file,
):
    client = iot_v1.DeviceManagerClient()

    parent = client.registry_path(project_id, cloud_region, registry_id)

    with io.open(certificate_file) as f:
        certificate = f.read()

    # Note: You can have multiple credentials associated with a device.
    device_template = {
        "id":
        device_id,
        "credentials": [{
            "public_key": {
                "format": iot_v1.PublicKeyFormat.RSA_X509_PEM,
                "key": certificate,
            }
        }],
    }

    return client.create_device(request={
        "parent": parent,
        "device": device_template
    })
Exemplo n.º 9
0
def create_devices(stations):
    """
    Creates all stations that currently do not exist in the IoT Core registry as new devices. 
    To handle authentication pre created certificates are used, which are part of this git repository. This is only for demo purpose!
    """
    try:
        #get content of pre created certificate
        with io.open('certs/rsa_public.pem') as f:
            certificate = f.read()

        #retrieve a list of already existing devices
        device_manager = gcp_iot.DeviceManagerClient()
        parent = device_manager.registry_path(os.environ['IOT_PROJECT_ID'],
                                              os.environ['IOT_REGION'],
                                              os.environ['IOT_REGISTRY_ID'])
        dev_list = list(device_manager.list_devices(parent=parent))
        devices = {
            dev_list[i].id: dev_list[i]
            for i in range(0, len(dev_list))
        }
        del dev_list

        for station in stations.values():
            device_id = get_device_id(station.get('station_id', ''))

            if devices.get(device_id, None) == None:
                #create new devices that do not exist
                create_device(device_manager, parent, device_id, certificate)

    except Exception as ex:
        print("Unexpected error {0}".format(ex))
Exemplo n.º 10
0
def create_registry(service_account_json, project_id, cloud_region,
                    pubsub_topic, registry_id):
    client = iot_v1.DeviceManagerClient()
    parent = f"projects/{project_id}/locations/{cloud_region}"

    if not pubsub_topic.startswith("projects/"):
        pubsub_topic = "projects/{}/topics/{}".format(project_id, pubsub_topic)

    body = {
        "event_notification_configs": [{
            "pubsub_topic_name": pubsub_topic
        }],
        "id": registry_id,
    }

    try:
        response = client.create_device_registry(request={
            "parent": parent,
            "device_registry": body
        })
        print("Created registry")
        return response
    except HttpError:
        print("Error, registry not created")
        raise
    except AlreadyExists:
        print("Error, registry already exists")
        raise
Exemplo n.º 11
0
def patch_rsa256_auth(
    service_account_json,
    project_id,
    cloud_region,
    registry_id,
    device_id,
    public_key_file,
):
    print("Patch device with RSA256 certificate")

    client = iot_v1.DeviceManagerClient()
    device_path = client.device_path(project_id, cloud_region, registry_id,
                                     device_id)

    public_key_bytes = ""
    with io.open(public_key_file) as f:
        public_key_bytes = f.read()

    key = iot_v1.PublicKeyCredential(
        format=iot_v1.PublicKeyFormat.RSA_X509_PEM, key=public_key_bytes)

    cred = iot_v1.DeviceCredential(public_key=key)
    device = client.get_device(request={"name": device_path})

    device.id = b""
    device.num_id = 0
    device.credentials.append(cred)

    mask = gp_field_mask.FieldMask()
    mask.paths.append("credentials")

    return client.update_device(request={
        "device": device,
        "update_mask": mask
    })
Exemplo n.º 12
0
def create_device(service_account_json, project_id, cloud_region, registry_id,
                  device_id):
    # Check that the device doesn't already exist
    client = iot_v1.DeviceManagerClient()

    exists = False

    parent = client.registry_path(project_id, cloud_region, registry_id)

    devices = list(client.list_devices(request={"parent": parent}))

    for device in devices:
        if device.id == device_id:
            exists = True

    # Create the device
    device_template = {
        "id": device_id,
        "gateway_config": {
            "gateway_type": iot_v1.GatewayType.NON_GATEWAY,
            "gateway_auth_method": iot_v1.GatewayAuthMethod.ASSOCIATION_ONLY,
        },
    }

    if not exists:
        res = client.create_device(request={
            "parent": parent,
            "device": device_template
        })
        print("Created Device {}".format(res))
    else:
        print("Device exists, skipping")
Exemplo n.º 13
0
    def test_list_device_states(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = device_manager_pb2.ListDeviceStatesResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = iot_v1.DeviceManagerClient()

        # Setup Request
        name = client.device_path("[PROJECT]", "[LOCATION]", "[REGISTRY]",
                                  "[DEVICE]")

        response = client.list_device_states(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = device_manager_pb2.ListDeviceStatesRequest(
            name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 14
0
def create_registry(project_id, cloud_region, pubsub_topic, registry_id):
    client = iot_v1.DeviceManagerClient()
    parent = client.location_path(project_id, cloud_region)

    if not pubsub_topic.startswith('projects/'):
        pubsub_topic = 'projects/{}/topics/{}'.format(project_id, pubsub_topic)

    pubsub_topic_power = 'projects/{}/topics/power'.format(project_id)

    body = {
        'event_notification_configs': [{
            'pubsub_topic_name': pubsub_topic,
            'subfolder_matches': 'water',
            'subfolder_matches': 'rice'
        }, {
            'pubsub_topic_name': pubsub_topic_power,
        }],
        'id':
        registry_id
    }

    try:
        response = client.create_device_registry(parent, body)
        print('Created registry')
        return response
    except HttpError:
        print('Error, registry not created')
        raise
    except AlreadyExists:
        print('Error, registry already exists')
        raise
    def test_send_command_to_device(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = device_manager_pb2.SendCommandToDeviceResponse(
            **expected_response
        )

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = iot_v1.DeviceManagerClient()

        # Setup Request
        name = client.device_path("[PROJECT]", "[LOCATION]", "[REGISTRY]", "[DEVICE]")
        binary_data = b"40"

        response = client.send_command_to_device(name, binary_data)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = device_manager_pb2.SendCommandToDeviceRequest(
            name=name, binary_data=binary_data
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_get_device(self):
        # Setup Expected Response
        id_ = "id3355"
        name_2 = "name2-1052831874"
        num_id = 1034366860
        blocked = True
        expected_response = {
            "id": id_,
            "name": name_2,
            "num_id": num_id,
            "blocked": blocked,
        }
        expected_response = resources_pb2.Device(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = iot_v1.DeviceManagerClient()

        # Setup Request
        name = client.device_path("[PROJECT]", "[LOCATION]", "[REGISTRY]", "[DEVICE]")

        response = client.get_device(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = device_manager_pb2.GetDeviceRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_unbind_device_from_gateway(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = device_manager_pb2.UnbindDeviceFromGatewayResponse(
            **expected_response
        )

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = iot_v1.DeviceManagerClient()

        # Setup Request
        parent = client.registry_path("[PROJECT]", "[LOCATION]", "[REGISTRY]")
        gateway_id = "gatewayId955798774"
        device_id = "deviceId25209764"

        response = client.unbind_device_from_gateway(parent, gateway_id, device_id)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = device_manager_pb2.UnbindDeviceFromGatewayRequest(
            parent=parent, gateway_id=gateway_id, device_id=device_id
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_update_device(self):
        # Setup Expected Response
        id_ = "id3355"
        name = "name3373707"
        num_id = 1034366860
        blocked = True
        expected_response = {
            "id": id_,
            "name": name,
            "num_id": num_id,
            "blocked": blocked,
        }
        expected_response = resources_pb2.Device(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = iot_v1.DeviceManagerClient()

        # Setup Request
        device = {}
        update_mask = {}

        response = client.update_device(device, update_mask)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = device_manager_pb2.UpdateDeviceRequest(
            device=device, update_mask=update_mask
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 19
0
def create_es256_device(service_account_json, project_id, cloud_region,
                        registry_id, device_id, public_key_file):
    """Create a new device with the given id, using ES256 for
    authentication."""
    # [START iot_create_es_device]
    # project_id = 'YOUR_PROJECT_ID'
    # cloud_region = 'us-central1'
    # registry_id = 'your-registry-id'
    # device_id = 'your-device-id'
    # public_key_file = 'path/to/certificate.pem'

    client = iot_v1.DeviceManagerClient()

    parent = client.registry_path(project_id, cloud_region, registry_id)

    with io.open(public_key_file) as f:
        public_key = f.read()

    # Note: You can have multiple credentials associated with a device.
    device_template = {
        'id': device_id,
        'credentials': [{
            'public_key': {
                'format': 'ES256_PEM',
                'key': public_key
            }
        }]
    }

    return client.create_device(parent, device_template)
    def test_list_devices(self):
        # Setup Expected Response
        next_page_token = ""
        devices_element = {}
        devices = [devices_element]
        expected_response = {"next_page_token": next_page_token, "devices": devices}
        expected_response = device_manager_pb2.ListDevicesResponse(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = iot_v1.DeviceManagerClient()

        # Setup Request
        parent = client.registry_path("[PROJECT]", "[LOCATION]", "[REGISTRY]")

        paged_list_response = client.list_devices(parent)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.devices[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = device_manager_pb2.ListDevicesRequest(parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 21
0
def setup():
    page_template = './iot_setup.jinja'
    cloud_region = 'us-central1'
    registry_id = 'cybergym-registry'

    # Initiate IoT client and get list of all registered IoT devices in project
    iot_client = iot_v1.DeviceManagerClient()
    devices_gen = iot_client.list_devices(
        parent=
        f'projects/{project}/locations/{cloud_region}/registries/{registry_id}'
    )
    device_list = [i.id for i in devices_gen]

    if request.method == 'POST':
        print(request.data)
        resp = json.loads(request.data)
        device_id = resp['device_id']
        check_device = True if device_id in device_list else False
        if check_device:
            print(f'Recieved object {resp}')
            return jsonify(
                {'url': url_for('iot_bp.index', device_id=device_id)})
        message = jsonify(
            {'error_msg': f'Unrecognized device with ID: {device_id}'})
        return make_response(message, 400)
    return render_template(page_template)
    def test_modify_cloud_to_device_config(self):
        # Setup Expected Response
        version = 351608024
        binary_data_2 = b"-37"
        expected_response = {"version": version, "binary_data": binary_data_2}
        expected_response = resources_pb2.DeviceConfig(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = iot_v1.DeviceManagerClient()

        # Setup Request
        name = client.device_path("[PROJECT]", "[LOCATION]", "[REGISTRY]", "[DEVICE]")
        binary_data = b"40"

        response = client.modify_cloud_to_device_config(name, binary_data)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = device_manager_pb2.ModifyCloudToDeviceConfigRequest(
            name=name, binary_data=binary_data
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
def device_config(config):
    client = iot_v1.DeviceManagerClient()
    name = client.device_path(os.environ['PROJECT_ID'],
                              os.environ['REGION'],  os.environ['REGISTRY'],
                              os.environ['DEVICE'])
    binary_data = bytes(config, 'utf-8')
    client.modify_cloud_to_device_config(name, binary_data)
    def test_set_iam_policy(self):
        # Setup Expected Response
        version = 351608024
        etag = b"21"
        expected_response = {"version": version, "etag": etag}
        expected_response = policy_pb2.Policy(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = iot_v1.DeviceManagerClient()

        # Setup Request
        resource = client.registry_path("[PROJECT]", "[LOCATION]", "[REGISTRY]")
        policy = {}

        response = client.set_iam_policy(resource, policy)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = iam_policy_pb2.SetIamPolicyRequest(
            resource=resource, policy=policy
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 25
0
def patch_rsa256_auth(service_account_json, project_id, cloud_region,
                      registry_id, device_id, public_key_file):
    """Patch the device to add an RSA256 public key to the device."""
    # [START iot_patch_rsa]
    print('Patch device with RSA256 certificate')

    client = iot_v1.DeviceManagerClient()
    device_path = client.device_path(project_id, cloud_region, registry_id,
                                     device_id)

    public_key_bytes = ''
    with io.open(public_key_file) as f:
        public_key_bytes = f.read()

    key = iot_v1.types.PublicKeyCredential(format='RSA_X509_PEM',
                                           key=public_key_bytes)

    cred = iot_v1.types.DeviceCredential(public_key=key)
    device = client.get_device(device_path)

    device.id = b''
    device.num_id = 0
    device.credentials.append(cred)

    mask = iot_v1.types.FieldMask()
    mask.paths.append('credentials')

    return client.update_device(device=device, update_mask=mask)
    def test_create_device_registry(self):
        # Setup Expected Response
        id_ = "id3355"
        name = "name3373707"
        expected_response = {"id": id_, "name": name}
        expected_response = resources_pb2.DeviceRegistry(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = iot_v1.DeviceManagerClient()

        # Setup Request
        parent = client.location_path("[PROJECT]", "[LOCATION]")
        device_registry = {}

        response = client.create_device_registry(parent, device_registry)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = device_manager_pb2.CreateDeviceRegistryRequest(
            parent=parent, device_registry=device_registry
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 27
0
    def __init__(self, service_account_file_path, project_id, cloud_region, registry_id):
        self.project_id = project_id
        self.cloud_region = cloud_region
        self.registry_id = registry_id

        os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = service_account_file_path
        self.client = iot_v1.DeviceManagerClient()
    def test_test_iam_permissions(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = iam_policy_pb2.TestIamPermissionsResponse(
            **expected_response
        )

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = iot_v1.DeviceManagerClient()

        # Setup Request
        resource = client.registry_path("[PROJECT]", "[LOCATION]", "[REGISTRY]")
        permissions = []

        response = client.test_iam_permissions(resource, permissions)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = iam_policy_pb2.TestIamPermissionsRequest(
            resource=resource, permissions=permissions
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 29
0
def create_registry(service_account_json, project_id, cloud_region,
                    pubsub_topic, registry_id):
    """ Creates a registry and returns the result. Returns an empty result if
    the registry already exists."""
    # [START iot_create_registry]
    client = iot_v1.DeviceManagerClient()
    parent = client.location_path(project_id, cloud_region)

    if not pubsub_topic.startswith('projects/'):
        pubsub_topic = 'projects/{}/topics/{}'.format(project_id, pubsub_topic)

    body = {
        'event_notification_configs': [{
            'pubsub_topic_name': pubsub_topic
        }],
        'id': registry_id
    }

    try:
        response = client.create_device_registry(parent, body)
        print('Created registry')
        return response
    except HttpError:
        print('Error, registry not created')
        return ""
    except AlreadyExists:
        print('Error, registry already exists')
        return ""
    def test_get_device(self):
        # Setup Expected Response
        id_ = 'id3355'
        name_2 = 'name2-1052831874'
        num_id = 1034366860
        blocked = True
        expected_response = {
            'id': id_,
            'name': name_2,
            'num_id': num_id,
            'blocked': blocked
        }
        expected_response = resources_pb2.Device(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch('google.api_core.grpc_helpers.create_channel')
        with patch as create_channel:
            create_channel.return_value = channel
            client = iot_v1.DeviceManagerClient()

        # Setup Request
        name = client.device_path('[PROJECT]', '[LOCATION]', '[REGISTRY]',
                                  '[DEVICE]')

        response = client.get_device(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = device_manager_pb2.GetDeviceRequest(name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request