Search in sources :

Example 6 with AuthenticationMechanism

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);
}
Also used : AuthenticationMechanism(com.microsoft.azure.sdk.iot.service.auth.AuthenticationMechanism) SymmetricKey(com.microsoft.azure.sdk.iot.service.auth.SymmetricKey) BaseDevice(com.microsoft.azure.sdk.iot.service.BaseDevice) Test(org.junit.Test)

Example 7 with AuthenticationMechanism

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);
}
Also used : AuthenticationMechanism(com.microsoft.azure.sdk.iot.service.auth.AuthenticationMechanism)

Example 8 with AuthenticationMechanism

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);
}
Also used : AuthenticationMechanism(com.microsoft.azure.sdk.iot.service.auth.AuthenticationMechanism)

Example 9 with AuthenticationMechanism

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();
}
Also used : AuthenticationMechanism(com.microsoft.azure.sdk.iot.service.auth.AuthenticationMechanism) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CloudStorageAccount(com.microsoft.azure.storage.CloudStorageAccount) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 10 with AuthenticationMechanism

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());
}
Also used : AuthenticationMechanism(com.microsoft.azure.sdk.iot.service.auth.AuthenticationMechanism) Test(org.junit.Test)

Aggregations

AuthenticationMechanism (com.microsoft.azure.sdk.iot.service.auth.AuthenticationMechanism)29 Test (org.junit.Test)25 ExportImportDevice (com.microsoft.azure.sdk.iot.service.ExportImportDevice)8 SymmetricKey (com.microsoft.azure.sdk.iot.service.auth.SymmetricKey)5 BaseDevice (com.microsoft.azure.sdk.iot.service.BaseDevice)4 X509Thumbprint (com.microsoft.azure.sdk.iot.service.auth.X509Thumbprint)3 TwinCollection (com.microsoft.azure.sdk.iot.deps.twin.TwinCollection)1 Device (com.microsoft.azure.sdk.iot.service.Device)1 DeviceStatus (com.microsoft.azure.sdk.iot.service.DeviceStatus)1 ImportMode (com.microsoft.azure.sdk.iot.service.ImportMode)1 CloudStorageAccount (com.microsoft.azure.storage.CloudStorageAccount)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1