Skip to content

Commit 1580d0b

Browse files
committed
Merge remote-tracking branch 'azure/preview' into server2015
2 parents 6e6ca80 + b87bc4e commit 1580d0b

File tree

263 files changed

+157407
-26120
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

263 files changed

+157407
-26120
lines changed

src/Common/Commands.Common.Authentication.Abstractions/AzureEnvironment.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class AzureEnvironment : IAzureEnvironment
5252
AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix = AzureEnvironmentConstants.AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix,
5353
AzureDataLakeStoreFileSystemEndpointSuffix = AzureEnvironmentConstants.AzureDataLakeStoreFileSystemEndpointSuffix,
5454
GraphEndpointResourceId = AzureEnvironmentConstants.AzureGraphEndpoint,
55+
DataLakeEndpointResourceId = AzureEnvironmentConstants.AzureDataLakeServiceEndpointResourceId,
5556
AdTenant = "Common"
5657
}
5758
},
@@ -222,6 +223,11 @@ public AzureEnvironment(IAzureEnvironment other)
222223
/// </summary>
223224
public string GraphEndpointResourceId { get; set; }
224225

226+
/// <summary>
227+
/// The token audience required for communicating with the Azure Active Directory Data Lake service in this environment
228+
/// </summary>
229+
public string DataLakeEndpointResourceId { get; set; }
230+
225231
/// <summary>
226232
/// The domain name suffix for Azure DataLake Catalog and Job services created in this environment
227233
/// </summary>
@@ -268,7 +274,8 @@ public static class Endpoint
268274
PublishSettingsFileUrl = "PublishSettingsFileUrl",
269275
ManagementPortalUrl = "ManagementPortalUrl",
270276
AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix = "AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix",
271-
AzureDataLakeStoreFileSystemEndpointSuffix = "AzureDataLakeStoreFileSystemEndpointSuffix";
277+
AzureDataLakeStoreFileSystemEndpointSuffix = "AzureDataLakeStoreFileSystemEndpointSuffix",
278+
DataLakeEndpointResourceId = "DataLakeEndpointResourceId";
272279

273280
}
274281
}

src/Common/Commands.Common.Authentication.Abstractions/AzureEnvironmentConstants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,10 @@ public static class AzureEnvironmentConstants
120120
/// </summary>
121121
public const string AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix = "azuredatalakeanalytics.net";
122122
public const string AzureDataLakeStoreFileSystemEndpointSuffix = "azuredatalakestore.net";
123+
124+
/// <summary>
125+
/// The token audience for authorizing DataLake requests
126+
/// </summary>
127+
public const string AzureDataLakeServiceEndpointResourceId = "https://datalake.azure.net";
123128
}
124129
}

