コード例 #1
0
        /// <summary>
        /// Creates shared access signature authorization for the service bus entity. This authorization works on
        /// public Microsoft Azure environments and Windows Azure Pack on prim as well.
        /// </summary>
        /// <param name="namespaceName">The service bus namespace name</param>
        /// <param name="entityName">The fully qualified service bus entity name</param>
        /// <param name="entityType">The service bus entity type (e.g. Queue)</param>
        /// <param name="ruleName">The SAS authorization rule name</param>
        /// <param name="primaryKey">The SAS primary key. It'll be generated if empty</param>
        /// <param name="secondaryKey">The SAS secondary key</param>
        /// <param name="permissions">Set of permissions given to the rule</param>
        /// <returns>The created Shared Access Signature authorization rule</returns>
        public virtual ExtendedAuthorizationRule CreateSharedAccessAuthorization(
            string namespaceName,
            string entityName,
            ServiceBusEntityType entityType,
            string ruleName,
            string primaryKey,
            string secondaryKey,
            params AccessRights[] permissions)
        {
            // Create the SAS authorization rule
            SharedAccessAuthorizationRule rule = new SharedAccessAuthorizationRule(
                ruleName,
                string.IsNullOrEmpty(primaryKey) ? SharedAccessAuthorizationRule.GenerateRandomKey() : primaryKey,
                secondaryKey,
                permissions);

            // Create namespace manager
            NamespaceManager namespaceManager = CreateNamespaceManager(namespaceName);

            // Add the SAS rule and update the entity
            switch (entityType)
            {
            case ServiceBusEntityType.Queue:
                QueueDescription queue = namespaceManager.GetQueue(entityName);
                queue.Authorization.Add(rule);
                namespaceManager.UpdateQueue(queue);
                break;

            case ServiceBusEntityType.Topic:
                TopicDescription topic = namespaceManager.GetTopic(entityName);
                topic.Authorization.Add(rule);
                namespaceManager.UpdateTopic(topic);
                break;

            case ServiceBusEntityType.Relay:
                RelayDescription relay = namespaceManager.GetRelayAsync(entityName).Result;
                relay.Authorization.Add(rule);
                namespaceManager.UpdateRelayAsync(relay).Wait();
                break;

            case ServiceBusEntityType.NotificationHub:
                NotificationHubDescription notificationHub = namespaceManager.GetNotificationHub(entityName);
                notificationHub.Authorization.Add(rule);
                namespaceManager.UpdateNotificationHub(notificationHub);
                break;

            default:
                throw new Exception(string.Format(Resources.ServiceBusEntityTypeNotFound, entityType.ToString()));
            }

            return(CreateExtendedAuthorizationRule(rule, namespaceName, entityName, entityType));
        }
コード例 #2
0
        /// <summary>
        /// Removed shared access signature authorization for the service bus entity.
        /// </summary>
        /// <param name="namespaceName">The service bus namespace name</param>
        /// <param name="entityName">The fully qualified service bus entity name</param>
        /// <param name="entityType">The service bus entity type (e.g. Queue)</param>
        /// <param name="ruleName">The SAS authorization rule name</param>
        public virtual void RemoveAuthorizationRule(
            string namespaceName,
            string entityName,
            ServiceBusEntityType entityType,
            string ruleName)
        {
            bool removed = false;
            SharedAccessAuthorizationRule rule = (SharedAccessAuthorizationRule)GetAuthorizationRule(
                namespaceName,
                entityName,
                entityType,
                ruleName).Rule;

            // Create namespace manager
            NamespaceManager namespaceManager = CreateNamespaceManager(namespaceName);

            // Add the SAS rule and update the entity
            switch (entityType)
            {
            case ServiceBusEntityType.Queue:
                QueueDescription queue = namespaceManager.GetQueue(entityName);
                removed = queue.Authorization.Remove(rule);
                Debug.Assert(removed);
                namespaceManager.UpdateQueue(queue);
                break;

            case ServiceBusEntityType.Topic:
                TopicDescription topic = namespaceManager.GetTopic(entityName);
                removed = topic.Authorization.Remove(rule);
                Debug.Assert(removed);
                namespaceManager.UpdateTopic(topic);
                break;

            case ServiceBusEntityType.Relay:
                RelayDescription relay = namespaceManager.GetRelayAsync(entityName).Result;
                removed = relay.Authorization.Remove(rule);
                Debug.Assert(removed);
                namespaceManager.UpdateRelayAsync(relay).Wait();
                break;

            case ServiceBusEntityType.NotificationHub:
                NotificationHubDescription notificationHub = namespaceManager.GetNotificationHub(entityName);
                removed = notificationHub.Authorization.Remove(rule);
                Debug.Assert(removed);
                namespaceManager.UpdateNotificationHub(notificationHub);
                break;

            default:
                throw new Exception(string.Format(Resources.ServiceBusEntityTypeNotFound, entityType.ToString()));
            }
        }
