use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.
the class Tools method buildDeviceMethodClientWithTokenCredential.
public static com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod buildDeviceMethodClientWithTokenCredential() {
IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
TokenCredential tokenCredential = buildTokenCredentialFromEnvironment();
DeviceMethodClientOptions options = DeviceMethodClientOptions.builder().build();
return new com.microsoft.azure.sdk.iot.service.devicetwin.DeviceMethod(iotHubConnectionStringObj.getHostName(), tokenCredential, options);
}
use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.
the class Tools method buildDigitalTwinClientWithTokenCredential.
public static DigitalTwinClient buildDigitalTwinClientWithTokenCredential() {
IotHubConnectionString iotHubConnectionStringObj = IotHubConnectionStringBuilder.createIotHubConnectionString(iotHubConnectionString);
TokenCredential tokenCredential = buildTokenCredentialFromEnvironment();
DigitalTwinClientOptions options = DigitalTwinClientOptions.builder().build();
return new DigitalTwinClient(iotHubConnectionStringObj.getHostName(), tokenCredential, options);
}
use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString 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);
}
use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString 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();
}
use of com.microsoft.azure.sdk.iot.service.IotHubConnectionString in project azure-iot-sdk-java by Azure.
the class DeviceMethodTests method invokeMethodWithServiceSideProxy.
@Test
@StandardTierHubOnlyTest
public void invokeMethodWithServiceSideProxy() throws Exception {
if (testInstance.protocol != IotHubClientProtocol.MQTT || testInstance.authenticationType != AuthenticationType.SAS || testInstance.clientType != ClientType.DEVICE_CLIENT) {
// when the device is using MQTT with SAS auth
return;
}
String testProxyHostname = "127.0.0.1";
int testProxyPort = 8894;
HttpProxyServer proxyServer = DefaultHttpProxyServer.bootstrap().withPort(testProxyPort).start();
try {
Proxy serviceSideProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(testProxyHostname, testProxyPort));
ProxyOptions proxyOptions = new ProxyOptions(serviceSideProxy);
DeviceMethodClientOptions options = DeviceMethodClientOptions.builder().proxyOptions(proxyOptions).httpReadTimeout(HTTP_READ_TIMEOUT).build();
this.testInstance.methodServiceClient = DeviceMethod.createFromConnectionString(iotHubConnectionString, options);
super.openDeviceClientAndSubscribeToMethods();
super.invokeMethodSucceed();
} finally {
proxyServer.stop();
}
}
Aggregations