src/Common/Commands.Common.Authentication.Abstractions/Extensions/AzureEnvironmentExtensions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public static bool TryGetEndpointUrl(this IAzureEnvironment environment, string
6262
case AzureEnvironment.Endpoint.GraphEndpointResourceId:
6363
endpoint = new Uri(environment.GraphEndpointResourceId);
6464
break;
65+
case AzureEnvironment.Endpoint.DataLakeEndpointResourceId:
66+
endpoint = new Uri(environment.DataLakeEndpointResourceId);
67+
break;
6568
default:
6669
result = false;
6770
break;
@@ -220,6 +223,9 @@ public static void SetEndpoint(this IAzureEnvironment environment, string endpoi
220223
case AzureEnvironment.Endpoint.AzureDataLakeStoreFileSystemEndpointSuffix:
221224
environment.AzureDataLakeStoreFileSystemEndpointSuffix = propertyValue;
222225
break;
226+
case AzureEnvironment.Endpoint.DataLakeEndpointResourceId:
227+
environment.DataLakeEndpointResourceId = propertyValue;
228+
break;
223229
case AzureEnvironment.Endpoint.ActiveDirectory:
224230
environment.ActiveDirectoryAuthority = propertyValue;
225231
break;
@@ -258,6 +264,12 @@ public static string GetTokenAudience(this IAzureEnvironment environment, string
258264
{
259265
resource = AzureEnvironment.Endpoint.GraphEndpointResourceId;
260266
}
267+
else if (targetEndpoint == AzureEnvironment.Endpoint.AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix ||
268+
targetEndpoint == AzureEnvironment.Endpoint.AzureDataLakeStoreFileSystemEndpointSuffix ||
269+
targetEndpoint == AzureEnvironment.Endpoint.DataLakeEndpointResourceId)
270+
{
271+
resource = AzureEnvironment.Endpoint.DataLakeEndpointResourceId;
272+
}
261273

262274
return resource;
263275
}

src/Common/Commands.Common.Authentication.Abstractions/Interfaces/IAzureEnvironment.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ public interface IAzureEnvironment : IExtensibleModel
102102
/// </summary>
103103
string GraphEndpointResourceId { get; set; }
104104

105+
/// <summary>
106+
/// The token audience required to authenticate with the Azure Active Directory Data Lake services
107+
/// </summary>
108+
string DataLakeEndpointResourceId { get; set; }
109+
105110
/// <summary>
106111
/// The domain name suffix for Azure DataLake Catalog and Job services
107112
/// </summary>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.Common.Authentication;
16+
using Microsoft.Azure.Commands.Common.Authentication.Factories;
17+
using Microsoft.Azure.Commands.Common.Authentication.Models;
18+
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
19+
using System;
20+
using System.Collections.Generic;
21+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
22+
using Xunit;
23+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
24+
using System.IO;
25+
26+
namespace Common.Authentication.Test
27+
{
28+
public class AzureSessionTests
29+
{
30+
[Fact]
31+
[Trait(Category.AcceptanceType, Category.CheckIn)]
32+
public void InitializerCreatesTokenCacheFile()
33+
{
34+
AzureSessionInitializer.InitializeAzureSession();
35+
IAzureSession oldSession = null;
36+
try
37+
{
38+
oldSession = AzureSession.Instance;
39+
}
40+
catch { }
41+
try
42+
{
43+
var store = new MemoryDataStore();
44+
AzureSessionInitializer.CreateOrReplaceSession(store);
45+
var session = AzureSession.Instance;
46+
var tokenCacheFile = Path.Combine(session.ProfileDirectory, session.TokenCacheFile);
47+
Assert.True(store.FileExists(tokenCacheFile));
48+
49+
}
50+
finally
51+
{
52+
AzureSession.Initialize(() => oldSession, true);
53+
}
54+
}
55+
56+
[Fact]
57+
[Trait(Category.AcceptanceType, Category.CheckIn)]
58+
public void TokenCacheIgnoresInvalidData()
59+
{
60+
var store = new AzureTokenCache { CacheData = new byte[] { 3, 0, 0, 0, 0, 0, 0, 0 } };
61+
var cache = new AuthenticationStoreTokenCache(store);
62+
Assert.NotEqual(cache.CacheData, store.CacheData);
63+
}
64+
65+
[Fact]
66+
[Trait(Category.AcceptanceType, Category.CheckIn)]
67+
public void TokenCacheUsesValidData()
68+
{
69+
var store = new AzureTokenCache { CacheData = new byte[] { 2, 0, 0, 0, 0, 0, 0, 0 } };
70+
var cache = new AuthenticationStoreTokenCache(store);
71+
Assert.Equal(cache.CacheData, store.CacheData);
72+
}
73+
74+
}
75+
}

src/Common/Commands.Common.Authentication.Test/Commands.Common.Authentication.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
</Reference>
121121
</ItemGroup>
122122
<ItemGroup>
123+
<Compile Include="AzureSessionTests.cs" />
123124
<Compile Include="ClientFactoryHandlerTests.cs" />
124125
<Compile Include="ClientFactoryTests.cs" />
125126
<Compile Include="AuthenticationFactoryTests.cs" />

src/Common/Commands.Common.Authentication/Authentication/AuthenticationStoreTokenCache.cs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,17 @@ namespace Microsoft.Azure.Commands.Common.Authentication
2222
[Serializable]
2323
public class AuthenticationStoreTokenCache : TokenCache, IAzureTokenCache, IDisposable
2424
{
25-
AzureTokenCache _tokenStore;
26-
25+
IAzureTokenCache _store = new AzureTokenCache();
2726
public byte[] CacheData
2827
{
2928
get
3029
{
31-
return _tokenStore.CacheData;
30+
return Serialize();
3231
}
3332

3433
set
3534
{
36-
this.Clear();
37-
_tokenStore.CacheData = value;
35+
this.Deserialize(value);
3836
}
3937
}
4038

@@ -45,10 +43,9 @@ public AuthenticationStoreTokenCache(AzureTokenCache store) : base()
4543
throw new ArgumentNullException("store");
4644
}
4745

48-
_tokenStore = store;
49-
if (_tokenStore != null && _tokenStore.CacheData != null && _tokenStore.CacheData.Length > 0)
46+
if (store.CacheData != null && store.CacheData.Length > 0)
5047
{
51-
base.Deserialize(_tokenStore.CacheData);
48+
CacheData = store.CacheData;
5249
}
5350

5451
AfterAccess += HandleAfterAccess;
@@ -59,41 +56,33 @@ public AuthenticationStoreTokenCache(AzureTokenCache store) : base()
5956
/// </summary>
6057
/// <param name="cache">The cache to copy</param>
6158
/// <param name="store">The store to use for persisting state</param>
62-
public AuthenticationStoreTokenCache(TokenCache cache, AzureTokenCache store) : this(store)
59+
public AuthenticationStoreTokenCache(TokenCache cache) : base()
6360
{
6461
if (null == cache)
6562
{
6663
throw new ArgumentNullException("Cache");
6764
}
6865

69-
Deserialize(cache.Serialize());
70-
}
71-
72-
/// <summary>
73-
/// Create a token cache, copying any data from the given token cache
74-
/// </summary>
75-
/// <param name="cache">The cache to copy</param>
76-
public AuthenticationStoreTokenCache(TokenCache cache) : this(cache, new AzureTokenCache())
77-
{
66+
CacheData = cache.Serialize();
67+
AfterAccess += HandleAfterAccess;
7868
}
7969

80-
8170
public void HandleAfterAccess(TokenCacheNotificationArgs args)
8271
{
8372
if (HasStateChanged)
8473
{
85-
_tokenStore.CacheData = Serialize();
74+
_store.CacheData = Serialize();
8675
}
8776
}
8877

8978
protected virtual void Dispose(bool disposing)
9079
{
9180
if (disposing)
9281
{
93-
var cache = Interlocked.Exchange(ref _tokenStore, null);
82+
var cache = Interlocked.Exchange(ref _store, null);
9483
if (cache != null)
9584
{
96-
cache.CacheData = base.Serialize();
85+
cache.CacheData = Serialize();
9786
}
9887
}
9988
}

src/Common/Commands.Common.Authentication/Authentication/ProtectedFileTokenCache.cs

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public class ProtectedFileTokenCache : TokenCache, IAzureTokenCache
3737

3838
private static readonly Lazy<ProtectedFileTokenCache> instance = new Lazy<ProtectedFileTokenCache>(() => new ProtectedFileTokenCache());
3939

40+
IDataStore _store;
41+
4042
public byte[] CacheData
4143
{
4244
get
@@ -47,32 +49,37 @@ public byte[] CacheData
4749
set
4850
{
4951
Deserialize(value);
52+
HasStateChanged = true;
53+
EnsureStateSaved();
5054
}
5155
}
5256

5357
// Initializes the cache against a local file.
5458
// If the file is already present, it loads its content in the ADAL cache
5559
private ProtectedFileTokenCache()
5660
{
61+
_store = AzureSession.Instance.DataStore;
5762
Initialize(CacheFileName);
5863
}
5964

60-
public ProtectedFileTokenCache(byte[] inputData)
65+
public ProtectedFileTokenCache(byte[] inputData, IDataStore store = null) : this(CacheFileName, store)
6166
{
62-
AfterAccess = AfterAccessNotification;
63-
BeforeAccess = BeforeAccessNotification;
6467
CacheData = inputData;
6568
}
6669

70+
public ProtectedFileTokenCache(string cacheFile, IDataStore store = null)
71+
{
72+
_store = store ?? AzureSession.Instance.DataStore;
73+
Initialize(cacheFile);
74+
}
75+
6776
private void Initialize(string fileName)
6877
{
69-
AfterAccess = AfterAccessNotification;
70-
BeforeAccess = BeforeAccessNotification;
7178
lock (fileLock)
7279
{
73-
if (AzureSession.Instance.DataStore.FileExists(fileName))
80+
if (_store.FileExists(fileName))
7481
{
75-
var existingData = AzureSession.Instance.DataStore.ReadFileAsBytes(fileName);
82+
var existingData = _store.ReadFileAsBytes(fileName);
7683
if (existingData != null)
7784
{
7885
try
@@ -81,25 +88,26 @@ private void Initialize(string fileName)
8188
}
8289
catch (CryptographicException)
8390
{
84-
AzureSession.Instance.DataStore.DeleteFile(fileName);
91+
_store.DeleteFile(fileName);
8592
}
8693
}
8794
}
95+
96+
// Create the file to start with
97+
_store.WriteFile(CacheFileName, ProtectedData.Protect(Serialize(), null, DataProtectionScope.CurrentUser));
8898
}
89-
}
9099

91-
public ProtectedFileTokenCache(string cacheFile)
92-
{
93-
Initialize(cacheFile);
100+
AfterAccess = AfterAccessNotification;
101+
BeforeAccess = BeforeAccessNotification;
94102
}
95103

96104
// Empties the persistent store.
97105
public override void Clear()
98106
{
99107
base.Clear();
100-
if (AzureSession.Instance.DataStore.FileExists(CacheFileName))
108+
if (_store.FileExists(CacheFileName))
101109
{
102-
AzureSession.Instance.DataStore.DeleteFile(CacheFileName);
110+
_store.DeleteFile(CacheFileName);
103111
}
104112
}
105113

@@ -109,9 +117,9 @@ void BeforeAccessNotification(TokenCacheNotificationArgs args)
109117
{
110118
lock (fileLock)
111119
{
112-
if (AzureSession.Instance.DataStore.FileExists(CacheFileName))
120+
if (_store.FileExists(CacheFileName))
113121
{
114-
var existingData = AzureSession.Instance.DataStore.ReadFileAsBytes(CacheFileName);
122+
var existingData = _store.ReadFileAsBytes(CacheFileName);
115123
if (existingData != null)
116124
{
117125
try
@@ -120,7 +128,7 @@ void BeforeAccessNotification(TokenCacheNotificationArgs args)
120128
}
121129
catch (CryptographicException)
122130
{
123-
AzureSession.Instance.DataStore.DeleteFile(CacheFileName);
131+
_store.DeleteFile(CacheFileName);
124132
}
125133
}
126134
}
@@ -131,13 +139,18 @@ void BeforeAccessNotification(TokenCacheNotificationArgs args)
131139
void AfterAccessNotification(TokenCacheNotificationArgs args)
132140
{
133141
// if the access operation resulted in a cache update
134-
if (HasStateChanged)
142+
EnsureStateSaved();
143+
}
144+
145+
void EnsureStateSaved()
146+
{
147+
lock (fileLock)
135148
{
136-
lock (fileLock)
149+
if (HasStateChanged)
137150
{
138151
// reflect changes in the persistent store
139-
AzureSession.Instance.DataStore.WriteFile(CacheFileName,
140-
ProtectedData.Protect(Serialize(), null, DataProtectionScope.CurrentUser));
152+
_store.WriteFile(CacheFileName,
153+
ProtectedData.Protect(Serialize(), null, DataProtectionScope.CurrentUser));
141154
// once the write operation took place, restore the HasStateChanged bit to false
142155
HasStateChanged = false;
143156
}

0 commit comments

Comments
 (0)