Search in sources :

Example 1 with MultiplexingClientDeviceRegistrationAuthenticationException

use of com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException in project azure-iot-sdk-java by Azure.

the class MultiplexingClient method open.

/**
 * Opens this multiplexing client. This may be done before or after registering any number of device clients.
 * <p>
 * This call behaves synchronously, so if it returns without throwing, then all registered device clients were
 * successfully opened.
 * <p>
 * If this client is already open, then this method will do nothing.
 * <p>
 * @param withRetry if true, this open call will apply the current retry policy to allow for the open call to be
 * retried if it fails.
 *
 * @throws MultiplexingClientException If any IO or authentication errors occur while opening the multiplexed connection.
 * @throws MultiplexingClientDeviceRegistrationAuthenticationException If one or many of the registered devices failed to authenticate.
 * Any devices not found in the map of registration exceptions provided by
 * {@link MultiplexingClientDeviceRegistrationAuthenticationException#getRegistrationExceptions()} have registered successfully.
 * Even when this is thrown, the AMQPS/AMQPS_WS connection is still open, and other clients may be registered to it.
 */
public void open(boolean withRetry) throws MultiplexingClientException {
    synchronized (this.operationLock) {
        log.info("Opening multiplexing client");
        try {
            this.deviceIO.openWithoutWrappingException(withRetry);
        } catch (TransportException e) {
            // so that users don't need to look at the cause of the thrown exception to get this important information.
            if (e instanceof MultiplexingDeviceUnauthorizedException) {
                MultiplexingClientDeviceRegistrationAuthenticationException newException = new MultiplexingClientDeviceRegistrationAuthenticationException(OPEN_ERROR_MESSAGE, e);
                // Bring the exceptions map from the cause to the root level exception, so that users don't have to use
                // fields from inner exceptions.
                newException.setRegistrationExceptionsMap(((MultiplexingDeviceUnauthorizedException) e).getRegistrationExceptions());
                throw newException;
            }
            throw new MultiplexingClientException(OPEN_ERROR_MESSAGE, e);
        }
        log.info("Successfully opened multiplexing client");
    }
}
Also used : MultiplexingDeviceUnauthorizedException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingDeviceUnauthorizedException) MultiplexingClientDeviceRegistrationAuthenticationException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException) TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException) MultiplexingClientException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientException)

Example 2 with MultiplexingClientDeviceRegistrationAuthenticationException

use of com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method registerDevicesWithIncorrectCredentialsBeforeOpenThrowsOnOpen.

