예제 #1
0
 public UserActionsController(ConsoleApplicationWrapper <MinecraftMessageParser> wrapper, UserRepository userRepository, UserManager <ApplicationUser> userManager, WhiteListService whiteListService, IOptions <ApplicationSettings> applicationSettings)
 {
     _wrapper             = wrapper;
     _userRepository      = userRepository;
     _userManager         = userManager;
     _whiteListService    = whiteListService;
     _applicationSettings = applicationSettings.Value;
 }
        public WhiteListConfiguration GetWindowsAzureHostedServiceWhiteListConfiguration()
        {
            // The "StealFocusForecastConfiguration.Instance.WindowsAzure.HostedService.WhiteList" property is
            // never null because it's a "Configuration Element Collection". So the best way to test if the White
            // List has been explicitly configured is if the "PollingIntervalInMinutes" is set (i.e. not "0").
            if (StealFocusForecastConfiguration.Instance.WindowsAzure.HostedService.WhiteList.PollingIntervalInMinutes > 0)
            {
                WhiteListConfiguration whiteListConfiguration = new WhiteListConfiguration();
                whiteListConfiguration.IncludeDeploymentCreateServices = StealFocusForecastConfiguration.Instance.WindowsAzure.HostedService.WhiteList.IncludeDeploymentCreateServices;
                whiteListConfiguration.IncludeDeploymentDeleteServices = StealFocusForecastConfiguration.Instance.WindowsAzure.HostedService.WhiteList.IncludeDeploymentDeleteServices;
                whiteListConfiguration.IncludeHorizontalScaleServices  = StealFocusForecastConfiguration.Instance.WindowsAzure.HostedService.WhiteList.IncludeHorizontalScaleServices;
                whiteListConfiguration.PollingIntervalInMinutes        = StealFocusForecastConfiguration.Instance.WindowsAzure.HostedService.WhiteList.PollingIntervalInMinutes;
                foreach (ServiceConfigurationElement serviceConfigurationElement in StealFocusForecastConfiguration.Instance.WindowsAzure.HostedService.WhiteList)
                {
                    WhiteListService whiteListService = new WhiteListService {
                        Name = serviceConfigurationElement.Name
                    };
                    foreach (RoleConfigurationElement roleConfigurationElement in serviceConfigurationElement.Roles)
                    {
                        WhiteListRole whiteListRole = new WhiteListRole {
                            Name = roleConfigurationElement.Name
                        };
                        if (!string.IsNullOrEmpty(roleConfigurationElement.MaxInstanceSize))
                        {
                            InstanceSize instanceSize;
                            bool         parsed = System.Enum.TryParse(roleConfigurationElement.MaxInstanceSize, true, out instanceSize);
                            if (!parsed)
                            {
                                string exceptionMessage = string.Format(CultureInfo.CurrentCulture, "The configured instance size of '{0}' could not be parsed as a valid Azure instance size. Valid values are '{1}', '{2}', '{3}', '{4}' or '{5}'.", roleConfigurationElement.MaxInstanceSize, InstanceSize.ExtraSmall, InstanceSize.Small, InstanceSize.Medium, InstanceSize.Large, InstanceSize.ExtraLarge);
                                throw new ForecastException(exceptionMessage);
                            }

                            whiteListRole.MaxInstanceSize = instanceSize;
                        }

                        if (roleConfigurationElement.MaxInstanceCount > 0)
                        {
                            whiteListRole.MaxInstanceCount = roleConfigurationElement.MaxInstanceCount;
                        }

                        whiteListService.Roles.Add(whiteListRole);
                    }

                    whiteListConfiguration.Services.Add(whiteListService);
                }

                return(whiteListConfiguration);
            }

            return(null);
        }
        public override void DoWork()
        {
            if (this.lastTimeWeDidWork.AddMinutes(this.pollingIntervalInMinutes) < DateTime.Now)
            {
                string doingWorkLogMessage = string.Format(CultureInfo.CurrentCulture, "{0} '{1}' is doing work.", this.GetType().Name, this.Id);
                Logger.Debug(doingWorkLogMessage);
                lock (SyncRoot)
                {
                    foreach (ISubscription subscription in this.subscriptions)
                    {
                        string[] hostedServiceNames = new string[0];
                        try
                        {
                            hostedServiceNames = subscription.ListHostedServices();
                        }
                        catch (Exception e)
                        {
                            string errorMessage = string.Format(CultureInfo.CurrentCulture, "{0} '{1}' experienced an error getting the hosted services for Subscription ID '{2}'. The operation will be retried after the next polling interval.", this.GetType().Name, this.Id, subscription.SubscriptionId);
                            Logger.Error(errorMessage, e);
                        }

                        foreach (string hostedServiceName in hostedServiceNames)
                        {
                            bool allowed = this.allowedServices.Any(allowedService => allowedService.Name == hostedServiceName);
                            if (allowed)
                            {
                                WhiteListService whiteListService = this.allowedServices.Single(allowedService => allowedService.Name == hostedServiceName);
                                this.CheckDeploymentAgainstItsWhiteListEntry(subscription, hostedServiceName, whiteListService, DeploymentSlot.Staging);
                                this.CheckDeploymentAgainstItsWhiteListEntry(subscription, hostedServiceName, whiteListService, DeploymentSlot.Production);
                            }
                            else
                            {
                                this.DeleteDeployedService(subscription, hostedServiceName, DeploymentSlot.Staging);
                                this.DeleteDeployedService(subscription, hostedServiceName, DeploymentSlot.Production);
                            }
                        }
                    }
                }

                this.lastTimeWeDidWork = DateTime.Now;
            }
            else
            {
                string notDoingWorkLogMessage = string.Format(CultureInfo.CurrentCulture, "{0} '{1}' is skipping doing work as we did work within the current polling window.", this.GetType().Name, this.Id);
                Logger.Debug(notDoingWorkLogMessage);
            }
        }
        public void UnitTestDoWork_With_No_Explicit_Roles_Configured_For_White_List_Service()
        {
            MockRepository   mockRepository   = new MockRepository();
            WhiteListService whiteListService = new WhiteListService {
                Name = "wls1"
            };

            WhiteListService[] whiteListServices = new[] { whiteListService };

            // Arrange
            ISubscription mockSubscription = mockRepository.StrictMock <ISubscription>();

            mockSubscription
            .Expect(s => s.ListHostedServices())
            .Repeat.Once()
            .Return(new[] { "wls1" });
            mockSubscription
            .Expect(s => s.SubscriptionId)
            .Repeat.Any()
            .Return(SubscriptionId);
            mockSubscription
            .Expect(s => s.CertificateThumbprint)
            .Repeat.Any()
            .Return(CertificateThumbprint);
            ISubscription[] subscriptions  = new[] { mockSubscription };
            IDeployment     mockDeployment = mockRepository.StrictMock <IDeployment>();
            IOperation      mockOperation  = mockRepository.StrictMock <IOperation>();

            // Act
            // There are no roles configured for the white list service, therefore there should be no checks made against
            // the service, we just accept however it is deployed for instance count or instance size.
            mockRepository.ReplayAll();
            WhiteListForecastWorker whiteListForecastWorker = new WhiteListForecastWorker(subscriptions, mockDeployment, mockOperation, whiteListServices, 1);

            whiteListForecastWorker.DoWork();
            whiteListForecastWorker.DoWork(); // Call twice to test the polling interval (nothing should be invoked the second time).

            // Assert
            mockRepository.VerifyAll();
        }
