Search in sources :

Example 6 with AzureSasCredential

use of com.azure.core.credential.AzureSasCredential in project azure-iot-sdk-java by Azure.

the class QueryTwinTests method rawQueryTokenRenewalWithAzureSasCredential.

@Test
@StandardTierHubOnlyTest
public void rawQueryTokenRenewalWithAzureSasCredential() throws IOException, InterruptedException, IotHubException, GeneralSecurityException, URISyntaxException, ModuleClientException {
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    AzureSasCredential sasCredential = new AzureSasCredential(serviceSasToken.toString());
    testInstance.twinServiceClient = new DeviceTwin(iotHubConnectionStringObj.getHostName(), sasCredential);
    // test that the service client can query before the shared access signature expires
    testRawQueryTwin();
    // deliberately expire the SAS token to provoke a 401 to ensure that the registry manager is using the shared
    // access signature that is set here.
    sasCredential.update(SasTokenTools.makeSasTokenExpired(serviceSasToken.toString()));
    try {
        testRawQueryTwin();
        fail("Expected query call to throw unauthorized exception since an expired SAS token was used, but no exception was thrown");
    } catch (IotHubUnathorizedException e) {
        log.debug("IotHubUnauthorizedException was thrown as expected, continuing test");
    }
    // Renew the expired shared access signature
    serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    sasCredential.update(serviceSasToken.toString());
    // test that the service client can query after renewing the shared access signature expires
    testRawQueryTwin();
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) AzureSasCredential(com.azure.core.credential.AzureSasCredential) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IotHubUnathorizedException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test)

Example 7 with AzureSasCredential

use of com.azure.core.credential.AzureSasCredential in project azure-iot-sdk-java by Azure.

the class DigitalTwinClientTests method digitalTwinClientTokenRenewalWithAzureSasCredential.

@Test
@StandardTierHubOnlyTest
public void digitalTwinClientTokenRenewalWithAzureSasCredential() {
    if (protocol != MQTT) {
        // This test is for the service client, so no need to rerun it for all the different device protocols
        return;
    }
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(IOTHUB_CONNECTION_STRING);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    AzureSasCredential sasCredential = new AzureSasCredential(serviceSasToken.toString());
    digitalTwinClient = new DigitalTwinClient(iotHubConnectionStringObj.getHostName(), sasCredential);
    // get a digital twin with a valid SAS token in the AzureSasCredential instance
    // don't care about the return value, just checking that the request isn't unauthorized
    digitalTwinClient.getDigitalTwin(deviceId, BasicDigitalTwin.class);
    // deliberately expire the SAS token to provoke a 401 to ensure that the digital twin client is using the shared
    // access signature that is set here.
    sasCredential.update(SasTokenTools.makeSasTokenExpired(serviceSasToken.toString()));
    try {
        digitalTwinClient.getDigitalTwin(deviceId, BasicDigitalTwin.class);
        fail("Expected get digital twin call to throw unauthorized exception since an expired SAS token was used, but no exception was thrown");
    } catch (RestException e) {
        if (e.response().code() == 401) {
            log.debug("IotHubUnauthorizedException was thrown as expected, continuing test");
        } else {
            throw e;
        }
    }
    // Renew the expired shared access signature
    serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    sasCredential.update(serviceSasToken.toString());
    // get a digital twin using the renewed shared access signature
    // don't care about the return value, just checking that the request isn't unauthorized
    digitalTwinClient.getDigitalTwin(deviceId, BasicDigitalTwin.class);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) AzureSasCredential(com.azure.core.credential.AzureSasCredential) RestException(com.microsoft.rest.RestException) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DigitalTwinClient(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) DigitalTwinTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.DigitalTwinTest) IntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test)

Example 8 with AzureSasCredential

use of com.azure.core.credential.AzureSasCredential in project azure-iot-sdk-java by Azure.

the class DeviceMethodTests method serviceClientTokenRenewalWithAzureSasCredential.

