Search in sources :

Example 1 with HttpsHsmTrustBundleProvider

use of com.microsoft.azure.sdk.iot.device.edge.HttpsHsmTrustBundleProvider in project azure-iot-sdk-java by Azure.

the class ModuleClient method createFromEnvironment.

/**
 * Create a module client instance from your environment variables
 * @param protocol the protocol the module client instance will use
 * @param clientOptions The options that allow configuration of the module client instance during initialization
 * @return the created module client instance
 * @throws ModuleClientException if the module client cannot be created
 */
public static ModuleClient createFromEnvironment(IotHubClientProtocol protocol, ClientOptions clientOptions) throws ModuleClientException {
    log.info("Creating module client from environment with protocol {}...", protocol);
    Map<String, String> envVariables = System.getenv();
    // Codes_SRS_MODULECLIENT_34_013: [This function shall check for a saved edgehub connection string.]
    log.debug("Checking for an edgehub connection string...");
    String connectionString = envVariables.get(EdgehubConnectionstringVariableName);
    if (connectionString == null) {
        log.debug("No edgehub connection string was configured, checking for an IoThub connection string...");
        // Codes_SRS_MODULECLIENT_34_019: [If no edgehub connection string is present, this function shall check for a saved iothub connection string.]
        connectionString = envVariables.get(IothubConnectionstringVariableName);
    }
    // First try to create from connection string and if env variable for connection string is not found try to create from edgedUri
    if (connectionString != null) {
        log.debug("Creating module client with the provided connection string");
        // Codes_SRS_MODULECLIENT_34_020: [If an edgehub or iothub connection string is present, this function shall create a module client instance using that connection string and the provided protocol.]
        ModuleClient moduleClient;
        try {
            moduleClient = new ModuleClient(connectionString, protocol, clientOptions);
        } catch (URISyntaxException e) {
            throw new ModuleClientException("Could not create module client", e);
        }
        // Check for a different default cert to be used
        String alternativeDefaultTrustedCert = envVariables.get(EdgeCaCertificateFileVariableName);
        if (alternativeDefaultTrustedCert != null && !alternativeDefaultTrustedCert.isEmpty()) {
            log.debug("Configuring module client to use the configured alternative trusted certificate");
            // Codes_SRS_MODULECLIENT_34_031: [If an alternative default trusted cert is saved in the environment
            // variables, this function shall set that trusted cert in the created module client.]
            moduleClient.setOption_SetCertificatePath(alternativeDefaultTrustedCert);
        }
        return moduleClient;
    } else {
        log.info("No connection string was configured for this module, so it will get its credentials from the edgelet");
        // Codes_SRS_MODULECLIENT_34_014: [This function shall check for environment variables for edgedUri, deviceId, moduleId,
        // hostname, authScheme, gatewayHostname, and generationId. If any of these other than gatewayHostname is missing,
        // this function shall throw a ModuleClientException.]
        String edgedUri = envVariables.get(IotEdgedUriVariableName);
        String deviceId = envVariables.get(DeviceIdVariableName);
        String moduleId = envVariables.get(ModuleIdVariableName);
        String hostname = envVariables.get(IotHubHostnameVariableName);
        String authScheme = envVariables.get(AuthSchemeVariableName);
        String gatewayHostname = envVariables.get(GatewayHostnameVariableName);
        String generationId = envVariables.get(ModuleGenerationIdVariableName);
        if (edgedUri == null) {
            throw new ModuleClientException("Environment variable " + IotEdgedUriVariableName + " is required.");
        }
        if (deviceId == null) {
            throw new ModuleClientException("Environment variable " + DeviceIdVariableName + " is required.");
        }
        if (moduleId == null) {
            throw new ModuleClientException("Environment variable " + ModuleIdVariableName + " is required.");
        }
        if (hostname == null) {
            throw new ModuleClientException("Environment variable " + IotHubHostnameVariableName + " is required.");
        }
        if (authScheme == null) {
            throw new ModuleClientException("Environment variable " + AuthSchemeVariableName + " is required.");
        }
        if (generationId == null) {
            throw new ModuleClientException("Environment variable " + ModuleGenerationIdVariableName + " is required");
        }
        if (!authScheme.equalsIgnoreCase(SasTokenAuthScheme)) {
            // Codes_SRS_MODULECLIENT_34_030: [If the auth scheme environment variable is not "SasToken", this function shall throw a moduleClientException.]
            throw new ModuleClientException("Unsupported authentication scheme. Supported scheme is " + SasTokenAuthScheme + ".");
        }
        SignatureProvider signatureProvider;
        try {
            signatureProvider = new HttpHsmSignatureProvider(edgedUri, DEFAULT_API_VERSION);
        } catch (NoSuchAlgorithmException | URISyntaxException e) {
            throw new ModuleClientException("Could not use Hsm Signature Provider", e);
        }
        try {
            // Codes_SRS_MODULECLIENT_34_017: [This function shall create an authentication provider using the created
            // signature provider, and the environment variables for deviceid, moduleid, hostname, gatewayhostname,
            // and the default time for tokens to live and the default sas token buffer time.]
            IotHubAuthenticationProvider iotHubAuthenticationProvider = IotHubSasTokenHsmAuthenticationProvider.create(signatureProvider, deviceId, moduleId, hostname, gatewayHostname, generationId, DEFAULT_SAS_TOKEN_TIME_TO_LIVE_SECONDS, DEFAULT_SAS_TOKEN_BUFFER_PERCENTAGE);
            // Codes_SRS_MODULECLIENT_34_018: [This function shall return a new ModuleClient instance built from the created authentication provider and the provided protocol.]
            ModuleClient moduleClient = new ModuleClient(iotHubAuthenticationProvider, protocol, SEND_PERIOD_MILLIS, getReceivePeriod(protocol));
            if (gatewayHostname != null && !gatewayHostname.isEmpty()) {
                // Codes_SRS_MODULECLIENT_34_032: [This function shall retrieve the trust bundle from the hsm and set them in the module client.]
                TrustBundleProvider trustBundleProvider = new HttpsHsmTrustBundleProvider();
                String trustCertificates = trustBundleProvider.getTrustBundleCerts(edgedUri, DEFAULT_API_VERSION);
                moduleClient.setTrustedCertificates(trustCertificates);
            }
            return moduleClient;
        } catch (IOException | TransportException | HsmException | URISyntaxException e) {
            throw new ModuleClientException(e);
        }
    }
}
Also used : HttpsHsmTrustBundleProvider(com.microsoft.azure.sdk.iot.device.edge.HttpsHsmTrustBundleProvider) TrustBundleProvider(com.microsoft.azure.sdk.iot.device.edge.TrustBundleProvider) ModuleClientException(com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException) URISyntaxException(java.net.URISyntaxException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) TransportException(com.microsoft.azure.sdk.iot.device.exceptions.TransportException) SignatureProvider(com.microsoft.azure.sdk.iot.device.auth.SignatureProvider) HttpHsmSignatureProvider(com.microsoft.azure.sdk.iot.device.hsm.HttpHsmSignatureProvider) IotHubAuthenticationProvider(com.microsoft.azure.sdk.iot.device.auth.IotHubAuthenticationProvider) HsmException(com.microsoft.azure.sdk.iot.device.hsm.HsmException) HttpsHsmTrustBundleProvider(com.microsoft.azure.sdk.iot.device.edge.HttpsHsmTrustBundleProvider) HttpHsmSignatureProvider(com.microsoft.azure.sdk.iot.device.hsm.HttpHsmSignatureProvider)

