コード例 #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
        public void ManagementApi_ShouldCreateGetOrDeleteHubOrRecieveConflictException()
        {
            LoadMockData();
            try
            {
                bool notificationHubExists = true;
                IEnumerable <NotificationHubDescription> notificationHubDescriptions;

                // Check that GetNotification returns MessagingEntityNotFoundException than hub does not exist
                Assert.Throws <MessagingEntityNotFoundException>(() => _namespaceManager.GetNotificationHub(_notificationHubName));

                // Check that NotificationHubExists returns false when notification hub does not exist
                notificationHubExists = _namespaceManager.NotificationHubExists(_notificationHubName);
                Assert.False(notificationHubExists);

                // Check that GetNotificationHubs returns collection without hub
                notificationHubDescriptions = _namespaceManager.GetNotificationHubs();
                Assert.DoesNotContain(notificationHubDescriptions, nhd => nhd.Path == _notificationHubName);

                // Check that CreateNotificationHub method create hub with correct Path
                var createNotificationHubDescription = _namespaceManager.CreateNotificationHub(_notificationHubName);
                Assert.Equal(_notificationHubName, createNotificationHubDescription.Path);

                // Check that NotificationHubExists return true when notification hub exist
                notificationHubExists = _namespaceManager.NotificationHubExists(_notificationHubName);
                Assert.True(notificationHubExists);

                // Check that GetNotificationHubs returns collection with existed hub
                notificationHubDescriptions = _namespaceManager.GetNotificationHubs();
                Assert.Single(notificationHubDescriptions);

                // Check that CreateNotificationHub returns MessagingEntityAlreadyExistsException than hub is alredy exist
                Assert.Throws <MessagingEntityAlreadyExistsException>(() => _namespaceManager.CreateNotificationHub(_notificationHubName));

                // Check that GetNotificationHub returns correct hub
                var getNotificationHubDescription = _namespaceManager.GetNotificationHub(_notificationHubName);
                Assert.NotNull(getNotificationHubDescription);

                // Check that UpdateNotificationHub correctly update hub
                createNotificationHubDescription.IsDisabled = true;
                var updatedNotificationHubDescription = _namespaceManager.UpdateNotificationHub(createNotificationHubDescription);
                Assert.True(updatedNotificationHubDescription.IsDisabled);

                // Check that DeleteNotificationHub correctly remove hub
                _namespaceManager.DeleteNotificationHub(_notificationHubName);

                // Check that NotificationHubExists return false when notification hub is not exist
                notificationHubExists = _namespaceManager.NotificationHubExists(_notificationHubName);
                Assert.False(notificationHubExists);

                // Check that GetNotificationHubs returns collection without not existed hub
                notificationHubDescriptions = _namespaceManager.GetNotificationHubs();
                Assert.Empty(notificationHubDescriptions);

                // Check that DeleteNotificationHub returns MessagingEntityNotFoundException than hub is not exist
                Assert.Throws <MessagingEntityNotFoundException>(() => _namespaceManager.DeleteNotificationHub(_notificationHubName));
            }
            finally
            {
                RecordTestResults();
            }
        }
コード例 #5
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));
        }