Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move back to public SDK nugets #4371

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.33.1-NestedEdge" />
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.33.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Linq.Async" Version="4.1.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Devices" Version="1.28.1-NestedEdge" />
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.33.1-NestedEdge" />
<PackageReference Include="Microsoft.Azure.Devices" Version="1.28.0" />
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.33.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Amqp" Version="2.4.7" />
<PackageReference Include="Microsoft.Azure.Devices" Version="1.28.1-NestedEdge" />
<PackageReference Include="Microsoft.Azure.Devices" Version="1.28.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Devices" Version="1.28.1-NestedEdge" />
<PackageReference Include="Microsoft.Azure.Devices" Version="1.28.0" />
<PackageReference Include="Stateless" Version="4.0.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
namespace Microsoft.Azure.Devices.Edge.Hub.CloudProxy
{
using System.Collections.Generic;
using Microsoft.Azure.Devices.Edge.Util;
using Newtonsoft.Json;

public class ScopeResult
{
[JsonConstructor]
public ScopeResult(IEnumerable<Device> devices, IEnumerable<Module> modules, string continuationLink)
public ScopeResult(IEnumerable<IotHubDevice> devices, IEnumerable<Module> modules, string continuationLink)
{
this.Devices = devices;
this.Modules = modules;
Expand All @@ -18,7 +19,7 @@ public ScopeResult(IEnumerable<Device> devices, IEnumerable<Module> modules, str
/// The scope result items, as a collection.
/// </summary>
[JsonProperty(PropertyName = "devices", Required = Required.AllowNull)]
public IEnumerable<Device> Devices { get; }
public IEnumerable<IotHubDevice> Devices { get; }

/// <summary>
/// The scope result items, as a collection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.Azure.Devices.Edge.Hub.CloudProxy

public static class ServiceIdentityHelpers
{
public static ServiceIdentity ToServiceIdentity(this Device device)
public static ServiceIdentity ToServiceIdentity(this IotHubDevice device)
{
Preconditions.CheckNotNull(device, nameof(device));
return new ServiceIdentity(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<ItemGroup>
<PackageReference Include="App.Metrics" Version="3.0.0" />
<PackageReference Include="JetBrains.Annotations" Version="2018.3.0" />
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.33.1-NestedEdge" />
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.33.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Devices" Version="1.28.1-NestedEdge" />
<PackageReference Include="Microsoft.Azure.Devices" Version="1.28.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Microsoft.Azure.Devices.Edge.Hub.CloudProxy.Test
using System.Text;
using Microsoft.Azure.Devices.Edge.Hub.Core;
using Microsoft.Azure.Devices.Edge.Hub.Core.Identity.Service;
using Microsoft.Azure.Devices.Edge.Util;
using Microsoft.Azure.Devices.Edge.Util.Test.Common;
using Newtonsoft.Json;
using Xunit;
Expand Down Expand Up @@ -261,7 +262,7 @@ public static IEnumerable<object[]> GetModuleJson()
public void DeviceToServiceIdentityTest(string deviceJson)
{
// Arrange
var device = JsonConvert.DeserializeObject<Device>(deviceJson);
var device = JsonConvert.DeserializeObject<IotHubDevice>(deviceJson);

// Act
ServiceIdentity serviceIdentity = device.ToServiceIdentity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ public class ServiceProxyTest
public async Task IteratorTest()
{
// Arrange
IEnumerable<Device> devices1 = new[] { GetDevice("d1"), GetDevice("d2"), GetDevice("d3") };
IEnumerable<IotHubDevice> devices1 = new[] { GetDevice("d1"), GetDevice("d2"), GetDevice("d3") };
IEnumerable<Module> modules1 = null;
string continuationToken1 = "/devices/d301/modules/%24edgeHub/devicesAndModulesInDeviceScope?deviceCount=10&continuationToken=cccccDDDDDRRRRRssssw&api-version=2018-08-30-preview";
var scopeResult1 = new ScopeResult(devices1, modules1, continuationToken1);

IEnumerable<Device> devices2 = new[] { GetDevice("d4"), GetDevice("d5") };
IEnumerable<IotHubDevice> devices2 = new[] { GetDevice("d4"), GetDevice("d5") };
IEnumerable<Module> modules2 = new[] { GetModule("d10", "m1") };
string continuationToken2 = "/devices/d301/modules/%24edgeHub/devicesAndModulesInDeviceScope?deviceCount=10&continuationToken=cccccbbbbRRRRRssssw&api-version=2018-08-30-preview";
var scopeResult2 = new ScopeResult(devices2, modules2, continuationToken2);

IEnumerable<Device> devices3 = null;
IEnumerable<IotHubDevice> devices3 = null;
IEnumerable<Module> modules3 = new[] { GetModule("d11", "m1"), GetModule("d11", "m2"), GetModule("d12", "m2") };
string continuationToken3 = null;
var scopeResult3 = new ScopeResult(devices3, modules3, continuationToken3);
Expand Down Expand Up @@ -77,7 +77,7 @@ public async Task IteratorTest()
public async Task GetServiceIdentitiy_DeviceTest()
{
// Arrange
IEnumerable<Device> devices1 = new[] { GetDevice("d1") };
IEnumerable<IotHubDevice> devices1 = new[] { GetDevice("d1") };
IEnumerable<Module> modules1 = null;
string continuationToken1 = null;
var scopeResult1 = new ScopeResult(devices1, modules1, continuationToken1);
Expand All @@ -101,7 +101,7 @@ public async Task GetServiceIdentitiy_DeviceTest()
public async Task GetServiceIdentitiy_Device_InvalidTest_MultipleResults()
{
// Arrange
IEnumerable<Device> devices1 = new[] { GetDevice("d1"), GetDevice("d1") };
IEnumerable<IotHubDevice> devices1 = new[] { GetDevice("d1"), GetDevice("d1") };
IEnumerable<Module> modules1 = null;
string continuationToken1 = null;
var scopeResult1 = new ScopeResult(devices1, modules1, continuationToken1);
Expand All @@ -124,7 +124,7 @@ public async Task GetServiceIdentitiy_Device_InvalidTest_MultipleResults()
public async Task GetServiceIdentitiy_Device_InvalidTest_EmptyResult()
{
// Arrange
IEnumerable<Device> devices1 = new Device[0];
IEnumerable<IotHubDevice> devices1 = new IotHubDevice[0];
IEnumerable<Module> modules1 = null;
string continuationToken1 = null;
var scopeResult1 = new ScopeResult(devices1, modules1, continuationToken1);
Expand Down Expand Up @@ -239,7 +239,7 @@ public async Task GetServiceIdentitiy_Exception_ModuleTest()
public async Task GetServiceIdentitiy_ModuleTest()
{
// Arrange
IEnumerable<Device> devices1 = null;
IEnumerable<IotHubDevice> devices1 = null;
IEnumerable<Module> modules1 = new[] { GetModule("d1", "m1") };

string continuationToken1 = null;
Expand All @@ -264,7 +264,7 @@ public async Task GetServiceIdentitiy_ModuleTest()
public async Task GetServiceIdentitiy_Module_InvalidTest_MultipleResults()
{
// Arrange
IEnumerable<Device> devices1 = null;
IEnumerable<IotHubDevice> devices1 = null;
IEnumerable<Module> modules1 = new[] { GetModule("d1", "m1"), GetModule("d1", "m1") };

string continuationToken1 = null;
Expand All @@ -288,7 +288,7 @@ public async Task GetServiceIdentitiy_Module_InvalidTest_MultipleResults()
public async Task GetServiceIdentitiy_Module_InvalidTest_EmptyResult()
{
// Arrange
IEnumerable<Device> devices1 = null;
IEnumerable<IotHubDevice> devices1 = null;
IEnumerable<Module> modules1 = new Module[0];

string continuationToken1 = null;
Expand Down Expand Up @@ -333,7 +333,7 @@ static bool Compare(IEnumerable<ServiceIdentity> serviceIdentities, ScopeResult
List<ServiceIdentity> serviceIdentitiesList = serviceIdentities.ToList();
if (scopeResult.Devices != null)
{
foreach (Device d in scopeResult.Devices)
foreach (IotHubDevice d in scopeResult.Devices)
{
if (!serviceIdentitiesList.Any(s => s.DeviceId == d.Id && !s.IsModule))
{
Expand All @@ -356,7 +356,7 @@ static bool Compare(IEnumerable<ServiceIdentity> serviceIdentities, ScopeResult
return true;
}

static Device GetDevice(string id)
static IotHubDevice GetDevice(string id)
{
string deviceJson = @"{
""deviceId"": """ + id + @""",
Expand Down Expand Up @@ -385,7 +385,7 @@ static Device GetDevice(string id)
},
""deviceScope"": ""ms-azure-iot-edge://d301-12345""
}";
var device = JsonConvert.DeserializeObject<Device>(deviceJson);
var device = JsonConvert.DeserializeObject<IotHubDevice>(deviceJson);
return device;
}

Expand Down
2 changes: 1 addition & 1 deletion edge-modules/MetricsCollector/MetricsCollector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.33.1-NestedEdge" />
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.33.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.3" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.33.1-NestedEdge" />
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.33.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.3" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.33.1-NestedEdge" />
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.33.0" />
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.4" />
</ItemGroup>

Expand Down
21 changes: 21 additions & 0 deletions edge-util/src/Microsoft.Azure.Devices.Edge.Util/IotHubDevice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.Azure.Devices.Edge.Util
{
using System.Collections.Generic;
using Newtonsoft.Json;

public class IotHubDevice : Device
{
public IotHubDevice()
{
}

public IotHubDevice(string id)
: base(id)
{
}

[JsonProperty(PropertyName = "parentScopes", NullValueHandling = NullValueHandling.Ignore)]
public virtual IEnumerable<string> ParentScopes { get; set; }
}
}
136 changes: 136 additions & 0 deletions edge-util/src/Microsoft.Azure.Devices.Edge.Util/IotHubDeviceHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.Azure.Devices.Edge.Util
{
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Common;
using Newtonsoft.Json;

public class IotHubDeviceHelper
{
const string ApiVersion = "api-version=2020-06-30-preview";
const string RequestUriFormat = "/devices/{0}?{1}";
static readonly TimeSpan TokenTimeToLive = TimeSpan.FromHours(1);
ISignatureProvider signatureProvider;
IotHubConnectionStringBuilder connectionStringBuilder;
Uri iotHubBaseHttpsUri;
HttpClient httpClient;

public IotHubDeviceHelper(string connectionString)
{
this.connectionStringBuilder = IotHubConnectionStringBuilder.Create(connectionString);
this.signatureProvider = new SharedAccessKeySignatureProvider(this.connectionStringBuilder.SharedAccessKey);
this.iotHubBaseHttpsUri = new UriBuilder(Uri.UriSchemeHttps, this.connectionStringBuilder.HostName).Uri;

this.httpClient = new HttpClient()
{
BaseAddress = this.iotHubBaseHttpsUri,
Timeout = TimeSpan.FromSeconds(60),
};
this.httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
this.httpClient.DefaultRequestHeaders.ExpectContinue = false;
}

public async Task<IotHubDevice> GetDeviceAsync(string deviceId)
{
Uri uri = this.GetServiceUri(deviceId);

using var msg = new HttpRequestMessage(HttpMethod.Get, uri);
msg.Headers.Add(HttpRequestHeader.Authorization.ToString(), await this.GetToken());

HttpResponseMessage responseMsg;
responseMsg = await this.httpClient.SendAsync(msg);
if (responseMsg == null)
{
throw new InvalidOperationException($"The response message was null when getting device: {deviceId}");
}

string resultString = await responseMsg.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<IotHubDevice>(resultString);
}

public async Task<IotHubDevice> AddDeviceAsync(IotHubDevice device)
{
NormalizeDevice(device);

Uri uri = this.GetServiceUri(device.Id);
using var msg = new HttpRequestMessage(HttpMethod.Put, uri);
msg.Headers.Add(HttpRequestHeader.Authorization.ToString(), await this.GetToken());

string str = JsonConvert.SerializeObject(device);
msg.Content = new StringContent(str, Encoding.UTF8, "application/json");

HttpResponseMessage responseMsg;
responseMsg = await this.httpClient.SendAsync(msg);
if (responseMsg == null)
{
throw new InvalidOperationException($"The response message was null when adding a new device: {device.Id}");
}

string resultString = await responseMsg.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<IotHubDevice>(resultString);
}

Uri GetServiceUri(string deviceId)
{
string encodedDeviceId = WebUtility.UrlEncode(deviceId);
return new Uri(this.iotHubBaseHttpsUri, new Uri(RequestUriFormat.FormatInvariant(deviceId, ApiVersion), UriKind.Relative));
}

async Task<string> GetToken()
{
// Audience for service APIs is just the iothub hostname
string audience = this.connectionStringBuilder.HostName;
string expiresOn = SasTokenHelper.BuildExpiresOn(DateTime.UtcNow, TokenTimeToLive);
string data = string.Join(
"\n",
new List<string>
{
audience,
expiresOn
});

try
{
string signature = await this.signatureProvider.SignAsync(data);
string token = SasTokenHelper.BuildSasToken(audience, signature, expiresOn);
token += "&skn=" + this.connectionStringBuilder.SharedAccessKeyName;
return token;
}
catch (SignatureProviderException e)
{
throw new TokenProviderException(e);
}
}

static void NormalizeDevice(IotHubDevice device)
{
// auto generate keys if not specified
if (device.Authentication == null)
{
device.Authentication = new AuthenticationMechanism();
}

NormalizeAuthenticationInfo(device.Authentication);
}

static void NormalizeAuthenticationInfo(AuthenticationMechanism authenticationInfo)
{
if (authenticationInfo.SymmetricKey != null && !authenticationInfo.SymmetricKey.IsEmpty())
{
authenticationInfo.Type = AuthenticationType.Sas;
}

if (authenticationInfo.X509Thumbprint != null && !authenticationInfo.X509Thumbprint.IsEmpty())
{
authenticationInfo.Type = AuthenticationType.SelfSigned;
}
}
}
}
Loading