Skip to content

Commit 0b1f8b4

Browse files
authored
Merge pull request #282 from markcowl/cpersistclean
AzureStack modules and Context Persistence implementation
2 parents 5cb285a + 9609437 commit 0b1f8b4

File tree

2,513 files changed

+3389491
-10935
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,513 files changed

+3389491
-10935
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Azure PowerShell specific
22
src/Publish/
33
src/Package/
4+
src/Stack/
45

56
obj
67
TestResults
@@ -12,6 +13,7 @@ msbuild.log
1213
# app.config is not useful for PowerShell.
1314
# please do not commit any app.config files.
1415
app.config
16+
!src/ResourceManager/Profile/Commands.Profile.Test/App.config
1517
## Ignore Visual Studio temporary files, build results, and
1618
## files generated by popular Visual Studio add-ons.
1719

NuGet.config

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<packageSources>
44
<add key="local-feed" value="tools/LocalFeed" />
5-
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
5+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
66
<add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
77
<add key="powershell-core" value="https://powershell.myget.org/F/powershell-core/api/v3/index.json" />
88
</packageSources>
9+
<disabledPackageSources>
10+
<add key="dotnet-core" value="true" />
11+
<add key="powershell-core" value="true" />
12+
</disabledPackageSources>
913
</configuration>

build.proj

Lines changed: 80 additions & 30 deletions
Large diffs are not rendered by default.

setup/azurecmd.wxs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension" xmlns:netfx="http://schemas.microsoft.com/wix/NetFxExtension">
33

4-
<?define productName="Microsoft Azure PowerShell - August 2017" ?>
4+
<?define productName="Microsoft Azure PowerShell - September 2017" ?>
55
<?define sourceDir="$(var.SolutionDir)..\src\Package\$(var.Configuration)" ?>
66
<?define caSourceDir="$(var.SolutionDir)setup\bin\$(var.Configuration)" ?>
77

8-
<?define version="4.3.1" ?>
8+
<?define version="4.3.2" ?>
99

1010
<Product Id="*"
1111
Name="$(var.productName)"

src/Common/Commands.Common.Authentication.Abstractions/AzureAccount.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,22 @@ public static class Property
104104
/// </summary>
105105
AccessToken = "AccessToken",
106106

107+
/// <summary>
108+
/// Access token for AD Graph service.
109+
/// </summary>
110+
GraphAccessToken = "GraphAccessToken",
111+
112+
/// <summary>
113+
/// Access token for KeyVault service.
114+
/// </summary>
115+
KeyVaultAccessToken = "KeyVault",
116+
107117
/// <summary>
108118
/// Thumbprint for associated certificate
109119
/// </summary>
110120
CertificateThumbprint = "CertificateThumbprint";
111121

122+
112123
}
113124
}
114125
}

src/Common/Commands.Common.Authentication.Abstractions/AzureSession.cs

Lines changed: 176 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Collections.Generic;
1818
using System.Threading;
1919
using System.Diagnostics;
20+
using System.Collections.Concurrent;
2021

