Skip to content

Commit fc3da19

Browse files
committed
Merge branch 'develop' of github.com:Azure/azure-powershell-common into key-store-refactor
2 parents 06d30a5 + c584435 commit fc3da19

File tree

11 files changed

+142
-15
lines changed

11 files changed

+142
-15
lines changed

build/publish.proj

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="14.0" DefaultTargets="All" InitialTargets="CheckApiKey" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<Project ToolsVersion="14.0" DefaultTargets="All" InitialTargets="CheckRequiredParameters" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<Root>$(MSBuildThisFileDirectory)..\</Root>
55
<Configuration Condition="'$(Configuration)' == ''">Debug</Configuration>
66
<Artifacts>$(Root)artifacts\</Artifacts>
77
<Tools>$(Root)tools\</Tools>
88
<NuGet>$(Tools)NuGet-4.7.0\nuget.exe</NuGet>
99
<PackDir>$(Artifacts)Package\$(Configuration)\</PackDir>
10+
<PowerShell>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</PowerShell>
1011
</PropertyGroup>
1112

12-
<Target Name="CheckApiKey">
13-
<Error Text="ApiKey is not assigned a value!" Condition="'$(ApiKey)' == ''" />
13+
<Target Name="CheckRequiredParameters">
14+
<Error Text="NuGetApiKey is not assigned a value!" Condition="'$(NuGetApiKey)' == ''" />
15+
<Error Text="GitHubAccessToken is not assigned a value!" Condition="'$(GitHubAccessToken)' == ''" />
16+
<Error Text="ReleaseVersion is not assigned a value!" Condition="'$(ReleaseVersion)' == ''" />
17+
<Error Text="CommitId is not assigned a value!" Condition="'$(CommitId)' == ''" />
1418
</Target>
1519

1620
<Target Name="All">
1721
<Message Importance="high" Text="Publish: All started" />
1822
<ItemGroup>
1923
<PackagePaths Include="$(PackDir)*.nupkg" />
2024
</ItemGroup>
21-
<Exec Command="$(NuGet) push %(PackagePaths.FullPath) -ApiKey $(ApiKey) -Source https://www.myget.org/F/azure-powershell/api/v2/package" />
25+
<Exec Command="$(NuGet) push %(PackagePaths.FullPath) -ApiKey $(NuGetApiKey) -Source https://www.myget.org/F/azure-powershell/api/v2/package" />
26+
<PropertyGroup>
27+
<!-- https://stackoverflow.com/a/41618979/294804 -->
28+
<ReleaseCommand>[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -Uri 'https://api.github.com/repos/Azure/azure-powershell-common/releases' -Headers @{ 'Authorization' = 'Token $(GitHubAccessToken)' } -Method Post -Body (ConvertTo-Json @{ 'tag_name' = '$(ReleaseVersion)'; 'target_commitish' = '$(CommitId)'; 'name' = '$(ReleaseVersion)'; 'body' = 'Azure PowerShell Common version $(ReleaseVersion)'; 'draft' = $false; 'prerelease' = $false })</ReleaseCommand>
29+
</PropertyGroup>
30+
<Message Importance="high" Text="Publish: Creating GitHub release" />
31+
<Exec Command="$(PowerShell) -NonInteractive -NoLogo -NoProfile -Command &quot;$(ReleaseCommand)&quot;" />
2232
</Target>
2333
</Project>

src/Authentication.Abstractions/AzureEnvironment.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ static IDictionary<string, AzureEnvironment> InitializeBuiltInEnvironments()
5353
};
5454
azureCloud.SetProperty(ExtendedEndpoint.OperationalInsightsEndpoint, AzureEnvironmentConstants.AzureOperationalInsightsEndpoint);
5555
azureCloud.SetProperty(ExtendedEndpoint.OperationalInsightsEndpointResourceId, AzureEnvironmentConstants.AzureOperationalInsightsEndpointResourceId);
56+
azureCloud.SetProperty(ExtendedEndpoint.AnalysisServicesEndpointSuffix, AzureEnvironmentConstants.AzureAnalysisServicesEndpointSuffix);
5657
var azureChina = new AzureEnvironment
5758
{
5859
Name = EnvironmentName.AzureChinaCloud,
@@ -76,6 +77,8 @@ static IDictionary<string, AzureEnvironment> InitializeBuiltInEnvironments()
7677
BatchEndpointResourceId = AzureEnvironmentConstants.ChinaBatchEndpointResourceId,
7778
AdTenant = "Common"
7879
};
80+
azureChina.SetProperty(ExtendedEndpoint.AnalysisServicesEndpointSuffix, AzureEnvironmentConstants.ChinaAnalysisServicesEndpointSuffix);
81+
7982
var azureUSGovernment = new AzureEnvironment
8083
{
8184
Name = EnvironmentName.AzureUSGovernment,
@@ -101,6 +104,7 @@ static IDictionary<string, AzureEnvironment> InitializeBuiltInEnvironments()
101104
};
102105
azureUSGovernment.SetProperty(ExtendedEndpoint.OperationalInsightsEndpoint, AzureEnvironmentConstants.USGovernmentOperationalInsightsEndpoint);
103106
azureUSGovernment.SetProperty(ExtendedEndpoint.OperationalInsightsEndpointResourceId, AzureEnvironmentConstants.USGovernmentOperationalInsightsEndpointResourceId);
107+
azureUSGovernment.SetProperty(ExtendedEndpoint.AnalysisServicesEndpointSuffix, AzureEnvironmentConstants.USGovernmentAnalysisServicesEndpointSuffix);
104108
var azureGermany = new AzureEnvironment
105109
{
106110
Name = EnvironmentName.AzureGermanCloud,
@@ -124,6 +128,7 @@ static IDictionary<string, AzureEnvironment> InitializeBuiltInEnvironments()
124128
BatchEndpointResourceId = AzureEnvironmentConstants.GermanBatchEndpointResourceId,
125129
AdTenant = "Common"
126130
};
131+
azureGermany.SetProperty(ExtendedEndpoint.AnalysisServicesEndpointSuffix, AzureEnvironmentConstants.GermanAnalysisServicesEndpointSuffix);
127132
var result = new ConcurrentDictionary<string, AzureEnvironment>(StringComparer.InvariantCultureIgnoreCase);
128133

