Skip to content

Implemented Profile ParameterSet #181

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 19 commits into from
Mar 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.2.0.2\lib\net45\Microsoft.Azure.Common.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication">
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.8-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.13-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common.Storage/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<packages>
<package id="Hyak.Common" version="1.0.2" targetFramework="net45" />
<package id="Microsoft.Azure.Common" version="2.0.2" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.0.13-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.0.8-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.12.0-preview" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />
Expand Down
6 changes: 3 additions & 3 deletions src/Common/Commands.Common.Test/Commands.Common.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@
<Reference Include="Microsoft.Azure.Common">
<HintPath>..\..\packages\Microsoft.Azure.Common.2.0.2\lib\net45\Microsoft.Azure.Common.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication">
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.8-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.13-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework">
<HintPath>..\..\packages\Microsoft.Azure.Common.2.0.2\lib\net45\Microsoft.Azure.Common.NetFramework.dll</HintPath>
Expand Down Expand Up @@ -155,7 +155,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Common\AuthenticationFactoryTests.cs" />
<Compile Include="Common\ProfileCmdltsTests.cs" />
<Compile Include="Common\MockSubsciptionFactory.cs" />
<Compile Include="Common\ServicePrincipalStoreTests.cs" />
<Compile Include="Common\GeneralUtilitiesTests.cs" />
<Compile Include="Common\Data.cs" />
Expand Down
79 changes: 79 additions & 0 deletions src/Common/Commands.Common.Test/Common/MockSubsciptionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// ----------------------------------------------------------------------------------
//
// 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.
// ----------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Common.Authentication;
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;

namespace Microsoft.WindowsAzure.Commands.Common.Test.Common
{
public class MockSubsciptionFactory : IClientFactory
{
public void AddAction(Azure.Common.Authentication.Models.IClientAction action)
{
throw new NotImplementedException();
}

public TClient CreateClient<TClient>(Azure.Common.Authentication.Models.AzureProfile profile, Azure.Common.Authentication.Models.AzureSubscription subscription, Azure.Common.Authentication.Models.AzureEnvironment.Endpoint endpoint) where TClient : Hyak.Common.ServiceClient<TClient>
{
throw new NotImplementedException();
}

public TClient CreateClient<TClient>(Azure.Common.Authentication.Models.AzureProfile profile, Azure.Common.Authentication.Models.AzureEnvironment.Endpoint endpoint) where TClient : Hyak.Common.ServiceClient<TClient>
{
throw new NotImplementedException();
}

public TClient CreateClient<TClient>(Azure.Common.Authentication.Models.AzureContext context, Azure.Common.Authentication.Models.AzureEnvironment.Endpoint endpoint) where TClient : Hyak.Common.ServiceClient<TClient>
{
throw new NotImplementedException();
}

public TClient CreateCustomClient<TClient>(params object[] parameters) where TClient : Hyak.Common.ServiceClient<TClient>
{
throw new NotImplementedException();
}

public System.Net.Http.HttpClient CreateHttpClient(string endpoint, System.Net.Http.HttpMessageHandler effectiveHandler)
{
throw new NotImplementedException();
}

public System.Net.Http.HttpClient CreateHttpClient(string endpoint, System.Net.ICredentials credentials)
{
throw new NotImplementedException();
}

public void RemoveAction(Type actionType)
{
throw new NotImplementedException();
}

public List<System.Net.Http.Headers.ProductInfoHeaderValue> UserAgents
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public MockTokenAuthenticationFactory(string userId, string accessToken)
LoginType = LoginType.OrgId,
AccessToken = accessToken
};

TokenProvider = ((account, environment, tenant) => Token);
}

public IAccessToken Authenticate(AzureAccount account, AzureEnvironment environment, string tenant, SecureString password, ShowDialog promptBehavior,
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Commands.Common.Test/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<packages>
<package id="Hyak.Common" version="1.0.2" targetFramework="net45" />
<package id="Microsoft.Azure.Common" version="2.0.2" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.0.13-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.0.8-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.12.0-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Test.HttpRecorder" version="1.0.5513.27084-prerelease" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
Expand Down
82 changes: 76 additions & 6 deletions src/Common/Commands.Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using Microsoft.Azure.Common.Authentication;
using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.WindowsAzure.Commands.Common;
using Microsoft.WindowsAzure.Commands.Common.Properties;
using System;
Expand All @@ -30,6 +31,14 @@ public abstract class AzurePSCmdlet : PSCmdlet
[Parameter(Mandatory = false, HelpMessage = "In-memory profile.")]
public AzureProfile Profile { get; set; }

public static AzureProfile CurrentProfile { get; set; }

protected static TokenCache DefaultDiskTokenCache { get; set; }

protected static TokenCache DefaultMemoryTokenCache { get; set; }

protected static AzureProfile DefaultProfile { get; set; }

static AzurePSCmdlet()
{
if (!TestMockSupport.RunningMocked)
Expand All @@ -38,6 +47,66 @@ static AzurePSCmdlet()
}

AzureSession.ClientFactory.UserAgents.Add(AzurePowerShell.UserAgentValue);
if (!TestMockSupport.RunningMocked)
{
InitializeTokenCaches();
DefaultProfile = InitializeDefaultProfile();
CurrentProfile = DefaultProfile;
UpdateSessionStateForProfile(CurrentProfile);
AzureSession.DataStore = new DiskDataStore();
}
}

