use of com.microsoft.azure.sdk.iot.service.auth.AuthenticationMechanism in project azure-iot-sdk-java by Azure.
the class BaseDeviceTest method toParserIllegalStateThrownWhenUsingSASAuthenticationWithoutPrimaryKeySaved.
// Tests_SRS_SERVICE_SDK_JAVA_BASEDEVICE_34_019: [If this device uses sas authentication, but does not have a primary and secondary symmetric key saved, an IllegalStateException shall be thrown.]
@Test(expected = IllegalStateException.class)
public void toParserIllegalStateThrownWhenUsingSASAuthenticationWithoutPrimaryKeySaved() {
// arrange
BaseDevice device = Deencapsulation.newInstance(BaseDevice.class, new Class[] { String.class, AuthenticationType.class }, "someDevice", AuthenticationType.SAS);
SymmetricKey symmetricKey = new SymmetricKey();
Deencapsulation.setField(symmetricKey, "primaryKey", null);
AuthenticationMechanism authenticationMechanism = new AuthenticationMechanism(symmetricKey);
Deencapsulation.setField(device, "authentication", authenticationMechanism);
// act
reflectivelyInvokeToDeviceParser(device);
}
use of com.microsoft.azure.sdk.iot.service.auth.AuthenticationMechanism in project azure-iot-sdk-java by Azure.
the class BaseDevice method setThumbprint.
/**
* Setter for X509 thumbprint
* @param primaryThumbprint the primary thumbprint to set
* @param secondaryThumbprint the secondary thumbprint to set
*
* @deprecated as of service-client version 1.15.1, please use {@link #setThumbprintFinal(String, String)}
*
* @throws IllegalArgumentException if primaryThumbprint or secondaryThumbprint is null or empty
*/
@Deprecated
public void setThumbprint(String primaryThumbprint, String secondaryThumbprint) {
if (Tools.isNullOrEmpty(primaryThumbprint) || Tools.isNullOrEmpty(secondaryThumbprint)) {
throw new IllegalArgumentException("Thumbprint may not be null or empty");
}
if (this.authentication == null) {
this.authentication = new AuthenticationMechanism(AuthenticationType.SELF_SIGNED);
}
this.authentication.setPrimaryThumbprint(primaryThumbprint);
this.authentication.setSecondaryThumbprint(secondaryThumbprint);
}
use of com.microsoft.azure.sdk.iot.service.auth.AuthenticationMechanism in project azure-iot-sdk-java by Azure.
the class BaseDevice method setThumbprintFinal.
/**
* Setter for X509 thumbprint
* @param primaryThumbprint the primary thumbprint to set
* @param secondaryThumbprint the secondary thumbprint to set
* @throws IllegalArgumentException if primaryThumbprint or secondaryThumbprint is null or empty
*/
public final void setThumbprintFinal(String primaryThumbprint, String secondaryThumbprint) {
if (Tools.isNullOrEmpty(primaryThumbprint) || Tools.isNullOrEmpty(secondaryThumbprint)) {
throw new IllegalArgumentException("Thumbprint may not be null or empty");
}
if (this.authentication == null) {
this.authentication = new AuthenticationMechanism(AuthenticationType.SELF_SIGNED);
}
this.authentication.setPrimaryThumbprint(primaryThumbprint);
this.authentication.setSecondaryThumbprint(secondaryThumbprint);
}
use of com.microsoft.azure.sdk.iot.service.auth.AuthenticationMechanism in project azure-iot-sdk-java by Azure.
the class DeviceManagerImportSample method main.
public static void main(String[] args) throws Exception {
System.out.println("Starting import sample...");
// Creating Azure storage container and getting its URI
CloudStorageAccount storageAccount = CloudStorageAccount.parse(SampleUtils.storageConnectionString);
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference(DeviceManagerImportSample.importContainerName);
container.createIfNotExists();
String containerSasUri = SampleUtils.getContainerSasUri(container);
// Creating the list of devices to be submitted for import
StringBuilder devicesToImport = new StringBuilder();
for (int i = 0; i < 1; i++) {
String deviceId = UUID.randomUUID().toString();
Device device = Device.createFromId(deviceId, null, null);
AuthenticationMechanism authentication = new AuthenticationMechanism(device.getSymmetricKey());
ExportImportDevice deviceToAdd = new ExportImportDevice();
deviceToAdd.setId(deviceId);
deviceToAdd.setAuthentication(authentication);
deviceToAdd.setStatus(DeviceStatus.Enabled);
deviceToAdd.setImportMode(ImportMode.CreateOrUpdate);
devicesToImport.append(gson.toJson(deviceToAdd));
if (i < numberOfDevice - 1) {
devicesToImport.append("\r\n");
}
}
byte[] blobToImport = devicesToImport.toString().getBytes(StandardCharsets.UTF_8);
// Creating the Azure storage blob and uploading the serialized string of devices
System.out.println("Uploading " + blobToImport.length + " bytes into Azure storage.");
InputStream stream = new ByteArrayInputStream(blobToImport);
CloudBlockBlob importBlob = container.getBlockBlobReference(DeviceManagerImportSample.importBlobName);
importBlob.deleteIfExists();
importBlob.upload(stream, blobToImport.length);
// Starting the import job
RegistryManager registryManager = RegistryManager.createFromConnectionString(SampleUtils.iotHubConnectionString);
JobProperties importJob = registryManager.importDevices(containerSasUri, containerSasUri);
// Waiting for the import job to complete
while (true) {
importJob = registryManager.getJob(importJob.getJobId());
if (importJob.getStatus() == JobProperties.JobStatus.COMPLETED || importJob.getStatus() == JobProperties.JobStatus.FAILED) {
break;
}
Thread.sleep(500);
}
// Checking the result of the import job
if (importJob.getStatus() == JobProperties.JobStatus.COMPLETED) {
System.out.println("Import job completed. The new devices are now added to the hub.");
} else {
System.out.println("Import job failed. Failure reason: " + importJob.getFailureReason());
}
// Cleaning up the blob
for (ListBlobItem blobItem : container.listBlobs()) {
if (blobItem instanceof CloudBlob) {
CloudBlob blob = (CloudBlockBlob) blobItem;
blob.deleteIfExists();
}
}
registryManager.close();
}
use of com.microsoft.azure.sdk.iot.service.auth.AuthenticationMechanism in project azure-iot-sdk-java by Azure.
the class AuthenticationMechanismTest method symmetricKeyPropertyWorks.
// Tests_SRS_AUTHENTICATION_MECHANISM_34_005: [This function shall return this object's symmetric key.]
// Tests_SRS_AUTHENTICATION_MECHANISM_34_007: [This function shall set this object's symmetric key to the provided value.]
@Test
public void symmetricKeyPropertyWorks() {
// arrange
AuthenticationMechanism actualAuthentication = new AuthenticationMechanism(AuthenticationType.CERTIFICATE_AUTHORITY);
// act
actualAuthentication.setSymmetricKey(expectedSymmetricKey);
// assert
assertEquals(expectedSymmetricKey, actualAuthentication.getSymmetricKey());
assertEquals(AuthenticationType.SAS, actualAuthentication.getAuthenticationType());
}
Aggregations