129134
result[EnvironmentName.AzureCloud] = azureCloud;
@@ -296,7 +301,8 @@ public static class Endpoint
296301
public static class ExtendedEndpoint
297302
{
298303
public const string OperationalInsightsEndpointResourceId = "OperationalInsightsEndpointResourceId",
299-
OperationalInsightsEndpoint = "OperationalInsightsEndpoint";
304+
OperationalInsightsEndpoint = "OperationalInsightsEndpoint",
305+
AnalysisServicesEndpointSuffix = "AzureAnalysisServicesEndpointSuffix";
300306
}
301307
}
302308
}

src/Authentication.Abstractions/AzureEnvironmentConstants.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,20 @@ public static class AzureEnvironmentConstants
138138
/// </summary>
139139
public const string AzureDataLakeServiceEndpointResourceId = "https://datalake.azure.net";
140140

141+
/// <summary>
142+
/// The token audience for Batch data plane requests
143+
/// </summary>
141144
public const string BatchEndpointResourceId = "https://batch.core.windows.net/";
142145
public const string ChinaBatchEndpointResourceId = "https://batch.chinacloudapi.cn/";
143146
public const string USGovernmentBatchEndpointResourceId = "https://batch.core.usgovcloudapi.net/";
144147
public const string GermanBatchEndpointResourceId = "https://batch.cloudapi.de/";
148+
149+
/// <summary>
150+
/// The domain name suffix for Azure Analysis Services
151+
/// </summary>
152+
public const string AzureAnalysisServicesEndpointSuffix = "asazure.windows.net";
153+
public const string ChinaAnalysisServicesEndpointSuffix = "asazure.chinacloudapi.cn";
154+
public const string USGovernmentAnalysisServicesEndpointSuffix = "asazure.usgovcloudapi.net";
155+
public const string GermanAnalysisServicesEndpointSuffix = "asazure.cloudapi.de";
145156
}
146157
}

src/Authentication.Abstractions/Extensions/AzureEnvironmentExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ public static void SetEndpoint(this IAzureEnvironment environment, string endpoi
271271
case AzureEnvironment.ExtendedEndpoint.OperationalInsightsEndpoint:
272272
environment.SetProperty(AzureEnvironment.ExtendedEndpoint.OperationalInsightsEndpoint, propertyValue);
273273
break;
274+
case AzureEnvironment.ExtendedEndpoint.AnalysisServicesEndpointSuffix:
275+
environment.SetProperty(AzureEnvironment.ExtendedEndpoint.AnalysisServicesEndpointSuffix, propertyValue);
276+
break;
277+
274278
}
275279
}
276280
}

src/Authentication.ResourceManager/Models/PSAzureEnvironment.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,21 @@ public string AzureOperationalInsightsEndpoint
257257
}
258258
}
259259

260+
/// <summary>
261+
/// The domain name suffix for Azure Analysis Services
262+
/// </summary>
263+
public string AzureAnalysisServicesEndpointSuffix
264+
{
265+
get
266+
{
267+
return this.GetEndpointSuffix(AzureEnvironment.ExtendedEndpoint.AnalysisServicesEndpointSuffix);
268+
}
269+
set
270+
{
271+
this.SetEndpoint(AzureEnvironment.ExtendedEndpoint.AnalysisServicesEndpointSuffix, value);
272+
}
273+
}
274+
260275
public IList<string> VersionProfiles { get; } = new List<string>();
261276

262277
public IDictionary<string, string> ExtendedProperties { get; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

src/Authentication/Authentication/ProtectedFileTokenCache.cs

Lines changed: 6 additions & 2 deletions
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

Lines changed: 61 additions & 5 deletions
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

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Authentication/Properties/Resources.resx

Lines changed: 4 additions & 1 deletion
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

Lines changed: 6 additions & 0 deletions
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)