Skip to content

Commit

Permalink
Merge branch 'develop' into analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
markcowl authored Sep 13, 2018
2 parents 2456bcf + 5a97580 commit 04ce4e7
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/Authentication/Authentication/ProtectedFileTokenCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ namespace Microsoft.Azure.Commands.Common.Authentication
public class ProtectedFileTokenCache : TokenCache, IAzureTokenCache
{
private static readonly string CacheFileName = Path.Combine(
#if !NETSTANDARD
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
#else
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
#endif
Resources.AzureDirectoryName, "TokenCache.dat");

private static readonly object fileLock = new object();
Expand Down Expand Up @@ -158,7 +162,7 @@ private void WriteCacheIntoFile(string cacheFileName = null)
lock(fileLock)
{
if (HasStateChanged)
{
{
_store.WriteFile(cacheFileName, dataToWrite);
HasStateChanged = false;
}
Expand Down Expand Up @@ -188,7 +192,7 @@ private void EnsureCacheFile(string cacheFileName = null)
#endif
}
}

// Eagerly create cache file.
#if !NETSTANDARD
var dataToWrite = ProtectedData.Protect(Serialize(), null, DataProtectionScope.CurrentUser);
Expand Down
66 changes: 61 additions & 5 deletions src/Authentication/AzureSessionInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Microsoft.Azure.Commands.Common.Authentication.Properties;
using Newtonsoft.Json;
using TraceLevel = System.Diagnostics.TraceLevel;
using System.Linq;

namespace Microsoft.Azure.Commands.Common.Authentication
{
Expand All @@ -29,6 +30,9 @@ namespace Microsoft.Azure.Commands.Common.Authentication
/// </summary>
public static class AzureSessionInitializer
{
private const string ContextAutosaveSettingFileName = ContextAutosaveSettings.AutoSaveSettingsFile;
private const string DataCollectionFileName = AzurePSDataCollectionProfile.DefaultFileName;

/// <summary>
/// Initialize the azure session if it is not already initialized
/// </summary>
Expand Down Expand Up @@ -73,7 +77,43 @@ static IAzureTokenCache InitializeTokenCache(IDataStore store, string cacheDirec
return result;
}

static ContextAutosaveSettings InitializeSessionSettings(IDataStore store, string profileDirectory, string settingsFile)
static bool MigrateSettings(IDataStore store, string oldProfileDirectory, string newProfileDirectory)
{
var filesToMigrate = new string[] { ContextAutosaveSettingFileName,
DataCollectionFileName };
try
{
if (!store.DirectoryExists(newProfileDirectory))
{
store.CreateDirectory(newProfileDirectory);
}

// Only migrate if
// (1) all files to migrate can be found in the old directory, and
// (2) none of the files to migrate can be found in the new directory
var oldFiles = Directory.EnumerateFiles(oldProfileDirectory).Where(f => filesToMigrate.Contains(Path.GetFileName(f)));
var newFiles = Directory.EnumerateFiles(newProfileDirectory).Where(f => filesToMigrate.Contains(Path.GetFileName(f)));
if (store.DirectoryExists(oldProfileDirectory) && oldFiles.Count() == filesToMigrate.Length && !newFiles.Any())
{
foreach (var oldFilePath in oldFiles)
{
var fileName = Path.GetFileName(oldFilePath);
var newFilePath = Path.Combine(newProfileDirectory, fileName);
store.CopyFile(oldFilePath, newFilePath);
}

return true;
}
}
catch
{
// ignore exceptions in reading settings from disk
}

return false;
}

static ContextAutosaveSettings InitializeSessionSettings(IDataStore store, string profileDirectory, string settingsFile, bool migrated = false)
{
var result = new ContextAutosaveSettings
{
Expand All @@ -92,11 +132,16 @@ static ContextAutosaveSettings InitializeSessionSettings(IDataStore store, strin
{
var settingsText = store.ReadFileAsText(settingsPath);
ContextAutosaveSettings settings = JsonConvert.DeserializeObject<ContextAutosaveSettings>(settingsText);
result.CacheDirectory = settings.CacheDirectory ?? result.CacheDirectory;
result.CacheDirectory = migrated ? profileDirectory : settings.CacheDirectory ?? result.CacheDirectory;
result.CacheFile = settings.CacheFile ?? result.CacheFile;
result.ContextDirectory = settings.ContextDirectory ?? result.ContextDirectory;
result.ContextDirectory = migrated ? profileDirectory : settings.ContextDirectory ?? result.ContextDirectory;
result.Mode = settings.Mode;
result.ContextFile = settings.ContextFile ?? result.ContextFile;
if (migrated)
{
string autoSavePath = Path.Combine(profileDirectory, settingsFile);
store.WriteFile(autoSavePath, JsonConvert.SerializeObject(result));
}
}
else
{
Expand Down Expand Up @@ -127,8 +172,13 @@ static void InitializeDataCollection(IAzureSession session)
static IAzureSession CreateInstance(IDataStore dataStore = null)
{
string profilePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
#if NETSTANDARD
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
Resources.AzureDirectoryName);
string oldProfilePath = Path.Combine(
#endif
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
Resources.OldAzureDirectoryName);
dataStore = dataStore ?? new DiskDataStore();

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

var autoSave = InitializeSessionSettings(dataStore, profilePath, ContextAutosaveSettings.AutoSaveSettingsFile);
var migrated =
#if !NETSTANDARD
false;
#else
MigrateSettings(dataStore, oldProfilePath, profilePath);
#endif
var autoSave = InitializeSessionSettings(dataStore, profilePath, ContextAutosaveSettings.AutoSaveSettingsFile, migrated);
session.ARMContextSaveMode = autoSave.Mode;
session.ARMProfileDirectory = autoSave.ContextDirectory;
session.ARMProfileFile = autoSave.ContextFile;
Expand Down
13 changes: 11 additions & 2 deletions src/Authentication/Properties/Resources.Designer.cs

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

5 changes: 4 additions & 1 deletion src/Authentication/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
<value>Account needs to be specified</value>
</data>
<data name="AzureDirectoryName" xml:space="preserve">
<value>Windows Azure Powershell</value>
<value>.Azure</value>
</data>
<data name="CertificateNotFoundInStore" xml:space="preserve">
<value>No certificate was found in the certificate store with thumbprint {0}</value>
Expand Down Expand Up @@ -337,4 +337,7 @@
<data name="CacheHit" xml:space="preserve">
<value>Cache Hit</value>
</data>
<data name="OldAzureDirectoryName" xml:space="preserve">
<value>Windows Azure Powershell</value>
</data>
</root>
6 changes: 6 additions & 0 deletions src/Common/AzurePowerShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ public class AzurePowerShell
string.Format("v{0}", AzurePowerShell.AssemblyVersion));

public static string ProfileDirectory = Path.Combine(
#if NETSTANDARD
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
".Azure");

public static string OldProfileDirectory = Path.Combine(
#endif
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"Windows Azure PowerShell");
}
Expand Down

0 comments on commit 04ce4e7

Please sign in to comment.