Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
cc2a8c5
Removed redundant qualifiers
Apr 27, 2020
52cb410
Removed redundant type specifications
Apr 27, 2020
505722d
Adjusted namespaces to match folder structure
Apr 27, 2020
5b98ae7
Added missing access modifiers
Apr 27, 2020
ede08a3
Added missing braces
Apr 27, 2020
3addeeb
Removed redundant argument values
Apr 27, 2020
30e1879
Remove unused values
Apr 27, 2020
eca32c7
Fixed localization warnings
Apr 27, 2020
382ec93
Standardized naming throughout
Apr 27, 2020
bb6ef2e
Cleaned up XML commenting
Apr 27, 2020
2f5eb48
Removed unneeded imports
Apr 27, 2020
6b7a249
Removed redundant field initializers
Apr 27, 2020
edc41c6
Removed redundant casts
Apr 27, 2020
6c53a30
Fixed parameter name in XML comment
Apr 27, 2020
8687e90
Adjusted namespace
Apr 27, 2020
6bfb0c1
Fixed null reference warnings
Apr 27, 2020
c5a8ccf
Refactored Console WriteLine's to TestOutputHelper calls in XUnit tests
Apr 27, 2020
954dd56
Removed redundant import
Apr 27, 2020
4065415
Removed unused variable
Apr 27, 2020
dfdc55e
Removed redundant import
Apr 27, 2020
9a23167
Removed redundant "this" qualifier
Apr 27, 2020
c342da5
Removed redundant semicolon
Apr 27, 2020
af9f2b4
Removed unused values
Apr 27, 2020
d79feb2
Used var throughout system
Apr 27, 2020
a115738
Set instantiated value to unused variable
Apr 27, 2020
7498a30
Removed unnecessary imports
Apr 28, 2020
17be779
Removed redundant constructor
Apr 28, 2020
f1690a1
Fixed redundant type check
Apr 28, 2020
dddf106
Fixed backward parameters in ArgumentException construction
Apr 28, 2020
1097d6b
Removed unneeded constant
Apr 28, 2020
103d3e4
Removed unused method
Apr 28, 2020
e911f32
Removed redundant semicolon
Apr 28, 2020
fd7b1be
Fixed access modifier of Main method
Apr 28, 2020
22258fa
Commented acknowledging disregarded catch
Apr 28, 2020
bc96feb
Refactored lambda to be method group call
Apr 28, 2020
7b36bc5
Removed redundant ToString calls
Apr 28, 2020
ce7081e
Removed redundant import
Apr 28, 2020
fddba5c
Fixed null reference warnings
Apr 28, 2020
8b6bcef
Removed redundant parenthesis
Apr 28, 2020
86a63de
Fixed null reference warning
Apr 28, 2020
42ad7ad
Refactored property name from "parameters" to "properties" on SqlRule…
Apr 28, 2020
78952e2
Fixed null reference warning
Apr 28, 2020
ed3081d
Refactored exception to better exception type
Apr 28, 2020
b9e6dd7
Fixed null reference warning
Apr 28, 2020
743d1f1
Removed redundant check
Apr 28, 2020
e7e8979
Refactored property from "properties" to "FilterProperties" in Correl…
Apr 28, 2020
8c06c0f
Fixed localizable string warning
Apr 28, 2020
d699471
Fixed naming of field
Apr 28, 2020
75e6c80
Removed redundant type check
Apr 28, 2020
2554309
Removed redundant type check
Apr 28, 2020
a057c98
Refactored log statement to use string formatter
Apr 28, 2020
070be5a
Removed redundant initializer
Apr 28, 2020
ae122b3
Adjusted namespace to match directory
Apr 28, 2020
2e1dc49
Fixed naming of fields
Apr 28, 2020
95dd35d
Simplified method names in converter
Apr 28, 2020
4c541cb
Converter "if" statements to pattern matching "switch"
Apr 28, 2020
28eb14a
Removed redundant type check
Apr 28, 2020
68fb27e
Assigned instantiated value to ignored variable
Apr 28, 2020
04f3f7d
Fixed constant name
Apr 28, 2020
00f921d
Fixed spelling mistake in constant name
Apr 28, 2020
706e68d
Refactored fields to readonly/const where available
Apr 28, 2020
2f26926
Removed redundant property initialization
Apr 28, 2020
c9bf232
Refactored namespace to match directory
Apr 28, 2020
d4bf4fe
Fixed string localization warning
Apr 28, 2020
925f0d8
Fixed type reference
Apr 28, 2020
de9aed9
Assigned value to unused variable to satisfy warnings
Apr 28, 2020
3b3dca1
Sealed properties to prevent virtual calls in constructors
Apr 28, 2020
a506fd3
Removed redundant constructor parameters
Apr 28, 2020
3d2bce7
Sealed property to prevent virtual call in constructor
Apr 28, 2020
5a51661
Reverted changes to namespace for Program
Apr 28, 2020
4997d8f
Fixed string localization warning
Apr 28, 2020
61d97c2
Fixed XML see reference
Apr 28, 2020
3ccfb2a
Fixed XML see reference
Apr 28, 2020
565dde6
Fixed null reference warning
Apr 28, 2020
d205dbd
Fixed field naming
Apr 28, 2020
8a5cfc6
Removed redundant property initializations
Apr 28, 2020
8428c9d
Fixed spelling type in comment
Apr 28, 2020
065914b
Fixed field naming
Apr 28, 2020
7516adc
Added conditional access modifier
Apr 28, 2020
baf7d56
Simplified property accessor
Apr 28, 2020
b389d60
Rolled back some changes
Apr 28, 2020
0abba6a
Fixed broken tests
Apr 28, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,146 +9,149 @@ namespace Microsoft.Azure.ServiceBus.Amqp
using System.Threading;
using System.Threading.Tasks;

