Skip to content

Commit 7735a2e

Browse files
authored
Merge pull request Azure#327 from markcowl/spn
Fix SPN auth issues and Fix Import-Context Parameter set
2 parents 2e2b611 + 5c9bddb commit 7735a2e

File tree

5 files changed

+108
-55
lines changed

5 files changed

+108
-55
lines changed

src/Common/Commands.Common/DataCollectionController.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,32 @@ public abstract class DataCollectionController
2626

2727
static AzurePSDataCollectionProfile Initialize(IAzureSession session)
2828
{
29-
AzurePSDataCollectionProfile result = new AzurePSDataCollectionProfile();
29+
AzurePSDataCollectionProfile result = new AzurePSDataCollectionProfile(true);
3030
try
3131
{
32-
var store = session.DataStore;
33-
string dataPath = Path.Combine(session.ProfileDirectory, AzurePSDataCollectionProfile.DefaultFileName);
34-
if (store.FileExists(dataPath))
32+
var environmentValue = Environment.GetEnvironmentVariable(AzurePSDataCollectionProfile.EnvironmentVariableName);
33+
bool enabled = true;
34+
if (!string.IsNullOrWhiteSpace(environmentValue) && bool.TryParse(environmentValue, out enabled))
3535
{
36-
string contents = store.ReadFileAsText(dataPath);
37-
var localResult = JsonConvert.DeserializeObject<AzurePSDataCollectionProfile>(contents);
38-
if (localResult != null && localResult.EnableAzureDataCollection.HasValue)
39-
{
40-
result = localResult;
41-
}
36+
result.EnableAzureDataCollection = enabled;
4237
}
4338
else
4439
{
45-
WritePSDataCollectionProfile(session, new AzurePSDataCollectionProfile(true));
40+
var store = session.DataStore;
41+
string dataPath = Path.Combine(session.ProfileDirectory, AzurePSDataCollectionProfile.DefaultFileName);
42+
if (store.FileExists(dataPath))
43+
{
44+
string contents = store.ReadFileAsText(dataPath);
45+
var localResult = JsonConvert.DeserializeObject<AzurePSDataCollectionProfile>(contents);
46+
if (localResult != null && localResult.EnableAzureDataCollection.HasValue)
47+
{
48+
result = localResult;
49+
}
50+
}
51+
else
52+
{
53+
WritePSDataCollectionProfile(session, new AzurePSDataCollectionProfile(true));
54+
}
4655
}
4756
}
4857
catch

src/ResourceManager/Common/Commands.Common.Authentication.ResourceManager/AzureRmProfile.cs

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -356,55 +356,57 @@ public bool TryFindContext(IAzureContext context, out string name)
356356
{
357357
bool result = false;
358358
name = null;
359-
var foundContext = Contexts.FirstOrDefault((c) =>
360-
c.Value != null
361-
&& (
362-
(c.Value.Account != null && context.Account != null && string.Equals(c.Value.Account.Id, context.Account.Id, StringComparison.OrdinalIgnoreCase))
363-
|| (c.Value.Account == context.Account))
364-
&& (
365-
(c.Value.Tenant != null && context.Tenant != null && c.Value.Tenant.GetId() == context.Tenant.GetId())
366-
|| (c.Value.Tenant == context.Tenant))
367-
&& (
368-
(c.Value.Subscription != null && context.Subscription != null && c.Value.Subscription.GetId() == context.Subscription.GetId())
369-
|| (c.Value.Subscription == context.Subscription)));
370-
if (!string.IsNullOrEmpty(foundContext.Key))
371-
{
372-
name = foundContext.Key;
373-
result = true;
359+
if (context != null)
360+
{
361+
var foundContext = Contexts.FirstOrDefault((c) =>
362+
c.Value != null
363+
&& (
364+
(c.Value.Account != null && context.Account != null && string.Equals(c.Value.Account.Id, context.Account.Id, StringComparison.OrdinalIgnoreCase))
365+
|| (c.Value.Account == context.Account))
366+
&& (
367+
(c.Value.Tenant != null && context.Tenant != null && c.Value.Tenant.GetId() == context.Tenant.GetId())
368+
|| (c.Value.Tenant == context.Tenant))
369+
&& (
370+
(c.Value.Subscription != null && context.Subscription != null && c.Value.Subscription.GetId() == context.Subscription.GetId())
371+
|| (c.Value.Subscription == context.Subscription)));
372+
if (!string.IsNullOrEmpty(foundContext.Key))
373+
{
374+
name = foundContext.Key;
375+
result = true;
376+
}
374377
}
375378

376379
return result;
377380
}
378381

379382
public bool TryGetContextName(IAzureContext context, out string name)
380383
{
381-
if (context == null)
382-
{
383-
throw new ArgumentNullException(nameof(context));
384-
}
385-
386384
bool result = false;
387385
name = null;
388-
if ((context.Account != null && !string.IsNullOrWhiteSpace(context.Account.Id)) || context.Subscription != null)
386+
if (context != null)
389387
{
390-
List<string> components = new List<string>();
391-
if (context.Account != null && !string.IsNullOrWhiteSpace(context.Account.Id))
388+
389+
if ((context.Account != null && !string.IsNullOrWhiteSpace(context.Account.Id)) || context.Subscription != null)
392390
{
393-
components.Add(context.Account.Id);
391+
List<string> components = new List<string>();
392+
if (context.Account != null && !string.IsNullOrWhiteSpace(context.Account.Id))
393+
{
394+
components.Add(context.Account.Id);
395+
}
396+
397+
if (context.Subscription != null)
398+
{
399+
components.Add(context.Subscription.GetId().ToString());
400+
}
401+
402+
name = string.Format("[{0}]", string.Join(", ", components));
403+
result = true;
394404
}
395-
396-
if (context.Subscription != null)
405+
else
397406
{
398-
components.Add(context.Subscription.GetId().ToString());
407+
name = "Default";
408+
result = true;
399409
}
400-
401-
name = string.Format("[{0}]", string.Join(", ", components));
402-
result = true;
403-
}
404-
else
405-
{
406-
name = "Default";
407-
result = true;
408410
}
409411

410412
return result;
@@ -478,7 +480,8 @@ public bool TrySetDefaultContext(IAzureContext context)
478480
{
479481
bool result = false;
480482
string contextName;
481-
if (TryFindContext(context, out contextName) || TryAddContext(context, out contextName))
483+
484+
if (context != null && (TryFindContext(context, out contextName) || TryAddContext(context, out contextName)))
482485
{
483486
result = TrySetDefaultContext(contextName);
484487
}
@@ -489,13 +492,16 @@ public bool TrySetDefaultContext(IAzureContext context)
489492
public bool TrySetDefaultContext(string name, IAzureContext context)
490493
{
491494
bool result = false;
492-
if (string.IsNullOrWhiteSpace(name))
495+
if (context != null)
493496
{
494-
result = TrySetDefaultContext(context);
495-
}
496-
else if (TrySetContext(name, context))
497-
{
498-
result = TrySetDefaultContext(name);
497+
if (string.IsNullOrWhiteSpace(name))
498+
{
499+
result = TrySetDefaultContext(context);
500+
}
501+
else if (TrySetContext(name, context))
502+
{
503+
result = TrySetDefaultContext(name);
504+
}
499505
}
500506

501507
return result;
@@ -562,6 +568,11 @@ public bool TryCopyProfile(AzureRmProfile other)
562568
TrySetContext(context.Key, context.Value);
563569
}
564570

571+
if (other.DefaultContext != null)
572+
{
573+
this.TrySetDefaultContext(other.DefaultContext);
574+
}
575+
565576
this.CopyPropertiesFrom(other);
566577
return true;
567578
}

src/ResourceManager/Profile/Commands.Profile.Test/SessionInitializationTests.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,35 @@ void ResetState()
6060
AzureSession.Instance.ARMContextSaveMode = ContextSaveMode.Process;
6161
AzureSession.Instance.AuthenticationFactory = new MockTokenAuthenticationFactory();
6262
AzureSession.Instance.TokenCache = new AuthenticationStoreTokenCache(new AzureTokenCache());
63-
Environment.SetEnvironmentVariable("Azure_PS_Data_Collection", "false");
63+
Environment.SetEnvironmentVariable("Azure_PS_Data_Collection", "");
64+
}
65+
66+
[Fact]
67+
[Trait(Category.AcceptanceType, Category.CheckIn)]
68+
public void DataCollectionSettingPreventsFileWrite()
69+
{
70+
try
71+
{
72+
Environment.SetEnvironmentVariable("Azure_PS_Data_Collection", "true");
73+
var store = SetupStore();
74+
store.Setup(f => f.FileExists(It.IsAny<string>())).Returns(false);
75+
store.Setup(f => f.WriteFile(It.IsAny<string>(), It.IsAny<string>())).Throws(new IOException("Cannot access file"));
76+
store.Setup(f => f.WriteFile(It.IsAny<string>(), It.IsAny<byte[]>())).Throws(new IOException("Cannot access file"));
77+
store.Setup(f => f.WriteFile(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Encoding>())).Throws(new IOException("Cannot access file"));
78+
AzureSessionInitializer.CreateOrReplaceSession(store.Object);
79+
var session = AzureSession.Instance;
80+
Assert.NotNull(session);
81+
Assert.Equal(ContextSaveMode.Process, session.ARMContextSaveMode);
82+
Assert.NotNull(session.TokenCache);
83+
Assert.Equal(typeof(AuthenticationStoreTokenCache), session.TokenCache.GetType());
84+
Assert.NotNull(AzureRmProfileProvider.Instance);
85+
Assert.Equal(typeof(ResourceManagerProfileProvider), AzureRmProfileProvider.Instance.GetType());
86+
store.Verify(f => f.WriteFile(It.IsAny<string>(), It.IsAny<string>()), Times.Never);
87+
}
88+
finally
89+
{
90+
ResetState();
91+
}
6492
}
6593

6694
[Fact]

src/ResourceManager/Profile/Commands.Profile/Account/AddAzureRmAccount.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ public override void ExecuteCmdlet()
192192
{
193193
azureAccount.Id = ApplicationId;
194194
}
195+
196+
if (!string.IsNullOrWhiteSpace(CertificateThumbprint))
197+
{
198+
azureAccount.SetThumbprint(CertificateThumbprint);
199+
}
195200

196201
if (!string.IsNullOrEmpty(TenantId))
197202
{

src/ResourceManager/Profile/Commands.Profile/Context/ImportAzureRMContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Microsoft.Azure.Commands.Profile
2626
/// <summary>
2727
/// Selects Microsoft Azure profile.
2828
/// </summary>
29-
[Cmdlet(VerbsData.Import, "AzureRmContext", SupportsShouldProcess = true), OutputType(typeof(PSAzureProfile))]
29+
[Cmdlet(VerbsData.Import, "AzureRmContext", SupportsShouldProcess = true, DefaultParameterSetName = ProfileFromDiskParameterSet), OutputType(typeof(PSAzureProfile))]
3030
public class ImportAzureRMContextCommand : AzureContextModificationCmdlet
3131
{
3232
internal const string InMemoryProfileParameterSet = "InMemoryProfile";

0 commit comments

Comments
 (0)