@ContinuousIntegrationTest
@Test
public void registerDevicesWithIncorrectCredentialsBeforeOpenThrowsOnOpen() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        testInstance.multiplexingClient.unregisterDeviceClient(testInstance.deviceClientArray.get(i));
    }
    List<DeviceClient> clientsWithIncorrectCredentials = new ArrayList<>();
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        // shift the keys for each device so that device n uses key for device n + 1 (and final device uses key for device 0)
        String incorrectConnectionString;
        if (i == DEVICE_MULTIPLEX_COUNT - 1) {
            incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(0));
            incorrectConnectionString = incorrectConnectionString.replace(testInstance.deviceIdentityArray.get(0).getDeviceId(), testInstance.deviceIdentityArray.get(i).getDeviceId());
        } else {
            incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(i + 1));
            incorrectConnectionString = incorrectConnectionString.replace(testInstance.deviceIdentityArray.get(i + 1).getDeviceId(), testInstance.deviceIdentityArray.get(i).getDeviceId());
        }
        DeviceClient clientWithIncorrectCredentials = new DeviceClient(incorrectConnectionString, testInstance.protocol);
        clientsWithIncorrectCredentials.add(clientWithIncorrectCredentials);
    }
    testInstance.multiplexingClient.registerDeviceClients(clientsWithIncorrectCredentials);
    boolean expectedExceptionThrown = false;
    try {
        testInstance.multiplexingClient.open();
    } catch (MultiplexingClientDeviceRegistrationAuthenticationException e) {
        Map<String, Exception> registrationExceptions = e.getRegistrationExceptions();
        assertEquals(DEVICE_MULTIPLEX_COUNT, registrationExceptions.size());
        for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
            String deviceId = testInstance.deviceIdentityArray.get(i).getDeviceId();
            assertTrue(registrationExceptions.containsKey(deviceId));
            assertTrue(registrationExceptions.get(deviceId) instanceof UnauthorizedException);
        }
        expectedExceptionThrown = true;
    }
    testInstance.multiplexingClient.close();
    assertTrue("Expected exception was not thrown", expectedExceptionThrown);
}
Also used : DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) MultiplexingClientDeviceRegistrationAuthenticationException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException) ArrayList(java.util.ArrayList) UnauthorizedException(com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Map(java.util.Map) HashMap(java.util.HashMap) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 3 with MultiplexingClientDeviceRegistrationAuthenticationException

use of com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method registerDevicesWithIncorrectCredentialsAfterOpenThrows.

// Attempt to register a batch of devices, all with the wrong connection string. The thrown exception
// should contain all the exceptions thrown by the service.
@ContinuousIntegrationTest
@Test
public void registerDevicesWithIncorrectCredentialsAfterOpenThrows() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        testInstance.multiplexingClient.unregisterDeviceClient(testInstance.deviceClientArray.get(i));
    }
    testInstance.multiplexingClient.open();
    List<DeviceClient> clientsWithIncorrectCredentials = new ArrayList<>();
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        // shift the keys for each device so that device n uses key for device n + 1 (and final device uses key for device 0)
        String incorrectConnectionString;
        if (i == DEVICE_MULTIPLEX_COUNT - 1) {
            incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(0));
            incorrectConnectionString = incorrectConnectionString.replace(testInstance.deviceIdentityArray.get(0).getDeviceId(), testInstance.deviceIdentityArray.get(i).getDeviceId());
        } else {
            incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(i + 1));
            incorrectConnectionString = incorrectConnectionString.replace(testInstance.deviceIdentityArray.get(i + 1).getDeviceId(), testInstance.deviceIdentityArray.get(i).getDeviceId());
        }
        DeviceClient clientWithIncorrectCredentials = new DeviceClient(incorrectConnectionString, testInstance.protocol);
        clientsWithIncorrectCredentials.add(clientWithIncorrectCredentials);
    }
    boolean expectedExceptionThrown = false;
    try {
        testInstance.multiplexingClient.registerDeviceClients(clientsWithIncorrectCredentials);
    } catch (MultiplexingClientDeviceRegistrationAuthenticationException e) {
        Map<String, Exception> registrationExceptions = e.getRegistrationExceptions();
        assertEquals(DEVICE_MULTIPLEX_COUNT, registrationExceptions.size());
        for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
            String deviceId = testInstance.deviceIdentityArray.get(i).getDeviceId();
            assertTrue(registrationExceptions.containsKey(deviceId));
            assertTrue(registrationExceptions.get(deviceId) instanceof UnauthorizedException);
        }
        expectedExceptionThrown = true;
    }
    testInstance.multiplexingClient.close();
    assertTrue("Expected exception was not thrown", expectedExceptionThrown);
}
Also used : DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) MultiplexingClientDeviceRegistrationAuthenticationException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException) ArrayList(java.util.ArrayList) UnauthorizedException(com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Map(java.util.Map) HashMap(java.util.HashMap) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 4 with MultiplexingClientDeviceRegistrationAuthenticationException

use of com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method disableDeviceAfterOpenBeforeRegister.

// If you register a disabled device to an active multiplexed connection, the other devices on the connection
// should not be affected nor should the multiplexed connection itself.
@ContinuousIntegrationTest
@Test
public void disableDeviceAfterOpenBeforeRegister() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    // Only register the soon-to-be-disabled device after opening the multiplexing client
    testInstance.multiplexingClient.unregisterDeviceClient(testInstance.deviceClientArray.get(0));
    ConnectionStatusChangeTracker multiplexedConnectionStatusChangeTracker = new ConnectionStatusChangeTracker();
    testInstance.multiplexingClient.registerConnectionStatusChangeCallback(multiplexedConnectionStatusChangeTracker, null);
    ConnectionStatusChangeTracker[] connectionStatusChangeTrackers = new ConnectionStatusChangeTracker[DEVICE_MULTIPLEX_COUNT];
    for (int i = 0; i < DEVICE_MULTIPLEX_COUNT; i++) {
        connectionStatusChangeTrackers[i] = new ConnectionStatusChangeTracker();
        testInstance.deviceClientArray.get(i).registerConnectionStatusChangeCallback(connectionStatusChangeTrackers[i], null);
    }
    testInstance.multiplexingClient.open();
    // Disable a device that will be on the multiplexed connection
    Device deviceToDisable = registryManager.getDevice(testInstance.deviceIdentityArray.get(0).getDeviceId());
    deviceToDisable.setStatus(DeviceStatus.Disabled);
    registryManager.updateDevice(deviceToDisable);
    try {
        try {
            testInstance.multiplexingClient.registerDeviceClient(testInstance.deviceClientArray.get(0));
            fail("Registering a disabled device to an active multiplexing connection should have thrown an exception");
        } catch (MultiplexingClientDeviceRegistrationAuthenticationException ex) {
            assertTrue(ex.getRegistrationExceptions().containsKey(deviceToDisable.getDeviceId()));
        }
        // verify that the disabled device eventually loses its device session
        long startTime = System.currentTimeMillis();
        while (!connectionStatusChangeTrackers[0].wentDisconnectedRetrying) {
            Thread.sleep(200);
            if (System.currentTimeMillis() - startTime > FAULT_INJECTION_TIMEOUT_MILLIS) {
                fail("Timed out waiting for the disabled device's client to report DISCONNECTED_RETRYING");
            }
        }
        assertFalse("Device failed to be registered, but the multiplexing client still reports it as registered", testInstance.multiplexingClient.isDeviceRegistered(deviceToDisable.getDeviceId()));
        for (int i = 1; i < DEVICE_MULTIPLEX_COUNT; i++) {
            assertTrue("One device failed to be registered, but the other devices should still have been registered.", testInstance.multiplexingClient.isDeviceRegistered(testInstance.deviceClientArray.get(i).getConfig().getDeviceId()));
        }
        // Verify that the other devices on the multiplexed connection were unaffected
        for (int i = 1; i < DEVICE_MULTIPLEX_COUNT; i++) {
            assertFalse(connectionStatusChangeTrackers[i].wentDisconnectedRetrying);
            assertTrue(connectionStatusChangeTrackers[i].isOpen);
        }
        // Verify that the multiplexed connection itself was unaffected
        assertFalse(multiplexedConnectionStatusChangeTracker.wentDisconnectedRetrying);
        assertTrue(multiplexedConnectionStatusChangeTracker.isOpen);
        // Verify that the other devices can still send telemetry
        testSendingMessagesFromMultiplexedClients(testInstance.deviceClientArray.subList(1, DEVICE_MULTIPLEX_COUNT));
        testInstance.multiplexingClient.close();
    } finally {
        // re enable the device in case it gets recycled
        deviceToDisable.setStatus(DeviceStatus.Enabled);
        registryManager.updateDevice(deviceToDisable);
    }
}
Also used : Device(com.microsoft.azure.sdk.iot.service.Device) DeviceTwinDevice(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice) MultiplexingClientDeviceRegistrationAuthenticationException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Example 5 with MultiplexingClientDeviceRegistrationAuthenticationException