sealed class ActiveClientLinkManager
internal sealed class ActiveClientLinkManager
{
static readonly TimeSpan SendTokenTimeout = TimeSpan.FromMinutes(1);
static readonly TimeSpan TokenRefreshBuffer = TimeSpan.FromSeconds(10);
static readonly TimeSpan MaxTokenRefreshTime = TimeSpan.FromDays(30);
private static readonly TimeSpan SendTokenTimeout = TimeSpan.FromMinutes(1);
private static readonly TimeSpan TokenRefreshBuffer = TimeSpan.FromSeconds(10);
private static readonly TimeSpan MaxTokenRefreshTime = TimeSpan.FromDays(30);

readonly string clientId;
readonly RetryPolicy retryPolicy;
readonly ICbsTokenProvider cbsTokenProvider;
Timer sendReceiveLinkCbsTokenRenewalTimer;
Timer requestResponseLinkCbsTokenRenewalTimer;
private readonly string _clientId;
private readonly RetryPolicy _retryPolicy;
private readonly ICbsTokenProvider _cbsTokenProvider;
private Timer _sendReceiveLinkCbsTokenRenewalTimer;
private Timer _requestResponseLinkCbsTokenRenewalTimer;

ActiveSendReceiveClientLink activeSendReceiveClientLink;
ActiveRequestResponseLink activeRequestResponseClientLink;
private ActiveSendReceiveClientLink _activeSendReceiveClientLink;
private ActiveRequestResponseLink _activeRequestResponseClientLink;

public ActiveClientLinkManager(ClientEntity client, ICbsTokenProvider tokenProvider)
{
this.clientId = client.ClientId;
this.retryPolicy = client.RetryPolicy ?? RetryPolicy.Default;
this.cbsTokenProvider = tokenProvider;
this.sendReceiveLinkCbsTokenRenewalTimer = new Timer(OnRenewSendReceiveCbsToken, this, Timeout.Infinite, Timeout.Infinite);
this.requestResponseLinkCbsTokenRenewalTimer = new Timer(OnRenewRequestResponseCbsToken, this, Timeout.Infinite, Timeout.Infinite);
_clientId = client.ClientId;
_retryPolicy = client.RetryPolicy ?? RetryPolicy.Default;
_cbsTokenProvider = tokenProvider;
_sendReceiveLinkCbsTokenRenewalTimer = new Timer(OnRenewSendReceiveCbsToken, this, Timeout.Infinite, Timeout.Infinite);
_requestResponseLinkCbsTokenRenewalTimer = new Timer(OnRenewRequestResponseCbsToken, this, Timeout.Infinite, Timeout.Infinite);
}

public void Close()
{
this.sendReceiveLinkCbsTokenRenewalTimer.Dispose();
this.sendReceiveLinkCbsTokenRenewalTimer = null;
this.requestResponseLinkCbsTokenRenewalTimer.Dispose();
this.requestResponseLinkCbsTokenRenewalTimer = null;
_sendReceiveLinkCbsTokenRenewalTimer.Dispose();
_sendReceiveLinkCbsTokenRenewalTimer = null;
_requestResponseLinkCbsTokenRenewalTimer.Dispose();
_requestResponseLinkCbsTokenRenewalTimer = null;
}

public void SetActiveSendReceiveLink(ActiveSendReceiveClientLink sendReceiveClientLink)
{
this.activeSendReceiveClientLink = sendReceiveClientLink;
this.activeSendReceiveClientLink.Link.Closed += this.OnSendReceiveLinkClosed;
if (this.activeSendReceiveClientLink.Link.State == AmqpObjectState.Opened)
_activeSendReceiveClientLink = sendReceiveClientLink;
_activeSendReceiveClientLink.Link.Closed += OnSendReceiveLinkClosed;
if (_activeSendReceiveClientLink.Link.State == AmqpObjectState.Opened)
{
this.SetRenewCbsTokenTimer(sendReceiveClientLink);
SetRenewCbsTokenTimer(sendReceiveClientLink);
}
}

void OnSendReceiveLinkClosed(object sender, EventArgs e)
private void OnSendReceiveLinkClosed(object sender, EventArgs e)
{
this.ChangeRenewTimer(this.activeSendReceiveClientLink, Timeout.InfiniteTimeSpan);
ChangeRenewTimer(_activeSendReceiveClientLink, Timeout.InfiniteTimeSpan);
}

public void SetActiveRequestResponseLink(ActiveRequestResponseLink requestResponseLink)
{
this.activeRequestResponseClientLink = requestResponseLink;
this.activeRequestResponseClientLink.Link.Closed += this.OnRequestResponseLinkClosed;
if (this.activeRequestResponseClientLink.Link.State == AmqpObjectState.Opened)
_activeRequestResponseClientLink = requestResponseLink;
_activeRequestResponseClientLink.Link.Closed += OnRequestResponseLinkClosed;
if (_activeRequestResponseClientLink.Link.State == AmqpObjectState.Opened)
{
this.SetRenewCbsTokenTimer(requestResponseLink);
SetRenewCbsTokenTimer(requestResponseLink);
}
}

static async void OnRenewSendReceiveCbsToken(object state)
private static async void OnRenewSendReceiveCbsToken(object state)
{
var activeClientLinkManager = (ActiveClientLinkManager)state;
await activeClientLinkManager.RenewCbsTokenAsync(activeClientLinkManager.activeSendReceiveClientLink).ConfigureAwait(false);
await activeClientLinkManager.RenewCbsTokenAsync(activeClientLinkManager._activeSendReceiveClientLink).ConfigureAwait(false);
}

static async void OnRenewRequestResponseCbsToken(object state)
private static async void OnRenewRequestResponseCbsToken(object state)
{
var activeClientLinkManager = (ActiveClientLinkManager)state;
await activeClientLinkManager.RenewCbsTokenAsync(activeClientLinkManager.activeRequestResponseClientLink).ConfigureAwait(false);
await activeClientLinkManager.RenewCbsTokenAsync(activeClientLinkManager._activeRequestResponseClientLink).ConfigureAwait(false);
}

async Task RenewCbsTokenAsync(ActiveClientLinkObject activeClientLinkObject)
private async Task RenewCbsTokenAsync(ActiveClientLinkObject activeClientLinkObject)
{
try
{
var cbsLink = activeClientLinkObject.Connection.Extensions.Find<AmqpCbsLink>() ?? new AmqpCbsLink(activeClientLinkObject.Connection);
DateTime cbsTokenExpiresAtUtc = DateTime.MaxValue;
var cbsTokenExpiresAtUtc = DateTime.MaxValue;

foreach (var resource in activeClientLinkObject.Audience)
{
MessagingEventSource.Log.AmqpSendAuthenticationTokenStart(activeClientLinkObject.EndpointUri, resource, resource, activeClientLinkObject.RequiredClaims);

await this.retryPolicy.RunOperation(
await _retryPolicy.RunOperation(
async () =>
{
cbsTokenExpiresAtUtc = TimeoutHelper.Min(
cbsTokenExpiresAtUtc,
await cbsLink.SendTokenAsync(
this.cbsTokenProvider,
_cbsTokenProvider,
activeClientLinkObject.EndpointUri,
resource,
resource,
activeClientLinkObject.RequiredClaims,
ActiveClientLinkManager.SendTokenTimeout).ConfigureAwait(false));
}, ActiveClientLinkManager.SendTokenTimeout).ConfigureAwait(false);
SendTokenTimeout).ConfigureAwait(false));
}, SendTokenTimeout).ConfigureAwait(false);

MessagingEventSource.Log.AmqpSendAuthenticationTokenStop();
}

