Skip to content

Commit

Permalink
Merge branch 'master' into varunpuranik/Tokupdfix1
Browse files Browse the repository at this point in the history
  • Loading branch information
varunpuranik authored Sep 4, 2018
2 parents 00ddb6a + 16f3a43 commit 1436521
Show file tree
Hide file tree
Showing 26 changed files with 1,822 additions and 119 deletions.
56 changes: 28 additions & 28 deletions builds/checkin/edgelet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,34 @@ phases:
inputs:
filePath: edgelet/build/linux/test.sh

################################################################################
- phase: linux_arm32v7
################################################################################
displayName: Linux arm32v7
queue:
name: Hosted Linux Preview
steps:
- script: |
echo "##vso[task.setvariable variable=RUSTUP_HOME;]$VSTS_WORK/rustup"
echo "##vso[task.setvariable variable=CARGO_HOME;]$VSTS_WORK/cargo"
echo "##vso[task.setvariable variable=PATH;]$VSTS_WORK/cargo/bin:$PATH"
displayName: Modify path
- task: Bash@3
displayName: Install Rust
inputs:
filePath: edgelet/build/linux/install.sh
- script: cargo install --git https://github.com/myagley/cross.git --branch set-path
displayName: Install cross (fork with docker fix)
- task: Bash@3
displayName: armv7-unknown-linux-gnueabihf build
inputs:
filePath: edgelet/build/linux/cross.sh
arguments: --toolchain armv7-unknown-linux-gnueabihf --release true
- task: Bash@3
displayName: armv7-unknown-linux-gnueabihf test
inputs:
filePath: edgelet/build/linux/cross-test.sh
arguments: --toolchain armv7-unknown-linux-gnueabihf --release true
# ################################################################################
# - phase: linux_arm32v7
# ################################################################################
# displayName: Linux arm32v7
# queue:
# name: Hosted Linux Preview
# steps:
# - script: |
# echo "##vso[task.setvariable variable=RUSTUP_HOME;]$VSTS_WORK/rustup"
# echo "##vso[task.setvariable variable=CARGO_HOME;]$VSTS_WORK/cargo"
# echo "##vso[task.setvariable variable=PATH;]$VSTS_WORK/cargo/bin:$PATH"
# displayName: Modify path
# - task: Bash@3
# displayName: Install Rust
# inputs:
# filePath: edgelet/build/linux/install.sh
# - script: cargo install --git https://github.com/myagley/cross.git --branch set-path
# displayName: Install cross (fork with docker fix)
# - task: Bash@3
# displayName: armv7-unknown-linux-gnueabihf build
# inputs:
# filePath: edgelet/build/linux/cross.sh
# arguments: --toolchain armv7-unknown-linux-gnueabihf --release true
# - task: Bash@3
# displayName: armv7-unknown-linux-gnueabihf test
# inputs:
# filePath: edgelet/build/linux/cross-test.sh
# arguments: --toolchain armv7-unknown-linux-gnueabihf --release true

################################################################################
- phase: windows_amd64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public static class CloudProxyEventIds
public const int ConnectivityAwareClient = EventIdStart + 600;
public const int ServiceProxy = EventIdStart + 700;
public const int DeviceScopeApiClient = EventIdStart + 800;
public const int CloudTokenAuthenticator = EventIdStart + 900;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,29 @@ public class DeviceScopeApiClient : IDeviceScopeApiClient
const string InScopeIdentitiesUriTemplate = "/devices/{0}/modules/{1}/devicesAndModulesInDeviceScope?deviceCount={2}&continuationToken={3}&api-version={4}";
const string InScopeTargetIdentityUriFormat = "/devices/{0}/modules/{1}/deviceAndModuleInDeviceScope?targetDeviceId={2}&targetModuleId={3}&api-version={4}";

readonly RetryStrategy retryStrategy;
readonly Uri iotHubBaseHttpUri;
readonly string deviceId;
readonly string moduleId;
readonly int batchSize;
readonly ITokenProvider edgeHubTokenProvider;
const string ApiVersion = "2018-08-30-preview";

public DeviceScopeApiClient(string iotHubHostName, string deviceId, string moduleId, int batchSize, ITokenProvider edgeHubTokenProvider)
public DeviceScopeApiClient(
string iotHubHostName,
string deviceId,
string moduleId,
int batchSize,
ITokenProvider edgeHubTokenProvider,
RetryStrategy retryStrategy = null)
{
Preconditions.CheckNonWhiteSpace(iotHubHostName, nameof(iotHubHostName));
this.iotHubBaseHttpUri = new UriBuilder(Uri.UriSchemeHttps, iotHubHostName).Uri;
this.deviceId = Preconditions.CheckNonWhiteSpace(deviceId, nameof(deviceId));
this.moduleId = Preconditions.CheckNonWhiteSpace(moduleId, nameof(moduleId));
this.batchSize = Preconditions.CheckRange(batchSize, 0, 1000, nameof(batchSize));
this.edgeHubTokenProvider = Preconditions.CheckNotNull(edgeHubTokenProvider, nameof(edgeHubTokenProvider));
this.retryStrategy = retryStrategy ?? TransientRetryStrategy;
}