use of com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException in project azure-iot-sdk-java by Azure.

the class MultiplexingClientTests method registerDeviceWithIncorrectCredentialsBeforeOpenThrowsOnOpen.

// Before opening the multiplexed connection, register a single device with incorrect credentials. Opening the client
// should throw and the thrown exception should have details on why the open failed
@ContinuousIntegrationTest
@Test
public void registerDeviceWithIncorrectCredentialsBeforeOpenThrowsOnOpen() throws Exception {
    testInstance.setup(DEVICE_MULTIPLEX_COUNT);
    testInstance.multiplexingClient.unregisterDeviceClient(testInstance.deviceClientArray.get(0));
    // Get a valid connection string, but swap out the deviceId for a deviceId that does exist, but whose symmetric key is different
    String incorrectConnectionString = registryManager.getDeviceConnectionString(testInstance.deviceIdentityArray.get(1)).replace(testInstance.deviceIdentityArray.get(1).getDeviceId(), testInstance.deviceIdentityArray.get(0).getDeviceId());
    DeviceClient clientWithIncorrectCredentials = new DeviceClient(incorrectConnectionString, testInstance.protocol);
    testInstance.multiplexingClient.registerDeviceClient(clientWithIncorrectCredentials);
    boolean expectedExceptionThrown = false;
    try {
        testInstance.multiplexingClient.open();
    } catch (MultiplexingClientDeviceRegistrationAuthenticationException e) {
        Map<String, Exception> registrationExceptions = e.getRegistrationExceptions();
        assertEquals(1, registrationExceptions.size());
        String deviceId = testInstance.deviceIdentityArray.get(0).getDeviceId();
        assertTrue(registrationExceptions.containsKey(deviceId));
        assertTrue(registrationExceptions.get(deviceId) instanceof UnauthorizedException);
        expectedExceptionThrown = true;
    }
    testInstance.multiplexingClient.close();
    assertTrue("Expected exception was not thrown", expectedExceptionThrown);
}
Also used : DeviceClient(com.microsoft.azure.sdk.iot.device.DeviceClient) MultiplexingClientDeviceRegistrationAuthenticationException(com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException) UnauthorizedException(com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) Map(java.util.Map) HashMap(java.util.HashMap) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) Test(org.junit.Test)

Aggregations

MultiplexingClientDeviceRegistrationAuthenticationException (com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientDeviceRegistrationAuthenticationException)7 Test (org.junit.Test)6 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)6 DeviceClient (com.microsoft.azure.sdk.iot.device.DeviceClient)5 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)5 UnauthorizedException (com.microsoft.azure.sdk.iot.device.exceptions.UnauthorizedException)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ArrayList (java.util.ArrayList)2 MultiplexingClientException (com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingClientException)1 MultiplexingDeviceUnauthorizedException (com.microsoft.azure.sdk.iot.device.exceptions.MultiplexingDeviceUnauthorizedException)1 TransportException (com.microsoft.azure.sdk.iot.device.exceptions.TransportException)1 Device (com.microsoft.azure.sdk.iot.service.Device)1 DeviceTwinDevice (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinDevice)1 TestDeviceIdentity (tests.integration.com.microsoft.azure.sdk.iot.helpers.TestDeviceIdentity)1