Example 2 with HttpsHsmTrustBundleProvider

use of com.microsoft.azure.sdk.iot.device.edge.HttpsHsmTrustBundleProvider in project azure-iot-sdk-java by Azure.

the class HttpsHsmTrustBundleProviderTest method getTrustBundleCertsSuccess.

// Tests_SRS_TRUSTBUNDLEPROVIDER_34_001: [This function shall create an HttpsHsmClient using the provided provider uri.]
// Tests_SRS_TRUSTBUNDLEPROVIDER_34_002: [This function shall invoke getTrustBundle on the HttpsHsmClient and return the resulting certificates.]
@Test
public void getTrustBundleCertsSuccess() throws TransportException, IOException, HsmException, URISyntaxException {
    // arrange
    final String expectedCertificatesString = "some collection of certificates";
    HttpsHsmTrustBundleProvider provider = new HttpsHsmTrustBundleProvider();
    new NonStrictExpectations() {

        {
            mockedHttpsHsmClient.getTrustBundle(expectedAPIVersion);
            result = mockedTrustBundleResponse;
            mockedTrustBundleResponse.getCertificates();
            result = expectedCertificatesString;
        }
    };
    // act
    String actualCertificatesString = provider.getTrustBundleCerts(expectedUri, expectedAPIVersion);
    // assert
    assertEquals(expectedCertificatesString, actualCertificatesString);
}
Also used : HttpsHsmTrustBundleProvider(com.microsoft.azure.sdk.iot.device.edge.HttpsHsmTrustBundleProvider) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Aggregations

HttpsHsmTrustBundleProvider (com.microsoft.azure.sdk.iot.device.edge.HttpsHsmTrustBundleProvider)2 IotHubAuthenticationProvider (com.microsoft.azure.sdk.iot.device.auth.IotHubAuthenticationProvider)1 SignatureProvider (com.microsoft.azure.sdk.iot.device.auth.SignatureProvider)1 TrustBundleProvider (com.microsoft.azure.sdk.iot.device.edge.TrustBundleProvider)1 ModuleClientException (com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException)1 TransportException (com.microsoft.azure.sdk.iot.device.exceptions.TransportException)1 HsmException (com.microsoft.azure.sdk.iot.device.hsm.HsmException)1 HttpHsmSignatureProvider (com.microsoft.azure.sdk.iot.device.hsm.HttpHsmSignatureProvider)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NonStrictExpectations (mockit.NonStrictExpectations)1 Test (org.junit.Test)1