-
Notifications
You must be signed in to change notification settings - Fork 19
Extend support to read auth mode from environment variables for ado subcommands. #424
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
06fb499
Read auth mode from env for all sub commands
mvanchaa 655a982
Update changelog
mvanchaa 920c154
Use env extension class instead of creating a helper class
mvanchaa 75bc8f8
remove eventdata object from read env var method
mvanchaa 0b276f5
Fix tests
mvanchaa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| using FluentAssertions; | ||
| using Microsoft.Authentication.AzureAuth; | ||
| using Microsoft.Authentication.MSALWrapper; | ||
| using Microsoft.Authentication.TestHelper; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Office.Lasso.Interfaces; | ||
| using Microsoft.Office.Lasso.Telemetry; | ||
| using Moq; | ||
| using NLog.Targets; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace AzureAuth.Test | ||
| { | ||
| public class AuthModeHelperTest | ||
| { | ||
| private Mock<IEnv> envMock; | ||
| private ILogger logger; | ||
| private MemoryTarget logTarget; | ||
|
|
||
| [SetUp] | ||
| public void SetUp() | ||
| { | ||
| this.envMock = new Mock<IEnv>(); | ||
| (this.logger, this.logTarget) = MemoryLogger.Create(); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ReadAuthModeFromEnvOrSetDefault_ReturnsDefault_WhenEnvVarIsEmpty() | ||
| { | ||
| // Arrange | ||
| envMock.Setup(e => e.Get(It.IsAny<string>())).Returns(string.Empty); | ||
|
|
||
| // Act | ||
| var result = AuthModeHelper.ReadAuthModeFromEnvOrSetDefault(envMock.Object, new EventData(), logger); | ||
|
|
||
| // Assert | ||
| result.Should().BeEquivalentTo(new[] { AuthMode.Default }); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ReadAuthModeFromEnvOrSetDefault_ReturnsParsedAuthModes_WhenEnvVarIsValid() | ||
| { | ||
| // Arrange | ||
| envMock.Setup(e => e.Get(It.IsAny<string>())).Returns("Web,Broker"); | ||
|
|
||
| // Act | ||
| var result = AuthModeHelper.ReadAuthModeFromEnvOrSetDefault(envMock.Object, new EventData(), logger); | ||
|
|
||
| // Assert | ||
| result.Should().BeEquivalentTo(new[] { AuthMode.Web, AuthMode.Broker }); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ReadAuthModeFromEnvOrSetDefault_LogsErrorAndReturnsEmpty_WhenEnvVarIsInvalid() | ||
| { | ||
| // Arrange | ||
| envMock.Setup(e => e.Get(It.IsAny<string>())).Returns("InvalidMode"); | ||
|
|
||
| // Act | ||
| var result = AuthModeHelper.ReadAuthModeFromEnvOrSetDefault(envMock.Object, new EventData(), logger); | ||
|
|
||
| // Assert | ||
| result.Should().BeEmpty(); | ||
| this.logTarget.Logs.Should().ContainMatch("Invalid value specified for environment variable*"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ReadAuthModeFromEnvOrSetDefault_AddsEventData_WhenEnvVarIsValid() | ||
| { | ||
| // Arrange | ||
| var eventData = new EventData(); | ||
| envMock.Setup(e => e.Get(It.IsAny<string>())).Returns("Web"); | ||
|
|
||
| // Act | ||
| var result = AuthModeHelper.ReadAuthModeFromEnvOrSetDefault(envMock.Object, eventData, logger); | ||
|
|
||
| // Assert | ||
| var env_var = $"env_{EnvVars.AuthMode}"; | ||
| eventData.Properties[env_var.ToLower()].Should().Be("Web"); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| using Microsoft.Authentication.AzureAuth.Commands; | ||
| using Microsoft.Authentication.MSALWrapper; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Office.Lasso.Interfaces; | ||
| using Microsoft.Office.Lasso.Telemetry; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Microsoft.Authentication.AzureAuth | ||
| { | ||
| /// <summary> | ||
| /// Helper class for <see cref="AuthMode"/>s. | ||
| /// </summary> | ||
| public static class AuthModeHelper | ||
| { | ||
| /// <summary> | ||
| /// Get the auth modes from the environment or set the default. | ||
| /// </summary> | ||
| /// <param name="env">The <see cref="IEnv"/> to use.</param> | ||
| /// <param name="eventData">Event data to add the auth mode to.</param> | ||
| /// <param name="logger">The <see cref="ILogger"/> to use.</param> | ||
| /// <returns>AuthModes.</returns> | ||
| public static IEnumerable<AuthMode> ReadAuthModeFromEnvOrSetDefault(IEnv env, EventData eventData, ILogger logger) | ||
| { | ||
| var authModesFromEnv = env.Get(EnvVars.AuthMode); | ||
|
|
||
| // If auth modes are not specified in the environment, then return the default. | ||
| if (string.IsNullOrEmpty(authModesFromEnv)) | ||
| { | ||
| return new[] { AuthMode.Default }; | ||
| } | ||
|
|
||
| var result = new List<AuthMode>(); | ||
| foreach (var val in authModesFromEnv.Split(',')) | ||
| { | ||
| if (Enum.TryParse<AuthMode>(val, ignoreCase: true, out var mode)) | ||
| { | ||
| result.Add(mode); | ||
| } | ||
| else | ||
| { | ||
| logger.LogError($"Invalid value specified for environment variable {EnvVars.AuthMode}. Allowed values are: {CommandAad.AuthModeHelperText}"); | ||
| return new List<AuthMode>(); | ||
| } | ||
| } | ||
|
|
||
| eventData.Add($"env_{EnvVars.AuthMode}", authModesFromEnv); | ||
| return result; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.