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

Updating previews/RBAC branch - Master to previews/RBAC #1825

Merged
merged 29 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c7aa1ac
fix: samples readme links were out-of-date (#1784)
Feb 10, 2021
91d7fe2
fix: samples arguments and remove 1 more dead link (#1785)
Feb 10, 2021
c371f57
fix(readme): Update the location of ConsoleEventListener in our readme
abhipsaMisra Feb 16, 2021
5a753da
doc(service-client) - Updating readme (#1799)
vinagesh Feb 19, 2021
c7d4a5f
fix(doc): Update amqp transport exception doc to have detailed descri…
abhipsaMisra Feb 22, 2021
2726aff
feature(device-client): Make the DeviceClient and ModuleClient extens…
vinagesh Feb 25, 2021
c95dc1f
doc(service-client): Added extra comments to clarify true and false i…
vinagesh Feb 26, 2021
5fcd166
feature,fix (device-client) Handle Twin failures using Amqp (#1796)
bikamani Feb 27, 2021
a6f7409
fix(iot-service): Update xml comments for ServiceClient
abhipsaMisra Mar 2, 2021
347232f
fix(iot-device): Update MqttTransportHandler to not use SemaphoreSlim…
abhipsaMisra Feb 5, 2021
f3c9f8b
fix(iot-device): Update dotnetty task calls to use ConfigureAwait(true)
abhipsaMisra Feb 6, 2021
a0af7b7
fix(iot-device): Fix MqttTransportHandler to not await on user suppli…
abhipsaMisra Feb 25, 2021
747f2ac
IoTHub Exception for Get and Patch Twin failures (#1815)
bikamani Mar 5, 2021
2e71c39
fix(edge): UnixDomainSocketEndPoint is available in .NET 2.1 and grea…
jamdavi Mar 9, 2021
d374837
feat(e2e) - Enabling soft delete when creating keyvaults (#1820)
vinagesh Mar 10, 2021
c4b8097
fix(e2e) - Change event logging to opt in to specific events only. (#…
vinagesh Mar 11, 2021
c75e5d4
(service-client): Design for IoT hub AAD authentication
vinagesh Feb 9, 2021
0d7b54d
(service-client: Refactor and add implementation for token credential…
vinagesh Feb 10, 2021
cbc9b3e
(service-client): Refactor and add sas credential (#1786)
vinagesh Feb 11, 2021
eb4ba7e
(service-client): Add constructors in service client to accept aad an…
vinagesh Feb 11, 2021
6d490b1
(service-client): Add constructors in registry manager to accept aad …
vinagesh Feb 11, 2021
ae64908
(service-client): Add constructors in job client to accept aad and sa…
vinagesh Feb 11, 2021
b1c424c
(service-client): Add constructors to accept aad and sas tokens for d…
vinagesh Feb 11, 2021
5cbe79d
fix(service-client): Support for AzureSasCredential for a better user…
vinagesh Feb 19, 2021
296ccd9
doc(service-client): Update readme about the differnt client and oper…
vinagesh Feb 19, 2021
ee87095
tests(service-client): E2E tests for aad auth on all our clients (#1800)
vinagesh Feb 19, 2021
8c9d253
test(service-client): Adding e2e tests for sas credential auth for Io…
vinagesh Feb 26, 2021
b0652d1
feature(service-client): Adding chaching for aad tokens. (#1807)
vinagesh Feb 26, 2021
30ffbc9
fix(service-client)- Add IoT hub token scope. (#1812)
vinagesh Mar 4, 2021
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
14 changes: 13 additions & 1 deletion common/src/service/CommonConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ namespace Microsoft.Azure.Devices.Common
{
using System;

static class CommonConstants
internal static class CommonConstants
{
// Custom HTTP response contents
internal const string ErrorCode = "errorCode";

// TODO: move these to ConfigProvider
public const string DeviceAudienceFormat = "{0}/devices/{1}";

public const string MediaTypeForDeviceManagementApis = "application/json";
public const string AmqpsScheme = "amqps";
public const string AmqpScheme = "amqp";
Expand All @@ -23,11 +24,13 @@ static class CommonConstants

// IotHub WindowsFabric Constants
public const int WindowsFabricRetryLimit = 20;

public const int WindowsFabricRetryWaitInMilliseconds = 3000;
public const int WindowsFabricClientConnectionPort = 19000;

// AzureStorage Constants
public const int AzureStorageRetryLimit = 3;

public const int AzureStorageRetryWaitInMilliseconds = 3000;

public const string IotHubApplicationName = "fabric:/microsoft.azure.devices.container";
Expand All @@ -42,19 +45,23 @@ static class CommonConstants

// EventHub
public const int EventHubEndpointPortNumber = 5671;

public const string EventHubConnectionStringTemplate = "{0};PartitionCount={1}";

// Namespace paths
public const string ResourceProviderNamespace = "Microsoft.Devices";

public const string ResourceProviderServiceResourceType = ResourceProviderNamespace + "/IotHubs";
public const string ResourceProviderBasePathTemplate = "/subscriptions/{0}/resourceGroups/{1}/providers/" + ResourceProviderServiceResourceType + "/{2}";

// Runtime Retry Constants
public const int RuntimeRetryLimit = 3;

public const int RuntimeRetryWaitInMilliseconds = 5000;

// Device URI Templates
public const string DeviceEventPathTemplate = "/devices/{0}/messages/events";

public const string DeviceBoundPathTemplate = "/devices/{0}/messages/deviceBound";
public const string DeviceBoundPathCompleteTemplate = DeviceBoundPathTemplate + "/{1}";
public const string DeviceBoundPathAbandonTemplate = DeviceBoundPathCompleteTemplate + "/abandon";
Expand All @@ -65,6 +72,7 @@ static class CommonConstants

// IotHub provisioning terminal states (CSM/ARM)
public const string ProvisioningStateSucceed = "Succeeded";

public const string ProvisioningStateFailed = "Failed";
public const string ProvisioningStateCanceled = "Canceled";

Expand All @@ -79,6 +87,7 @@ static class CommonConstants

// Service configurable parameters
public const string PartitionCount = "PartitionCount";

public const string TargetReplicaSetSize = "TargetReplicaSetSize";
public const string MinReplicaSetSize = "MinReplicaSetSize";
public const string SkuMaxUnitOverride = "SkuMaxUnitOverride";
Expand Down Expand Up @@ -118,8 +127,11 @@ static class CommonConstants

// Custom HTTP headers
public const string IotHubErrorCode = "IotHubErrorCode";

public const string HttpErrorCodeName = "iothub-errorcode";

public static readonly string[] IotHubAadTokenScopes = new string[] { "https://iothubs.azure.net/.default" };

//Service Analytics related
public static class ServiceAnalytics
{
Expand Down
79 changes: 11 additions & 68 deletions common/src/service/IotHubConnectionString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,27 @@

namespace Microsoft.Azure.Devices
{
internal sealed class IotHubConnectionString : IAuthorizationHeaderProvider, ICbsTokenProvider
/// <summary>
/// The properties required for authentication to IoT hub using a connection string.
/// </summary>
internal sealed class IotHubConnectionString
: IotHubConnectionProperties
{
private static readonly TimeSpan s_defaultTokenTimeToLive = TimeSpan.FromHours(1);
private const char UserSeparator = '@';
private static readonly TimeSpan _tokenTimeToLive = TimeSpan.FromHours(1);

public IotHubConnectionString(IotHubConnectionStringBuilder builder)
public IotHubConnectionString(IotHubConnectionStringBuilder builder) : base(builder?.HostName)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

Audience = builder.HostName;
HostName = string.IsNullOrEmpty(builder.GatewayHostName) ? builder.HostName : builder.GatewayHostName;
SharedAccessKeyName = builder.SharedAccessKeyName;
SharedAccessKey = builder.SharedAccessKey;
SharedAccessSignature = builder.SharedAccessSignature;
IotHubName = builder.IotHubName;
HttpsEndpoint = new UriBuilder("https", HostName).Uri;
AmqpEndpoint = new UriBuilder(CommonConstants.AmqpsScheme, builder.HostName, AmqpConstants.DefaultSecurePort).Uri;
DeviceId = builder.DeviceId;
ModuleId = builder.ModuleId;
GatewayHostName = builder.GatewayHostName;
}

public string IotHubName { get; private set; }

public string HostName { get; private set; }

public Uri HttpsEndpoint { get; private set; }

public Uri AmqpEndpoint { get; private set; }

public string Audience { get; private set; }

public string SharedAccessKeyName { get; private set; }
Expand All @@ -52,45 +40,17 @@ public IotHubConnectionString(IotHubConnectionStringBuilder builder)

public string SharedAccessSignature { get; private set; }

public string DeviceId { get; private set; }

public string ModuleId { get; private set; }

public string GatewayHostName { get; private set; }

public string GetUser()
{
var stringBuilder = new StringBuilder();
stringBuilder.Append(SharedAccessKeyName);
stringBuilder.Append(UserSeparator);
stringBuilder.Append("sas.");
stringBuilder.Append("root.");
stringBuilder.Append(IotHubName);

return stringBuilder.ToString();
}

public string GetPassword()
{
string password;
if (string.IsNullOrWhiteSpace(SharedAccessSignature))
{
password = BuildToken(out _);
}
else
{
password = SharedAccessSignature;
}

return password;
return string.IsNullOrWhiteSpace(SharedAccessSignature) ? BuildToken(out _) : SharedAccessSignature;
}

public string GetAuthorizationHeader()
public override string GetAuthorizationHeader()
{
return GetPassword();
}

Task<CbsToken> ICbsTokenProvider.GetTokenAsync(Uri namespaceAddress, string appliesTo, string[] requiredClaims)
public override Task<CbsToken> GetTokenAsync(Uri namespaceAddress, string appliesTo, string[] requiredClaims)
{
string tokenValue;
CbsToken token;
Expand All @@ -108,16 +68,6 @@ Task<CbsToken> ICbsTokenProvider.GetTokenAsync(Uri namespaceAddress, string appl
return Task.FromResult(token);
}

public Uri BuildLinkAddress(string path)
{
var builder = new UriBuilder(AmqpEndpoint)
{
Path = path,
};

return builder.Uri;
}

public static IotHubConnectionString Parse(string connectionString)
{
var builder = IotHubConnectionStringBuilder.Create(connectionString);
Expand All @@ -130,17 +80,10 @@ private string BuildToken(out TimeSpan ttl)
{
KeyName = SharedAccessKeyName,
Key = SharedAccessKey,
TimeToLive = s_defaultTokenTimeToLive,
TimeToLive = _tokenTimeToLive,
Target = Audience
};

if (DeviceId != null)
{
builder.Target = string.IsNullOrEmpty(ModuleId)
? "{0}/devices/{1}".FormatInvariant(Audience, WebUtility.UrlEncode(DeviceId))
: "{0}/devices/{1}/modules/{2}".FormatInvariant(Audience, WebUtility.UrlEncode(DeviceId), WebUtility.UrlEncode(ModuleId));
}

ttl = builder.TimeToLive;

return builder.ToSignature();
Expand Down
3 changes: 1 addition & 2 deletions e2e/test/E2EMsTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ namespace Microsoft.Azure.Devices.E2ETests
/// </summary>
public class E2EMsTestBase : IDisposable
{
private static readonly string[] s_eventProviders = new string[] { "DotNetty-Default", "Microsoft-Azure-", };
private ConsoleEventListener _listener;

// Test specific logger instance
Expand All @@ -41,7 +40,7 @@ public void TestInitialize()
// Note: Events take long and increase run time of the test suite, so only using trace.
Logger.Trace($"Starting test - {TestContext.TestName}", SeverityLevel.Information);

_listener = new ConsoleEventListener(s_eventProviders);
_listener = new ConsoleEventListener();
}

[TestCleanup]
Expand Down
1 change: 1 addition & 0 deletions e2e/test/E2ETests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<!-- NetCore and .NET 4.7.2 -->
<ItemGroup Condition=" '$(TargetFramework)' != 'net451' ">
<PackageReference Include="Microsoft.Azure.Storage.Blob" Version="11.1.3" />
<PackageReference Include="Azure.Identity" Version="1.3.0" />
</ItemGroup>

<ItemGroup Condition=" '$(AZURE_IOT_LOCALPACKAGES)' == '' ">
Expand Down
78 changes: 22 additions & 56 deletions e2e/test/Helpers/ConsoleEventListener.cs
Original file line number Diff line number Diff line change
@@ -1,89 +1,55 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace System.Diagnostics.Tracing
{
public sealed class ConsoleEventListener : EventListener
{
private readonly string[] _eventFilters;
private readonly object _lock = new object();

public ConsoleEventListener(string filter)
{
_eventFilters = new string[1];
_eventFilters[0] = filter ?? throw new ArgumentNullException(nameof(filter));

InitializeEventSources();
}

public ConsoleEventListener(string[] filters)
{
_eventFilters = filters ?? throw new ArgumentNullException(nameof(filters));
if (_eventFilters.Length == 0)
{
throw new ArgumentException("Filters cannot be empty", nameof(filters));
}

foreach (string filter in _eventFilters)
{
if (string.IsNullOrWhiteSpace(filter))
{
throw new ArgumentNullException(nameof(filters));
}
}
// Configure this value to filter all the necessary events when OnEventSourceCreated is called.
// OnEventSourceCreated is triggered as soon as the EventListener is registered and an event source is created.
// So trying to configure this value in the ConsoleEventListener constructor does not work.
// The OnEventSourceCreated can be triggered sooner than the filter is initialized in the ConsoleEventListener constructor.
private static string[] _eventFilters = new string[] { "DotNetty-Default", "Microsoft-Azure-Devices" };

InitializeEventSources();
}

private void InitializeEventSources()
{
foreach (EventSource source in EventSource.GetSources())
{
EnableEvents(source, EventLevel.LogAlways);
}
}
private readonly object _lock = new object();

protected override void OnEventSourceCreated(EventSource eventSource)
{
base.OnEventSourceCreated(eventSource);
EnableEvents(
eventSource,
EventLevel.LogAlways
if (_eventFilters.Any(filter => eventSource.Name.StartsWith(filter, StringComparison.OrdinalIgnoreCase)))
{
base.OnEventSourceCreated(eventSource);
EnableEvents(
eventSource,
EventLevel.LogAlways
#if !NET451
, EventKeywords.All
#endif
);
}
}

protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
if (_eventFilters == null)
{
return;
}

lock (_lock)
{
if (_eventFilters.Any(ef => eventData.EventSource.Name.StartsWith(ef, StringComparison.Ordinal)))
{
string eventIdent;
string eventIdent;
#if NET451
// net451 doesn't have EventName, so we'll settle for EventId
eventIdent = eventData.EventId.ToString(CultureInfo.InvariantCulture);
#else
eventIdent = eventData.EventName;
eventIdent = eventData.EventName;
#endif
string text = $"{DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffffff", CultureInfo.InvariantCulture)} [{eventData.EventSource.Name}-{eventIdent}]{(eventData.Payload != null ? $" ({string.Join(", ", eventData.Payload)})." : "")}";
string text = $"{DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffffff", CultureInfo.InvariantCulture)} [{eventData.EventSource.Name}-{eventIdent}]{(eventData.Payload != null ? $" ({string.Join(", ", eventData.Payload)})." : "")}";

ConsoleColor origForeground = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(text);
Debug.WriteLine(text);
Console.ForegroundColor = origForeground;
}
ConsoleColor origForeground = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(text);
Debug.WriteLine(text);
Console.ForegroundColor = origForeground;
}
}
}
Expand Down
Loading