activeClientLinkObject.AuthorizationValidUntilUtc = cbsTokenExpiresAtUtc;
this.SetRenewCbsTokenTimer(activeClientLinkObject);
SetRenewCbsTokenTimer(activeClientLinkObject);
}
catch (Exception e)
{
// failed to refresh token, no need to do anything since the server will shut the link itself
MessagingEventSource.Log.AmqpSendAuthenticationTokenException(this.clientId, e);
MessagingEventSource.Log.AmqpSendAuthenticationTokenException(_clientId, e);

this.ChangeRenewTimer(activeClientLinkObject, Timeout.InfiniteTimeSpan);
ChangeRenewTimer(activeClientLinkObject, Timeout.InfiniteTimeSpan);
}
}

void OnRequestResponseLinkClosed(object sender, EventArgs e)
private void OnRequestResponseLinkClosed(object sender, EventArgs e)
{
this.ChangeRenewTimer(this.activeRequestResponseClientLink, Timeout.InfiniteTimeSpan);
ChangeRenewTimer(_activeRequestResponseClientLink, Timeout.InfiniteTimeSpan);
}

void SetRenewCbsTokenTimer(ActiveClientLinkObject activeClientLinkObject)
private void SetRenewCbsTokenTimer(ActiveClientLinkObject activeClientLinkObject)
{
var utcNow = DateTime.UtcNow;
if (activeClientLinkObject.AuthorizationValidUntilUtc < utcNow)
{
return;
}

var interval = activeClientLinkObject.AuthorizationValidUntilUtc.Subtract(utcNow) - ActiveClientLinkManager.TokenRefreshBuffer;
if (interval < ActiveClientLinkManager.TokenRefreshBuffer)
interval = TimeSpan.Zero;
var interval = activeClientLinkObject.AuthorizationValidUntilUtc.Subtract(utcNow) - TokenRefreshBuffer;

if (interval < TokenRefreshBuffer)
{
interval = TimeSpan.Zero;
}

interval = TimeoutHelper.Min(interval, ActiveClientLinkManager.MaxTokenRefreshTime);
interval = TimeoutHelper.Min(interval, MaxTokenRefreshTime);

this.ChangeRenewTimer(activeClientLinkObject, interval);
ChangeRenewTimer(activeClientLinkObject, interval);
}

