Skip to content

Commit 5a97580

Browse files
authored
Merge pull request #13 from Azure/update-directory
Update directory for profile and context setting files
2 parents 5d27ca0 + 11eceaf commit 5a97580

File tree

5 files changed

+88
-10
lines changed

5 files changed

+88
-10
lines changed

src/Authentication/Authentication/ProtectedFileTokenCache.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ namespace Microsoft.Azure.Commands.Common.Authentication
2828
public class ProtectedFileTokenCache : TokenCache, IAzureTokenCache
2929
{
3030
private static readonly string CacheFileName = Path.Combine(
31+
#if !NETSTANDARD
3132
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
33+
#else
34+
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
35+
#endif
3236
Resources.AzureDirectoryName, "TokenCache.dat");
3337

3438
private static readonly object fileLock = new object();
@@ -158,7 +162,7 @@ private void WriteCacheIntoFile(string cacheFileName = null)
158162
lock(fileLock)
159163
{
160164
if (HasStateChanged)
161-
{
165+
{
162166
_store.WriteFile(cacheFileName, dataToWrite);
163167
HasStateChanged = false;
164168
}
@@ -188,7 +192,7 @@ private void EnsureCacheFile(string cacheFileName = null)
188192
#endif
189193
}
190194
}
191-
195+
192196
// Eagerly create cache file.
193197
#if !NETSTANDARD
194198
var dataToWrite = ProtectedData.Protect(Serialize(), null, DataProtectionScope.CurrentUser);

src/Authentication/AzureSessionInitializer.cs

+61-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using Microsoft.Azure.Commands.Common.Authentication.Properties;
2222
using Newtonsoft.Json;
2323
using TraceLevel = System.Diagnostics.TraceLevel;
24+
using System.Linq;
2425

2526
namespace Microsoft.Azure.Commands.Common.Authentication
2627
{
@@ -29,6 +30,9 @@ namespace Microsoft.Azure.Commands.Common.Authentication
2930
/// </summary>
3031
public static class AzureSessionInitializer
3132
{
33+
private const string ContextAutosaveSettingFileName = ContextAutosaveSettings.AutoSaveSettingsFile;
34+
private const string DataCollectionFileName = AzurePSDataCollectionProfile.DefaultFileName;
35+
3236
/// <summary>
3337
/// Initialize the azure session if it is not already initialized
3438
/// </summary>
@@ -73,7 +77,43 @@ static IAzureTokenCache InitializeTokenCache(IDataStore store, string cacheDirec
7377
return result;
7478
}
7579

76-
static ContextAutosaveSettings InitializeSessionSettings(IDataStore store, string profileDirectory, string settingsFile)
80+
static bool MigrateSettings(IDataStore store, string oldProfileDirectory, string newProfileDirectory)
81+
{
82+
var filesToMigrate = new string[] { ContextAutosaveSettingFileName,
83+
DataCollectionFileName };
84+
try
85+
{
86+
if (!store.DirectoryExists(newProfileDirectory))
87+
{
88+
store.CreateDirectory(newProfileDirectory);
89+
}
90+
91+
// Only migrate if
92+
// (1) all files to migrate can be found in the old directory, and
93+
// (2) none of the files to migrate can be found in the new directory
94+
var oldFiles = Directory.EnumerateFiles(oldProfileDirectory).Where(f => filesToMigrate.Contains(Path.GetFileName(f)));
95+
var newFiles = Directory.EnumerateFiles(newProfileDirectory).Where(f => filesToMigrate.Contains(Path.GetFileName(f)));
96+
if (store.DirectoryExists(oldProfileDirectory) && oldFiles.Count() == filesToMigrate.Length && !newFiles.Any())
97+
{
98+
foreach (var oldFilePath in oldFiles)
99+
{
100+
var fileName = Path.GetFileName(oldFilePath);
101+
var newFilePath = Path.Combine(newProfileDirectory, fileName);
102+
store.CopyFile(oldFilePath, newFilePath);
103+
}
104+
105+
return true;
106+
}
107+
}
108+
catch
109+
{
110+
// ignore exceptions in reading settings from disk
111+
}
112+
113+
return false;
114+
}
115+
116+
static ContextAutosaveSettings InitializeSessionSettings(IDataStore store, string profileDirectory, string settingsFile, bool migrated = false)
77117
{
78118
var result = new ContextAutosaveSettings
79119
{
@@ -92,11 +132,16 @@ static ContextAutosaveSettings InitializeSessionSettings(IDataStore store, strin
92132
{
93133
var settingsText = store.ReadFileAsText(settingsPath);
94134
ContextAutosaveSettings settings = JsonConvert.DeserializeObject<ContextAutosaveSettings>(settingsText);
95-
result.CacheDirectory = settings.CacheDirectory ?? result.CacheDirectory;
135+
result.CacheDirectory = migrated ? profileDirectory : settings.CacheDirectory ?? result.CacheDirectory;
96136
result.CacheFile = settings.CacheFile ?? result.CacheFile;
97-
result.ContextDirectory = settings.ContextDirectory ?? result.ContextDirectory;
137+
result.ContextDirectory = migrated ? profileDirectory : settings.ContextDirectory ?? result.ContextDirectory;
98138
result.Mode = settings.Mode;
99139
result.ContextFile = settings.ContextFile ?? result.ContextFile;
140+
if (migrated)
141+
{
142+
string autoSavePath = Path.Combine(profileDirectory, settingsFile);
143+
store.WriteFile(autoSavePath, JsonConvert.SerializeObject(result));
144+
}
100145
}
101146
else
102147
{
@@ -127,8 +172,13 @@ static void InitializeDataCollection(IAzureSession session)
127172
static IAzureSession CreateInstance(IDataStore dataStore = null)
128173
{
129174
string profilePath = Path.Combine(
130-
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
175+
#if NETSTANDARD
176+
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
131177
Resources.AzureDirectoryName);
178+
string oldProfilePath = Path.Combine(
179+
#endif
180+
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
181+
Resources.OldAzureDirectoryName);
132182
dataStore = dataStore ?? new DiskDataStore();
133183

134184
var session = new AdalSession
@@ -142,7 +192,13 @@ static IAzureSession CreateInstance(IDataStore dataStore = null)
142192
ProfileFile = "AzureProfile.json",
143193
};
144194

145-
var autoSave = InitializeSessionSettings(dataStore, profilePath, ContextAutosaveSettings.AutoSaveSettingsFile);
195+
var migrated =
196+
#if !NETSTANDARD
197+
false;
198+
#else
199+
MigrateSettings(dataStore, oldProfilePath, profilePath);
200+
#endif
201+
var autoSave = InitializeSessionSettings(dataStore, profilePath, ContextAutosaveSettings.AutoSaveSettingsFile, migrated);
146202
session.ARMContextSaveMode = autoSave.Mode;
147203
session.ARMProfileDirectory = autoSave.ContextDirectory;
148204
session.ARMProfileFile = autoSave.ContextFile;

src/Authentication/Properties/Resources.Designer.cs

+11-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Authentication/Properties/Resources.resx

+4-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
<value>Account needs to be specified</value>
122122
</data>
123123
<data name="AzureDirectoryName" xml:space="preserve">
124-
<value>Windows Azure Powershell</value>
124+
<value>.Azure</value>
125125
</data>
126126
<data name="CertificateNotFoundInStore" xml:space="preserve">
127127
<value>No certificate was found in the certificate store with thumbprint {0}</value>
@@ -337,4 +337,7 @@
337337
<data name="CacheHit" xml:space="preserve">
338338
<value>Cache Hit</value>
339339
</data>
340+
<data name="OldAzureDirectoryName" xml:space="preserve">
341+
<value>Windows Azure Powershell</value>
342+
</data>
340343
</root>

src/Common/AzurePowerShell.cs

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public class AzurePowerShell
4343
string.Format("v{0}", AzurePowerShell.AssemblyVersion));
4444

4545
public static string ProfileDirectory = Path.Combine(
46+
#if NETSTANDARD
47+
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
48+
".Azure");
49+
50+
public static string OldProfileDirectory = Path.Combine(
51+
#endif
4652
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
4753
"Windows Azure PowerShell");
4854
}

0 commit comments

Comments
 (0)