예제 #5
0
        private void OnPlayerConnectedHandler(Client client)
        {
            if (Settings.WhitelistEnabled)
            {
                if (!WhiteListService.IsClientWhitelisted(client.socialClubName))
                {
                    API.kickPlayer(client, "You are not whitelisted on this server");
                    return;
                }
            }
            Account account = AccountService.LoadAccount(client.socialClubName);

            if (account != null)
            {
                // User has an account
                if (account.Locked == 1)
                {
                    API.kickPlayer(client, "Your Account is Locked!");
                    return;
                }
                client.setData("preaccount", account);
            }
            client.dimension = -10 - API.getAllPlayers().Count;
        }
        private HorizontalScale[] FindHorizontalScaleChangesComparedToWhitelist(ISubscription subscription, string hostedServiceName, WhiteListService whiteListService, string deploymentSlot)
        {
            List <HorizontalScale> horizontalScales = new List <HorizontalScale>();

            foreach (WhiteListRole whiteListRole in whiteListService.Roles)
            {
                if (whiteListRole.MaxInstanceCount.HasValue)
                {
                    int instanceCount = this.deployment.GetInstanceCount(subscription.SubscriptionId, subscription.CertificateThumbprint, hostedServiceName, deploymentSlot, whiteListRole.Name);
                    if (instanceCount > whiteListRole.MaxInstanceCount)
                    {
                        HorizontalScale horizontalScale = new HorizontalScale {
                            InstanceCount = whiteListRole.MaxInstanceCount.Value, RoleName = whiteListRole.Name
                        };
                        horizontalScales.Add(horizontalScale);
                    }
                }
            }

            return(horizontalScales.ToArray());
        }
        private bool FindIfDeployedInstanceSizesExceedWhiteListConfiguration(ISubscription subscription, string hostedServiceName, WhiteListService whiteListService, string deploymentSlot)
        {
            if (whiteListService.Roles.Count == 0)
            {
                // No explicit roles configured, so just allow whatever is there.
                return(false);
            }

            foreach (WhiteListRole whiteListRole in whiteListService.Roles)
            {
                if (whiteListRole.MaxInstanceSize.HasValue)
                {
                    string       actualInstanceSizeValue = this.deployment.GetInstanceSize(subscription.SubscriptionId, subscription.CertificateThumbprint, hostedServiceName, deploymentSlot, whiteListRole.Name);
                    InstanceSize actualInstanceSize      = (InstanceSize)Enum.Parse(typeof(InstanceSize), actualInstanceSizeValue);
                    if (actualInstanceSize > whiteListRole.MaxInstanceSize.Value)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
        private void CheckDeploymentAgainstItsWhiteListEntry(ISubscription subscription, string hostedServiceName, WhiteListService whiteListService, string deploymentSlot)
        {
            bool deployedInstanceSizesExceedWhiteListConfiguration = this.FindIfDeployedInstanceSizesExceedWhiteListConfiguration(subscription, hostedServiceName, whiteListService, deploymentSlot);

            if (deployedInstanceSizesExceedWhiteListConfiguration)
            {
                this.DeleteDeployedService(subscription, hostedServiceName, deploymentSlot);
                return;
            }

            HorizontalScale[] horizontalScales = this.FindHorizontalScaleChangesComparedToWhitelist(subscription, hostedServiceName, whiteListService, deploymentSlot);
            if (horizontalScales.Length > 0)
            {
                this.HorizontallyScaleDeployedServiceRole(subscription, hostedServiceName, deploymentSlot, horizontalScales);
            }
        }
        public void UnitTestDoWork_With_One_Role_Having_Instance_Count_Too_High_And_Second_Role_Having_Instance_Size_Too_Large()
        {
            MockRepository   mockRepository   = new MockRepository();
            WhiteListService whiteListService = new WhiteListService {
                Name = "wls1"
            };
            WhiteListRole whiteListRole = new WhiteListRole {
                Name = "wlr1", MaxInstanceSize = InstanceSize.ExtraSmall, MaxInstanceCount = 1
            };

            whiteListService.Roles.Add(whiteListRole);
            WhiteListService[] whiteListServices = new[] { whiteListService };

            // Arrange
            ISubscription mockSubscription = mockRepository.StrictMock <ISubscription>();

            mockSubscription
            .Expect(s => s.ListHostedServices())
            .Repeat.Once()
            .Return(new[] { "bls1", "wls1" });
            mockSubscription
            .Expect(s => s.SubscriptionId)
            .Repeat.Any()
            .Return(SubscriptionId);
            mockSubscription
            .Expect(s => s.CertificateThumbprint)
            .Repeat.Any()
            .Return(CertificateThumbprint);
            ISubscription[] subscriptions  = new[] { mockSubscription };
            IDeployment     mockDeployment = mockRepository.StrictMock <IDeployment>();
            IOperation      mockOperation  = mockRepository.StrictMock <IOperation>();

            // Delete the black listed service from Staging and Production
            SetupDeleteRequestWithStatusCheck(mockDeployment, mockOperation, "bls1", DeploymentSlot.Staging, "deleteRequest1");
            SetupDeleteRequestWithStatusCheck(mockDeployment, mockOperation, "bls1", DeploymentSlot.Production, "deleteRequest2");

            // Get the instance size of White List Role 1 from Staging, all okay.
            mockDeployment
            .Expect(d => d.GetInstanceSize(SubscriptionId, CertificateThumbprint, "wls1", DeploymentSlot.Staging, "wlr1"))
            .Repeat.Once()
            .Return(InstanceSize.ExtraSmall.ToString());

            // Get the instance count of White List Role 1 from Staging, horizontally scale as a result.
            mockDeployment
            .Expect(d => d.GetInstanceCount(SubscriptionId, CertificateThumbprint, "wls1", DeploymentSlot.Staging, "wlr1"))
            .Repeat.Once()
            .Return(2);
            SetupHorizontallyScaleWithStatusCheck(mockDeployment, mockOperation, "wls1", DeploymentSlot.Staging, new[] { new HorizontalScale {
                                                                                                                             RoleName = "wlr1", InstanceCount = 1
                                                                                                                         } }, "scaleRequest1");

            // Get the instance size of the White List Role 1 from Production, delete as a result.
            mockDeployment
            .Expect(d => d.GetInstanceSize(SubscriptionId, CertificateThumbprint, "wls1", DeploymentSlot.Production, "wlr1"))
            .Repeat.Once()
            .Return(InstanceSize.ExtraLarge.ToString());
            SetupDeleteRequestWithStatusCheck(mockDeployment, mockOperation, "wls1", DeploymentSlot.Production, "deleteRequest3");

            // Act
            mockRepository.ReplayAll();
            WhiteListForecastWorker whiteListForecastWorker = new WhiteListForecastWorker(subscriptions, mockDeployment, mockOperation, whiteListServices, 1);

            whiteListForecastWorker.DoWork();
            whiteListForecastWorker.DoWork(); // Call twice to test the polling interval (nothing should be invoked the second time).

            // Assert
            mockRepository.VerifyAll();
        }
예제 #10
0
        internal static WhiteListForecastWorker GetWhiteListForecastWorker()
        {
            IConfigurationSource   configurationSource    = GetConfigurationSource();
            WhiteListConfiguration whiteListConfiguration = configurationSource.GetWindowsAzureHostedServiceWhiteListConfiguration();

            if (whiteListConfiguration != null)
            {
                SubscriptionConfiguration[] subscriptionConfigurations = configurationSource.GetAllWindowsAzureSubscriptionConfigurations();
                ISubscription[]             subscriptions = new ISubscription[subscriptionConfigurations.Length];
                for (int i = 0; i < subscriptionConfigurations.Length; i++)
                {
                    subscriptions[i] = subscriptionConfigurations[i].Convert();
                }

                if (whiteListConfiguration.IncludeDeploymentCreateServices)
                {
                    foreach (DeploymentCreateConfiguration deploymentCreateConfiguration in configurationSource.GetWindowsAzureDeploymentCreateConfigurations())
                    {
                        bool alreadyIncluded = false;
                        foreach (WhiteListService configuredWhiteListService in whiteListConfiguration.Services)
                        {
                            if (configuredWhiteListService.Name == deploymentCreateConfiguration.ServiceName)
                            {
                                alreadyIncluded = true;
                            }
                        }

                        if (!alreadyIncluded)
                        {
                            WhiteListService whiteListService = new WhiteListService {
                                Name = deploymentCreateConfiguration.ServiceName
                            };
                            whiteListConfiguration.Services.Add(whiteListService);
                        }
                    }
                }

                if (whiteListConfiguration.IncludeDeploymentDeleteServices)
                {
                    foreach (DeploymentDeleteConfiguration deploymentDeleteConfiguration in configurationSource.GetWindowsAzureDeploymentDeleteConfigurations())
                    {
                        bool alreadyIncluded = false;
                        foreach (WhiteListService configuredWhiteListService in whiteListConfiguration.Services)
                        {
                            if (configuredWhiteListService.Name == deploymentDeleteConfiguration.ServiceName)
                            {
                                alreadyIncluded = true;
                            }
                        }

                        if (!alreadyIncluded)
                        {
                            WhiteListService whiteListService = new WhiteListService {
                                Name = deploymentDeleteConfiguration.ServiceName
                            };
                            whiteListConfiguration.Services.Add(whiteListService);
                        }
                    }
                }

                if (whiteListConfiguration.IncludeHorizontalScaleServices)
                {
                    foreach (ScheduledHorizontalScaleConfiguration windowsAzureScheduledHorizontalScaleConfiguration in configurationSource.GetWindowsAzureScheduledHorizontalScaleConfigurations())
                    {
                        bool alreadyIncluded = false;
                        foreach (WhiteListService configuredWhiteListService in whiteListConfiguration.Services)
                        {
                            if (configuredWhiteListService.Name == windowsAzureScheduledHorizontalScaleConfiguration.ServiceName)
                            {
                                alreadyIncluded = true;
                            }
                        }

                        if (!alreadyIncluded)
                        {
                            WhiteListService whiteListService = new WhiteListService {
                                Name = windowsAzureScheduledHorizontalScaleConfiguration.ServiceName
                            };
                            whiteListConfiguration.Services.Add(whiteListService);
                        }
                    }
                }

                WhiteListForecastWorker whiteListForecastWorker = new WhiteListForecastWorker(
                    subscriptions,
                    new Deployment(),
                    new Operation(),
                    whiteListConfiguration.Services.ToArray(),
                    whiteListConfiguration.PollingIntervalInMinutes);
                return(whiteListForecastWorker);
            }

            return(null);
        }