void ChangeRenewTimer(ActiveClientLinkObject activeClientLinkObject, TimeSpan dueTime)
private void ChangeRenewTimer(ActiveClientLinkObject activeClientLinkObject, TimeSpan dueTime)
{
if (activeClientLinkObject is ActiveSendReceiveClientLink)
{
this.sendReceiveLinkCbsTokenRenewalTimer?.Change(dueTime, Timeout.InfiniteTimeSpan);
_sendReceiveLinkCbsTokenRenewalTimer?.Change(dueTime, Timeout.InfiniteTimeSpan);
}
else
{
this.requestResponseLinkCbsTokenRenewalTimer?.Change(dueTime, Timeout.InfiniteTimeSpan);
_requestResponseLinkCbsTokenRenewalTimer?.Change(dueTime, Timeout.InfiniteTimeSpan);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ namespace Microsoft.Azure.ServiceBus.Amqp
using Microsoft.Azure.Amqp;
using System;

abstract class ActiveClientLinkObject
internal abstract class ActiveClientLinkObject
{
readonly string[] requiredClaims;
private readonly string[] _requiredClaims;

protected ActiveClientLinkObject(AmqpObject amqpLinkObject, Uri endpointUri, string[] audience, string[] requiredClaims, DateTime authorizationValidUntilUtc)
{
this.LinkObject = amqpLinkObject;
this.EndpointUri = endpointUri;
this.Audience = audience;
this.requiredClaims = requiredClaims;
this.AuthorizationValidUntilUtc = authorizationValidUntilUtc;
LinkObject = amqpLinkObject;
EndpointUri = endpointUri;
Audience = audience;
_requiredClaims = requiredClaims;
AuthorizationValidUntilUtc = authorizationValidUntilUtc;
}

public AmqpObject LinkObject { get; }
Expand All @@ -25,7 +25,7 @@ protected ActiveClientLinkObject(AmqpObject amqpLinkObject, Uri endpointUri, st

public Uri EndpointUri { get; }

public string[] RequiredClaims => (string[])this.requiredClaims.Clone();
public string[] RequiredClaims => (string[])_requiredClaims.Clone();

public DateTime AuthorizationValidUntilUtc { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ namespace Microsoft.Azure.ServiceBus.Amqp
using Microsoft.Azure.Amqp;
using System;

sealed class ActiveRequestResponseLink : ActiveClientLinkObject
internal sealed class ActiveRequestResponseLink : ActiveClientLinkObject
{
public ActiveRequestResponseLink(RequestResponseAmqpLink link, Uri endpointUri, string[] audience, string[] requiredClaims, DateTime authorizationValidUntilUtc)
: base(link, endpointUri, audience, requiredClaims, authorizationValidUntilUtc)
{
this.Link = link;
Link = link;
}

public RequestResponseAmqpLink Link { get; }

public override AmqpConnection Connection => this.Link.Session.Connection;
public override AmqpConnection Connection => Link.Session.Connection;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ namespace Microsoft.Azure.ServiceBus.Amqp
using Microsoft.Azure.Amqp;
using System;

sealed class ActiveSendReceiveClientLink : ActiveClientLinkObject
internal sealed class ActiveSendReceiveClientLink : ActiveClientLinkObject
{
public ActiveSendReceiveClientLink(AmqpLink link, Uri endpointUri, string[] audience, string[] requiredClaims, DateTime authorizationValidUntilUtc)
: base(link, endpointUri, audience, requiredClaims, authorizationValidUntilUtc)
{
this.Link = link;
Link = link;
}

public AmqpLink Link { get; }

public override AmqpConnection Connection => this.Link.Session.Connection;
public override AmqpConnection Connection => Link.Session.Connection;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.Azure.ServiceBus.Amqp
using Microsoft.Azure.Amqp;
using Microsoft.Azure.Amqp.Encoding;

class AmqpClientConstants
internal class AmqpClientConstants
{
// AMQP Management Operation
public const string ManagementAddress = "$management";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace Microsoft.Azure.ServiceBus.Amqp
using Microsoft.Azure.Amqp;
using Microsoft.Azure.Amqp.Sasl;
using Microsoft.Azure.Amqp.Transport;
using Microsoft.Azure.ServiceBus.Primitives;
using Primitives;

internal class AmqpConnectionHelper
{
const string CbsSaslMechanismName = "MSSBCBS";
private const string CbsSaslMechanismName = "MSSBCBS";

public static AmqpSettings CreateAmqpSettings(
Version amqpVersion,
Expand All @@ -21,7 +21,7 @@ public static AmqpSettings CreateAmqpSettings(
string sslHostName = null,
bool useWebSockets = false,
bool sslStreamUpgrade = false,
System.Net.NetworkCredential networkCredential = null,
NetworkCredential networkCredential = null,
bool forceTokenProvider = true)
{
var amqpSettings = new AmqpSettings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ namespace Microsoft.Azure.ServiceBus.Amqp
using Microsoft.Azure.Amqp.Encoding;
using Microsoft.Azure.Amqp.Framing;

static class AmqpExceptionHelper
internal static class AmqpExceptionHelper
{
static readonly Dictionary<string, AmqpResponseStatusCode> ConditionToStatusMap = new Dictionary<string, AmqpResponseStatusCode>
private static readonly Dictionary<string, AmqpResponseStatusCode> ConditionToStatusMap = new Dictionary<string, AmqpResponseStatusCode>
{
{ AmqpClientConstants.TimeoutError.Value, AmqpResponseStatusCode.RequestTimeout },
{ AmqpErrorCode.NotFound.Value, AmqpResponseStatusCode.NotFound },
Expand All @@ -42,7 +42,7 @@ static class AmqpExceptionHelper

public static AmqpSymbol GetResponseErrorCondition(AmqpMessage response, AmqpResponseStatusCode statusCode)
{
object condition = response.ApplicationProperties.Map[ManagementConstants.Response.ErrorCondition];
var condition = response.ApplicationProperties.Map[ManagementConstants.Response.ErrorCondition];
if (condition != null)
{
return (AmqpSymbol)condition;
Expand All @@ -63,7 +63,7 @@ public static AmqpSymbol GetResponseErrorCondition(AmqpMessage response, AmqpRes
public static AmqpResponseStatusCode GetResponseStatusCode(this AmqpMessage responseMessage)
{
var amqpResponseStatusCode = AmqpResponseStatusCode.Unused;
object statusCodeValue = responseMessage?.ApplicationProperties.Map[ManagementConstants.Response.StatusCode];
var statusCodeValue = responseMessage?.ApplicationProperties.Map[ManagementConstants.Response.StatusCode];
if (statusCodeValue is int && Enum.IsDefined(typeof(AmqpResponseStatusCode), statusCodeValue))
{
amqpResponseStatusCode = (AmqpResponseStatusCode)statusCodeValue;
Expand All @@ -74,9 +74,9 @@ public static AmqpResponseStatusCode GetResponseStatusCode(this AmqpMessage resp

public static Exception ToMessagingContractException(this AmqpMessage responseMessage, AmqpResponseStatusCode statusCode)
{
AmqpSymbol errorCondition = AmqpExceptionHelper.GetResponseErrorCondition(responseMessage, statusCode);
var errorCondition = GetResponseErrorCondition(responseMessage, statusCode);
var statusDescription = responseMessage.ApplicationProperties.Map[ManagementConstants.Response.StatusDescription] as string ?? errorCondition.Value;
return AmqpExceptionHelper.ToMessagingContractException(errorCondition.Value, statusDescription);
return ToMessagingContractException(errorCondition.Value, statusDescription);
}

public static Exception ToMessagingContractException(this Error error, bool connectionError = false)
Expand All @@ -89,7 +89,7 @@ public static Exception ToMessagingContractException(this Error error, bool conn
return ToMessagingContractException(error.Condition.Value, error.Description, connectionError);
}

static Exception ToMessagingContractException(string condition, string message, bool connectionError = false)
private static Exception ToMessagingContractException(string condition, string message, bool connectionError = false)
{
if (string.Equals(condition, AmqpClientConstants.TimeoutError.Value))
{
Expand Down
Loading