use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.
the class DeviceMethodCommon method buildDeviceMethodClientWithAzureSasCredential.
protected static DeviceMethod buildDeviceMethodClientWithAzureSasCredential() {
IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
IotHubServiceSasToken serviceSasToken = new IotHubServiceSasToken(iotHubConnectionStringObj);
AzureSasCredential azureSasCredential = new AzureSasCredential(serviceSasToken.toString());
DeviceMethodClientOptions options = DeviceMethodClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build();
return new DeviceMethod(iotHubConnectionStringObj.getHostName(), azureSasCredential, options);
}
use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.
the class GetTwinTests method testGetDeviceTwinWithProxy.
@Test
@StandardTierHubOnlyTest
public void testGetDeviceTwinWithProxy() throws IOException, InterruptedException, IotHubException, GeneralSecurityException, ModuleClientException, URISyntaxException {
if (testInstance.protocol != IotHubClientProtocol.MQTT || testInstance.authenticationType != AuthenticationType.SAS || testInstance.clientType != ClientType.DEVICE_CLIENT) {
// when the device is using MQTT with SAS auth
return;
}
super.setUpNewDeviceAndModule();
String testProxyHostname = "127.0.0.1";
int testProxyPort = 8892;
HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap().withPort(testProxyPort).start();
try {
Proxy serviceSideProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(testProxyHostname, testProxyPort));
ProxyOptions proxyOptions = new ProxyOptions(serviceSideProxy);
DeviceTwinClientOptions options = DeviceTwinClientOptions.builder().proxyOptions(proxyOptions).build();
testInstance.twinServiceClient = DeviceTwin.createFromConnectionString(iotHubConnectionString, options);
super.testGetDeviceTwin();
} finally {
proxyServer.stop();
}
}
use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString 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();
}
use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.
the class QueryTwinTests method queryCollectionCanReturnEmptyQueryResults.
@Test
@StandardTierHubOnlyTest
@ContinuousIntegrationTest
public void queryCollectionCanReturnEmptyQueryResults() throws IOException, IotHubException {
String fullQuery = "select * from devices where deviceId='nonexistantdevice'";
DeviceTwin twinClient = new DeviceTwin(iotHubConnectionString, DeviceTwinClientOptions.builder().httpReadTimeout(HTTP_READ_TIMEOUT).build());
QueryCollection twinQuery = twinClient.queryTwinCollection(fullQuery);
QueryOptions options = new QueryOptions();
QueryCollectionResponse<DeviceTwinDevice> response = twinClient.next(twinQuery, options);
assertNull(response.getContinuationToken());
assertTrue(response.getCollection().isEmpty());
}
use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.
the class ProvisioningTests method customAllocationFlow.
/**
* This test flow uses a custom allocation policy to decide which of the two hubs a device should be provisioned to.
* The custom allocation policy has a webhook to an Azure function, and that function will always dictate to provision
* the device to the hub with the longest host name. This test verifies that an enrollment with a custom allocation policy
* pointing to that Azure function will always enroll to the hub with the longest name
* @param enrollmentType The type of the enrollment to test
* @throws Exception if an exception occurs during provisioning or while creating the security provider
*/
protected void customAllocationFlow(EnrollmentType enrollmentType) throws Exception {
if (enrollmentType == EnrollmentType.GROUP && testInstance.attestationType != AttestationType.SYMMETRIC_KEY) {
// tpm doesn't support group, and x509 group test has not been implemented yet
return;
}
List<String> possibleStartingHubHostNames = new ArrayList<>();
String farAwayIotHubHostname = IotHubConnectionString.createConnectionString(farAwayIotHubConnectionString).getHostName();
String iothubHostName = IotHubConnectionString.createConnectionString(iotHubConnectionString).getHostName();
possibleStartingHubHostNames.add(farAwayIotHubHostname);
possibleStartingHubHostNames.add(iothubHostName);
String expectedHubToProvisionTo;
if (farAwayIotHubHostname.length() > iothubHostName.length()) {
expectedHubToProvisionTo = farAwayIotHubHostname;
} else if (iothubHostName.length() > farAwayIotHubHostname.length()) {
expectedHubToProvisionTo = iothubHostName;
} else {
throw new IllegalArgumentException("Both possible hub's cannot have a host name of the same length for this test to work");
}
CustomAllocationDefinition customAllocationDefinition = new CustomAllocationDefinition();
customAllocationDefinition.setApiVersion(CUSTOM_ALLOCATION_WEBHOOK_API_VERSION);
customAllocationDefinition.setWebhookUrl(customAllocationWebhookUrl);
testInstance.securityProvider = getSecurityProviderInstance(enrollmentType, AllocationPolicy.CUSTOM, null, customAllocationDefinition, possibleStartingHubHostNames);
registerDevice(testInstance.protocol, testInstance.securityProvider, provisioningServiceGlobalEndpoint, true, null, expectedHubToProvisionTo, null);
}
Aggregations