コード例 #3
0
        private List <ExtendedAuthorizationRule> GetAuthorizationRuleCore(
            string namespaceName,
            string entityName,
            ServiceBusEntityType entityType,
            Predicate <AuthorizationRule> match)
        {
            NamespaceManager         namespaceManager = CreateNamespaceManager(namespaceName);
            List <AuthorizationRule> rules            = null;

            switch (entityType)
            {
            case ServiceBusEntityType.Queue:
                rules = namespaceManager.GetQueue(entityName).Authorization.GetRules(match);
                break;

            case ServiceBusEntityType.Topic:
                rules = namespaceManager.GetTopic(entityName).Authorization.GetRules(match);
                break;

            case ServiceBusEntityType.Relay:
                rules = namespaceManager.GetRelayAsync(entityName).Result.Authorization.GetRules(match);
                break;

            case ServiceBusEntityType.NotificationHub:
                rules = namespaceManager.GetNotificationHub(entityName).Authorization.GetRules(match);
                break;

            default:
                throw new InvalidOperationException();
            }

            return(rules.Select(r => CreateExtendedAuthorizationRule(
                                    r,
                                    namespaceName,
                                    entityName,
                                    entityType)).ToList());
        }
コード例 #4
0
        /// <summary>
        /// Updates shared access signature authorization for the service bus entity. This authorization works on
        /// public Windows Azure environments and Windows Azure Pack on prim as well.
        /// </summary>
        /// <param name="namespaceName">The service bus namespace name</param>
        /// <param name="entityName">The fully qualified service bus entity name</param>
        /// <param name="entityType">The service bus entity type (e.g. Queue)</param>
        /// <param name="ruleName">The SAS authorization rule name</param>
        /// <param name="primaryKey">The SAS primary key. It'll be generated if empty</param>
        /// <param name="secondaryKey">The SAS secondary key</param>
        /// <param name="permissions">Set of permissions given to the rule</param>
        /// <returns>The created Shared Access Signature authorization rule</returns>
        public ExtendedAuthorizationRule UpdateSharedAccessAuthorization(
            string namespaceName,
            string entityName,
            ServiceBusEntityType entityType,
            string ruleName,
            string primaryKey,
            string secondaryKey,
            params AccessRights[] permissions)
        {
            bool removed = false;
            ExtendedAuthorizationRule rule = GetAuthorizationRule(namespaceName, entityName, entityType, ruleName);

            if (null == rule)
            {
                throw new ArgumentException(Resources.ServiceBusAuthorizationRuleNotFound);
            }

            SharedAccessAuthorizationRule oldRule = (SharedAccessAuthorizationRule)rule.Rule;

            SharedAccessAuthorizationRule newRule = new SharedAccessAuthorizationRule(
                ruleName,
                string.IsNullOrEmpty(primaryKey) ? SharedAccessAuthorizationRule.GenerateRandomKey() : primaryKey,
                secondaryKey,
                permissions ?? oldRule.Rights);

            // Create namespace manager
            NamespaceManager namespaceManager = CreateNamespaceManager(namespaceName);

            // Add the SAS rule and update the entity
            switch (entityType)
            {
            case ServiceBusEntityType.Queue:
                QueueDescription queue = namespaceManager.GetQueue(entityName);
                removed = queue.Authorization.Remove(oldRule);
                Debug.Assert(removed);
                queue.Authorization.Add(newRule);
                namespaceManager.UpdateQueue(queue);
                break;

            case ServiceBusEntityType.Topic:
                TopicDescription topic = namespaceManager.GetTopic(entityName);
                removed = topic.Authorization.Remove(oldRule);
                Debug.Assert(removed);
                topic.Authorization.Add(newRule);
                namespaceManager.UpdateTopic(topic);
                break;

            case ServiceBusEntityType.Relay:
                RelayDescription relay = namespaceManager.GetRelayAsync(entityName).Result;
                removed = relay.Authorization.Remove(oldRule);
                Debug.Assert(removed);
                relay.Authorization.Add(newRule);
                namespaceManager.UpdateRelayAsync(relay).Wait();
                break;

            case ServiceBusEntityType.NotificationHub:
                NotificationHubDescription notificationHub = namespaceManager.GetNotificationHub(entityName);
                removed = notificationHub.Authorization.Remove(oldRule);
                Debug.Assert(removed);
                notificationHub.Authorization.Add(newRule);
                namespaceManager.UpdateNotificationHub(notificationHub);
                break;

            default:
                throw new Exception(string.Format(Resources.ServiceBusEntityTypeNotFound, entityType.ToString()));
            }

            return(CreateExtendedAuthorizationRule(newRule, namespaceName, entityName, entityType));
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: Azure/azure-relay
        /***********************************************************************
        * The code below is not specific to Role Based Access Control.
        * The code below can work with any Microsoft.ServiceBus.TokenProvider.
        ***********************************************************************/

        static async Task RunWcfSampleAsync(string hostAddress, string wcfRelayName, TokenProvider tokenProvider, Binding binding)
        {
            var              namespaceManager = new NamespaceManager(hostAddress, tokenProvider);
            ServiceHost      serviceHost      = null;
            RelayDescription relayDescription = null;
            bool             deleted          = false;

            UriBuilder uriBuilder = new UriBuilder(hostAddress);

            uriBuilder.Scheme = binding.Scheme;
            uriBuilder.Path   = wcfRelayName;
            Uri relayAddress = uriBuilder.Uri;

            try
            {
                // Cannot create NetOneWay and NetEvent (extends NetOneway) Relays, they must be created dynamically
                if (!(binding is NetOnewayRelayBinding))
                {
                    Console.WriteLine("Creating the WCF Relay...");
                    relayDescription = new RelayDescription(wcfRelayName, GetRelayType(binding));
                    namespaceManager.CreateRelay(relayDescription);
                    Console.WriteLine($"Created the WCF Relay: {wcfRelayName}");
                }

                Console.WriteLine("Creating and opening the listener for the WCF Relay...");
                ServiceEndpoint endpoint = null;
                if (binding is NetTcpRelayBinding || binding is BasicHttpRelayBinding || binding is WSHttpRelayBinding)
                {
                    serviceHost = new ServiceHost(typeof(MathService), relayAddress);
                    endpoint    = serviceHost.AddServiceEndpoint(typeof(IMathService), binding, string.Empty);
                }
                else if (binding is WebHttpRelayBinding)
                {
                    serviceHost = new WebServiceHost(typeof(WebHttpService), relayAddress);
                    endpoint    = serviceHost.AddServiceEndpoint(typeof(IWebRequestResponse), binding, string.Empty);
                    endpoint.Behaviors.Add(new WebHttpBehavior());
                }
                else if (binding is NetOnewayRelayBinding)
                {
                    serviceHost = new ServiceHost(new NotificationService(), relayAddress);
                    endpoint    = serviceHost.AddServiceEndpoint(typeof(INotificationService), binding, string.Empty);
                }

                endpoint.EndpointBehaviors.Add(new TransportClientEndpointBehavior(tokenProvider));
                DisableHttpHelpPage(serviceHost);
                serviceHost.Open();
                Console.WriteLine($"The listener to {wcfRelayName} was sucessfully opened");

                Console.WriteLine($"Creating and sending with a channel to WCF Relay...");
                CreateAndSendWithChannel(relayAddress, tokenProvider, binding);
                Console.WriteLine($"Created and sent with channel to WCF Relay {wcfRelayName}");

                if (!(binding is NetOnewayRelayBinding))
                {
                    Console.WriteLine("Getting the WCF Relay...");
                    await namespaceManager.GetRelayAsync(relayDescription.Path);

                    Console.WriteLine($"Got WCF Relay {relayDescription.Path}");

                    Console.WriteLine("Updating the WCF Relay...");
                    await namespaceManager.UpdateRelayAsync(relayDescription);

                    Console.WriteLine($"Updated the WCF Relay {relayDescription.Path}");

                    Console.WriteLine("Deleting the WCF Relay...");
                    await namespaceManager.DeleteRelayAsync(relayDescription.Path);

                    Console.WriteLine($"Deleted the WCF Relay {relayDescription.Path}");
                    deleted = true;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"Exception: {e}");
            }
            finally
            {
                SafeClose(serviceHost);
                if (!deleted && relayDescription != null)
                {
                    await namespaceManager.DeleteRelayAsync(relayDescription.Path);
                }
            }
        }