use of com.microsoft.azure.sdk.iot.device.hsm.HttpHsmSignatureProvider in project azure-iot-sdk-java by Azure.
the class HttpHsmSignatureProviderTest method constructorSuccessWithApiVersion.
// Codes_SRS_HTTPHSMSIGNATUREPROVIDER_34_003: [This constructor shall save the provided api version.]
@Test
public void constructorSuccessWithApiVersion() throws NoSuchAlgorithmException, URISyntaxException {
// act
HttpHsmSignatureProvider httpHsmSignatureProvider = new HttpHsmSignatureProvider(expectedProviderUri, expectedApiVersion);
// assert
assertEquals(expectedApiVersion, Deencapsulation.getField(httpHsmSignatureProvider, "apiVersion"));
new Verifications() {
{
new HttpsHsmClient(expectedProviderUri);
times = 1;
}
};
}
use of com.microsoft.azure.sdk.iot.device.hsm.HttpHsmSignatureProvider in project azure-iot-sdk-java by Azure.
the class HttpHsmSignatureProviderTest method signThrowsForNullData.
// Codes_SRS_HTTPHSMSIGNATUREPROVIDER_34_007: [If the provided data is null or empty, this function shall throw an IllegalArgumentException.]
@Test(expected = IllegalArgumentException.class)
public void signThrowsForNullData() throws NoSuchAlgorithmException, TransportException, IOException, URISyntaxException, HsmException {
// arrange
final String keyName = "keyName";
final HttpHsmSignatureProvider signatureProvider = new HttpHsmSignatureProvider(expectedProviderUri, expectedApiVersion);
// act
signatureProvider.sign(keyName, null, expectedGenId);
}
use of com.microsoft.azure.sdk.iot.device.hsm.HttpHsmSignatureProvider in project azure-iot-sdk-java by Azure.
the class HttpHsmSignatureProviderTest method signSuccess.
// Codes_SRS_HTTPHSMSIGNATUREPROVIDER_34_006: [This function shall create a signRequest for the hsm http client to sign, and shall return the utf-8 encoded result of that signing.]
@Test
public void signSuccess(@Mocked URLEncoder mockedURLEncoder) throws NoSuchAlgorithmException, TransportException, IOException, URISyntaxException, HsmException {
// arrange
final String keyName = "keyName";
final String data = "some data";
final String expectedDigest = "some digest";
final String expectedDigestEncoded = "some encoded digest";
new NonStrictExpectations() {
{
new HttpsHsmClient(expectedProviderUri);
result = mockedHttpsHsmClient;
new SignRequest();
result = mockedSignRequest;
mockedHttpsHsmClient.sign(expectedApiVersion, keyName, mockedSignRequest, expectedGenId);
result = mockedSignResponse;
mockedSignResponse.getDigest();
result = expectedDigest;
URLEncoder.encode(expectedDigest, "UTF-8");
result = expectedDigestEncoded;
}
};
final HttpHsmSignatureProvider signatureProvider = new HttpHsmSignatureProvider(expectedProviderUri, expectedApiVersion);
// act
String actualDigest = signatureProvider.sign(keyName, data, expectedGenId);
// assert
assertEquals(expectedDigestEncoded, actualDigest);
new Verifications() {
{
mockedSignRequest.setData(data.getBytes(StandardCharsets.UTF_8));
mockedSignRequest.setKeyId("primary");
mockedSignRequest.setAlgo((Mac) Deencapsulation.getField(signatureProvider, "defaultSignRequestAlgo"));
mockedHttpsHsmClient.sign(expectedApiVersion, keyName, mockedSignRequest, expectedGenId);
mockedSignResponse.getDigest();
}
};
}
use of com.microsoft.azure.sdk.iot.device.hsm.HttpHsmSignatureProvider 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);
}
}
}
use of com.microsoft.azure.sdk.iot.device.hsm.HttpHsmSignatureProvider in project azure-iot-sdk-java by Azure.
the class HttpHsmSignatureProviderTest method constructorSuccess.
// Codes_SRS_HTTPHSMSIGNATUREPROVIDER_34_001: [This constructor shall call the overloaded constructor with the default api version.]
// Codes_SRS_HTTPHSMSIGNATUREPROVIDER_34_002: [This constructor shall create a new HttpsHsmClient with the provided providerUri.]
@Test
public void constructorSuccess() throws NoSuchAlgorithmException, URISyntaxException {
// act
HttpHsmSignatureProvider httpHsmSignatureProvider = new HttpHsmSignatureProvider(expectedProviderUri, defaultApiVersion);
// assert
assertEquals(defaultApiVersion, Deencapsulation.getField(httpHsmSignatureProvider, "apiVersion"));
new Verifications() {
{
new HttpsHsmClient(expectedProviderUri);
times = 1;
}
};
}
Aggregations