public Task<ScopeResult> GetIdentitiesInScope() =>
Expand Down Expand Up @@ -77,7 +85,8 @@ async Task<ScopeResult> GetIdentitiesInScopeWithRetry(Uri uri)
{
return await ExecuteWithRetry(
() => this.GetIdentitiesInScope(uri),
Events.RetryingGetIdentities);
Events.RetryingGetIdentities,
this.retryStrategy);
}
catch (Exception e)
{
Expand All @@ -104,14 +113,14 @@ async Task<ScopeResult> GetIdentitiesInScope(Uri uri)
}
else
{
throw new Exception($"Error getting Scope result from IoTHub. Status code - {response.StatusCode}, Message - {content}");
throw new DeviceScopeApiException("Error getting device scope result from IoTHub", response.StatusCode, content);
}
}
}

static Task<T> ExecuteWithRetry<T>(Func<Task<T>> func, Action<RetryingEventArgs> onRetry)
static Task<T> ExecuteWithRetry<T>(Func<Task<T>> func, Action<RetryingEventArgs> onRetry, RetryStrategy retryStrategy)
{
var transientRetryPolicy = new RetryPolicy(TransientErrorDetectionStrategy, TransientRetryStrategy);
var transientRetryPolicy = new RetryPolicy(TransientErrorDetectionStrategy, retryStrategy);
transientRetryPolicy.Retrying += (_, args) => onRetry(args);
return transientRetryPolicy.ExecuteAsync(func);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft. All rights reserved.

namespace Microsoft.Azure.Devices.Edge.Hub.CloudProxy
{
using System;
using System.Net;

public class DeviceScopeApiException : Exception
{
public DeviceScopeApiException(string message, HttpStatusCode statusCode, string content)
: base(message)
{
this.StatusCode = statusCode;
this.Content = content;
}

public HttpStatusCode StatusCode { get; }

public string Content { get; }

public override string Message => $"Message: {base.Message}, HttpStatusCode: {this.StatusCode}, Content: {this.Content}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Microsoft.Azure.Devices.Edge.Hub.CloudProxy
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Edge.Hub.Core;
using Microsoft.Azure.Devices.Edge.Hub.Core.Identity.Service;
using Microsoft.Azure.Devices.Edge.Util;
using Microsoft.Extensions.Logging;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.Azure.Devices.Edge.Hub.CloudProxy.Authenticators
{
using System;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Edge.Hub.Core;
using Microsoft.Azure.Devices.Edge.Hub.Core.Cloud;
using Microsoft.Azure.Devices.Edge.Hub.Core.Identity;
using Microsoft.Azure.Devices.Edge.Util;
using Microsoft.Extensions.Logging;

public class CloudTokenAuthenticator : IAuthenticator
{
readonly IConnectionManager connectionManager;

public CloudTokenAuthenticator(IConnectionManager connectionManager)
{
this.connectionManager = Preconditions.CheckNotNull(connectionManager, nameof(connectionManager));
}

public async Task<bool> AuthenticateAsync(IClientCredentials clientCredentials)
{
if (!(clientCredentials is ITokenCredentials))
{
return false;
}

Try<ICloudProxy> cloudProxyTry = await this.connectionManager.CreateCloudConnectionAsync(clientCredentials);
if (cloudProxyTry.Success)
{
try
{
await cloudProxyTry.Value.OpenAsync();
Events.AuthenticatedWithIotHub(clientCredentials.Identity);
return true;
}
catch (Exception ex)
{
Events.ErrorValidatingTokenWithIoTHub(clientCredentials.Identity, ex);
}
}
else
{
Events.ErrorGettingCloudProxy(clientCredentials.Identity, cloudProxyTry.Exception);
}

return false;
}


static class Events
{
static readonly ILogger Log = Logger.Factory.CreateLogger<CloudTokenAuthenticator>();
const int IdStart = CloudProxyEventIds.CloudTokenAuthenticator;

enum EventIds
{
AuthenticatedWithCloud = IdStart,
ErrorValidatingToken,
ErrorGettingCloudProxy
}

public static void AuthenticatedWithIotHub(IIdentity identity)
{
Log.LogDebug((int)EventIds.AuthenticatedWithCloud, $"Authenticated {identity.Id} with IotHub");
}

public static void ErrorValidatingTokenWithIoTHub(IIdentity identity, Exception ex)
{
Log.LogWarning((int)EventIds.ErrorValidatingToken, ex, $"Error validating token for {identity.Id} with IoTHub");
}

public static void ErrorGettingCloudProxy(IIdentity identity, Exception ex)
{
Log.LogWarning((int)EventIds.ErrorGettingCloudProxy, ex, $"Error getting cloud proxy for {identity.Id}");
}
}
}
}
Loading

0 comments on commit 1436521

Please sign in to comment.