| | | 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 System; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Microsoft.Azure.Amqp; |
| | | 9 | | using Microsoft.Azure.Amqp.Framing; |
| | | 10 | | using Microsoft.Azure.ServiceBus.Primitives; |
| | | 11 | | |
| | | 12 | | internal abstract class AmqpLinkCreator |
| | | 13 | | { |
| | | 14 | | readonly string entityPath; |
| | | 15 | | readonly ServiceBusConnection serviceBusConnection; |
| | | 16 | | readonly Uri endpointAddress; |
| | | 17 | | readonly string[] audience; |
| | | 18 | | readonly string[] requiredClaims; |
| | | 19 | | readonly ICbsTokenProvider cbsTokenProvider; |
| | | 20 | | readonly AmqpLinkSettings amqpLinkSettings; |
| | | 21 | | |
| | 0 | 22 | | protected AmqpLinkCreator(string entityPath, ServiceBusConnection serviceBusConnection, Uri endpointAddress, str |
| | | 23 | | { |
| | 0 | 24 | | this.entityPath = entityPath; |
| | 0 | 25 | | this.serviceBusConnection = serviceBusConnection; |
| | 0 | 26 | | this.endpointAddress = endpointAddress; |
| | 0 | 27 | | this.audience = audience; |
| | 0 | 28 | | this.requiredClaims = requiredClaims; |
| | 0 | 29 | | this.cbsTokenProvider = cbsTokenProvider; |
| | 0 | 30 | | this.amqpLinkSettings = amqpLinkSettings; |
| | 0 | 31 | | this.ClientId = clientId; |
| | 0 | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | protected string ClientId { get; } |
| | | 35 | | |
| | | 36 | | public async Task<Tuple<AmqpObject, DateTime>> CreateAndOpenAmqpLinkAsync() |
| | | 37 | | { |
| | 0 | 38 | | var timeoutHelper = new TimeoutHelper(this.serviceBusConnection.OperationTimeout, true); |
| | | 39 | | |
| | 0 | 40 | | MessagingEventSource.Log.AmqpGetOrCreateConnectionStart(); |
| | 0 | 41 | | var amqpConnection = await this.serviceBusConnection.ConnectionManager.GetOrCreateAsync(timeoutHelper.Remain |
| | 0 | 42 | | MessagingEventSource.Log.AmqpGetOrCreateConnectionStop(this.entityPath, amqpConnection.ToString(), amqpConne |
| | | 43 | | |
| | | 44 | | // Authenticate over CBS |
| | 0 | 45 | | var cbsLink = amqpConnection.Extensions.Find<AmqpCbsLink>(); |
| | 0 | 46 | | DateTime cbsTokenExpiresAtUtc = DateTime.MaxValue; |
| | | 47 | | |
| | 0 | 48 | | foreach (var resource in this.audience) |
| | | 49 | | { |
| | 0 | 50 | | MessagingEventSource.Log.AmqpSendAuthenticationTokenStart(this.endpointAddress, resource, resource, this |
| | 0 | 51 | | cbsTokenExpiresAtUtc = TimeoutHelper.Min( |
| | 0 | 52 | | cbsTokenExpiresAtUtc, |
| | 0 | 53 | | await cbsLink.SendTokenAsync(this.cbsTokenProvider, this.endpointAddress, resource, resource, this.r |
| | 0 | 54 | | MessagingEventSource.Log.AmqpSendAuthenticationTokenStop(); |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | AmqpSession session = null; |
| | | 58 | | try |
| | | 59 | | { |
| | | 60 | | // Create Session |
| | 0 | 61 | | var amqpSessionSettings = new AmqpSessionSettings { Properties = new Fields() }; |
| | 0 | 62 | | session = amqpConnection.CreateSession(amqpSessionSettings); |
| | 0 | 63 | | await session.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); |
| | 0 | 64 | | } |
| | 0 | 65 | | catch (Exception exception) |
| | | 66 | | { |
| | 0 | 67 | | MessagingEventSource.Log.AmqpSessionCreationException(this.entityPath, amqpConnection, exception); |
| | 0 | 68 | | session?.Abort(); |
| | 0 | 69 | | throw AmqpExceptionHelper.GetClientException(exception, null, session.GetInnerException(), amqpConnectio |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | AmqpObject link = null; |
| | | 73 | | try |
| | | 74 | | { |
| | | 75 | | // Create Link |
| | 0 | 76 | | link = this.OnCreateAmqpLink(amqpConnection, this.amqpLinkSettings, session); |
| | 0 | 77 | | await link.OpenAsync(timeoutHelper.RemainingTime()).ConfigureAwait(false); |
| | 0 | 78 | | return new Tuple<AmqpObject, DateTime>(link, cbsTokenExpiresAtUtc); |
| | | 79 | | } |
| | 0 | 80 | | catch (Exception exception) |
| | | 81 | | { |
| | 0 | 82 | | MessagingEventSource.Log.AmqpLinkCreationException( |
| | 0 | 83 | | this.entityPath, |
| | 0 | 84 | | session, |
| | 0 | 85 | | amqpConnection, |
| | 0 | 86 | | exception); |
| | | 87 | | |
| | 0 | 88 | | session.SafeClose(exception); |
| | 0 | 89 | | throw AmqpExceptionHelper.GetClientException(exception, null, link?.GetInnerException(), amqpConnection. |
| | | 90 | | } |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | protected abstract AmqpObject OnCreateAmqpLink(AmqpConnection connection, AmqpLinkSettings linkSettings, AmqpSes |
| | | 94 | | } |
| | | 95 | | } |