Search in sources :

Example 16 with AzureSasCredential

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

the class GetTwinTests 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;
    }
    super.setUpNewDeviceAndModule();
    IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
    IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
    String sasToken = serviceSasToken.toString();
    AzureSasCredential sasCredential = new AzureSasCredential(sasToken);
    this.testInstance.twinServiceClient = new DeviceTwin(iotHubConnectionStringObj.getHostName(), sasCredential, DeviceTwinClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
    // add first device just to make sure that the first credential update worked
    super.testGetDeviceTwin();
    // deliberately expire the SAS token to provoke a 401 to ensure that the twin client is using the shared
    // access signature that is set here.
    sasCredential.update(SasTokenTools.makeSasTokenExpired(sasToken));
    try {
        super.testGetDeviceTwin();
        fail("Expected get twin 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());
    // adding third device should succeed since the shared access signature has been renewed
    super.testGetDeviceTwin();
}
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) 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) 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 17 with AzureSasCredential

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

the class QueryTwinTests method testRawQueryTwinWithAzureSasCredential.

@Test
@StandardTierHubOnlyTest
public void testRawQueryTwinWithAzureSasCredential() 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);
    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) 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)

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