2122
namespace Microsoft.Azure.Commands.Common.Authentication
2223
{
@@ -28,6 +29,8 @@ public abstract class AzureSession : IAzureSession
2829
static IAzureSession _instance;
2930
static bool _initialized = false;
3031
static ReaderWriterLockSlim sessionLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
32+
private IDictionary<ComponentKey, object> _componentRegistry = new ConcurrentDictionary<ComponentKey, object>(new ComponentKeyComparer());
33+
3134
/// <summary>
3235
/// Gets or sets Azure client factory.
3336
/// </summary>
@@ -63,6 +66,11 @@ public abstract class AzureSession : IAzureSession
6366
/// </summary>
6467
public string ProfileFile { get; set; }
6568

69+
/// <summary>
70+
/// Gets or sets the context container file for azure resource manager
71+
/// </summary>
72+
public string ResourceManagerContextFile { get; set; }
73+
6674
/// <summary>
6775
/// Gets or sets file name for the migration backup.
6876
/// </summary>
@@ -126,6 +134,10 @@ public static IAzureSession Instance
126134

127135
public abstract SourceLevels AuthenticationTraceSourceLevel { get; set; }
128136

137+
public string TokenCacheDirectory { get; set; }
138+
139+
public string ARMContextSaveMode { get; set; }
140+
129141
/// <summary>
130142
/// Initialize the AzureSession, avoid contention at startup
131143
/// </summary>
@@ -138,10 +150,10 @@ public static void Initialize(Func<IAzureSession> instanceCreator, bool overwrit
138150
sessionLock.EnterWriteLock();
139151
try
140152
{
141-
if (!_initialized || overwrite)
153+
if (overwrite || !_initialized)
142154
{
143-
_initialized = true;
144155
_instance = instanceCreator();
156+
_initialized = true;
145157
}
146158
}
147159
finally
@@ -167,5 +179,167 @@ public static void Initialize(Func<IAzureSession> instanceCreator)
167179
{
168180
Initialize(instanceCreator, false);
169181
}
182+
183+
public static void Modify(Action<IAzureSession> modifier)
184+
{
185+
try
186+
{
187+
sessionLock.EnterWriteLock();
188+
try
189+
{
190+
modifier(_instance);
191+
}
192+
finally
193+
{
194+
sessionLock.ExitWriteLock();
195+
}
196+
}
197+
catch (LockRecursionException lockException)
198+
{
199+
throw new InvalidOperationException(Abstractions.Properties.Resources.SessionLockWriteRecursion, lockException);
200+
}
201+
catch (ObjectDisposedException disposedException)
202+
{
203+
throw new InvalidOperationException(Abstractions.Properties.Resources.SessionLockWriteDisposed, disposedException);
204+
}
205+
}
206+
207+
public bool TryGetComponent<T>(string componentName, out T component) where T : class
208+
{
209+
var key = new ComponentKey(componentName, typeof(T));
210+
component = null;
211+
if (_componentRegistry.ContainsKey(key))
212+
{
213+
component = _componentRegistry[key] as T;
214+
}
215+
216+
return component != null;
217+
}
218+
219+
public void RegisterComponent<T>(string componentName, Func<T> componentInitializer) where T : class
220+
{
221+
RegisterComponent(componentName, componentInitializer, false); ;
222+
}
223+
224+
public void RegisterComponent<T>(string componentName, Func<T> componentInitializer, bool overwrite) where T : class
225+
{
226+
ChangeRegistry(
227+
() =>
228+
{
229+
var key = new ComponentKey(componentName, typeof(T));
230+
if (!_componentRegistry.ContainsKey(key) || overwrite)
231+
{
232+
_componentRegistry[key] = componentInitializer();
233+
}
234+
});
235+
}
236+
237+
public void UnregisterComponent<T>(string componentName) where T : class
238+
{
239+
ChangeRegistry(
240+
() =>
241+
{
242+
var key = new ComponentKey(componentName, typeof(T));
243+
if (_componentRegistry.ContainsKey(key))
244+
{
245+
_componentRegistry.Remove(key);
246+
}
247+
});
248+
}
249+
250+
public void ClearComponents()
251+
{
252+
ChangeRegistry(_componentRegistry.Clear);
253+
}
254+
255+
void ChangeRegistry(Action changeAction)
256+
{
257+
changeAction();
258+
}
259+
260+
private class ComponentKey : IComparable<ComponentKey>, IEquatable<ComponentKey>
261+
{
262+
public string Name { get; private set; }
263+
264+
public string Type { get; private set; }
265+
266+
public ComponentKey(string name, Type type)
267+
{
268+
if (name == null)
269+
{
270+
throw new ArgumentNullException(nameof(name));
271+
}
272+
273+
if (type == null)
274+
{
275+
throw new ArgumentNullException(nameof(type));
276+
}
277+
278+
Name = name;
279+
Type = type.FullName;
280+
}
281+
282+
public override int GetHashCode()
283+
{
284+
return ToString().GetHashCode();
285+
}
286+
287+
public override string ToString()
288+
{
289+
return string.Format($"{Name}-{Type}");
290+
}
291+
292+
public override bool Equals(object obj)
293+
{
294+
var other = obj as ComponentKey;
295+
return other != null
296+
&& this.Equals(other);
297+
}
298+
299+
public int CompareTo(ComponentKey other)
300+
{
301+
if (other == null)
302+
{
303+
return 1;
304+
}
305+
306+
var stringCompare = this.Name.ToLowerInvariant().CompareTo(other.Name.ToLowerInvariant());
307+
return (stringCompare != 0 ? stringCompare : this.Type.CompareTo(other.Type));
308+
}
309+
310+
public bool Equals(ComponentKey other)
311+
{
312+
return other != null
313+
&& string.Equals(this.Name, other.Name, StringComparison.OrdinalIgnoreCase)
314+
&& string.Equals(this.Type, other.Type, StringComparison.OrdinalIgnoreCase);
315+
}
316+
}
317+
318+
private class ComponentKeyComparer : IComparer<ComponentKey>, IEqualityComparer<ComponentKey>
319+
{
320+
public int Compare(ComponentKey x, ComponentKey y)
321+
{
322+
if (x == null)
323+
{
324+
throw new ArgumentNullException(nameof(x));
325+
}
326+
if (y == null)
327+
{
328+
throw new ArgumentNullException(nameof(y));
329+
}
330+
331+
return x.CompareTo(y);
332+
}
333+
334+
public bool Equals(ComponentKey x, ComponentKey y)
335+
{
336+
return x != null && y != null && x.Equals(y);
337+
}
338+
339+
public int GetHashCode(ComponentKey obj)
340+
{
341+
return obj.GetHashCode();
342+
}
343+
}
170344
}
171345
}

