Skip to content

Commit 79189f2

Browse files
author
Maddie Clayton
authored
Merge pull request #131 from maddieclayton/movetests
Move test coverage for common code into powershell-common repo
2 parents 9c19eef + 9c35b72 commit 79189f2

7 files changed

+799
-0
lines changed

Azure.PowerShell.Common.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Probe.Test", "src\Probe.Tes
3535
EndProject
3636
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Strategies.Test", "src\Strategies.Test\Strategies.Test.csproj", "{6756A7F2-1141-4065-BA23-0C555D2A2BC3}"
3737
EndProject
38+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ResourceManager.Test", "src\ResourceManager.Test\ResourceManager.Test.csproj", "{B571E523-6A04-4EFF-8E89-730078D7FBD1}"
39+
EndProject
3840
Global
3941
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4042
Debug|Any CPU = Debug|Any CPU
@@ -101,13 +103,18 @@ Global
101103
{6756A7F2-1141-4065-BA23-0C555D2A2BC3}.Debug|Any CPU.Build.0 = Debug|Any CPU
102104
{6756A7F2-1141-4065-BA23-0C555D2A2BC3}.Release|Any CPU.ActiveCfg = Release|Any CPU
103105
{6756A7F2-1141-4065-BA23-0C555D2A2BC3}.Release|Any CPU.Build.0 = Release|Any CPU
106+
{B571E523-6A04-4EFF-8E89-730078D7FBD1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
107+
{B571E523-6A04-4EFF-8E89-730078D7FBD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
108+
{B571E523-6A04-4EFF-8E89-730078D7FBD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
109+
{B571E523-6A04-4EFF-8E89-730078D7FBD1}.Release|Any CPU.Build.0 = Release|Any CPU
104110
EndGlobalSection
105111
GlobalSection(SolutionProperties) = preSolution
106112
HideSolutionNode = FALSE
107113
EndGlobalSection
108114
GlobalSection(NestedProjects) = preSolution
109115
{3B48A77B-5956-4A62-9081-92BA04B02B27} = {625CE04D-FD62-471D-BBCF-E7B716B5FE56}
110116
{6756A7F2-1141-4065-BA23-0C555D2A2BC3} = {625CE04D-FD62-471D-BBCF-E7B716B5FE56}
117+
{B571E523-6A04-4EFF-8E89-730078D7FBD1} = {625CE04D-FD62-471D-BBCF-E7B716B5FE56}
111118
EndGlobalSection
112119
GlobalSection(ExtensibilityGlobals) = postSolution
113120
SolutionGuid = {D4C68FE4-3D0C-4F70-B5BA-499E0C0F177E}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.Commands.Common.Compute.Tests
16+
{
17+
using System;
18+
using System.Collections.Generic;
19+
using System.IO;
20+
using Microsoft.IdentityModel.Clients.ActiveDirectory;
21+
using Microsoft.Rest;
22+
23+
class CredentialManager
24+
{
25+
protected CredentialManager() { }
26+
27+
private const string ServicePrincipalEnvVariableName = "AZURE_SERVICE_PRINCIPAL";
28+
29+
private static readonly string UserId = "UserId".ToLower();
30+
private static readonly string Password = "Password".ToLower();
31+
private static readonly string AadTenant = "AADTenant".ToLower();
32+
private static readonly string Subscription = "SubscriptionId".ToLower();
33+
private const string AuthUrl = "https://login.microsoftonline.com/";
34+
private const string BaseUrl = "https://management.azure.com/";
35+
36+
public string ApplicationId { get; private set; }
37+
public string ApplicationSecret { get; private set; }
38+
public string TenantId { get; private set; }
39+
public string SubscriptionId { get; private set; }
40+
41+
public TokenCredentials TokenCredentials
42+
{
43+
get
44+
{
45+
var clientCredential = new ClientCredential(ApplicationId, ApplicationSecret);
46+
var context = new AuthenticationContext(Path.Combine(AuthUrl, TenantId));
47+
var result = context.AcquireTokenAsync(BaseUrl, clientCredential);
48+
49+
if (result == null) throw new InvalidOperationException("Failed to obtain the token");
50+
51+
return new TokenCredentials(result.Result.AccessToken);
52+
}
53+
}
54+
55+
public static CredentialManager FromServicePrincipalEnvVariable(string envVariableName = ServicePrincipalEnvVariableName)
56+
{
57+
//AZURE_SERVICE_PRINCIPAL = UserId =< UserGuid >; Password =< Password >; AADTenant =< TenantGuid >; SubscriptionId =< SubscriptionId >
58+
var spString = Environment.GetEnvironmentVariable(envVariableName);
59+
60+
if (spString == null) throw new ArgumentNullException($"Failed to get environment variable {envVariableName}");
61+
62+
var sp = new Dictionary<string, string>();
63+
var pairs = spString.Trim().Split(';');
64+
foreach (var pair in pairs)
65+
{
66+
var keyVal = pair.Trim().Split(new[] { '=' }, 2);
67+
if (keyVal.Length < 2) throw new ArgumentException($"Failed to parse {envVariableName}");
68+
sp.Add(keyVal[0].Trim().ToLower(), keyVal[1].Trim());
69+
}
70+
71+
if (!sp.ContainsKey(UserId.ToLower())) throw new ArgumentException($"Failed to find {UserId} in {envVariableName}");
72+
if (!sp.ContainsKey(Password.ToLower())) throw new ArgumentException($"Failed to find {Password} in {envVariableName}");
73+
if (!sp.ContainsKey(AadTenant.ToLower())) throw new ArgumentException($"Failed to find {AadTenant} in {envVariableName}");
74+
if (!sp.ContainsKey(Subscription.ToLower())) throw new ArgumentException($"Failed to find {Subscription} in {envVariableName}");
75+
76+
var credentialManager = new CredentialManager
77+
{
78+
ApplicationId = sp[UserId],
79+
ApplicationSecret = sp[Password],
80+
TenantId = sp[AadTenant],
81+
SubscriptionId = sp[Subscription]
82+
};
83+
84+
return credentialManager;
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)