@Test
@StandardTierHubOnlyTest
public void serviceClientTokenRenewalWithAzureSasCredential() throws Exception {
    if (testInstance.protocol != IotHubClientProtocol.AMQPS || testInstance.clientType != ClientType.DEVICE_CLIENT || testInstance.authenticationType != AuthenticationType.SAS) {
        // This test is for the service client, so no need to rerun it for all the different client types or device protocols
        return;
    }
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    AzureSasCredential sasCredential = new AzureSasCredential(serviceSasToken.toString());
    this.testInstance.methodServiceClient = new DeviceMethod(iotHubConnectionStringObj.getHostName(), sasCredential, DeviceMethodClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
    super.openDeviceClientAndSubscribeToMethods();
    // add first device just to make sure that the first credential update worked
    super.invokeMethodSucceed();
    // deliberately expire the SAS token to provoke a 401 to ensure that the method client is using the shared
    // access signature that is set here.
    sasCredential.update(SasTokenTools.makeSasTokenExpired(serviceSasToken.toString()));
    try {
        super.invokeMethodSucceed();
        fail("Expected invoke method call to throw unauthorized exception since an expired SAS token was used, but no exception was thrown");
    } catch (IotHubUnathorizedException e) {
        log.debug("IotHubUnauthorizedException was thrown as expected, continuing test");
    }
    // Renew the expired shared access signature
    serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    sasCredential.update(serviceSasToken.toString());
    // final method invocation should succeed since the shared access signature has been renewed
    super.invokeMethodSucceed();
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) AzureSasCredential(com.azure.core.credential.AzureSasCredential) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IotHubUnathorizedException(com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException) DeviceMethod(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) ContinuousIntegrationTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest) IotHubTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest) StandardTierHubOnlyTest(tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest) Test(org.junit.Test)

Example 9 with AzureSasCredential

use of com.azure.core.credential.AzureSasCredential in project azure-iot-sdk-java by Azure.

the class DigitalTwinClientTests method buildDigitalTwinClientWithAzureSasCredential.

private static DigitalTwinClient buildDigitalTwinClientWithAzureSasCredential() {
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(IOTHUB_CONNECTION_STRING);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    AzureSasCredential azureSasCredential = new AzureSasCredential(serviceSasToken.toString());
    DigitalTwinClientOptions options = DigitalTwinClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build();
    return new DigitalTwinClient(iotHubConnectionStringObj.getHostName(), azureSasCredential, options);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) AzureSasCredential(com.azure.core.credential.AzureSasCredential) DigitalTwinClientOptions(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClientOptions) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DigitalTwinClient(com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient)

Example 10 with AzureSasCredential

use of com.azure.core.credential.AzureSasCredential in project azure-iot-sdk-java by Azure.

the class DeviceTwinCommon method buildDeviceTwinClientWithAzureSasCredential.

protected static DeviceTwin buildDeviceTwinClientWithAzureSasCredential() {
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    AzureSasCredential azureSasCredential = new AzureSasCredential(serviceSasToken.toString());
    DeviceTwinClientOptions options = DeviceTwinClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build();
    return new DeviceTwin(iotHubConnectionStringObj.getHostName(), azureSasCredential, options);
}
Also used : IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) AzureSasCredential(com.azure.core.credential.AzureSasCredential) DeviceTwinClientOptions(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwinClientOptions) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) DeviceTwin(com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)

Aggregations

AzureSasCredential (com.azure.core.credential.AzureSasCredential)17 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)16 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)16 Test (org.junit.Test)10 IotHubTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.IotHubTest)9 StandardTierHubOnlyTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.StandardTierHubOnlyTest)9 IntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.IntegrationTest)8 ContinuousIntegrationTest (tests.integration.com.microsoft.azure.sdk.iot.helpers.annotations.ContinuousIntegrationTest)8 IotHubUnathorizedException (com.microsoft.azure.sdk.iot.service.exceptions.IotHubUnathorizedException)6 RegistryManager (com.microsoft.azure.sdk.iot.service.RegistryManager)5 ServiceClient (com.microsoft.azure.sdk.iot.service.ServiceClient)4 DeviceTwin (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceTwin)4 Device (com.microsoft.azure.sdk.iot.service.Device)2 DeviceMethod (com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod)2 DigitalTwinClient (com.microsoft.azure.sdk.iot.service.digitaltwin.DigitalTwinClient)2 JobClient (com.microsoft.azure.sdk.iot.service.jobs.JobClient)2 FeedbackReceiver (com.microsoft.azure.sdk.iot.service.FeedbackReceiver)1 FileUploadNotificationReceiver (com.microsoft.azure.sdk.iot.service.FileUploadNotificationReceiver)1 Message (com.microsoft.azure.sdk.iot.service.Message)1 RegistryManagerOptions (com.microsoft.azure.sdk.iot.service.RegistryManagerOptions)1