/// <summary>
/// Create the default profile, based on the default profile path
/// </summary>
/// <returns>The default prpofile, serialized from the default location on disk</returns>
protected static AzureProfile InitializeDefaultProfile()
{
if (!string.IsNullOrEmpty(AzureSession.ProfileDirectory) && !string.IsNullOrEmpty(AzureSession.ProfileFile))
{
try
{
return new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
}
catch
{
// swallow exceptions in creating the profile from disk
}
}

return new AzureProfile();
}

protected static void InitializeTokenCaches()
{
DefaultMemoryTokenCache = new TokenCache();
if (!string.IsNullOrWhiteSpace(AzureSession.ProfileDirectory) &&
!string.IsNullOrWhiteSpace(AzureSession.TokenCacheFile))
{
DefaultDiskTokenCache = new ProtectedFileTokenCache(Path.Combine(AzureSession.ProfileDirectory, AzureSession.TokenCacheFile));
}
else
{
DefaultDiskTokenCache = DefaultMemoryTokenCache;
}
}

/// <summary>
/// Update the token cache when setting the profile
/// </summary>
/// <param name="profile"></param>
protected static void UpdateSessionStateForProfile(AzureProfile profile)
{
var defaultProfilePath = Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile);
if (string.Equals(profile.ProfilePath, defaultProfilePath, StringComparison.OrdinalIgnoreCase))
{
AzureSession.TokenCache = DefaultDiskTokenCache;
}
else
{
AzureSession.TokenCache = DefaultMemoryTokenCache;
}
}

/// <summary>
Expand All @@ -46,7 +115,6 @@ static AzurePSCmdlet()
protected override void BeginProcessing()
{
InitializeProfile();

if (string.IsNullOrEmpty(ParameterSetName))
{
WriteDebugWithTimestamp(string.Format(Resources.BeginProcessingWithoutParameterSetLog, this.GetType().Name));
Expand All @@ -66,15 +134,17 @@ protected override void BeginProcessing()
base.BeginProcessing();
}

/// <summary>
/// Ensure that there is a profile for the command
/// </summary>
private void InitializeProfile()
{
// Load profile from disk
var profileFromDisk = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
if (Profile == null ||
Profile.ProfilePath == profileFromDisk.ProfilePath)
if (Profile == null)
{
Profile = profileFromDisk;
Profile = AzurePSCmdlet.CurrentProfile;
}

UpdateSessionStateForProfile(Profile);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Common/Commands.Common/Commands.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.2.0.2\lib\net45\Microsoft.Azure.Common.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.Authentication">
<Reference Include="Microsoft.Azure.Common.Authentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.8-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
<HintPath>..\..\packages\Microsoft.Azure.Common.Authentication.1.0.13-preview\lib\net45\Microsoft.Azure.Common.Authentication.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Azure.Common.NetFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down
47 changes: 46 additions & 1 deletion src/Common/Commands.Common/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/Common/Commands.Common/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1355,4 +1355,19 @@ use and privacy statement at &lt;url&gt; and (c) agree to sharing my contact inf
<data name="InvalidDefaultSubscription" xml:space="preserve">
<value>No default subscription has been designated. Use Select-AzureSubscription -Default &lt;subscriptionName&gt; to set the default subscription.</value>
</data>
<data name="InvalidNewProfilePath" xml:space="preserve">
<value>Path must specify a valid path to an Azure profile.</value>
</data>
<data name="InvalidProfileProperties" xml:space="preserve">
<value>Property bag Hashtable must contain one of the following sets of properties: {SubscriptionId, Certificate}, {SubscriptionId, Username, Password}, {SubscriptionId, ServicePrincipal, Password, Tenant}, {SubscriptionId, AccountId, Token}</value>
</data>
<data name="MissingCertificateInProfileProperties" xml:space="preserve">
<value>Property bag Hashtable must contain a 'Certificate' of type 'X509Certificate2'.</value>
</data>
<data name="MissingPasswordInProfileProperties" xml:space="preserve">
<value>Property bag Hashtable must contain a 'Password' with an associated 'Username' or 'ServicePrincipal'.</value>
</data>
<data name="MissingSubscriptionInProfileProperties" xml:space="preserve">
<value>Property bag Hashtable must contain a 'SubscriptionId'.</value>
</data>
</root>
2 changes: 1 addition & 1 deletion src/Common/Commands.Common/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<package id="Hyak.Common" version="1.0.2" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" />
<package id="Microsoft.Azure.Common" version="2.0.2" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.0.13-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.Azure.Common.Authentication" version="1.0.8-preview" targetFramework="net45" />
<package id="Microsoft.Azure.Management.Resources" version="2.12.0-preview" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" />
<package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" />
Expand Down
Loading