From 50d6f8ffaa8bd4d1ea05538dd2878f44e575fe72 Mon Sep 17 00:00:00 2001 From: Rick Tang Date: Fri, 27 Jan 2023 14:51:12 -0800 Subject: [PATCH 01/14] Upgrade Lasso and Add subcommand `info`. --- src/AzureAuth.Test/AzureAuth.Test.csproj | 10 +--- src/AzureAuth/AzureAuth.csproj | 2 +- src/AzureAuth/CommandInfo.cs | 48 +++++++++++++++++++ src/AzureAuth/CommandMain.cs | 4 ++ .../ExceptionListToStringConverter.cs | 2 +- src/AzureAuth/OutputMode.cs | 5 ++ src/AzureAuth/Program.cs | 4 +- 7 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 src/AzureAuth/CommandInfo.cs diff --git a/src/AzureAuth.Test/AzureAuth.Test.csproj b/src/AzureAuth.Test/AzureAuth.Test.csproj index 1b0c264e..30229923 100644 --- a/src/AzureAuth.Test/AzureAuth.Test.csproj +++ b/src/AzureAuth.Test/AzureAuth.Test.csproj @@ -1,19 +1,11 @@  - net6.0-windows10.0.19041.0 PlatformWindows - - net6.0 - - - - net6.0 - - + net6.0 false diff --git a/src/AzureAuth/AzureAuth.csproj b/src/AzureAuth/AzureAuth.csproj index 75d2efc5..cb518fbb 100644 --- a/src/AzureAuth/AzureAuth.csproj +++ b/src/AzureAuth/AzureAuth.csproj @@ -39,7 +39,7 @@ - + diff --git a/src/AzureAuth/CommandInfo.cs b/src/AzureAuth/CommandInfo.cs new file mode 100644 index 00000000..1125dcf0 --- /dev/null +++ b/src/AzureAuth/CommandInfo.cs @@ -0,0 +1,48 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +namespace Microsoft.Authentication.AzureAuth +{ + using System.IO.Abstractions; + using System.Reflection; + using McMaster.Extensions.CommandLineUtils; + using Microsoft.Extensions.Logging; + using Microsoft.Office.Lasso.Telemetry; + + /// + /// The command shows debug information and system runtime. + /// + [Command(Name = "info", Description = "Show debug information of AzureAuth. Please provide when asking for help.")] + internal class CommandInfo + { + /// + /// This method executes the info process. + /// + /// The logger. + /// The file system. + /// The error code: 0 is normal execution, and the rest means errors during execution. + public int OnExecute(ILogger logger, IFileSystem fileSystem) + { + Assembly assembly = Assembly.GetExecutingAssembly(); + + string azureauthVersion = assembly.GetName().Version.ToString(); + logger.LogInformation($"AzureAuth Version: {azureauthVersion}"); + + string lassoVersion = Office.Lasso.Version.LassoVersion(); + logger.LogInformation($"Lasso Version: {lassoVersion}"); + + string clrVersion = assembly.ImageRuntimeVersion; + logger.LogInformation($"CLR Version: {clrVersion}"); + + string deviceID = TelemetryMachineIDHelper.GetRandomDeviceIDAsync(fileSystem).Result; + logger.LogInformation($"Device ID: {deviceID}"); + + logger.LogInformation($"Device Identifier File Location: {TelemetryMachineIDHelper.GetIdentifierLocation(fileSystem)}"); + logger.LogInformation($"To reset your device identifier, delete the file at {TelemetryMachineIDHelper.GetIdentifierLocation(fileSystem)}."); + + logger.LogInformation($"\nTo get the user's sid, use option --output=sid in normal authentication process."); + + return 0; + } + } +} diff --git a/src/AzureAuth/CommandMain.cs b/src/AzureAuth/CommandMain.cs index 85cda4d8..d5a153b4 100644 --- a/src/AzureAuth/CommandMain.cs +++ b/src/AzureAuth/CommandMain.cs @@ -23,6 +23,7 @@ namespace Microsoft.Authentication.AzureAuth /// The command main class parses commands and dispatches to the corresponding methods. /// [Command(Name = "azureauth", Description = "A CLI interface to MSAL authentication")] + [Subcommand(typeof(CommandInfo))] public class CommandMain { private const string ResourceOption = "--resource"; @@ -489,6 +490,9 @@ private int GetToken() case OutputMode.Json: Console.Write(tokenResult.ToJson()); break; + case OutputMode.SID: + Console.Write(tokenResult.SID); + break; case OutputMode.None: break; } diff --git a/src/AzureAuth/ExceptionListToStringConverter.cs b/src/AzureAuth/ExceptionListToStringConverter.cs index 47dd54b1..b8ed6972 100644 --- a/src/AzureAuth/ExceptionListToStringConverter.cs +++ b/src/AzureAuth/ExceptionListToStringConverter.cs @@ -100,7 +100,7 @@ public string Message /// /// Sets the error code. /// - /// exception from which error code is extracted + /// exception from which error code is extracted. private void SetAADErrorCode(Exception exception) { var exceptionType = exception.GetType(); diff --git a/src/AzureAuth/OutputMode.cs b/src/AzureAuth/OutputMode.cs index 9c08283c..30e38b39 100644 --- a/src/AzureAuth/OutputMode.cs +++ b/src/AzureAuth/OutputMode.cs @@ -29,5 +29,10 @@ public enum OutputMode /// The none. /// None, + + /// + /// The SID. + /// + SID, } } diff --git a/src/AzureAuth/Program.cs b/src/AzureAuth/Program.cs index 8529b81f..e989972e 100644 --- a/src/AzureAuth/Program.cs +++ b/src/AzureAuth/Program.cs @@ -49,7 +49,9 @@ private static void Main(string[] args) backend: backend, ingestionToken: ingestionToken, useAsync: true, - envVarsToCollect: new[] { "SYSTEM_DEFINITIONID", "QBUILD_DISTRIBUTED" }); + envVarsToCollect: new[] { "SYSTEM_DEFINITIONID", "QBUILD_DISTRIBUTED" }, + hideAlias: true, + hideMachineName: true); // We want redirect stdout to get just token output // while warnings and errors still go to stderr to be seen by a user. From e5d34e6588c3ced1f113b273ae21228faddb6053 Mon Sep 17 00:00:00 2001 From: Rick Tang Date: Fri, 27 Jan 2023 15:59:55 -0800 Subject: [PATCH 02/14] CLR and Lasso version may not be necessary --- src/AzureAuth/CommandInfo.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/AzureAuth/CommandInfo.cs b/src/AzureAuth/CommandInfo.cs index 1125dcf0..bf3359ad 100644 --- a/src/AzureAuth/CommandInfo.cs +++ b/src/AzureAuth/CommandInfo.cs @@ -28,12 +28,6 @@ public int OnExecute(ILogger logger, IFileSystem fileSystem) string azureauthVersion = assembly.GetName().Version.ToString(); logger.LogInformation($"AzureAuth Version: {azureauthVersion}"); - string lassoVersion = Office.Lasso.Version.LassoVersion(); - logger.LogInformation($"Lasso Version: {lassoVersion}"); - - string clrVersion = assembly.ImageRuntimeVersion; - logger.LogInformation($"CLR Version: {clrVersion}"); - string deviceID = TelemetryMachineIDHelper.GetRandomDeviceIDAsync(fileSystem).Result; logger.LogInformation($"Device ID: {deviceID}"); From 70db8c787c5237a6540c53ca3495cb86492f22b0 Mon Sep 17 00:00:00 2001 From: Rick Tang Date: Tue, 31 Jan 2023 12:41:02 -0800 Subject: [PATCH 03/14] Add option --reset-device-id --- CHANGELOG.md | 1 + src/AzureAuth/CommandInfo.cs | 36 +++++++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbcf48f3..3b86da0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Added On-premises Security Identifier in Telemetry as `sid`. +- Added Subcommand `info` and its option `--reset-device-id` to show or reset the device ID. ### Changed - Upgrade MSAL to 4.47.2 and opt-in native WAM mode. diff --git a/src/AzureAuth/CommandInfo.cs b/src/AzureAuth/CommandInfo.cs index bf3359ad..987e4068 100644 --- a/src/AzureAuth/CommandInfo.cs +++ b/src/AzureAuth/CommandInfo.cs @@ -15,6 +15,14 @@ namespace Microsoft.Authentication.AzureAuth [Command(Name = "info", Description = "Show debug information of AzureAuth. Please provide when asking for help.")] internal class CommandInfo { + private const string OptionResetDeviceID = "--reset-device-id"; + + /// + /// Gets or sets a value indicating whether reset device id. + /// + [Option(OptionResetDeviceID, "Reset Device ID", CommandOptionType.NoValue)] + public bool ResetDeviceID { get; set; } + /// /// This method executes the info process. /// @@ -23,18 +31,32 @@ internal class CommandInfo /// The error code: 0 is normal execution, and the rest means errors during execution. public int OnExecute(ILogger logger, IFileSystem fileSystem) { - Assembly assembly = Assembly.GetExecutingAssembly(); + if (this.ResetDeviceID) + { + return this.ResetID(logger, fileSystem); + } + Assembly assembly = Assembly.GetExecutingAssembly(); string azureauthVersion = assembly.GetName().Version.ToString(); - logger.LogInformation($"AzureAuth Version: {azureauthVersion}"); - string deviceID = TelemetryMachineIDHelper.GetRandomDeviceIDAsync(fileSystem).Result; - logger.LogInformation($"Device ID: {deviceID}"); + string deviceIDLocation = TelemetryMachineIDHelper.GetIdentifierLocation(fileSystem); - logger.LogInformation($"Device Identifier File Location: {TelemetryMachineIDHelper.GetIdentifierLocation(fileSystem)}"); - logger.LogInformation($"To reset your device identifier, delete the file at {TelemetryMachineIDHelper.GetIdentifierLocation(fileSystem)}."); + logger.LogInformation( + $"AzureAuth Version: {azureauthVersion} \n" + + $"Device ID: {deviceID} \n" + + $"Device ID Path: {deviceIDLocation} \n" + + $"To reset your device identifier, Run `azureauth info {OptionResetDeviceID}` \n" + + $"\n" + + $"To get the user's sid, use option --output=sid. For example:\n" + + $"azureauth --client --scope --tenant --output sid"); - logger.LogInformation($"\nTo get the user's sid, use option --output=sid in normal authentication process."); + return 0; + } + + private int ResetID(ILogger logger, IFileSystem fileSystem) + { + fileSystem.File.Delete(TelemetryMachineIDHelper.GetIdentifierLocation(fileSystem)); + logger.LogInformation($"Device ID was reset."); return 0; } From b91d6c97a40e29013fbbebe4e1125e15e4a8f6c5 Mon Sep 17 00:00:00 2001 From: goagain Date: Tue, 31 Jan 2023 16:36:42 -0800 Subject: [PATCH 04/14] Apply suggestions from code review Co-authored-by: Shalini Khare <31912044+shalinikhare27@users.noreply.github.com> --- src/AzureAuth/CommandInfo.cs | 2 +- src/AzureAuth/ExceptionListToStringConverter.cs | 2 +- src/AzureAuth/OutputMode.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/AzureAuth/CommandInfo.cs b/src/AzureAuth/CommandInfo.cs index 987e4068..6fdae21b 100644 --- a/src/AzureAuth/CommandInfo.cs +++ b/src/AzureAuth/CommandInfo.cs @@ -12,7 +12,7 @@ namespace Microsoft.Authentication.AzureAuth /// /// The command shows debug information and system runtime. /// - [Command(Name = "info", Description = "Show debug information of AzureAuth. Please provide when asking for help.")] + [Command(Name = "info", Description = "Shows AzureAuth debug information. Please provide when asking for help.")] internal class CommandInfo { private const string OptionResetDeviceID = "--reset-device-id"; diff --git a/src/AzureAuth/ExceptionListToStringConverter.cs b/src/AzureAuth/ExceptionListToStringConverter.cs index b8ed6972..e3bae816 100644 --- a/src/AzureAuth/ExceptionListToStringConverter.cs +++ b/src/AzureAuth/ExceptionListToStringConverter.cs @@ -100,7 +100,7 @@ public string Message /// /// Sets the error code. /// - /// exception from which error code is extracted. + /// Exception from which error code is extracted. private void SetAADErrorCode(Exception exception) { var exceptionType = exception.GetType(); diff --git a/src/AzureAuth/OutputMode.cs b/src/AzureAuth/OutputMode.cs index 30e38b39..73d0ac12 100644 --- a/src/AzureAuth/OutputMode.cs +++ b/src/AzureAuth/OutputMode.cs @@ -31,7 +31,7 @@ public enum OutputMode None, /// - /// The SID. + /// The Security Identifier. /// SID, } From 383cac8048124ac70373114735e0539f2c6bc7c2 Mon Sep 17 00:00:00 2001 From: Rick Tang Date: Fri, 3 Feb 2023 10:47:54 -0800 Subject: [PATCH 05/14] Upgrade to 2023.2.3.1 --- src/AzureAuth/AzureAuth.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AzureAuth/AzureAuth.csproj b/src/AzureAuth/AzureAuth.csproj index cb518fbb..bc301f59 100644 --- a/src/AzureAuth/AzureAuth.csproj +++ b/src/AzureAuth/AzureAuth.csproj @@ -39,7 +39,7 @@ - + From 913f69b2cdae7b669bfc7df49370ec561447ed65 Mon Sep 17 00:00:00 2001 From: Rick Tang Date: Fri, 3 Feb 2023 12:16:10 -0800 Subject: [PATCH 06/14] Remove the support of SID. We will add it in subcommand `info` in further. --- src/AzureAuth/CommandInfo.cs | 37 ++++++++++++++++++++++-------------- src/AzureAuth/CommandMain.cs | 3 --- src/AzureAuth/OutputMode.cs | 5 ----- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/AzureAuth/CommandInfo.cs b/src/AzureAuth/CommandInfo.cs index 6fdae21b..d9a2d78c 100644 --- a/src/AzureAuth/CommandInfo.cs +++ b/src/AzureAuth/CommandInfo.cs @@ -16,6 +16,19 @@ namespace Microsoft.Authentication.AzureAuth internal class CommandInfo { private const string OptionResetDeviceID = "--reset-device-id"; + private readonly ILogger logger; + private readonly IFileSystem fileSystem; + + /// + /// Initializes a new instance of the class. + /// + /// The logger. + /// The file system. + public CommandInfo(ILogger logger, IFileSystem fileSystem) + { + this.logger = logger; + this.fileSystem = fileSystem; + } /// /// Gets or sets a value indicating whether reset device id. @@ -26,37 +39,33 @@ internal class CommandInfo /// /// This method executes the info process. /// - /// The logger. - /// The file system. /// The error code: 0 is normal execution, and the rest means errors during execution. - public int OnExecute(ILogger logger, IFileSystem fileSystem) + public int OnExecute() { if (this.ResetDeviceID) { - return this.ResetID(logger, fileSystem); + return this.ResetID(); } Assembly assembly = Assembly.GetExecutingAssembly(); string azureauthVersion = assembly.GetName().Version.ToString(); - string deviceID = TelemetryMachineIDHelper.GetRandomDeviceIDAsync(fileSystem).Result; - string deviceIDLocation = TelemetryMachineIDHelper.GetIdentifierLocation(fileSystem); + string deviceID = TelemetryMachineIDHelper.GetRandomDeviceIDAsync(this.fileSystem).Result; + string deviceIDLocation = TelemetryMachineIDHelper.GetIdentifierLocation(this.fileSystem); - logger.LogInformation( + this.logger.LogInformation( $"AzureAuth Version: {azureauthVersion} \n" + $"Device ID: {deviceID} \n" + $"Device ID Path: {deviceIDLocation} \n" + - $"To reset your device identifier, Run `azureauth info {OptionResetDeviceID}` \n" + - $"\n" + - $"To get the user's sid, use option --output=sid. For example:\n" + - $"azureauth --client --scope --tenant --output sid"); + $"To reset your device identifier, Run `azureauth info {OptionResetDeviceID}` \n"); return 0; } - private int ResetID(ILogger logger, IFileSystem fileSystem) + private int ResetID() { - fileSystem.File.Delete(TelemetryMachineIDHelper.GetIdentifierLocation(fileSystem)); - logger.LogInformation($"Device ID was reset."); + string deviceIDPath = TelemetryMachineIDHelper.GetIdentifierLocation(this.fileSystem); + this.fileSystem.File.Delete(deviceIDPath); + this.logger.LogInformation($"Device ID was reset."); return 0; } diff --git a/src/AzureAuth/CommandMain.cs b/src/AzureAuth/CommandMain.cs index d5a153b4..6cbabd21 100644 --- a/src/AzureAuth/CommandMain.cs +++ b/src/AzureAuth/CommandMain.cs @@ -490,9 +490,6 @@ private int GetToken() case OutputMode.Json: Console.Write(tokenResult.ToJson()); break; - case OutputMode.SID: - Console.Write(tokenResult.SID); - break; case OutputMode.None: break; } diff --git a/src/AzureAuth/OutputMode.cs b/src/AzureAuth/OutputMode.cs index 73d0ac12..9c08283c 100644 --- a/src/AzureAuth/OutputMode.cs +++ b/src/AzureAuth/OutputMode.cs @@ -29,10 +29,5 @@ public enum OutputMode /// The none. /// None, - - /// - /// The Security Identifier. - /// - SID, } } From 30f2f48ea8c86f1ff4ebd790a184b3f9f8287b43 Mon Sep 17 00:00:00 2001 From: Rick Tang Date: Fri, 3 Feb 2023 14:27:34 -0800 Subject: [PATCH 07/14] Add unit test. --- src/AzureAuth.Test/CommandInfoTest.cs | 75 +++++++++++++++++++++++++++ src/AzureAuth/CommandInfo.cs | 10 ++-- 2 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 src/AzureAuth.Test/CommandInfoTest.cs diff --git a/src/AzureAuth.Test/CommandInfoTest.cs b/src/AzureAuth.Test/CommandInfoTest.cs new file mode 100644 index 00000000..267e03aa --- /dev/null +++ b/src/AzureAuth.Test/CommandInfoTest.cs @@ -0,0 +1,75 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +namespace AzureAuth.Test +{ + using System; + using System.IO.Abstractions; + using System.IO.Abstractions.TestingHelpers; + using System.Runtime.InteropServices; + using System.Threading.Tasks; + using FluentAssertions; + using Microsoft.Authentication.AzureAuth; + using Microsoft.Authentication.MSALWrapper.AuthFlow; + using Microsoft.Extensions.DependencyInjection; + using Microsoft.Extensions.Logging; + using Microsoft.Office.Lasso.Interfaces; + using Microsoft.Office.Lasso.Telemetry; + using Moq; + using NLog.Extensions.Logging; + using NLog.Targets; + using NUnit.Framework; + + internal class CommandInfoTest + { + private MockFileSystem fileSystem; + private MemoryTarget logTarget; + private Mock envMock; + private ServiceProvider serviceProvider; + + [SetUp] + public void Setup() + { + this.fileSystem = new MockFileSystem(); + + // Setup in memory logging target with NLog - allows making assertions against what has been logged. + var loggingConfig = new NLog.Config.LoggingConfiguration(); + this.logTarget = new MemoryTarget("memory_target"); + this.logTarget.Layout = "${message}"; // Define a simple layout so we don't get timestamps in messages. + loggingConfig.AddTarget(this.logTarget); + loggingConfig.AddRuleForAllLevels(this.logTarget); + + this.envMock = new Mock(MockBehavior.Strict); + + // Setup Dependency Injection container to provide logger and out class under test (the "subject"). + this.serviceProvider = new ServiceCollection() + .AddLogging(loggingBuilder => + { + loggingBuilder.ClearProviders(); + loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); + loggingBuilder.AddNLog(loggingConfig); + }) + .AddSingleton(this.fileSystem) + .AddSingleton(this.envMock.Object) + .AddTransient() + .BuildServiceProvider(); + } + + /// + /// After calling , the device ID should be different with the previous one. + /// + public void TestResetDeviceID() + { + CommandInfo subject = this.serviceProvider.GetService(); + subject.ResetDeviceID = true; + + string deviceIDBeforeReset = TelemetryMachineIDHelper.GetRandomDeviceIDAsync(this.fileSystem).Result; + deviceIDBeforeReset.Should().NotBeNullOrEmpty(); + + subject.OnExecute(); + + string deviceIDAfterReset = TelemetryMachineIDHelper.GetRandomDeviceIDAsync(this.fileSystem).Result; + deviceIDAfterReset.Should().NotBeEquivalentTo(deviceIDBeforeReset); + } + } +} diff --git a/src/AzureAuth/CommandInfo.cs b/src/AzureAuth/CommandInfo.cs index d9a2d78c..2792987f 100644 --- a/src/AzureAuth/CommandInfo.cs +++ b/src/AzureAuth/CommandInfo.cs @@ -13,7 +13,7 @@ namespace Microsoft.Authentication.AzureAuth /// The command shows debug information and system runtime. /// [Command(Name = "info", Description = "Shows AzureAuth debug information. Please provide when asking for help.")] - internal class CommandInfo + public class CommandInfo { private const string OptionResetDeviceID = "--reset-device-id"; private readonly ILogger logger; @@ -44,7 +44,7 @@ public int OnExecute() { if (this.ResetDeviceID) { - return this.ResetID(); + return this.ExecuteResetDeviceID(); } Assembly assembly = Assembly.GetExecutingAssembly(); @@ -61,7 +61,11 @@ public int OnExecute() return 0; } - private int ResetID() + /// + /// Reset the current device ID. + /// + /// The error code: 0 is normal execution, and the rest means errors during execution. + public int ExecuteResetDeviceID() { string deviceIDPath = TelemetryMachineIDHelper.GetIdentifierLocation(this.fileSystem); this.fileSystem.File.Delete(deviceIDPath); From 07da8528a6103f1720c2c1c25ae4ee87e0910ef4 Mon Sep 17 00:00:00 2001 From: Rick Tang Date: Mon, 6 Feb 2023 17:58:31 -0800 Subject: [PATCH 08/14] Update Lasso interface. --- src/AzureAuth.Test/CommandInfoTest.cs | 75 --------------------------- src/AzureAuth/AzureAuth.csproj | 2 +- src/AzureAuth/CommandInfo.cs | 7 +-- 3 files changed, 3 insertions(+), 81 deletions(-) delete mode 100644 src/AzureAuth.Test/CommandInfoTest.cs diff --git a/src/AzureAuth.Test/CommandInfoTest.cs b/src/AzureAuth.Test/CommandInfoTest.cs deleted file mode 100644 index 267e03aa..00000000 --- a/src/AzureAuth.Test/CommandInfoTest.cs +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -namespace AzureAuth.Test -{ - using System; - using System.IO.Abstractions; - using System.IO.Abstractions.TestingHelpers; - using System.Runtime.InteropServices; - using System.Threading.Tasks; - using FluentAssertions; - using Microsoft.Authentication.AzureAuth; - using Microsoft.Authentication.MSALWrapper.AuthFlow; - using Microsoft.Extensions.DependencyInjection; - using Microsoft.Extensions.Logging; - using Microsoft.Office.Lasso.Interfaces; - using Microsoft.Office.Lasso.Telemetry; - using Moq; - using NLog.Extensions.Logging; - using NLog.Targets; - using NUnit.Framework; - - internal class CommandInfoTest - { - private MockFileSystem fileSystem; - private MemoryTarget logTarget; - private Mock envMock; - private ServiceProvider serviceProvider; - - [SetUp] - public void Setup() - { - this.fileSystem = new MockFileSystem(); - - // Setup in memory logging target with NLog - allows making assertions against what has been logged. - var loggingConfig = new NLog.Config.LoggingConfiguration(); - this.logTarget = new MemoryTarget("memory_target"); - this.logTarget.Layout = "${message}"; // Define a simple layout so we don't get timestamps in messages. - loggingConfig.AddTarget(this.logTarget); - loggingConfig.AddRuleForAllLevels(this.logTarget); - - this.envMock = new Mock(MockBehavior.Strict); - - // Setup Dependency Injection container to provide logger and out class under test (the "subject"). - this.serviceProvider = new ServiceCollection() - .AddLogging(loggingBuilder => - { - loggingBuilder.ClearProviders(); - loggingBuilder.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace); - loggingBuilder.AddNLog(loggingConfig); - }) - .AddSingleton(this.fileSystem) - .AddSingleton(this.envMock.Object) - .AddTransient() - .BuildServiceProvider(); - } - - /// - /// After calling , the device ID should be different with the previous one. - /// - public void TestResetDeviceID() - { - CommandInfo subject = this.serviceProvider.GetService(); - subject.ResetDeviceID = true; - - string deviceIDBeforeReset = TelemetryMachineIDHelper.GetRandomDeviceIDAsync(this.fileSystem).Result; - deviceIDBeforeReset.Should().NotBeNullOrEmpty(); - - subject.OnExecute(); - - string deviceIDAfterReset = TelemetryMachineIDHelper.GetRandomDeviceIDAsync(this.fileSystem).Result; - deviceIDAfterReset.Should().NotBeEquivalentTo(deviceIDBeforeReset); - } - } -} diff --git a/src/AzureAuth/AzureAuth.csproj b/src/AzureAuth/AzureAuth.csproj index bc301f59..868bdef3 100644 --- a/src/AzureAuth/AzureAuth.csproj +++ b/src/AzureAuth/AzureAuth.csproj @@ -39,7 +39,7 @@ - + diff --git a/src/AzureAuth/CommandInfo.cs b/src/AzureAuth/CommandInfo.cs index 2792987f..580a5b8d 100644 --- a/src/AzureAuth/CommandInfo.cs +++ b/src/AzureAuth/CommandInfo.cs @@ -49,13 +49,11 @@ public int OnExecute() Assembly assembly = Assembly.GetExecutingAssembly(); string azureauthVersion = assembly.GetName().Version.ToString(); - string deviceID = TelemetryMachineIDHelper.GetRandomDeviceIDAsync(this.fileSystem).Result; - string deviceIDLocation = TelemetryMachineIDHelper.GetIdentifierLocation(this.fileSystem); + string deviceID = TelemetryDeviceID.GetAsync(this.fileSystem).Result; this.logger.LogInformation( $"AzureAuth Version: {azureauthVersion} \n" + $"Device ID: {deviceID} \n" + - $"Device ID Path: {deviceIDLocation} \n" + $"To reset your device identifier, Run `azureauth info {OptionResetDeviceID}` \n"); return 0; @@ -67,8 +65,7 @@ public int OnExecute() /// The error code: 0 is normal execution, and the rest means errors during execution. public int ExecuteResetDeviceID() { - string deviceIDPath = TelemetryMachineIDHelper.GetIdentifierLocation(this.fileSystem); - this.fileSystem.File.Delete(deviceIDPath); + TelemetryDeviceID.Delete(this.fileSystem); this.logger.LogInformation($"Device ID was reset."); return 0; From 0e752332c15be1e02b94c6e3650600c79d032622 Mon Sep 17 00:00:00 2001 From: Rick Tang Date: Tue, 7 Feb 2023 17:07:29 -0800 Subject: [PATCH 09/14] change `reset-device-id` to subcommand. --- src/AzureAuth/CommandInfo.cs | 74 ------------------- src/AzureAuth/CommandMain.cs | 1 + src/AzureAuth/Commands/CommandInfo.cs | 40 ++++++++++ .../Commands/Info/CommandInfoResetDeviceID.cs | 31 ++++++++ 4 files changed, 72 insertions(+), 74 deletions(-) delete mode 100644 src/AzureAuth/CommandInfo.cs create mode 100644 src/AzureAuth/Commands/CommandInfo.cs create mode 100644 src/AzureAuth/Commands/Info/CommandInfoResetDeviceID.cs diff --git a/src/AzureAuth/CommandInfo.cs b/src/AzureAuth/CommandInfo.cs deleted file mode 100644 index 580a5b8d..00000000 --- a/src/AzureAuth/CommandInfo.cs +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -namespace Microsoft.Authentication.AzureAuth -{ - using System.IO.Abstractions; - using System.Reflection; - using McMaster.Extensions.CommandLineUtils; - using Microsoft.Extensions.Logging; - using Microsoft.Office.Lasso.Telemetry; - - /// - /// The command shows debug information and system runtime. - /// - [Command(Name = "info", Description = "Shows AzureAuth debug information. Please provide when asking for help.")] - public class CommandInfo - { - private const string OptionResetDeviceID = "--reset-device-id"; - private readonly ILogger logger; - private readonly IFileSystem fileSystem; - - /// - /// Initializes a new instance of the class. - /// - /// The logger. - /// The file system. - public CommandInfo(ILogger logger, IFileSystem fileSystem) - { - this.logger = logger; - this.fileSystem = fileSystem; - } - - /// - /// Gets or sets a value indicating whether reset device id. - /// - [Option(OptionResetDeviceID, "Reset Device ID", CommandOptionType.NoValue)] - public bool ResetDeviceID { get; set; } - - /// - /// This method executes the info process. - /// - /// The error code: 0 is normal execution, and the rest means errors during execution. - public int OnExecute() - { - if (this.ResetDeviceID) - { - return this.ExecuteResetDeviceID(); - } - - Assembly assembly = Assembly.GetExecutingAssembly(); - string azureauthVersion = assembly.GetName().Version.ToString(); - string deviceID = TelemetryDeviceID.GetAsync(this.fileSystem).Result; - - this.logger.LogInformation( - $"AzureAuth Version: {azureauthVersion} \n" + - $"Device ID: {deviceID} \n" + - $"To reset your device identifier, Run `azureauth info {OptionResetDeviceID}` \n"); - - return 0; - } - - /// - /// Reset the current device ID. - /// - /// The error code: 0 is normal execution, and the rest means errors during execution. - public int ExecuteResetDeviceID() - { - TelemetryDeviceID.Delete(this.fileSystem); - this.logger.LogInformation($"Device ID was reset."); - - return 0; - } - } -} diff --git a/src/AzureAuth/CommandMain.cs b/src/AzureAuth/CommandMain.cs index 6cbabd21..a77a9fb0 100644 --- a/src/AzureAuth/CommandMain.cs +++ b/src/AzureAuth/CommandMain.cs @@ -11,6 +11,7 @@ namespace Microsoft.Authentication.AzureAuth using System.Threading; using System.Threading.Tasks; using McMaster.Extensions.CommandLineUtils; + using Microsoft.Authentication.AzureAuth.Commands; using Microsoft.Authentication.MSALWrapper; using Microsoft.Authentication.MSALWrapper.AuthFlow; using Microsoft.Extensions.Logging; diff --git a/src/AzureAuth/Commands/CommandInfo.cs b/src/AzureAuth/Commands/CommandInfo.cs new file mode 100644 index 00000000..022bbcda --- /dev/null +++ b/src/AzureAuth/Commands/CommandInfo.cs @@ -0,0 +1,40 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +namespace Microsoft.Authentication.AzureAuth.Commands +{ + using System.IO.Abstractions; + using System.Reflection; + using McMaster.Extensions.CommandLineUtils; + using Microsoft.Authentication.AzureAuth.Commands.Info; + using Microsoft.Extensions.Logging; + using Microsoft.Office.Lasso.Telemetry; + + /// + /// The command shows debug information and system runtime. + /// + [Command(Name = "info", Description = "Shows AzureAuth debug information. Please provide when asking for help.")] + [Subcommand(typeof(CommandResetDeviceID))] + public class CommandInfo + { + /// + /// This method executes the info process. + /// + /// The logger. + /// The file system. + /// The error code: 0 is normal execution, and the rest means errors during execution. + public int OnExecute(ILogger logger, IFileSystem fileSystem) + { + Assembly assembly = Assembly.GetExecutingAssembly(); + string azureauthVersion = assembly.GetName().Version.ToString(); + string deviceID = TelemetryDeviceID.GetAsync(fileSystem).Result; + + logger.LogInformation( + $"AzureAuth Version: {azureauthVersion} \n" + + $"Device ID: {deviceID} \n" + + $"To reset your device identifier, Run `azureauth info reset-device-id`"); + + return 0; + } + } +} diff --git a/src/AzureAuth/Commands/Info/CommandInfoResetDeviceID.cs b/src/AzureAuth/Commands/Info/CommandInfoResetDeviceID.cs new file mode 100644 index 00000000..5949b8c0 --- /dev/null +++ b/src/AzureAuth/Commands/Info/CommandInfoResetDeviceID.cs @@ -0,0 +1,31 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +namespace Microsoft.Authentication.AzureAuth.Commands.Info +{ + using System.IO.Abstractions; + using McMaster.Extensions.CommandLineUtils; + using Microsoft.Extensions.Logging; + using Microsoft.Office.Lasso.Telemetry; + + /// + /// The command reset the device ID in local storage. + /// + [Command(Name = "reset-device-id", Description = "Reset your device identifier.")] + public class CommandResetDeviceID + { + /// + /// This method executes the reset device ID process. + /// + /// The logger. + /// The file system. + /// The error code: 0 is normal execution, and the rest means errors during execution. + public int OnExecute(ILogger logger, IFileSystem fileSystem) + { + TelemetryDeviceID.Delete(fileSystem); + logger.LogInformation($"Device ID was reset."); + + return 0; + } + } +} From 3819833c25b4b0b18e45e8bf7a28b0a569bfc37c Mon Sep 17 00:00:00 2001 From: Rick Tang Date: Tue, 7 Feb 2023 17:10:41 -0800 Subject: [PATCH 10/14] It's time to update the change log. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b86da0a..2e3b3cc9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Added On-premises Security Identifier in Telemetry as `sid`. -- Added Subcommand `info` and its option `--reset-device-id` to show or reset the device ID. +- Added Subcommand `info` and its subcommand `reset-device-id` to show or reset the device ID. ### Changed - Upgrade MSAL to 4.47.2 and opt-in native WAM mode. From 864b8fbbd5ea63eb6601aa7605f20a2758e97dab Mon Sep 17 00:00:00 2001 From: Rick Tang Date: Wed, 8 Feb 2023 10:40:11 -0800 Subject: [PATCH 11/14] add missing command. --- src/AzureAuth/Commands/CommandAzureAuth.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/AzureAuth/Commands/CommandAzureAuth.cs b/src/AzureAuth/Commands/CommandAzureAuth.cs index 96e40f08..9d7a9355 100644 --- a/src/AzureAuth/Commands/CommandAzureAuth.cs +++ b/src/AzureAuth/Commands/CommandAzureAuth.cs @@ -11,6 +11,7 @@ namespace Microsoft.Authentication.AzureAuth.Commands [Command(Name = "azureauth", Description = "A CLI interface to MSAL (Microsoft Authentication Library)")] [Subcommand(typeof(CommandAad))] [Subcommand(typeof(CommandAdo))] + [Subcommand(typeof(CommandInfo))] public class CommandAzureAuth { /// From 39d0ee1700f228de15c913e87854e5f6b0658bf6 Mon Sep 17 00:00:00 2001 From: goagain Date: Wed, 8 Feb 2023 10:55:34 -0800 Subject: [PATCH 12/14] Apply suggestions from code review Co-authored-by: Kyle W. Rader --- src/AzureAuth/Commands/Info/CommandInfoResetDeviceID.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AzureAuth/Commands/Info/CommandInfoResetDeviceID.cs b/src/AzureAuth/Commands/Info/CommandInfoResetDeviceID.cs index 5949b8c0..01d351b0 100644 --- a/src/AzureAuth/Commands/Info/CommandInfoResetDeviceID.cs +++ b/src/AzureAuth/Commands/Info/CommandInfoResetDeviceID.cs @@ -11,7 +11,7 @@ namespace Microsoft.Authentication.AzureAuth.Commands.Info /// /// The command reset the device ID in local storage. /// - [Command(Name = "reset-device-id", Description = "Reset your device identifier.")] + [Command(Name = "reset-device-id", Description = "Reset your AzureAuth telemetry device identifier.")] public class CommandResetDeviceID { /// @@ -23,7 +23,7 @@ public class CommandResetDeviceID public int OnExecute(ILogger logger, IFileSystem fileSystem) { TelemetryDeviceID.Delete(fileSystem); - logger.LogInformation($"Device ID was reset."); + logger.LogSuccess($"Telemetry Device ID was reset."); return 0; } From 1190bcf43fa3769559c4b3bba25515aa2a72e1ad Mon Sep 17 00:00:00 2001 From: Rick Tang Date: Wed, 8 Feb 2023 10:57:48 -0800 Subject: [PATCH 13/14] add using for LogSuccess --- src/AzureAuth/Commands/Info/CommandInfoResetDeviceID.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/AzureAuth/Commands/Info/CommandInfoResetDeviceID.cs b/src/AzureAuth/Commands/Info/CommandInfoResetDeviceID.cs index 01d351b0..5e25444f 100644 --- a/src/AzureAuth/Commands/Info/CommandInfoResetDeviceID.cs +++ b/src/AzureAuth/Commands/Info/CommandInfoResetDeviceID.cs @@ -6,6 +6,7 @@ namespace Microsoft.Authentication.AzureAuth.Commands.Info using System.IO.Abstractions; using McMaster.Extensions.CommandLineUtils; using Microsoft.Extensions.Logging; + using Microsoft.Office.Lasso.Extensions; using Microsoft.Office.Lasso.Telemetry; /// From 30302104032aa9f5376d4aef916f0d9d056944de Mon Sep 17 00:00:00 2001 From: Rick Tang Date: Wed, 8 Feb 2023 11:00:57 -0800 Subject: [PATCH 14/14] Apply code suggestion. --- src/AzureAuth/Commands/CommandInfo.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/AzureAuth/Commands/CommandInfo.cs b/src/AzureAuth/Commands/CommandInfo.cs index 022bbcda..3cb4b5d3 100644 --- a/src/AzureAuth/Commands/CommandInfo.cs +++ b/src/AzureAuth/Commands/CommandInfo.cs @@ -29,10 +29,12 @@ public int OnExecute(ILogger logger, IFileSystem fileSystem) string azureauthVersion = assembly.GetName().Version.ToString(); string deviceID = TelemetryDeviceID.GetAsync(fileSystem).Result; - logger.LogInformation( - $"AzureAuth Version: {azureauthVersion} \n" + - $"Device ID: {deviceID} \n" + - $"To reset your device identifier, Run `azureauth info reset-device-id`"); + logger.LogInformation($"AzureAuth Version: {azureauthVersion}"); + logger.LogInformation($"Telemetry Device ID: {deviceID}"); + logger.LogInformation(string.Empty); + + logger.LogInformation("To reset your device identifier run the following command:"); + logger.LogInformation(" azureauth info reset-device-id"); return 0; }