src/Common/Commands.Common.Authentication.Abstractions/Commands.Common.Authentication.Abstractions.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<Private>True</Private>
5959
</Reference>
6060
<Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
61-
<HintPath>..\..\packages\Microsoft.Rest.ClientRuntime.2.3.8\lib\net452\Microsoft.Rest.ClientRuntime.dll</HintPath>
61+
<HintPath>..\..\packages\Microsoft.Rest.ClientRuntime.2.3.9\lib\net452\Microsoft.Rest.ClientRuntime.dll</HintPath>
6262
<Private>True</Private>
6363
</Reference>
6464
<Reference Include="Microsoft.Threading.Tasks, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
@@ -103,6 +103,7 @@
103103
<Compile Include="AzureAccount.cs" />
104104
<Compile Include="AzureRmProfileProvider.cs" />
105105
<Compile Include="AzureSMProfileProvider.cs" />
106+
<Compile Include="ContextSaveMode.cs" />
106107
<Compile Include="Extensions\AzureAccountExtensions.cs" />
107108
<Compile Include="AzureEnvironmentConstants.cs" />
108109
<Compile Include="Extensions\AzureContextExtensions.cs" />
@@ -118,6 +119,7 @@
118119
<Compile Include="Interfaces\IAccessToken.cs" />
119120
<Compile Include="Interfaces\IExtensibleSettings.cs" />
120121
<Compile Include="Interfaces\IExtensibleModel.cs" />
122+
<Compile Include="Interfaces\IFileProvider.cs" />
121123
<Compile Include="Interfaces\IHyakAuthenticationFactory.cs" />
122124
<Compile Include="Interfaces\IAzureSession.cs" />
123125
<Compile Include="Interfaces\IAuthenticationFactory.cs" />
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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.Authentication.Abstractions
16+
{
17+
/// <summary>
18+
/// Constants for use with the autosave mode for the context
19+
/// </summary>
20+
public static class ContextSaveMode
21+
{
22+
public const string Process = "Process", CurrentUser = "CurrentUser";
23+
}
24+
}

src/Common/Commands.Common.Authentication.Abstractions/Extensions/AzureAccountExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public static void RemoveSubscription(this IAzureAccount account, Guid id)
188188
/// <param name="other">The account to copy from (source)</param>
189189
public static void CopyFrom(this IAzureAccount account, IAzureAccount source)
190190
{
191-
if (source != null)
191+
if (account != null && source != null)
192192
{
193193
account.Credential = source.Credential;
194194
account.Id = source.Id;

src/Common/Commands.Common.Authentication.Abstractions/Extensions/AzureEnvironmentExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ public static string GetPublishSettingsFileUrlWithRealm(this IAzureEnvironment e
366366

367367
public static void CopyFrom(this IAzureEnvironment environment, IAzureEnvironment other)
368368
{
369-
if (other != null)
369+
if (environment != null && other != null)
370370
{
371371
environment.Name = other.Name;
372372
environment.OnPremise = other.OnPremise;

src/Common/Commands.Common.Authentication.Abstractions/Extensions/AzureSubscriptionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public static void SetTenant(this IAzureSubscription subscription, string tenant
148148
/// <param name="other"></param>
149149
public static void CopyFrom(this IAzureSubscription subscription, IAzureSubscription other)
150150
{
151-
if (other != null)
151+
if (subscription != null && other != null)
152152
{
153153
subscription.Id = other.Id;
154154
subscription.Name = other.Name;

src/Common/Commands.Common.Authentication.Abstractions/Extensions/AzureTenantExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static Guid GetId(this IAzureTenant tenant)
3535
/// <param name="other"></param>
3636
public static void CopyFrom(this IAzureTenant tenant, IAzureTenant other)
3737
{
38-
if (other != null)
38+
if (tenant != null && other != null)
3939
{
4040
tenant.Id = other.Id;
4141
tenant.Directory = other.Directory;

src/Common/Commands.Common.Authentication.Abstractions/Extensions/ModelExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static bool IsPropertySet(this IExtensibleModel model, string propertyKey
107107
/// <param name="source"></param>
108108
public static void CopyPropertiesFrom(this IExtensibleModel model, IExtensibleModel source)
109109
{
110-
if (source != null)
110+
if (model != null && source != null)
111111
{
112112
foreach (var item in source.ExtendedProperties)
113113
{

src/Common/Commands.Common.Authentication.Abstractions/Interfaces/IAuthenticationFactory.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,12 @@ IAccessToken Authenticate(
7979
/// <param name="targetEndpoint">The named endpoint the AutoRest client will target</param>
8080
/// <returns>AutoRest client crentials targeting the given context and endpoint</returns>
8181
ServiceClientCredentials GetServiceClientCredentials(IAzureContext context, string targetEndpoint);
82+
83+
/// <summary>
84+
/// Remove any stored credentials for the given user
85+
/// </summary>
86+
/// <param name="account">The account to remove credentials for</param>
87+
/// <param name="tokenCache">The TokenCache to remove credentials from</param>
88+
void RemoveUser(IAzureAccount account, IAzureTokenCache tokenCache);
8289
}
8390
}

0 commit comments

Comments
 (0)