-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove inactive keys from sychronizer
- Loading branch information
1 parent
64b9805
commit 32116fa
Showing
5 changed files
with
79 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 42 additions & 13 deletions
55
test/Microsoft.Azure.SignalR.Common.Tests/Auth/AccessKeySynchronizerFacts.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,72 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Identity; | ||
using Microsoft.Azure.SignalR.Tests.Common; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Xunit; | ||
|
||
namespace Microsoft.Azure.SignalR.Common.Tests.Auth; | ||
|
||
public class AccessKeySynchronizerFacts | ||
{ | ||
[Fact] | ||
public void AddAndRemoveServiceEndpointsTest() | ||
public async void AddRemoveServiceEndpointTest() | ||
{ | ||
var synchronizer = GetInstanceForTest(); | ||
var synchronizer = new AccessKeySynchronizer(); | ||
Assert.Equal(0, synchronizer.Count); | ||
|
||
var credential = new DefaultAzureCredential(); | ||
var endpoint = new TestServiceEndpoint(credential); | ||
synchronizer.AddServiceEndpoint(endpoint); | ||
Assert.Equal(1, synchronizer.Count); | ||
|
||
var field = typeof(MicrosoftEntraAccessKey).GetField("_lastUsedAt", BindingFlags.NonPublic | BindingFlags.Instance); | ||
|
||
var key = Assert.IsType<MicrosoftEntraAccessKey>(endpoint.AccessKey); | ||
var before = Assert.IsType<DateTime>(field.GetValue(key)); | ||
|
||
var source = new CancellationTokenSource(1000); | ||
await Assert.ThrowsAsync<TaskCanceledException>(async () => await key.GenerateAccessTokenAsync("localhost", [], TimeSpan.FromHours(1), AccessTokenAlgorithm.HS256, source.Token)); | ||
var after = Assert.IsType<DateTime>(field.GetValue(key)); | ||
Assert.NotEqual(before, after); | ||
|
||
synchronizer.UpdateAllAccessKey(); | ||
await Task.Delay(TimeSpan.FromSeconds(1)); | ||
Assert.Equal(1, synchronizer.Count); | ||
|
||
key.UpdateAccessKey("foo", "bar"); | ||
field.SetValue(key, DateTime.UtcNow - TimeSpan.FromHours(3)); | ||
synchronizer.UpdateAllAccessKey(); | ||
Assert.Equal(0, synchronizer.Count); | ||
} | ||
|
||
[Fact] | ||
public void HotReloadServiceEndpointTest() | ||
{ | ||
var synchronizer = new AccessKeySynchronizer(); | ||
|
||
var credential = new DefaultAzureCredential(); | ||
var endpoint1 = new TestServiceEndpoint(credential); | ||
var endpoint2 = new TestServiceEndpoint(credential); | ||
|
||
Assert.Equal(0, synchronizer.Count()); | ||
Assert.Equal(0, synchronizer.Count); | ||
synchronizer.UpdateServiceEndpoints([endpoint1]); | ||
Assert.Equal(1, synchronizer.Count()); | ||
Assert.Equal(1, synchronizer.Count); | ||
synchronizer.UpdateServiceEndpoints([endpoint1, endpoint2]); | ||
Assert.Empty(synchronizer.InitializedKeyList); | ||
|
||
Assert.Equal(2, synchronizer.Count()); | ||
Assert.Equal(2, synchronizer.Count); | ||
Assert.True(synchronizer.ContainsKey(endpoint1)); | ||
Assert.True(synchronizer.ContainsKey(endpoint2)); | ||
|
||
synchronizer.UpdateServiceEndpoints([endpoint2]); | ||
Assert.Equal(1, synchronizer.Count()); | ||
Assert.Equal(1, synchronizer.Count); | ||
synchronizer.UpdateServiceEndpoints([]); | ||
Assert.Equal(0, synchronizer.Count()); | ||
Assert.Equal(0, synchronizer.Count); | ||
Assert.Empty(synchronizer.InitializedKeyList); | ||
} | ||
|
||
private static AccessKeySynchronizer GetInstanceForTest() | ||
{ | ||
return new AccessKeySynchronizer(NullLoggerFactory.Instance, false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters