Skip to content

Commit

Permalink
Merge pull request #131 from maddieclayton/movetests
Browse files Browse the repository at this point in the history
Move test coverage for common code into powershell-common repo
  • Loading branch information
Maddie Clayton authored Jan 30, 2019
2 parents 9c19eef + 9c35b72 commit 79189f2
Show file tree
Hide file tree
Showing 7 changed files with 799 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Azure.PowerShell.Common.sln
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Probe.Test", "src\Probe.Tes
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Strategies.Test", "src\Strategies.Test\Strategies.Test.csproj", "{6756A7F2-1141-4065-BA23-0C555D2A2BC3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ResourceManager.Test", "src\ResourceManager.Test\ResourceManager.Test.csproj", "{B571E523-6A04-4EFF-8E89-730078D7FBD1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -101,13 +103,18 @@ Global
{6756A7F2-1141-4065-BA23-0C555D2A2BC3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6756A7F2-1141-4065-BA23-0C555D2A2BC3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6756A7F2-1141-4065-BA23-0C555D2A2BC3}.Release|Any CPU.Build.0 = Release|Any CPU
{B571E523-6A04-4EFF-8E89-730078D7FBD1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B571E523-6A04-4EFF-8E89-730078D7FBD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B571E523-6A04-4EFF-8E89-730078D7FBD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B571E523-6A04-4EFF-8E89-730078D7FBD1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{3B48A77B-5956-4A62-9081-92BA04B02B27} = {625CE04D-FD62-471D-BBCF-E7B716B5FE56}
{6756A7F2-1141-4065-BA23-0C555D2A2BC3} = {625CE04D-FD62-471D-BBCF-E7B716B5FE56}
{B571E523-6A04-4EFF-8E89-730078D7FBD1} = {625CE04D-FD62-471D-BBCF-E7B716B5FE56}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D4C68FE4-3D0C-4F70-B5BA-499E0C0F177E}
Expand Down
87 changes: 87 additions & 0 deletions src/ResourceManager.Test/CredentialManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.Common.Compute.Tests
{
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest;

class CredentialManager
{
protected CredentialManager() { }

private const string ServicePrincipalEnvVariableName = "AZURE_SERVICE_PRINCIPAL";

private static readonly string UserId = "UserId".ToLower();
private static readonly string Password = "Password".ToLower();
private static readonly string AadTenant = "AADTenant".ToLower();
private static readonly string Subscription = "SubscriptionId".ToLower();
private const string AuthUrl = "https://login.microsoftonline.com/";
private const string BaseUrl = "https://management.azure.com/";

public string ApplicationId { get; private set; }
public string ApplicationSecret { get; private set; }
public string TenantId { get; private set; }
public string SubscriptionId { get; private set; }

public TokenCredentials TokenCredentials
{
get
{
var clientCredential = new ClientCredential(ApplicationId, ApplicationSecret);
var context = new AuthenticationContext(Path.Combine(AuthUrl, TenantId));
var result = context.AcquireTokenAsync(BaseUrl, clientCredential);

if (result == null) throw new InvalidOperationException("Failed to obtain the token");

return new TokenCredentials(result.Result.AccessToken);
}
}

public static CredentialManager FromServicePrincipalEnvVariable(string envVariableName = ServicePrincipalEnvVariableName)
{
//AZURE_SERVICE_PRINCIPAL = UserId =< UserGuid >; Password =< Password >; AADTenant =< TenantGuid >; SubscriptionId =< SubscriptionId >
var spString = Environment.GetEnvironmentVariable(envVariableName);

if (spString == null) throw new ArgumentNullException($"Failed to get environment variable {envVariableName}");

var sp = new Dictionary<string, string>();
var pairs = spString.Trim().Split(';');
foreach (var pair in pairs)
{
var keyVal = pair.Trim().Split(new[] { '=' }, 2);
if (keyVal.Length < 2) throw new ArgumentException($"Failed to parse {envVariableName}");
sp.Add(keyVal[0].Trim().ToLower(), keyVal[1].Trim());
}

if (!sp.ContainsKey(UserId.ToLower())) throw new ArgumentException($"Failed to find {UserId} in {envVariableName}");
if (!sp.ContainsKey(Password.ToLower())) throw new ArgumentException($"Failed to find {Password} in {envVariableName}");
if (!sp.ContainsKey(AadTenant.ToLower())) throw new ArgumentException($"Failed to find {AadTenant} in {envVariableName}");
if (!sp.ContainsKey(Subscription.ToLower())) throw new ArgumentException($"Failed to find {Subscription} in {envVariableName}");

var credentialManager = new CredentialManager
{
ApplicationId = sp[UserId],
ApplicationSecret = sp[Password],
TenantId = sp[AadTenant],
SubscriptionId = sp[Subscription]
};

return credentialManager;
}
}
}
Loading

0 comments on commit 79189f2

Please sign in to comment.