Skip to content

Commit e27f2ae

Browse files
committed
Fixing SPN certificate login and data collection environment read
1 parent 0a0dd87 commit e27f2ae

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
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/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
{

0 commit comments

Comments
 (0)