| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus.Amqp |
| | 5 | | { |
| | 6 | | using Azure.Amqp; |
| | 7 | | using Primitives; |
| | 8 | | using System; |
| | 9 | | using System.Threading; |
| | 10 | | using System.Threading.Tasks; |
| | 11 | |
|
| | 12 | | sealed class ActiveClientLinkManager |
| | 13 | | { |
| 0 | 14 | | static readonly TimeSpan SendTokenTimeout = TimeSpan.FromMinutes(1); |
| 0 | 15 | | static readonly TimeSpan TokenRefreshBuffer = TimeSpan.FromSeconds(10); |
| 0 | 16 | | static readonly TimeSpan MaxTokenRefreshTime = TimeSpan.FromDays(30); |
| | 17 | |
|
| | 18 | | readonly string clientId; |
| | 19 | | readonly RetryPolicy retryPolicy; |
| | 20 | | readonly ICbsTokenProvider cbsTokenProvider; |
| | 21 | | Timer sendReceiveLinkCbsTokenRenewalTimer; |
| | 22 | | Timer requestResponseLinkCbsTokenRenewalTimer; |
| | 23 | |
|
| | 24 | | ActiveSendReceiveClientLink activeSendReceiveClientLink; |
| | 25 | | ActiveRequestResponseLink activeRequestResponseClientLink; |
| | 26 | |
|
| 12 | 27 | | public ActiveClientLinkManager(ClientEntity client, ICbsTokenProvider tokenProvider) |
| | 28 | | { |
| 12 | 29 | | this.clientId = client.ClientId; |
| 12 | 30 | | this.retryPolicy = client.RetryPolicy ?? RetryPolicy.Default; |
| 12 | 31 | | this.cbsTokenProvider = tokenProvider; |
| 12 | 32 | | this.sendReceiveLinkCbsTokenRenewalTimer = new Timer(OnRenewSendReceiveCbsToken, this, Timeout.Infinite, Tim |
| 12 | 33 | | this.requestResponseLinkCbsTokenRenewalTimer = new Timer(OnRenewRequestResponseCbsToken, this, Timeout.Infin |
| 12 | 34 | | } |
| | 35 | |
|
| | 36 | | public void Close() |
| | 37 | | { |
| 0 | 38 | | this.sendReceiveLinkCbsTokenRenewalTimer.Dispose(); |
| 0 | 39 | | this.sendReceiveLinkCbsTokenRenewalTimer = null; |
| 0 | 40 | | this.requestResponseLinkCbsTokenRenewalTimer.Dispose(); |
| 0 | 41 | | this.requestResponseLinkCbsTokenRenewalTimer = null; |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | public void SetActiveSendReceiveLink(ActiveSendReceiveClientLink sendReceiveClientLink) |
| | 45 | | { |
| 0 | 46 | | this.activeSendReceiveClientLink = sendReceiveClientLink; |
| 0 | 47 | | this.activeSendReceiveClientLink.Link.Closed += this.OnSendReceiveLinkClosed; |
| 0 | 48 | | if (this.activeSendReceiveClientLink.Link.State == AmqpObjectState.Opened) |
| | 49 | | { |
| 0 | 50 | | this.SetRenewCbsTokenTimer(sendReceiveClientLink); |
| | 51 | | } |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | void OnSendReceiveLinkClosed(object sender, EventArgs e) |
| | 55 | | { |
| 0 | 56 | | this.ChangeRenewTimer(this.activeSendReceiveClientLink, Timeout.InfiniteTimeSpan); |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | public void SetActiveRequestResponseLink(ActiveRequestResponseLink requestResponseLink) |
| | 60 | | { |
| 0 | 61 | | this.activeRequestResponseClientLink = requestResponseLink; |
| 0 | 62 | | this.activeRequestResponseClientLink.Link.Closed += this.OnRequestResponseLinkClosed; |
| 0 | 63 | | if (this.activeRequestResponseClientLink.Link.State == AmqpObjectState.Opened) |
| | 64 | | { |
| 0 | 65 | | this.SetRenewCbsTokenTimer(requestResponseLink); |
| | 66 | | } |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | static async void OnRenewSendReceiveCbsToken(object state) |
| | 70 | | { |
| 0 | 71 | | var activeClientLinkManager = (ActiveClientLinkManager)state; |
| 0 | 72 | | await activeClientLinkManager.RenewCbsTokenAsync(activeClientLinkManager.activeSendReceiveClientLink).Config |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | static async void OnRenewRequestResponseCbsToken(object state) |
| | 76 | | { |
| 0 | 77 | | var activeClientLinkManager = (ActiveClientLinkManager)state; |
| 0 | 78 | | await activeClientLinkManager.RenewCbsTokenAsync(activeClientLinkManager.activeRequestResponseClientLink).Co |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | async Task RenewCbsTokenAsync(ActiveClientLinkObject activeClientLinkObject) |
| | 82 | | { |
| | 83 | | try |
| | 84 | | { |
| 0 | 85 | | var cbsLink = activeClientLinkObject.Connection.Extensions.Find<AmqpCbsLink>() ?? new AmqpCbsLink(active |
| 0 | 86 | | DateTime cbsTokenExpiresAtUtc = DateTime.MaxValue; |
| | 87 | |
|
| 0 | 88 | | foreach (var resource in activeClientLinkObject.Audience) |
| | 89 | | { |
| 0 | 90 | | MessagingEventSource.Log.AmqpSendAuthenticationTokenStart(activeClientLinkObject.EndpointUri, resour |
| | 91 | |
|
| 0 | 92 | | await this.retryPolicy.RunOperation( |
| 0 | 93 | | async () => |
| 0 | 94 | | { |
| 0 | 95 | | cbsTokenExpiresAtUtc = TimeoutHelper.Min( |
| 0 | 96 | | cbsTokenExpiresAtUtc, |
| 0 | 97 | | await cbsLink.SendTokenAsync( |
| 0 | 98 | | this.cbsTokenProvider, |
| 0 | 99 | | activeClientLinkObject.EndpointUri, |
| 0 | 100 | | resource, |
| 0 | 101 | | resource, |
| 0 | 102 | | activeClientLinkObject.RequiredClaims, |
| 0 | 103 | | ActiveClientLinkManager.SendTokenTimeout).ConfigureAwait(false)); |
| 0 | 104 | | }, ActiveClientLinkManager.SendTokenTimeout).ConfigureAwait(false); |
| | 105 | |
|
| 0 | 106 | | MessagingEventSource.Log.AmqpSendAuthenticationTokenStop(); |
| | 107 | | } |
| | 108 | |
|
| 0 | 109 | | activeClientLinkObject.AuthorizationValidUntilUtc = cbsTokenExpiresAtUtc; |
| 0 | 110 | | this.SetRenewCbsTokenTimer(activeClientLinkObject); |
| 0 | 111 | | } |
| 0 | 112 | | catch (Exception e) |
| | 113 | | { |
| | 114 | | // failed to refresh token, no need to do anything since the server will shut the link itself |
| 0 | 115 | | MessagingEventSource.Log.AmqpSendAuthenticationTokenException(this.clientId, e); |
| | 116 | |
|
| 0 | 117 | | this.ChangeRenewTimer(activeClientLinkObject, Timeout.InfiniteTimeSpan); |
| 0 | 118 | | } |
| 0 | 119 | | } |
| | 120 | |
|
| | 121 | | void OnRequestResponseLinkClosed(object sender, EventArgs e) |
| | 122 | | { |
| 0 | 123 | | this.ChangeRenewTimer(this.activeRequestResponseClientLink, Timeout.InfiniteTimeSpan); |
| 0 | 124 | | } |
| | 125 | |
|
| | 126 | | void SetRenewCbsTokenTimer(ActiveClientLinkObject activeClientLinkObject) |
| | 127 | | { |
| 0 | 128 | | var utcNow = DateTime.UtcNow; |
| 0 | 129 | | if (activeClientLinkObject.AuthorizationValidUntilUtc < utcNow) |
| | 130 | | { |
| 0 | 131 | | return; |
| | 132 | | } |
| | 133 | |
|
| 0 | 134 | | var interval = activeClientLinkObject.AuthorizationValidUntilUtc.Subtract(utcNow) - ActiveClientLinkManager. |
| 0 | 135 | | if (interval < ActiveClientLinkManager.TokenRefreshBuffer) |
| 0 | 136 | | interval = TimeSpan.Zero; |
| | 137 | |
|
| 0 | 138 | | interval = TimeoutHelper.Min(interval, ActiveClientLinkManager.MaxTokenRefreshTime); |
| | 139 | |
|
| 0 | 140 | | this.ChangeRenewTimer(activeClientLinkObject, interval); |
| 0 | 141 | | } |
| | 142 | |
|
| | 143 | | void ChangeRenewTimer(ActiveClientLinkObject activeClientLinkObject, TimeSpan dueTime) |
| | 144 | | { |
| 0 | 145 | | if (activeClientLinkObject is ActiveSendReceiveClientLink) |
| | 146 | | { |
| 0 | 147 | | this.sendReceiveLinkCbsTokenRenewalTimer?.Change(dueTime, Timeout.InfiniteTimeSpan); |
| | 148 | | } |
| | 149 | | else |
| | 150 | | { |
| 0 | 151 | | this.requestResponseLinkCbsTokenRenewalTimer?.Change(dueTime, Timeout.InfiniteTimeSpan); |
| | 152 | | } |
| 0 | 153 | | } |
| | 154 | | } |
| | 155 | | } |